|
| 1 | +#include "config.h" |
| 2 | +#include <bitcoin/pubkey.h> |
| 3 | +#include <bitcoin/shadouble.h> |
| 4 | +#include <bitcoin/varint.h> |
| 5 | +#include <ccan/err/err.h> |
| 6 | +#include <common/bech32.h> |
| 7 | +#include <common/utils.h> |
| 8 | +#include <secp256k1_recovery.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <wally_core.h> |
| 11 | + |
| 12 | +/* FIXME: should we check signatures against addresses instead of keys? */ |
| 13 | +static void usage(void) |
| 14 | +{ |
| 15 | + fprintf(stderr, "Usage: bip137-checkmessage message hex-sig [key]\n" |
| 16 | + "If key does not match, signature is not valid!\n"); |
| 17 | + exit(1); |
| 18 | +} |
| 19 | + |
| 20 | +int main(int argc, char *argv[]) |
| 21 | +{ |
| 22 | + u8 *sig; |
| 23 | + u8 varint[VARINT_MAX_LEN]; |
| 24 | + size_t varintlen, msg_len; |
| 25 | + secp256k1_ecdsa_recoverable_signature rsig; |
| 26 | + struct sha256_ctx sctx = SHA256_INIT; |
| 27 | + struct sha256_double shad; |
| 28 | + struct pubkey reckey; |
| 29 | + const char *keystr; |
| 30 | + |
| 31 | + setup_locale(); |
| 32 | + err_set_progname(argv[0]); |
| 33 | + wally_init(0); |
| 34 | + secp256k1_ctx = wally_get_secp_context(); |
| 35 | + |
| 36 | + if (argc != 3 && argc != 4) |
| 37 | + usage(); |
| 38 | + |
| 39 | + sig = tal_hexdata(NULL, argv[2], strlen(argv[2])); |
| 40 | + if (!sig) |
| 41 | + errx(1, "Not a valid hex string"); |
| 42 | + |
| 43 | + if (sig[0] < 39 || sig[0] >= 43) |
| 44 | + errx(1, |
| 45 | + "Signature header does not correspond to a P2WPKH type"); |
| 46 | + |
| 47 | + if (!secp256k1_ecdsa_recoverable_signature_parse_compact( |
| 48 | + secp256k1_ctx, &rsig, sig + 1, sig[0] - 39)) |
| 49 | + errx(1, "Signature not parsable"); |
| 50 | + |
| 51 | + sha256_update(&sctx, |
| 52 | + "\x18" |
| 53 | + "Bitcoin Signed Message:\n", |
| 54 | + strlen("\x18" |
| 55 | + "Bitcoin Signed Message:\n")); |
| 56 | + msg_len = strlen(argv[1]); |
| 57 | + varintlen = varint_put(varint, msg_len); |
| 58 | + sha256_update(&sctx, varint, varintlen); |
| 59 | + sha256_update(&sctx, argv[1], msg_len); |
| 60 | + sha256_double_done(&sctx, &shad); |
| 61 | + |
| 62 | + if (!secp256k1_ecdsa_recover(secp256k1_ctx, &reckey.pubkey, &rsig, |
| 63 | + shad.sha.u.u8)) |
| 64 | + errx(1, "Signature not recoverable"); |
| 65 | + |
| 66 | + keystr = fmt_pubkey(NULL, &reckey); |
| 67 | + if (argv[3]) { |
| 68 | + if (!streq(keystr, argv[3])) |
| 69 | + errx(1, "Signature is invalid"); |
| 70 | + printf("Signature is valid!\n"); |
| 71 | + } else |
| 72 | + printf("Signature claims to be from key %s\n", keystr); |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
0 commit comments