remove Script object
This commit is contained in:
parent
e743c58ed6
commit
e05f8a542d
11 changed files with 223 additions and 305 deletions
|
@ -20,10 +20,10 @@ function fromBase58Check (address) {
|
||||||
function fromOutputScript (script, network) {
|
function fromOutputScript (script, network) {
|
||||||
network = network || networks.bitcoin
|
network = network || networks.bitcoin
|
||||||
|
|
||||||
if (scripts.isPubKeyHashOutput(script)) return toBase58Check(Script.fromBuffer(script).chunks[2], network.pubKeyHash)
|
if (scripts.isPubKeyHashOutput(script)) return toBase58Check(script[2], network.pubKeyHash)
|
||||||
if (scripts.isScriptHashOutput(script)) return toBase58Check(Script.fromBuffer(script).chunks[1], network.scriptHash)
|
if (scripts.isScriptHashOutput(script)) return toBase58Check(script[1], network.scriptHash)
|
||||||
|
|
||||||
throw new Error(Script.fromBuffer(script).toASM() + ' has no matching Address')
|
throw new Error(Script.toASM(script) + ' has no matching Address')
|
||||||
}
|
}
|
||||||
|
|
||||||
function toBase58Check (hash, version) {
|
function toBase58Check (hash, version) {
|
||||||
|
|
140
src/script.js
140
src/script.js
|
@ -1,17 +1,30 @@
|
||||||
var bufferutils = require('./bufferutils')
|
var bufferutils = require('./bufferutils')
|
||||||
var crypto = require('./crypto')
|
var opcodes = require('./opcodes')
|
||||||
var typeforce = require('typeforce')
|
var typeforce = require('typeforce')
|
||||||
var types = require('./types')
|
var types = require('./types')
|
||||||
var opcodes = require('./opcodes')
|
|
||||||
|
|
||||||
function Script (buffer, chunks) {
|
function coerceChunks (chunks) {
|
||||||
typeforce(types.tuple(types.Buffer, types.Array), arguments)
|
return types.Array(chunks) ? chunks : decompile(chunks)
|
||||||
|
|
||||||
this.buffer = buffer
|
|
||||||
this.chunks = chunks
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Script.fromASM = function (asm) {
|
function toASM (chunks) {
|
||||||
|
chunks = coerceChunks(chunks)
|
||||||
|
|
||||||
|
return chunks.map(function (chunk) {
|
||||||
|
// data chunk
|
||||||
|
if (Buffer.isBuffer(chunk)) {
|
||||||
|
return chunk.toString('hex')
|
||||||
|
|
||||||
|
// opcode
|
||||||
|
} else {
|
||||||
|
return reverseOps[chunk]
|
||||||
|
}
|
||||||
|
}).join(' ')
|
||||||
|
}
|
||||||
|
|
||||||
|
function fromASM (asm) {
|
||||||
|
typeforce(types.String, asm)
|
||||||
|
|
||||||
var strChunks = asm.split(' ')
|
var strChunks = asm.split(' ')
|
||||||
var chunks = strChunks.map(function (strChunk) {
|
var chunks = strChunks.map(function (strChunk) {
|
||||||
// opcode
|
// opcode
|
||||||
|
@ -24,44 +37,10 @@ Script.fromASM = function (asm) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return Script.fromChunks(chunks)
|
return chunks
|
||||||
}
|
}
|
||||||
|
|
||||||
Script.fromBuffer = function (buffer) {
|
function compile (chunks) {
|
||||||
var chunks = []
|
|
||||||
var i = 0
|
|
||||||
|
|
||||||
while (i < buffer.length) {
|
|
||||||
var opcode = buffer.readUInt8(i)
|
|
||||||
|
|
||||||
// data chunk
|
|
||||||
if ((opcode > opcodes.OP_0) && (opcode <= opcodes.OP_PUSHDATA4)) {
|
|
||||||
var d = bufferutils.readPushDataInt(buffer, i)
|
|
||||||
|
|
||||||
// did reading a pushDataInt fail? return non-chunked script
|
|
||||||
if (d === null) return new Script(buffer, [])
|
|
||||||
i += d.size
|
|
||||||
|
|
||||||
// attempt to read too much data?
|
|
||||||
if (i + d.number > buffer.length) return new Script(buffer, [])
|
|
||||||
|
|
||||||
var data = buffer.slice(i, i + d.number)
|
|
||||||
i += d.number
|
|
||||||
|
|
||||||
chunks.push(data)
|
|
||||||
|
|
||||||
// opcode
|
|
||||||
} else {
|
|
||||||
chunks.push(opcode)
|
|
||||||
|
|
||||||
i += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Script(buffer, chunks)
|
|
||||||
}
|
|
||||||
|
|
||||||
Script.fromChunks = function (chunks) {
|
|
||||||
typeforce(types.Array, chunks)
|
typeforce(types.Array, chunks)
|
||||||
|
|
||||||
var bufferSize = chunks.reduce(function (accum, chunk) {
|
var bufferSize = chunks.reduce(function (accum, chunk) {
|
||||||
|
@ -93,28 +72,43 @@ Script.fromChunks = function (chunks) {
|
||||||
})
|
})
|
||||||
|
|
||||||
if (offset !== buffer.length) throw new Error('Could not decode chunks')
|
if (offset !== buffer.length) throw new Error('Could not decode chunks')
|
||||||
return new Script(buffer, chunks)
|
return buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
Script.fromHex = function (hex) {
|
function decompile (buffer) {
|
||||||
return Script.fromBuffer(new Buffer(hex, 'hex'))
|
typeforce(types.Buffer, buffer)
|
||||||
}
|
|
||||||
|
|
||||||
Script.EMPTY = Script.fromChunks([])
|
var chunks = []
|
||||||
|
var i = 0
|
||||||
|
|
||||||
Script.prototype.equals = function (script) {
|
while (i < buffer.length) {
|
||||||
return bufferutils.equal(this.buffer, script.buffer)
|
var opcode = buffer.readUInt8(i)
|
||||||
}
|
|
||||||
|
|
||||||
Script.prototype.getHash = function () {
|
// data chunk
|
||||||
return crypto.hash160(this.buffer)
|
if ((opcode > opcodes.OP_0) && (opcode <= opcodes.OP_PUSHDATA4)) {
|
||||||
}
|
var d = bufferutils.readPushDataInt(buffer, i)
|
||||||
|
|
||||||
// FIXME: doesn't work for data chunks, maybe time to use buffertools.compare...
|
// did reading a pushDataInt fail? empty script
|
||||||
Script.prototype.without = function (needle) {
|
if (d === null) return []
|
||||||
return Script.fromChunks(this.chunks.filter(function (op) {
|
i += d.size
|
||||||
return op !== needle
|
|
||||||
}))
|
// attempt to read too much data? empty script
|
||||||
|
if (i + d.number > buffer.length) return []
|
||||||
|
|
||||||
|
var data = buffer.slice(i, i + d.number)
|
||||||
|
i += d.number
|
||||||
|
|
||||||
|
chunks.push(data)
|
||||||
|
|
||||||
|
// opcode
|
||||||
|
} else {
|
||||||
|
chunks.push(opcode)
|
||||||
|
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return chunks
|
||||||
}
|
}
|
||||||
|
|
||||||
var reverseOps = []
|
var reverseOps = []
|
||||||
|
@ -123,25 +117,9 @@ for (var op in opcodes) {
|
||||||
reverseOps[code] = op
|
reverseOps[code] = op
|
||||||
}
|
}
|
||||||
|
|
||||||
Script.prototype.toASM = function () {
|
module.exports = {
|
||||||
return this.chunks.map(function (chunk) {
|
compile: compile,
|
||||||
// data chunk
|
decompile: decompile,
|
||||||
if (Buffer.isBuffer(chunk)) {
|
toASM: toASM,
|
||||||
return chunk.toString('hex')
|
fromASM: fromASM
|
||||||
|
|
||||||
// opcode
|
|
||||||
} else {
|
|
||||||
return reverseOps[chunk]
|
|
||||||
}
|
|
||||||
}).join(' ')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Script.prototype.toBuffer = function () {
|
|
||||||
return this.buffer
|
|
||||||
}
|
|
||||||
|
|
||||||
Script.prototype.toHex = function () {
|
|
||||||
return this.toBuffer().toString('hex')
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = Script
|
|
||||||
|
|
217
src/scripts.js
217
src/scripts.js
|
@ -40,105 +40,102 @@ function isCanonicalSignature (buffer) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
function isPubKeyHashInput (script) {
|
function coerceBuffer (buffer) {
|
||||||
typeforce(types.Buffer, script)
|
return types.Buffer(buffer) ? buffer : Script.compile(buffer)
|
||||||
script = Script.fromBuffer(script)
|
|
||||||
|
|
||||||
return script.chunks.length === 2 &&
|
|
||||||
isCanonicalSignature(script.chunks[0]) &&
|
|
||||||
isCanonicalPubKey(script.chunks[1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isPubKeyHashOutput (script) {
|
function coerceChunks (chunks) {
|
||||||
typeforce(types.Buffer, script)
|
return types.Array(chunks) ? chunks : Script.decompile(chunks)
|
||||||
script = Script.fromBuffer(script)
|
|
||||||
|
|
||||||
return script.chunks.length === 5 &&
|
|
||||||
script.chunks[0] === ops.OP_DUP &&
|
|
||||||
script.chunks[1] === ops.OP_HASH160 &&
|
|
||||||
Buffer.isBuffer(script.chunks[2]) &&
|
|
||||||
script.chunks[2].length === 20 &&
|
|
||||||
script.chunks[3] === ops.OP_EQUALVERIFY &&
|
|
||||||
script.chunks[4] === ops.OP_CHECKSIG
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isPubKeyInput (script) {
|
function isPubKeyHashInput (chunks) {
|
||||||
typeforce(types.Buffer, script)
|
chunks = coerceChunks(chunks)
|
||||||
script = Script.fromBuffer(script)
|
|
||||||
|
|
||||||
return script.chunks.length === 1 &&
|
return chunks.length === 2 &&
|
||||||
isCanonicalSignature(script.chunks[0])
|
isCanonicalSignature(chunks[0]) &&
|
||||||
|
isCanonicalPubKey(chunks[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
function isPubKeyOutput (script) {
|
function isPubKeyHashOutput (chunks) {
|
||||||
typeforce(types.Buffer, script)
|
chunks = coerceChunks(chunks)
|
||||||
script = Script.fromBuffer(script)
|
|
||||||
|
|
||||||
return script.chunks.length === 2 &&
|
return chunks.length === 5 &&
|
||||||
isCanonicalPubKey(script.chunks[0]) &&
|
chunks[0] === ops.OP_DUP &&
|
||||||
script.chunks[1] === ops.OP_CHECKSIG
|
chunks[1] === ops.OP_HASH160 &&
|
||||||
|
Buffer.isBuffer(chunks[2]) &&
|
||||||
|
chunks[2].length === 20 &&
|
||||||
|
chunks[3] === ops.OP_EQUALVERIFY &&
|
||||||
|
chunks[4] === ops.OP_CHECKSIG
|
||||||
}
|
}
|
||||||
|
|
||||||
function isScriptHashInput (script, allowIncomplete) {
|
function isPubKeyInput (chunks) {
|
||||||
typeforce(types.Buffer, script)
|
chunks = coerceChunks(chunks)
|
||||||
script = Script.fromBuffer(script)
|
|
||||||
|
|
||||||
if (script.chunks.length < 2) return false
|
return chunks.length === 1 &&
|
||||||
|
isCanonicalSignature(chunks[0])
|
||||||
|
}
|
||||||
|
|
||||||
var lastChunk = script.chunks[script.chunks.length - 1]
|
function isPubKeyOutput (chunks) {
|
||||||
|
chunks = coerceChunks(chunks)
|
||||||
|
|
||||||
|
return chunks.length === 2 &&
|
||||||
|
isCanonicalPubKey(chunks[0]) &&
|
||||||
|
chunks[1] === ops.OP_CHECKSIG
|
||||||
|
}
|
||||||
|
|
||||||
|
function isScriptHashInput (chunks, allowIncomplete) {
|
||||||
|
chunks = coerceChunks(chunks)
|
||||||
|
if (chunks.length < 2) return false
|
||||||
|
|
||||||
|
var lastChunk = chunks[chunks.length - 1]
|
||||||
if (!Buffer.isBuffer(lastChunk)) return false
|
if (!Buffer.isBuffer(lastChunk)) return false
|
||||||
|
|
||||||
var scriptSig = Script.fromChunks(script.chunks.slice(0, -1)).buffer
|
var scriptSigChunks = chunks.slice(0, -1)
|
||||||
var redeemScript = lastChunk
|
var redeemScriptChunks = Script.decompile(lastChunk)
|
||||||
|
|
||||||
// is redeemScript a valid script?
|
// is redeemScript a valid script?
|
||||||
if (Script.fromBuffer(redeemScript).chunks.length === 0) return false
|
if (redeemScriptChunks.length === 0) return false
|
||||||
|
|
||||||
return classifyInput(scriptSig, allowIncomplete) === classifyOutput(redeemScript)
|
return classifyInput(scriptSigChunks, allowIncomplete) === classifyOutput(redeemScriptChunks)
|
||||||
}
|
}
|
||||||
|
|
||||||
function isScriptHashOutput (script) {
|
function isScriptHashOutput (chunks) {
|
||||||
typeforce(types.Buffer, script)
|
chunks = coerceChunks(chunks)
|
||||||
script = Script.fromBuffer(script)
|
|
||||||
|
|
||||||
return script.chunks.length === 3 &&
|
return chunks.length === 3 &&
|
||||||
script.chunks[0] === ops.OP_HASH160 &&
|
chunks[0] === ops.OP_HASH160 &&
|
||||||
Buffer.isBuffer(script.chunks[1]) &&
|
Buffer.isBuffer(chunks[1]) &&
|
||||||
script.chunks[1].length === 20 &&
|
chunks[1].length === 20 &&
|
||||||
script.chunks[2] === ops.OP_EQUAL
|
chunks[2] === ops.OP_EQUAL
|
||||||
}
|
}
|
||||||
|
|
||||||
// allowIncomplete is to account for combining signatures
|
// allowIncomplete is to account for combining signatures
|
||||||
// See https://github.com/bitcoin/bitcoin/blob/f425050546644a36b0b8e0eb2f6934a3e0f6f80f/src/script/sign.cpp#L195-L197
|
// See https://github.com/bitcoin/bitcoin/blob/f425050546644a36b0b8e0eb2f6934a3e0f6f80f/src/script/sign.cpp#L195-L197
|
||||||
function isMultisigInput (script, allowIncomplete) {
|
function isMultisigInput (chunks, allowIncomplete) {
|
||||||
typeforce(types.Buffer, script)
|
chunks = coerceChunks(chunks)
|
||||||
script = Script.fromBuffer(script)
|
if (chunks.length < 2) return false
|
||||||
|
if (chunks[0] !== ops.OP_0) return false
|
||||||
if (script.chunks.length < 2) return false
|
|
||||||
if (script.chunks[0] !== ops.OP_0) return false
|
|
||||||
|
|
||||||
if (allowIncomplete) {
|
if (allowIncomplete) {
|
||||||
return script.chunks.slice(1).every(function (chunk) {
|
return chunks.slice(1).every(function (chunk) {
|
||||||
return chunk === ops.OP_0 || isCanonicalSignature(chunk)
|
return chunk === ops.OP_0 || isCanonicalSignature(chunk)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return script.chunks.slice(1).every(isCanonicalSignature)
|
return chunks.slice(1).every(isCanonicalSignature)
|
||||||
}
|
}
|
||||||
|
|
||||||
function isMultisigOutput (script) {
|
function isMultisigOutput (chunks) {
|
||||||
typeforce(types.Buffer, script)
|
chunks = coerceChunks(chunks)
|
||||||
script = Script.fromBuffer(script)
|
if (chunks.length < 4) return false
|
||||||
|
if (chunks[chunks.length - 1] !== ops.OP_CHECKMULTISIG) return false
|
||||||
|
|
||||||
if (script.chunks.length < 4) return false
|
var mOp = chunks[0]
|
||||||
if (script.chunks[script.chunks.length - 1] !== ops.OP_CHECKMULTISIG) return false
|
|
||||||
|
|
||||||
var mOp = script.chunks[0]
|
|
||||||
if (mOp === ops.OP_0) return false
|
if (mOp === ops.OP_0) return false
|
||||||
if (mOp < ops.OP_1) return false
|
if (mOp < ops.OP_1) return false
|
||||||
if (mOp > ops.OP_16) return false
|
if (mOp > ops.OP_16) return false
|
||||||
|
|
||||||
var nOp = script.chunks[script.chunks.length - 2]
|
var nOp = chunks[chunks.length - 2]
|
||||||
if (nOp === ops.OP_0) return false
|
if (nOp === ops.OP_0) return false
|
||||||
if (nOp < ops.OP_1) return false
|
if (nOp < ops.OP_1) return false
|
||||||
if (nOp > ops.OP_16) return false
|
if (nOp > ops.OP_16) return false
|
||||||
|
@ -147,47 +144,43 @@ function isMultisigOutput (script) {
|
||||||
var n = nOp - (ops.OP_1 - 1)
|
var n = nOp - (ops.OP_1 - 1)
|
||||||
if (n < m) return false
|
if (n < m) return false
|
||||||
|
|
||||||
var pubKeys = script.chunks.slice(1, -2)
|
var pubKeys = chunks.slice(1, -2)
|
||||||
if (n < pubKeys.length) return false
|
if (n < pubKeys.length) return false
|
||||||
|
|
||||||
return pubKeys.every(isCanonicalPubKey)
|
return pubKeys.every(isCanonicalPubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNullDataOutput (script) {
|
function isNullDataOutput (chunks) {
|
||||||
typeforce(types.Buffer, script)
|
chunks = coerceChunks(chunks)
|
||||||
script = Script.fromBuffer(script)
|
return chunks[0] === ops.OP_RETURN
|
||||||
|
|
||||||
return script.chunks[0] === ops.OP_RETURN
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function classifyOutput (script) {
|
function classifyOutput (chunks) {
|
||||||
typeforce(types.Buffer, script)
|
chunks = coerceChunks(chunks)
|
||||||
|
if (isPubKeyHashOutput(chunks)) {
|
||||||
if (isPubKeyHashOutput(script)) {
|
|
||||||
return 'pubkeyhash'
|
return 'pubkeyhash'
|
||||||
} else if (isScriptHashOutput(script)) {
|
} else if (isScriptHashOutput(chunks)) {
|
||||||
return 'scripthash'
|
return 'scripthash'
|
||||||
} else if (isMultisigOutput(script)) {
|
} else if (isMultisigOutput(chunks)) {
|
||||||
return 'multisig'
|
return 'multisig'
|
||||||
} else if (isPubKeyOutput(script)) {
|
} else if (isPubKeyOutput(chunks)) {
|
||||||
return 'pubkey'
|
return 'pubkey'
|
||||||
} else if (isNullDataOutput(script)) {
|
} else if (isNullDataOutput(chunks)) {
|
||||||
return 'nulldata'
|
return 'nulldata'
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'nonstandard'
|
return 'nonstandard'
|
||||||
}
|
}
|
||||||
|
|
||||||
function classifyInput (script, allowIncomplete) {
|
function classifyInput (chunks, allowIncomplete) {
|
||||||
typeforce(types.Buffer, script)
|
chunks = coerceChunks(chunks)
|
||||||
|
if (isPubKeyHashInput(chunks)) {
|
||||||
if (isPubKeyHashInput(script)) {
|
|
||||||
return 'pubkeyhash'
|
return 'pubkeyhash'
|
||||||
} else if (isMultisigInput(script, allowIncomplete)) {
|
} else if (isMultisigInput(chunks, allowIncomplete)) {
|
||||||
return 'multisig'
|
return 'multisig'
|
||||||
} else if (isScriptHashInput(script, allowIncomplete)) {
|
} else if (isScriptHashInput(chunks, allowIncomplete)) {
|
||||||
return 'scripthash'
|
return 'scripthash'
|
||||||
} else if (isPubKeyInput(script)) {
|
} else if (isPubKeyInput(chunks)) {
|
||||||
return 'pubkey'
|
return 'pubkey'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,34 +190,21 @@ function classifyInput (script, allowIncomplete) {
|
||||||
// Standard Script Templates
|
// Standard Script Templates
|
||||||
// {pubKey} OP_CHECKSIG
|
// {pubKey} OP_CHECKSIG
|
||||||
function pubKeyOutput (pubKey) {
|
function pubKeyOutput (pubKey) {
|
||||||
return Script.fromChunks([
|
return Script.compile([pubKey, ops.OP_CHECKSIG])
|
||||||
pubKey,
|
|
||||||
ops.OP_CHECKSIG
|
|
||||||
]).buffer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
|
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
|
||||||
function pubKeyHashOutput (hash) {
|
function pubKeyHashOutput (pubKeyHash) {
|
||||||
typeforce(types.Hash160bit, hash)
|
typeforce(typeforce.Buffer, pubKeyHash)
|
||||||
|
|
||||||
return Script.fromChunks([
|
return Script.compile([ops.OP_DUP, ops.OP_HASH160, pubKeyHash, ops.OP_EQUALVERIFY, ops.OP_CHECKSIG])
|
||||||
ops.OP_DUP,
|
|
||||||
ops.OP_HASH160,
|
|
||||||
hash,
|
|
||||||
ops.OP_EQUALVERIFY,
|
|
||||||
ops.OP_CHECKSIG
|
|
||||||
]).buffer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// OP_HASH160 {scriptHash} OP_EQUAL
|
// OP_HASH160 {scriptHash} OP_EQUAL
|
||||||
function scriptHashOutput (hash) {
|
function scriptHashOutput (scriptHash) {
|
||||||
typeforce(types.Hash160bit, hash)
|
typeforce(typeforce.Buffer, scriptHash)
|
||||||
|
|
||||||
return Script.fromChunks([
|
return Script.compile([ops.OP_HASH160, scriptHash, ops.OP_EQUAL])
|
||||||
ops.OP_HASH160,
|
|
||||||
hash,
|
|
||||||
ops.OP_EQUAL
|
|
||||||
]).buffer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// m [pubKeys ...] n OP_CHECKMULTISIG
|
// m [pubKeys ...] n OP_CHECKMULTISIG
|
||||||
|
@ -234,46 +214,47 @@ function multisigOutput (m, pubKeys) {
|
||||||
var n = pubKeys.length
|
var n = pubKeys.length
|
||||||
if (n < m) throw new Error('Not enough pubKeys provided')
|
if (n < m) throw new Error('Not enough pubKeys provided')
|
||||||
|
|
||||||
return Script.fromChunks([].concat(
|
return Script.compile([].concat(
|
||||||
(ops.OP_1 - 1) + m,
|
(ops.OP_1 - 1) + m,
|
||||||
pubKeys,
|
pubKeys,
|
||||||
(ops.OP_1 - 1) + n,
|
(ops.OP_1 - 1) + n,
|
||||||
ops.OP_CHECKMULTISIG
|
ops.OP_CHECKMULTISIG
|
||||||
)).buffer
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
// {signature}
|
// {signature}
|
||||||
function pubKeyInput (signature) {
|
function pubKeyInput (signature) {
|
||||||
typeforce(types.Buffer, signature)
|
typeforce(types.Buffer, signature)
|
||||||
|
|
||||||
return Script.fromChunks([signature]).buffer
|
return Script.compile([signature])
|
||||||
}
|
}
|
||||||
|
|
||||||
// {signature} {pubKey}
|
// {signature} {pubKey}
|
||||||
function pubKeyHashInput (signature, pubKey) {
|
function pubKeyHashInput (signature, pubKey) {
|
||||||
typeforce(types.tuple(types.Buffer, types.Buffer), arguments)
|
typeforce(types.tuple(types.Buffer, types.Buffer), arguments)
|
||||||
|
|
||||||
return Script.fromChunks([signature, pubKey]).buffer
|
return Script.compile([signature, pubKey])
|
||||||
}
|
}
|
||||||
|
|
||||||
// <scriptSig> {serialized scriptPubKey script}
|
// <scriptSig> {serialized scriptPubKey script}
|
||||||
function scriptHashInput (scriptSig, scriptPubKey) {
|
function scriptHashInput (scriptSig, scriptPubKeyBuffer) {
|
||||||
var scriptSigChunks = Script.fromBuffer(scriptSig).chunks
|
scriptSig = coerceChunks(scriptSig)
|
||||||
|
scriptPubKeyBuffer = coerceBuffer(scriptPubKeyBuffer)
|
||||||
|
|
||||||
return Script.fromChunks([].concat(
|
return Script.compile([].concat(
|
||||||
scriptSigChunks,
|
scriptSig,
|
||||||
scriptPubKey
|
scriptPubKeyBuffer
|
||||||
)).buffer
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
// OP_0 [signatures ...]
|
// OP_0 [signatures ...]
|
||||||
function multisigInput (signatures, scriptPubKey) {
|
function multisigInput (signatures, scriptPubKey) {
|
||||||
if (scriptPubKey) {
|
if (scriptPubKey) {
|
||||||
if (!isMultisigOutput(scriptPubKey)) throw new Error('Expected multisig scriptPubKey')
|
if (!isMultisigOutput(scriptPubKey)) throw new Error('Expected multisig scriptPubKey')
|
||||||
scriptPubKey = Script.fromBuffer(scriptPubKey)
|
scriptPubKey = coerceChunks(scriptPubKey)
|
||||||
|
|
||||||
var mOp = scriptPubKey.chunks[0]
|
var mOp = scriptPubKey[0]
|
||||||
var nOp = scriptPubKey.chunks[scriptPubKey.chunks.length - 2]
|
var nOp = scriptPubKey[scriptPubKey.length - 2]
|
||||||
var m = mOp - (ops.OP_1 - 1)
|
var m = mOp - (ops.OP_1 - 1)
|
||||||
var n = nOp - (ops.OP_1 - 1)
|
var n = nOp - (ops.OP_1 - 1)
|
||||||
|
|
||||||
|
@ -281,11 +262,11 @@ function multisigInput (signatures, scriptPubKey) {
|
||||||
if (signatures.length > n) throw new Error('Too many signatures provided')
|
if (signatures.length > n) throw new Error('Too many signatures provided')
|
||||||
}
|
}
|
||||||
|
|
||||||
return Script.fromChunks([].concat(ops.OP_0, signatures)).buffer
|
return Script.compile([].concat(ops.OP_0, signatures))
|
||||||
}
|
}
|
||||||
|
|
||||||
function nullDataOutput (data) {
|
function nullDataOutput (data) {
|
||||||
return Script.fromChunks([ops.OP_RETURN, data]).buffer
|
return Script.compile([ops.OP_RETURN, data])
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -193,7 +193,9 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
|
||||||
|
|
||||||
// in case concatenating two scripts ends up with two codeseparators,
|
// in case concatenating two scripts ends up with two codeseparators,
|
||||||
// or an extra one at the end, this prevents all those possible incompatibilities.
|
// or an extra one at the end, this prevents all those possible incompatibilities.
|
||||||
var hashScript = Script.fromBuffer(prevOutScript).without(opcodes.OP_CODESEPARATOR).buffer
|
var hashScript = Script.compile(Script.decompile(prevOutScript).filter(function (x) {
|
||||||
|
return x !== opcodes.OP_CODESEPARATOR
|
||||||
|
}))
|
||||||
var i
|
var i
|
||||||
|
|
||||||
// blank out other inputs' signatures
|
// blank out other inputs' signatures
|
||||||
|
|
|
@ -13,7 +13,7 @@ var Transaction = require('./transaction')
|
||||||
function extractInput (txIn) {
|
function extractInput (txIn) {
|
||||||
var redeemScript
|
var redeemScript
|
||||||
var scriptSig = txIn.script
|
var scriptSig = txIn.script
|
||||||
var scriptSigChunks = Script.fromBuffer(scriptSig).chunks
|
var scriptSigChunks = Script.decompile(scriptSig)
|
||||||
|
|
||||||
var prevOutScript
|
var prevOutScript
|
||||||
var prevOutType = scripts.classifyInput(scriptSig, true)
|
var prevOutType = scripts.classifyInput(scriptSig, true)
|
||||||
|
@ -24,7 +24,7 @@ function extractInput (txIn) {
|
||||||
redeemScript = scriptSigChunks.slice(-1)[0]
|
redeemScript = scriptSigChunks.slice(-1)[0]
|
||||||
prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript))
|
prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript))
|
||||||
|
|
||||||
scriptSig = Script.fromChunks(scriptSigChunks.slice(0, -1)).buffer
|
scriptSig = Script.compile(scriptSigChunks.slice(0, -1))
|
||||||
scriptSigChunks = scriptSigChunks.slice(0, -1)
|
scriptSigChunks = scriptSigChunks.slice(0, -1)
|
||||||
|
|
||||||
scriptType = scripts.classifyInput(scriptSig, true)
|
scriptType = scripts.classifyInput(scriptSig, true)
|
||||||
|
@ -51,7 +51,7 @@ function extractInput (txIn) {
|
||||||
signatures = [parsed.signature]
|
signatures = [parsed.signature]
|
||||||
|
|
||||||
if (redeemScript) {
|
if (redeemScript) {
|
||||||
pubKeys = Script.fromBuffer(redeemScript).chunks.slice(0, 1)
|
pubKeys = Script.decompile(redeemScript).slice(0, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
break
|
break
|
||||||
|
@ -67,7 +67,7 @@ function extractInput (txIn) {
|
||||||
})
|
})
|
||||||
|
|
||||||
if (redeemScript) {
|
if (redeemScript) {
|
||||||
pubKeys = Script.fromBuffer(redeemScript).chunks.slice(1, -2)
|
pubKeys = Script.decompile(redeemScript).slice(1, -2)
|
||||||
}
|
}
|
||||||
|
|
||||||
break
|
break
|
||||||
|
@ -142,8 +142,8 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
|
||||||
|
|
||||||
var input = {}
|
var input = {}
|
||||||
if (prevOutScript) {
|
if (prevOutScript) {
|
||||||
var prevOutType = scripts.classifyOutput(prevOutScript)
|
var prevOutScriptChunks = Script.decompile(prevOutScript)
|
||||||
var prevOutScriptChunks = Script.fromBuffer(prevOutScript).chunks
|
var prevOutType = scripts.classifyOutput(prevOutScriptChunks)
|
||||||
|
|
||||||
// if we can, extract pubKey information
|
// if we can, extract pubKey information
|
||||||
switch (prevOutType) {
|
switch (prevOutType) {
|
||||||
|
@ -313,14 +313,14 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
|
||||||
if (input.prevOutScript) {
|
if (input.prevOutScript) {
|
||||||
if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH')
|
if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH')
|
||||||
|
|
||||||
var scriptHash = Script.fromBuffer(input.prevOutScript).chunks[1]
|
var scriptHash = Script.decompile(input.prevOutScript)[1]
|
||||||
if (!bufferutils.equal(scriptHash, bcrypto.hash160(redeemScript))) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex'))
|
if (!bufferutils.equal(scriptHash, bcrypto.hash160(redeemScript))) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex'))
|
||||||
}
|
}
|
||||||
|
|
||||||
var scriptType = scripts.classifyOutput(redeemScript)
|
var scriptType = scripts.classifyOutput(redeemScript)
|
||||||
if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')')
|
if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')')
|
||||||
|
|
||||||
var redeemScriptChunks = Script.fromBuffer(redeemScript).chunks
|
var redeemScriptChunks = Script.decompile(redeemScript)
|
||||||
var pubKeys = []
|
var pubKeys = []
|
||||||
switch (scriptType) {
|
switch (scriptType) {
|
||||||
case 'multisig':
|
case 'multisig':
|
||||||
|
|
|
@ -31,7 +31,7 @@ describe('Address', function () {
|
||||||
describe('fromOutputScript', function () {
|
describe('fromOutputScript', function () {
|
||||||
fixtures.valid.forEach(function (f) {
|
fixtures.valid.forEach(function (f) {
|
||||||
it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
|
it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
|
||||||
var script = Script.fromASM(f.script).buffer
|
var script = Script.fromASM(f.script)
|
||||||
var address = Address.fromOutputScript(script, networks[f.network])
|
var address = Address.fromOutputScript(script, networks[f.network])
|
||||||
|
|
||||||
assert.strictEqual(address, f.base58check)
|
assert.strictEqual(address, f.base58check)
|
||||||
|
@ -40,7 +40,7 @@ describe('Address', function () {
|
||||||
|
|
||||||
fixtures.invalid.fromOutputScript.forEach(function (f) {
|
fixtures.invalid.fromOutputScript.forEach(function (f) {
|
||||||
it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () {
|
it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () {
|
||||||
var script = Script.fromASM(f.script).buffer
|
var script = Script.fromASM(f.script)
|
||||||
|
|
||||||
assert.throws(function () {
|
assert.throws(function () {
|
||||||
Address.fromOutputScript(script)
|
Address.fromOutputScript(script)
|
||||||
|
@ -66,7 +66,7 @@ describe('Address', function () {
|
||||||
it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
|
it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
|
||||||
var script = Address.toOutputScript(f.base58check, network)
|
var script = Address.toOutputScript(f.base58check, network)
|
||||||
|
|
||||||
assert.strictEqual(Script.fromBuffer(script).toASM(), f.script)
|
assert.strictEqual(Script.toASM(script), f.script)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -227,8 +227,9 @@ describe('Bitcoin-core', function () {
|
||||||
var transaction = Transaction.fromHex(txHex)
|
var transaction = Transaction.fromHex(txHex)
|
||||||
assert.strictEqual(transaction.toHex(), txHex)
|
assert.strictEqual(transaction.toHex(), txHex)
|
||||||
|
|
||||||
var script = Script.fromHex(scriptHex).buffer
|
var script = new Buffer(scriptHex, 'hex')
|
||||||
assert.strictEqual(Script.fromBuffer(script).toHex(), scriptHex)
|
var scriptChunks = Script.decompile(script)
|
||||||
|
assert.strictEqual(Script.compile(scriptChunks).toString('hex'), scriptHex)
|
||||||
|
|
||||||
var hash = transaction.hashForSignature(inIndex, script, hashType)
|
var hash = transaction.hashForSignature(inIndex, script, hashType)
|
||||||
assert.deepEqual(hash, expectedHash)
|
assert.deepEqual(hash, expectedHash)
|
||||||
|
|
|
@ -2,30 +2,12 @@
|
||||||
/* eslint-disable no-new */
|
/* eslint-disable no-new */
|
||||||
|
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var opcodes = require('../src/opcodes')
|
|
||||||
|
|
||||||
var Script = require('../src/script')
|
var Script = require('../src/script')
|
||||||
|
var OPS = require('../src/opcodes')
|
||||||
|
|
||||||
var fixtures = require('./fixtures/script.json')
|
var fixtures = require('./fixtures/script.json')
|
||||||
|
|
||||||
describe('Script', function () {
|
describe('Script', function () {
|
||||||
describe('constructor', function () {
|
|
||||||
it('accepts valid parameters', function () {
|
|
||||||
var buffer = new Buffer([1])
|
|
||||||
var chunks = [1]
|
|
||||||
var script = new Script(buffer, chunks)
|
|
||||||
|
|
||||||
assert.strictEqual(script.buffer, buffer)
|
|
||||||
assert.strictEqual(script.chunks, chunks)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws an error when input is not an array', function () {
|
|
||||||
assert.throws(function () {
|
|
||||||
new Script({})
|
|
||||||
}, /Expected Buffer, got/)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('fromASM/toASM', function () {
|
describe('fromASM/toASM', function () {
|
||||||
fixtures.valid.forEach(function (f) {
|
fixtures.valid.forEach(function (f) {
|
||||||
if (!f.asm) return
|
if (!f.asm) return
|
||||||
|
@ -33,53 +15,23 @@ describe('Script', function () {
|
||||||
it('decodes/encodes ' + f.description, function () {
|
it('decodes/encodes ' + f.description, function () {
|
||||||
var script = Script.fromASM(f.asm)
|
var script = Script.fromASM(f.asm)
|
||||||
|
|
||||||
assert.strictEqual(script.toASM(), f.asm)
|
assert.strictEqual(Script.toASM(script), f.asm)
|
||||||
assert.strictEqual(script.buffer.toString('hex'), f.hex)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('fromHex/toHex', function () {
|
describe('compile', function () {
|
||||||
fixtures.valid.forEach(function (f) {
|
|
||||||
it('decodes/encodes ' + f.description, function () {
|
|
||||||
var script = Script.fromHex(f.hex)
|
|
||||||
|
|
||||||
assert.strictEqual(script.toASM(), f.asm)
|
|
||||||
assert.strictEqual(script.buffer.toString('hex'), f.hex)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('fromChunks', function () {
|
|
||||||
it('should match expected behaviour', function () {
|
it('should match expected behaviour', function () {
|
||||||
var hash = new Buffer(32)
|
var hash = new Buffer(32)
|
||||||
hash.fill(0)
|
hash.fill(0)
|
||||||
|
|
||||||
var script = Script.fromChunks([
|
var script = Script.compile([
|
||||||
opcodes.OP_HASH160,
|
OPS.OP_HASH160,
|
||||||
hash,
|
hash,
|
||||||
opcodes.OP_EQUAL
|
OPS.OP_EQUAL
|
||||||
])
|
])
|
||||||
|
|
||||||
assert.strictEqual(script.buffer.toString('hex'), 'a920000000000000000000000000000000000000000000000000000000000000000087')
|
assert.strictEqual(script.toString('hex'), 'a920000000000000000000000000000000000000000000000000000000000000000087')
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('without', function () {
|
|
||||||
var hex = 'a914e8c300c87986efa94c37c0519929019ef86eb5b487'
|
|
||||||
var script = Script.fromHex(hex)
|
|
||||||
|
|
||||||
it('should return a script without the given value', function () {
|
|
||||||
var subScript = script.without(opcodes.OP_HASH160)
|
|
||||||
|
|
||||||
assert.strictEqual(subScript.buffer.toString('hex'), '14e8c300c87986efa94c37c0519929019ef86eb5b487')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('shouldnt mutate the original script', function () {
|
|
||||||
var subScript = script.without(opcodes.OP_EQUAL)
|
|
||||||
|
|
||||||
assert.notEqual(subScript.buffer.toString('hex'), hex)
|
|
||||||
assert.strictEqual(script.buffer.toString('hex'), hex)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -19,7 +19,7 @@ describe('Scripts', function () {
|
||||||
if (!f.scriptSig) return
|
if (!f.scriptSig) return
|
||||||
|
|
||||||
it('classifies ' + f.scriptSig + ' as ' + f.type, function () {
|
it('classifies ' + f.scriptSig + ' as ' + f.type, function () {
|
||||||
var script = Script.fromASM(f.scriptSig).buffer
|
var script = Script.fromASM(f.scriptSig)
|
||||||
var type = scripts.classifyInput(script)
|
var type = scripts.classifyInput(script)
|
||||||
|
|
||||||
assert.strictEqual(type, f.type)
|
assert.strictEqual(type, f.type)
|
||||||
|
@ -31,7 +31,7 @@ describe('Scripts', function () {
|
||||||
if (!f.typeIncomplete) return
|
if (!f.typeIncomplete) return
|
||||||
|
|
||||||
it('classifies incomplete ' + f.scriptSig + ' as ' + f.typeIncomplete, function () {
|
it('classifies incomplete ' + f.scriptSig + ' as ' + f.typeIncomplete, function () {
|
||||||
var script = Script.fromASM(f.scriptSig).buffer
|
var script = Script.fromASM(f.scriptSig)
|
||||||
var type = scripts.classifyInput(script, true)
|
var type = scripts.classifyInput(script, true)
|
||||||
|
|
||||||
assert.strictEqual(type, f.typeIncomplete)
|
assert.strictEqual(type, f.typeIncomplete)
|
||||||
|
@ -44,7 +44,7 @@ describe('Scripts', function () {
|
||||||
if (!f.scriptPubKey) return
|
if (!f.scriptPubKey) return
|
||||||
|
|
||||||
it('classifies ' + f.scriptPubKey + ' as ' + f.type, function () {
|
it('classifies ' + f.scriptPubKey + ' as ' + f.type, function () {
|
||||||
var script = Script.fromASM(f.scriptPubKey).buffer
|
var script = Script.fromASM(f.scriptPubKey)
|
||||||
var type = scripts.classifyOutput(script)
|
var type = scripts.classifyOutput(script)
|
||||||
|
|
||||||
assert.strictEqual(type, f.type)
|
assert.strictEqual(type, f.type)
|
||||||
|
@ -67,9 +67,9 @@ describe('Scripts', function () {
|
||||||
var script
|
var script
|
||||||
|
|
||||||
if (f.scriptSig) {
|
if (f.scriptSig) {
|
||||||
script = Script.fromASM(f.scriptSig).buffer
|
script = Script.fromASM(f.scriptSig)
|
||||||
} else {
|
} else {
|
||||||
script = Script.fromHex(f.scriptSigHex).buffer
|
script = Script.fromHex(f.scriptSigHex)
|
||||||
}
|
}
|
||||||
|
|
||||||
it('returns ' + expected + ' for ' + f.scriptSig, function () {
|
it('returns ' + expected + ' for ' + f.scriptSig, function () {
|
||||||
|
@ -94,9 +94,9 @@ describe('Scripts', function () {
|
||||||
var script
|
var script
|
||||||
|
|
||||||
if (f.scriptSig) {
|
if (f.scriptSig) {
|
||||||
script = Script.fromASM(f.scriptSig).buffer
|
script = Script.fromASM(f.scriptSig)
|
||||||
} else {
|
} else {
|
||||||
script = Script.fromHex(f.scriptSigHex).buffer
|
script = Script.fromHex(f.scriptSigHex)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.strictEqual(inputFn(script), false)
|
assert.strictEqual(inputFn(script), false)
|
||||||
|
@ -111,7 +111,7 @@ describe('Scripts', function () {
|
||||||
|
|
||||||
if (outputFn && f.scriptPubKey) {
|
if (outputFn && f.scriptPubKey) {
|
||||||
it('returns ' + expected + ' for ' + f.scriptPubKey, function () {
|
it('returns ' + expected + ' for ' + f.scriptPubKey, function () {
|
||||||
var script = Script.fromASM(f.scriptPubKey).buffer
|
var script = Script.fromASM(f.scriptPubKey)
|
||||||
|
|
||||||
assert.strictEqual(outputFn(script), expected)
|
assert.strictEqual(outputFn(script), expected)
|
||||||
})
|
})
|
||||||
|
@ -123,7 +123,7 @@ describe('Scripts', function () {
|
||||||
fixtures.invalid[outputFnName].forEach(function (f) {
|
fixtures.invalid[outputFnName].forEach(function (f) {
|
||||||
if (outputFn && f.scriptPubKey) {
|
if (outputFn && f.scriptPubKey) {
|
||||||
it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () {
|
it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () {
|
||||||
var script = Script.fromASM(f.scriptPubKey).buffer
|
var script = Script.fromASM(f.scriptPubKey)
|
||||||
|
|
||||||
assert.strictEqual(outputFn(script), false)
|
assert.strictEqual(outputFn(script), false)
|
||||||
})
|
})
|
||||||
|
@ -140,7 +140,7 @@ describe('Scripts', function () {
|
||||||
var signature = new Buffer(f.signature, 'hex')
|
var signature = new Buffer(f.signature, 'hex')
|
||||||
|
|
||||||
var scriptSig = scripts.pubKeyInput(signature)
|
var scriptSig = scripts.pubKeyInput(signature)
|
||||||
assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig)
|
assert.strictEqual(Script.toASM(scriptSig), f.scriptSig)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -152,7 +152,8 @@ describe('Scripts', function () {
|
||||||
it('returns ' + f.scriptPubKey, function () {
|
it('returns ' + f.scriptPubKey, function () {
|
||||||
var pubKey = new Buffer(f.pubKey, 'hex')
|
var pubKey = new Buffer(f.pubKey, 'hex')
|
||||||
var scriptPubKey = scripts.pubKeyOutput(pubKey)
|
var scriptPubKey = scripts.pubKeyOutput(pubKey)
|
||||||
assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey)
|
|
||||||
|
assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -167,7 +168,7 @@ describe('Scripts', function () {
|
||||||
var signature = new Buffer(f.signature, 'hex')
|
var signature = new Buffer(f.signature, 'hex')
|
||||||
|
|
||||||
var scriptSig = scripts.pubKeyHashInput(signature, pubKey)
|
var scriptSig = scripts.pubKeyHashInput(signature, pubKey)
|
||||||
assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig)
|
assert.strictEqual(Script.toASM(scriptSig), f.scriptSig)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -181,7 +182,7 @@ describe('Scripts', function () {
|
||||||
|
|
||||||
it('returns ' + f.scriptPubKey, function () {
|
it('returns ' + f.scriptPubKey, function () {
|
||||||
var scriptPubKey = scripts.pubKeyHashOutput(pubKeyHash)
|
var scriptPubKey = scripts.pubKeyHashOutput(pubKeyHash)
|
||||||
assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey)
|
assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -196,12 +197,12 @@ describe('Scripts', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
var scriptSig = scripts.multisigInput(signatures)
|
var scriptSig = scripts.multisigInput(signatures)
|
||||||
assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig)
|
assert.strictEqual(Script.toASM(scriptSig), f.scriptSig)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
fixtures.invalid.multisigInput.forEach(function (f) {
|
fixtures.invalid.multisigInput.forEach(function (f) {
|
||||||
var scriptPubKey = Script.fromASM(f.scriptPubKey).buffer
|
var scriptPubKey = Script.fromASM(f.scriptPubKey)
|
||||||
|
|
||||||
it('throws on ' + f.exception, function () {
|
it('throws on ' + f.exception, function () {
|
||||||
var signatures = f.signatures.map(function (signature) {
|
var signatures = f.signatures.map(function (signature) {
|
||||||
|
@ -223,7 +224,7 @@ describe('Scripts', function () {
|
||||||
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
|
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
|
||||||
|
|
||||||
it('returns ' + f.scriptPubKey, function () {
|
it('returns ' + f.scriptPubKey, function () {
|
||||||
assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey)
|
assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -244,16 +245,17 @@ describe('Scripts', function () {
|
||||||
fixtures.valid.forEach(function (f) {
|
fixtures.valid.forEach(function (f) {
|
||||||
if (f.type !== 'scripthash') return
|
if (f.type !== 'scripthash') return
|
||||||
|
|
||||||
var redeemScript = Script.fromASM(f.redeemScript).buffer
|
var redeemScript = Script.fromASM(f.redeemScript)
|
||||||
var redeemScriptSig = Script.fromASM(f.redeemScriptSig).buffer
|
var redeemScriptSig = Script.fromASM(f.redeemScriptSig)
|
||||||
|
|
||||||
it('returns ' + f.scriptSig, function () {
|
it('returns ' + f.scriptSig, function () {
|
||||||
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
|
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
|
||||||
|
|
||||||
if (f.scriptSig) {
|
if (f.scriptSig) {
|
||||||
assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig)
|
assert.strictEqual(Script.toASM(scriptSig), f.scriptSig)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
assert.strictEqual(Script.fromBuffer(scriptSig).toHex(), f.scriptSigHex)
|
assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -265,10 +267,10 @@ describe('Scripts', function () {
|
||||||
if (!f.scriptPubKey) return
|
if (!f.scriptPubKey) return
|
||||||
|
|
||||||
it('returns ' + f.scriptPubKey, function () {
|
it('returns ' + f.scriptPubKey, function () {
|
||||||
var redeemScript = Script.fromASM(f.redeemScript).buffer
|
var redeemScript = Script.compile(Script.fromASM(f.redeemScript))
|
||||||
var scriptPubKey = scripts.scriptHashOutput(bcrypto.hash160(redeemScript))
|
var scriptPubKey = scripts.scriptHashOutput(bcrypto.hash160(redeemScript))
|
||||||
|
|
||||||
assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey)
|
assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -281,7 +283,7 @@ describe('Scripts', function () {
|
||||||
var scriptPubKey = scripts.nullDataOutput(data)
|
var scriptPubKey = scripts.nullDataOutput(data)
|
||||||
|
|
||||||
it('returns ' + f.scriptPubKey, function () {
|
it('returns ' + f.scriptPubKey, function () {
|
||||||
assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey)
|
assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -20,8 +20,9 @@ describe('Transaction', function () {
|
||||||
if (txIn.data) {
|
if (txIn.data) {
|
||||||
var data = new Buffer(txIn.data, 'hex')
|
var data = new Buffer(txIn.data, 'hex')
|
||||||
script = data
|
script = data
|
||||||
|
|
||||||
} else if (txIn.script) {
|
} else if (txIn.script) {
|
||||||
script = Script.fromASM(txIn.script).buffer
|
script = Script.compile(Script.fromASM(txIn.script))
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.addInput(txHash, txIn.index, txIn.sequence, script)
|
tx.addInput(txHash, txIn.index, txIn.sequence, script)
|
||||||
|
@ -33,8 +34,9 @@ describe('Transaction', function () {
|
||||||
if (txOut.data) {
|
if (txOut.data) {
|
||||||
var data = new Buffer(txOut.data, 'hex')
|
var data = new Buffer(txOut.data, 'hex')
|
||||||
script = data
|
script = data
|
||||||
|
|
||||||
} else if (txOut.script) {
|
} else if (txOut.script) {
|
||||||
script = Script.fromASM(txOut.script).buffer
|
script = Script.compile(Script.fromASM(txOut.script))
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.addOutput(script, txOut.value)
|
tx.addOutput(script, txOut.value)
|
||||||
|
|
|
@ -22,14 +22,14 @@ function construct (f, sign) {
|
||||||
var prevTxScript
|
var prevTxScript
|
||||||
|
|
||||||
if (input.prevTxScript) {
|
if (input.prevTxScript) {
|
||||||
prevTxScript = Script.fromASM(input.prevTxScript).buffer
|
prevTxScript = Script.compile(Script.fromASM(input.prevTxScript))
|
||||||
}
|
}
|
||||||
|
|
||||||
txb.addInput(input.txId, input.vout, input.sequence, prevTxScript)
|
txb.addInput(input.txId, input.vout, input.sequence, prevTxScript)
|
||||||
})
|
})
|
||||||
|
|
||||||
f.outputs.forEach(function (output) {
|
f.outputs.forEach(function (output) {
|
||||||
var script = Script.fromASM(output.script).buffer
|
var script = Script.compile(Script.fromASM(output.script))
|
||||||
|
|
||||||
txb.addOutput(script, output.value)
|
txb.addOutput(script, output.value)
|
||||||
})
|
})
|
||||||
|
@ -41,7 +41,7 @@ function construct (f, sign) {
|
||||||
var redeemScript
|
var redeemScript
|
||||||
|
|
||||||
if (sign.redeemScript) {
|
if (sign.redeemScript) {
|
||||||
redeemScript = Script.fromASM(sign.redeemScript).buffer
|
redeemScript = Script.compile(Script.fromASM(sign.redeemScript))
|
||||||
}
|
}
|
||||||
|
|
||||||
txb.sign(index, keyPair, redeemScript, sign.hashType)
|
txb.sign(index, keyPair, redeemScript, sign.hashType)
|
||||||
|
@ -200,7 +200,7 @@ describe('TransactionBuilder', function () {
|
||||||
var redeemScript
|
var redeemScript
|
||||||
|
|
||||||
if (sign.redeemScript) {
|
if (sign.redeemScript) {
|
||||||
redeemScript = Script.fromASM(sign.redeemScript).buffer
|
redeemScript = Script.compile(Script.fromASM(sign.redeemScript))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sign.throws) {
|
if (!sign.throws) {
|
||||||
|
@ -262,7 +262,7 @@ describe('TransactionBuilder', function () {
|
||||||
var network = NETWORKS[f.network]
|
var network = NETWORKS[f.network]
|
||||||
|
|
||||||
f.inputs.forEach(function (input, i) {
|
f.inputs.forEach(function (input, i) {
|
||||||
var redeemScript = Script.fromASM(input.redeemScript).buffer
|
var redeemScript = Script.compile(Script.fromASM(input.redeemScript))
|
||||||
|
|
||||||
input.signs.forEach(function (sign) {
|
input.signs.forEach(function (sign) {
|
||||||
// rebuild the transaction each-time after the first
|
// rebuild the transaction each-time after the first
|
||||||
|
@ -272,11 +272,11 @@ describe('TransactionBuilder', function () {
|
||||||
var scriptSig = tx.ins[i].script
|
var scriptSig = tx.ins[i].script
|
||||||
|
|
||||||
// ignore OP_0 on the front, ignore redeemScript
|
// ignore OP_0 on the front, ignore redeemScript
|
||||||
var signatures = Script.fromBuffer(scriptSig).chunks.slice(1, -1).filter(function (x) { return x !== ops.OP_0 })
|
var signatures = Script.decompile(scriptSig).slice(1, -1).filter(function (x) { return x !== ops.OP_0 })
|
||||||
|
|
||||||
// rebuild/replace the scriptSig without them
|
// rebuild/replace the scriptSig without them
|
||||||
var replacement = scripts.scriptHashInput(scripts.multisigInput(signatures), redeemScript)
|
var replacement = scripts.scriptHashInput(scripts.multisigInput(signatures), redeemScript)
|
||||||
assert.strictEqual(Script.fromBuffer(replacement).toASM(), sign.scriptSigFiltered)
|
assert.strictEqual(Script.toASM(replacement), sign.scriptSigFiltered)
|
||||||
|
|
||||||
tx.ins[i].script = replacement
|
tx.ins[i].script = replacement
|
||||||
}
|
}
|
||||||
|
@ -292,7 +292,7 @@ describe('TransactionBuilder', function () {
|
||||||
tx = txb.buildIncomplete()
|
tx = txb.buildIncomplete()
|
||||||
|
|
||||||
// now verify the serialized scriptSig is as expected
|
// now verify the serialized scriptSig is as expected
|
||||||
assert.strictEqual(Script.fromBuffer(tx.ins[i].script).toASM(), sign.scriptSig)
|
assert.strictEqual(Script.toASM(tx.ins[i].script), sign.scriptSig)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -309,7 +309,7 @@ describe('TransactionBuilder', function () {
|
||||||
|
|
||||||
txb = TransactionBuilder.fromTransaction(lameTx, network)
|
txb = TransactionBuilder.fromTransaction(lameTx, network)
|
||||||
|
|
||||||
var redeemScript = Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG').buffer
|
var redeemScript = Script.compile(Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG'))
|
||||||
|
|
||||||
var keyPair = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', network)
|
var keyPair = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', network)
|
||||||
txb.sign(0, keyPair, redeemScript)
|
txb.sign(0, keyPair, redeemScript)
|
||||||
|
|
Loading…
Reference in a new issue