Skip to content

psbt: nSequence=0 is silently rewritten to 0xFFFFFFFF, producing signatures over the wrong transaction #146

Open

Description

@brunoerg

InputScope.vin (src/embit/psbt.py:200) uses a truthiness check to apply the BIP-370 default sequence:

return TransactionInput(
    self.txid, self.vout, sequence=(self.sequence or 0xFFFFFFFF)
)

nSequence = 0 is a legal and meaningful value, it signals RBF and enables nLockTime, but it is falsy in Python, so 0 or 0xFFFFFFFF yields 0xFFFFFFFF. Any input whose sequence is 0 is silently rewritten to the final sequence.

Because PSBT.tx (line 665) rebuilds the transaction from inp.vin, and sighash_segwit / sighash_legacy / sighash_taproot (lines 673–680) delegate to self.tx, signing computes the sighash over the rewritten transaction. The resulting signature does not commit to the transaction actually contained in the PSBT.

The same pattern on the next line affects the transaction version:

version=self.tx_version or 2,     # line 667: a wire version of 0 becomes 2

Reproduction

Verified on master @ fff7ffa (2026-06-01) and on the released 0.8.0.

import struct
from embit.psbt import PSBT
from embit.bip32 import HDKey
from embit.transaction import Transaction, TransactionInput, TransactionOutput
from embit.script import Script, p2wpkh, p2pkh
from embit import ec

def cs(n): return bytes([n]) if n < 253 else b'\xfd' + struct.pack('<H', n)
def kv(k, v): return cs(len(k)) + k + cs(len(v)) + v

root = HDKey.from_seed(b'\x01' * 32)
H = 0x80000000
path = [84 | H, 0 | H, 0 | H, 0, 0]
key = root
for idx in path:
    key = key.derive([idx])
pk = key.key.get_public_key()
spk = p2wpkh(pk)

AMT, SEQ, LOCK = 100000, 0, 500000          # SEQ = 0: RBF-signalling, locktime-enabled
txid = bytes.fromhex('aa' * 32)
out_spk = bytes([0x00, 0x14]) + bytes.fromhex('bb' * 20)

tx  = struct.pack('<i', 2) + cs(1) + txid + struct.pack('<I', 0) + cs(0) + struct.pack('<I', SEQ)
tx += cs(1) + struct.pack('<q', 90000) + cs(len(out_spk)) + out_spk + struct.pack('<I', LOCK)

inmap = kv(bytes([0x01]), struct.pack('<q', AMT) + cs(len(spk.data)) + spk.data) \
      + kv(bytes([0x06]) + pk.sec(),
           root.my_fingerprint + b''.join(struct.pack('<I', i) for i in path))
d = b'psbt\xff' + kv(bytes([0x00]), tx) + b'\x00' + inmap + b'\x00' + b'\x00'

p = PSBT.parse(d)
print("sequence on the wire :", hex(SEQ))
print("p.tx.vin[0].sequence :", hex(p.tx.vin[0].sequence))   # -> 0xffffffff
print("round-trips          :", p.serialize() == d)          # -> False

p.sign_with(root)
sig_der = list(p.inputs[0].partial_sigs.values())[0][:-1]

sc = p2pkh(pk)   # BIP143 scriptCode for P2WPKH
def mk(seq):
    return Transaction(version=2, locktime=LOCK,
        vin=[TransactionInput(txid, 0, Script(b''), sequence=seq)],
        vout=[TransactionOutput(90000, Script(out_spk))])

h_real = mk(0).sighash_segwit(0, sc, AMT)
h_rewritten = mk(0xffffffff).sighash_segwit(0, sc, AMT)
print("valid for the PSBT's own tx :", pk.verify(ec.Signature.parse(sig_der), h_real))
print("valid for the rewritten tx  :", pk.verify(ec.Signature.parse(sig_der), h_rewritten))

Output:

sequence on the wire : 0x0
p.tx.vin[0].sequence : 0xffffffff
round-trips          : False
valid for the PSBT's own tx : False
valid for the rewritten tx  : True

Suggested fix

Distinguish "absent" from "zero" explicitly:

@property
def vin(self):
    sequence = 0xFFFFFFFF if self.sequence is None else self.sequence
    return TransactionInput(self.txid, self.vout, sequence=sequence)

and likewise for the version on line 667:

version=2 if self.tx_version is None else self.tx_version,

(locktime=self.locktime or 0 on line 668 is harmless, since the fallback equals the falsy value, but is None there would be more consistent.)


Found it with differential fuzzing of PSBT parsing across implementations (Bitcoin Core, rust-psbt, libwally-core, btcd, NBitcoin, bitcoin-s, embit) in bitcoinfuzz.

Activity

  1. qlrd commented on Aug 16, 2026

    @qlrd
    Contributor

    Hi @brunoerg

    maybe i faced with something close while implementing BIP-322 message signing on Krux (selfcustody/krux#932), and would like your confirmation that it's the same root cause.

    Is this the same issue you found, or just somewhat related? IMO the root looks like the same self.sequence or 0xFFFFFFFF / tx_version or 2 like you reported.

  2. odudex commented on Aug 18, 2026

    @odudex
    Contributor

    This issue, among others, was fixed in 3383238 (PSBTv2 support), part of #145
    This is why I suggest cherry-picking this commit as a priority.

  3. jdlcdl commented on Nov 10, 2026

    @jdlcdl
    Collaborator

    This issue is resolved in "hardened" branch as it was superceded by pr152.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions