Skip to content

Commit 854c601

Browse files
committed
Add getters for Psbt.{txVersion,txLocktime,txInputs,txOutputs}
1 parent c95e15d commit 854c601

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/psbt.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ class Psbt {
9797
get inputCount() {
9898
return this.data.inputs.length;
9999
}
100+
get txVersion() {
101+
return this.__CACHE.__TX.version;
102+
}
103+
get txLocktime() {
104+
return this.__CACHE.__TX.locktime;
105+
}
106+
get txInputs() {
107+
return deepClone(this.__CACHE.__TX.ins);
108+
}
109+
get txOutputs() {
110+
return deepClone(this.__CACHE.__TX.outs);
111+
}
100112
combine(...those) {
101113
this.data.combine(...those.map(o => o.data));
102114
return this;
@@ -579,6 +591,11 @@ class PsbtTransaction {
579591
return this.tx.toBuffer();
580592
}
581593
}
594+
function deepClone(obj) {
595+
return JSON.parse(JSON.stringify(obj), (_, value) =>
596+
value.type === 'Buffer' ? Buffer.from(value.data) : value,
597+
);
598+
}
582599
function canFinalize(input, script, scriptType) {
583600
switch (scriptType) {
584601
case 'pubkey':

ts_src/psbt.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ export class Psbt {
129129
return this.data.inputs.length;
130130
}
131131

132+
get txVersion(): number {
133+
return this.__CACHE.__TX.version;
134+
}
135+
136+
get txLocktime(): number {
137+
return this.__CACHE.__TX.locktime;
138+
}
139+
140+
get txInputs(): TransactionInput[] {
141+
return deepClone(this.__CACHE.__TX.ins);
142+
}
143+
144+
get txOutputs(): TransactionInput[] {
145+
return deepClone(this.__CACHE.__TX.outs);
146+
}
147+
132148
combine(...those: Psbt[]): this {
133149
this.data.combine(...those.map(o => o.data));
134150
return this;
@@ -757,6 +773,12 @@ class PsbtTransaction implements ITransaction {
757773
}
758774
}
759775

776+
function deepClone(obj: any): any {
777+
return JSON.parse(JSON.stringify(obj), (_, value) =>
778+
value.type === 'Buffer' ? Buffer.from(value.data) : value,
779+
);
780+
}
781+
760782
function canFinalize(
761783
input: PsbtInput,
762784
script: Buffer,

types/psbt.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export declare class Psbt {
4444
private opts;
4545
constructor(opts?: PsbtOptsOptional, data?: PsbtBase);
4646
readonly inputCount: number;
47+
readonly txVersion: number;
48+
readonly txLocktime: number;
49+
readonly txInputs: TransactionInput[];
50+
readonly txOutputs: TransactionInput[];
4751
combine(...those: Psbt[]): this;
4852
clone(): Psbt;
4953
setMaximumFeeRate(satoshiPerByte: number): void;

0 commit comments

Comments
 (0)