bitcoinjs-lib/ts_src/transaction.ts

629 lines
16 KiB
TypeScript
Raw Normal View History

2019-03-03 15:07:49 +01:00
import * as bcrypto from './crypto';
import * as bscript from './script';
import * as types from './types';
import * as bufferutils from './bufferutils';
import { reverseBuffer } from './bufferutils';
import { OPS as opcodes } from './script';
2019-03-03 15:07:49 +01:00
const typeforce = require('typeforce');
const varuint = require('varuint-bitcoin');
2014-06-04 06:07:29 +02:00
2019-03-03 15:07:49 +01:00
function varSliceSize(someScript: Buffer): number {
const length = someScript.length;
2019-03-03 15:07:49 +01:00
return varuint.encodingLength(length) + length;
}
2019-03-03 15:07:49 +01:00
function vectorSize(someVector: Array<Buffer>): number {
const length = someVector.length;
2019-03-03 15:07:49 +01:00
return (
varuint.encodingLength(length) +
someVector.reduce((sum, witness) => {
return sum + varSliceSize(witness);
}, 0)
);
}
2019-03-03 15:07:49 +01:00
const EMPTY_SCRIPT: Buffer = Buffer.allocUnsafe(0);
const EMPTY_WITNESS: Array<Buffer> = [];
const ZERO: Buffer = Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000000',
'hex',
);
const ONE: Buffer = Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000001',
'hex',
);
const VALUE_UINT64_MAX: Buffer = Buffer.from('ffffffffffffffff', 'hex');
2018-12-27 10:26:08 +01:00
const BLANK_OUTPUT: BlankOutput = {
script: EMPTY_SCRIPT,
2019-03-03 15:07:49 +01:00
valueBuffer: VALUE_UINT64_MAX,
};
2018-12-27 10:26:08 +01:00
function isOutput(out: Output | BlankOutput): out is Output {
2019-03-03 15:07:49 +01:00
return (<Output>out).value !== undefined;
2018-12-27 10:26:08 +01:00
}
2018-12-27 10:26:08 +01:00
export type BlankOutput = {
2019-03-03 15:07:49 +01:00
script: Buffer;
valueBuffer: Buffer;
};
2018-12-27 10:26:08 +01:00
export type Output = {
2019-03-03 15:07:49 +01:00
script: Buffer;
value: number;
};
2018-12-27 10:26:08 +01:00
export type Input = {
2019-03-03 15:07:49 +01:00
hash: Buffer;
index: number;
script: Buffer;
sequence: number;
witness: Array<Buffer>;
};
2018-12-27 10:26:08 +01:00
export class Transaction {
2019-03-03 15:07:49 +01:00
version: number;
locktime: number;
ins: Array<Input>;
outs: Array<Output | BlankOutput>;
static readonly DEFAULT_SEQUENCE = 0xffffffff;
static readonly SIGHASH_ALL = 0x01;
static readonly SIGHASH_NONE = 0x02;
static readonly SIGHASH_SINGLE = 0x03;
static readonly SIGHASH_ANYONECANPAY = 0x80;
static readonly ADVANCED_TRANSACTION_MARKER = 0x00;
static readonly ADVANCED_TRANSACTION_FLAG = 0x01;
constructor() {
this.version = 1;
this.locktime = 0;
this.ins = [];
this.outs = [];
}
2018-12-28 16:53:54 +01:00
2019-03-03 15:07:49 +01:00
static fromBuffer(buffer: Buffer, __noStrict?: boolean): Transaction {
let offset: number = 0;
2019-03-03 15:07:49 +01:00
function readSlice(n: number): Buffer {
offset += n;
return buffer.slice(offset - n, offset);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function readUInt32(): number {
const i = buffer.readUInt32LE(offset);
offset += 4;
return i;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function readInt32(): number {
const i = buffer.readInt32LE(offset);
offset += 4;
return i;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function readUInt64(): number {
const i = bufferutils.readUInt64LE(buffer, offset);
offset += 8;
return i;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function readVarInt(): number {
const vi = varuint.decode(buffer, offset);
offset += varuint.decode.bytes;
return vi;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function readVarSlice(): Buffer {
return readSlice(readVarInt());
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function readVector(): Array<Buffer> {
const count = readVarInt();
const vector: Array<Buffer> = [];
for (var i = 0; i < count; i++) vector.push(readVarSlice());
return vector;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
const tx = new Transaction();
tx.version = readInt32();
2018-12-27 10:26:08 +01:00
2019-03-03 15:07:49 +01:00
const marker = buffer.readUInt8(offset);
const flag = buffer.readUInt8(offset + 1);
2018-12-27 10:26:08 +01:00
2019-03-03 15:07:49 +01:00
let hasWitnesses = false;
if (
marker === Transaction.ADVANCED_TRANSACTION_MARKER &&
flag === Transaction.ADVANCED_TRANSACTION_FLAG
) {
offset += 2;
hasWitnesses = true;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
const vinLen = readVarInt();
2018-12-27 10:26:08 +01:00
for (var i = 0; i < vinLen; ++i) {
tx.ins.push({
hash: readSlice(32),
index: readUInt32(),
script: readVarSlice(),
sequence: readUInt32(),
2019-03-03 15:07:49 +01:00
witness: EMPTY_WITNESS,
});
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
const voutLen = readVarInt();
2018-12-27 10:26:08 +01:00
for (i = 0; i < voutLen; ++i) {
tx.outs.push({
value: readUInt64(),
2019-03-03 15:07:49 +01:00
script: readVarSlice(),
});
2018-12-27 10:26:08 +01:00
}
2018-12-27 10:26:08 +01:00
if (hasWitnesses) {
for (i = 0; i < vinLen; ++i) {
2019-03-03 15:07:49 +01:00
tx.ins[i].witness = readVector();
2018-12-27 10:26:08 +01:00
}
// was this pointless?
2019-03-03 15:07:49 +01:00
if (!tx.hasWitnesses())
throw new Error('Transaction has superfluous witness data');
2018-09-26 07:54:53 +02:00
}
2019-03-03 15:07:49 +01:00
tx.locktime = readUInt32();
2019-03-03 15:07:49 +01:00
if (__noStrict) return tx;
if (offset !== buffer.length)
throw new Error('Transaction has unexpected data');
2019-03-03 15:07:49 +01:00
return tx;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
static fromHex(hex: string): Transaction {
return Transaction.fromBuffer(Buffer.from(hex, 'hex'), false);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
static isCoinbaseHash(buffer: Buffer): boolean {
typeforce(types.Hash256bit, buffer);
2018-12-27 10:26:08 +01:00
for (var i = 0; i < 32; ++i) {
2019-03-03 15:07:49 +01:00
if (buffer[i] !== 0) return false;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
return true;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
isCoinbase(): boolean {
return (
this.ins.length === 1 && Transaction.isCoinbaseHash(this.ins[0].hash)
);
}
2019-03-03 15:07:49 +01:00
addInput(
hash: Buffer,
index: number,
sequence?: number,
scriptSig?: Buffer,
): number {
typeforce(
types.tuple(
types.Hash256bit,
types.UInt32,
types.maybe(types.UInt32),
types.maybe(types.Buffer),
),
arguments,
);
2014-03-23 20:01:33 +01:00
2018-12-27 10:26:08 +01:00
if (types.Null(sequence)) {
2019-03-03 15:07:49 +01:00
sequence = Transaction.DEFAULT_SEQUENCE;
2018-12-27 10:26:08 +01:00
}
2018-12-27 10:26:08 +01:00
// Add the input and return the input's index
2019-03-03 15:07:49 +01:00
return (
this.ins.push({
hash: hash,
index: index,
script: scriptSig || EMPTY_SCRIPT,
sequence: <number>sequence,
witness: EMPTY_WITNESS,
}) - 1
);
}
2019-03-03 15:07:49 +01:00
addOutput(scriptPubKey: Buffer, value: number): number {
typeforce(types.tuple(types.Buffer, types.Satoshi), arguments);
2018-12-27 10:26:08 +01:00
// Add the output and return the output's index
2019-03-03 15:07:49 +01:00
return (
this.outs.push({
script: scriptPubKey,
value: value,
}) - 1
);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
hasWitnesses(): boolean {
return this.ins.some(x => {
return x.witness.length !== 0;
});
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
weight(): number {
const base = this.__byteLength(false);
const total = this.__byteLength(true);
return base * 3 + total;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
virtualSize(): number {
return Math.ceil(this.weight() / 4);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
byteLength(): number {
return this.__byteLength(true);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
private __byteLength(__allowWitness: boolean): number {
const hasWitnesses = __allowWitness && this.hasWitnesses();
2018-12-27 10:26:08 +01:00
return (
(hasWitnesses ? 10 : 8) +
varuint.encodingLength(this.ins.length) +
varuint.encodingLength(this.outs.length) +
this.ins.reduce((sum, input) => {
2019-03-03 15:07:49 +01:00
return sum + 40 + varSliceSize(input.script);
2018-12-27 10:26:08 +01:00
}, 0) +
this.outs.reduce((sum, output) => {
2019-03-03 15:07:49 +01:00
return sum + 8 + varSliceSize(output.script);
2018-12-27 10:26:08 +01:00
}, 0) +
2019-03-03 15:07:49 +01:00
(hasWitnesses
? this.ins.reduce((sum, input) => {
return sum + vectorSize(input.witness);
}, 0)
: 0)
);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
clone(): Transaction {
const newTx = new Transaction();
newTx.version = this.version;
newTx.locktime = this.locktime;
2018-12-27 10:26:08 +01:00
2019-03-03 15:07:49 +01:00
newTx.ins = this.ins.map(txIn => {
2018-12-27 10:26:08 +01:00
return {
hash: txIn.hash,
index: txIn.index,
script: txIn.script,
sequence: txIn.sequence,
2019-03-03 15:07:49 +01:00
witness: txIn.witness,
};
});
2018-09-26 07:54:53 +02:00
2019-03-03 15:07:49 +01:00
newTx.outs = this.outs.map(txOut => {
2018-12-27 10:26:08 +01:00
return {
script: txOut.script,
2019-03-03 15:07:49 +01:00
value: (<Output>txOut).value,
};
});
2019-03-03 15:07:49 +01:00
return newTx;
2018-12-27 10:26:08 +01:00
}
2018-12-27 10:26:08 +01:00
/**
* Hash transaction for signing a specific input.
*
* Bitcoin uses a different hash for each signed transaction input.
* This method copies the transaction, makes the necessary changes based on the
* hashType, and then hashes the result.
* This hash can then be used to sign the provided transaction input.
*/
2019-03-03 15:07:49 +01:00
hashForSignature(
inIndex: number,
prevOutScript: Buffer,
hashType: number,
): Buffer {
typeforce(
types.tuple(types.UInt32, types.Buffer, /* types.UInt8 */ types.Number),
arguments,
);
2018-12-27 10:26:08 +01:00
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29
2019-03-03 15:07:49 +01:00
if (inIndex >= this.ins.length) return ONE;
2018-12-27 10:26:08 +01:00
// ignore OP_CODESEPARATOR
2019-03-03 15:07:49 +01:00
const ourScript = bscript.compile(
bscript.decompile(prevOutScript)!.filter(x => {
return x !== opcodes.OP_CODESEPARATOR;
}),
);
2018-12-27 10:26:08 +01:00
2019-03-03 15:07:49 +01:00
const txTmp = this.clone();
2018-12-27 10:26:08 +01:00
// SIGHASH_NONE: ignore all outputs? (wildcard payee)
if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) {
2019-03-03 15:07:49 +01:00
txTmp.outs = [];
2018-12-27 10:26:08 +01:00
// ignore sequence numbers (except at inIndex)
txTmp.ins.forEach((input, i) => {
2019-03-03 15:07:49 +01:00
if (i === inIndex) return;
2018-12-27 10:26:08 +01:00
2019-03-03 15:07:49 +01:00
input.sequence = 0;
});
2018-12-27 10:26:08 +01:00
// SIGHASH_SINGLE: ignore all outputs, except at the same index?
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE) {
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L60
2019-03-03 15:07:49 +01:00
if (inIndex >= this.outs.length) return ONE;
2018-12-27 10:26:08 +01:00
// truncate outputs after
2019-03-03 15:07:49 +01:00
txTmp.outs.length = inIndex + 1;
2018-12-27 10:26:08 +01:00
// "blank" outputs before
for (var i = 0; i < inIndex; i++) {
2019-03-03 15:07:49 +01:00
txTmp.outs[i] = BLANK_OUTPUT;
2018-12-27 10:26:08 +01:00
}
// ignore sequence numbers (except at inIndex)
txTmp.ins.forEach((input, y) => {
2019-03-03 15:07:49 +01:00
if (y === inIndex) return;
2018-12-27 10:26:08 +01:00
2019-03-03 15:07:49 +01:00
input.sequence = 0;
});
}
2018-12-27 10:26:08 +01:00
// SIGHASH_ANYONECANPAY: ignore inputs entirely?
if (hashType & Transaction.SIGHASH_ANYONECANPAY) {
2019-03-03 15:07:49 +01:00
txTmp.ins = [txTmp.ins[inIndex]];
txTmp.ins[0].script = ourScript;
2015-02-23 00:36:57 +01:00
2018-12-27 10:26:08 +01:00
// SIGHASH_ALL: only ignore input scripts
} else {
// "blank" others input scripts
2019-03-03 15:07:49 +01:00
txTmp.ins.forEach(input => {
input.script = EMPTY_SCRIPT;
});
txTmp.ins[inIndex].script = ourScript;
2018-12-27 10:26:08 +01:00
}
2018-09-26 07:54:53 +02:00
2018-12-27 10:26:08 +01:00
// serialize and hash
2019-03-03 15:07:49 +01:00
const buffer: Buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4);
buffer.writeInt32LE(hashType, buffer.length - 4);
txTmp.__toBuffer(buffer, 0, false);
2019-03-03 15:07:49 +01:00
return bcrypto.hash256(buffer);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
hashForWitnessV0(
inIndex: number,
prevOutScript: Buffer,
value: number,
hashType: number,
): Buffer {
typeforce(
types.tuple(types.UInt32, types.Buffer, types.Satoshi, types.UInt32),
arguments,
);
2014-03-21 03:15:15 +01:00
2019-03-03 15:07:49 +01:00
let tbuffer: Buffer = Buffer.from([]);
let toffset: number = 0;
2019-03-03 15:07:49 +01:00
function writeSlice(slice: Buffer): void {
toffset += slice.copy(tbuffer, toffset);
2018-12-27 10:26:08 +01:00
}
2014-03-21 03:15:15 +01:00
2019-03-03 15:07:49 +01:00
function writeUInt32(i: number): void {
toffset = tbuffer.writeUInt32LE(i, toffset);
2018-12-27 10:26:08 +01:00
}
2014-03-23 20:01:33 +01:00
2019-03-03 15:07:49 +01:00
function writeUInt64(i: number): void {
toffset = bufferutils.writeUInt64LE(tbuffer, i, toffset);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function writeVarInt(i: number): void {
varuint.encode(i, tbuffer, toffset);
toffset += varuint.encode.bytes;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function writeVarSlice(slice: Buffer): void {
writeVarInt(slice.length);
writeSlice(slice);
2018-09-26 07:54:53 +02:00
}
2019-03-03 15:07:49 +01:00
let hashOutputs = ZERO;
let hashPrevouts = ZERO;
let hashSequence = ZERO;
2018-12-27 10:26:08 +01:00
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
2019-03-03 15:07:49 +01:00
tbuffer = Buffer.allocUnsafe(36 * this.ins.length);
toffset = 0;
2018-09-26 07:54:53 +02:00
2019-03-03 15:07:49 +01:00
this.ins.forEach(txIn => {
writeSlice(txIn.hash);
writeUInt32(txIn.index);
});
2018-09-26 07:54:53 +02:00
2019-03-03 15:07:49 +01:00
hashPrevouts = bcrypto.hash256(tbuffer);
2018-12-27 10:26:08 +01:00
}
2018-09-26 07:54:53 +02:00
2019-03-03 15:07:49 +01:00
if (
!(hashType & Transaction.SIGHASH_ANYONECANPAY) &&
2018-12-27 10:26:08 +01:00
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
2019-03-03 15:07:49 +01:00
(hashType & 0x1f) !== Transaction.SIGHASH_NONE
) {
tbuffer = Buffer.allocUnsafe(4 * this.ins.length);
toffset = 0;
2018-09-26 07:54:53 +02:00
2019-03-03 15:07:49 +01:00
this.ins.forEach(txIn => {
writeUInt32(txIn.sequence);
});
2018-09-26 07:54:53 +02:00
2019-03-03 15:07:49 +01:00
hashSequence = bcrypto.hash256(tbuffer);
2018-12-27 10:26:08 +01:00
}
2018-09-26 07:54:53 +02:00
2019-03-03 15:07:49 +01:00
if (
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
(hashType & 0x1f) !== Transaction.SIGHASH_NONE
) {
2018-12-27 10:26:08 +01:00
const txOutsSize = this.outs.reduce((sum, output) => {
2019-03-03 15:07:49 +01:00
return sum + 8 + varSliceSize(output.script);
}, 0);
tbuffer = Buffer.allocUnsafe(txOutsSize);
toffset = 0;
this.outs.forEach(out => {
writeUInt64((<Output>out).value);
writeVarSlice(out.script);
});
hashOutputs = bcrypto.hash256(tbuffer);
} else if (
(hashType & 0x1f) === Transaction.SIGHASH_SINGLE &&
inIndex < this.outs.length
) {
const output = this.outs[inIndex];
tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script));
toffset = 0;
writeUInt64((<Output>output).value);
writeVarSlice(output.script);
hashOutputs = bcrypto.hash256(tbuffer);
}
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript));
toffset = 0;
const input = this.ins[inIndex];
writeUInt32(this.version);
writeSlice(hashPrevouts);
writeSlice(hashSequence);
writeSlice(input.hash);
writeUInt32(input.index);
writeVarSlice(prevOutScript);
writeUInt64(value);
writeUInt32(input.sequence);
writeSlice(hashOutputs);
writeUInt32(this.locktime);
writeUInt32(hashType);
return bcrypto.hash256(tbuffer);
2018-12-27 10:26:08 +01:00
}
2018-09-26 07:54:53 +02:00
2019-03-03 15:07:49 +01:00
getHash(forWitness?: boolean): Buffer {
2018-12-27 10:26:08 +01:00
// wtxid for coinbase is always 32 bytes of 0x00
2019-03-03 15:07:49 +01:00
if (forWitness && this.isCoinbase()) return Buffer.alloc(32, 0);
return bcrypto.hash256(this.__toBuffer(undefined, undefined, forWitness));
}
2019-03-03 15:07:49 +01:00
getId(): string {
2018-12-27 10:26:08 +01:00
// transaction hash's are displayed in reverse order
2019-03-03 15:07:49 +01:00
return reverseBuffer(this.getHash(false)).toString('hex');
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
toBuffer(buffer?: Buffer, initialOffset?: number): Buffer {
return this.__toBuffer(buffer, initialOffset, true);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
private __toBuffer(
buffer?: Buffer,
initialOffset?: number,
__allowWitness?: boolean,
): Buffer {
if (!buffer)
buffer = <Buffer>Buffer.allocUnsafe(this.__byteLength(__allowWitness!));
2019-03-03 15:07:49 +01:00
let offset = initialOffset || 0;
2019-03-03 15:07:49 +01:00
function writeSlice(slice: Buffer): void {
offset += slice.copy(buffer!, offset);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function writeUInt8(i: number) {
offset = buffer!.writeUInt8(i, offset);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function writeUInt32(i: number) {
offset = buffer!.writeUInt32LE(i, offset);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function writeInt32(i: number) {
offset = buffer!.writeInt32LE(i, offset);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function writeUInt64(i: number) {
offset = bufferutils.writeUInt64LE(buffer!, i, offset);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function writeVarInt(i: number) {
varuint.encode(i, buffer, offset);
offset += varuint.encode.bytes;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function writeVarSlice(slice: Buffer) {
writeVarInt(slice.length);
writeSlice(slice);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
function writeVector(vector: Array<Buffer>) {
writeVarInt(vector.length);
vector.forEach(writeVarSlice);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
writeInt32(this.version);
2019-03-03 15:07:49 +01:00
const hasWitnesses = __allowWitness && this.hasWitnesses();
2018-12-27 10:26:08 +01:00
if (hasWitnesses) {
2019-03-03 15:07:49 +01:00
writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER);
writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG);
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
writeVarInt(this.ins.length);
2014-03-23 20:02:31 +01:00
2019-03-03 15:07:49 +01:00
this.ins.forEach(txIn => {
writeSlice(txIn.hash);
writeUInt32(txIn.index);
writeVarSlice(txIn.script);
writeUInt32(txIn.sequence);
});
2019-03-03 15:07:49 +01:00
writeVarInt(this.outs.length);
this.outs.forEach(txOut => {
2018-12-27 10:26:08 +01:00
if (isOutput(txOut)) {
2019-03-03 15:07:49 +01:00
writeUInt64(txOut.value);
2018-12-27 10:26:08 +01:00
} else {
2019-03-03 15:07:49 +01:00
writeSlice(txOut.valueBuffer);
2018-12-27 10:26:08 +01:00
}
2014-03-23 20:02:31 +01:00
2019-03-03 15:07:49 +01:00
writeVarSlice(txOut.script);
});
2018-12-27 10:26:08 +01:00
if (hasWitnesses) {
2019-03-03 15:07:49 +01:00
this.ins.forEach(input => {
writeVector(input.witness);
});
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
writeUInt32(this.locktime);
2018-12-27 10:26:08 +01:00
// avoid slicing unless necessary
2019-03-03 15:07:49 +01:00
if (initialOffset !== undefined) return buffer.slice(initialOffset, offset);
return buffer;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
toHex() {
return this.toBuffer(undefined, undefined).toString('hex');
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
setInputScript(index: number, scriptSig: Buffer) {
typeforce(types.tuple(types.Number, types.Buffer), arguments);
2019-03-03 15:07:49 +01:00
this.ins[index].script = scriptSig;
2018-12-27 10:26:08 +01:00
}
2019-03-03 15:07:49 +01:00
setWitness(index: number, witness: Array<Buffer>) {
typeforce(types.tuple(types.Number, [types.Buffer]), arguments);
2019-03-03 15:07:49 +01:00
this.ins[index].witness = witness;
2018-12-27 10:26:08 +01:00
}
}