Merge pull request #1561 from lukechilds/psbt-tx-getters
PSBT internal transaction property getters
This commit is contained in:
commit
4eb698df50
7 changed files with 81 additions and 8 deletions
|
@ -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.
|
||||
*/
|
||||
|
|
26
src/psbt.js
26
src/psbt.js
|
@ -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;
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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,
|
||||
|
@ -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;
|
||||
|
|
1
types/bufferutils.d.ts
vendored
1
types/bufferutils.d.ts
vendored
|
@ -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.
|
||||
*/
|
||||
|
|
6
types/psbt.d.ts
vendored
6
types/psbt.d.ts
vendored
|
@ -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';
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue