Skip to content

Commit 437d32d

Browse files
committed
add signmessagewithkey RPC
signmessagewithkey: allows to sign a message with a key associated with one bitcoin address in our wallet. Signed-off-by: Lagrang3 <[email protected]>
1 parent bed4e79 commit 437d32d

File tree

3 files changed

+305
-0
lines changed

3 files changed

+305
-0
lines changed

Diff for: contrib/msggen/msggen/schema.json

+89
Original file line numberDiff line numberDiff line change
@@ -32794,6 +32794,95 @@
3279432794
}
3279532795
]
3279632796
},
32797+
"lightning-signmessagewithkey.json": {
32798+
"$schema": "../rpc-schema-draft.json",
32799+
"type": "object",
32800+
"rpc": "signmessagewithkey",
32801+
"title": "Command to create a signature using a key from the wallet",
32802+
"description": [
32803+
"The **signmessagewithkey** RPC command creates a digital signature of *message* using the key associated with the address provided in the input.",
32804+
"The signature scheme follows the same convention as electrum wallet."
32805+
],
32806+
"added": "v25.05",
32807+
"request": {
32808+
"required": [
32809+
"message",
32810+
"address"
32811+
],
32812+
"additionalProperties": false,
32813+
"properties": {
32814+
"message": {
32815+
"type": "string",
32816+
"description": [
32817+
"Less than 65536 characters long message to be signed by the node."
32818+
]
32819+
},
32820+
"address": {
32821+
"type": "string",
32822+
"description": [
32823+
"A Bitcoin accepted type, including a bech32, address for lookup in the list of addresses issued to date."
32824+
]
32825+
}
32826+
}
32827+
},
32828+
"response": {
32829+
"required": [
32830+
"keyidx",
32831+
"pubkey",
32832+
"signature",
32833+
"base64"
32834+
],
32835+
"additionalProperties": false,
32836+
"properties": {
32837+
"keyidx": {
32838+
"type": "u64",
32839+
"description": [
32840+
"The key index of the address requested."
32841+
]
32842+
},
32843+
"pubkey": {
32844+
"type": "pubkey",
32845+
"description": [
32846+
"The public key associated with the bitcoin address provided."
32847+
]
32848+
},
32849+
"signature": {
32850+
"type": "hex",
32851+
"description": [
32852+
"The signature."
32853+
]
32854+
},
32855+
"base64": {
32856+
"type": "string",
32857+
"description": [
32858+
"The signature encoded in base64."
32859+
]
32860+
},
32861+
"bech32": {
32862+
"type": "string",
32863+
"description": [
32864+
"The bech32 (native segwit) address."
32865+
]
32866+
},
32867+
"p2tr": {
32868+
"type": "string",
32869+
"description": [
32870+
"The taproot address."
32871+
]
32872+
}
32873+
}
32874+
},
32875+
"author": [
32876+
"Lagrang3 <<[email protected]>> is mainly responsible."
32877+
],
32878+
"see_also": [
32879+
"lightning-checkmessagewithkey(7)"
32880+
],
32881+
"resources": [
32882+
"Main web site: <https://github.com/ElementsProject/lightning>"
32883+
],
32884+
"examples": []
32885+
},
3279732886
"lightning-signpsbt.json": {
3279832887
"$schema": "../rpc-schema-draft.json",
3279932888
"type": "object",

Diff for: doc/schemas/lightning-signmessagewithkey.json

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"$schema": "../rpc-schema-draft.json",
3+
"type": "object",
4+
"rpc": "signmessagewithkey",
5+
"title": "Command to create a signature using a key from the wallet",
6+
"description": [
7+
"The **signmessagewithkey** RPC command creates a digital signature of *message* using the key associated with the address provided in the input.",
8+
"The signature scheme follows the same convention as electrum wallet."
9+
],
10+
"added": "v25.05",
11+
"request": {
12+
"required": [
13+
"message",
14+
"address"
15+
],
16+
"additionalProperties": false,
17+
"properties": {
18+
"message": {
19+
"type": "string",
20+
"description": [
21+
"Less than 65536 characters long message to be signed by the node."
22+
]
23+
},
24+
"address": {
25+
"type": "string",
26+
"description": [
27+
"A Bitcoin accepted type, including a bech32, address for lookup in the list of addresses issued to date."
28+
]
29+
}
30+
}
31+
},
32+
"response": {
33+
"required": [
34+
"keyidx",
35+
"pubkey",
36+
"signature",
37+
"base64"
38+
],
39+
"additionalProperties": false,
40+
"properties": {
41+
"keyidx": {
42+
"type": "u64",
43+
"description": [
44+
"The key index of the address requested."
45+
]
46+
},
47+
"pubkey": {
48+
"type": "pubkey",
49+
"description": [
50+
"The public key associated with the bitcoin address provided."
51+
]
52+
},
53+
"signature": {
54+
"type": "hex",
55+
"description": [
56+
"The signature."
57+
]
58+
},
59+
"base64": {
60+
"type": "string",
61+
"description": [
62+
"The signature encoded in base64."
63+
]
64+
},
65+
"bech32": {
66+
"type": "string",
67+
"description": [
68+
"The bech32 (native segwit) address."
69+
]
70+
},
71+
"p2tr": {
72+
"type": "string",
73+
"description": [
74+
"The taproot address."
75+
]
76+
}
77+
}
78+
},
79+
"author": [
80+
"Lagrang3 <<[email protected]>> is mainly responsible."
81+
],
82+
"see_also": [
83+
"lightning-checkmessagewithkey(7)"
84+
],
85+
"resources": [
86+
"Main web site: <https://github.com/ElementsProject/lightning>"
87+
],
88+
"examples": [
89+
]
90+
}
91+

Diff for: wallet/walletrpc.c

+125
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <ccan/array_size/array_size.h>
55
#include <ccan/cast/cast.h>
66
#include <common/addr.h>
7+
#include <common/base64.h>
78
#include <common/bech32.h>
89
#include <common/configdir.h>
910
#include <common/json_command.h>
@@ -1066,3 +1067,127 @@ static const struct json_command sendpsbt_command = {
10661067
};
10671068

10681069
AUTODATA(json_command, &sendpsbt_command);
1070+
1071+
static struct command_result *
1072+
json_signmessagewithkey(struct command *cmd, const char *buffer,
1073+
const jsmntok_t *obj UNNEEDED, const jsmntok_t *params)
1074+
{
1075+
/* decoding the address */
1076+
const u8 *scriptpubkey;
1077+
const char *message;
1078+
1079+
/* from wallet BIP32 */
1080+
struct pubkey pubkey;
1081+
1082+
if (!param(
1083+
cmd, buffer, params,
1084+
p_req("message", param_string, &message),
1085+
p_req("address", param_bitcoin_address, &scriptpubkey),
1086+
NULL))
1087+
return command_param_failed();
1088+
1089+
const size_t script_len = tal_bytelen(scriptpubkey);
1090+
1091+
/* FIXME: we already had the address from the input */
1092+
char *addr;
1093+
addr = encode_scriptpubkey_to_addr(tmpctx, chainparams, scriptpubkey);
1094+
enum addrtype addrtype;
1095+
1096+
if (is_p2tr(scriptpubkey, script_len, NULL))
1097+
addrtype = ADDR_P2TR;
1098+
else if (is_p2wpkh(scriptpubkey, script_len, NULL))
1099+
addrtype = ADDR_BECH32;
1100+
else {
1101+
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1102+
"address %s is neither p2wpkh nor p2tr and "
1103+
"it is not supported",
1104+
addr);
1105+
}
1106+
1107+
u32 keyidx;
1108+
1109+
/* loop over all generated keys, find a matching key */
1110+
/* FIXME: alternatively, can we use the our_addresses hash table?
1111+
*struct script_with_len *key;
1112+
*struct wallet_address *addr =
1113+
* wallet_address_htable_get(cmd->ld->wallet->our_addresses, key);
1114+
*/
1115+
struct issued_address_type *listaddrtypes =
1116+
wallet_list_addresses(tmpctx, cmd->ld->wallet, 1, NULL);
1117+
for (size_t i = 0; i < tal_count(listaddrtypes); i++) {
1118+
if (listaddrtypes[i].keyidx == BIP32_INITIAL_HARDENED_CHILD) {
1119+
break;
1120+
}
1121+
bip32_pubkey(cmd->ld, &pubkey, listaddrtypes[i].keyidx);
1122+
char *out_p2wpkh = "";
1123+
char *out_p2tr = "";
1124+
if (listaddrtypes[i].addrtype == ADDR_BECH32 ||
1125+
listaddrtypes[i].addrtype == ADDR_ALL) {
1126+
u8 *redeemscript_p2wpkh;
1127+
out_p2wpkh = encode_pubkey_to_addr(
1128+
cmd, &pubkey, ADDR_BECH32, &redeemscript_p2wpkh);
1129+
if (!out_p2wpkh) {
1130+
abort();
1131+
}
1132+
}
1133+
if (listaddrtypes[i].addrtype == ADDR_P2TR ||
1134+
listaddrtypes[i].addrtype == ADDR_ALL) {
1135+
out_p2tr =
1136+
encode_pubkey_to_addr(cmd, &pubkey, ADDR_P2TR,
1137+
/* out_redeemscript */ NULL);
1138+
if (!out_p2tr) {
1139+
abort();
1140+
}
1141+
}
1142+
1143+
if (streq(addr, out_p2wpkh) || streq(addr, out_p2tr)) {
1144+
keyidx = listaddrtypes[i].keyidx;
1145+
break;
1146+
}
1147+
}
1148+
1149+
/* wire to hsmd a sign request */
1150+
u8 *msg = towire_hsmd_sign_message_with_key(
1151+
cmd, tal_dup_arr(tmpctx, u8, (u8 *)message, strlen(message), 0),
1152+
keyidx);
1153+
if (!wire_sync_write(cmd->ld->hsm_fd, take(msg))) {
1154+
fatal("Could not write sign_with_key to HSM: %s",
1155+
strerror(errno));
1156+
}
1157+
1158+
/* read form hsmd a sign reply */
1159+
msg = wire_sync_read(cmd, cmd->ld->hsm_fd);
1160+
1161+
int recid;
1162+
u8 sig[65];
1163+
secp256k1_ecdsa_recoverable_signature rsig;
1164+
1165+
if (!fromwire_hsmd_sign_message_with_key_reply(msg, &rsig)) {
1166+
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1167+
"HSM gave bad sign_with_key_reply %s",
1168+
tal_hex(tmpctx, msg));
1169+
}
1170+
1171+
secp256k1_ecdsa_recoverable_signature_serialize_compact(
1172+
secp256k1_ctx, sig + 1, &recid, &rsig);
1173+
sig[0] = recid + 31;
1174+
1175+
struct json_stream *response;
1176+
response = json_stream_success(cmd);
1177+
if (addrtype & ADDR_BECH32)
1178+
json_add_string(response, "bech32", addr);
1179+
if (addrtype & ADDR_P2TR)
1180+
json_add_string(response, "p2tr", addr);
1181+
json_add_u32(response, "keyidx", keyidx);
1182+
json_add_pubkey(response, "pubkey", &pubkey);
1183+
json_add_hex(response, "signature", sig, sizeof(sig));
1184+
json_add_string(response, "base64",
1185+
b64_encode(tmpctx, sig, sizeof(sig)));
1186+
return command_success(cmd, response);
1187+
}
1188+
1189+
static const struct json_command signmessagewithkey_command = {
1190+
"signmessagewithkey",
1191+
json_signmessagewithkey
1192+
};
1193+
AUTODATA(json_command, &signmessagewithkey_command);

0 commit comments

Comments
 (0)