tx: prevent pre-bip144 serialization on elements, share arg checks
What changed, and why it matters
This commit tightens and unifies how transaction serialization arguments are checked in libwally-core. It prevents a caller from accidentally producing ambiguous pre-BIP144 transaction formats on Elements-based networks, and makes the length-calculation and byte-output functions agree on what inputs are valid. The change is defensive hardening rather than a clear-cut fix for an active vulnerability.
Review downstream callers that pass WALLY_TX_FLAG_PRE_BIP144 on Elements to ensure the new silently-cleared behavior does not break assumptions. Consider adding explicit regression tests for zero-input and pre-BIP144 serialization paths on both Bitcoin and Elements networks.
Security signals we found
Inconsistent argument validation between length and serialization functions
Behavior change for Elements: pre-BIP144 flag is now ignored rather than honored
Defensive unification of transaction serialization checks
Potential for ambiguous zero-input transaction encoding on non-Elements networks
Evidence from the diff
The patch introduces a shared helper, tx_check_args(), used by both tx_get_lengths() and tx_to_bytes(). For Elements transactions it now silently clears WALLY_TX_FLAG_PRE_BIP144 instead of allowing pre-BIP144 serialization. For Bitcoin mainnet-style transactions with zero inputs it still requires pre-BIP144 format and clears the witness flag. The previous code performed these checks only in tx_to_bytes(); tx_get_lengths() did not enforce the 0-input/pre-BIP144 rules, so the two code paths could disagree on validity and on whether witness serialization was used.
Changed components
src/transaction.ctx_get_lengths()tx_to_bytes()Elements transaction serializationPre-BIP144 transaction format handlingInspect captured patch +33 / −22
### src/transaction.c
@@ -1705,6 +1705,33 @@ static int get_txin_issuance_size(const struct wally_tx_input *input,
return WALLY_OK;
}
+static bool tx_check_args(const struct wally_tx *tx,
+ uint32_t *flags, bool is_elements)
+{
+ if (!is_valid_tx(tx) || ((*flags) & ~WALLY_TX_ALL_FLAGS))
+ return false;
+
+ if (!((*flags) & WALLY_TX_FLAG_ALLOW_PARTIAL)) {
+ /* 0-input/output txs can be only be written with this flag */
+ if (!tx->num_inputs || !tx->num_outputs)
+ return false;
+ }
+
+ if (is_elements) {
+ /* No pre-BIP 144 serialization for elements */
+ (*flags) &= ~WALLY_TX_FLAG_PRE_BIP144;
+ } else if (!tx->num_inputs) {
+ /* 0-input txs can only be written in the pre-BIP144 format,
+ * since otherwise the resulting tx is ambiguous.
+ * Used in PSBTs while building the tx for example.
+ */
+ if (!((*flags) & WALLY_TX_FLAG_PRE_BIP144))
+ return false;
+ *flags &= ~WALLY_TX_FLAG_USE_WITNESS;
+ }
+ return true;
+}
+
/* We compute the size of the witness separately so we can compute vsize
* without iterating the transaction twice with different flags.
*/
@@ -1719,12 +1746,11 @@ static int tx_get_lengths(const struct wally_tx *tx,
*witness_size = 0;
*witness_count = 0;
- if (!is_valid_tx(tx))
+ if (!tx_check_args(tx, &flags, is_elements))
return WALLY_EINVAL;
- if ((flags & ~WALLY_TX_ALL_FLAGS) ||
- ((flags & WALLY_TX_FLAG_USE_WITNESS) &&
- wally_tx_get_witness_count(tx, witness_count) != WALLY_OK))
+ if ((flags & WALLY_TX_FLAG_USE_WITNESS) &&
+ wally_tx_get_witness_count(tx, witness_count) != WALLY_OK)
return WALLY_EINVAL;
if (!*witness_count)
@@ -2014,26 +2040,11 @@ static int tx_to_bytes(const struct wally_tx *tx,
if (written)
*written = 0;
- if (!is_valid_tx(tx) ||
- (flags & ~WALLY_TX_ALL_FLAGS) || !bytes_out || !written ||
- tx_get_length(tx, flags, &n, is_elements) != WALLY_OK)
+ if (!tx_check_args(tx, &flags, is_elements) || !bytes_out || !written)
return WALLY_EINVAL;
- if (!(flags & WALLY_TX_FLAG_ALLOW_PARTIAL)) {
- /* 0-input/output txs can be only be written with this flag */
- if (!tx->num_inputs || !tx->num_outputs)
- return WALLY_EINVAL;
- }
-
- if (!tx->num_inputs) {
- /* 0-input txs can only be written in the pre-BIP144 format,
- * since otherwise the resulting tx is ambiguous.
- * Used in PSBTs while building the tx for example.
- */
- if (!(flags & WALLY_TX_FLAG_PRE_BIP144))
- return WALLY_EINVAL;
- flags &= ~WALLY_TX_FLAG_USE_WITNESS;
- }
+ if (tx_get_length(tx, flags, &n, is_elements) != WALLY_OK)
+ return WALLY_EINVAL;
if (n > len) {
*written = n;Why this scored 35/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.