|
| 1 | +#include "config.h" |
| 2 | +#include <assert.h> |
| 3 | +#include <bitcoin/base58.h> |
| 4 | +#include <bitcoin/pubkey.h> |
| 5 | +#include <bitcoin/shadouble.h> |
| 6 | +#include <bitcoin/varint.h> |
| 7 | +#include <ccan/err/err.h> |
| 8 | +#include <common/addr.h> |
| 9 | +#include <common/bech32.h> |
| 10 | +#include <common/utils.h> |
| 11 | +#include <secp256k1_recovery.h> |
| 12 | +#include <stdio.h> |
| 13 | +#include <wally_core.h> |
| 14 | + |
| 15 | +static void usage(void) |
| 16 | +{ |
| 17 | + fprintf(stderr, |
| 18 | + "Usage: bip137-verifysignature message hex-sig [address] [network]\n" |
| 19 | + "If key does not match, signature is not valid!\n"); |
| 20 | + exit(1); |
| 21 | +} |
| 22 | + |
| 23 | +static char *encode_pubkey_to_p2wpkh_addr(const tal_t *ctx, |
| 24 | + const struct pubkey *pubkey, |
| 25 | + const struct chainparams *chain) |
| 26 | +{ |
| 27 | + char *out; |
| 28 | + const char *hrp; |
| 29 | + struct ripemd160 h160; |
| 30 | + bool ok; |
| 31 | + hrp = chain->onchain_hrp; |
| 32 | + |
| 33 | + /* out buffer is 73 + strlen(human readable part), |
| 34 | + * see common/bech32.h*/ |
| 35 | + out = tal_arr(ctx, char, 73 + strlen(hrp)); |
| 36 | + pubkey_to_hash160(pubkey, &h160); |
| 37 | + ok = segwit_addr_encode(out, hrp, 0, h160.u.u8, sizeof(h160)); |
| 38 | + if(!ok) |
| 39 | + tal_free(out); |
| 40 | + return out; |
| 41 | +} |
| 42 | + |
| 43 | +int main(int argc, char *argv[]) |
| 44 | +{ |
| 45 | + u8 *sig; |
| 46 | + u8 varint[VARINT_MAX_LEN]; |
| 47 | + size_t varintlen, msg_len; |
| 48 | + secp256k1_ecdsa_recoverable_signature rsig; |
| 49 | + struct sha256_ctx sctx = SHA256_INIT; |
| 50 | + struct sha256_double shad; |
| 51 | + struct pubkey reckey; |
| 52 | + const char *addr; |
| 53 | + const struct chainparams *chain = NULL; |
| 54 | + const char *input_chain = NULL, *input_address = NULL; |
| 55 | + |
| 56 | + setup_locale(); |
| 57 | + err_set_progname(argv[0]); |
| 58 | + wally_init(0); |
| 59 | + secp256k1_ctx = wally_get_secp_context(); |
| 60 | + |
| 61 | + if (argc != 3 && argc != 4 && argc != 5) |
| 62 | + usage(); |
| 63 | + if (argc > 3) |
| 64 | + input_address = argv[3]; |
| 65 | + if (argc > 4) |
| 66 | + input_chain = argv[4]; |
| 67 | + |
| 68 | + sig = tal_hexdata(NULL, argv[2], strlen(argv[2])); |
| 69 | + if (!sig) |
| 70 | + errx(1, "Not a valid hex string"); |
| 71 | + |
| 72 | + if (sig[0] < 39 || sig[0] >= 43) |
| 73 | + errx(1, |
| 74 | + "Signature header does not correspond to a P2WPKH type"); |
| 75 | + |
| 76 | + if (!secp256k1_ecdsa_recoverable_signature_parse_compact( |
| 77 | + secp256k1_ctx, &rsig, sig + 1, sig[0] - 39)) |
| 78 | + errx(1, "Signature not parsable"); |
| 79 | + |
| 80 | + sha256_update(&sctx, |
| 81 | + "\x18" |
| 82 | + "Bitcoin Signed Message:\n", |
| 83 | + strlen("\x18" |
| 84 | + "Bitcoin Signed Message:\n")); |
| 85 | + msg_len = strlen(argv[1]); |
| 86 | + varintlen = varint_put(varint, msg_len); |
| 87 | + sha256_update(&sctx, varint, varintlen); |
| 88 | + sha256_update(&sctx, argv[1], msg_len); |
| 89 | + sha256_double_done(&sctx, &shad); |
| 90 | + |
| 91 | + if (!secp256k1_ecdsa_recover(secp256k1_ctx, &reckey.pubkey, &rsig, |
| 92 | + shad.sha.u.u8)) |
| 93 | + errx(1, "Signature not recoverable"); |
| 94 | + |
| 95 | + if (input_chain) { |
| 96 | + chain = chainparams_for_network(input_chain); |
| 97 | + if (!chain) |
| 98 | + errx(1, "Invalid network"); |
| 99 | + } else { |
| 100 | + /* By default, assume we are verifying a mainnet signature. */ |
| 101 | + chain = chainparams_for_network("bitcoin"); |
| 102 | + } |
| 103 | + addr = encode_pubkey_to_p2wpkh_addr(NULL, &reckey, chain); |
| 104 | + if (!addr) |
| 105 | + errx(1, "Failed to derive address from recovered key"); |
| 106 | + if (input_address) { |
| 107 | + if (!streq(addr, input_address)) |
| 108 | + errx(1, "Signature is invalid"); |
| 109 | + printf("Signature is valid!\n"); |
| 110 | + } else |
| 111 | + printf("Signature claims to be from address %s\n", addr); |
| 112 | + return 0; |
| 113 | +} |
| 114 | + |
0 commit comments