From d87f31f9a8419590b109a7ce390623e6faba5387 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 28 Jul 2020 21:08:17 -0500 Subject: [PATCH] utxo: clean up NULL handling of scriptpubkey Now that we're *guaranteed* to have a scriptpubkey entry in the database, we remove the NULL handling for it. --- common/utxo.c | 30 +++++++++++++----------------- wallet/test/run-wallet.c | 5 +++++ wallet/wallet.c | 17 ++++++----------- wallet/walletrpc.c | 26 +++----------------------- 4 files changed, 27 insertions(+), 51 deletions(-) diff --git a/common/utxo.c b/common/utxo.c index af30e993c..f8987025b 100644 --- a/common/utxo.c +++ b/common/utxo.c @@ -74,7 +74,7 @@ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx, u32 nsequence) { struct pubkey key; - u8 *scriptSig, *scriptPubkey, *redeemscript; + u8 *scriptSig, *redeemscript; size_t outcount = add_change_output ? 1 + num_output : num_output; struct bitcoin_tx *tx = bitcoin_tx(ctx, chainparams, tal_count(utxos), @@ -83,30 +83,26 @@ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx, for (size_t i = 0; i < tal_count(utxos); i++) { if (utxos[i]->is_p2sh && bip32_base) { bip32_pubkey(bip32_base, &key, utxos[i]->keyindex); - scriptSig = bitcoin_scriptsig_p2sh_p2wpkh(tmpctx, &key); - redeemscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key); - scriptPubkey = scriptpubkey_p2sh(tmpctx, redeemscript); - - /* Make sure we've got the right info! */ - if (utxos[i]->scriptPubkey) - assert(memeq(utxos[i]->scriptPubkey, - tal_bytelen(utxos[i]->scriptPubkey), - scriptPubkey, tal_bytelen(scriptPubkey))); + scriptSig = + bitcoin_scriptsig_p2sh_p2wpkh(tmpctx, &key); + redeemscript = + bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key); + } else { scriptSig = NULL; redeemscript = NULL; - /* We can't definitively derive the pubkey without - * hitting the HSM, so we don't */ - scriptPubkey = utxos[i]->scriptPubkey; } - bitcoin_tx_add_input(tx, &utxos[i]->txid, utxos[i]->outnum, - nsequence, scriptSig, utxos[i]->amount, - scriptPubkey, NULL); + bitcoin_tx_add_input(tx, &utxos[i]->txid, + utxos[i]->outnum, + nsequence, + scriptSig, utxos[i]->amount, + utxos[i]->scriptPubkey, NULL); /* Add redeemscript to the PSBT input */ if (redeemscript) - psbt_input_set_redeemscript(tx->psbt, i, redeemscript); + psbt_input_set_redeemscript(tx->psbt, i, + redeemscript); } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index ef8e18bbe..981cd8562 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -932,6 +932,9 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx) u.close_info->channel_id = 42; u.close_info->peer_id = id; u.close_info->commitment_point = &pk; + /* Arbitrarily set scriptpubkey len to 20 */ + u.scriptPubkey = tal_arr(w, u8, 20); + memset(u.scriptPubkey, 1, 20); CHECK_MSG(wallet_add_utxo(w, &u, p2sh_wpkh), "wallet_add_utxo with close_info"); @@ -980,6 +983,8 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx) u.close_info->channel_id = 42; u.close_info->peer_id = id; u.close_info->commitment_point = NULL; + u.scriptPubkey = tal_arr(w, u8, 20); + memset(u.scriptPubkey, 1, 20); CHECK_MSG(wallet_add_utxo(w, &u, p2sh_wpkh), "wallet_add_utxo with close_info no commitment_point"); diff --git a/wallet/wallet.c b/wallet/wallet.c index 708932af7..4cc99ce59 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -146,11 +146,8 @@ static bool wallet_add_utxo(struct wallet *w, struct utxo *utxo, else db_bind_null(stmt, 10); - if (utxo->scriptPubkey) - db_bind_blob(stmt, 11, utxo->scriptPubkey, - tal_bytelen(utxo->scriptPubkey)); - else - db_bind_null(stmt, 11); + db_bind_blob(stmt, 11, utxo->scriptPubkey, + tal_bytelen(utxo->scriptPubkey)); db_exec_prepared_v2(take(stmt)); return true; @@ -184,9 +181,12 @@ static struct utxo *wallet_stmt2output(const tal_t *ctx, struct db_stmt *stmt) utxo->close_info = NULL; } + utxo->scriptPubkey = + tal_dup_arr(utxo, u8, db_column_blob(stmt, 11), + db_column_bytes(stmt, 11), 0); + utxo->blockheight = NULL; utxo->spendheight = NULL; - utxo->scriptPubkey = NULL; utxo->scriptSig = NULL; utxo->reserved_til = NULL; @@ -202,11 +202,6 @@ static struct utxo *wallet_stmt2output(const tal_t *ctx, struct db_stmt *stmt) utxo->spendheight = spendheight; } - if (!db_column_is_null(stmt, 11)) { - utxo->scriptPubkey = - tal_dup_arr(utxo, u8, db_column_blob(stmt, 11), - db_column_bytes(stmt, 11), 0); - } if (!db_column_is_null(stmt, 12)) { reserved_til = tal(utxo, u32); *reserved_til = db_column_int(stmt, 12); diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 04a4da283..383d45280 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -868,29 +868,9 @@ static void json_add_utxo(struct json_stream *response, json_add_amount_sat_compat(response, utxo->amount, "value", "amount_msat"); - if (utxo->scriptPubkey != NULL) { - json_add_hex_talarr(response, "scriptpubkey", utxo->scriptPubkey); - out = encode_scriptpubkey_to_addr( - tmpctx, chainparams, - utxo->scriptPubkey); - } else { - out = NULL; -#ifdef COMPAT_V072 - /* scriptpubkey was introduced in v0.7.3. - * We could handle close_info via HSM to get address, - * but who cares? We'll print a warning though. */ - if (utxo->close_info == NULL) { - struct pubkey funding_pubkey; - bip32_pubkey(wallet->bip32_base, - &funding_pubkey, - utxo->keyindex); - out = encode_pubkey_to_addr(tmpctx, - &funding_pubkey, - utxo->is_p2sh, - NULL); - } -#endif - } + json_add_hex_talarr(response, "scriptpubkey", utxo->scriptPubkey); + out = encode_scriptpubkey_to_addr(tmpctx, chainparams, + utxo->scriptPubkey); if (!out) log_broken(wallet->log, "Could not encode utxo %s:%u%s!",