1 #include <bitcoin/address.h>
2 #include <bitcoin/base58.h>
3 #include <bitcoin/privkey.h>
4 #include <bitcoin/script.h>
5 #include <ccan/err/err.h>
6 #include <ccan/opt/opt.h>
7 #include <ccan/tal/str/str.h>
8 #include <ccan/time/time.h>
9 #include <common/bech32.h>
10 #include <common/bolt11.h>
11 #include <common/features.h>
12 #include <common/setup.h>
13 #include <common/type_to_string.h>
14 #include <common/version.h>
15 #include <inttypes.h>
16 #include <stdio.h>
17 
18 #define NO_ERROR 0
19 #define ERROR_BAD_DECODE 1
20 #define ERROR_USAGE 3
21 
22 /* Tal wrappers for opt. */
opt_allocfn(size_t size)23 static void *opt_allocfn(size_t size)
24 {
25 	return tal_arr_label(NULL, char, size, TAL_LABEL("opt_allocfn", ""));
26 }
27 
tal_reallocfn(void * ptr,size_t size)28 static void *tal_reallocfn(void *ptr, size_t size)
29 {
30 	if (!ptr)
31 		return opt_allocfn(size);
32 	tal_resize_(&ptr, 1, size, false);
33 	return ptr;
34 }
35 
tal_freefn(void * ptr)36 static void tal_freefn(void *ptr)
37 {
38 	tal_free(ptr);
39 }
40 
fmt_time(const tal_t * ctx,u64 time)41 static char *fmt_time(const tal_t *ctx, u64 time)
42 {
43 	/* ctime is not sane.  Take pointer, returns \n in string. */
44 	time_t t = time;
45 	const char *p = ctime(&t);
46 
47 	return tal_fmt(ctx, "%.*s", (int)strcspn(p, "\n"), p);
48 }
49 
main(int argc,char * argv[])50 int main(int argc, char *argv[])
51 {
52 	const tal_t *ctx = tal(NULL, char);
53 	const char *method;
54 	struct bolt11 *b11;
55 	struct bolt11_field *extra;
56 	size_t i;
57 	char *fail, *description = NULL;
58 
59 	common_setup(argv[0]);
60 
61 	opt_set_alloc(opt_allocfn, tal_reallocfn, tal_freefn);
62 	opt_register_noarg("--help|-h", opt_usage_and_exit,
63 			   "<decode> <bolt11>", "Show this message");
64 	opt_register_arg("--hashed-description", opt_set_charp, opt_show_charp,
65 			 &description,
66 			 "Description to check hashed description against");
67 	opt_register_version();
68 
69 	opt_early_parse(argc, argv, opt_log_stderr_exit);
70 	opt_parse(&argc, argv, opt_log_stderr_exit);
71 
72 	method = argv[1];
73 	if (!method)
74 		errx(ERROR_USAGE, "Need at least one argument\n%s",
75 		     opt_usage(argv[0], NULL));
76 
77 	if (!streq(method, "decode"))
78 		errx(ERROR_USAGE, "Need decode argument\n%s",
79 		     opt_usage(argv[0], NULL));
80 
81 	if (!argv[2])
82 		errx(ERROR_USAGE, "Need argument\n%s",
83 		     opt_usage(argv[0], NULL));
84 
85 	b11 = bolt11_decode(ctx, argv[2], NULL, description, NULL, &fail);
86 	if (!b11)
87 		errx(ERROR_BAD_DECODE, "%s", fail);
88 
89 	printf("currency: %s\n", b11->chain->bip173_name);
90 	printf("timestamp: %"PRIu64" (%s)\n",
91 	       b11->timestamp, fmt_time(ctx, b11->timestamp));
92 	printf("expiry: %"PRIu64" (%s)\n",
93 	       b11->expiry, fmt_time(ctx, b11->timestamp + b11->expiry));
94 	printf("payee: %s\n",
95 	       type_to_string(ctx, struct node_id, &b11->receiver_id));
96 	printf("payment_hash: %s\n",
97 	       tal_hexstr(ctx, &b11->payment_hash, sizeof(b11->payment_hash)));
98 	printf("min_final_cltv_expiry: %u\n", b11->min_final_cltv_expiry);
99         if (b11->msat) {
100 		printf("msatoshi: %"PRIu64"\n", b11->msat->millisatoshis); /* Raw: raw int for backwards compat */
101 		printf("amount_msat: %s\n",
102 		       type_to_string(tmpctx, struct amount_msat, b11->msat));
103 	}
104         if (b11->description)
105                 printf("description: '%s'\n", b11->description);
106         if (b11->description_hash)
107 		printf("description_hash: %s\n",
108 		       tal_hexstr(ctx, b11->description_hash,
109 				  sizeof(*b11->description_hash)));
110 	if (b11->payment_secret)
111 		printf("payment_secret: %s\n",
112 		       tal_hexstr(ctx, b11->payment_secret,
113 				  sizeof(*b11->payment_secret)));
114 	if (tal_bytelen(b11->features)) {
115 		printf("features:");
116 		for (size_t i = 0; i < tal_bytelen(b11->features) * CHAR_BIT; i++) {
117 			if (feature_is_set(b11->features, i))
118 				printf(" %zu", i);
119 		}
120 		printf("\n");
121 	}
122 	for (i = 0; i < tal_count(b11->fallbacks); i++) {
123                 struct bitcoin_address pkh;
124                 struct ripemd160 sh;
125                 struct sha256 wsh;
126 
127 		printf("fallback: %s\n", tal_hex(ctx, b11->fallbacks[i]));
128                 if (is_p2pkh(b11->fallbacks[i], &pkh)) {
129 			printf("fallback-P2PKH: %s\n",
130 			       bitcoin_to_base58(ctx, b11->chain,
131 						 &pkh));
132                 } else if (is_p2sh(b11->fallbacks[i], &sh)) {
133 			printf("fallback-P2SH: %s\n",
134 			       p2sh_to_base58(ctx,
135 					      b11->chain,
136 					      &sh));
137                 } else if (is_p2wpkh(b11->fallbacks[i], &pkh)) {
138                         char out[73 + strlen(b11->chain->bip173_name)];
139                         if (segwit_addr_encode(out, b11->chain->bip173_name, 0,
140                                                (const u8 *)&pkh, sizeof(pkh)))
141 				printf("fallback-P2WPKH: %s\n", out);
142                 } else if (is_p2wsh(b11->fallbacks[i], &wsh)) {
143                         char out[73 + strlen(b11->chain->bip173_name)];
144                         if (segwit_addr_encode(out, b11->chain->bip173_name, 0,
145                                                (const u8 *)&wsh, sizeof(wsh)))
146 				printf("fallback-P2WSH: %s\n", out);
147                 }
148         }
149 
150 	for (i = 0; i < tal_count(b11->routes); i++) {
151 		printf("route: (node/chanid/fee/expirydelta) ");
152 		for (size_t n = 0; n < tal_count(b11->routes[i]); n++) {
153 			printf(" %s/%s/%u/%u/%u",
154 			       type_to_string(ctx, struct node_id,
155 					      &b11->routes[i][n].pubkey),
156 			       type_to_string(ctx, struct short_channel_id,
157 					      &b11->routes[i][n].short_channel_id),
158 			       b11->routes[i][n].fee_base_msat,
159 			       b11->routes[i][n].fee_proportional_millionths,
160 			       b11->routes[i][n].cltv_expiry_delta);
161 		}
162 		printf("\n");
163 	}
164 
165 	list_for_each(&b11->extra_fields, extra, list) {
166 		char *data = tal_arr(ctx, char, tal_count(extra->data)+1);
167 
168 		for (i = 0; i < tal_count(extra->data); i++)
169 			data[i] = bech32_charset[extra->data[i]];
170 
171 		data[i] = '\0';
172 		printf("unknown: %c %s\n", extra->tag, data);
173 	}
174 
175 	printf("signature: %s\n",
176 	       type_to_string(ctx, secp256k1_ecdsa_signature, &b11->sig));
177 	tal_free(ctx);
178 	common_shutdown();
179 	return NO_ERROR;
180 }
181