Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions bitcoin/psbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <wally_psbt.h>
#include <wally_psbt_members.h>
#include <wire/wire.h>


Expand Down Expand Up @@ -483,6 +484,28 @@ void psbt_input_set_witscript(struct wally_psbt *psbt, size_t in, const u8 *wscr
tal_wally_end(psbt);
}

const u8 *psbt_input_get_witscript(const tal_t *ctx,
const struct wally_psbt *psbt,
size_t in)
{
size_t witscript_len, written_len;
u8 *witscript;
if (wally_psbt_get_input_witness_script_len(psbt, in, &witscript_len) != WALLY_OK)
abort();
witscript = tal_arr(ctx, u8, witscript_len);
if (wally_psbt_get_input_witness_script(psbt, in, witscript, witscript_len, &written_len) != WALLY_OK)
abort();
if (witscript_len != written_len)
abort();
return witscript;
}

bool psbt_input_get_ecdsa_sig(const tal_t *ctx,
const struct wally_psbt *psbt,
size_t in,
const struct pubkey *pubkey,
struct bitcoin_signature **sig);

void psbt_elements_input_set_asset(struct wally_psbt *psbt, size_t in,
struct amount_asset *asset)
{
Expand Down Expand Up @@ -595,10 +618,16 @@ struct amount_sat psbt_input_get_amount(const struct wally_psbt *psbt,
}

size_t psbt_input_get_weight(const struct wally_psbt *psbt,
size_t in)
size_t in,
enum PSBT_GUESS guess)
{
size_t weight;
const struct wally_map_item *redeem_script;
struct wally_psbt_input *input = &psbt->inputs[in];
struct wally_tx_output *utxo_out = NULL;

if (input->utxo)
utxo_out = &input->utxo->outputs[input->index];

redeem_script = wally_map_get_integer(&psbt->inputs[in].psbt_fields, /* PSBT_IN_REDEEM_SCRIPT */ 0x04);

Expand All @@ -608,8 +637,20 @@ size_t psbt_input_get_weight(const struct wally_psbt *psbt,
weight +=
(redeem_script->value_len +
varint_size(redeem_script->value_len)) * 4;
} else if ((guess & PSBT_GUESS_2OF2)
&& utxo_out
&& is_p2wsh(utxo_out->script, utxo_out->script_len, NULL)) {
weight = bitcoin_tx_input_weight(false,
bitcoin_tx_2of2_input_witness_weight());
} else if (utxo_out
&& is_p2wpkh(utxo_out->script, utxo_out->script_len, NULL)) {
weight = bitcoin_tx_input_weight(false,
bitcoin_tx_input_witness_weight(UTXO_P2SH_P2WPKH));
} else if (utxo_out
&& is_p2tr(utxo_out->script, utxo_out->script_len, NULL)) {
weight = bitcoin_tx_input_weight(false,
bitcoin_tx_input_witness_weight(UTXO_P2TR));
} else {
/* zero scriptSig length */
weight += varint_size(0) * 4;
}

Expand Down
19 changes: 17 additions & 2 deletions bitcoin/psbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ WARN_UNUSED_RESULT bool psbt_input_get_ecdsa_sig(const tal_t *ctx,

void psbt_input_set_witscript(struct wally_psbt *psbt, size_t in, const u8 *wscript);

const u8 *psbt_input_get_witscript(const tal_t *ctx,
const struct wally_psbt *psbt,
size_t in);

/* psbt_input_set_unknown - Set the given Key-Value in the psbt's input keymap
* @ctx - tal context for allocations
* @in - psbt input to set key-value on
Expand Down Expand Up @@ -265,9 +269,20 @@ void psbt_output_set_unknown(const tal_t *ctx,
struct amount_sat psbt_input_get_amount(const struct wally_psbt *psbt,
size_t in);

/* psbt_input_get_weight - Calculate the tx weight for input index `in` */
enum PSBT_GUESS {
PSBT_GUESS_ZERO = 0x0, /* Assume unknown is 0 bytes (fallback) */
PSBT_GUESS_2OF2 = 0x1, /* Assume P2WSH is 2of2 multisig (req prevtx) */
};

/* psbt_input_get_weight - Calculate the tx weight for input index `in`.
*
* @psbt - psbt
* @in - index of input who's weight you want
* @guess - How to guess if we have incomplete information
* */
size_t psbt_input_get_weight(const struct wally_psbt *psbt,
size_t in);
size_t in,
enum PSBT_GUESS guess);

/* psbt_output_get_amount - Returns the value of this output
*
Expand Down
9 changes: 9 additions & 0 deletions bitcoin/test/run-bitcoin_block_from_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED)
/* 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(); }
/* Generated stub for is_p2wsh */
bool is_p2wsh(const u8 *script UNNEEDED, size_t script_len UNNEEDED, struct sha256 *addr UNNEEDED)
{ fprintf(stderr, "is_p2wsh called!\n"); abort(); }
/* Generated stub for is_p2tr */
bool is_p2tr(const u8 *script UNNEEDED, size_t script_len UNNEEDED, u8 xonly_pubkey[32] UNNEEDED)
{ fprintf(stderr, "is_p2tr called!\n"); abort(); }
/* Generated stub for is_p2wpkh */
bool is_p2wpkh(const u8 *script UNNEEDED, size_t script_len UNNEEDED, struct bitcoin_address *addr UNNEEDED)
{ fprintf(stderr, "is_p2wpkh called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

static const char block[] =
Expand Down
9 changes: 9 additions & 0 deletions bitcoin/test/run-psbt-from-tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ size_t varint_put(u8 buf[VARINT_MAX_LEN] UNNEEDED, varint_t v UNNEEDED)
/* Generated stub for varint_size */
size_t varint_size(varint_t v UNNEEDED)
{ fprintf(stderr, "varint_size called!\n"); abort(); }
/* Generated stub for is_p2wsh */
bool is_p2wsh(const u8 *script UNNEEDED, size_t script_len UNNEEDED, struct sha256 *addr UNNEEDED)
{ fprintf(stderr, "is_p2wsh called!\n"); abort(); }
/* Generated stub for is_p2tr */
bool is_p2tr(const u8 *script UNNEEDED, size_t script_len UNNEEDED, u8 xonly_pubkey[32] UNNEEDED)
{ fprintf(stderr, "is_p2tr called!\n"); abort(); }
/* Generated stub for is_p2wpkh */
bool is_p2wpkh(const u8 *script UNNEEDED, size_t script_len UNNEEDED, struct bitcoin_address *addr UNNEEDED)
{ fprintf(stderr, "is_p2wpkh called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

/* This transaction has scriptSig data in it.
Expand Down
9 changes: 9 additions & 0 deletions bitcoin/test/run-tx-encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED)
/* 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(); }
/* Generated stub for is_p2wsh */
bool is_p2wsh(const u8 *script UNNEEDED, size_t script_len UNNEEDED, struct sha256 *addr UNNEEDED)
{ fprintf(stderr, "is_p2wsh called!\n"); abort(); }
/* Generated stub for is_p2tr */
bool is_p2tr(const u8 *script UNNEEDED, size_t script_len UNNEEDED, u8 xonly_pubkey[32] UNNEEDED)
{ fprintf(stderr, "is_p2tr called!\n"); abort(); }
/* Generated stub for is_p2wpkh */
bool is_p2wpkh(const u8 *script UNNEEDED, size_t script_len UNNEEDED, struct bitcoin_address *addr UNNEEDED)
{ fprintf(stderr, "is_p2wpkh called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

const char extended_tx[] =
Expand Down
3 changes: 1 addition & 2 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,7 @@ size_t bitcoin_tx_2of2_input_witness_weight(void)
/* BOLT #03:
* Signatures are 73 bytes long (the maximum length).
*/
return 1 + /* Prefix: 4 elements to push on stack */
(1 + 0) + /* [0]: witness-marker-and-flag */
return (1 + 0) + /* [0]: witness-marker-and-flag */
(1 + 73) + /* [1] Party A signature and length prefix */
(1 + 73) + /* [2] Party B signature and length prefix */
(1 + 1 + /* [3] length prefix and numpushes (2) */
Expand Down
Loading
Loading