From 2874e46c695b87151dbda6923f4849a7a3bfa579 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 7 Mar 2017 11:28:42 +1030 Subject: [PATCH] lightningd/hsm: use funding_tx helper to sign funding transaction. DRY. Signed-off-by: Rusty Russell --- lightningd/hsm/Makefile | 2 +- lightningd/hsm/hsm.c | 36 ++++++++++++++---------------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/lightningd/hsm/Makefile b/lightningd/hsm/Makefile index e0584b581..f5ed9c0b9 100644 --- a/lightningd/hsm/Makefile +++ b/lightningd/hsm/Makefile @@ -41,7 +41,7 @@ $(LIGHTNINGD_HSM_OBJS): $(CORE_TX_HEADERS) lightningd/hsm-all: lightningd/lightningd_hsm $(LIGHTNINGD_HSM_CLIENT_OBJS) -lightningd/lightningd_hsm: $(LIGHTNINGD_HSM_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) $(LIBBASE58_OBJS) lightningd/utxo.o libsecp256k1.a libsodium.a libwallycore.a +lightningd/lightningd_hsm: $(LIGHTNINGD_HSM_OBJS) $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) $(LIBBASE58_OBJS) lightningd/utxo.o libsecp256k1.a libsodium.a libwallycore.a $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) lightningd/hsm/gen_hsm_client_wire.h: $(WIRE_GEN) lightningd/hsm/hsm_client_wire_csv diff --git a/lightningd/hsm/hsm.c b/lightningd/hsm/hsm.c index a80f9ebd9..6c30b0b35 100644 --- a/lightningd/hsm/hsm.c +++ b/lightningd/hsm/hsm.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -360,11 +361,13 @@ static u8 *sign_funding_tx(const tal_t *ctx, const u8 *data) u32 change_keyindex; struct pubkey local_pubkey, remote_pubkey; struct utxo *inputs; + const struct utxo **utxomap; struct bitcoin_tx *tx; u8 *wscript, *msg_out; secp256k1_ecdsa_signature *sig; - const void **inmap; + u32 outnum; size_t i; + struct pubkey changekey; /* FIXME: Check fee is "reasonable" */ if (!fromwire_hsmctl_sign_funding(tmpctx, data, NULL, @@ -373,35 +376,24 @@ static u8 *sign_funding_tx(const tal_t *ctx, const u8 *data) &remote_pubkey, &inputs)) status_failed(WIRE_HSMSTATUS_BAD_REQUEST, "Bad SIGN_FUNDING"); - tx = bitcoin_tx(tmpctx, tal_count(inputs), 1 + !!change_out); - inmap = tal_arr(tmpctx, const void *, tal_count(inputs)); - for (i = 0; i < tal_count(inputs); i++) { - tx->input[i].txid = inputs[i].txid; - tx->input[i].index = inputs[i].outnum; - tx->input[i].amount = tal_dup(tx->input, u64, &inputs[i].amount); - inmap[i] = int2ptr(i); - } - tx->output[0].amount = satoshi_out; - wscript = bitcoin_redeem_2of2(tx, &local_pubkey, &remote_pubkey); - tx->output[0].script = scriptpubkey_p2wsh(tx, wscript); - if (change_out) { - struct pubkey changekey; + /* FIXME: unmarshall gives array, not array of pointers. */ + utxomap = tal_arr(tmpctx, const struct utxo *, tal_count(inputs)); + for (i = 0; i < tal_count(inputs); i++) + utxomap[i] = &inputs[i]; + + if (change_out) bitcoin_pubkey(&changekey, change_keyindex); - tx->output[1].amount = change_out; - tx->output[1].script = scriptpubkey_p2wpkh(tx, &changekey); - } - - /* Now permute. */ - permute_outputs(tx->output, tal_count(tx->output), NULL); - permute_inputs(tx->input, tal_count(tx->input), inmap); + tx = funding_tx(tmpctx, &outnum, utxomap, + satoshi_out, &local_pubkey, &remote_pubkey, + change_out, &changekey); /* Now generate signatures. */ sig = tal_arr(tmpctx, secp256k1_ecdsa_signature, tal_count(inputs)); for (i = 0; i < tal_count(inputs); i++) { struct pubkey inkey; struct privkey inprivkey; - const struct utxo *in = &inputs[ptr2int(inmap[i])]; + const struct utxo *in = utxomap[i]; u8 *subscript; bitcoin_keypair(&inprivkey, &inkey, in->keyindex);