Skip to content

Commit d87f31f

Browse files
niftyneicdecker
authored andcommitted
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.
1 parent 90b393c commit d87f31f

File tree

4 files changed

+27
-51
lines changed

4 files changed

+27
-51
lines changed

common/utxo.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx,
7474
u32 nsequence)
7575
{
7676
struct pubkey key;
77-
u8 *scriptSig, *scriptPubkey, *redeemscript;
77+
u8 *scriptSig, *redeemscript;
7878

7979
size_t outcount = add_change_output ? 1 + num_output : num_output;
8080
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,
8383
for (size_t i = 0; i < tal_count(utxos); i++) {
8484
if (utxos[i]->is_p2sh && bip32_base) {
8585
bip32_pubkey(bip32_base, &key, utxos[i]->keyindex);
86-
scriptSig = bitcoin_scriptsig_p2sh_p2wpkh(tmpctx, &key);
87-
redeemscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key);
88-
scriptPubkey = scriptpubkey_p2sh(tmpctx, redeemscript);
89-
90-
/* Make sure we've got the right info! */
91-
if (utxos[i]->scriptPubkey)
92-
assert(memeq(utxos[i]->scriptPubkey,
93-
tal_bytelen(utxos[i]->scriptPubkey),
94-
scriptPubkey, tal_bytelen(scriptPubkey)));
86+
scriptSig =
87+
bitcoin_scriptsig_p2sh_p2wpkh(tmpctx, &key);
88+
redeemscript =
89+
bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key);
90+
9591
} else {
9692
scriptSig = NULL;
9793
redeemscript = NULL;
98-
/* We can't definitively derive the pubkey without
99-
* hitting the HSM, so we don't */
100-
scriptPubkey = utxos[i]->scriptPubkey;
10194
}
10295

103-
bitcoin_tx_add_input(tx, &utxos[i]->txid, utxos[i]->outnum,
104-
nsequence, scriptSig, utxos[i]->amount,
105-
scriptPubkey, NULL);
96+
bitcoin_tx_add_input(tx, &utxos[i]->txid,
97+
utxos[i]->outnum,
98+
nsequence,
99+
scriptSig, utxos[i]->amount,
100+
utxos[i]->scriptPubkey, NULL);
106101

107102
/* Add redeemscript to the PSBT input */
108103
if (redeemscript)
109-
psbt_input_set_redeemscript(tx->psbt, i, redeemscript);
104+
psbt_input_set_redeemscript(tx->psbt, i,
105+
redeemscript);
110106

111107
}
112108

wallet/test/run-wallet.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,9 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
932932
u.close_info->channel_id = 42;
933933
u.close_info->peer_id = id;
934934
u.close_info->commitment_point = &pk;
935+
/* Arbitrarily set scriptpubkey len to 20 */
936+
u.scriptPubkey = tal_arr(w, u8, 20);
937+
memset(u.scriptPubkey, 1, 20);
935938
CHECK_MSG(wallet_add_utxo(w, &u, p2sh_wpkh),
936939
"wallet_add_utxo with close_info");
937940

@@ -980,6 +983,8 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
980983
u.close_info->channel_id = 42;
981984
u.close_info->peer_id = id;
982985
u.close_info->commitment_point = NULL;
986+
u.scriptPubkey = tal_arr(w, u8, 20);
987+
memset(u.scriptPubkey, 1, 20);
983988
CHECK_MSG(wallet_add_utxo(w, &u, p2sh_wpkh),
984989
"wallet_add_utxo with close_info no commitment_point");
985990

wallet/wallet.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,8 @@ static bool wallet_add_utxo(struct wallet *w, struct utxo *utxo,
146146
else
147147
db_bind_null(stmt, 10);
148148

149-
if (utxo->scriptPubkey)
150-
db_bind_blob(stmt, 11, utxo->scriptPubkey,
151-
tal_bytelen(utxo->scriptPubkey));
152-
else
153-
db_bind_null(stmt, 11);
149+
db_bind_blob(stmt, 11, utxo->scriptPubkey,
150+
tal_bytelen(utxo->scriptPubkey));
154151

155152
db_exec_prepared_v2(take(stmt));
156153
return true;
@@ -184,9 +181,12 @@ static struct utxo *wallet_stmt2output(const tal_t *ctx, struct db_stmt *stmt)
184181
utxo->close_info = NULL;
185182
}
186183

184+
utxo->scriptPubkey =
185+
tal_dup_arr(utxo, u8, db_column_blob(stmt, 11),
186+
db_column_bytes(stmt, 11), 0);
187+
187188
utxo->blockheight = NULL;
188189
utxo->spendheight = NULL;
189-
utxo->scriptPubkey = NULL;
190190
utxo->scriptSig = NULL;
191191
utxo->reserved_til = NULL;
192192

@@ -202,11 +202,6 @@ static struct utxo *wallet_stmt2output(const tal_t *ctx, struct db_stmt *stmt)
202202
utxo->spendheight = spendheight;
203203
}
204204

205-
if (!db_column_is_null(stmt, 11)) {
206-
utxo->scriptPubkey =
207-
tal_dup_arr(utxo, u8, db_column_blob(stmt, 11),
208-
db_column_bytes(stmt, 11), 0);
209-
}
210205
if (!db_column_is_null(stmt, 12)) {
211206
reserved_til = tal(utxo, u32);
212207
*reserved_til = db_column_int(stmt, 12);

wallet/walletrpc.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -868,29 +868,9 @@ static void json_add_utxo(struct json_stream *response,
868868
json_add_amount_sat_compat(response, utxo->amount,
869869
"value", "amount_msat");
870870

871-
if (utxo->scriptPubkey != NULL) {
872-
json_add_hex_talarr(response, "scriptpubkey", utxo->scriptPubkey);
873-
out = encode_scriptpubkey_to_addr(
874-
tmpctx, chainparams,
875-
utxo->scriptPubkey);
876-
} else {
877-
out = NULL;
878-
#ifdef COMPAT_V072
879-
/* scriptpubkey was introduced in v0.7.3.
880-
* We could handle close_info via HSM to get address,
881-
* but who cares? We'll print a warning though. */
882-
if (utxo->close_info == NULL) {
883-
struct pubkey funding_pubkey;
884-
bip32_pubkey(wallet->bip32_base,
885-
&funding_pubkey,
886-
utxo->keyindex);
887-
out = encode_pubkey_to_addr(tmpctx,
888-
&funding_pubkey,
889-
utxo->is_p2sh,
890-
NULL);
891-
}
892-
#endif
893-
}
871+
json_add_hex_talarr(response, "scriptpubkey", utxo->scriptPubkey);
872+
out = encode_scriptpubkey_to_addr(tmpctx, chainparams,
873+
utxo->scriptPubkey);
894874
if (!out)
895875
log_broken(wallet->log,
896876
"Could not encode utxo %s:%u%s!",

0 commit comments

Comments
 (0)