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

Function json_signmessage

lightningd/signmessage.c:57–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55}
56
57static struct command_result *json_signmessage(struct command *cmd,
58 const char *buffer,
59 const jsmntok_t *obj UNNEEDED,
60 const jsmntok_t *params)
61{
62 const char *message;
63 secp256k1_ecdsa_recoverable_signature rsig;
64 struct json_stream *response;
65 u8 sig[65];
66 const u8 *msg;
67 int recid;
68
69 if (!param_check(cmd, buffer, params,
70 p_req("message", param_string, &message),
71 NULL))
72 return command_param_failed();
73
74 if (strlen(message) > 65535)
75 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
76 "Message must be < 64k");
77
78 if (command_check_only(cmd))
79 return command_check_done(cmd);
80
81 msg = towire_hsmd_sign_message(NULL,
82 tal_dup_arr(tmpctx, u8, (u8 *)message,
83 strlen(message), 0));
84 msg = hsm_sync_req(tmpctx, cmd->ld, take(msg));
85 if (!fromwire_hsmd_sign_message_reply(msg, &rsig))
86 fatal("HSM gave bad hsm_sign_message_reply %s",
87 tal_hex(msg, msg));
88
89 secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_ctx,
90 sig+1, &recid,
91 &rsig);
92 response = json_stream_success(cmd);
93 json_add_hex(response, "signature", sig+1, sizeof(sig)-1);
94 sig[0] = recid;
95 json_add_hex(response, "recid", sig, 1);
96
97 /* From https://twitter.com/rusty_twit/status/1182102005914800128:
98 * @roasbeef & @bitconner point out that #lnd algo is:
99 * zbase32(SigRec(SHA256(SHA256("Lightning Signed Message:" + msg)))).
100 * zbase32 from https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt
101 * and SigRec has first byte 31 + recovery id, followed by 64 byte sig.
102 * #specinatweet */
103 sig[0] += 31;
104 json_add_string(response, "zbase",
105 to_zbase32(response, sig, sizeof(sig)));
106 return command_success(cmd, response);
107}
108
109static const struct json_command json_signmessage_cmd = {
110 "signmessage",

Callers

nothing calls this directly

Calls 13

param_checkFunction · 0.85
tal_hexFunction · 0.85
json_add_hexFunction · 0.85
to_zbase32Function · 0.85
command_param_failedFunction · 0.70
command_failFunction · 0.70
command_check_onlyFunction · 0.70
command_check_doneFunction · 0.70
hsm_sync_reqFunction · 0.70
fatalFunction · 0.70
json_stream_successFunction · 0.70
command_successFunction · 0.70

Tested by

no test coverage detected