mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
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.
This commit is contained in:
parent
90b393ca1a
commit
d87f31f9a8
@ -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);
|
||||
scriptSig =
|
||||
bitcoin_scriptsig_p2sh_p2wpkh(tmpctx, &key);
|
||||
redeemscript =
|
||||
bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key);
|
||||
|
||||
/* 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)));
|
||||
} 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);
|
||||
|
||||
}
|
||||
|
||||
|
@ -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");
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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!",
|
||||
|
Loading…
Reference in New Issue
Block a user