sed -i 's/ var / const /', with const->let fixes
This commit is contained in:
parent
91b8823aa8
commit
a5db0a4e44
46 changed files with 776 additions and 769 deletions
src
address.jsblock.jsbufferutils.jsecpair.jsscript.jsscript_number.jsscript_signature.js
templates
index.js
transaction.jstransaction_builder.jsmultisig
nulldata.jspubkey
pubkeyhash
scripthash
witnesscommitment
witnesspubkeyhash
witnessscripthash
|
@ -8,21 +8,21 @@ const typeforce = require('typeforce')
|
|||
const types = require('./types')
|
||||
|
||||
function fromBase58Check (address) {
|
||||
var payload = bs58check.decode(address)
|
||||
const payload = bs58check.decode(address)
|
||||
|
||||
// TODO: 4.0.0, move to "toOutputScript"
|
||||
if (payload.length < 21) throw new TypeError(address + ' is too short')
|
||||
if (payload.length > 21) throw new TypeError(address + ' is too long')
|
||||
|
||||
var version = payload.readUInt8(0)
|
||||
var hash = payload.slice(1)
|
||||
const version = payload.readUInt8(0)
|
||||
const hash = payload.slice(1)
|
||||
|
||||
return { version: version, hash: hash }
|
||||
}
|
||||
|
||||
function fromBech32 (address) {
|
||||
var result = bech32.decode(address)
|
||||
var data = bech32.fromWords(result.words.slice(1))
|
||||
const result = bech32.decode(address)
|
||||
const data = bech32.fromWords(result.words.slice(1))
|
||||
|
||||
return {
|
||||
version: result.words[0],
|
||||
|
@ -34,7 +34,7 @@ function fromBech32 (address) {
|
|||
function toBase58Check (hash, version) {
|
||||
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments)
|
||||
|
||||
var payload = Buffer.allocUnsafe(21)
|
||||
const payload = Buffer.allocUnsafe(21)
|
||||
payload.writeUInt8(version, 0)
|
||||
hash.copy(payload, 1)
|
||||
|
||||
|
@ -42,7 +42,7 @@ function toBase58Check (hash, version) {
|
|||
}
|
||||
|
||||
function toBech32 (data, version, prefix) {
|
||||
var words = bech32.toWords(data)
|
||||
const words = bech32.toWords(data)
|
||||
words.unshift(version)
|
||||
|
||||
return bech32.encode(prefix, words)
|
||||
|
@ -62,7 +62,7 @@ function fromOutputScript (outputScript, network) {
|
|||
function toOutputScript (address, network) {
|
||||
network = network || networks.bitcoin
|
||||
|
||||
var decode
|
||||
let decode
|
||||
try {
|
||||
decode = fromBase58Check(address)
|
||||
} catch (e) {}
|
||||
|
|
38
src/block.js
38
src/block.js
|
@ -19,25 +19,25 @@ function Block () {
|
|||
Block.fromBuffer = function (buffer) {
|
||||
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)')
|
||||
|
||||
var offset = 0
|
||||
let offset = 0
|
||||
function readSlice (n) {
|
||||
offset += n
|
||||
return buffer.slice(offset - n, offset)
|
||||
}
|
||||
|
||||
function readUInt32 () {
|
||||
var i = buffer.readUInt32LE(offset)
|
||||
const i = buffer.readUInt32LE(offset)
|
||||
offset += 4
|
||||
return i
|
||||
}
|
||||
|
||||
function readInt32 () {
|
||||
var i = buffer.readInt32LE(offset)
|
||||
const i = buffer.readInt32LE(offset)
|
||||
offset += 4
|
||||
return i
|
||||
}
|
||||
|
||||
var block = new Block()
|
||||
const block = new Block()
|
||||
block.version = readInt32()
|
||||
block.prevHash = readSlice(32)
|
||||
block.merkleRoot = readSlice(32)
|
||||
|
@ -48,22 +48,22 @@ Block.fromBuffer = function (buffer) {
|
|||
if (buffer.length === 80) return block
|
||||
|
||||
function readVarInt () {
|
||||
var vi = varuint.decode(buffer, offset)
|
||||
const vi = varuint.decode(buffer, offset)
|
||||
offset += varuint.decode.bytes
|
||||
return vi
|
||||
}
|
||||
|
||||
function readTransaction () {
|
||||
var tx = Transaction.fromBuffer(buffer.slice(offset), true)
|
||||
const tx = Transaction.fromBuffer(buffer.slice(offset), true)
|
||||
offset += tx.byteLength()
|
||||
return tx
|
||||
}
|
||||
|
||||
var nTransactions = readVarInt()
|
||||
const nTransactions = readVarInt()
|
||||
block.transactions = []
|
||||
|
||||
for (var i = 0; i < nTransactions; ++i) {
|
||||
var tx = readTransaction()
|
||||
const tx = readTransaction()
|
||||
block.transactions.push(tx)
|
||||
}
|
||||
|
||||
|
@ -91,7 +91,7 @@ Block.prototype.getId = function () {
|
|||
}
|
||||
|
||||
Block.prototype.getUTCDate = function () {
|
||||
var date = new Date(0) // epoch
|
||||
const date = new Date(0) // epoch
|
||||
date.setUTCSeconds(this.timestamp)
|
||||
|
||||
return date
|
||||
|
@ -99,9 +99,9 @@ Block.prototype.getUTCDate = function () {
|
|||
|
||||
// TODO: buffer, offset compatibility
|
||||
Block.prototype.toBuffer = function (headersOnly) {
|
||||
var buffer = Buffer.allocUnsafe(this.byteLength(headersOnly))
|
||||
const buffer = Buffer.allocUnsafe(this.byteLength(headersOnly))
|
||||
|
||||
var offset = 0
|
||||
let offset = 0
|
||||
function writeSlice (slice) {
|
||||
slice.copy(buffer, offset)
|
||||
offset += slice.length
|
||||
|
@ -129,7 +129,7 @@ Block.prototype.toBuffer = function (headersOnly) {
|
|||
offset += varuint.encode.bytes
|
||||
|
||||
this.transactions.forEach(function (tx) {
|
||||
var txSize = tx.byteLength() // TODO: extract from toBuffer?
|
||||
const txSize = tx.byteLength() // TODO: extract from toBuffer?
|
||||
tx.toBuffer(buffer, offset)
|
||||
offset += txSize
|
||||
})
|
||||
|
@ -142,9 +142,9 @@ Block.prototype.toHex = function (headersOnly) {
|
|||
}
|
||||
|
||||
Block.calculateTarget = function (bits) {
|
||||
var exponent = ((bits & 0xff000000) >> 24) - 3
|
||||
var mantissa = bits & 0x007fffff
|
||||
var target = Buffer.alloc(32, 0)
|
||||
const exponent = ((bits & 0xff000000) >> 24) - 3
|
||||
const mantissa = bits & 0x007fffff
|
||||
const target = Buffer.alloc(32, 0)
|
||||
target.writeUInt32BE(mantissa, 28 - exponent)
|
||||
return target
|
||||
}
|
||||
|
@ -153,7 +153,7 @@ Block.calculateMerkleRoot = function (transactions) {
|
|||
typeforce([{ getHash: types.Function }], transactions)
|
||||
if (transactions.length === 0) throw TypeError('Cannot compute merkle root for zero transactions')
|
||||
|
||||
var hashes = transactions.map(function (transaction) {
|
||||
const hashes = transactions.map(function (transaction) {
|
||||
return transaction.getHash()
|
||||
})
|
||||
|
||||
|
@ -163,13 +163,13 @@ Block.calculateMerkleRoot = function (transactions) {
|
|||
Block.prototype.checkMerkleRoot = function () {
|
||||
if (!this.transactions) return false
|
||||
|
||||
var actualMerkleRoot = Block.calculateMerkleRoot(this.transactions)
|
||||
const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions)
|
||||
return this.merkleRoot.compare(actualMerkleRoot) === 0
|
||||
}
|
||||
|
||||
Block.prototype.checkProofOfWork = function () {
|
||||
var hash = this.getHash().reverse()
|
||||
var target = Block.calculateTarget(this.bits)
|
||||
const hash = this.getHash().reverse()
|
||||
const target = Block.calculateTarget(this.bits)
|
||||
|
||||
return hash.compare(target) <= 0
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ function verifuint (value, max) {
|
|||
}
|
||||
|
||||
function readUInt64LE (buffer, offset) {
|
||||
var a = buffer.readUInt32LE(offset)
|
||||
var b = buffer.readUInt32LE(offset + 4)
|
||||
const a = buffer.readUInt32LE(offset)
|
||||
let b = buffer.readUInt32LE(offset + 4)
|
||||
b *= 0x100000000
|
||||
|
||||
verifuint(b + a, 0x001fffffffffffff)
|
||||
|
|
|
@ -63,8 +63,8 @@ function fromPublicKey (buffer, options) {
|
|||
}
|
||||
|
||||
function fromWIF (string, network) {
|
||||
let decoded = wif.decode(string)
|
||||
let version = decoded.version
|
||||
const decoded = wif.decode(string)
|
||||
const version = decoded.version
|
||||
|
||||
// list of networks?
|
||||
if (types.Array(network)) {
|
||||
|
@ -90,7 +90,7 @@ function fromWIF (string, network) {
|
|||
function makeRandom (options) {
|
||||
typeforce(isOptions, options)
|
||||
options = options || {}
|
||||
let rng = options.rng || randomBytes
|
||||
const rng = options.rng || randomBytes
|
||||
|
||||
let d
|
||||
do {
|
||||
|
|
|
@ -38,7 +38,7 @@ function compile (chunks) {
|
|||
|
||||
typeforce(types.Array, chunks)
|
||||
|
||||
var bufferSize = chunks.reduce(function (accum, chunk) {
|
||||
const bufferSize = chunks.reduce(function (accum, chunk) {
|
||||
// data chunk
|
||||
if (Buffer.isBuffer(chunk)) {
|
||||
// adhere to BIP62.3, minimal push policy
|
||||
|
@ -53,14 +53,14 @@ function compile (chunks) {
|
|||
return accum + 1
|
||||
}, 0.0)
|
||||
|
||||
var buffer = Buffer.allocUnsafe(bufferSize)
|
||||
var offset = 0
|
||||
const buffer = Buffer.allocUnsafe(bufferSize)
|
||||
let offset = 0
|
||||
|
||||
chunks.forEach(function (chunk) {
|
||||
// data chunk
|
||||
if (Buffer.isBuffer(chunk)) {
|
||||
// adhere to BIP62.3, minimal push policy
|
||||
var opcode = asMinimalOP(chunk)
|
||||
const opcode = asMinimalOP(chunk)
|
||||
if (opcode !== undefined) {
|
||||
buffer.writeUInt8(opcode, offset)
|
||||
offset += 1
|
||||
|
@ -88,15 +88,15 @@ function decompile (buffer) {
|
|||
|
||||
typeforce(types.Buffer, buffer)
|
||||
|
||||
var chunks = []
|
||||
var i = 0
|
||||
const chunks = []
|
||||
let i = 0
|
||||
|
||||
while (i < buffer.length) {
|
||||
var opcode = buffer[i]
|
||||
const opcode = buffer[i]
|
||||
|
||||
// data chunk
|
||||
if ((opcode > OPS.OP_0) && (opcode <= OPS.OP_PUSHDATA4)) {
|
||||
var d = pushdata.decode(buffer, i)
|
||||
const d = pushdata.decode(buffer, i)
|
||||
|
||||
// did reading a pushDataInt fail? empty script
|
||||
if (d === null) return null
|
||||
|
@ -105,11 +105,11 @@ function decompile (buffer) {
|
|||
// attempt to read too much data? empty script
|
||||
if (i + d.number > buffer.length) return null
|
||||
|
||||
var data = buffer.slice(i, i + d.number)
|
||||
const data = buffer.slice(i, i + d.number)
|
||||
i += d.number
|
||||
|
||||
// decompile minimally
|
||||
var op = asMinimalOP(data)
|
||||
const op = asMinimalOP(data)
|
||||
if (op !== undefined) {
|
||||
chunks.push(op)
|
||||
} else {
|
||||
|
@ -135,7 +135,7 @@ function toASM (chunks) {
|
|||
return chunks.map(function (chunk) {
|
||||
// data?
|
||||
if (Buffer.isBuffer(chunk)) {
|
||||
var op = asMinimalOP(chunk)
|
||||
const op = asMinimalOP(chunk)
|
||||
if (op === undefined) return chunk.toString('hex')
|
||||
chunk = op
|
||||
}
|
||||
|
@ -175,7 +175,7 @@ function isCanonicalPubKey (buffer) {
|
|||
}
|
||||
|
||||
function isDefinedHashType (hashType) {
|
||||
var hashTypeMod = hashType & ~0x80
|
||||
const hashTypeMod = hashType & ~0x80
|
||||
|
||||
// return hashTypeMod > SIGHASH_ALL && hashTypeMod < SIGHASH_SINGLE
|
||||
return hashTypeMod > 0x00 && hashTypeMod < 0x04
|
||||
|
|
|
@ -4,7 +4,7 @@ function decode (buffer, maxLength, minimal) {
|
|||
maxLength = maxLength || 4
|
||||
minimal = minimal === undefined ? true : minimal
|
||||
|
||||
var length = buffer.length
|
||||
const length = buffer.length
|
||||
if (length === 0) return 0
|
||||
if (length > maxLength) throw new TypeError('Script number overflow')
|
||||
if (minimal) {
|
||||
|
@ -15,16 +15,15 @@ function decode (buffer, maxLength, minimal) {
|
|||
|
||||
// 40-bit
|
||||
if (length === 5) {
|
||||
var a = buffer.readUInt32LE(0)
|
||||
var b = buffer.readUInt8(4)
|
||||
const a = buffer.readUInt32LE(0)
|
||||
const b = buffer.readUInt8(4)
|
||||
|
||||
if (b & 0x80) return -(((b & ~0x80) * 0x100000000) + a)
|
||||
return (b * 0x100000000) + a
|
||||
}
|
||||
|
||||
var result = 0
|
||||
|
||||
// 32-bit / 24-bit / 16-bit / 8-bit
|
||||
let result = 0
|
||||
for (var i = 0; i < length; ++i) {
|
||||
result |= buffer[i] << (8 * i)
|
||||
}
|
||||
|
@ -43,10 +42,10 @@ function scriptNumSize (i) {
|
|||
}
|
||||
|
||||
function encode (number) {
|
||||
var value = Math.abs(number)
|
||||
var size = scriptNumSize(value)
|
||||
var buffer = Buffer.allocUnsafe(size)
|
||||
var negative = number < 0
|
||||
let value = Math.abs(number)
|
||||
const size = scriptNumSize(value)
|
||||
const buffer = Buffer.allocUnsafe(size)
|
||||
const negative = number < 0
|
||||
|
||||
for (var i = 0; i < size; ++i) {
|
||||
buffer.writeUInt8(value & 0xff, i)
|
||||
|
|
|
@ -15,21 +15,21 @@ function toDER (x) {
|
|||
|
||||
function fromDER (x) {
|
||||
if (x[0] === 0x00) x = x.slice(1)
|
||||
let buffer = Buffer.alloc(32, 0)
|
||||
let bstart = Math.max(0, 32 - x.length)
|
||||
const buffer = Buffer.alloc(32, 0)
|
||||
const bstart = Math.max(0, 32 - x.length)
|
||||
x.copy(buffer, bstart)
|
||||
return buffer
|
||||
}
|
||||
|
||||
// BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed)
|
||||
function decode (buffer) {
|
||||
let hashType = buffer.readUInt8(buffer.length - 1)
|
||||
let hashTypeMod = hashType & ~0x80
|
||||
const hashType = buffer.readUInt8(buffer.length - 1)
|
||||
const hashTypeMod = hashType & ~0x80
|
||||
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
|
||||
|
||||
let decode = bip66.decode(buffer.slice(0, -1))
|
||||
let r = fromDER(decode.r)
|
||||
let s = fromDER(decode.s)
|
||||
const decode = bip66.decode(buffer.slice(0, -1))
|
||||
const r = fromDER(decode.r)
|
||||
const s = fromDER(decode.s)
|
||||
|
||||
return {
|
||||
signature: Buffer.concat([r, s], 64),
|
||||
|
@ -43,14 +43,14 @@ function encode (signature, hashType) {
|
|||
hashType: types.UInt8
|
||||
}, { signature, hashType })
|
||||
|
||||
let hashTypeMod = hashType & ~0x80
|
||||
const hashTypeMod = hashType & ~0x80
|
||||
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
|
||||
|
||||
let hashTypeBuffer = Buffer.allocUnsafe(1)
|
||||
const hashTypeBuffer = Buffer.allocUnsafe(1)
|
||||
hashTypeBuffer.writeUInt8(hashType, 0)
|
||||
|
||||
let r = toDER(signature.slice(0, 32))
|
||||
let s = toDER(signature.slice(32, 64))
|
||||
const r = toDER(signature.slice(0, 32))
|
||||
const s = toDER(signature.slice(32, 64))
|
||||
|
||||
return Buffer.concat([
|
||||
bip66.encode(r, s),
|
||||
|
|
|
@ -27,7 +27,7 @@ function classifyOutput (script) {
|
|||
if (scriptHash.output.check(script)) return types.P2SH
|
||||
|
||||
// XXX: optimization, below functions .decompile before use
|
||||
var chunks = decompile(script)
|
||||
const chunks = decompile(script)
|
||||
if (!chunks) throw new TypeError('Invalid script')
|
||||
|
||||
if (multisig.output.check(chunks)) return types.MULTISIG
|
||||
|
@ -40,7 +40,7 @@ function classifyOutput (script) {
|
|||
|
||||
function classifyInput (script, allowIncomplete) {
|
||||
// XXX: optimization, below functions .decompile before use
|
||||
var chunks = decompile(script)
|
||||
const chunks = decompile(script)
|
||||
if (!chunks) throw new TypeError('Invalid script')
|
||||
|
||||
if (pubKeyHash.input.check(chunks)) return types.P2PKH
|
||||
|
@ -53,7 +53,7 @@ function classifyInput (script, allowIncomplete) {
|
|||
|
||||
function classifyWitness (script, allowIncomplete) {
|
||||
// XXX: optimization, below functions .decompile before use
|
||||
var chunks = decompile(script)
|
||||
const chunks = decompile(script)
|
||||
if (!chunks) throw new TypeError('Invalid script')
|
||||
|
||||
if (witnessPubKeyHash.input.check(chunks)) return types.P2WPKH
|
||||
|
|
|
@ -11,7 +11,7 @@ function partialSignature (value) {
|
|||
}
|
||||
|
||||
function check (script, allowIncomplete) {
|
||||
var chunks = bscript.decompile(script)
|
||||
const chunks = bscript.decompile(script)
|
||||
if (chunks.length < 2) return false
|
||||
if (chunks[0] !== OPS.OP_0) return false
|
||||
|
||||
|
@ -29,7 +29,7 @@ function encodeStack (signatures, scriptPubKey) {
|
|||
typeforce([partialSignature], signatures)
|
||||
|
||||
if (scriptPubKey) {
|
||||
var scriptData = p2mso.decode(scriptPubKey)
|
||||
const scriptData = p2mso.decode(scriptPubKey)
|
||||
|
||||
if (signatures.length < scriptData.m) {
|
||||
throw new TypeError('Not enough signatures provided')
|
||||
|
@ -59,7 +59,7 @@ function decodeStack (stack, allowIncomplete) {
|
|||
}
|
||||
|
||||
function decode (buffer, allowIncomplete) {
|
||||
var stack = bscript.decompile(buffer)
|
||||
const stack = bscript.decompile(buffer)
|
||||
return decodeStack(stack, allowIncomplete)
|
||||
}
|
||||
|
||||
|
|
|
@ -7,14 +7,14 @@ const OPS = require('bitcoin-ops')
|
|||
const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
|
||||
|
||||
function check (script, allowIncomplete) {
|
||||
var chunks = bscript.decompile(script)
|
||||
const chunks = bscript.decompile(script)
|
||||
|
||||
if (chunks.length < 4) return false
|
||||
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) return false
|
||||
if (!types.Number(chunks[0])) return false
|
||||
if (!types.Number(chunks[chunks.length - 2])) return false
|
||||
var m = chunks[0] - OP_INT_BASE
|
||||
var n = chunks[chunks.length - 2] - OP_INT_BASE
|
||||
const m = chunks[0] - OP_INT_BASE
|
||||
const n = chunks[chunks.length - 2] - OP_INT_BASE
|
||||
|
||||
if (m <= 0) return false
|
||||
if (n > 16) return false
|
||||
|
@ -22,7 +22,7 @@ function check (script, allowIncomplete) {
|
|||
if (n !== chunks.length - 3) return false
|
||||
if (allowIncomplete) return true
|
||||
|
||||
var keys = chunks.slice(1, -2)
|
||||
const keys = chunks.slice(1, -2)
|
||||
return keys.every(bscript.isCanonicalPubKey)
|
||||
}
|
||||
check.toJSON = function () { return 'multi-sig output' }
|
||||
|
@ -36,7 +36,7 @@ function encode (m, pubKeys) {
|
|||
pubKeys: pubKeys
|
||||
})
|
||||
|
||||
var n = pubKeys.length
|
||||
const n = pubKeys.length
|
||||
if (n < m) throw new TypeError('Not enough pubKeys provided')
|
||||
|
||||
return bscript.compile([].concat(
|
||||
|
@ -48,7 +48,7 @@ function encode (m, pubKeys) {
|
|||
}
|
||||
|
||||
function decode (buffer, allowIncomplete) {
|
||||
var chunks = bscript.decompile(buffer)
|
||||
const chunks = bscript.decompile(buffer)
|
||||
typeforce(check, chunks, allowIncomplete)
|
||||
|
||||
return {
|
||||
|
|
|
@ -6,7 +6,7 @@ const typeforce = require('typeforce')
|
|||
const OPS = require('bitcoin-ops')
|
||||
|
||||
function check (script) {
|
||||
var buffer = bscript.compile(script)
|
||||
const buffer = bscript.compile(script)
|
||||
|
||||
return buffer.length > 1 &&
|
||||
buffer[0] === OPS.OP_RETURN
|
||||
|
|
|
@ -4,7 +4,7 @@ const bscript = require('../../script')
|
|||
const typeforce = require('typeforce')
|
||||
|
||||
function check (script) {
|
||||
var chunks = bscript.decompile(script)
|
||||
const chunks = bscript.decompile(script)
|
||||
|
||||
return chunks.length === 1 &&
|
||||
bscript.isCanonicalScriptSignature(chunks[0])
|
||||
|
@ -27,7 +27,7 @@ function decodeStack (stack) {
|
|||
}
|
||||
|
||||
function decode (buffer) {
|
||||
var stack = bscript.decompile(buffer)
|
||||
const stack = bscript.decompile(buffer)
|
||||
return decodeStack(stack)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ const typeforce = require('typeforce')
|
|||
const OPS = require('bitcoin-ops')
|
||||
|
||||
function check (script) {
|
||||
var chunks = bscript.decompile(script)
|
||||
const chunks = bscript.decompile(script)
|
||||
|
||||
return chunks.length === 2 &&
|
||||
bscript.isCanonicalPubKey(chunks[0]) &&
|
||||
|
@ -20,7 +20,7 @@ function encode (pubKey) {
|
|||
}
|
||||
|
||||
function decode (buffer) {
|
||||
var chunks = bscript.decompile(buffer)
|
||||
const chunks = bscript.decompile(buffer)
|
||||
typeforce(check, chunks)
|
||||
|
||||
return chunks[0]
|
||||
|
|
|
@ -4,7 +4,7 @@ const bscript = require('../../script')
|
|||
const typeforce = require('typeforce')
|
||||
|
||||
function check (script) {
|
||||
var chunks = bscript.decompile(script)
|
||||
const chunks = bscript.decompile(script)
|
||||
|
||||
return chunks.length === 2 &&
|
||||
bscript.isCanonicalScriptSignature(chunks[0]) &&
|
||||
|
@ -39,7 +39,7 @@ function decodeStack (stack) {
|
|||
}
|
||||
|
||||
function decode (buffer) {
|
||||
var stack = bscript.decompile(buffer)
|
||||
const stack = bscript.decompile(buffer)
|
||||
return decodeStack(stack)
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ const typeforce = require('typeforce')
|
|||
const OPS = require('bitcoin-ops')
|
||||
|
||||
function check (script) {
|
||||
var buffer = bscript.compile(script)
|
||||
const buffer = bscript.compile(script)
|
||||
|
||||
return buffer.length === 25 &&
|
||||
buffer[0] === OPS.OP_DUP &&
|
||||
|
|
|
@ -11,14 +11,14 @@ const p2wpkho = require('../witnesspubkeyhash/output')
|
|||
const p2wsho = require('../witnessscripthash/output')
|
||||
|
||||
function check (script, allowIncomplete) {
|
||||
var chunks = bscript.decompile(script)
|
||||
const chunks = bscript.decompile(script)
|
||||
if (chunks.length < 1) return false
|
||||
|
||||
var lastChunk = chunks[chunks.length - 1]
|
||||
const lastChunk = chunks[chunks.length - 1]
|
||||
if (!Buffer.isBuffer(lastChunk)) return false
|
||||
|
||||
var scriptSigChunks = bscript.decompile(bscript.compile(chunks.slice(0, -1)))
|
||||
var redeemScriptChunks = bscript.decompile(lastChunk)
|
||||
const scriptSigChunks = bscript.decompile(bscript.compile(chunks.slice(0, -1)))
|
||||
const redeemScriptChunks = bscript.decompile(lastChunk)
|
||||
|
||||
// is redeemScript a valid script?
|
||||
if (!redeemScriptChunks) return false
|
||||
|
@ -47,13 +47,13 @@ function check (script, allowIncomplete) {
|
|||
check.toJSON = function () { return 'scriptHash input' }
|
||||
|
||||
function encodeStack (redeemScriptStack, redeemScript) {
|
||||
var serializedScriptPubKey = bscript.compile(redeemScript)
|
||||
const serializedScriptPubKey = bscript.compile(redeemScript)
|
||||
|
||||
return [].concat(redeemScriptStack, serializedScriptPubKey)
|
||||
}
|
||||
|
||||
function encode (redeemScriptSig, redeemScript) {
|
||||
var redeemScriptStack = bscript.decompile(redeemScriptSig)
|
||||
const redeemScriptStack = bscript.decompile(redeemScriptSig)
|
||||
|
||||
return bscript.compile(encodeStack(redeemScriptStack, redeemScript))
|
||||
}
|
||||
|
@ -69,8 +69,8 @@ function decodeStack (stack) {
|
|||
}
|
||||
|
||||
function decode (buffer) {
|
||||
var stack = bscript.decompile(buffer)
|
||||
var result = decodeStack(stack)
|
||||
const stack = bscript.decompile(buffer)
|
||||
const result = decodeStack(stack)
|
||||
result.redeemScriptSig = bscript.compile(result.redeemScriptStack)
|
||||
delete result.redeemScriptStack
|
||||
return result
|
||||
|
|
|
@ -6,7 +6,7 @@ const typeforce = require('typeforce')
|
|||
const OPS = require('bitcoin-ops')
|
||||
|
||||
function check (script) {
|
||||
var buffer = bscript.compile(script)
|
||||
const buffer = bscript.compile(script)
|
||||
|
||||
return buffer.length === 23 &&
|
||||
buffer[0] === OPS.OP_HASH160 &&
|
||||
|
|
|
@ -9,7 +9,7 @@ const OPS = require('bitcoin-ops')
|
|||
const HEADER = Buffer.from('aa21a9ed', 'hex')
|
||||
|
||||
function check (script) {
|
||||
var buffer = bscript.compile(script)
|
||||
const buffer = bscript.compile(script)
|
||||
|
||||
return buffer.length > 37 &&
|
||||
buffer[0] === OPS.OP_RETURN &&
|
||||
|
@ -22,7 +22,7 @@ check.toJSON = function () { return 'Witness commitment output' }
|
|||
function encode (commitment) {
|
||||
typeforce(types.Hash256bit, commitment)
|
||||
|
||||
var buffer = Buffer.allocUnsafe(36)
|
||||
const buffer = Buffer.allocUnsafe(36)
|
||||
HEADER.copy(buffer, 0)
|
||||
commitment.copy(buffer, 4)
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ function isCompressedCanonicalPubKey (pubKey) {
|
|||
}
|
||||
|
||||
function check (script) {
|
||||
var chunks = bscript.decompile(script)
|
||||
const chunks = bscript.decompile(script)
|
||||
|
||||
return chunks.length === 2 &&
|
||||
bscript.isCanonicalScriptSignature(chunks[0]) &&
|
||||
|
|
|
@ -6,7 +6,7 @@ const typeforce = require('typeforce')
|
|||
const OPS = require('bitcoin-ops')
|
||||
|
||||
function check (script) {
|
||||
var buffer = bscript.compile(script)
|
||||
const buffer = bscript.compile(script)
|
||||
|
||||
return buffer.length === 22 &&
|
||||
buffer[0] === OPS.OP_0 &&
|
||||
|
|
|
@ -12,15 +12,15 @@ function check (chunks, allowIncomplete) {
|
|||
typeforce(types.Array, chunks)
|
||||
if (chunks.length < 1) return false
|
||||
|
||||
var witnessScript = chunks[chunks.length - 1]
|
||||
const witnessScript = chunks[chunks.length - 1]
|
||||
if (!Buffer.isBuffer(witnessScript)) return false
|
||||
|
||||
var witnessScriptChunks = bscript.decompile(witnessScript)
|
||||
const witnessScriptChunks = bscript.decompile(witnessScript)
|
||||
|
||||
// is witnessScript a valid script?
|
||||
if (witnessScriptChunks.length === 0) return false
|
||||
|
||||
var witnessRawScriptSig = bscript.compile(chunks.slice(0, -1))
|
||||
const witnessRawScriptSig = bscript.compile(chunks.slice(0, -1))
|
||||
|
||||
// match types
|
||||
if (p2pkh.input.check(witnessRawScriptSig) &&
|
||||
|
|
|
@ -6,7 +6,7 @@ const typeforce = require('typeforce')
|
|||
const OPS = require('bitcoin-ops')
|
||||
|
||||
function check (script) {
|
||||
var buffer = bscript.compile(script)
|
||||
const buffer = bscript.compile(script)
|
||||
|
||||
return buffer.length === 34 &&
|
||||
buffer[0] === OPS.OP_0 &&
|
||||
|
|
|
@ -8,13 +8,13 @@ const types = require('./types')
|
|||
const varuint = require('varuint-bitcoin')
|
||||
|
||||
function varSliceSize (someScript) {
|
||||
var length = someScript.length
|
||||
const length = someScript.length
|
||||
|
||||
return varuint.encodingLength(length) + length
|
||||
}
|
||||
|
||||
function vectorSize (someVector) {
|
||||
var length = someVector.length
|
||||
const length = someVector.length
|
||||
|
||||
return varuint.encodingLength(length) + someVector.reduce(function (sum, witness) {
|
||||
return sum + varSliceSize(witness)
|
||||
|
@ -47,32 +47,32 @@ const BLANK_OUTPUT = {
|
|||
}
|
||||
|
||||
Transaction.fromBuffer = function (buffer, __noStrict) {
|
||||
var offset = 0
|
||||
let offset = 0
|
||||
function readSlice (n) {
|
||||
offset += n
|
||||
return buffer.slice(offset - n, offset)
|
||||
}
|
||||
|
||||
function readUInt32 () {
|
||||
var i = buffer.readUInt32LE(offset)
|
||||
const i = buffer.readUInt32LE(offset)
|
||||
offset += 4
|
||||
return i
|
||||
}
|
||||
|
||||
function readInt32 () {
|
||||
var i = buffer.readInt32LE(offset)
|
||||
const i = buffer.readInt32LE(offset)
|
||||
offset += 4
|
||||
return i
|
||||
}
|
||||
|
||||
function readUInt64 () {
|
||||
var i = bufferutils.readUInt64LE(buffer, offset)
|
||||
const i = bufferutils.readUInt64LE(buffer, offset)
|
||||
offset += 8
|
||||
return i
|
||||
}
|
||||
|
||||
function readVarInt () {
|
||||
var vi = varuint.decode(buffer, offset)
|
||||
const vi = varuint.decode(buffer, offset)
|
||||
offset += varuint.decode.bytes
|
||||
return vi
|
||||
}
|
||||
|
@ -82,26 +82,26 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
|
|||
}
|
||||
|
||||
function readVector () {
|
||||
var count = readVarInt()
|
||||
var vector = []
|
||||
const count = readVarInt()
|
||||
const vector = []
|
||||
for (var i = 0; i < count; i++) vector.push(readVarSlice())
|
||||
return vector
|
||||
}
|
||||
|
||||
var tx = new Transaction()
|
||||
const tx = new Transaction()
|
||||
tx.version = readInt32()
|
||||
|
||||
var marker = buffer.readUInt8(offset)
|
||||
var flag = buffer.readUInt8(offset + 1)
|
||||
const marker = buffer.readUInt8(offset)
|
||||
const flag = buffer.readUInt8(offset + 1)
|
||||
|
||||
var hasWitnesses = false
|
||||
let hasWitnesses = false
|
||||
if (marker === Transaction.ADVANCED_TRANSACTION_MARKER &&
|
||||
flag === Transaction.ADVANCED_TRANSACTION_FLAG) {
|
||||
offset += 2
|
||||
hasWitnesses = true
|
||||
}
|
||||
|
||||
var vinLen = readVarInt()
|
||||
const vinLen = readVarInt()
|
||||
for (var i = 0; i < vinLen; ++i) {
|
||||
tx.ins.push({
|
||||
hash: readSlice(32),
|
||||
|
@ -112,7 +112,7 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
|
|||
})
|
||||
}
|
||||
|
||||
var voutLen = readVarInt()
|
||||
const voutLen = readVarInt()
|
||||
for (i = 0; i < voutLen; ++i) {
|
||||
tx.outs.push({
|
||||
value: readUInt64(),
|
||||
|
@ -192,8 +192,8 @@ Transaction.prototype.hasWitnesses = function () {
|
|||
}
|
||||
|
||||
Transaction.prototype.weight = function () {
|
||||
var base = this.__byteLength(false)
|
||||
var total = this.__byteLength(true)
|
||||
const base = this.__byteLength(false)
|
||||
const total = this.__byteLength(true)
|
||||
return base * 3 + total
|
||||
}
|
||||
|
||||
|
@ -206,7 +206,7 @@ Transaction.prototype.byteLength = function () {
|
|||
}
|
||||
|
||||
Transaction.prototype.__byteLength = function (__allowWitness) {
|
||||
var hasWitnesses = __allowWitness && this.hasWitnesses()
|
||||
const hasWitnesses = __allowWitness && this.hasWitnesses()
|
||||
|
||||
return (
|
||||
(hasWitnesses ? 10 : 8) +
|
||||
|
@ -219,7 +219,7 @@ Transaction.prototype.__byteLength = function (__allowWitness) {
|
|||
}
|
||||
|
||||
Transaction.prototype.clone = function () {
|
||||
var newTx = new Transaction()
|
||||
const newTx = new Transaction()
|
||||
newTx.version = this.version
|
||||
newTx.locktime = this.locktime
|
||||
|
||||
|
@ -258,11 +258,11 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
|
|||
if (inIndex >= this.ins.length) return ONE
|
||||
|
||||
// ignore OP_CODESEPARATOR
|
||||
var ourScript = bscript.compile(bscript.decompile(prevOutScript).filter(function (x) {
|
||||
const ourScript = bscript.compile(bscript.decompile(prevOutScript).filter(function (x) {
|
||||
return x !== opcodes.OP_CODESEPARATOR
|
||||
}))
|
||||
|
||||
var txTmp = this.clone()
|
||||
const txTmp = this.clone()
|
||||
|
||||
// SIGHASH_NONE: ignore all outputs? (wildcard payee)
|
||||
if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) {
|
||||
|
@ -309,7 +309,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
|
|||
}
|
||||
|
||||
// serialize and hash
|
||||
var buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4)
|
||||
const buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4)
|
||||
buffer.writeInt32LE(hashType, buffer.length - 4)
|
||||
txTmp.__toBuffer(buffer, 0, false)
|
||||
|
||||
|
@ -319,7 +319,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
|
|||
Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value, hashType) {
|
||||
typeforce(types.tuple(types.UInt32, types.Buffer, types.Satoshi, types.UInt32), arguments)
|
||||
|
||||
var tbuffer, toffset
|
||||
let tbuffer, toffset
|
||||
function writeSlice (slice) { toffset += slice.copy(tbuffer, toffset) }
|
||||
function writeUInt32 (i) { toffset = tbuffer.writeUInt32LE(i, toffset) }
|
||||
function writeUInt64 (i) { toffset = bufferutils.writeUInt64LE(tbuffer, i, toffset) }
|
||||
|
@ -329,9 +329,9 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
|||
}
|
||||
function writeVarSlice (slice) { writeVarInt(slice.length); writeSlice(slice) }
|
||||
|
||||
var hashOutputs = ZERO
|
||||
var hashPrevouts = ZERO
|
||||
var hashSequence = ZERO
|
||||
let hashOutputs = ZERO
|
||||
let hashPrevouts = ZERO
|
||||
let hashSequence = ZERO
|
||||
|
||||
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
|
||||
tbuffer = Buffer.allocUnsafe(36 * this.ins.length)
|
||||
|
@ -360,7 +360,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
|||
|
||||
if ((hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
|
||||
(hashType & 0x1f) !== Transaction.SIGHASH_NONE) {
|
||||
var txOutsSize = this.outs.reduce(function (sum, output) {
|
||||
const txOutsSize = this.outs.reduce(function (sum, output) {
|
||||
return sum + 8 + varSliceSize(output.script)
|
||||
}, 0)
|
||||
|
||||
|
@ -374,7 +374,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
|||
|
||||
hashOutputs = bcrypto.hash256(tbuffer)
|
||||
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE && inIndex < this.outs.length) {
|
||||
var output = this.outs[inIndex]
|
||||
const output = this.outs[inIndex]
|
||||
|
||||
tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script))
|
||||
toffset = 0
|
||||
|
@ -387,7 +387,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
|||
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript))
|
||||
toffset = 0
|
||||
|
||||
var input = this.ins[inIndex]
|
||||
const input = this.ins[inIndex]
|
||||
writeUInt32(this.version)
|
||||
writeSlice(hashPrevouts)
|
||||
writeSlice(hashSequence)
|
||||
|
@ -418,7 +418,7 @@ Transaction.prototype.toBuffer = function (buffer, initialOffset) {
|
|||
Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitness) {
|
||||
if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength(__allowWitness))
|
||||
|
||||
var offset = initialOffset || 0
|
||||
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) }
|
||||
|
@ -433,7 +433,7 @@ Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitne
|
|||
|
||||
writeInt32(this.version)
|
||||
|
||||
var hasWitnesses = __allowWitness && this.hasWitnesses()
|
||||
const hasWitnesses = __allowWitness && this.hasWitnesses()
|
||||
|
||||
if (hasWitnesses) {
|
||||
writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER)
|
||||
|
|
|
@ -23,8 +23,8 @@ function supportedP2SHType (type) {
|
|||
}
|
||||
|
||||
function extractChunks (type, chunks, script) {
|
||||
var pubKeys = []
|
||||
var signatures = []
|
||||
let pubKeys = []
|
||||
let signatures = []
|
||||
switch (type) {
|
||||
case scriptTypes.P2PKH:
|
||||
// if (redeemScript) throw new Error('Nonstandard... P2SH(P2PKH)')
|
||||
|
@ -39,7 +39,7 @@ function extractChunks (type, chunks, script) {
|
|||
|
||||
case scriptTypes.MULTISIG:
|
||||
if (script) {
|
||||
var multisig = btemplates.multisig.output.decode(script)
|
||||
const multisig = btemplates.multisig.output.decode(script)
|
||||
pubKeys = multisig.pubKeys
|
||||
}
|
||||
|
||||
|
@ -57,22 +57,22 @@ function extractChunks (type, chunks, script) {
|
|||
function expandInput (scriptSig, witnessStack) {
|
||||
if (scriptSig.length === 0 && witnessStack.length === 0) return {}
|
||||
|
||||
var prevOutScript
|
||||
var prevOutType
|
||||
var scriptType
|
||||
var script
|
||||
var redeemScript
|
||||
var witnessScript
|
||||
var witnessScriptType
|
||||
var redeemScriptType
|
||||
var witness = false
|
||||
var p2wsh = false
|
||||
var p2sh = false
|
||||
var witnessProgram
|
||||
var chunks
|
||||
let prevOutScript
|
||||
let prevOutType
|
||||
let scriptType
|
||||
let script
|
||||
let redeemScript
|
||||
let witnessScript
|
||||
let witnessScriptType
|
||||
let redeemScriptType
|
||||
let witness = false
|
||||
let p2wsh = false
|
||||
let p2sh = false
|
||||
let witnessProgram
|
||||
let chunks
|
||||
|
||||
var scriptSigChunks = bscript.decompile(scriptSig) || []
|
||||
var sigType = btemplates.classifyInput(scriptSigChunks, true)
|
||||
const scriptSigChunks = bscript.decompile(scriptSig) || []
|
||||
const sigType = btemplates.classifyInput(scriptSigChunks, true)
|
||||
if (sigType === scriptTypes.P2SH) {
|
||||
p2sh = true
|
||||
redeemScript = scriptSigChunks[scriptSigChunks.length - 1]
|
||||
|
@ -82,7 +82,7 @@ function expandInput (scriptSig, witnessStack) {
|
|||
script = redeemScript
|
||||
}
|
||||
|
||||
var classifyWitness = btemplates.classifyWitness(witnessStack, true)
|
||||
const classifyWitness = btemplates.classifyWitness(witnessStack, true)
|
||||
if (classifyWitness === scriptTypes.P2WSH) {
|
||||
witnessScript = witnessStack[witnessStack.length - 1]
|
||||
witnessScriptType = btemplates.classifyOutput(witnessScript)
|
||||
|
@ -114,8 +114,8 @@ function expandInput (scriptSig, witnessStack) {
|
|||
chunks = witnessStack.slice(0, -1)
|
||||
} else if (classifyWitness === scriptTypes.P2WPKH) {
|
||||
witness = true
|
||||
var key = witnessStack[witnessStack.length - 1]
|
||||
var keyHash = bcrypto.hash160(key)
|
||||
const key = witnessStack[witnessStack.length - 1]
|
||||
const keyHash = bcrypto.hash160(key)
|
||||
if (scriptSig.length === 0) {
|
||||
prevOutScript = btemplates.witnessPubKeyHash.output.encode(keyHash)
|
||||
prevOutType = scriptTypes.P2WPKH
|
||||
|
@ -147,9 +147,9 @@ function expandInput (scriptSig, witnessStack) {
|
|||
chunks = scriptSigChunks
|
||||
}
|
||||
|
||||
var expanded = extractChunks(scriptType, chunks, script)
|
||||
const expanded = extractChunks(scriptType, chunks, script)
|
||||
|
||||
var result = {
|
||||
const result = {
|
||||
pubKeys: expanded.pubKeys,
|
||||
signatures: expanded.signatures,
|
||||
prevOutScript: prevOutScript,
|
||||
|
@ -177,11 +177,11 @@ function fixMultisigOrder (input, transaction, vin) {
|
|||
if (input.redeemScriptType !== scriptTypes.MULTISIG || !input.redeemScript) return
|
||||
if (input.pubKeys.length === input.signatures.length) return
|
||||
|
||||
var unmatched = input.signatures.concat()
|
||||
const unmatched = input.signatures.concat()
|
||||
|
||||
input.signatures = input.pubKeys.map(function (pubKey) {
|
||||
var keyPair = ECPair.fromPublicKey(pubKey)
|
||||
var match
|
||||
const keyPair = ECPair.fromPublicKey(pubKey)
|
||||
let match
|
||||
|
||||
// check for a signature
|
||||
unmatched.some(function (signature, i) {
|
||||
|
@ -189,8 +189,8 @@ function fixMultisigOrder (input, transaction, vin) {
|
|||
if (!signature) return false
|
||||
|
||||
// TODO: avoid O(n) hashForSignature
|
||||
var parsed = bscript.signature.decode(signature)
|
||||
var hash = transaction.hashForSignature(vin, input.redeemScript, parsed.hashType)
|
||||
const parsed = bscript.signature.decode(signature)
|
||||
const hash = transaction.hashForSignature(vin, input.redeemScript, parsed.hashType)
|
||||
|
||||
// skip if signature does not match pubKey
|
||||
if (!keyPair.verify(hash, parsed.signature)) return false
|
||||
|
@ -209,20 +209,20 @@ function fixMultisigOrder (input, transaction, vin) {
|
|||
function expandOutput (script, scriptType, ourPubKey) {
|
||||
typeforce(types.Buffer, script)
|
||||
|
||||
var scriptChunks = bscript.decompile(script) || []
|
||||
const scriptChunks = bscript.decompile(script) || []
|
||||
if (!scriptType) {
|
||||
scriptType = btemplates.classifyOutput(script)
|
||||
}
|
||||
|
||||
var pubKeys = []
|
||||
let pubKeys = []
|
||||
|
||||
switch (scriptType) {
|
||||
// does our hash160(pubKey) match the output scripts?
|
||||
case scriptTypes.P2PKH:
|
||||
if (!ourPubKey) break
|
||||
|
||||
var pkh1 = scriptChunks[2]
|
||||
var pkh2 = bcrypto.hash160(ourPubKey)
|
||||
const pkh1 = scriptChunks[2]
|
||||
const pkh2 = bcrypto.hash160(ourPubKey)
|
||||
if (pkh1.equals(pkh2)) pubKeys = [ourPubKey]
|
||||
break
|
||||
|
||||
|
@ -230,8 +230,8 @@ function expandOutput (script, scriptType, ourPubKey) {
|
|||
case scriptTypes.P2WPKH:
|
||||
if (!ourPubKey) break
|
||||
|
||||
var wpkh1 = scriptChunks[1]
|
||||
var wpkh2 = bcrypto.hash160(ourPubKey)
|
||||
const wpkh1 = scriptChunks[1]
|
||||
const wpkh2 = bcrypto.hash160(ourPubKey)
|
||||
if (wpkh1.equals(wpkh2)) pubKeys = [ourPubKey]
|
||||
break
|
||||
|
||||
|
@ -257,7 +257,7 @@ function checkP2SHInput (input, redeemScriptHash) {
|
|||
if (input.prevOutType) {
|
||||
if (input.prevOutType !== scriptTypes.P2SH) throw new Error('PrevOutScript must be P2SH')
|
||||
|
||||
var chunks = bscript.decompile(input.prevOutScript)
|
||||
const chunks = bscript.decompile(input.prevOutScript)
|
||||
if (!chunks) throw new Error('Invalid prevOutScript')
|
||||
if (!chunks[1].equals(redeemScriptHash)) throw new Error('Inconsistent hash160(redeemScript)')
|
||||
}
|
||||
|
@ -267,28 +267,28 @@ function checkP2WSHInput (input, witnessScriptHash) {
|
|||
if (input.prevOutType) {
|
||||
if (input.prevOutType !== scriptTypes.P2WSH) throw new Error('PrevOutScript must be P2WSH')
|
||||
|
||||
var chunks = bscript.decompile(input.prevOutScript)
|
||||
const chunks = bscript.decompile(input.prevOutScript)
|
||||
if (!chunks) throw new Error('Invalid witnessScript')
|
||||
if (!chunks[1].equals(witnessScriptHash)) throw new Error('Inconsistent sha256(witnessScript)')
|
||||
}
|
||||
}
|
||||
|
||||
function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScript) {
|
||||
var expanded
|
||||
var prevOutType
|
||||
var prevOutScript
|
||||
let expanded
|
||||
let prevOutType
|
||||
let prevOutScript
|
||||
|
||||
var p2sh = false
|
||||
var p2shType
|
||||
var redeemScriptHash
|
||||
let p2sh = false
|
||||
let p2shType
|
||||
let redeemScriptHash
|
||||
|
||||
var witness = false
|
||||
var p2wsh = false
|
||||
var witnessType
|
||||
var witnessScriptHash
|
||||
let witness = false
|
||||
let p2wsh = false
|
||||
let witnessType
|
||||
let witnessScriptHash
|
||||
|
||||
var signType
|
||||
var signScript
|
||||
let signType
|
||||
let signScript
|
||||
|
||||
if (redeemScript && witnessScript) {
|
||||
redeemScriptHash = bcrypto.hash160(redeemScript)
|
||||
|
@ -408,15 +408,15 @@ function buildStack (type, signatures, pubKeys, allowIncomplete) {
|
|||
}
|
||||
|
||||
function buildInput (input, allowIncomplete) {
|
||||
var scriptType = input.prevOutType
|
||||
var sig = []
|
||||
var witness = []
|
||||
let scriptType = input.prevOutType
|
||||
let sig = []
|
||||
let witness = []
|
||||
|
||||
if (supportedType(scriptType)) {
|
||||
sig = buildStack(scriptType, input.signatures, input.pubKeys, allowIncomplete)
|
||||
}
|
||||
|
||||
var p2sh = false
|
||||
let p2sh = false
|
||||
if (scriptType === btemplates.types.P2SH) {
|
||||
// We can remove this error later when we have a guarantee prepareInput
|
||||
// rejects unsignable scripts - it MUST be signable at this point.
|
||||
|
@ -503,7 +503,7 @@ TransactionBuilder.prototype.setVersion = function (version) {
|
|||
}
|
||||
|
||||
TransactionBuilder.fromTransaction = function (transaction, network) {
|
||||
var txb = new TransactionBuilder(network)
|
||||
const txb = new TransactionBuilder(network)
|
||||
|
||||
// Copy transaction fields
|
||||
txb.setVersion(transaction.version)
|
||||
|
@ -536,7 +536,7 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
|
|||
throw new Error('No, this would invalidate signatures')
|
||||
}
|
||||
|
||||
var value
|
||||
let value
|
||||
|
||||
// is it a hex string?
|
||||
if (typeof txHash === 'string') {
|
||||
|
@ -545,7 +545,7 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
|
|||
|
||||
// is it a Transaction object?
|
||||
} else if (txHash instanceof Transaction) {
|
||||
var txOut = txHash.outs[vout]
|
||||
const txOut = txHash.outs[vout]
|
||||
prevOutScript = txOut.script
|
||||
value = txOut.value
|
||||
|
||||
|
@ -564,10 +564,10 @@ TransactionBuilder.prototype.__addInputUnsafe = function (txHash, vout, options)
|
|||
throw new Error('coinbase inputs not supported')
|
||||
}
|
||||
|
||||
var prevTxOut = txHash.toString('hex') + ':' + vout
|
||||
const prevTxOut = txHash.toString('hex') + ':' + vout
|
||||
if (this.__prevTxSet[prevTxOut] !== undefined) throw new Error('Duplicate TxOut: ' + prevTxOut)
|
||||
|
||||
var input = {}
|
||||
let input = {}
|
||||
|
||||
// derive what we can from the scriptSig
|
||||
if (options.script !== undefined) {
|
||||
|
@ -581,10 +581,10 @@ TransactionBuilder.prototype.__addInputUnsafe = function (txHash, vout, options)
|
|||
|
||||
// derive what we can from the previous transactions output script
|
||||
if (!input.prevOutScript && options.prevOutScript) {
|
||||
var prevOutType
|
||||
let prevOutType
|
||||
|
||||
if (!input.pubKeys && !input.signatures) {
|
||||
var expanded = expandOutput(options.prevOutScript)
|
||||
const expanded = expandOutput(options.prevOutScript)
|
||||
|
||||
if (expanded.pubKeys) {
|
||||
input.pubKeys = expanded.pubKeys
|
||||
|
@ -598,7 +598,7 @@ TransactionBuilder.prototype.__addInputUnsafe = function (txHash, vout, options)
|
|||
input.prevOutType = prevOutType || btemplates.classifyOutput(options.prevOutScript)
|
||||
}
|
||||
|
||||
var vin = this.__tx.addInput(txHash, vout, options.sequence, options.scriptSig)
|
||||
const vin = this.__tx.addInput(txHash, vout, options.sequence, options.scriptSig)
|
||||
this.__inputs[vin] = input
|
||||
this.__prevTxSet[prevTxOut] = true
|
||||
return vin
|
||||
|
@ -630,12 +630,12 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
|
|||
if (!this.__tx.outs.length) throw new Error('Transaction has no outputs')
|
||||
}
|
||||
|
||||
var tx = this.__tx.clone()
|
||||
const tx = this.__tx.clone()
|
||||
// Create script signatures from inputs
|
||||
this.__inputs.forEach(function (input, i) {
|
||||
var scriptType = input.witnessScriptType || input.redeemScriptType || input.prevOutType
|
||||
const scriptType = input.witnessScriptType || input.redeemScriptType || input.prevOutType
|
||||
if (!scriptType && !allowIncomplete) throw new Error('Transaction is not complete')
|
||||
var result = buildInput(input, allowIncomplete)
|
||||
const result = buildInput(input, allowIncomplete)
|
||||
|
||||
// skip if no result
|
||||
if (!allowIncomplete) {
|
||||
|
@ -677,7 +677,7 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
|
|||
if (!this.__inputs[vin]) throw new Error('No input at index: ' + vin)
|
||||
hashType = hashType || Transaction.SIGHASH_ALL
|
||||
|
||||
var input = this.__inputs[vin]
|
||||
const input = this.__inputs[vin]
|
||||
|
||||
// if redeemScript was previously provided, enforce consistency
|
||||
if (input.redeemScript !== undefined &&
|
||||
|
@ -686,7 +686,7 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
|
|||
throw new Error('Inconsistent redeemScript')
|
||||
}
|
||||
|
||||
var kpPubKey = keyPair.publicKey || keyPair.getPublicKey()
|
||||
const kpPubKey = keyPair.publicKey || keyPair.getPublicKey()
|
||||
if (!canSign(input)) {
|
||||
if (witnessValue !== undefined) {
|
||||
if (input.value !== undefined && input.value !== witnessValue) throw new Error('Input didn\'t match witnessValue')
|
||||
|
@ -699,7 +699,7 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
|
|||
}
|
||||
|
||||
// ready to sign
|
||||
var signatureHash
|
||||
let signatureHash
|
||||
if (input.witness) {
|
||||
signatureHash = this.__tx.hashForWitnessV0(vin, input.signScript, input.value, hashType)
|
||||
} else {
|
||||
|
@ -707,7 +707,7 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
|
|||
}
|
||||
|
||||
// enforce in order signing of public keys
|
||||
var signed = input.pubKeys.some(function (pubKey, i) {
|
||||
const signed = input.pubKeys.some(function (pubKey, i) {
|
||||
if (!kpPubKey.equals(pubKey)) return false
|
||||
if (input.signatures[i]) throw new Error('Signature already exists')
|
||||
|
||||
|
@ -717,7 +717,7 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
|
|||
input.prevOutType === scriptTypes.P2WSH
|
||||
)) throw new Error('BIP143 rejects uncompressed public keys in P2WPKH or P2WSH')
|
||||
|
||||
let signature = keyPair.sign(signatureHash)
|
||||
const signature = keyPair.sign(signatureHash)
|
||||
input.signatures[i] = bscript.signature.encode(signature, hashType)
|
||||
return true
|
||||
})
|
||||
|
@ -736,7 +736,7 @@ TransactionBuilder.prototype.__canModifyInputs = function () {
|
|||
|
||||
return input.signatures.every(function (signature) {
|
||||
if (!signature) return true
|
||||
var hashType = signatureHashType(signature)
|
||||
const hashType = signatureHashType(signature)
|
||||
|
||||
// if SIGHASH_ANYONECANPAY is set, signatures would not
|
||||
// be invalidated by more inputs
|
||||
|
@ -746,17 +746,17 @@ TransactionBuilder.prototype.__canModifyInputs = function () {
|
|||
}
|
||||
|
||||
TransactionBuilder.prototype.__canModifyOutputs = function () {
|
||||
var nInputs = this.__tx.ins.length
|
||||
var nOutputs = this.__tx.outs.length
|
||||
const nInputs = this.__tx.ins.length
|
||||
const nOutputs = this.__tx.outs.length
|
||||
|
||||
return this.__inputs.every(function (input) {
|
||||
if (input.signatures === undefined) return true
|
||||
|
||||
return input.signatures.every(function (signature) {
|
||||
if (!signature) return true
|
||||
var hashType = signatureHashType(signature)
|
||||
const hashType = signatureHashType(signature)
|
||||
|
||||
var hashTypeMod = hashType & 0x1f
|
||||
const hashTypeMod = hashType & 0x1f
|
||||
if (hashTypeMod === Transaction.SIGHASH_NONE) return true
|
||||
if (hashTypeMod === Transaction.SIGHASH_SINGLE) {
|
||||
// if SIGHASH_SINGLE is set, and nInputs > nOutputs
|
||||
|
@ -770,13 +770,13 @@ TransactionBuilder.prototype.__canModifyOutputs = function () {
|
|||
|
||||
TransactionBuilder.prototype.__overMaximumFees = function (bytes) {
|
||||
// not all inputs will have .value defined
|
||||
var incoming = this.__inputs.reduce(function (a, x) { return a + (x.value >>> 0) }, 0)
|
||||
const incoming = this.__inputs.reduce(function (a, x) { return a + (x.value >>> 0) }, 0)
|
||||
|
||||
// but all outputs do, and if we have any input value
|
||||
// we can immediately determine if the outputs are too small
|
||||
var outgoing = this.__tx.outs.reduce(function (a, x) { return a + x.value }, 0)
|
||||
var fee = incoming - outgoing
|
||||
var feeRate = fee / bytes
|
||||
const outgoing = this.__tx.outs.reduce(function (a, x) { return a + x.value }, 0)
|
||||
const fee = incoming - outgoing
|
||||
const feeRate = fee / bytes
|
||||
|
||||
return feeRate > this.maximumFeeRate
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue