Skip to content

Commit

Permalink
Merge pull request #54 from BitGo/BG-54756-pass-in-bip32-decoding-fla…
Browse files Browse the repository at this point in the history
…g-into-bip174

feat: pass bip32PathsAbsolute flag into bip174
  • Loading branch information
davidkaplanbitgo authored Jan 24, 2023
2 parents 0115d1b + 4a88a1e commit 17653d8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
],
"dependencies": {
"bech32": "^2.0.0",
"bip174": "npm:@bitgo/[email protected]",
"bip174": "npm:@bitgo-forks/[email protected]-rc.1",
"bs58check": "^2.1.2",
"create-hash": "^1.1.0",
"fastpriorityqueue": "^0.7.1",
Expand Down
9 changes: 6 additions & 3 deletions src/psbt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export declare type ValidateSigFunction = (pubkey: Buffer, msghash: Buffer, sign
*/
export declare class Psbt {
readonly data: PsbtBase;
static fromBase64(data: string, opts?: PsbtOptsOptional): Psbt;
static fromHex(data: string, opts?: PsbtOptsOptional): Psbt;
static fromBuffer(buffer: Buffer, opts?: PsbtOptsOptional): Psbt;
static fromBase64(data: string, opts?: PsbtDeserializeOptsOptional): Psbt;
static fromHex(data: string, opts?: PsbtDeserializeOptsOptional): Psbt;
static fromBuffer(buffer: Buffer, opts?: PsbtDeserializeOptsOptional): Psbt;
protected static transactionFromBuffer(buffer: Buffer, _network: Network): Transaction<bigint>;
private __CACHE;
private opts;
Expand Down Expand Up @@ -112,6 +112,9 @@ interface PsbtOptsOptional {
network?: Network;
maximumFeeRate?: number;
}
interface PsbtDeserializeOptsOptional extends PsbtOptsOptional {
bip32PathsAbsolute?: boolean;
}
interface PsbtInputExtended extends PsbtInput, TransactionInput {
}
declare type PsbtOutputExtended = PsbtOutputExtendedAddress | PsbtOutputExtendedScript;
Expand Down
4 changes: 3 additions & 1 deletion src/psbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ class Psbt {
return this.fromBuffer(buffer, opts);
}
static fromBuffer(buffer, opts = {}) {
const psbtBase = bip174_1.Psbt.fromBuffer(buffer, transactionFromBuffer);
const psbtBase = bip174_1.Psbt.fromBuffer(buffer, transactionFromBuffer, {
bip32PathsAbsolute: opts.bip32PathsAbsolute,
});
const psbt = new Psbt(opts, psbtBase);
checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
return psbt;
Expand Down
24 changes: 24 additions & 0 deletions test/psbt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,30 @@ describe(`Psbt`, () => {
});
});

describe('deserialization', () => {
[true, false].forEach((bip32PathsAbsolute: boolean) => {
it(`bip32Derivation paths are ${
bip32PathsAbsolute ? '' : 'not'
} absolute`, async () => {
const psbt = Psbt.fromBase64(
fixtures.signInputHD.checks[0].shouldSign.psbt,
{ bip32PathsAbsolute },
);
const input = psbt.data.inputs[0];
assert(input);
const bip32Derivations = input.bip32Derivation;
assert(bip32Derivations);
const bip32Derivation = bip32Derivations[0];
assert(bip32Derivation);
const pathString = bip32Derivation.path;
assert(pathString);
const path = pathString.split('/');
const bip32PathPrefix = bip32PathsAbsolute ? 'm' : `44'`;
assert(path[0] === bip32PathPrefix);
});
});
});

describe('signAllInputsHDAsync', () => {
fixtures.signInputHD.checks.forEach(f => {
it(f.description, async () => {
Expand Down
20 changes: 16 additions & 4 deletions ts_src/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,26 @@ const DEFAULT_OPTS: PsbtOpts = {
* Transaction object. Such as fee rate not being larger than maximumFeeRate etc.
*/
export class Psbt {
static fromBase64(data: string, opts: PsbtOptsOptional = {}): Psbt {
static fromBase64(
data: string,
opts: PsbtDeserializeOptsOptional = {},
): Psbt {
const buffer = Buffer.from(data, 'base64');
return this.fromBuffer(buffer, opts);
}

static fromHex(data: string, opts: PsbtOptsOptional = {}): Psbt {
static fromHex(data: string, opts: PsbtDeserializeOptsOptional = {}): Psbt {
const buffer = Buffer.from(data, 'hex');
return this.fromBuffer(buffer, opts);
}

static fromBuffer(buffer: Buffer, opts: PsbtOptsOptional = {}): Psbt {
const psbtBase = PsbtBase.fromBuffer(buffer, transactionFromBuffer);
static fromBuffer(
buffer: Buffer,
opts: PsbtDeserializeOptsOptional = {},
): Psbt {
const psbtBase = PsbtBase.fromBuffer(buffer, transactionFromBuffer, {
bip32PathsAbsolute: opts.bip32PathsAbsolute,
});
const psbt = new Psbt(opts, psbtBase);
checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
return psbt;
Expand Down Expand Up @@ -775,6 +783,10 @@ interface PsbtOptsOptional {
maximumFeeRate?: number;
}

interface PsbtDeserializeOptsOptional extends PsbtOptsOptional {
bip32PathsAbsolute?: boolean;
}

interface PsbtOpts {
network: Network;
maximumFeeRate: number;
Expand Down

0 comments on commit 17653d8

Please sign in to comment.