From 854c6013420fca71f051adf03d65fd13a8dfbda7 Mon Sep 17 00:00:00 2001 From: Luke Childs <lukechilds123@gmail.com> Date: Sun, 26 Apr 2020 14:30:13 +0700 Subject: [PATCH] Add getters for Psbt.{txVersion,txLocktime,txInputs,txOutputs} --- src/psbt.js | 17 +++++++++++++++++ ts_src/psbt.ts | 22 ++++++++++++++++++++++ types/psbt.d.ts | 4 ++++ 3 files changed, 43 insertions(+) diff --git a/src/psbt.js b/src/psbt.js index 5719586..95b344f 100644 --- a/src/psbt.js +++ b/src/psbt.js @@ -97,6 +97,18 @@ class Psbt { get inputCount() { return this.data.inputs.length; } + get txVersion() { + return this.__CACHE.__TX.version; + } + get txLocktime() { + return this.__CACHE.__TX.locktime; + } + get txInputs() { + return deepClone(this.__CACHE.__TX.ins); + } + get txOutputs() { + return deepClone(this.__CACHE.__TX.outs); + } combine(...those) { this.data.combine(...those.map(o => o.data)); return this; @@ -579,6 +591,11 @@ class PsbtTransaction { return this.tx.toBuffer(); } } +function deepClone(obj) { + return JSON.parse(JSON.stringify(obj), (_, value) => + value.type === 'Buffer' ? Buffer.from(value.data) : value, + ); +} function canFinalize(input, script, scriptType) { switch (scriptType) { case 'pubkey': diff --git a/ts_src/psbt.ts b/ts_src/psbt.ts index 54fe380..d86c714 100644 --- a/ts_src/psbt.ts +++ b/ts_src/psbt.ts @@ -129,6 +129,22 @@ export class Psbt { return this.data.inputs.length; } + get txVersion(): number { + return this.__CACHE.__TX.version; + } + + get txLocktime(): number { + return this.__CACHE.__TX.locktime; + } + + get txInputs(): TransactionInput[] { + return deepClone(this.__CACHE.__TX.ins); + } + + get txOutputs(): TransactionInput[] { + return deepClone(this.__CACHE.__TX.outs); + } + combine(...those: Psbt[]): this { this.data.combine(...those.map(o => o.data)); return this; @@ -757,6 +773,12 @@ class PsbtTransaction implements ITransaction { } } +function deepClone(obj: any): any { + return JSON.parse(JSON.stringify(obj), (_, value) => + value.type === 'Buffer' ? Buffer.from(value.data) : value, + ); +} + function canFinalize( input: PsbtInput, script: Buffer, diff --git a/types/psbt.d.ts b/types/psbt.d.ts index 44eb4d8..fd8f8dc 100644 --- a/types/psbt.d.ts +++ b/types/psbt.d.ts @@ -44,6 +44,10 @@ export declare class Psbt { private opts; constructor(opts?: PsbtOptsOptional, data?: PsbtBase); readonly inputCount: number; + readonly txVersion: number; + readonly txLocktime: number; + readonly txInputs: TransactionInput[]; + readonly txOutputs: TransactionInput[]; combine(...those: Psbt[]): this; clone(): Psbt; setMaximumFeeRate(satoshiPerByte: number): void;