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

Function json_signmessage

lightningd/signmessage.c:60–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 12

tal_hexFunction · 0.85
json_add_hexFunction · 0.85
to_zbase32Function · 0.85
command_param_failedFunction · 0.70
command_failFunction · 0.70
fatalFunction · 0.70
json_stream_successFunction · 0.70
command_successFunction · 0.70
paramFunction · 0.50
wire_sync_writeFunction · 0.50
wire_sync_readFunction · 0.50
json_add_stringFunction · 0.50

Tested by

no test coverage detected