Add getters for Psbt.{txVersion,txLocktime,txInputs,txOutputs}

This commit is contained in:
Luke Childs 2020-04-26 14:30:13 +07:00
parent c95e15de01
commit 854c601342
3 changed files with 43 additions and 0 deletions

View file

@ -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':

View file

@ -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,

4
types/psbt.d.ts vendored
View file

@ -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;