2019-08-12 21:29:13 +02:00
|
|
|
#include <assert.h>
|
2020-06-06 22:02:22 +02:00
|
|
|
#include <bitcoin/privkey.h>
|
|
|
|
#include <bitcoin/psbt.h>
|
2018-12-03 00:02:11 +01:00
|
|
|
#include <bitcoin/script.h>
|
2020-06-06 22:02:22 +02:00
|
|
|
#include <ccan/ccan/mem/mem.h>
|
2018-12-03 00:02:11 +01:00
|
|
|
#include <common/key_derive.h>
|
2019-06-05 07:29:08 +02:00
|
|
|
#include <common/utils.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/utxo.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <wire/wire.h>
|
|
|
|
|
|
|
|
void towire_utxo(u8 **pptr, const struct utxo *utxo)
|
|
|
|
{
|
2018-01-02 14:54:32 +01:00
|
|
|
/* Is this a unilateral close output and needs the
|
|
|
|
* close_info? */
|
|
|
|
bool is_unilateral_close = utxo->close_info != NULL;
|
2017-12-18 07:41:52 +01:00
|
|
|
towire_bitcoin_txid(pptr, &utxo->txid);
|
2017-02-21 05:45:29 +01:00
|
|
|
towire_u32(pptr, utxo->outnum);
|
2019-02-21 03:39:51 +01:00
|
|
|
towire_amount_sat(pptr, utxo->amount);
|
2017-02-21 05:45:29 +01:00
|
|
|
towire_u32(pptr, utxo->keyindex);
|
|
|
|
towire_bool(pptr, utxo->is_p2sh);
|
2018-01-02 14:54:32 +01:00
|
|
|
|
2019-10-01 00:05:28 +02:00
|
|
|
towire_u16(pptr, tal_count(utxo->scriptPubkey));
|
|
|
|
towire_u8_array(pptr, utxo->scriptPubkey, tal_count(utxo->scriptPubkey));
|
|
|
|
|
2018-01-02 14:54:32 +01:00
|
|
|
towire_bool(pptr, is_unilateral_close);
|
|
|
|
if (is_unilateral_close) {
|
|
|
|
towire_u64(pptr, utxo->close_info->channel_id);
|
2019-04-08 11:58:32 +02:00
|
|
|
towire_node_id(pptr, &utxo->close_info->peer_id);
|
2019-09-10 04:24:27 +02:00
|
|
|
towire_bool(pptr, utxo->close_info->commitment_point != NULL);
|
|
|
|
if (utxo->close_info->commitment_point)
|
|
|
|
towire_pubkey(pptr, utxo->close_info->commitment_point);
|
2020-08-14 03:30:42 +02:00
|
|
|
towire_bool(pptr, utxo->close_info->option_anchor_outputs);
|
2018-01-02 14:54:32 +01:00
|
|
|
}
|
2017-02-21 05:45:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-08 02:23:46 +01:00
|
|
|
struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
2018-02-08 02:23:46 +01:00
|
|
|
struct utxo *utxo = tal(ctx, struct utxo);
|
|
|
|
|
2017-12-18 07:41:52 +01:00
|
|
|
fromwire_bitcoin_txid(ptr, max, &utxo->txid);
|
2017-02-21 05:45:29 +01:00
|
|
|
utxo->outnum = fromwire_u32(ptr, max);
|
2019-02-21 03:39:51 +01:00
|
|
|
utxo->amount = fromwire_amount_sat(ptr, max);
|
2017-02-21 05:45:29 +01:00
|
|
|
utxo->keyindex = fromwire_u32(ptr, max);
|
|
|
|
utxo->is_p2sh = fromwire_bool(ptr, max);
|
2019-02-22 15:47:30 +01:00
|
|
|
|
2019-10-01 00:05:28 +02:00
|
|
|
utxo->scriptPubkey = fromwire_tal_arrn(utxo, ptr, max, fromwire_u16(ptr, max));
|
2019-02-22 15:47:30 +01:00
|
|
|
|
2018-01-02 14:54:32 +01:00
|
|
|
if (fromwire_bool(ptr, max)) {
|
2018-02-08 02:23:46 +01:00
|
|
|
utxo->close_info = tal(utxo, struct unilateral_close_info);
|
2018-01-02 14:54:32 +01:00
|
|
|
utxo->close_info->channel_id = fromwire_u64(ptr, max);
|
2019-04-08 11:58:32 +02:00
|
|
|
fromwire_node_id(ptr, max, &utxo->close_info->peer_id);
|
2019-09-10 04:24:27 +02:00
|
|
|
if (fromwire_bool(ptr, max)) {
|
|
|
|
utxo->close_info->commitment_point = tal(utxo,
|
|
|
|
struct pubkey);
|
|
|
|
fromwire_pubkey(ptr, max,
|
|
|
|
utxo->close_info->commitment_point);
|
|
|
|
} else
|
|
|
|
utxo->close_info->commitment_point = NULL;
|
2020-08-14 03:30:42 +02:00
|
|
|
utxo->close_info->option_anchor_outputs
|
|
|
|
= fromwire_bool(ptr, max);
|
2018-01-02 14:54:32 +01:00
|
|
|
} else {
|
|
|
|
utxo->close_info = NULL;
|
|
|
|
}
|
2018-02-08 02:23:46 +01:00
|
|
|
return utxo;
|
2017-02-21 05:45:29 +01:00
|
|
|
}
|
2018-12-03 00:02:11 +01:00
|
|
|
|
|
|
|
struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx,
|
2019-07-30 16:14:43 +02:00
|
|
|
const struct chainparams *chainparams,
|
2018-12-03 00:02:11 +01:00
|
|
|
const struct utxo **utxos,
|
|
|
|
const struct ext_key *bip32_base,
|
2019-08-12 21:29:13 +02:00
|
|
|
bool add_change_output,
|
2020-01-29 12:59:06 +01:00
|
|
|
size_t num_output,
|
|
|
|
u32 nlocktime,
|
|
|
|
u32 nsequence)
|
2018-12-03 00:02:11 +01:00
|
|
|
{
|
2019-03-14 16:21:45 +01:00
|
|
|
struct pubkey key;
|
2020-07-29 04:08:17 +02:00
|
|
|
u8 *scriptSig, *redeemscript;
|
2019-08-12 21:29:13 +02:00
|
|
|
|
|
|
|
size_t outcount = add_change_output ? 1 + num_output : num_output;
|
2020-01-29 12:59:06 +01:00
|
|
|
struct bitcoin_tx *tx = bitcoin_tx(ctx, chainparams, tal_count(utxos),
|
|
|
|
outcount, nlocktime);
|
2018-12-03 00:02:11 +01:00
|
|
|
|
|
|
|
for (size_t i = 0; i < tal_count(utxos); i++) {
|
2020-08-14 03:30:42 +02:00
|
|
|
u32 this_nsequence;
|
2018-12-03 00:02:11 +01:00
|
|
|
if (utxos[i]->is_p2sh && bip32_base) {
|
|
|
|
bip32_pubkey(bip32_base, &key, utxos[i]->keyindex);
|
2020-07-29 04:08:17 +02:00
|
|
|
scriptSig =
|
|
|
|
bitcoin_scriptsig_p2sh_p2wpkh(tmpctx, &key);
|
|
|
|
redeemscript =
|
|
|
|
bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key);
|
|
|
|
|
2019-03-14 16:21:45 +01:00
|
|
|
} else {
|
2020-06-06 22:02:22 +02:00
|
|
|
scriptSig = NULL;
|
|
|
|
redeemscript = NULL;
|
2018-12-03 00:02:11 +01:00
|
|
|
}
|
2019-03-14 16:21:45 +01:00
|
|
|
|
2020-08-14 03:30:42 +02:00
|
|
|
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
|
|
|
|
* #### `to_remote` Output
|
|
|
|
* ...
|
|
|
|
* The output is spent by a transaction with `nSequence` field
|
|
|
|
* set to `1` and witness:
|
|
|
|
*/
|
|
|
|
if (utxos[i]->close_info && utxos[i]->close_info->option_anchor_outputs)
|
|
|
|
this_nsequence = 1;
|
|
|
|
else
|
|
|
|
this_nsequence = nsequence;
|
|
|
|
|
2020-07-29 04:08:17 +02:00
|
|
|
bitcoin_tx_add_input(tx, &utxos[i]->txid,
|
|
|
|
utxos[i]->outnum,
|
2020-08-14 03:30:42 +02:00
|
|
|
this_nsequence,
|
2020-07-29 04:08:17 +02:00
|
|
|
scriptSig, utxos[i]->amount,
|
|
|
|
utxos[i]->scriptPubkey, NULL);
|
2020-06-06 22:02:22 +02:00
|
|
|
|
|
|
|
/* Add redeemscript to the PSBT input */
|
|
|
|
if (redeemscript)
|
2020-07-29 04:08:17 +02:00
|
|
|
psbt_input_set_redeemscript(tx->psbt, i,
|
|
|
|
redeemscript);
|
2020-06-06 22:02:22 +02:00
|
|
|
|
2018-12-03 00:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return tx;
|
|
|
|
}
|
2020-07-06 07:26:14 +02:00
|
|
|
|
|
|
|
size_t utxo_spend_weight(const struct utxo *utxo)
|
|
|
|
{
|
|
|
|
return bitcoin_tx_simple_input_weight(utxo->is_p2sh);
|
|
|
|
}
|