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

Function json_checkmessage

lightningd/signmessage.c:151–235  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

149}
150
151static struct command_result *json_checkmessage(struct command *cmd,
152 const char *buffer,
153 const jsmntok_t *obj UNNEEDED,
154 const jsmntok_t *params)
155{
156 struct pubkey *pubkey, reckey;
157 const u8 *u8sig;
158 const char *message, *zb;
159 secp256k1_ecdsa_recoverable_signature rsig;
160 struct sha256_ctx sctx = SHA256_INIT;
161 struct sha256_double shad;
162 struct json_stream *response;
163
164 if (!param(cmd, buffer, params,
165 p_req("message", param_string, &message),
166 p_req("zbase", param_string, &zb),
167 p_opt("pubkey", param_pubkey, &pubkey),
168 NULL))
169 return command_param_failed();
170
171 u8sig = from_zbase32(tmpctx, zb);
172 if (!u8sig)
173 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
174 "zbase is not valid zbase32");
175
176 if (tal_bytelen(u8sig) != 65)
177 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
178 "zbase is too %s",
179 tal_bytelen(u8sig) < 65 ? "short" : "long");
180
181 if (!secp256k1_ecdsa_recoverable_signature_parse_compact(secp256k1_ctx,
182 &rsig,
183 u8sig + 1,
184 u8sig[0] - 31))
185 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
186 "cannot parse zbase signature");
187
188 sha256_update(&sctx, "Lightning Signed Message:",
189 strlen("Lightning Signed Message:"));
190 sha256_update(&sctx, message, strlen(message));
191 sha256_double_done(&sctx, &shad);
192
193 if (!secp256k1_ecdsa_recover(secp256k1_ctx, &reckey.pubkey, &rsig,
194 shad.sha.u.u8)) {
195 response = json_stream_success(cmd);
196 json_add_bool(response, "verified", false);
197 return command_success(cmd, response);
198 }
199
200 /* If they didn't specify pubkey, we only accept the signature if it's
201 * in the graph (thus, they've signed something with it). This idea
202 * was stolen directly from lnd, thanks @roasbeef.
203 *
204 * FIXME: We could also look through known invoices: AFAICT you can't
205 * make two (different) signed messages with the same recovered key
206 * unless you know the secret key */
207 if (!pubkey) {
208 struct jsonrpc_request *req;

Callers

nothing calls this directly

Calls 15

from_zbase32Function · 0.85
tal_bytelenFunction · 0.85
sha256_updateFunction · 0.85
sha256_double_doneFunction · 0.85
node_id_from_pubkeyFunction · 0.85
json_add_pubkeyFunction · 0.85
command_param_failedFunction · 0.70
command_failFunction · 0.70
json_stream_successFunction · 0.70
command_successFunction · 0.70
command_logFunction · 0.70
jsonrpc_request_endFunction · 0.70

Tested by

no test coverage detected