Skip to content

Commit 4eb698d

Browse files
authored
Merge pull request #1561 from lukechilds/psbt-tx-getters
PSBT internal transaction property getters
2 parents c95e15d + fde6025 commit 4eb698d

File tree

7 files changed

+81
-8
lines changed

7 files changed

+81
-8
lines changed

src/bufferutils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ function reverseBuffer(buffer) {
4141
return buffer;
4242
}
4343
exports.reverseBuffer = reverseBuffer;
44+
function cloneBuffer(buffer) {
45+
const clone = Buffer.alloc(buffer.length);
46+
buffer.copy(clone);
47+
return buffer;
48+
}
49+
exports.cloneBuffer = cloneBuffer;
4450
/**
4551
* Helper class for serialization of bitcoin data types into a pre-allocated buffer.
4652
*/

src/psbt.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,32 @@ class Psbt {
9797
get inputCount() {
9898
return this.data.inputs.length;
9999
}
100+
get version() {
101+
return this.__CACHE.__TX.version;
102+
}
103+
set version(version) {
104+
this.setVersion(version);
105+
}
106+
get locktime() {
107+
return this.__CACHE.__TX.locktime;
108+
}
109+
set locktime(locktime) {
110+
this.setLocktime(locktime);
111+
}
112+
get txInputs() {
113+
return this.__CACHE.__TX.ins.map(input => ({
114+
hash: bufferutils_1.cloneBuffer(input.hash),
115+
index: input.index,
116+
sequence: input.sequence,
117+
}));
118+
}
119+
get txOutputs() {
120+
return this.__CACHE.__TX.outs.map(output => ({
121+
script: bufferutils_1.cloneBuffer(output.script),
122+
value: output.value,
123+
address: address_1.fromOutputScript(output.script, this.opts.network),
124+
}));
125+
}
100126
combine(...those) {
101127
this.data.combine(...those.map(o => o.data));
102128
return this;

test/psbt.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,9 @@ describe(`Psbt`, () => {
523523
});
524524

525525
assert.strictEqual(psbt.inputCount, 1);
526-
assert.strictEqual(
527-
(psbt as any).__CACHE.__TX.ins[0].sequence,
528-
0xffffffff,
529-
);
526+
assert.strictEqual(psbt.txInputs[0].sequence, 0xffffffff);
530527
psbt.setInputSequence(0, 0);
531-
assert.strictEqual((psbt as any).__CACHE.__TX.ins[0].sequence, 0);
528+
assert.strictEqual(psbt.txInputs[0].sequence, 0);
532529
});
533530

534531
it('throws if input index is too high', () => {

ts_src/bufferutils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ export function reverseBuffer(buffer: Buffer): Buffer {
4848
return buffer;
4949
}
5050

51+
export function cloneBuffer(buffer: Buffer): Buffer {
52+
const clone = Buffer.alloc(buffer.length);
53+
buffer.copy(clone);
54+
return buffer;
55+
}
56+
5157
/**
5258
* Helper class for serialization of bitcoin data types into a pre-allocated buffer.
5359
*/

ts_src/psbt.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import {
1111
Transaction as ITransaction,
1212
TransactionFromBuffer,
1313
TransactionInput,
14+
TransactionOutput,
1415
} from 'bip174/src/lib/interfaces';
1516
import { checkForInput } from 'bip174/src/lib/utils';
16-
import { toOutputScript } from './address';
17-
import { reverseBuffer } from './bufferutils';
17+
import { fromOutputScript, toOutputScript } from './address';
18+
import { cloneBuffer, reverseBuffer } from './bufferutils';
1819
import { hash160 } from './crypto';
1920
import {
2021
fromPublicKey as ecPairFromPublicKey,
@@ -129,6 +130,38 @@ export class Psbt {
129130
return this.data.inputs.length;
130131
}
131132

133+
get version(): number {
134+
return this.__CACHE.__TX.version;
135+
}
136+
137+
set version(version: number) {
138+
this.setVersion(version);
139+
}
140+
141+
get locktime(): number {
142+
return this.__CACHE.__TX.locktime;
143+
}
144+
145+
set locktime(locktime: number) {
146+
this.setLocktime(locktime);
147+
}
148+
149+
get txInputs(): TransactionInput[] {
150+
return this.__CACHE.__TX.ins.map(input => ({
151+
hash: cloneBuffer(input.hash),
152+
index: input.index,
153+
sequence: input.sequence,
154+
}));
155+
}
156+
157+
get txOutputs(): TransactionOutput[] {
158+
return this.__CACHE.__TX.outs.map(output => ({
159+
script: cloneBuffer(output.script),
160+
value: output.value,
161+
address: fromOutputScript(output.script, this.opts.network),
162+
}));
163+
}
164+
132165
combine(...those: Psbt[]): this {
133166
this.data.combine(...those.map(o => o.data));
134167
return this;

types/bufferutils.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export declare function readUInt64LE(buffer: Buffer, offset: number): number;
22
export declare function writeUInt64LE(buffer: Buffer, value: number, offset: number): number;
33
export declare function reverseBuffer(buffer: Buffer): Buffer;
4+
export declare function cloneBuffer(buffer: Buffer): Buffer;
45
/**
56
* Helper class for serialization of bitcoin data types into a pre-allocated buffer.
67
*/

types/psbt.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Psbt as PsbtBase } from 'bip174';
2-
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate, TransactionInput } from 'bip174/src/lib/interfaces';
2+
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate, TransactionInput, TransactionOutput } from 'bip174/src/lib/interfaces';
33
import { Signer, SignerAsync } from './ecpair';
44
import { Network } from './networks';
55
import { Transaction } from './transaction';
@@ -44,6 +44,10 @@ export declare class Psbt {
4444
private opts;
4545
constructor(opts?: PsbtOptsOptional, data?: PsbtBase);
4646
readonly inputCount: number;
47+
version: number;
48+
locktime: number;
49+
readonly txInputs: TransactionInput[];
50+
readonly txOutputs: TransactionOutput[];
4751
combine(...those: Psbt[]): this;
4852
clone(): Psbt;
4953
setMaximumFeeRate(satoshiPerByte: number): void;

0 commit comments

Comments
 (0)