Skip to content

Commit 17653d8

Browse files
feat: pass bip32PathsAbsolute flag into bip174
2 parents 0115d1b + 4a88a1e commit 17653d8

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
],
5454
"dependencies": {
5555
"bech32": "^2.0.0",
56-
"bip174": "npm:@bitgo/[email protected]",
56+
"bip174": "npm:@bitgo-forks/[email protected]-rc.1",
5757
"bs58check": "^2.1.2",
5858
"create-hash": "^1.1.0",
5959
"fastpriorityqueue": "^0.7.1",

src/psbt.d.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ export declare type ValidateSigFunction = (pubkey: Buffer, msghash: Buffer, sign
5353
*/
5454
export declare class Psbt {
5555
readonly data: PsbtBase;
56-
static fromBase64(data: string, opts?: PsbtOptsOptional): Psbt;
57-
static fromHex(data: string, opts?: PsbtOptsOptional): Psbt;
58-
static fromBuffer(buffer: Buffer, opts?: PsbtOptsOptional): Psbt;
56+
static fromBase64(data: string, opts?: PsbtDeserializeOptsOptional): Psbt;
57+
static fromHex(data: string, opts?: PsbtDeserializeOptsOptional): Psbt;
58+
static fromBuffer(buffer: Buffer, opts?: PsbtDeserializeOptsOptional): Psbt;
5959
protected static transactionFromBuffer(buffer: Buffer, _network: Network): Transaction<bigint>;
6060
private __CACHE;
6161
private opts;
@@ -112,6 +112,9 @@ interface PsbtOptsOptional {
112112
network?: Network;
113113
maximumFeeRate?: number;
114114
}
115+
interface PsbtDeserializeOptsOptional extends PsbtOptsOptional {
116+
bip32PathsAbsolute?: boolean;
117+
}
115118
interface PsbtInputExtended extends PsbtInput, TransactionInput {
116119
}
117120
declare type PsbtOutputExtended = PsbtOutputExtendedAddress | PsbtOutputExtendedScript;

src/psbt.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ class Psbt {
100100
return this.fromBuffer(buffer, opts);
101101
}
102102
static fromBuffer(buffer, opts = {}) {
103-
const psbtBase = bip174_1.Psbt.fromBuffer(buffer, transactionFromBuffer);
103+
const psbtBase = bip174_1.Psbt.fromBuffer(buffer, transactionFromBuffer, {
104+
bip32PathsAbsolute: opts.bip32PathsAbsolute,
105+
});
104106
const psbt = new Psbt(opts, psbtBase);
105107
checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
106108
return psbt;

test/psbt.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,30 @@ describe(`Psbt`, () => {
396396
});
397397
});
398398

399+
describe('deserialization', () => {
400+
[true, false].forEach((bip32PathsAbsolute: boolean) => {
401+
it(`bip32Derivation paths are ${
402+
bip32PathsAbsolute ? '' : 'not'
403+
} absolute`, async () => {
404+
const psbt = Psbt.fromBase64(
405+
fixtures.signInputHD.checks[0].shouldSign.psbt,
406+
{ bip32PathsAbsolute },
407+
);
408+
const input = psbt.data.inputs[0];
409+
assert(input);
410+
const bip32Derivations = input.bip32Derivation;
411+
assert(bip32Derivations);
412+
const bip32Derivation = bip32Derivations[0];
413+
assert(bip32Derivation);
414+
const pathString = bip32Derivation.path;
415+
assert(pathString);
416+
const path = pathString.split('/');
417+
const bip32PathPrefix = bip32PathsAbsolute ? 'm' : `44'`;
418+
assert(path[0] === bip32PathPrefix);
419+
});
420+
});
421+
});
422+
399423
describe('signAllInputsHDAsync', () => {
400424
fixtures.signInputHD.checks.forEach(f => {
401425
it(f.description, async () => {

ts_src/psbt.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,26 @@ const DEFAULT_OPTS: PsbtOpts = {
9797
* Transaction object. Such as fee rate not being larger than maximumFeeRate etc.
9898
*/
9999
export class Psbt {
100-
static fromBase64(data: string, opts: PsbtOptsOptional = {}): Psbt {
100+
static fromBase64(
101+
data: string,
102+
opts: PsbtDeserializeOptsOptional = {},
103+
): Psbt {
101104
const buffer = Buffer.from(data, 'base64');
102105
return this.fromBuffer(buffer, opts);
103106
}
104107

105-
static fromHex(data: string, opts: PsbtOptsOptional = {}): Psbt {
108+
static fromHex(data: string, opts: PsbtDeserializeOptsOptional = {}): Psbt {
106109
const buffer = Buffer.from(data, 'hex');
107110
return this.fromBuffer(buffer, opts);
108111
}
109112

110-
static fromBuffer(buffer: Buffer, opts: PsbtOptsOptional = {}): Psbt {
111-
const psbtBase = PsbtBase.fromBuffer(buffer, transactionFromBuffer);
113+
static fromBuffer(
114+
buffer: Buffer,
115+
opts: PsbtDeserializeOptsOptional = {},
116+
): Psbt {
117+
const psbtBase = PsbtBase.fromBuffer(buffer, transactionFromBuffer, {
118+
bip32PathsAbsolute: opts.bip32PathsAbsolute,
119+
});
112120
const psbt = new Psbt(opts, psbtBase);
113121
checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
114122
return psbt;
@@ -775,6 +783,10 @@ interface PsbtOptsOptional {
775783
maximumFeeRate?: number;
776784
}
777785

786+
interface PsbtDeserializeOptsOptional extends PsbtOptsOptional {
787+
bip32PathsAbsolute?: boolean;
788+
}
789+
778790
interface PsbtOpts {
779791
network: Network;
780792
maximumFeeRate: number;

0 commit comments

Comments
 (0)