From 280310c8a01084667d4f5c431c6cf3fc365b993b Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 24 Dec 2014 01:26:39 +0100 Subject: [PATCH] add xpubaddrgen utility --- tools/.gitignore | 1 + tools/Makefile | 37 +++++++++++++++++++++++++++++++++++++ tools/README | 36 ++++++++++++++++++++++++++++++++++++ tools/xpubaddrgen.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 tools/.gitignore create mode 100644 tools/Makefile create mode 100644 tools/README create mode 100644 tools/xpubaddrgen.c diff --git a/tools/.gitignore b/tools/.gitignore new file mode 100644 index 0000000000..ac00e6bcd2 --- /dev/null +++ b/tools/.gitignore @@ -0,0 +1 @@ +xpubaddrgen diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 0000000000..67c652637f --- /dev/null +++ b/tools/Makefile @@ -0,0 +1,37 @@ +CC = gcc + +OPTFLAGS = -O3 + +CFLAGS += $(OPTFLAGS) \ + -W \ + -Wall \ + -Wextra \ + -Wimplicit-function-declaration \ + -Wredundant-decls \ + -Wstrict-prototypes \ + -Wundef \ + -Wshadow \ + -Wpointer-arith \ + -Wformat \ + -Wreturn-type \ + -Wsign-compare \ + -Wmultichar \ + -Wformat-nonliteral \ + -Winit-self \ + -Wuninitialized \ + -Wformat-security \ + -Werror \ + -I.. + +all: xpubaddrgen + +OBJS = ../bip32.o ../ecdsa.o ../sha2.o ../bignum.o ../base58.o ../secp256k1.o ../ripemd160.o ../hmac.o ../rand.o + +%.o: %.c %.h options.h + $(CC) $(CFLAGS) -o $@ -c $< + +xpubaddrgen: xpubaddrgen.o $(OBJS) + $(CC) xpubaddrgen.o $(OBJS) -o xpubaddrgen + +clean: + rm -f *.o xpubaddrgen diff --git a/tools/README b/tools/README new file mode 100644 index 0000000000..c401ed873f --- /dev/null +++ b/tools/README @@ -0,0 +1,36 @@ +trezor-crypto tools +=================== + +Set of small utilities using the trezor-crypto library. + +xpubaddrgen +----------- + +xpubaddrgen reads job specification from stdin in format: + + + +and prints the results to stdout in format: + +
+ +Example input: + +23 xpub6BcjTvRCYD4VvFQ8whztSXhbNyhS56eTd5P3g9Zvd3zPEeUeL5CUqBYX8NSd1b6Thitr8bZcSnesmXZH7KerMcc4tUkenBShYCtQ1L8ebVe 0 0 5 +42 xpub6AT2YrLinU4Be5UWUxMaUz3zTA99CSGvXt1jt2Lgym8PqXbTzmpQ8MHjoLnx8YJiMMUP5iEfR97YQVmgF6B2tAhbCZrXqn65ur526NkZ6ey 1 1000 1005 + +Example output: + +23 0 14vb5Cws75p2i5rmSiF5CKMyezUX4hxSb9 +23 1 1Lf4ciA36dsi1niF6smVcpCiHcpj2skaPq +23 2 1LraByp7gQAipvHnFS1gTSzixBtYaVyQGp +23 3 1Hy6n56qZj1EefLVfDAeEpmveNteY9jpiG +23 4 183Nn4mrUjPizM3xu8C6SrmViaWrk8YyRS +42 1000 12eAFGAqGUtszc9R7euRqk7DUcQNXvQZSg +42 1001 1BrLbFCD3MNYedJaz92U9iqy9ukHrtQ1A6 +42 1002 1Jhv33bJy229ThM7HKxUa92cMK5gi7DyPC +42 1003 13LxbTjQPByisj4F4sZEivUBdnJwigzg6R +42 1004 1BWBpSWkPwcKxVr2WDyUqQbmvk5SGihcx9 + +It will print " error" when there was an error processing job . +It will print "error" when it encountered a malformed line. diff --git a/tools/xpubaddrgen.c b/tools/xpubaddrgen.c new file mode 100644 index 0000000000..9830d6fa4d --- /dev/null +++ b/tools/xpubaddrgen.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include + +void process_job(uint32_t jobid, const char *xpub, uint32_t change, uint32_t from, uint32_t to) +{ + HDNode node, child; + if (change > 1 || to <= from || hdnode_deserialize(xpub, &node) != 0) { + printf("%d error\n", jobid); + return; + } + hdnode_public_ckd(&node, change); + uint32_t i; + char address[36]; + for (i = from; i < to; i++) { + memcpy(&child, &node, sizeof(HDNode)); + hdnode_public_ckd(&child, i); + ecdsa_get_address(child.public_key, 0, address, sizeof(address)); + printf("%d %d %s\n", jobid, i, address); + } +} + +int main(void) +{ + char line[1024], xpub[1024]; + uint32_t jobid, change, from, to; + int r; + for (;;) { + fgets(line, sizeof(line), stdin); + r = sscanf(line, "%u %s %u %u %u\n", &jobid, xpub, &change, &from, &to); + if (r < 1) { + printf("error\n"); + } else if (r != 5) { + printf("%d error\n", jobid); + } else { + process_job(jobid, xpub, change, from, to); + } + } + return 0; +}