Skip to content

Commit 7bf9588

Browse files
committed
tx: make the existing bip341 size calculation explicitly btc
The Elements case is more complicated and will be computed in its own function.
1 parent 56b99ea commit 7bf9588

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/transaction.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,13 +1707,15 @@ static int get_txin_issuance_size(const struct wally_tx_input *input,
17071707
return WALLY_OK;
17081708
}
17091709

1710-
static size_t get_bip341_size(uint32_t sighash, bool have_annex, unsigned char ext_flag)
1710+
/* Get the (exact) BIP341 serialized tx size as per BIP341/342/118 */
1711+
static size_t get_btc_bip341_size(const struct tx_serialize_opts *opts)
17111712
{
1712-
const bool sh_anyonecanpay = sighash & WALLY_SIGHASH_ANYONECANPAY;
1713-
const bool sh_none = (sighash & WALLY_SIGHASH_MASK) == WALLY_SIGHASH_NONE;
1714-
/* See BIP341/342/118. Note the leading 1 for the sighash epoc byte */
1715-
return 1 + 174 - sh_anyonecanpay * 49 - sh_none * 32 +
1716-
have_annex * 32 + (ext_flag == EXT_FLAG_BIP342 ? 37 : 0);
1713+
const bool sh_anyonecanpay = opts->tx_sighash & WALLY_SIGHASH_ANYONECANPAY;
1714+
const bool sh_none = (opts->tx_sighash & WALLY_SIGHASH_MASK) == WALLY_SIGHASH_NONE;
1715+
/* Note the leading 1 is for the sighash epoc byte */
1716+
return 1 + 174 - (sh_anyonecanpay ? 49 : 0) - (sh_none ? SHA256_LEN : 0) +
1717+
(opts->annex_len ? SHA256_LEN : 0) +
1718+
(opts->ext_flag == EXT_FLAG_BIP342 ? SHA256_LEN + 1 + 4 : 0);
17171719
}
17181720

17191721
/* We compute the size of the witness separately so we can compute vsize
@@ -1743,8 +1745,7 @@ static int tx_get_lengths(const struct wally_tx *tx,
17431745
return WALLY_ERROR; /* Segwit tx hashing uses bip143 opts member */
17441746

17451747
if (opts->bip341) {
1746-
*base_size = get_bip341_size(opts->tx_sighash, opts->annex_len != 0,
1747-
opts->ext_flag);
1748+
*base_size = get_btc_bip341_size(opts);
17481749
*witness_size = 0;
17491750
return WALLY_OK;
17501751
}

0 commit comments

Comments
 (0)