Merge pull request #581 from LedgerHQ/client-fixes
What changed, and why it matters
This commit tightens how the Ledger Bitcoin app and its companion client libraries read PSBT (Partially Signed Bitcoin Transaction) data. It adds checks that reject malformed or suspicious PSBTs: truncated fields, unexpected extra bytes after the PSBT, duplicate keys inside PSBT maps, and PSBTv0 files that contain fields only allowed in PSBTv2. It also fixes a C-language integer-overflow check when parsing PSBT maps on the device. These changes are defensive hardening: they prevent the client/device from silently accepting a malformed or intentionally crafted PSBT that could later influence transaction details such as locktime.
Treat this as a security-hardening fix and include it in the next release. Users and integrators should upgrade the Python/JS/Rust client libraries and the device app so that malformed PSBTs are rejected consistently. Wallets that build or convert PSBTs should verify their output still passes the stricter parser, especially PSBTv0 files and v0-to-v2 conversions.
Security signals we found
Hardened PSBT deserialization against truncation and trailing data
Added duplicate-key detection in PSBT maps
Rejected PSBTv2-only fields inside PSBTv0 to prevent locktime/field override attacks
Fixed C integer-overflow/wraparound bounds checks in psbt_parse.c
Cleared per-input required locktimes when converting PSBTv0 to v2 to preserve unsigned-tx locktime
Added regression tests for malformed/trailing/duplicate/v2-only PSBT cases
Evidence from the diff
The patch hardens PSBT deserialization across Python (ledger_bitcoin), TypeScript (bitcoin_client_js), Rust (bitcoin_client_rs), and C device code (unit-tests/libs/psbt_parse.c). Key changes: (1) deser_string now fails if fewer bytes are available than the compact-size length declares; (2) parse_stream_to_map raises on truncation, duplicate keys, and trailing data; (3) PSBT.deserialize raises on truncation and trailing bytes and rejects v2-only fields in v0 PSBTs; (4) convert_to_v2 clears per-input locktime fields so the unsigned tx locktime is preserved; (5) JS PsbtV2 resets internal maps on deserialize, rejects duplicate keys, trailing data, and v2-only fields in v0; (6) Rust get_v2_maps rejects v2-only fields; (7) C psbt_parse switches to subtraction form to avoid pointer/offset wrap and rejects trailing bytes. Remaining changes are test-framework updates (authproxy sync, ragger navigation helpers, instruction counts).
Changed components
bitcoin_client/ledger_bitcoin/_serialize.pybitcoin_client/ledger_bitcoin/client.pybitcoin_client/ledger_bitcoin/psbt.pybitcoin_client_js/src/lib/psbtv2.tsbitcoin_client_rs/src/psbt.rsunit-tests/libs/psbt_parse.cragger_bitcoin/ragger_bitcoin.pytest_utils/authproxy.pyInspect captured patch +544 / −170
### bitcoin_client/ledger_bitcoin/_serialize.py
@@ -80,7 +80,10 @@ def deser_string(f: Readable) -> bytes:
:returns: The byte string that was serialized
"""
nit = deser_compact_size(f)
- return f.read(nit)
+ s = f.read(nit)
+ if len(s) != nit:
+ raise ValueError("Unexpected end of data while reading a string")
+ return s
def ser_string(s: bytes) -> bytes:
"""
### bitcoin_client/ledger_bitcoin/client.py
@@ -18,24 +18,35 @@
from .merkle import get_merkleized_map_commitment
from .wallet import WalletPolicy, WalletType
from .psbt import PSBT, normalize_psbt
-from ._serialize import deser_string
+from ._serialize import deser_compact_size
from .bip0327 import key_agg, cbytes
+def _deser_string_strict(f: BufferedReader) -> bytes:
+ try:
+ length = deser_compact_size(f)
+ except Exception:
+ raise ValueError("Truncated PSBT map")
+ data = f.read(length)
+ if len(data) != length:
+ raise ValueError("Truncated PSBT map")
+ return data
+
+
def parse_stream_to_map(f: BufferedReader) -> Mapping[bytes, bytes]:
result = {}
while True:
- try:
- key = deser_string(f)
- except Exception:
- break
+ key = _deser_string_strict(f)
# Check for separator
if len(key) == 0:
break
- value = deser_string(f)
+ value = _deser_string_strict(f)
+
+ if key in result:
+ raise ValueError(f"Duplicate key in PSBT map: {key.hex()}")
result[key] = value
return result
@@ -317,6 +328,9 @@ def sign_psbt(self, psbt: Union[PSBT, bytes, str], wallet: WalletPolicy, wallet_
for m in output_maps:
client_intepreter.add_known_mapping(m)
+ if f.read() != b"":
+ raise ValueError("Unexpected trailing data after the PSBT maps")
+
# We also add the Merkle tree of the input (resp. output) map commitments as a known tree
input_commitments = [get_merkleized_map_commitment(m_in) for m_in in input_maps]
output_commitments = [get_merkleized_map_commitment(m_out) for m_out in output_maps]
### bitcoin_client/ledger_bitcoin/psbt.py
@@ -180,7 +180,7 @@ def deserialize(self, f: Readable) -> None:
try:
key = deser_string(f)
except Exception:
- break
+ raise PSBTSerializationError("Truncated PSBT map")
# Check for separator
if len(key) == 0:
@@ -416,6 +416,18 @@ def deserialize(self, f: Readable) -> None:
key_lookup.add(key)
+ # Make sure no PSBTv2-only fields are present in PSBTv0
+ if self.version == 0:
+ for key_type in [
+ PartiallySignedInput.PSBT_IN_PREVIOUS_TXID,
+ PartiallySignedInput.PSBT_IN_OUTPUT_INDEX,
+ PartiallySignedInput.PSBT_IN_SEQUENCE,
+ PartiallySignedInput.PSBT_IN_REQUIRED_TIME_LOCKTIME,
+ PartiallySignedInput.PSBT_IN_REQUIRED_HEIGHT_LOCKTIME,
+ ]:
+ if ser_compact_size(key_type) in key_lookup:
+ raise PSBTSerializationError(f"Input field of type {key_type:#04x} is not allowed in PSBTv0")
+
# Make sure required PSBTv2 fields are present
if self.version >= 2:
if len(self.prev_txid) == 0:
@@ -599,7 +611,7 @@ def deserialize(self, f: Readable) -> None:
try:
key = deser_string(f)
except Exception:
- break
+ raise PSBTSerializationError("Truncated PSBT map")
# Check for separator
if len(key) == 0:
@@ -688,6 +700,12 @@ def deserialize(self, f: Readable) -> None:
key_lookup.add(key)
+ # Make sure no PSBTv2-only fields are present in PSBTv0
+ if self.version == 0:
+ for key_type in [PartiallySignedOutput.PSBT_OUT_AMOUNT, PartiallySignedOutput.PSBT_OUT_SCRIPT]:
+ if ser_compact_size(key_type) in key_lookup:
+ raise PSBTSerializationError(f"Output field of type {key_type:#04x} is not allowed in PSBTv0")
+
# Make sure required PSBTv2 fields are present
if self.version >= 2:
if self.amount is None:
@@ -831,7 +849,7 @@ def deserialize(self, psbt: str) -> None:
try:
key = deser_string(f)
except Exception:
- break
+ raise PSBTSerializationError("Truncated PSBT map")
# Check for separator
if len(key) == 0:
@@ -995,6 +1013,9 @@ def deserialize(self, psbt: str) -> None:
if len(self.outputs) != output_count:
raise PSBTSerializationError("Outputs provided does not match the number of outputs in transaction")
+ if f.tell() != end:
+ raise PSBTSerializationError("Unexpected trailing data after the PSBT")
+
self.cache_unsigned_tx_pieces()
def serialize(self) -> str:
@@ -1185,6 +1206,10 @@ def convert_to_v2(self) -> None:
if self.version == 0:
# make sure fields that PSBT version 0 fields are not present
self.setup_from_tx(self.tx)
+ # the locktime of a PSBTv0 is the one of the unsigned transaction
+ for psbt_in in self.inputs:
+ psbt_in.time_locktime = None
+ psbt_in.height_locktime = None
self._convert_version(2)
### bitcoin_client/tests/test_parse_stream_to_map.py
@@ -0,0 +1,77 @@
+import base64
+from io import BytesIO
+
+import pytest
+
+from bitcoin_client.ledger_bitcoin.client import NewClient, parse_stream_to_map
+from bitcoin_client.ledger_bitcoin.errors import PSBTSerializationError
+from bitcoin_client.ledger_bitcoin.psbt import PSBT, PartiallySignedInput, PartiallySignedOutput
+from bitcoin_client.ledger_bitcoin.tx import CTransaction, CTxIn, CTxOut, COutPoint
+from bitcoin_client.ledger_bitcoin.wallet import WalletPolicy
+
+
+def make_psbt_v2() -> PSBT:
+ """A PSBTv2 with one input and one output."""
+ tx = CTransaction()
+ tx.vin = [CTxIn(COutPoint(1, 0), b"", 0)]
+ tx.vout = [CTxOut(1000, b"\x6a")]
+
+ psbt = PSBT(tx)
+ psbt.inputs = [PartiallySignedInput(0)]
+ psbt.outputs = [PartiallySignedOutput(0)]
+ psbt.convert_to_v2()
+ return psbt
+
+
+def parse_output_map(psbt: PSBT):
+ f = BytesIO(base64.b64decode(psbt.serialize()))
+ assert f.read(5) == b"psbt\xff"
+ parse_stream_to_map(f) # global map
+ parse_stream_to_map(f) # input map
+ return parse_stream_to_map(f)
+
+
+def test_parse_stream_to_map():
+ output_map = parse_output_map(make_psbt_v2())
+ assert output_map[b"\x03"] == (1000).to_bytes(8, "little")
+ assert output_map[b"\x04"] == b"\x6a"
+
+
+def test_parse_stream_to_map_rejects_duplicate_keys():
+ # an unknown entry colliding with PSBT_OUT_AMOUNT is serialized as a duplicate key
+ psbt = make_psbt_v2()
+ psbt.outputs[0].unknown[b"\x03"] = (1).to_bytes(8, "little")
+
+ with pytest.raises(ValueError, match="Duplicate key"):
+ parse_output_map(psbt)
+
+
+class NoDeviceTransport:
+ def apdu_exchange(self, *args, **kwargs):
+ raise AssertionError("no APDU must be sent")
+
+
+@pytest.mark.parametrize("as_base64", [False, True])
+def test_sign_psbt_rejects_trailing_data(as_base64: bool):
+ psbt_bytes = base64.b64decode(make_psbt_v2().serialize()) + b"\x00"
+ psbt = base64.b64encode(psbt_bytes).decode() if as_base64 else psbt_bytes
+
+ wallet = WalletPolicy("", "wpkh(@0/**)", ["[f5acc2fd/84'/1'/0']tpubDCtKfsNyRhULjZ9XMS4VKKtVcPdVDi8MKUbcSD9MJDyjRu1A2ND5MiipozyyspBT9bg8upEp7a8EAgFxNxXn1d7QkdbL52Ty5jiSLcxPt1P"])
+ client = NewClient(NoDeviceTransport())
+ with pytest.raises(PSBTSerializationError, match="trailing data"):
+ client.sign_psbt(psbt, wallet, None)
+
+
+# Dropping 1 byte removes the separator of the last output map; dropping 2 bytes also truncates
+# the value of its last entry. Reserializing the PSBT would restore the missing bytes, so these
+# must be rejected while deserializing.
+@pytest.mark.parametrize("as_base64", [False, True])
+@pytest.mark.parametrize("n_dropped", [1, 2])
+def test_sign_psbt_rejects_truncated_psbt(as_base64: bool, n_dropped: int):
+ psbt_bytes = base64.b64decode(make_psbt_v2().serialize())[:-n_dropped]
+ psbt = base64.b64encode(psbt_bytes).decode() if as_base64 else psbt_bytes
+
+ wallet = WalletPolicy("", "wpkh(@0/**)", ["[f5acc2fd/84'/1'/0']tpubDCtKfsNyRhULjZ9XMS4VKKtVcPdVDi8MKUbcSD9MJDyjRu1A2ND5MiipozyyspBT9bg8upEp7a8EAgFxNxXn1d7QkdbL52Ty5jiSLcxPt1P"])
+ client = NewClient(NoDeviceTransport())
+ with pytest.raises((PSBTSerializationError, ValueError)):
+ client.sign_psbt(psbt, wallet, None)
### bitcoin_client/tests/test_psbt_v0_v2_only_fields.py
@@ -0,0 +1,70 @@
+import base64
+
+import pytest
+
+from bitcoin_client.ledger_bitcoin._serialize import ser_string
+from bitcoin_client.ledger_bitcoin.errors import PSBTSerializationError
+from bitcoin_client.ledger_bitcoin.psbt import PSBT
+from bitcoin_client.ledger_bitcoin.tx import CTransaction, CTxIn, CTxOut, COutPoint
+
+
+LOCKTIME = 800000
+
+
+def make_psbt_v0(input_entries: bytes = b"", output_entries: bytes = b"") -> str:
+ """A serialized PSBTv0 with one input and one output, with the given (raw) map entries."""
+ tx = CTransaction()
+ tx.nVersion = 2
+ tx.nLockTime = LOCKTIME
+ tx.vin = [CTxIn(COutPoint(1, 0), b"", 0xfffffffe)]
+ tx.vout = [CTxOut(1000, b"\x6a")]
+
+ psbt = (
+ b"psbt\xff"
+ + ser_string(b"\x00") + ser_string(tx.serialize_without_witness()) # PSBT_GLOBAL_UNSIGNED_TX
+ + b"\x00" # end of global map
+ + input_entries + b"\x00" # end of input map
+ + output_entries + b"\x00" # end of output map
+ )
+ return base64.b64encode(psbt).decode()
+
+
+def entry(key_type: int, value: bytes) -> bytes:
+ return ser_string(bytes([key_type])) + ser_string(value)
+
+
+@pytest.mark.parametrize("input_entry", [
+ entry(0x0e, bytes(32)), # PSBT_IN_PREVIOUS_TXID
+ entry(0x0f, (0).to_bytes(4, "little")), # PSBT_IN_OUTPUT_INDEX
+ entry(0x10, (0).to_bytes(4, "little")), # PSBT_IN_SEQUENCE
+ entry(0x11, (900000000).to_bytes(4, "little")), # PSBT_IN_REQUIRED_TIME_LOCKTIME
+ entry(0x12, (900000).to_bytes(4, "little")), # PSBT_IN_REQUIRED_HEIGHT_LOCKTIME
+])
+def test_psbt_v0_rejects_v2_only_input_fields(input_entry: bytes):
+ psbt = PSBT()
+ with pytest.raises(PSBTSerializationError, match="not allowed in PSBTv0"):
+ psbt.deserialize(make_psbt_v0(input_entries=input_entry))
+
+
+@pytest.mark.parametrize("output_entry", [
+ entry(0x03, (1000).to_bytes(8, "little")), # PSBT_OUT_AMOUNT
+ entry(0x04, b"\x6a"), # PSBT_OUT_SCRIPT
+])
+def test_psbt_v0_rejects_v2_only_output_fields(output_entry: bytes):
+ psbt = PSBT()
+ with pytest.raises(PSBTSerializationError, match="not allowed in PSBTv0"):
+ psbt.deserialize(make_psbt_v0(output_entries=output_entry))
+
+
+def test_psbt_v0_to_v2_keeps_locktime():
+ psbt = PSBT()
+ psbt.deserialize(make_psbt_v0())
+
+ # required locktimes set programmatically on a PSBTv0 are ignored by the conversion
+ psbt.inputs[0].height_locktime = 900000
+ psbt.convert_to_v2()
+
+ psbt_v2 = PSBT()
+ psbt_v2.deserialize(psbt.serialize())
+ assert psbt_v2.version == 2
+ assert psbt_v2.get_unsigned_tx().nLockTime == LOCKTIME
### bitcoin_client_js/src/__tests__/psbtv2.test.ts
@@ -1,5 +1,25 @@
+import * as bjs from "bitcoinjs-lib";
+
import { PsbtV2 } from "../lib/psbtv2";
+// A PSBTv0 with one input and one output, with the given (raw) input map entries
+function makePsbtV0(locktime: number, inputEntries: Buffer = Buffer.alloc(0)): Buffer {
+ const tx = new bjs.Transaction();
+ tx.version = 2;
+ tx.locktime = locktime;
+ tx.addInput(Buffer.alloc(32, 1), 0, 0xfffffffe);
+ tx.addOutput(Buffer.from("6a", "hex"), BigInt(1000));
+ const txRaw = tx.toBuffer();
+
+ return Buffer.concat([
+ Buffer.from("70736274ff", "hex"),
+ Buffer.from([0x01, 0x00, txRaw.length]), txRaw, // PSBT_GLOBAL_UNSIGNED_TX
+ Buffer.from([0x00]), // end of global map
+ inputEntries, Buffer.from([0x00]), // end of input map
+ Buffer.from([0x00]), // end of output map
+ ]);
+}
+
describe("PsbtV2", () => {
it("deserializes a psbtV2 and reserializes it unchanged", async () => {
const psbtBuf = Buffer.from(
@@ -30,4 +50,50 @@ describe("PsbtV2", () => {
expect(psbt.serialize()).toEqual(psbtV2);
});
+
+ it("keeps the unsigned transaction's locktime when converting a psbtV0", async () => {
+ const psbt = new PsbtV2();
+ psbt.deserialize(makePsbtV0(800000));
+
+ expect(psbt.getGlobalFallbackLocktime()).toEqual(800000);
+ });
+
+ it("rejects a psbtV0 with PSBTv2-only fields", async () => {
+ // PSBT_IN_REQUIRED_HEIGHT_LOCKTIME = 1, which would override the fallback locktime
+ const requiredHeightLocktime = Buffer.from("011204" + "01000000", "hex");
+
+ const psbt = new PsbtV2();
+ expect(() => psbt.deserialize(makePsbtV0(800000, requiredHeightLocktime))).toThrow(
+ "PsbtV0 contains fields that are only allowed in PsbtV2"
+ );
+ });
+
+ it("rejects a psbt with a duplicate key", async () => {
+ // PSBT_IN_SIGHASH_TYPE = 3, twice with different values
+ const duplicateSighash = Buffer.from("010304" + "01000000" + "010304" + "02000000", "hex");
+
+ const psbt = new PsbtV2();
+ expect(() => psbt.deserialize(makePsbtV0(0, duplicateSighash))).toThrow(
+ "Duplicate key in PSBT map"
+ );
+ });
+
+ it("can deserialize multiple psbts with the same instance", async () => {
+ const psbt = new PsbtV2();
+ psbt.deserialize(makePsbtV0(800000));
+ const first = psbt.serialize();
+
+ psbt.deserialize(makePsbtV0(800000));
+ expect(psbt.serialize()).toEqual(first);
+
+ psbt.deserialize(makePsbtV0(700000));
+ expect(psbt.getGlobalFallbackLocktime()).toEqual(700000);
+ });
+
+ it("rejects a psbt with trailing data", async () => {
+ const psbt = new PsbtV2();
+ expect(() => psbt.deserialize(Buffer.concat([makePsbtV0(0), Buffer.from([0x00])]))).toThrow(
+ "Unexpected trailing data after the PSBT"
+ );
+ });
});
### bitcoin_client_js/src/lib/psbtv2.ts
@@ -33,6 +33,8 @@ export enum psbtIn {
PREVIOUS_TXID = 0x0e,
OUTPUT_INDEX = 0x0f,
SEQUENCE = 0x10,
+ REQUIRED_TIME_LOCKTIME = 0x11,
+ REQUIRED_HEIGHT_LOCKTIME = 0x12,
TAP_KEY_SIG = 0x13,
TAP_BIP32_DERIVATION = 0x16,
}
@@ -47,7 +49,7 @@ export enum psbtOut {
const PSBT_MAGIC_BYTES = Buffer.from([0x70, 0x73, 0x62, 0x74, 0xff]);
-export class NoSuchEntry extends Error {}
+export class NoSuchEntry extends Error { }
/**
* Implements Partially Signed Bitcoin Transaction version 2, BIP370, as
@@ -369,6 +371,10 @@ export class PsbtV2 {
if (!buf.readSlice(5).equals(PSBT_MAGIC_BYTES)) {
throw new Error('Invalid magic bytes');
}
+ // discard any previous content, so that it does not mix with the new PSBT's
+ this.globalMap = new Map();
+ this.inputMaps = [];
+ this.outputMaps = [];
while (this.readKeyPair(this.globalMap, buf));
let psbtVersion: number;
@@ -403,6 +409,10 @@ export class PsbtV2 {
while (this.readKeyPair(this.outputMaps[i], buf));
}
+ if (buf.offset !== psbt.length) {
+ throw new Error('Unexpected trailing data after the PSBT');
+ }
+
this.normalizeToV2();
}
normalizeToV2() {
@@ -414,6 +424,31 @@ export class PsbtV2 {
throw new Error('Invalid or unsupported value for PSBT_GLOBAL_VERSION');
}
+ // Fail if fields only allowed in PsbtV2 are present in a PsbtV0
+ const hasKeyType = (map: ReadonlyMap<string, Buffer>, keyTypes: readonly KeyType[]) =>
+ Array.from(map.keys()).some((k) => this.isKeyType(k, keyTypes));
+ if (
+ hasKeyType(this.globalMap, [
+ psbtGlobal.TX_VERSION,
+ psbtGlobal.FALLBACK_LOCKTIME,
+ psbtGlobal.INPUT_COUNT,
+ psbtGlobal.OUTPUT_COUNT,
+ psbtGlobal.TX_MODIFIABLE,
+ ]) ||
+ this.inputMaps.some((m) =>
+ hasKeyType(m, [
+ psbtIn.PREVIOUS_TXID,
+ psbtIn.OUTPUT_INDEX,
+ psbtIn.SEQUENCE,
+ psbtIn.REQUIRED_TIME_LOCKTIME,
+ psbtIn.REQUIRED_HEIGHT_LOCKTIME,
+ ])
+ ) ||
+ this.outputMaps.some((m) => hasKeyType(m, [psbtOut.AMOUNT, psbtOut.SCRIPT]))
+ ) {
+ throw new Error('PsbtV0 contains fields that are only allowed in PsbtV2');
+ }
+
// Convert PsbtV0 to PsbtV2 by parsing the PSBT_GLOBAL_UNSIGNED_TX field
// and filling in the corresponding fields.
const txRaw = this.getGlobal(psbtGlobal.UNSIGNED_TX);
@@ -450,7 +485,7 @@ export class PsbtV2 {
* Note: This method supports all the policies that the Ledger is able to
* sign, with the exception of taproot: tr(@0).
*/
- fromBitcoinJS(psbtBJS: bjs.Psbt) : PsbtV2 {
+ fromBitcoinJS(psbtBJS: bjs.Psbt): PsbtV2 {
function isTaprootInput(input): boolean {
let isP2TR;
try {
@@ -529,6 +564,9 @@ export class PsbtV2 {
const keyType = buf.readUInt8();
const keyData = buf.readSlice(keyLen - 1);
const value = buf.readVarSlice();
+ if (map.has(new Key(keyType, keyData).toString())) {
+ throw new Error('Duplicate key in PSBT map');
+ }
set(map, keyType, keyData, value);
return true;
### bitcoin_client_rs/src/psbt.rs
@@ -27,11 +27,14 @@ const PSBT_GLOBAL_TX_VERSION: u8 = 0x02;
const PSBT_GLOBAL_FALLBACK_LOCKTIME: u8 = 0x03;
const PSBT_GLOBAL_INPUT_COUNT: u8 = 0x04;
const PSBT_GLOBAL_OUTPUT_COUNT: u8 = 0x05;
+const PSBT_GLOBAL_TX_MODIFIABLE: u8 = 0x06;
const PSBT_GLOBAL_VERSION: u8 = 0xFB;
const PSBT_IN_PREVIOUS_TXID: u8 = 0x0E;
const PSBT_IN_OUTPUT_INDEX: u8 = 0x0F;
const PSBT_IN_SEQUENCE: u8 = 0x10;
+const PSBT_IN_REQUIRED_TIME_LOCKTIME: u8 = 0x11;
+const PSBT_IN_REQUIRED_HEIGHT_LOCKTIME: u8 = 0x12;
const PSBT_OUT_AMOUNT: u8 = 0x03;
const PSBT_OUT_SCRIPT: u8 = 0x04;
@@ -81,6 +84,34 @@ pub fn get_v2_maps(psbt: &Psbt) -> Result<PsbtV2Maps, PsbtV2Error> {
let mut inputs = maps.split_off(1);
let mut global = maps.pop().ok_or(PsbtV2Error::MalformedSerialization)?;
+ // Fields that BIP-370 excludes from PSBTv0 would acquire meaning after the conversion
+ // (e.g. the required locktimes override the fallback locktime), so they are rejected.
+ reject_v2_fields(
+ &global,
+ &[
+ PSBT_GLOBAL_TX_VERSION,
+ PSBT_GLOBAL_FALLBACK_LOCKTIME,
+ PSBT_GLOBAL_INPUT_COUNT,
+ PSBT_GLOBAL_OUTPUT_COUNT,
+ PSBT_GLOBAL_TX_MODIFIABLE,
+ ],
+ )?;
+ for map in &inputs {
+ reject_v2_fields(
+ map,
+ &[
+ PSBT_IN_PREVIOUS_TXID,
+ PSBT_IN_OUTPUT_INDEX,
+ PSBT_IN_SEQUENCE,
+ PSBT_IN_REQUIRED_TIME_LOCKTIME,
+ PSBT_IN_REQUIRED_HEIGHT_LOCKTIME,
+ ],
+ )?;
+ }
+ for map in &outputs {
+ reject_v2_fields(map, &[PSBT_OUT_AMOUNT, PSBT_OUT_SCRIPT])?;
+ }
+
global.retain(|(key, _)| key.as_slice() != [PSBT_GLOBAL_UNSIGNED_TX]);
push_v2_field(
@@ -143,6 +174,17 @@ pub fn get_v2_maps(psbt: &Psbt) -> Result<PsbtV2Maps, PsbtV2Error> {
})
}
+/// Errors if `map` has a key of any of `key_types`, whatever its keydata.
+fn reject_v2_fields(map: &PsbtMap, key_types: &[u8]) -> Result<(), PsbtV2Error> {
+ match map
+ .iter()
+ .find_map(|(key, _)| key.first().filter(|t| key_types.contains(t)))
+ {
+ Some(key_type) => Err(PsbtV2Error::UnexpectedV2Field(*key_type)),
+ None => Ok(()),
+ }
+}
+
/// Adds a keyless PSBTv2 field synthesized from the v0 unsigned transaction, erroring if the
/// PSBT already carries that key.
///
@@ -341,23 +383,38 @@ mod tests {
assert_eq!(values_for(&maps.outputs[0], PSBT_OUT_SCRIPT).len(), 1);
}
- /// A key type rust-bitcoin does not know must still reach the app: this is what lets it read
- /// the BIP-370 per-input required locktimes.
+ /// A key type rust-bitcoin does not know must still reach the app.
#[test]
fn keys_unknown_to_rust_bitcoin_are_forwarded() {
- const PSBT_IN_REQUIRED_HEIGHT_LOCKTIME: u8 = 0x12;
- let height = 10_000_u32.to_le_bytes().to_vec();
+ const UNKNOWN_KEY_TYPE: u8 = 0x30;
+ let value = vec![1, 2, 3];
let mut psbt = unsigned_psbt();
psbt.inputs[0]
.unknown
- .insert(keyless(PSBT_IN_REQUIRED_HEIGHT_LOCKTIME), height.clone());
+ .insert(keyless(UNKNOWN_KEY_TYPE), value.clone());
let maps = get_v2_maps(&psbt).unwrap();
- assert_eq!(
- values_for(&maps.inputs[0], PSBT_IN_REQUIRED_HEIGHT_LOCKTIME),
- vec![&height]
- );
+ assert_eq!(values_for(&maps.inputs[0], UNKNOWN_KEY_TYPE), vec![&value]);
+ }
+
+ /// The BIP-370 per-input required locktimes are excluded from PSBTv0: once converted, they
+ /// would override the locktime of the unsigned transaction.
+ #[test]
+ fn required_locktimes_are_rejected() {
+ for key_type in [
+ PSBT_IN_REQUIRED_TIME_LOCKTIME,
+ PSBT_IN_REQUIRED_HEIGHT_LOCKTIME,
+ ] {
+ let mut psbt = unsigned_psbt();
+ psbt.inputs[0]
+ .unknown
+ .insert(keyless(key_type), 1_u32.to_le_bytes().to_vec());
+ assert_eq!(
+ get_v2_maps(&psbt).err(),
+ Some(PsbtV2Error::UnexpectedV2Field(key_type))
+ );
+ }
}
/// A v0 PSBT that already re-encodes part of its unsigned transaction as a v2 field is a
### ragger_bitcoin/ragger_bitcoin.py
@@ -1,4 +1,5 @@
-from typing import Tuple, List, Optional, Union
+from contextlib import contextmanager
+from typing import Iterator, Tuple, List, Optional, Union
from pathlib import Path
from ledger_bitcoin.common import Chain
@@ -9,7 +10,7 @@
from ledger_bitcoin.client import NewClient
from ledger_bitcoin.client_base import print_response, print_apdu, ApduException
-from ragger.navigator import Navigator
+from ragger.navigator import Navigator, NavInsID
from ragger_bitcoin.ragger_instructions import Instructions
from ragger.utils import pack_APDU
@@ -59,6 +60,20 @@ def _make_request(
self, apdu, client_intepreter)
return sw, response
+ @contextmanager
+ def _navigation(self, navigator: Optional[Navigator], testname: str,
+ instructions: Optional[Instructions]) -> Iterator[None]:
+ # Navigation applies to the requests of this call only: not to the requests of a nested
+ # call without a navigator (e.g. the address check in register_wallet), nor to later calls
+ # if this one raises.
+ saved = (self.navigate, self.navigator, self.testname, self.instructions)
+ self.navigate = navigator is not None
+ self.navigator, self.testname, self.instructions = navigator, testname, instructions
+ try:
+ yield
+ finally:
+ self.navigate, self.navigator, self.testname, self.instructions = saved
+
def last_async_response(self) -> Tuple[int, bytes]:
return self.transport_client.last_async_response.status, self.transport_client.last_async_response.data
@@ -123,36 +138,34 @@ def _make_request_with_navigation(self, navigator: Navigator, apdu: dict, client
sw, response, index = self.ragger_navigate(
navigator, apdu, instructions, testname, index)
+
+ # A request that succeeds without showing all the expected screens skipped the review.
+ # A final status screen (e.g. "Transaction signed") does not block the APDU, so it is only
+ # navigated if a later APDU happens to still be pending: it is not required.
+ if sw == 0x9000 and instructions:
+ required = len(instructions.data['text'])
+ if required > 0 and all(i == NavInsID.USE_CASE_STATUS_DISMISS
+ for i in instructions.data['instruction_on_text'][-1]):
+ required -= 1
+ if index < required:
+ raise RuntimeError(
+ f"Request completed after {index} of the {required} expected UI interactions")
return sw, response
def get_extended_pubkey(self, path: str, display: bool = False, navigator: Optional[Navigator] = None,
testname: str = "",
instructions: Instructions = None) -> str:
- if navigator:
- self.navigate = True
- self.navigator = navigator
- self.testname = testname
- self.instructions = instructions
-
- response = NewClient.get_extended_pubkey(self, path, display)
-
- self.navigate = False
+ with self._navigation(navigator, testname, instructions):
+ response = NewClient.get_extended_pubkey(self, path, display)
return response
def register_wallet(self, wallet: WalletPolicy, navigator: Optional[Navigator] = None,
testname: str = "", instructions: Instructions = None) -> Tuple[bytes, bytes]:
- if navigator:
- self.navigate = True
- self.navigator = navigator
- self.testname = testname
- self.instructions = instructions
-
- wallet_id, wallet_hmac = NewClient.register_wallet(self, wallet)
-
- self.navigate = False
+ with self._navigation(navigator, testname, instructions):
+ wallet_id, wallet_hmac = NewClient.register_wallet(self, wallet)
return wallet_id, wallet_hmac
@@ -168,32 +181,18 @@ def get_wallet_address(
testname: str = ""
) -> str:
- if navigator:
- self.navigate = True
- self.navigator = navigator
- self.testname = testname
- self.instructions = instructions
-
- result = NewClient.get_wallet_address(
- self, wallet, wallet_hmac, change, address_index, display)
-
- self.navigate = False
+ with self._navigation(navigator, testname, instructions):
+ result = NewClient.get_wallet_address(
+ self, wallet, wallet_hmac, change, address_index, display)
return result
def sign_psbt(self, psbt: Union[PSBT, bytes, str], wallet: WalletPolicy, wallet_hmac:
Optional[bytes], navigator: Optional[Navigator] = None,
testname: str = "", instructions: Instructions = None) -> List[Tuple[int, PartialSignature]]:
- if navigator:
- self.navigate = True
- self.navigator = navigator
- self.testname = testname
- self.instructions = instructions
-
- result = NewClient.sign_psbt(self, psbt, wallet, wallet_hmac)
-
- self.navigate = False
+ with self._navigation(navigator, testname, instructions):
+ result = NewClient.sign_psbt(self, psbt, wallet, wallet_hmac)
return result
@@ -203,15 +202,8 @@ def sign_message(self, message: Union[str, bytes], bip32_path: str, navigator:
testname: str = ""
) -> str:
- if navigator:
- self.navigate = True
- self.navigator = navigator
- self.testname = testname
- self.instructions = instructions
-
- response = NewClient.sign_message(self, message, bip32_path)
-
- self.navigate = False
+ with self._navigation(navigator, testname, instructions):
+ response = NewClient.sign_message(self, message, bip32_path)
return response
### test_utils/authproxy.py
@@ -20,16 +20,18 @@
# along with this software; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
-# Taken from https://raw.githubusercontent.com/bitcoin/bitcoin/fad02274ba28189f28f5af536b8ea0ecddf9df55/test/functional/test_framework/authproxy.py
-
+# Taken from https://raw.githubusercontent.com/bitcoin/bitcoin/f42226d526ebb3eb9217e738108e4f8148a1b069/test/functional/test_framework/authproxy.py
+# Local changes:
+# - JSONRPCException and assert_equal are defined in this file, instead of being imported from
+# test_framework/util.py.
"""HTTP proxy for opening RPC connection to bitcoind.
AuthServiceProxy has the following improvements over python-jsonrpc's
ServiceProxy class:
- HTTP connections persist for the life of the AuthServiceProxy object
(if server supports HTTP/1.1)
-- sends protocol 'version', per JSON-RPC 1.1
+- sends "jsonrpc":"2.0", per JSON-RPC 2.0
- sends proper, incrementing 'id'
- sends Basic HTTP authentication headers
- parses all JSON numbers that look like floats as Decimal
@@ -42,16 +44,18 @@
import http.client
import json
import logging
-import os
+import pathlib
import socket
import time
import urllib.parse
+
HTTP_TIMEOUT = 30
USER_AGENT = "AuthServiceProxy/0.1"
log = logging.getLogger("BitcoinRPC")
+
class JSONRPCException(Exception):
def __init__(self, rpc_error, http_status=None):
try:
@@ -63,9 +67,16 @@ def __init__(self, rpc_error, http_status=None):
self.http_status = http_status
-def EncodeDecimal(o):
+def assert_equal(thing1, thing2):
+ if thing1 != thing2:
+ raise AssertionError("not(%s == %s)" % (str(thing1), str(thing2)))
+
+
+def serialization_fallback(o):
if isinstance(o, decimal.Decimal):
return str(o)
+ if isinstance(o, pathlib.Path):
+ return str(o)
raise TypeError(repr(o) + " is not JSON serializable")
class AuthServiceProxy():
@@ -76,12 +87,16 @@ def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connect
self.__service_url = service_url
self._service_name = service_name
self.ensure_ascii = ensure_ascii # can be toggled on the fly by tests
+ self.reuse_http_connections = True
self.__url = urllib.parse.urlparse(service_url)
user = None if self.__url.username is None else self.__url.username.encode('utf8')
passwd = None if self.__url.password is None else self.__url.password.encode('utf8')
authpair = user + b':' + passwd
self.__auth_header = b'Basic ' + base64.b64encode(authpair)
- self.timeout = timeout
+ # clamp the socket timeout, since larger values can cause an
+ # "Invalid argument" exception in Python's HTTP(S) client
+ # library on some operating systems (e.g. OpenBSD, FreeBSD)
+ self.timeout = min(timeout, 2147483)
self._set_conn(connection)
def __getattr__(self, name):
@@ -90,77 +105,79 @@ def __getattr__(self, name):
raise AttributeError
if self._service_name is not None:
name = "%s.%s" % (self._service_name, name)
+ if not self.reuse_http_connections:
+ self._set_conn()
return AuthServiceProxy(self.__service_url, name, connection=self.__conn)
def _request(self, method, path, postdata):
'''
- Do a HTTP request, with retry if we get disconnected (e.g. due to a timeout).
- This is a workaround for https://bugs.python.org/issue3566 which is fixed in Python 3.5.
+ Do a HTTP request.
'''
headers = {'Host': self.__url.hostname,
'User-Agent': USER_AGENT,
'Authorization': self.__auth_header,
'Content-type': 'application/json'}
- if os.name == 'nt':
- # Windows somehow does not like to re-use connections
- # TODO: Find out why the connection would disconnect occasionally and make it reusable on Windows
- # Avoid "ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine"
+ if not self.reuse_http_connections:
self._set_conn()
- try:
- self.__conn.request(method, path, postdata, headers)
- return self._get_response()
- except (BrokenPipeError, ConnectionResetError):
- # Python 3.5+ raises BrokenPipeError when the connection was reset
- # ConnectionResetError happens on FreeBSD
- self.__conn.close()
- self.__conn.request(method, path, postdata, headers)
- return self._get_response()
- except OSError as e:
- # Workaround for a bug on macOS. See https://bugs.python.org/issue33450
- retry = '[Errno 41] Protocol wrong type for socket' in str(e)
- if retry:
- self.__conn.close()
- self.__conn.request(method, path, postdata, headers)
- return self._get_response()
- else:
- raise
+ self.__conn.request(method, path, postdata, headers)
+ return self._get_response()
+
+ def _json_dumps(self, obj):
+ return json.dumps(obj, default=serialization_fallback, ensure_ascii=self.ensure_ascii)
def get_request(self, *args, **argsn):
AuthServiceProxy.__id_count += 1
- log.debug("-{}-> {} {}".format(
+ log.debug("-{}-> {} {} {}".format(
AuthServiceProxy.__id_count,
self._service_name,
- json.dumps(args or argsn, default=EncodeDecimal, ensure_ascii=self.ensure_ascii),
+ self._json_dumps(args),
+ self._json_dumps(argsn),
))
+
if args and argsn:
- raise ValueError('Cannot handle both named and positional arguments')
- return {'version': '1.1',
+ params = dict(args=args, **argsn)
+ else:
+ params = args or argsn
+ return {'jsonrpc': '2.0',
'method': self._service_name,
- 'params': args or argsn,
+ 'params': params,
'id': AuthServiceProxy.__id_count}
def __call__(self, *args, **argsn):
- postdata = json.dumps(self.get_request(*args, **argsn), default=EncodeDecimal, ensure_ascii=self.ensure_ascii)
+ postdata = self._json_dumps(self.get_request(*args, **argsn))
response, status = self._request('POST', self.__url.path, postdata.encode('utf-8'))
- if response['error'] is not None:
- raise JSONRPCException(response['error'], status)
- elif 'result' not in response:
- raise JSONRPCException({
- 'code': -343, 'message': 'missing JSON-RPC result'}, status)
- elif status != HTTPStatus.OK:
- raise JSONRPCException({
- 'code': -342, 'message': 'non-200 HTTP status code but no JSON-RPC error'}, status)
+ # For backwards compatibility tests, accept JSON RPC 1.1 responses
+ if 'jsonrpc' not in response:
+ if response['error'] is not None:
+ raise JSONRPCException(response['error'], status)
+ elif 'result' not in response:
+ raise JSONRPCException({
+ 'code': -343, 'message': 'missing JSON-RPC result'}, status)
+ elif status != HTTPStatus.OK:
+ raise JSONRPCException({
+ 'code': -342, 'message': 'non-200 HTTP status code but no JSON-RPC error'}, status)
+ else:
+ return response['result']
else:
+ assert_equal(response['jsonrpc'], '2.0')
+ if status != HTTPStatus.OK:
+ raise JSONRPCException({
+ 'code': -342, 'message': 'non-200 HTTP status code'}, status)
+ if 'error' in response:
+ raise JSONRPCException(response['error'], status)
+ elif 'result' not in response:
+ raise JSONRPCException({
+ 'code': -343, 'message': 'missing JSON-RPC 2.0 result and error'}, status)
return response['result']
def batch(self, rpc_call_list):
- postdata = json.dumps(list(rpc_call_list), default=EncodeDecimal, ensure_ascii=self.ensure_ascii)
+ postdata = self._json_dumps(list(rpc_call_list))
log.debug("--> " + postdata)
response, status = self._request('POST', self.__url.path, postdata.encode('utf-8'))
if status != HTTPStatus.OK:
raise JSONRPCException({
- 'code': -342, 'message': 'non-200 HTTP status code but no JSON-RPC error'}, status)
+ 'code': -342, 'message': 'non-200 HTTP status code'}, status)
return response
def _get_response(self):
@@ -173,22 +190,36 @@ def _get_response(self):
'message': '%r RPC took longer than %f seconds. Consider '
'using larger timeout for calls that take '
'longer to return.' % (self._service_name,
- self.__conn.timeout)})
+ self.__conn.timeout)}) from None
if http_response is None:
raise JSONRPCException({
'code': -342, 'message': 'missing HTTP response from server'})
+ # Check for no-content HTTP status code, which can be returned when an
+ # RPC client requests a JSON-RPC 2.0 "notification" with no response.
+ # Currently this is only possible if clients call the _request() method
+ # directly to send a raw request.
+ if http_response.status == HTTPStatus.NO_CONTENT:
+ if len(http_response.read()) != 0:
+ raise JSONRPCException({'code': -342, 'message': 'Content received with NO CONTENT status code'})
+ return None, http_response.status
+
content_type = http_response.getheader('Content-Type')
if content_type != 'application/json':
raise JSONRPCException(
- {'code': -342, 'message': 'non-JSON HTTP response with \'%i %s\' from server' % (http_response.status, http_response.reason)},
+ {'code': -342, 'message': f"non-JSON HTTP response with \'{http_response.status} {http_response.reason}\' from server: {http_response.read().decode()}"},
http_response.status)
- responsedata = http_response.read().decode('utf8')
+ data = http_response.read()
+ try:
+ responsedata = data.decode('utf8')
+ except UnicodeDecodeError as e:
+ raise JSONRPCException({
+ 'code': -342, 'message': f'Cannot decode response in utf8 format, content: {data}, exception: {e}'})
response = json.loads(responsedata, parse_float=decimal.Decimal)
elapsed = time.time() - req_start_time
if "error" in response and response["error"] is None:
- log.debug("<-%s- [%.6f] %s" % (response["id"], elapsed, json.dumps(response["result"], default=EncodeDecimal, ensure_ascii=self.ensure_ascii)))
+ log.debug("<-%s- [%.6f] %s" % (response["id"], elapsed, self._json_dumps(response["result"])))
else:
log.debug("<-- [%.6f] %s" % (elapsed, responsedata))
return response, http_response.status
### tests/conftest.py
@@ -75,12 +75,20 @@ def additional_speculos_arguments(request) -> List[str]:
btc_addr = ""
+def _new_rpc(url: str) -> AuthServiceProxy:
+ rpc = AuthServiceProxy(url)
+ # Use a new connection for each call, as bitcoind closes idle connections after
+ # -rpcservertimeout, and tests can be idle for long while interacting with the device.
+ rpc.reuse_http_connections = False
+ return rpc
+
+
def get_rpc() -> AuthServiceProxy:
- return AuthServiceProxy(rpc_url)
+ return _new_rpc(rpc_url)
def get_wallet_rpc(wallet_name: str) -> AuthServiceProxy:
- return AuthServiceProxy(f"{rpc_url}/wallet/{wallet_name}")
+ return _new_rpc(f"{rpc_url}/wallet/{wallet_name}")
def setup_node():
### tests/instructions.py
@@ -140,8 +140,6 @@ def register_wallet_instruction_approve(model: Firmware, save_screenshot=True) -
instructions.new_request("Register account", save_screenshot=save_screenshot)
else:
instructions.choice_confirm(save_screenshot=save_screenshot)
- instructions.choice_confirm(save_screenshot=save_screenshot)
- instructions.choice_confirm(save_screenshot=save_screenshot)
return instructions
@@ -152,8 +150,6 @@ def register_wallet_instruction_approve_no_save(model: Firmware) -> Instructions
instructions.new_request("Register account", save_screenshot=False)
else:
instructions.choice_confirm(save_screenshot=False)
- instructions.choice_confirm(save_screenshot=False)
- instructions.choice_confirm(save_screenshot=False)
return instructions
@@ -164,9 +160,6 @@ def register_wallet_instruction_approve_long(model: Firmware, save_screenshot=Tr
instructions.new_request("Register account", save_screenshot=save_screenshot)
else:
instructions.choice_confirm(save_screenshot=save_screenshot)
- instructions.choice_confirm(save_screenshot=save_screenshot)
- instructions.choice_confirm(save_screenshot=save_screenshot)
- instructions.choice_confirm(save_screenshot=save_screenshot)
return instructions
@@ -177,7 +170,6 @@ def register_wallet_instruction_approve_unusual(model: Firmware) -> Instructions
instructions.new_request("Register account")
else:
instructions.choice_confirm()
- instructions.choice_confirm()
return instructions
@@ -334,15 +326,13 @@ def sign_psbt_instruction_approve_generic(model: Firmware, output_count: int, sa
return instructions
-def e2e_register_wallet_instruction(model: Firmware, n_keys) -> Instructions:
+def e2e_register_wallet_instruction(model: Firmware) -> Instructions:
instructions = Instructions(model)
if model.name.startswith("nano"):
instructions.new_request("Register account", save_screenshot=False)
else:
- for _ in range(n_keys + 1):
- instructions.choice_confirm(save_screenshot=False)
- instructions.choice_confirm(save_screenshot=False)
+ instructions.choice_confirm(save_screenshot=False)
return instructions
### tests/test_e2e_miniscript.py
@@ -174,7 +174,7 @@ def test_e2e_miniscript_one_of_two_1(navigator: Navigator, firmware: Firmware, c
])
run_test_e2e(navigator, client, wallet_policy, [], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_miniscript_one_of_two_2(navigator: Navigator, firmware: Firmware, client: RaggerClient,
@@ -194,7 +194,7 @@ def test_e2e_miniscript_one_of_two_2(navigator: Navigator, firmware: Firmware, c
])
run_test_e2e(navigator, client, wallet_policy, [_], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_miniscript_2fa(navigator: Navigator, firmware: Firmware, client: RaggerClient,
@@ -214,7 +214,7 @@ def test_e2e_miniscript_2fa(navigator: Navigator, firmware: Firmware, client: Ra
])
run_test_e2e(navigator, client, wallet_policy, [core_wallet_name], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_miniscript_decaying_3of3(navigator: Navigator, firmware: Firmware, client:
@@ -236,7 +236,7 @@ def test_e2e_miniscript_decaying_3of3(navigator: Navigator, firmware: Firmware,
])
run_test_e2e(navigator, client, wallet_policy, [core_wallet_name1, core_wallet_name2], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_miniscript_bolt3_offered_htlc(navigator: Navigator, firmware: Firmware, client:
@@ -259,7 +259,7 @@ def test_e2e_miniscript_bolt3_offered_htlc(navigator: Navigator, firmware: Firmw
])
run_test_e2e(navigator, client, wallet_policy, [core_wallet_name1, core_wallet_name2], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_miniscript_bolt3_received_htlc(navigator: Navigator, firmware: Firmware, client:
@@ -282,7 +282,7 @@ def test_e2e_miniscript_bolt3_received_htlc(navigator: Navigator, firmware: Firm
])
run_test_e2e(navigator, client, wallet_policy, [core_wallet_name1, core_wallet_name2], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_miniscript_me_or_3of5(navigator: Navigator, firmware: Firmware, client:
@@ -309,7 +309,7 @@ def test_e2e_miniscript_me_or_3of5(navigator: Navigator, firmware: Firmware, cli
])
run_test_e2e(navigator, client, wallet_policy, [], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_miniscript_me_large_vault(navigator: Navigator, firmware: Firmware, client:
@@ -338,7 +338,7 @@ def test_e2e_miniscript_me_large_vault(navigator: Navigator, firmware: Firmware,
])
run_test_e2e(navigator, client, wallet_policy, [], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_miniscript_me_and_bob_or_me_and_carl_1(navigator: Navigator, firmware: Firmware,
@@ -367,7 +367,7 @@ def test_e2e_miniscript_me_and_bob_or_me_and_carl_1(navigator: Navigator, firmwa
])
run_test_e2e(navigator, client, wallet_policy, [core_wallet_name1], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_miniscript_policy_with_a(navigator: Navigator, firmware: Firmware, client:
@@ -395,7 +395,7 @@ def test_e2e_miniscript_policy_with_a(navigator: Navigator, firmware: Firmware,
])
run_test_e2e(navigator, client, wallet_policy, [core_wallet_name2], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_invalid_miniscript(navigator: Navigator, firmware: Firmware, client: RaggerClient,
### tests/test_e2e_multisig.py
@@ -161,8 +161,7 @@ def test_e2e_multisig_2_of_2(navigator: Navigator, firmware: Firmware, client: R
)
run_test(navigator, client, wallet_policy, [core_wallet_name], rpc, rpc_test_wallet,
- speculos_globals, e2e_register_wallet_instruction(
- firmware, wallet_policy.n_keys),
+ speculos_globals, e2e_register_wallet_instruction(firmware),
e2e_sign_psbt_instruction(firmware), test_name)
@@ -195,7 +194,7 @@ def test_e2e_multisig_multiple_internal_keys(navigator: Navigator, firmware: Fir
run_test(navigator, client, wallet_policy, [core_wallet_name_3],
rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys),
+ e2e_register_wallet_instruction(firmware),
e2e_sign_psbt_instruction(firmware), test_name)
@@ -229,5 +228,5 @@ def test_e2e_multisig_15keys(navigator: Navigator, firmware: Firmware, client: R
run_test(navigator, client, wallet_policy, core_wallet_names,
rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys),
+ e2e_register_wallet_instruction(firmware),
e2e_sign_psbt_instruction(firmware), test_name)
### tests/test_e2e_musig2.py
@@ -134,9 +134,9 @@ def run_test_e2e_musig2(navigator: Navigator, client: RaggerClient, wallet_polic
psbt_b64 = psbt.serialize()
psbt_stripped = strip_non_musig2_derivations(psbt)
- hww_yielded: List[Tuple[int, SignPsbtYieldedObject]] = client.sign_psbt(psbt_stripped, wallet_policy, wallet_hmac, navigator,
- instructions=instructions_sign_psbt,
- testname=f"{test_name}_sign")
+ # Round 1 does not require navigation instructions, since it is silent
+ hww_yielded: List[Tuple[int, SignPsbtYieldedObject]] = client.sign_psbt(
+ psbt_stripped, wallet_policy, wallet_hmac)
for (input_index, yielded) in hww_yielded:
if isinstance(yielded, MusigPubNonce):
@@ -254,7 +254,7 @@ def test_e2e_musig2_keypath(navigator: Navigator, firmware: Firmware, client: Ra
])
run_test_e2e_musig2(navigator, client, wallet_policy, [core_wallet_name], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_musig2_keypath2(navigator: Navigator, firmware: Firmware, client: RaggerClient,
@@ -276,7 +276,7 @@ def test_e2e_musig2_keypath2(navigator: Navigator, firmware: Firmware, client: R
])
run_test_e2e_musig2(navigator, client, wallet_policy, [core_wallet_name], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_musig2_scriptpath(navigator: Navigator, firmware: Firmware, client: RaggerClient,
@@ -298,7 +298,7 @@ def test_e2e_musig2_scriptpath(navigator: Navigator, firmware: Firmware, client:
])
run_test_e2e_musig2(navigator, client, wallet_policy, [core_wallet_name], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_musig2_multi_a_scriptpath(navigator: Navigator, firmware: Firmware, client: RaggerClient,
@@ -324,7 +324,7 @@ def test_e2e_musig2_multi_a_scriptpath(navigator: Navigator, firmware: Firmware,
])
run_test_e2e_musig2(navigator, client, wallet_policy, [core_wallet_name_1, core_wallet_name_2], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_musig2_3of3keypath_decaying_scriptpath(navigator: Navigator, firmware: Firmware, client: RaggerClient,
@@ -345,7 +345,7 @@ def test_e2e_musig2_3of3keypath_decaying_scriptpath(navigator: Navigator, firmwa
])
run_test_e2e_musig2(navigator, client, wallet_policy, [core_wallet_name_1, core_wallet_name_2], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_musig2_5of5(navigator: Navigator, firmware: Firmware, client: RaggerClient,
@@ -385,7 +385,7 @@ def test_e2e_musig2_5of5(navigator: Navigator, firmware: Firmware, client: Ragge
])
run_test_e2e_musig2(navigator, client, wallet_policy, core_wallet_names, rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_musig_invalid(client: RaggerClient, speculos_globals: SpeculosGlobals):
### tests/test_e2e_tapscripts.py
@@ -176,7 +176,7 @@ def test_e2e_tapscript_one_of_two_keypath(navigator: Navigator, firmware: Firmwa
])
run_test_e2e(navigator, client, wallet_policy, [], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_tapscript_one_of_two_scriptpath(navigator: Navigator, firmware: Firmware, client:
@@ -196,7 +196,7 @@ def test_e2e_tapscript_one_of_two_scriptpath(navigator: Navigator, firmware: Fir
])
run_test_e2e(navigator, client, wallet_policy, [], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_tapscript_one_of_three_keypath(navigator: Navigator, firmware: Firmware, client:
@@ -218,7 +218,7 @@ def test_e2e_tapscript_one_of_three_keypath(navigator: Navigator, firmware: Firm
])
run_test_e2e(navigator, client, wallet_policy, [], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_tapscript_one_of_three_scriptpath(navigator: Navigator, firmware: Firmware, client:
@@ -240,7 +240,7 @@ def test_e2e_tapscript_one_of_three_scriptpath(navigator: Navigator, firmware: F
])
run_test_e2e(navigator, client, wallet_policy, [], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_tapscript_multi_a_2of2(navigator: Navigator, firmware: Firmware, client:
@@ -261,7 +261,7 @@ def test_e2e_tapscript_multi_a_2of2(navigator: Navigator, firmware: Firmware, cl
])
run_test_e2e(navigator, client, wallet_policy, [core_wallet_name2], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_tapscript_maxdepth(navigator: Navigator, firmware: Firmware, client: RaggerClient,
@@ -290,7 +290,7 @@ def test_e2e_tapscript_maxdepth(navigator: Navigator, firmware: Firmware, client
keys_info=keys_info)
run_test_e2e(navigator, client, wallet_policy, [], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_tapscript_large(navigator: Navigator, firmware: Firmware, client: RaggerClient,
@@ -320,7 +320,7 @@ def test_e2e_tapscript_large(navigator: Navigator, firmware: Firmware, client: R
keys_info=keys_info)
run_test_e2e(navigator, client, wallet_policy, [core_wallet_name], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_tapminiscript_keypath_or_decaying_3of3(navigator: Navigator, firmware: Firmware,
@@ -345,7 +345,7 @@ def test_e2e_tapminiscript_keypath_or_decaying_3of3(navigator: Navigator, firmwa
])
run_test_e2e(navigator, client, wallet_policy, [core_name_2, core_name_3], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_tapminiscript_with_hash256(navigator: Navigator, firmware: Firmware, client:
@@ -367,7 +367,7 @@ def test_e2e_tapminiscript_with_hash256(navigator: Navigator, firmware: Firmware
])
run_test_e2e(navigator, client, wallet_policy, [core_name_3], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_e2e_tapminiscript_mixed_leaves(navigator: Navigator, firmware: Firmware, client:
@@ -392,7 +392,7 @@ def test_e2e_tapminiscript_mixed_leaves(navigator: Navigator, firmware: Firmware
])
run_test_e2e(navigator, client, wallet_policy, [], rpc, rpc_test_wallet, speculos_globals,
- e2e_register_wallet_instruction(firmware, wallet_policy.n_keys), e2e_sign_psbt_instruction(firmware), test_name)
+ e2e_register_wallet_instruction(firmware), e2e_sign_psbt_instruction(firmware), test_name)
def test_invalid_tapminiscript(navigator: Navigator, firmware: Firmware, client: RaggerClient,
### tests/test_sign_psbt_musig.py
@@ -77,8 +77,8 @@ def get_participant_pubkey(self) -> bip0327.Point:
def generate_public_nonces(self, psbt: PSBT) -> None:
print("PSBT before nonce generation:", psbt.serialize())
- res = self.client.sign_psbt(
- psbt, self.wallet_policy, self.wallet_hmac, navigator=self.navigator, testname=self.testname, instructions=self.instructions)
+ # Round 1 does not require navigation instructions, since it is silent
+ res = self.client.sign_psbt(psbt, self.wallet_policy, self.wallet_hmac)
print("Pubnonces:", res)
for (input_index, yielded) in res:
if isinstance(yielded, MusigPubNonce):
### unit-tests/libs/psbt_parse.c
@@ -53,7 +53,8 @@ static int parse_one_map(const uint8_t *data, size_t data_len, size_t *offset, p
return 0;
}
- if (*offset + key_len > data_len) return -1;
+ /* Subtraction form: *offset <= data_len holds here, and `*offset + key_len` could wrap */
+ if (key_len > data_len - *offset) return -1;
if (map->n_entries >= PSBT_MAP_MAX_ENTRIES) return -1;
psbt_kv_t *entry = &map->entries[map->n_entries];
@@ -67,7 +68,7 @@ static int parse_one_map(const uint8_t *data, size_t data_len, size_t *offset, p
if (n < 0) return -1;
*offset += (size_t) n;
- if (*offset + value_len > data_len) return -1;
+ if (value_len > data_len - *offset) return -1;
entry->value = data + *offset;
entry->value_len = (size_t) value_len;
@@ -110,6 +111,9 @@ int psbt_parse(const uint8_t *data,
if (parse_one_map(data, data_len, &offset, &out->output_maps[i]) < 0) return -1;
}
+ /* Trailing bytes mean the PSBT has more maps than n_inputs/n_outputs say */
+ if (offset != data_len) return -1;
+
return 0;
}
Why this scored 66/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.