From be708ba64a5beafd582c0f9cf991394c2a4acd06 Mon Sep 17 00:00:00 2001 From: lisa neigut Date: Mon, 16 Sep 2019 19:02:27 -0500 Subject: [PATCH] hsmd: pull up utxo signing going to need to re-use this later. --- hsmd/hsmd.c | 64 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/hsmd/hsmd.c b/hsmd/hsmd.c index 39ad012b1..9f9631023 100644 --- a/hsmd/hsmd.c +++ b/hsmd/hsmd.c @@ -1547,6 +1547,41 @@ static void hsm_key_for_utxo(struct privkey *privkey, struct pubkey *pubkey, } } +static void sign_input(struct bitcoin_tx *tx, struct utxo *in, + struct pubkey *inkey, + struct bitcoin_signature *sig, + int index) +{ + struct privkey inprivkey; + u8 *subscript, *wscript, *script; + + /* Figure out keys to spend this. */ + hsm_key_for_utxo(&inprivkey, inkey, in); + + /* It's either a p2wpkh or p2sh (we support that so people from + * the last bitcoin era can put funds into the wallet) */ + wscript = p2wpkh_scriptcode(tmpctx, inkey); + if (in->is_p2sh) { + /* For P2SH-wrapped Segwit, the (implied) redeemScript + * is defined in BIP141 */ + subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, inkey); + script = bitcoin_scriptsig_p2sh_p2wpkh(tx, inkey); + bitcoin_tx_input_set_script(tx, index, script); + } else { + /* Pure segwit uses an empty inputScript; NULL has + * tal_count() == 0, so it works great here. */ + subscript = NULL; + bitcoin_tx_input_set_script(tx, index, NULL); + } + /* This is the core crypto magic. */ + sign_tx_input(tx, index, subscript, wscript, &inprivkey, inkey, + SIGHASH_ALL, sig); + + /* The witness is [sig] [key] */ + bitcoin_tx_input_set_witness( + tx, index, take(bitcoin_witness_p2wpkh(tx, sig, inkey))); +} + /* This completes the tx by filling in the input scripts with signatures. */ static void sign_all_inputs(struct bitcoin_tx *tx, struct utxo **utxos) { @@ -1564,36 +1599,9 @@ static void sign_all_inputs(struct bitcoin_tx *tx, struct utxo **utxos) assert(tx->wtx->num_inputs == tal_count(utxos)); for (size_t i = 0; i < tal_count(utxos); i++) { struct pubkey inkey; - struct privkey inprivkey; - const struct utxo *in = utxos[i]; - u8 *subscript, *wscript, *script; struct bitcoin_signature sig; - /* Figure out keys to spend this. */ - hsm_key_for_utxo(&inprivkey, &inkey, in); - - /* It's either a p2wpkh or p2sh (we support that so people from - * the last bitcoin era can put funds into the wallet) */ - wscript = p2wpkh_scriptcode(tmpctx, &inkey); - if (in->is_p2sh) { - /* For P2SH-wrapped Segwit, the (implied) redeemScript - * is defined in BIP141 */ - subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &inkey); - script = bitcoin_scriptsig_p2sh_p2wpkh(tx, &inkey); - bitcoin_tx_input_set_script(tx, i, script); - } else { - /* Pure segwit uses an empty inputScript; NULL has - * tal_count() == 0, so it works great here. */ - subscript = NULL; - bitcoin_tx_input_set_script(tx, i, NULL); - } - /* This is the core crypto magic. */ - sign_tx_input(tx, i, subscript, wscript, &inprivkey, &inkey, - SIGHASH_ALL, &sig); - - /* The witness is [sig] [key] */ - bitcoin_tx_input_set_witness( - tx, i, take(bitcoin_witness_p2wpkh(tx, &sig, &inkey))); + sign_input(tx, utxos[i], &inkey, &sig, i); } }