From 39d5117210f565e3af35e8dde4f037eb1b0787c2 Mon Sep 17 00:00:00 2001 From: lisa neigut Date: Mon, 30 Sep 2019 17:05:28 -0500 Subject: [PATCH] utxo: add scriptSig + scriptPubkey field Allow the utxo object to bear the scriptSig and scriptPubKey --- common/test/run-funding_tx.c | 13 +++++++++++++ common/utxo.c | 10 +++++++--- common/utxo.h | 3 +++ wallet/wallet.c | 17 +++++++++++++++++ wallet/wallet.h | 8 ++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/common/test/run-funding_tx.c b/common/test/run-funding_tx.c index 24e04e397..d5891715a 100644 --- a/common/test/run-funding_tx.c +++ b/common/test/run-funding_tx.c @@ -37,6 +37,13 @@ void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct n /* Generated stub for fromwire_pubkey */ void fromwire_pubkey(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct pubkey *pubkey UNNEEDED) { fprintf(stderr, "fromwire_pubkey called!\n"); abort(); } +/* Generated stub for fromwire_tal_arrn */ +u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED, + const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED) +{ fprintf(stderr, "fromwire_tal_arrn called!\n"); abort(); } +/* Generated stub for fromwire_u16 */ +u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) +{ fprintf(stderr, "fromwire_u16 called!\n"); abort(); } /* Generated stub for fromwire_u32 */ u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) { fprintf(stderr, "fromwire_u32 called!\n"); abort(); } @@ -58,12 +65,18 @@ void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED) /* Generated stub for towire_pubkey */ void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED) { fprintf(stderr, "towire_pubkey called!\n"); abort(); } +/* Generated stub for towire_u16 */ +void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED) +{ fprintf(stderr, "towire_u16 called!\n"); abort(); } /* Generated stub for towire_u32 */ void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED) { fprintf(stderr, "towire_u32 called!\n"); abort(); } /* Generated stub for towire_u64 */ void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED) { fprintf(stderr, "towire_u64 called!\n"); abort(); } +/* Generated stub for towire_u8_array */ +void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED) +{ fprintf(stderr, "towire_u8_array called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ #if 0 diff --git a/common/utxo.c b/common/utxo.c index b39adde67..456a95149 100644 --- a/common/utxo.c +++ b/common/utxo.c @@ -16,6 +16,11 @@ void towire_utxo(u8 **pptr, const struct utxo *utxo) towire_u32(pptr, utxo->keyindex); towire_bool(pptr, utxo->is_p2sh); + towire_u16(pptr, tal_count(utxo->scriptPubkey)); + towire_u8_array(pptr, utxo->scriptPubkey, tal_count(utxo->scriptPubkey)); + towire_u16(pptr, tal_count(utxo->scriptSig)); + towire_u8_array(pptr, utxo->scriptSig, tal_count(utxo->scriptSig)); + towire_bool(pptr, is_unilateral_close); if (is_unilateral_close) { towire_u64(pptr, utxo->close_info->channel_id); @@ -36,9 +41,8 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max) utxo->keyindex = fromwire_u32(ptr, max); utxo->is_p2sh = fromwire_bool(ptr, max); - /* No need to tell hsmd about the scriptPubkey, it has all the info to - * derive it from the rest. */ - utxo->scriptPubkey = NULL; + utxo->scriptPubkey = fromwire_tal_arrn(utxo, ptr, max, fromwire_u16(ptr, max)); + utxo->scriptSig = fromwire_tal_arrn(utxo, ptr, max, fromwire_u16(ptr, max)); if (fromwire_bool(ptr, max)) { utxo->close_info = tal(utxo, struct unilateral_close_info); diff --git a/common/utxo.h b/common/utxo.h index 459d5c5cc..1652cc2c6 100644 --- a/common/utxo.h +++ b/common/utxo.h @@ -41,6 +41,9 @@ struct utxo { /* The scriptPubkey if it is known */ u8 *scriptPubkey; + + /* scriptSig. Only for P2SH outputs */ + u8 *scriptSig; }; void towire_utxo(u8 **pptr, const struct utxo *utxo); diff --git a/wallet/wallet.c b/wallet/wallet.c index 452ccdfe1..662a4f10f 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -175,6 +175,7 @@ static struct utxo *wallet_stmt2output(const tal_t *ctx, struct db_stmt *stmt) utxo->blockheight = NULL; utxo->spendheight = NULL; utxo->scriptPubkey = NULL; + utxo->scriptSig = NULL; if (!db_column_is_null(stmt, 9)) { blockheight = tal(utxo, u32); @@ -537,6 +538,22 @@ const struct utxo **wallet_select_all(const tal_t *ctx, struct wallet *w, return utxo; } +u8 *derive_redeem_scriptsig(const tal_t *ctx, struct wallet *w, u32 keyindex) +{ + struct ext_key ext; + struct pubkey key; + + if (bip32_key_from_parent(w->bip32_base, keyindex, + BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { + fatal("Unable to derive pubkey"); + } + + if (!pubkey_from_der(ext.pub_key, PUBKEY_CMPR_LEN, &key)) + fatal("Unble to derive pubkey from DER"); + + return bitcoin_scriptsig_p2sh_p2wpkh(ctx, &key); +} + bool wallet_can_spend(struct wallet *w, const u8 *script, u32 *index, bool *output_is_p2sh) { diff --git a/wallet/wallet.h b/wallet/wallet.h index c3883ee07..40aead5ca 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -399,6 +399,14 @@ const struct utxo **wallet_select_all(const tal_t *ctx, struct wallet *w, struct amount_sat *sat, struct amount_sat *fee_estimate); +/* derive_redeem_scriptsig - Compute the scriptSig for a P2SH-P2WPKH + * + * @ctx - allocation context + * @w - wallet + * @keyindex - index of the internal BIP32 key + */ +u8 *derive_redeem_scriptsig(const tal_t *ctx, struct wallet *w, u32 keyindex); + /** * wallet_select_specific - Select utxos given an array of txids and an array of outputs index *