2021-10-20 12:18:01 +02:00
|
|
|
import {
|
|
|
|
BufferReader,
|
|
|
|
BufferWriter,
|
|
|
|
reverseBuffer,
|
|
|
|
varuint,
|
|
|
|
} from './bufferutils';
|
2019-03-07 05:40:23 +01:00
|
|
|
import * as bcrypto from './crypto';
|
|
|
|
import * as bscript from './script';
|
2019-03-03 15:07:49 +01:00
|
|
|
import { OPS as opcodes } from './script';
|
2019-03-07 05:40:23 +01:00
|
|
|
import * as types from './types';
|
2021-10-20 12:18:01 +02:00
|
|
|
const { typeforce } = types;
|
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;
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
return varuint.encodingLength(length) + length;
|
2016-07-14 11:50:35 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 05:40:23 +01:00
|
|
|
function vectorSize(someVector: Buffer[]): number {
|
2019-03-03 15:07:49 +01:00
|
|
|
const length = someVector.length;
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
return (
|
|
|
|
varuint.encodingLength(length) +
|
|
|
|
someVector.reduce((sum, witness) => {
|
|
|
|
return sum + varSliceSize(witness);
|
|
|
|
}, 0)
|
|
|
|
);
|
2016-07-14 11:50:35 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 00:33:18 +01:00
|
|
|
const EMPTY_BUFFER: Buffer = Buffer.allocUnsafe(0);
|
2019-03-07 05:40:23 +01:00
|
|
|
const EMPTY_WITNESS: Buffer[] = [];
|
2019-03-03 15:07:49 +01:00
|
|
|
const ZERO: Buffer = Buffer.from(
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
|
|
'hex',
|
|
|
|
);
|
|
|
|
const ONE: Buffer = Buffer.from(
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000001',
|
|
|
|
'hex',
|
|
|
|
);
|
|
|
|
const VALUE_UINT64_MAX: Buffer = Buffer.from('ffffffffffffffff', 'hex');
|
2019-11-21 08:35:40 +01:00
|
|
|
const BLANK_OUTPUT = {
|
2021-11-12 00:33:18 +01:00
|
|
|
script: EMPTY_BUFFER,
|
2019-03-03 15:07:49 +01:00
|
|
|
valueBuffer: VALUE_UINT64_MAX,
|
|
|
|
};
|
2014-09-15 06:21:01 +02:00
|
|
|
|
2019-11-21 08:35:40 +01:00
|
|
|
function isOutput(out: Output): boolean {
|
|
|
|
return out.value !== undefined;
|
2019-03-07 05:40:23 +01:00
|
|
|
}
|
2015-02-19 02:30:23 +01:00
|
|
|
|
2019-03-07 05:40:23 +01:00
|
|
|
export interface Output {
|
2019-03-03 15:07:49 +01:00
|
|
|
script: Buffer;
|
|
|
|
value: number;
|
2019-03-07 05:40:23 +01:00
|
|
|
}
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2019-03-07 05:40:23 +01:00
|
|
|
export interface Input {
|
2019-03-03 15:07:49 +01:00
|
|
|
hash: Buffer;
|
|
|
|
index: number;
|
|
|
|
script: Buffer;
|
|
|
|
sequence: number;
|
2019-03-07 05:40:23 +01:00
|
|
|
witness: Buffer[];
|
|
|
|
}
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2018-12-27 10:26:08 +01:00
|
|
|
export class Transaction {
|
2019-03-03 15:07:49 +01:00
|
|
|
static readonly DEFAULT_SEQUENCE = 0xffffffff;
|
2021-11-12 00:33:18 +01:00
|
|
|
static readonly SIGHASH_DEFAULT = 0x00;
|
2019-03-03 15:07:49 +01:00
|
|
|
static readonly SIGHASH_ALL = 0x01;
|
|
|
|
static readonly SIGHASH_NONE = 0x02;
|
|
|
|
static readonly SIGHASH_SINGLE = 0x03;
|
|
|
|
static readonly SIGHASH_ANYONECANPAY = 0x80;
|
2021-11-12 00:33:18 +01:00
|
|
|
static readonly SIGHASH_OUTPUT_MASK = 0x03;
|
|
|
|
static readonly SIGHASH_INPUT_MASK = 0x80;
|
2019-03-03 15:07:49 +01:00
|
|
|
static readonly ADVANCED_TRANSACTION_MARKER = 0x00;
|
|
|
|
static readonly ADVANCED_TRANSACTION_FLAG = 0x01;
|
|
|
|
|
2019-03-07 05:40:23 +01:00
|
|
|
static fromBuffer(buffer: Buffer, _NO_STRICT?: boolean): Transaction {
|
2020-01-15 02:50:33 +01:00
|
|
|
const bufferReader = new BufferReader(buffer);
|
2018-12-27 10:26:08 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
const tx = new Transaction();
|
2020-01-15 02:50:33 +01:00
|
|
|
tx.version = bufferReader.readInt32();
|
2018-12-27 10:26:08 +01:00
|
|
|
|
2020-01-15 02:50:33 +01:00
|
|
|
const marker = bufferReader.readUInt8();
|
|
|
|
const flag = bufferReader.readUInt8();
|
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
|
|
|
|
) {
|
|
|
|
hasWitnesses = true;
|
2020-01-15 02:50:33 +01:00
|
|
|
} else {
|
|
|
|
bufferReader.offset -= 2;
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2014-10-24 04:58:32 +02:00
|
|
|
|
2020-01-15 02:50:33 +01:00
|
|
|
const vinLen = bufferReader.readVarInt();
|
2019-03-07 05:40:23 +01:00
|
|
|
for (let i = 0; i < vinLen; ++i) {
|
2018-12-27 10:26:08 +01:00
|
|
|
tx.ins.push({
|
2020-01-15 02:50:33 +01:00
|
|
|
hash: bufferReader.readSlice(32),
|
|
|
|
index: bufferReader.readUInt32(),
|
|
|
|
script: bufferReader.readVarSlice(),
|
|
|
|
sequence: bufferReader.readUInt32(),
|
2019-03-03 15:07:49 +01:00
|
|
|
witness: EMPTY_WITNESS,
|
|
|
|
});
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2014-10-24 04:58:32 +02:00
|
|
|
|
2020-01-15 02:50:33 +01:00
|
|
|
const voutLen = bufferReader.readVarInt();
|
2019-03-07 05:40:23 +01:00
|
|
|
for (let i = 0; i < voutLen; ++i) {
|
2018-12-27 10:26:08 +01:00
|
|
|
tx.outs.push({
|
2020-01-15 02:50:33 +01:00
|
|
|
value: bufferReader.readUInt64(),
|
|
|
|
script: bufferReader.readVarSlice(),
|
2019-03-03 15:07:49 +01:00
|
|
|
});
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2015-02-19 02:04:37 +01:00
|
|
|
|
2018-12-27 10:26:08 +01:00
|
|
|
if (hasWitnesses) {
|
2019-03-07 05:40:23 +01:00
|
|
|
for (let i = 0; i < vinLen; ++i) {
|
2020-01-15 02:50:33 +01:00
|
|
|
tx.ins[i].witness = bufferReader.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
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2020-01-15 02:50:33 +01:00
|
|
|
tx.locktime = bufferReader.readUInt32();
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2019-03-07 05:40:23 +01:00
|
|
|
if (_NO_STRICT) return tx;
|
2020-01-15 02:50:33 +01:00
|
|
|
if (bufferReader.offset !== buffer.length)
|
2019-03-03 15:07:49 +01:00
|
|
|
throw new Error('Transaction has unexpected data');
|
2017-06-20 13:46:46 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
return tx;
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2017-06-20 13:46:46 +02: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
|
|
|
}
|
2016-11-14 01:37:45 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
static isCoinbaseHash(buffer: Buffer): boolean {
|
|
|
|
typeforce(types.Hash256bit, buffer);
|
2019-03-07 05:40:23 +01:00
|
|
|
for (let 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
|
|
|
}
|
2015-03-16 00:50:47 +01:00
|
|
|
|
2019-03-20 07:25:48 +01:00
|
|
|
version: number = 1;
|
|
|
|
locktime: number = 0;
|
|
|
|
ins: Input[] = [];
|
2019-11-21 08:35:40 +01:00
|
|
|
outs: Output[] = [];
|
2019-03-07 05:40:23 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
isCoinbase(): boolean {
|
|
|
|
return (
|
|
|
|
this.ins.length === 1 && Transaction.isCoinbaseHash(this.ins[0].hash)
|
|
|
|
);
|
2018-12-18 16:16:48 +01:00
|
|
|
}
|
2014-03-23 20:03:58 +01:00
|
|
|
|
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-18 16:16:48 +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({
|
2019-03-07 05:40:23 +01:00
|
|
|
hash,
|
|
|
|
index,
|
2021-11-12 00:33:18 +01:00
|
|
|
script: scriptSig || EMPTY_BUFFER,
|
2019-03-07 05:40:23 +01:00
|
|
|
sequence: sequence as number,
|
2019-03-03 15:07:49 +01:00
|
|
|
witness: EMPTY_WITNESS,
|
|
|
|
}) - 1
|
|
|
|
);
|
2018-12-18 16:16:48 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
addOutput(scriptPubKey: Buffer, value: number): number {
|
|
|
|
typeforce(types.tuple(types.Buffer, types.Satoshi), arguments);
|
2013-11-20 19:00:49 +01:00
|
|
|
|
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,
|
2019-03-07 05:40:23 +01:00
|
|
|
value,
|
2019-03-03 15:07:49 +01:00
|
|
|
}) - 1
|
|
|
|
);
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2015-04-28 02:35:32 +02: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
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
weight(): number {
|
2019-12-09 02:37:55 +01:00
|
|
|
const base = this.byteLength(false);
|
|
|
|
const total = this.byteLength(true);
|
2019-03-03 15:07:49 +01:00
|
|
|
return base * 3 + total;
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2013-02-17 06:39:15 +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
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2019-12-09 02:37:55 +01:00
|
|
|
byteLength(_ALLOW_WITNESS: boolean = true): number {
|
|
|
|
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
|
|
|
|
|
|
|
|
return (
|
|
|
|
(hasWitnesses ? 10 : 8) +
|
|
|
|
varuint.encodingLength(this.ins.length) +
|
|
|
|
varuint.encodingLength(this.outs.length) +
|
|
|
|
this.ins.reduce((sum, input) => {
|
|
|
|
return sum + 40 + varSliceSize(input.script);
|
|
|
|
}, 0) +
|
|
|
|
this.outs.reduce((sum, output) => {
|
|
|
|
return sum + 8 + varSliceSize(output.script);
|
|
|
|
}, 0) +
|
|
|
|
(hasWitnesses
|
|
|
|
? this.ins.reduce((sum, input) => {
|
|
|
|
return sum + vectorSize(input.witness);
|
|
|
|
}, 0)
|
|
|
|
: 0)
|
|
|
|
);
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2015-04-28 02:35:32 +02: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-11-21 10:01:04 +01:00
|
|
|
value: txOut.value,
|
2019-03-03 15:07:49 +01:00
|
|
|
};
|
|
|
|
});
|
2018-12-18 16:16:48 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
return newTx;
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2015-04-28 02:35:32 +02: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
|
2019-03-07 05:40:23 +01:00
|
|
|
for (let i = 0; i < inIndex; i++) {
|
2019-11-21 08:35:40 +01:00
|
|
|
(txTmp.outs as any)[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-18 16:16:48 +01:00
|
|
|
}
|
2015-04-28 02:35:32 +02:00
|
|
|
|
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 => {
|
2021-11-12 00:33:18 +01:00
|
|
|
input.script = EMPTY_BUFFER;
|
2019-03-03 15:07:49 +01:00
|
|
|
});
|
|
|
|
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-12-09 02:37:55 +01:00
|
|
|
const buffer: Buffer = Buffer.allocUnsafe(txTmp.byteLength(false) + 4);
|
2019-03-03 15:07:49 +01:00
|
|
|
buffer.writeInt32LE(hashType, buffer.length - 4);
|
|
|
|
txTmp.__toBuffer(buffer, 0, false);
|
2015-04-28 02:35:32 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
return bcrypto.hash256(buffer);
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2016-06-22 05:54:55 +02:00
|
|
|
|
2021-11-12 00:33:18 +01:00
|
|
|
hashForWitnessV1(
|
|
|
|
inIndex: number,
|
|
|
|
prevOutScripts: Buffer[],
|
|
|
|
values: number[],
|
|
|
|
hashType: number,
|
|
|
|
leafHash?: Buffer,
|
|
|
|
annex?: Buffer,
|
|
|
|
): Buffer {
|
|
|
|
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#common-signature-message
|
|
|
|
typeforce(
|
|
|
|
types.tuple(
|
|
|
|
types.UInt32,
|
|
|
|
typeforce.arrayOf(types.Buffer),
|
|
|
|
typeforce.arrayOf(types.Satoshi),
|
|
|
|
types.UInt32,
|
|
|
|
),
|
|
|
|
arguments,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
|
|
|
values.length !== this.ins.length ||
|
|
|
|
prevOutScripts.length !== this.ins.length
|
|
|
|
) {
|
|
|
|
throw new Error('Must supply prevout script and value for all inputs');
|
|
|
|
}
|
|
|
|
|
|
|
|
const outputType =
|
|
|
|
hashType === Transaction.SIGHASH_DEFAULT
|
|
|
|
? Transaction.SIGHASH_ALL
|
|
|
|
: hashType & Transaction.SIGHASH_OUTPUT_MASK;
|
|
|
|
|
|
|
|
const inputType = hashType & Transaction.SIGHASH_INPUT_MASK;
|
|
|
|
|
|
|
|
const isAnyoneCanPay = inputType === Transaction.SIGHASH_ANYONECANPAY;
|
|
|
|
const isNone = outputType === Transaction.SIGHASH_NONE;
|
|
|
|
const isSingle = outputType === Transaction.SIGHASH_SINGLE;
|
|
|
|
|
|
|
|
let hashPrevouts = EMPTY_BUFFER;
|
|
|
|
let hashAmounts = EMPTY_BUFFER;
|
|
|
|
let hashScriptPubKeys = EMPTY_BUFFER;
|
|
|
|
let hashSequences = EMPTY_BUFFER;
|
|
|
|
let hashOutputs = EMPTY_BUFFER;
|
|
|
|
|
|
|
|
if (!isAnyoneCanPay) {
|
|
|
|
let bufferWriter = BufferWriter.withCapacity(36 * this.ins.length);
|
|
|
|
this.ins.forEach(txIn => {
|
|
|
|
bufferWriter.writeSlice(txIn.hash);
|
|
|
|
bufferWriter.writeUInt32(txIn.index);
|
|
|
|
});
|
|
|
|
hashPrevouts = bcrypto.sha256(bufferWriter.end());
|
|
|
|
|
|
|
|
bufferWriter = BufferWriter.withCapacity(8 * this.ins.length);
|
|
|
|
values.forEach(value => bufferWriter.writeUInt64(value));
|
|
|
|
hashAmounts = bcrypto.sha256(bufferWriter.end());
|
|
|
|
|
|
|
|
bufferWriter = BufferWriter.withCapacity(
|
|
|
|
prevOutScripts.map(varSliceSize).reduce((a, b) => a + b),
|
|
|
|
);
|
|
|
|
prevOutScripts.forEach(prevOutScript =>
|
|
|
|
bufferWriter.writeVarSlice(prevOutScript),
|
|
|
|
);
|
|
|
|
hashScriptPubKeys = bcrypto.sha256(bufferWriter.end());
|
|
|
|
|
|
|
|
bufferWriter = BufferWriter.withCapacity(4 * this.ins.length);
|
|
|
|
this.ins.forEach(txIn => bufferWriter.writeUInt32(txIn.sequence));
|
|
|
|
hashSequences = bcrypto.sha256(bufferWriter.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(isNone || isSingle)) {
|
|
|
|
const txOutsSize = this.outs
|
|
|
|
.map(output => 8 + varSliceSize(output.script))
|
|
|
|
.reduce((a, b) => a + b);
|
|
|
|
const bufferWriter = BufferWriter.withCapacity(txOutsSize);
|
|
|
|
|
|
|
|
this.outs.forEach(out => {
|
|
|
|
bufferWriter.writeUInt64(out.value);
|
|
|
|
bufferWriter.writeVarSlice(out.script);
|
|
|
|
});
|
|
|
|
|
|
|
|
hashOutputs = bcrypto.sha256(bufferWriter.end());
|
|
|
|
} else if (isSingle && inIndex < this.outs.length) {
|
|
|
|
const output = this.outs[inIndex];
|
|
|
|
|
|
|
|
const bufferWriter = BufferWriter.withCapacity(
|
|
|
|
8 + varSliceSize(output.script),
|
|
|
|
);
|
|
|
|
bufferWriter.writeUInt64(output.value);
|
|
|
|
bufferWriter.writeVarSlice(output.script);
|
|
|
|
hashOutputs = bcrypto.sha256(bufferWriter.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
const spendType = (leafHash ? 2 : 0) + (annex ? 1 : 0);
|
|
|
|
|
|
|
|
// Length calculation from:
|
|
|
|
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-14
|
|
|
|
// With extension from:
|
|
|
|
// https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki#signature-validation
|
|
|
|
const sigMsgSize =
|
|
|
|
174 -
|
|
|
|
(isAnyoneCanPay ? 49 : 0) -
|
|
|
|
(isNone ? 32 : 0) +
|
|
|
|
(annex ? 32 : 0) +
|
|
|
|
(leafHash ? 37 : 0);
|
|
|
|
const sigMsgWriter = BufferWriter.withCapacity(sigMsgSize);
|
|
|
|
|
|
|
|
sigMsgWriter.writeUInt8(hashType);
|
|
|
|
// Transaction
|
|
|
|
sigMsgWriter.writeInt32(this.version);
|
|
|
|
sigMsgWriter.writeUInt32(this.locktime);
|
|
|
|
sigMsgWriter.writeSlice(hashPrevouts);
|
|
|
|
sigMsgWriter.writeSlice(hashAmounts);
|
|
|
|
sigMsgWriter.writeSlice(hashScriptPubKeys);
|
|
|
|
sigMsgWriter.writeSlice(hashSequences);
|
|
|
|
if (!(isNone || isSingle)) {
|
|
|
|
sigMsgWriter.writeSlice(hashOutputs);
|
|
|
|
}
|
|
|
|
// Input
|
|
|
|
sigMsgWriter.writeUInt8(spendType);
|
|
|
|
if (isAnyoneCanPay) {
|
|
|
|
const input = this.ins[inIndex];
|
|
|
|
sigMsgWriter.writeSlice(input.hash);
|
|
|
|
sigMsgWriter.writeUInt32(input.index);
|
|
|
|
sigMsgWriter.writeUInt64(values[inIndex]);
|
|
|
|
sigMsgWriter.writeVarSlice(prevOutScripts[inIndex]);
|
|
|
|
sigMsgWriter.writeUInt32(input.sequence);
|
|
|
|
} else {
|
|
|
|
sigMsgWriter.writeUInt32(inIndex);
|
|
|
|
}
|
|
|
|
if (annex) {
|
|
|
|
const bufferWriter = BufferWriter.withCapacity(varSliceSize(annex));
|
|
|
|
bufferWriter.writeVarSlice(annex);
|
|
|
|
sigMsgWriter.writeSlice(bcrypto.sha256(bufferWriter.end()));
|
|
|
|
}
|
|
|
|
// Output
|
|
|
|
if (isSingle) {
|
|
|
|
sigMsgWriter.writeSlice(hashOutputs);
|
|
|
|
}
|
|
|
|
// BIP342 extension
|
|
|
|
if (leafHash) {
|
|
|
|
sigMsgWriter.writeSlice(leafHash);
|
|
|
|
sigMsgWriter.writeUInt8(0);
|
|
|
|
sigMsgWriter.writeUInt32(0xffffffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extra zero byte because:
|
|
|
|
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-19
|
|
|
|
return bcrypto.taggedHash(
|
|
|
|
'TapSighash',
|
|
|
|
Buffer.concat([Buffer.of(0x00), sigMsgWriter.end()]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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([]);
|
2020-01-14 09:53:32 +01:00
|
|
|
let bufferWriter: BufferWriter;
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
let hashOutputs = ZERO;
|
|
|
|
let hashPrevouts = ZERO;
|
|
|
|
let hashSequence = ZERO;
|
2016-11-09 01:54:17 +01:00
|
|
|
|
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);
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter = new BufferWriter(tbuffer, 0);
|
2018-09-26 07:54:53 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
this.ins.forEach(txIn => {
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeSlice(txIn.hash);
|
|
|
|
bufferWriter.writeUInt32(txIn.index);
|
2019-03-03 15:07:49 +01:00
|
|
|
});
|
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);
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter = new BufferWriter(tbuffer, 0);
|
2018-09-26 07:54:53 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
this.ins.forEach(txIn => {
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeUInt32(txIn.sequence);
|
2019-03-03 15:07:49 +01:00
|
|
|
});
|
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);
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter = new BufferWriter(tbuffer, 0);
|
2019-03-03 15:07:49 +01:00
|
|
|
|
|
|
|
this.outs.forEach(out => {
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeUInt64(out.value);
|
|
|
|
bufferWriter.writeVarSlice(out.script);
|
2019-03-03 15:07:49 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
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));
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter = new BufferWriter(tbuffer, 0);
|
|
|
|
bufferWriter.writeUInt64(output.value);
|
|
|
|
bufferWriter.writeVarSlice(output.script);
|
2019-03-03 15:07:49 +01:00
|
|
|
|
|
|
|
hashOutputs = bcrypto.hash256(tbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript));
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter = new BufferWriter(tbuffer, 0);
|
2019-03-03 15:07:49 +01:00
|
|
|
|
|
|
|
const input = this.ins[inIndex];
|
2021-08-17 09:25:33 +02:00
|
|
|
bufferWriter.writeInt32(this.version);
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeSlice(hashPrevouts);
|
|
|
|
bufferWriter.writeSlice(hashSequence);
|
|
|
|
bufferWriter.writeSlice(input.hash);
|
|
|
|
bufferWriter.writeUInt32(input.index);
|
|
|
|
bufferWriter.writeVarSlice(prevOutScript);
|
|
|
|
bufferWriter.writeUInt64(value);
|
|
|
|
bufferWriter.writeUInt32(input.sequence);
|
|
|
|
bufferWriter.writeSlice(hashOutputs);
|
|
|
|
bufferWriter.writeUInt32(this.locktime);
|
|
|
|
bufferWriter.writeUInt32(hashType);
|
2019-03-03 15:07:49 +01:00
|
|
|
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));
|
2016-07-14 11:50:35 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2016-07-14 11:50:35 +02: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
|
|
|
}
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2019-03-21 16:15:37 +01:00
|
|
|
toHex(): string {
|
2019-03-07 05:40:23 +01:00
|
|
|
return this.toBuffer(undefined, undefined).toString('hex');
|
|
|
|
}
|
|
|
|
|
2019-03-21 16:15:37 +01:00
|
|
|
setInputScript(index: number, scriptSig: Buffer): void {
|
2019-03-07 05:40:23 +01:00
|
|
|
typeforce(types.tuple(types.Number, types.Buffer), arguments);
|
|
|
|
|
|
|
|
this.ins[index].script = scriptSig;
|
|
|
|
}
|
|
|
|
|
2019-03-21 16:15:37 +01:00
|
|
|
setWitness(index: number, witness: Buffer[]): void {
|
2019-03-07 05:40:23 +01:00
|
|
|
typeforce(types.tuple(types.Number, [types.Buffer]), arguments);
|
|
|
|
|
|
|
|
this.ins[index].witness = witness;
|
|
|
|
}
|
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
private __toBuffer(
|
|
|
|
buffer?: Buffer,
|
|
|
|
initialOffset?: number,
|
2019-12-09 02:37:55 +01:00
|
|
|
_ALLOW_WITNESS: boolean = false,
|
2019-03-03 15:07:49 +01:00
|
|
|
): Buffer {
|
|
|
|
if (!buffer)
|
2019-12-09 02:37:55 +01:00
|
|
|
buffer = Buffer.allocUnsafe(this.byteLength(_ALLOW_WITNESS)) as Buffer;
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2020-01-14 09:53:32 +01:00
|
|
|
const bufferWriter = new BufferWriter(buffer, initialOffset || 0);
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeInt32(this.version);
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2019-03-07 05:40:23 +01:00
|
|
|
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2018-12-27 10:26:08 +01:00
|
|
|
if (hasWitnesses) {
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER);
|
|
|
|
bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG);
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2014-05-01 22:36:21 +02:00
|
|
|
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeVarInt(this.ins.length);
|
2014-03-23 20:02:31 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
this.ins.forEach(txIn => {
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeSlice(txIn.hash);
|
|
|
|
bufferWriter.writeUInt32(txIn.index);
|
|
|
|
bufferWriter.writeVarSlice(txIn.script);
|
|
|
|
bufferWriter.writeUInt32(txIn.sequence);
|
2019-03-03 15:07:49 +01:00
|
|
|
});
|
2015-04-28 02:35:32 +02:00
|
|
|
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeVarInt(this.outs.length);
|
2019-03-03 15:07:49 +01:00
|
|
|
this.outs.forEach(txOut => {
|
2018-12-27 10:26:08 +01:00
|
|
|
if (isOutput(txOut)) {
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeUInt64(txOut.value);
|
2018-12-27 10:26:08 +01:00
|
|
|
} else {
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeSlice((txOut as any).valueBuffer);
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2014-03-23 20:02:31 +01:00
|
|
|
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeVarSlice(txOut.script);
|
2019-03-03 15:07:49 +01:00
|
|
|
});
|
2016-07-14 11:50:35 +02:00
|
|
|
|
2018-12-27 10:26:08 +01:00
|
|
|
if (hasWitnesses) {
|
2019-03-03 15:07:49 +01:00
|
|
|
this.ins.forEach(input => {
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeVector(input.witness);
|
2019-03-03 15:07:49 +01:00
|
|
|
});
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2020-01-14 09:53:32 +01:00
|
|
|
bufferWriter.writeUInt32(this.locktime);
|
2013-10-07 14:21:00 +02:00
|
|
|
|
2018-12-27 10:26:08 +01:00
|
|
|
// avoid slicing unless necessary
|
2020-01-14 09:53:32 +01:00
|
|
|
if (initialOffset !== undefined)
|
|
|
|
return buffer.slice(initialOffset, bufferWriter.offset);
|
2019-03-03 15:07:49 +01:00
|
|
|
return buffer;
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
|
|
|
}
|