mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-03 18:57:06 +01:00
hsmd: invoice signing support.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
02c1d10c9f
commit
f9c6f6413f
3 changed files with 62 additions and 0 deletions
|
@ -29,6 +29,7 @@ HSMD_COMMON_OBJS := \
|
||||||
common/daemon_conn.o \
|
common/daemon_conn.o \
|
||||||
common/debug.o \
|
common/debug.o \
|
||||||
common/funding_tx.o \
|
common/funding_tx.o \
|
||||||
|
common/hash_u5.o \
|
||||||
common/io_debug.o \
|
common/io_debug.o \
|
||||||
common/key_derive.o \
|
common/key_derive.o \
|
||||||
common/msg_queue.o \
|
common/msg_queue.o \
|
||||||
|
|
51
hsmd/hsm.c
51
hsmd/hsm.c
|
@ -16,6 +16,7 @@
|
||||||
#include <common/daemon_conn.h>
|
#include <common/daemon_conn.h>
|
||||||
#include <common/debug.h>
|
#include <common/debug.h>
|
||||||
#include <common/funding_tx.h>
|
#include <common/funding_tx.h>
|
||||||
|
#include <common/hash_u5.h>
|
||||||
#include <common/io_debug.h>
|
#include <common/io_debug.h>
|
||||||
#include <common/status.h>
|
#include <common/status.h>
|
||||||
#include <common/type_to_string.h>
|
#include <common/type_to_string.h>
|
||||||
|
@ -569,6 +570,51 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
|
||||||
tal_free(tmpctx);
|
tal_free(tmpctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sign_invoice - Sign an invoice with our key.
|
||||||
|
*/
|
||||||
|
static void sign_invoice(struct daemon_conn *master, const u8 *msg)
|
||||||
|
{
|
||||||
|
const tal_t *tmpctx = tal_tmpctx(master);
|
||||||
|
u5 *u5bytes;
|
||||||
|
u8 *hrpu8;
|
||||||
|
char *hrp;
|
||||||
|
struct sha256 sha;
|
||||||
|
secp256k1_ecdsa_recoverable_signature rsig;
|
||||||
|
struct hash_u5 hu5;
|
||||||
|
struct privkey node_pkey;
|
||||||
|
|
||||||
|
if (!fromwire_hsmctl_sign_invoice(tmpctx, msg, NULL, &u5bytes, &hrpu8)) {
|
||||||
|
status_trace("Failed to parse sign_invoice: %s",
|
||||||
|
tal_hex(trc, msg));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* FIXME: Check invoice! */
|
||||||
|
|
||||||
|
hrp = tal_dup_arr(tmpctx, char, (char *)hrpu8, tal_len(hrpu8), 1);
|
||||||
|
hrp[tal_len(hrpu8)] = '\0';
|
||||||
|
|
||||||
|
hash_u5_init(&hu5, hrp);
|
||||||
|
hash_u5(&hu5, u5bytes, tal_len(u5bytes));
|
||||||
|
hash_u5_done(&hu5, &sha);
|
||||||
|
|
||||||
|
node_key(&node_pkey, NULL);
|
||||||
|
if (!secp256k1_ecdsa_sign_recoverable(secp256k1_ctx, &rsig,
|
||||||
|
(const u8 *)&sha,
|
||||||
|
node_pkey.secret.data,
|
||||||
|
NULL, NULL)) {
|
||||||
|
/* FIXME: Now master will freeze... */
|
||||||
|
status_trace("Failed to sign invoice: %s",
|
||||||
|
tal_hex(trc, msg));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
daemon_conn_send(master,
|
||||||
|
take(towire_hsmctl_sign_invoice_reply(tmpctx, &rsig)));
|
||||||
|
tal_free(tmpctx);
|
||||||
|
}
|
||||||
|
|
||||||
static void sign_node_announcement(struct daemon_conn *master, const u8 *msg)
|
static void sign_node_announcement(struct daemon_conn *master, const u8 *msg)
|
||||||
{
|
{
|
||||||
/* 2 bytes msg type + 64 bytes signature */
|
/* 2 bytes msg type + 64 bytes signature */
|
||||||
|
@ -626,6 +672,10 @@ static struct io_plan *control_received_req(struct io_conn *conn,
|
||||||
sign_withdrawal_tx(master, master->msg_in);
|
sign_withdrawal_tx(master, master->msg_in);
|
||||||
return daemon_conn_read_next(conn, master);
|
return daemon_conn_read_next(conn, master);
|
||||||
|
|
||||||
|
case WIRE_HSMCTL_SIGN_INVOICE:
|
||||||
|
sign_invoice(master, master->msg_in);
|
||||||
|
return daemon_conn_read_next(conn, master);
|
||||||
|
|
||||||
case WIRE_HSMCTL_NODE_ANNOUNCEMENT_SIG_REQ:
|
case WIRE_HSMCTL_NODE_ANNOUNCEMENT_SIG_REQ:
|
||||||
sign_node_announcement(master, master->msg_in);
|
sign_node_announcement(master, master->msg_in);
|
||||||
return daemon_conn_read_next(conn, master);
|
return daemon_conn_read_next(conn, master);
|
||||||
|
@ -635,6 +685,7 @@ static struct io_plan *control_received_req(struct io_conn *conn,
|
||||||
case WIRE_HSMCTL_HSMFD_CHANNELD_REPLY:
|
case WIRE_HSMCTL_HSMFD_CHANNELD_REPLY:
|
||||||
case WIRE_HSMCTL_SIGN_FUNDING_REPLY:
|
case WIRE_HSMCTL_SIGN_FUNDING_REPLY:
|
||||||
case WIRE_HSMCTL_SIGN_WITHDRAWAL_REPLY:
|
case WIRE_HSMCTL_SIGN_WITHDRAWAL_REPLY:
|
||||||
|
case WIRE_HSMCTL_SIGN_INVOICE_REPLY:
|
||||||
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
|
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
|
||||||
case WIRE_HSMCTL_NODE_ANNOUNCEMENT_SIG_REPLY:
|
case WIRE_HSMCTL_NODE_ANNOUNCEMENT_SIG_REPLY:
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -63,3 +63,13 @@ hsmctl_sign_withdrawal,,inputs,num_inputs*struct utxo
|
||||||
hsmctl_sign_withdrawal_reply,107
|
hsmctl_sign_withdrawal_reply,107
|
||||||
hsmctl_sign_withdrawal_reply,,num_sigs,u16
|
hsmctl_sign_withdrawal_reply,,num_sigs,u16
|
||||||
hsmctl_sign_withdrawal_reply,,sig,num_sigs*secp256k1_ecdsa_signature
|
hsmctl_sign_withdrawal_reply,,sig,num_sigs*secp256k1_ecdsa_signature
|
||||||
|
|
||||||
|
# Sign an invoice
|
||||||
|
hsmctl_sign_invoice,8
|
||||||
|
hsmctl_sign_invoice,,len,u16
|
||||||
|
hsmctl_sign_invoice,,u5bytes,len*u8
|
||||||
|
hsmctl_sign_invoice,,hrplen,u16
|
||||||
|
hsmctl_sign_invoice,,hrp,hrplen*u8
|
||||||
|
|
||||||
|
hsmctl_sign_invoice_reply,108
|
||||||
|
hsmctl_sign_invoice_reply,,sig,secp256k1_ecdsa_recoverable_signature
|
||||||
|
|
|
Loading…
Add table
Reference in a new issue