Skip to content

Commit

Permalink
Merge pull request #1561 from lukechilds/psbt-tx-getters
Browse files Browse the repository at this point in the history
PSBT internal transaction property getters
  • Loading branch information
junderw authored Apr 27, 2020
2 parents c95e15d + fde6025 commit 4eb698d
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/bufferutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ function reverseBuffer(buffer) {
return buffer;
}
exports.reverseBuffer = reverseBuffer;
function cloneBuffer(buffer) {
const clone = Buffer.alloc(buffer.length);
buffer.copy(clone);
return buffer;
}
exports.cloneBuffer = cloneBuffer;
/**
* Helper class for serialization of bitcoin data types into a pre-allocated buffer.
*/
Expand Down
26 changes: 26 additions & 0 deletions src/psbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ class Psbt {
get inputCount() {
return this.data.inputs.length;
}
get version() {
return this.__CACHE.__TX.version;
}
set version(version) {
this.setVersion(version);
}
get locktime() {
return this.__CACHE.__TX.locktime;
}
set locktime(locktime) {
this.setLocktime(locktime);
}
get txInputs() {
return this.__CACHE.__TX.ins.map(input => ({
hash: bufferutils_1.cloneBuffer(input.hash),
index: input.index,
sequence: input.sequence,
}));
}
get txOutputs() {
return this.__CACHE.__TX.outs.map(output => ({
script: bufferutils_1.cloneBuffer(output.script),
value: output.value,
address: address_1.fromOutputScript(output.script, this.opts.network),
}));
}
combine(...those) {
this.data.combine(...those.map(o => o.data));
return this;
Expand Down
7 changes: 2 additions & 5 deletions test/psbt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,9 @@ describe(`Psbt`, () => {
});

assert.strictEqual(psbt.inputCount, 1);
assert.strictEqual(
(psbt as any).__CACHE.__TX.ins[0].sequence,
0xffffffff,
);
assert.strictEqual(psbt.txInputs[0].sequence, 0xffffffff);
psbt.setInputSequence(0, 0);
assert.strictEqual((psbt as any).__CACHE.__TX.ins[0].sequence, 0);
assert.strictEqual(psbt.txInputs[0].sequence, 0);
});

it('throws if input index is too high', () => {
Expand Down
6 changes: 6 additions & 0 deletions ts_src/bufferutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export function reverseBuffer(buffer: Buffer): Buffer {
return buffer;
}

export function cloneBuffer(buffer: Buffer): Buffer {
const clone = Buffer.alloc(buffer.length);
buffer.copy(clone);
return buffer;
}

/**
* Helper class for serialization of bitcoin data types into a pre-allocated buffer.
*/
Expand Down
37 changes: 35 additions & 2 deletions ts_src/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import {
Transaction as ITransaction,
TransactionFromBuffer,
TransactionInput,
TransactionOutput,
} from 'bip174/src/lib/interfaces';
import { checkForInput } from 'bip174/src/lib/utils';
import { toOutputScript } from './address';
import { reverseBuffer } from './bufferutils';
import { fromOutputScript, toOutputScript } from './address';
import { cloneBuffer, reverseBuffer } from './bufferutils';
import { hash160 } from './crypto';
import {
fromPublicKey as ecPairFromPublicKey,
Expand Down Expand Up @@ -129,6 +130,38 @@ export class Psbt {
return this.data.inputs.length;
}

get version(): number {
return this.__CACHE.__TX.version;
}

set version(version: number) {
this.setVersion(version);
}

get locktime(): number {
return this.__CACHE.__TX.locktime;
}

set locktime(locktime: number) {
this.setLocktime(locktime);
}

get txInputs(): TransactionInput[] {
return this.__CACHE.__TX.ins.map(input => ({
hash: cloneBuffer(input.hash),
index: input.index,
sequence: input.sequence,
}));
}

get txOutputs(): TransactionOutput[] {
return this.__CACHE.__TX.outs.map(output => ({
script: cloneBuffer(output.script),
value: output.value,
address: fromOutputScript(output.script, this.opts.network),
}));
}

combine(...those: Psbt[]): this {
this.data.combine(...those.map(o => o.data));
return this;
Expand Down
1 change: 1 addition & 0 deletions types/bufferutils.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export declare function readUInt64LE(buffer: Buffer, offset: number): number;
export declare function writeUInt64LE(buffer: Buffer, value: number, offset: number): number;
export declare function reverseBuffer(buffer: Buffer): Buffer;
export declare function cloneBuffer(buffer: Buffer): Buffer;
/**
* Helper class for serialization of bitcoin data types into a pre-allocated buffer.
*/
Expand Down
6 changes: 5 additions & 1 deletion types/psbt.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Psbt as PsbtBase } from 'bip174';
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate, TransactionInput } from 'bip174/src/lib/interfaces';
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate, TransactionInput, TransactionOutput } from 'bip174/src/lib/interfaces';
import { Signer, SignerAsync } from './ecpair';
import { Network } from './networks';
import { Transaction } from './transaction';
Expand Down Expand Up @@ -44,6 +44,10 @@ export declare class Psbt {
private opts;
constructor(opts?: PsbtOptsOptional, data?: PsbtBase);
readonly inputCount: number;
version: number;
locktime: number;
readonly txInputs: TransactionInput[];
readonly txOutputs: TransactionOutput[];
combine(...those: Psbt[]): this;
clone(): Psbt;
setMaximumFeeRate(satoshiPerByte: number): void;
Expand Down

0 comments on commit 4eb698d

Please sign in to comment.