MCPcopy Create free account
hub / github.com/ElementsProject/lightning / main

Function main

devtools/lightning-checkmessage.c:30–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28}
29
30int main(int argc, char *argv[])
31{
32 u8 *u5sig, *u8sig;
33 size_t len;
34 secp256k1_ecdsa_recoverable_signature rsig;
35 struct sha256_ctx sctx = SHA256_INIT;
36 struct sha256_double shad;
37 struct pubkey reckey;
38 const char *keystr;
39
40 setup_locale();
41 err_set_progname(argv[0]);
42 wally_init(0);
43 secp256k1_ctx = wally_get_secp_context();
44
45 if (argc != 3 && argc != 4)
46 usage();
47
48 u5sig = tal_arr(NULL, u8, strlen(argv[2]));
49 for (size_t i = 0; argv[2][i]; i++) {
50 u5sig[i] = zbase32_revchars[(unsigned char)argv[2][i]];
51 if (u5sig[i] > 31)
52 errx(1, "Not a valid zbase32 string");
53 }
54 u8sig = tal_arr(NULL, u8, (strlen(argv[2]) * 5 + 7) / 8 + 1);
55 len = 0;
56 if (!bech32_convert_bits(u8sig, &len, 8, u5sig, strlen(argv[2]), 5, false))
57 errx(1, "Invalid string");
58
59 if (len != 65)
60 errx(1, "Signature too %s", len < 65 ? "short" : "long");
61
62 if (!secp256k1_ecdsa_recoverable_signature_parse_compact(secp256k1_ctx,
63 &rsig,
64 u8sig + 1,
65 u8sig[0] - 31))
66 errx(1, "Signature not parsable");
67
68 sha256_update(&sctx, "Lightning Signed Message:",
69 strlen("Lightning Signed Message:"));
70 sha256_update(&sctx, argv[1], strlen(argv[1]));
71 sha256_double_done(&sctx, &shad);
72
73 if (!secp256k1_ecdsa_recover(secp256k1_ctx, &reckey.pubkey, &rsig,
74 shad.sha.u.u8))
75 errx(1, "Signature not recoverable");
76
77 keystr = fmt_pubkey(NULL, &reckey);
78 if (argv[3]) {
79 if (!streq(keystr, argv[3]))
80 errx(1, "Signature is invalid");
81 printf("Signature is valid!\n");
82 } else
83 printf("Signature claims to be from key %s\n", keystr);
84 return 0;
85}

Callers

nothing calls this directly

Calls 8

setup_localeFunction · 0.85
err_set_prognameFunction · 0.85
errxFunction · 0.85
bech32_convert_bitsFunction · 0.85
sha256_updateFunction · 0.85
sha256_double_doneFunction · 0.85
fmt_pubkeyFunction · 0.85
usageFunction · 0.70

Tested by

no test coverage detected