xpubaddrgen.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <inttypes.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "bip32.h"
  6. #include "curves.h"
  7. #include "ecdsa.h"
  8. #define VERSION_PUBLIC 0x0488b21e
  9. void process_job(uint32_t jobid, const char *xpub, uint32_t change,
  10. uint32_t from, uint32_t to) {
  11. HDNode node, child;
  12. if (change > 1 || to <= from ||
  13. hdnode_deserialize_public(xpub, VERSION_PUBLIC, SECP256K1_NAME, &node,
  14. NULL) != 0) {
  15. printf("%d error\n", jobid);
  16. return;
  17. }
  18. hdnode_public_ckd(&node, change);
  19. uint32_t i;
  20. char address[36];
  21. for (i = from; i < to; i++) {
  22. memcpy(&child, &node, sizeof(HDNode));
  23. hdnode_public_ckd(&child, i);
  24. ecdsa_get_address(child.public_key, 0, HASHER_SHA2, HASHER_SHA2D, address,
  25. sizeof(address));
  26. printf("%d %d %s\n", jobid, i, address);
  27. }
  28. }
  29. int main(void) {
  30. char line[1024], xpub[1024];
  31. uint32_t jobid, change, from, to;
  32. int r;
  33. for (;;) {
  34. if (!fgets(line, sizeof(line), stdin)) break;
  35. r = sscanf(line, "%u %s %u %u %u\n", &jobid, xpub, &change, &from, &to);
  36. if (r < 1) {
  37. printf("error\n");
  38. } else if (r != 5) {
  39. printf("%d error\n", jobid);
  40. } else {
  41. process_job(jobid, xpub, change, from, to);
  42. }
  43. }
  44. return 0;
  45. }