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