Skip to content

Commit a4dc714

Browse files
ksedgwicendothermicdev
authored andcommitted
hsmd: add hsmd_preapprove_keysend and check_preapprovekeysend pay modifier
Changelog-added: hsmd: A new message `hsmd_preapprove_keysend` is added. Changelog-added: JSON-RPC: A new command `preapprovekeysend` is added.
1 parent f29343d commit a4dc714

File tree

13 files changed

+238
-1
lines changed

13 files changed

+238
-1
lines changed

common/jsonrpc_errors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ enum jsonrpc_errcode {
4747
PAY_STATUS_UNEXPECTED = 211,
4848
PAY_INVOICE_REQUEST_INVALID = 212,
4949
PAY_INVOICE_PREAPPROVAL_DECLINED = 213,
50+
PAY_KEYSEND_PREAPPROVAL_DECLINED = 214,
5051

5152
/* `fundchannel` or `withdraw` errors */
5253
FUND_MAX_EXCEEDED = 300,

doc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ MANPAGES := doc/lightning-cli.1 \
7676
doc/lightning-parsefeerate.7 \
7777
doc/lightning-plugin.7 \
7878
doc/lightning-preapproveinvoice.7 \
79+
doc/lightning-preapprovekeysend.7 \
7980
doc/lightning-recoverchannel.7 \
8081
doc/lightning-reserveinputs.7 \
8182
doc/lightning-sendinvoice.7 \

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Core Lightning Documentation
106106
lightning-ping <lightning-ping.7.md>
107107
lightning-plugin <lightning-plugin.7.md>
108108
lightning-preapproveinvoice <lightning-preapproveinvoice.7.md>
109+
lightning-preapprovekeysend <lightning-preapprovekeysend.7.md>
109110
lightning-recoverchannel <lightning-recoverchannel.7.md>
110111
lightning-reserveinputs <lightning-reserveinputs.7.md>
111112
lightning-sendcustommsg <lightning-sendcustommsg.7.md>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
lightning-preapprovekeysend -- Ask the HSM to preapprove a keysend payment (low-level)
2+
==================================================================
3+
4+
SYNOPSIS
5+
--------
6+
7+
**preapprovekeysend** *destination* *payment\_hash* *amount\_msat*
8+
9+
DESCRIPTION
10+
-----------
11+
12+
The **preapprovekeysend** RPC command submits the *destination*, *payment\_hash*,
13+
and *amount\_msat* parameters to the HSM to check that they are approved as a
14+
keysend payment.
15+
16+
*destination* is a 33 byte, hex-encoded, node ID of the node that the payment should go to.
17+
18+
*payment\_hash* is the unique identifier of a payment.
19+
20+
*amount\_msat* is the amount to send in millisatoshi precision; it can
21+
be a whole number, or a whole number with suffix `msat` or `sat`, or a
22+
three decimal point number with suffix `sat`, or an 1 to 11 decimal
23+
point number suffixed by `btc`.
24+
25+
Generally the **preapprovekeysend** request does not need to be made
26+
explicitly, it is automatically generated as part of a **keysend** request.
27+
28+
By default, the HSM will approve all **preapprovekeysend** requests.
29+
30+
If a remote signer is being used it might decline an **preapprovekeysend**
31+
request because it would exceed velocity controls, is not covered by
32+
allowlist controls, was declined manually, or other reasons.
33+
34+
If a remote signer declines a **preapprovekeysend** request a subsequent
35+
attempt to pay the keysend anyway will fail; the signer will refuse to sign
36+
the commitment.
37+
38+
RETURN VALUE
39+
------------
40+
41+
[comment]: # (GENERATE-FROM-SCHEMA-START)
42+
On success, an empty object is returned.
43+
44+
[comment]: # (GENERATE-FROM-SCHEMA-END)
45+
46+
AUTHOR
47+
------
48+
49+
Ken Sedgwick <<ken@bonsai.com>> is mainly responsible.
50+
51+
SEE ALSO
52+
--------
53+
54+
lightning-keysend(7)
55+
56+
RESOURCES
57+
---------
58+
59+
Main web site: <https://github.com/ElementsProject/lightning>
60+
61+
[comment]: # ( SHA256STAMP:735dd61146b04745f1e884037ead662a386fec2c41e2de1a8698d6bb03f63540)

doc/schemas/preapproveinvoice.request.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
],
88
"properties": {
99
"bolt11": {
10-
"type": "string"
10+
"type": "string",
11+
"added": "v23.02"
1112
}
1213
}
1314
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"additionalProperties": false,
5+
"required": [
6+
"destination",
7+
"payment_hash",
8+
"amount_msat"
9+
],
10+
"properties": {
11+
"destination": {
12+
"type": "pubkey",
13+
"added": "v23.02"
14+
},
15+
"payment_hash": {
16+
"type": "hex",
17+
"added": "v23.02",
18+
"description": "the hash of the *payment_preimage* which will prove payment",
19+
"maxLength": 64,
20+
"minLength": 64
21+
},
22+
"amount_msat": {
23+
"type": "msat",
24+
"added": "v23.02"
25+
}
26+
}
27+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"additionalProperties": false,
5+
"properties": {}
6+
}

hsmd/hsmd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
669669
case WIRE_HSMD_SIGN_OPTION_WILL_FUND_OFFER:
670670
case WIRE_HSMD_SIGN_BOLT12:
671671
case WIRE_HSMD_PREAPPROVE_INVOICE:
672+
case WIRE_HSMD_PREAPPROVE_KEYSEND:
672673
case WIRE_HSMD_ECDH_REQ:
673674
case WIRE_HSMD_CHECK_FUTURE_SECRET:
674675
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY:
@@ -710,6 +711,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
710711
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY_REPLY:
711712
case WIRE_HSMD_SIGN_BOLT12_REPLY:
712713
case WIRE_HSMD_PREAPPROVE_INVOICE_REPLY:
714+
case WIRE_HSMD_PREAPPROVE_KEYSEND_REPLY:
713715
return bad_req_fmt(conn, c, c->msg_in,
714716
"Received an incoming message of type %s, "
715717
"which is not a request",

hsmd/hsmd_wire.csv

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ msgdata,hsmd_preapprove_invoice,invstring,wirestring,
121121
msgtype,hsmd_preapprove_invoice_reply,138
122122
msgdata,hsmd_preapprove_invoice_reply,approved,bool,
123123

124+
# Preapprove a keysend payment
125+
msgtype,hsmd_preapprove_keysend,39
126+
msgdata,hsmd_preapprove_keysend,destination,node_id,
127+
msgdata,hsmd_preapprove_keysend,payment_hash,sha256,
128+
msgdata,hsmd_preapprove_keysend,amount_msat,amount_msat,
129+
130+
# Result is true if approved, declined if false
131+
msgtype,hsmd_preapprove_keysend_reply,139
132+
msgdata,hsmd_preapprove_keysend_reply,approved,bool,
133+
124134
# Give me ECDH(node-id-secret,point)
125135
msgtype,hsmd_ecdh_req,1
126136
msgdata,hsmd_ecdh_req,point,pubkey,

hsmd/libhsmd.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
120120
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY:
121121
case WIRE_HSMD_SIGN_BOLT12:
122122
case WIRE_HSMD_PREAPPROVE_INVOICE:
123+
case WIRE_HSMD_PREAPPROVE_KEYSEND:
123124
case WIRE_HSMD_DERIVE_SECRET:
124125
return (client->capabilities & HSM_CAP_MASTER) != 0;
125126

@@ -151,6 +152,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
151152
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY_REPLY:
152153
case WIRE_HSMD_SIGN_BOLT12_REPLY:
153154
case WIRE_HSMD_PREAPPROVE_INVOICE_REPLY:
155+
case WIRE_HSMD_PREAPPROVE_KEYSEND_REPLY:
154156
case WIRE_HSMD_DERIVE_SECRET_REPLY:
155157
break;
156158
}
@@ -677,6 +679,24 @@ static u8 *handle_preapprove_invoice(struct hsmd_client *c, const u8 *msg_in)
677679
return towire_hsmd_preapprove_invoice_reply(NULL, approved);
678680
}
679681

682+
/*~ lightningd asks us to approve a keysend payment. This stub implementation
683+
* is overriden by fully validating signers that need to track keysend
684+
* payments. */
685+
static u8 *handle_preapprove_keysend(struct hsmd_client *c, const u8 *msg_in)
686+
{
687+
struct node_id destination;
688+
struct sha256 payment_hash;
689+
struct amount_msat amount_msat;
690+
bool approved;
691+
if (!fromwire_hsmd_preapprove_keysend(msg_in, &destination, &payment_hash, &amount_msat))
692+
return hsmd_status_malformed_request(c, msg_in);
693+
694+
/* This stub always approves */
695+
approved = true;
696+
697+
return towire_hsmd_preapprove_keysend_reply(NULL, approved);
698+
}
699+
680700
/*~ Lightning invoices, defined by BOLT 11, are signed. This has been
681701
* surprisingly controversial; it means a node needs to be online to create
682702
* invoices. However, it seems clear to me that in a world without
@@ -1592,6 +1612,8 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
15921612
return handle_sign_bolt12(client, msg);
15931613
case WIRE_HSMD_PREAPPROVE_INVOICE:
15941614
return handle_preapprove_invoice(client, msg);
1615+
case WIRE_HSMD_PREAPPROVE_KEYSEND:
1616+
return handle_preapprove_keysend(client, msg);
15951617
case WIRE_HSMD_SIGN_MESSAGE:
15961618
return handle_sign_message(client, msg);
15971619
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS:
@@ -1656,6 +1678,7 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
16561678
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY_REPLY:
16571679
case WIRE_HSMD_SIGN_BOLT12_REPLY:
16581680
case WIRE_HSMD_PREAPPROVE_INVOICE_REPLY:
1681+
case WIRE_HSMD_PREAPPROVE_KEYSEND_REPLY:
16591682
break;
16601683
}
16611684
return hsmd_status_bad_request(client, msg, "Unknown request");

0 commit comments

Comments
 (0)