Skip to content

feat: pass bip32PathsAbsolute flag into bip174 #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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