Skip to content

Commit ff56ac3

Browse files
committed
add signmessagewithkey RPC
signmessagewithkey: allows to sign a message with a key associated with one bitcoin address in our wallet. Changelog-Added: add a new rpc command signmessagewithkey to sign input messages with keys from our wallet. Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
1 parent 1a2fbb5 commit ff56ac3

File tree

5 files changed

+340
-0
lines changed

5 files changed

+340
-0
lines changed

contrib/msggen/msggen/schema.json

+93
Original file line numberDiff line numberDiff line change
@@ -32839,6 +32839,99 @@
3283932839
}
3284032840
]
3284132841
},
32842+
"signmessagewithkey.json": {
32843+
"$schema": "../rpc-schema-draft.json",
32844+
"type": "object",
32845+
"rpc": "signmessagewithkey",
32846+
"title": "Command to create a signature using a key from the wallet",
32847+
"description": [
32848+
"The **signmessagewithkey** RPC command creates a digital signature of *message* using the key associated with the address provided in the input.",
32849+
"The signature scheme follows the BIP137 specification."
32850+
],
32851+
"added": "v25.05",
32852+
"request": {
32853+
"required": [
32854+
"message",
32855+
"address"
32856+
],
32857+
"additionalProperties": false,
32858+
"properties": {
32859+
"message": {
32860+
"type": "string",
32861+
"description": [
32862+
"Less than 65536 characters long message to be signed by the node."
32863+
]
32864+
},
32865+
"address": {
32866+
"type": "string",
32867+
"description": [
32868+
"A Bitcoin accepted type address for lookup in the list of addresses issued to date.",
32869+
"Only P2WPKH type addresses are supported"
32870+
]
32871+
}
32872+
}
32873+
},
32874+
"response": {
32875+
"required": [
32876+
"address",
32877+
"pubkey",
32878+
"signature",
32879+
"base64"
32880+
],
32881+
"additionalProperties": false,
32882+
"properties": {
32883+
"address": {
32884+
"type": "string",
32885+
"description": [
32886+
"The bitcoin address used for signing."
32887+
]
32888+
},
32889+
"pubkey": {
32890+
"type": "pubkey",
32891+
"description": [
32892+
"The public key associated with the bitcoin address provided."
32893+
]
32894+
},
32895+
"signature": {
32896+
"type": "hex",
32897+
"description": [
32898+
"The signature."
32899+
]
32900+
},
32901+
"base64": {
32902+
"type": "string",
32903+
"description": [
32904+
"The signature encoded in base64."
32905+
]
32906+
}
32907+
}
32908+
},
32909+
"author": [
32910+
"Lagrang3 <<lagrang3@protonmail.com>> is mainly responsible."
32911+
],
32912+
"see_also": [],
32913+
"resources": [
32914+
"Main web site: <https://github.com/ElementsProject/lightning>"
32915+
],
32916+
"examples": [
32917+
{
32918+
"request": {
32919+
"id": "example:signmessagewithkey#1",
32920+
"method": "signmessagewithkey",
32921+
"params": {
32922+
"message": "a test message",
32923+
"address": "bcrt1qgrh5vtf63mtayzhxwp480aww3j3qfr5qpq65un"
32924+
}
32925+
},
32926+
"response": {
32927+
"address": "bcrt1qgrh5vtf63mtayzhxwp480aww3j3qfr5qpq65un",
32928+
"pubkey": "03bc4a456585ba21ba26af4a0e5399ec76410b2e0ca67db0f3bcb2f47b232fa4b0",
32929+
"signature": "28564edf260a72d991cbb38cf608e293124f8b8f478d13d4544fe27b9d76c65df1284ca395ccdfd3d5f151729ef18f56c028f5f860155d6aa4d0aaaa176a00db01",
32930+
"base64": "KFZO3yYKctmRy7OM9gjikxJPi49HjRPUVE/ie512xl3xKEyjlczf09XxUXKe8Y9WwCj1+GAVXWqk0KqqF2oA2wE="
32931+
}
32932+
}
32933+
]
32934+
},
3284232935
"signpsbt.json": {
3284332936
"$schema": "../rpc-schema-draft.json",
3284432937
"type": "object",

doc/schemas/signmessagewithkey.json

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 BIP137 specification."
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 address for lookup in the list of addresses issued to date.",
28+
"Only P2WPKH type addresses are supported"
29+
]
30+
}
31+
}
32+
},
33+
"response": {
34+
"required": [
35+
"address",
36+
"pubkey",
37+
"signature",
38+
"base64"
39+
],
40+
"additionalProperties": false,
41+
"properties": {
42+
"address": {
43+
"type": "string",
44+
"description": [
45+
"The bitcoin address used for signing."
46+
]
47+
},
48+
"pubkey": {
49+
"type": "pubkey",
50+
"description": [
51+
"The public key associated with the bitcoin address provided."
52+
]
53+
},
54+
"signature": {
55+
"type": "hex",
56+
"description": [
57+
"The signature."
58+
]
59+
},
60+
"base64": {
61+
"type": "string",
62+
"description": [
63+
"The signature encoded in base64."
64+
]
65+
}
66+
}
67+
},
68+
"author": [
69+
"Lagrang3 <<lagrang3@protonmail.com>> is mainly responsible."
70+
],
71+
"see_also": [],
72+
"resources": [
73+
"Main web site: <https://github.com/ElementsProject/lightning>"
74+
],
75+
"examples": [
76+
{
77+
"request": {
78+
"id": "example:signmessagewithkey#1",
79+
"method": "signmessagewithkey",
80+
"params": {
81+
"message": "a test message",
82+
"address": "bcrt1qgrh5vtf63mtayzhxwp480aww3j3qfr5qpq65un"
83+
}
84+
},
85+
"response": {
86+
"address": "bcrt1qgrh5vtf63mtayzhxwp480aww3j3qfr5qpq65un",
87+
"pubkey": "03bc4a456585ba21ba26af4a0e5399ec76410b2e0ca67db0f3bcb2f47b232fa4b0",
88+
"signature": "28564edf260a72d991cbb38cf608e293124f8b8f478d13d4544fe27b9d76c65df1284ca395ccdfd3d5f151729ef18f56c028f5f860155d6aa4d0aaaa176a00db01",
89+
"base64": "KFZO3yYKctmRy7OM9gjikxJPi49HjRPUVE/ie512xl3xKEyjlczf09XxUXKe8Y9WwCj1+GAVXWqk0KqqF2oA2wE="
90+
}
91+
}
92+
]
93+
}

tests/autogenerate-rpc-examples.py

+2
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,8 @@ def generate_utils_examples(l1, l2, l3, l4, l5, l6, c23_2, c34_2, inv_l11, inv_l
12111211
update_example(node=l2, method='signmessage', params={'message': 'message for you'})
12121212
update_example(node=l2, method='checkmessage', params={'message': 'testcase to check new rpc error', 'zbase': 'd66bqz3qsku5fxtqsi37j11pci47ydxa95iusphutggz9ezaxt56neh77kxe5hyr41kwgkncgiu94p9ecxiexgpgsz8daoq4tw8kj8yx', 'pubkey': '03be3b0e9992153b1d5a6e1623670b6c3663f72ce6cf2e0dd39c0a373a7de5a3b7'})
12131213
update_example(node=l2, method='checkmessage', params={'message': 'this is a test!', 'zbase': 'd6tqaeuonjhi98mmont9m4wag7gg4krg1f4txonug3h31e9h6p6k6nbwjondnj46dkyausobstnk7fhyy998bhgc1yr98dfmhb4k54d7'})
1214+
addr = l2.rpc.newaddr('bech32')['bech32']
1215+
update_example(node=l2, method='signmessagewithkey', params={'message': 'a test message', 'address': addr})
12141216
decodepay_res1 = update_example(node=l2, method='decodepay', params={'bolt11': inv_l11['bolt11']})
12151217
update_example(node=l2, method='decode', params=[rune_l21['rune']])
12161218
decode_res2 = update_example(node=l2, method='decode', params=[inv_l22['bolt11']])

tests/test_misc.py

+37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
from bitcoin.rpc import RawProxy
23
from decimal import Decimal
34
from fixtures import * # noqa: F401,F403
@@ -2480,6 +2481,42 @@ def test_signmessage(node_factory):
24802481
l2.rpc.checkmessage(message="wrong zbase format", zbase="wrong zbase format")
24812482

24822483

2484+
def test_signmessagewithkey(node_factory, chainparams):
2485+
l1, l2 = node_factory.get_nodes(2)
2486+
message = "a test message"
2487+
addr_bech32 = l1.rpc.newaddr("bech32")["bech32"]
2488+
addr_other = l2.rpc.newaddr("bech32")["bech32"]
2489+
if TEST_NETWORK != "liquid-regtest":
2490+
# refuse to sign if the address is not a P2WPKH
2491+
addr_p2tr = l1.rpc.newaddr("p2tr")["p2tr"]
2492+
with pytest.raises(
2493+
RpcError, match=r"Address is not p2wpkh and it is not supported"
2494+
):
2495+
l1.rpc.signmessagewithkey(message, addr_p2tr)
2496+
2497+
# refuse to sign if the address does not belong to us
2498+
with pytest.raises(
2499+
RpcError, match=r"Address is not found in the wallet\'s database"
2500+
):
2501+
l1.rpc.signmessagewithkey(message, addr_other)
2502+
response = l1.rpc.signmessagewithkey(message, addr_bech32)
2503+
assert response["address"] == addr_bech32
2504+
signature = base64.b64decode(response["base64"])
2505+
assert signature.hex() == response["signature"]
2506+
assert (
2507+
subprocess.check_output(
2508+
[
2509+
"devtools/bip137-verifysignature",
2510+
message,
2511+
response["signature"],
2512+
response["address"],
2513+
chainparams["name"],
2514+
]
2515+
).decode("utf-8")
2516+
== "Signature is valid!\n"
2517+
)
2518+
2519+
24832520
def test_include(node_factory):
24842521
l1 = node_factory.get_node(start=False)
24852522

wallet/walletrpc.c

+115
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,117 @@ 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+
1095+
if (!is_p2wpkh(scriptpubkey, script_len, NULL)) {
1096+
/* FIXME add support for BIP 322 */
1097+
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1098+
"Address is not p2wpkh and "
1099+
"it is not supported for signing");
1100+
}
1101+
1102+
if (!hsm_capable(cmd->ld, WIRE_HSMD_BIP137_SIGN_MESSAGE)) {
1103+
return command_fail(
1104+
cmd, JSONRPC2_INVALID_PARAMS,
1105+
"HSM does not support signing BIP137 signing.");
1106+
}
1107+
1108+
const u32 bip32_max_index =
1109+
db_get_intvar(cmd->ld->wallet->db, "bip32_max_index", 0);
1110+
bool match_found = false;
1111+
u32 keyidx;
1112+
enum addrtype addrtype;
1113+
1114+
/* loop over all generated keys, find a matching key */
1115+
for (keyidx = 1; keyidx <= bip32_max_index; keyidx++) {
1116+
bip32_pubkey(cmd->ld, &pubkey, keyidx);
1117+
u8 *redeemscript_p2wpkh;
1118+
char *out_p2wpkh = encode_pubkey_to_addr(
1119+
cmd, &pubkey, ADDR_BECH32, &redeemscript_p2wpkh);
1120+
if (!out_p2wpkh) {
1121+
abort();
1122+
}
1123+
/* wallet_get_addrtype fails for entries prior to v24.11, all
1124+
* address types are assumed in that case. */
1125+
if (!wallet_get_addrtype(cmd->ld->wallet, keyidx, &addrtype))
1126+
addrtype = ADDR_ALL;
1127+
if (streq(addr, out_p2wpkh) &&
1128+
(addrtype == ADDR_BECH32 || addrtype == ADDR_ALL)) {
1129+
match_found = true;
1130+
break;
1131+
}
1132+
}
1133+
1134+
if (!match_found) {
1135+
return command_fail(
1136+
cmd, JSONRPC2_INVALID_PARAMS,
1137+
"Address is not found in the wallet's database");
1138+
}
1139+
1140+
/* wire to hsmd a sign request */
1141+
u8 *msg = towire_hsmd_bip137_sign_message(
1142+
cmd, tal_dup_arr(tmpctx, u8, (u8 *)message, strlen(message), 0),
1143+
keyidx);
1144+
if (!wire_sync_write(cmd->ld->hsm_fd, take(msg))) {
1145+
fatal("Could not write sign_with_key to HSM: %s",
1146+
strerror(errno));
1147+
}
1148+
1149+
/* read form hsmd a sign reply */
1150+
msg = wire_sync_read(cmd, cmd->ld->hsm_fd);
1151+
1152+
int recid;
1153+
u8 sig[65];
1154+
secp256k1_ecdsa_recoverable_signature rsig;
1155+
1156+
if (!fromwire_hsmd_bip137_sign_message_reply(msg, &rsig)) {
1157+
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1158+
"HSM gave bad sign_with_key_reply %s",
1159+
tal_hex(tmpctx, msg));
1160+
}
1161+
1162+
secp256k1_ecdsa_recoverable_signature_serialize_compact(
1163+
secp256k1_ctx, sig + 1, &recid, &rsig);
1164+
/* this is the header value for P2WPKH specified in BIP137 */
1165+
sig[0] = recid + 39;
1166+
1167+
/* FIXME: Given the fact that we plan to extend support for BIP322
1168+
* signature in the future making a pubkey output here makes less sense. */
1169+
struct json_stream *response;
1170+
response = json_stream_success(cmd);
1171+
json_add_string(response, "address", addr);
1172+
json_add_pubkey(response, "pubkey", &pubkey);
1173+
json_add_hex(response, "signature", sig, sizeof(sig));
1174+
json_add_string(response, "base64",
1175+
b64_encode(tmpctx, sig, sizeof(sig)));
1176+
return command_success(cmd, response);
1177+
}
1178+
1179+
static const struct json_command signmessagewithkey_command = {
1180+
"signmessagewithkey",
1181+
json_signmessagewithkey
1182+
};
1183+
AUTODATA(json_command, &signmessagewithkey_command);

0 commit comments

Comments
 (0)