Skip to content

Commit 3a49a38

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 #put messages with keys from our wallet. Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
1 parent 42c00e3 commit 3a49a38

File tree

5 files changed

+338
-0
lines changed

5 files changed

+338
-0
lines changed

contrib/msggen/msggen/schema.json

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

doc/schemas/signmessagewithkey.json

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
"lightning-checkmessagewithkey(7)"
73+
],
74+
"resources": [
75+
"Main web site: <https://github.com/ElementsProject/lightning>"
76+
],
77+
"examples": [
78+
{
79+
"request": {
80+
"id": "example:signmessagewithkey#1",
81+
"method": "signmessagewithkey",
82+
"params": {
83+
"message": "a test message",
84+
"address": "bcrt1qgrh5vtf63mtayzhxwp480aww3j3qfr5qpq65un"
85+
}
86+
},
87+
"response": {
88+
"address": "bcrt1qgrh5vtf63mtayzhxwp480aww3j3qfr5qpq65un",
89+
"pubkey": "03bc4a456585ba21ba26af4a0e5399ec76410b2e0ca67db0f3bcb2f47b232fa4b0",
90+
"signature": "28564edf260a72d991cbb38cf608e293124f8b8f478d13d4544fe27b9d76c65df1284ca395ccdfd3d5f151729ef18f56c028f5f860155d6aa4d0aaaa176a00db01",
91+
"base64": "KFZO3yYKctmRy7OM9gjikxJPi49HjRPUVE/ie512xl3xKEyjlczf09XxUXKe8Y9WwCj1+GAVXWqk0KqqF2oA2wE="
92+
}
93+
}
94+
]
95+
}

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

+35
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
@@ -2479,6 +2480,40 @@ def test_signmessage(node_factory):
24792480
l2.rpc.checkmessage(message="wrong zbase format", zbase="wrong zbase format")
24802481

24812482

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

wallet/walletrpc.c

+111
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,113 @@ 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+
u32 keyidx = UINT32_MAX;
1103+
1104+
/* loop over all generated keys, find a matching key */
1105+
/* FIXME: alternatively, can we use the our_addresses hash table?
1106+
*struct script_with_len *key;
1107+
*struct wallet_address *addr =
1108+
* wallet_address_htable_get(cmd->ld->wallet->our_addresses, key);
1109+
*/
1110+
struct issued_address_type *listaddrtypes =
1111+
wallet_list_addresses(tmpctx, cmd->ld->wallet, 1, NULL);
1112+
for (size_t i = 0; i < tal_count(listaddrtypes); i++) {
1113+
bip32_pubkey(cmd->ld, &pubkey, listaddrtypes[i].keyidx);
1114+
char *out_p2wpkh = "";
1115+
if (listaddrtypes[i].addrtype == ADDR_BECH32 ||
1116+
listaddrtypes[i].addrtype == ADDR_ALL) {
1117+
u8 *redeemscript_p2wpkh;
1118+
out_p2wpkh = encode_pubkey_to_addr(
1119+
cmd, &pubkey, ADDR_BECH32, &redeemscript_p2wpkh);
1120+
if (!out_p2wpkh) {
1121+
abort();
1122+
}
1123+
}
1124+
if (streq(addr, out_p2wpkh)) {
1125+
keyidx = listaddrtypes[i].keyidx;
1126+
break;
1127+
}
1128+
}
1129+
1130+
if (keyidx == UINT32_MAX) {
1131+
return command_fail(
1132+
cmd, JSONRPC2_INVALID_PARAMS,
1133+
"Address is not found in the wallet's database");
1134+
}
1135+
1136+
/* wire to hsmd a sign request */
1137+
u8 *msg = towire_hsmd_sign_message_with_key(
1138+
cmd, tal_dup_arr(tmpctx, u8, (u8 *)message, strlen(message), 0),
1139+
keyidx);
1140+
if (!wire_sync_write(cmd->ld->hsm_fd, take(msg))) {
1141+
fatal("Could not write sign_with_key to HSM: %s",
1142+
strerror(errno));
1143+
}
1144+
1145+
/* read form hsmd a sign reply */
1146+
msg = wire_sync_read(cmd, cmd->ld->hsm_fd);
1147+
1148+
int recid;
1149+
u8 sig[65];
1150+
secp256k1_ecdsa_recoverable_signature rsig;
1151+
1152+
if (!fromwire_hsmd_sign_message_with_key_reply(msg, &rsig)) {
1153+
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1154+
"HSM gave bad sign_with_key_reply %s",
1155+
tal_hex(tmpctx, msg));
1156+
}
1157+
1158+
secp256k1_ecdsa_recoverable_signature_serialize_compact(
1159+
secp256k1_ctx, sig + 1, &recid, &rsig);
1160+
/* this is the header value for P2WPKH specified in BIP137 */
1161+
sig[0] = recid + 39;
1162+
1163+
/* FIXME: Given the fact that we plan to extend support for BIP322
1164+
* signature in the future making a pubkey output here makes less sense. */
1165+
struct json_stream *response;
1166+
response = json_stream_success(cmd);
1167+
json_add_string(response, "address", addr);
1168+
json_add_pubkey(response, "pubkey", &pubkey);
1169+
json_add_hex(response, "signature", sig, sizeof(sig));
1170+
json_add_string(response, "base64",
1171+
b64_encode(tmpctx, sig, sizeof(sig)));
1172+
return command_success(cmd, response);
1173+
}
1174+
1175+
static const struct json_command signmessagewithkey_command = {
1176+
"signmessagewithkey",
1177+
json_signmessagewithkey
1178+
};
1179+
AUTODATA(json_command, &signmessagewithkey_command);

0 commit comments

Comments
 (0)