Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSBT internal transaction property getters #1561

Merged
merged 10 commits into from
Apr 27, 2020
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
27 changes: 27 additions & 0 deletions src/psbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,33 @@ 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,
script: bufferutils_1.cloneBuffer(input.script),
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
38 changes: 36 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,39 @@ 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,
script: cloneBuffer(input.script),
lukechilds marked this conversation as resolved.
Show resolved Hide resolved
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