Reject PSBTv2-only fields when converting a PSBTv0 in the JS client
What changed, and why it matters
This commit fixes a bug in the Ledger Bitcoin app's JavaScript client library. When converting an older-style Bitcoin transaction file (PSBT version 0) into the newer PSBT version 2 format, the library previously ignored fields that are only legal in version 2. Those fields could silently override important transaction details, such as when the transaction can be spent (the locktime). The change now rejects such files with an error instead of converting them, preventing accidental or malicious transaction tampering from being hidden.
Users of the Ledger Bitcoin app JavaScript client should update to a version containing this commit. Developers integrating PSBTv0 conversion should verify that any PSBTv0 inputs they produce do not contain v2-only fields, as such inputs will now be rejected. Review other conversion paths for similar silent-overwrite behavior.
Security signals we found
Input validation fix: rejects malformed PSBTv0 containing v2-only fields
Prevents silent override of transaction locktime during PSBTv0-to-v2 conversion
Adds missing BIP-370 key type constants for required time/height locktime
Adds regression tests for locktime handling and v2-only field rejection
Evidence from the diff
The patch updates bitcoin_client_js/src/lib/psbtv2.ts so that the PsbtV2.deserialize() path for PSBTv0 explicitly checks for the presence of BIP-370-defined PSBTv2-only global, input, and output key types. If any are found, it throws ‘PsbtV0 contains fields that are only allowed in PsbtV2’ before performing the v0-to-v2 conversion. Previously these fields were silently dropped or could overwrite converted values. The patch also adds the missing psbtIn.REQUIRED_TIME_LOCKTIME and REQUIRED_HEIGHT_LOCKTIME enum values and adds unit tests covering locktime preservation and rejection of v2-only input fields.
Changed components
bitcoin_client_js/src/lib/psbtv2.tsbitcoin_client_js/src/__tests__/psbtv2.test.tsLedger Bitcoin app JavaScript client library PSBTv0-to-v2 conversion logicInspect captured patch +66 / −2
### 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,21 @@ 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"
+ );
+ });
});
### 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
@@ -414,6 +416,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 +477,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 {Why this scored 51/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.