remove usage of instanced Scripts
This commit is contained in:
parent
5d2abb523e
commit
e743c58ed6
11 changed files with 146 additions and 119 deletions
|
@ -1,6 +1,8 @@
|
|||
var base58check = require('bs58check')
|
||||
var typeforce = require('typeforce')
|
||||
var networks = require('./networks')
|
||||
|
||||
var Script = require('./script')
|
||||
var scripts = require('./scripts')
|
||||
var types = require('./types')
|
||||
|
||||
|
@ -18,10 +20,10 @@ function fromBase58Check (address) {
|
|||
function fromOutputScript (script, network) {
|
||||
network = network || networks.bitcoin
|
||||
|
||||
if (scripts.isPubKeyHashOutput(script)) return toBase58Check(script.chunks[2], network.pubKeyHash)
|
||||
if (scripts.isScriptHashOutput(script)) return toBase58Check(script.chunks[1], network.scriptHash)
|
||||
if (scripts.isPubKeyHashOutput(script)) return toBase58Check(Script.fromBuffer(script).chunks[2], network.pubKeyHash)
|
||||
if (scripts.isScriptHashOutput(script)) return toBase58Check(Script.fromBuffer(script).chunks[1], network.scriptHash)
|
||||
|
||||
throw new Error(script.toASM() + ' has no matching Address')
|
||||
throw new Error(Script.fromBuffer(script).toASM() + ' has no matching Address')
|
||||
}
|
||||
|
||||
function toBase58Check (hash, version) {
|
||||
|
|
|
@ -41,12 +41,18 @@ function isCanonicalSignature (buffer) {
|
|||
}
|
||||
|
||||
function isPubKeyHashInput (script) {
|
||||
typeforce(types.Buffer, script)
|
||||
script = Script.fromBuffer(script)
|
||||
|
||||
return script.chunks.length === 2 &&
|
||||
isCanonicalSignature(script.chunks[0]) &&
|
||||
isCanonicalPubKey(script.chunks[1])
|
||||
}
|
||||
|
||||
function isPubKeyHashOutput (script) {
|
||||
typeforce(types.Buffer, script)
|
||||
script = Script.fromBuffer(script)
|
||||
|
||||
return script.chunks.length === 5 &&
|
||||
script.chunks[0] === ops.OP_DUP &&
|
||||
script.chunks[1] === ops.OP_HASH160 &&
|
||||
|
@ -57,32 +63,44 @@ function isPubKeyHashOutput (script) {
|
|||
}
|
||||
|
||||
function isPubKeyInput (script) {
|
||||
typeforce(types.Buffer, script)
|
||||
script = Script.fromBuffer(script)
|
||||
|
||||
return script.chunks.length === 1 &&
|
||||
isCanonicalSignature(script.chunks[0])
|
||||
}
|
||||
|
||||
function isPubKeyOutput (script) {
|
||||
typeforce(types.Buffer, script)
|
||||
script = Script.fromBuffer(script)
|
||||
|
||||
return script.chunks.length === 2 &&
|
||||
isCanonicalPubKey(script.chunks[0]) &&
|
||||
script.chunks[1] === ops.OP_CHECKSIG
|
||||
}
|
||||
|
||||
function isScriptHashInput (script, allowIncomplete) {
|
||||
typeforce(types.Buffer, script)
|
||||
script = Script.fromBuffer(script)
|
||||
|
||||
if (script.chunks.length < 2) return false
|
||||
|
||||
var lastChunk = script.chunks[script.chunks.length - 1]
|
||||
if (!Buffer.isBuffer(lastChunk)) return false
|
||||
|
||||
var scriptSig = Script.fromChunks(script.chunks.slice(0, -1))
|
||||
var redeemScript = Script.fromBuffer(lastChunk)
|
||||
var scriptSig = Script.fromChunks(script.chunks.slice(0, -1)).buffer
|
||||
var redeemScript = lastChunk
|
||||
|
||||
// is redeemScript a valid script?
|
||||
if (redeemScript.chunks.length === 0) return false
|
||||
if (Script.fromBuffer(redeemScript).chunks.length === 0) return false
|
||||
|
||||
return classifyInput(scriptSig, allowIncomplete) === classifyOutput(redeemScript)
|
||||
}
|
||||
|
||||
function isScriptHashOutput (script) {
|
||||
typeforce(types.Buffer, script)
|
||||
script = Script.fromBuffer(script)
|
||||
|
||||
return script.chunks.length === 3 &&
|
||||
script.chunks[0] === ops.OP_HASH160 &&
|
||||
Buffer.isBuffer(script.chunks[1]) &&
|
||||
|
@ -93,6 +111,9 @@ function isScriptHashOutput (script) {
|
|||
// allowIncomplete is to account for combining signatures
|
||||
// See https://github.com/bitcoin/bitcoin/blob/f425050546644a36b0b8e0eb2f6934a3e0f6f80f/src/script/sign.cpp#L195-L197
|
||||
function isMultisigInput (script, allowIncomplete) {
|
||||
typeforce(types.Buffer, script)
|
||||
script = Script.fromBuffer(script)
|
||||
|
||||
if (script.chunks.length < 2) return false
|
||||
if (script.chunks[0] !== ops.OP_0) return false
|
||||
|
||||
|
@ -106,6 +127,9 @@ function isMultisigInput (script, allowIncomplete) {
|
|||
}
|
||||
|
||||
function isMultisigOutput (script) {
|
||||
typeforce(types.Buffer, script)
|
||||
script = Script.fromBuffer(script)
|
||||
|
||||
if (script.chunks.length < 4) return false
|
||||
if (script.chunks[script.chunks.length - 1] !== ops.OP_CHECKMULTISIG) return false
|
||||
|
||||
|
@ -130,11 +154,14 @@ function isMultisigOutput (script) {
|
|||
}
|
||||
|
||||
function isNullDataOutput (script) {
|
||||
typeforce(types.Buffer, script)
|
||||
script = Script.fromBuffer(script)
|
||||
|
||||
return script.chunks[0] === ops.OP_RETURN
|
||||
}
|
||||
|
||||
function classifyOutput (script) {
|
||||
typeforce(types.Script, script)
|
||||
typeforce(types.Buffer, script)
|
||||
|
||||
if (isPubKeyHashOutput(script)) {
|
||||
return 'pubkeyhash'
|
||||
|
@ -152,7 +179,7 @@ function classifyOutput (script) {
|
|||
}
|
||||
|
||||
function classifyInput (script, allowIncomplete) {
|
||||
typeforce(types.Script, script)
|
||||
typeforce(types.Buffer, script)
|
||||
|
||||
if (isPubKeyHashInput(script)) {
|
||||
return 'pubkeyhash'
|
||||
|
@ -173,7 +200,7 @@ function pubKeyOutput (pubKey) {
|
|||
return Script.fromChunks([
|
||||
pubKey,
|
||||
ops.OP_CHECKSIG
|
||||
])
|
||||
]).buffer
|
||||
}
|
||||
|
||||
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
|
||||
|
@ -186,7 +213,7 @@ function pubKeyHashOutput (hash) {
|
|||
hash,
|
||||
ops.OP_EQUALVERIFY,
|
||||
ops.OP_CHECKSIG
|
||||
])
|
||||
]).buffer
|
||||
}
|
||||
|
||||
// OP_HASH160 {scriptHash} OP_EQUAL
|
||||
|
@ -197,7 +224,7 @@ function scriptHashOutput (hash) {
|
|||
ops.OP_HASH160,
|
||||
hash,
|
||||
ops.OP_EQUAL
|
||||
])
|
||||
]).buffer
|
||||
}
|
||||
|
||||
// m [pubKeys ...] n OP_CHECKMULTISIG
|
||||
|
@ -212,35 +239,38 @@ function multisigOutput (m, pubKeys) {
|
|||
pubKeys,
|
||||
(ops.OP_1 - 1) + n,
|
||||
ops.OP_CHECKMULTISIG
|
||||
))
|
||||
)).buffer
|
||||
}
|
||||
|
||||
// {signature}
|
||||
function pubKeyInput (signature) {
|
||||
typeforce(types.Buffer, signature)
|
||||
|
||||
return Script.fromChunks([signature])
|
||||
return Script.fromChunks([signature]).buffer
|
||||
}
|
||||
|
||||
// {signature} {pubKey}
|
||||
function pubKeyHashInput (signature, pubKey) {
|
||||
typeforce(types.tuple(types.Buffer, types.Buffer), arguments)
|
||||
|
||||
return Script.fromChunks([signature, pubKey])
|
||||
return Script.fromChunks([signature, pubKey]).buffer
|
||||
}
|
||||
|
||||
// <scriptSig> {serialized scriptPubKey script}
|
||||
function scriptHashInput (scriptSig, scriptPubKey) {
|
||||
var scriptSigChunks = Script.fromBuffer(scriptSig).chunks
|
||||
|
||||
return Script.fromChunks([].concat(
|
||||
scriptSig.chunks,
|
||||
scriptPubKey.toBuffer()
|
||||
))
|
||||
scriptSigChunks,
|
||||
scriptPubKey
|
||||
)).buffer
|
||||
}
|
||||
|
||||
// OP_0 [signatures ...]
|
||||
function multisigInput (signatures, scriptPubKey) {
|
||||
if (scriptPubKey) {
|
||||
if (!isMultisigOutput(scriptPubKey)) throw new Error('Expected multisig scriptPubKey')
|
||||
scriptPubKey = Script.fromBuffer(scriptPubKey)
|
||||
|
||||
var mOp = scriptPubKey.chunks[0]
|
||||
var nOp = scriptPubKey.chunks[scriptPubKey.chunks.length - 2]
|
||||
|
@ -251,11 +281,11 @@ function multisigInput (signatures, scriptPubKey) {
|
|||
if (signatures.length > n) throw new Error('Too many signatures provided')
|
||||
}
|
||||
|
||||
return Script.fromChunks([].concat(ops.OP_0, signatures))
|
||||
return Script.fromChunks([].concat(ops.OP_0, signatures)).buffer
|
||||
}
|
||||
|
||||
function nullDataOutput (data) {
|
||||
return Script.fromChunks([ops.OP_RETURN, data])
|
||||
return Script.fromChunks([ops.OP_RETURN, data]).buffer
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
@ -45,11 +45,7 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
|
|||
}
|
||||
|
||||
function readScript () {
|
||||
return Script.fromBuffer(readSlice(readVarInt()))
|
||||
}
|
||||
|
||||
function readGenerationScript () {
|
||||
return new Script(readSlice(readVarInt()), [])
|
||||
return readSlice(readVarInt())
|
||||
}
|
||||
|
||||
var tx = new Transaction()
|
||||
|
@ -63,7 +59,7 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
|
|||
tx.ins.push({
|
||||
hash: hash,
|
||||
index: readUInt32(),
|
||||
script: readGenerationScript(),
|
||||
script: readScript(),
|
||||
sequence: readUInt32()
|
||||
})
|
||||
} else {
|
||||
|
@ -102,19 +98,21 @@ Transaction.isCoinbaseHash = function (buffer) {
|
|||
})
|
||||
}
|
||||
|
||||
var EMPTY = new Buffer(0)
|
||||
|
||||
Transaction.prototype.addInput = function (hash, index, sequence, script) {
|
||||
typeforce(types.tuple(
|
||||
types.Hash256bit,
|
||||
types.UInt32,
|
||||
types.maybe(types.UInt32),
|
||||
types.maybe(types.Script)
|
||||
types.maybe(types.Buffer)
|
||||
), arguments)
|
||||
|
||||
if (types.Null(sequence)) {
|
||||
sequence = Transaction.DEFAULT_SEQUENCE
|
||||
}
|
||||
|
||||
script = script || Script.EMPTY
|
||||
script = script || EMPTY
|
||||
|
||||
// Add the input and return the input's index
|
||||
return (this.ins.push({
|
||||
|
@ -126,7 +124,7 @@ Transaction.prototype.addInput = function (hash, index, sequence, script) {
|
|||
}
|
||||
|
||||
Transaction.prototype.addOutput = function (scriptPubKey, value) {
|
||||
typeforce(types.tuple(types.Script, types.UInt53), arguments)
|
||||
typeforce(types.tuple(types.Buffer, types.UInt53), arguments)
|
||||
|
||||
// Add the output and return the output's index
|
||||
return (this.outs.push({
|
||||
|
@ -137,7 +135,7 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) {
|
|||
|
||||
Transaction.prototype.byteLength = function () {
|
||||
function scriptSize (script) {
|
||||
var length = script.buffer.length
|
||||
var length = script.length
|
||||
|
||||
return bufferutils.varIntSize(length) + length
|
||||
}
|
||||
|
@ -186,7 +184,7 @@ var ONE = new Buffer('0000000000000000000000000000000000000000000000000000000000
|
|||
* This hash can then be used to sign the provided transaction input.
|
||||
*/
|
||||
Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashType) {
|
||||
typeforce(types.tuple(types.UInt32, types.Script, /* types.UInt8 */ types.Number), arguments)
|
||||
typeforce(types.tuple(types.UInt32, types.Buffer, /* types.UInt8 */ types.Number), arguments)
|
||||
|
||||
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29
|
||||
if (inIndex >= this.ins.length) return ONE
|
||||
|
@ -195,11 +193,11 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
|
|||
|
||||
// in case concatenating two scripts ends up with two codeseparators,
|
||||
// or an extra one at the end, this prevents all those possible incompatibilities.
|
||||
var hashScript = prevOutScript.without(opcodes.OP_CODESEPARATOR)
|
||||
var hashScript = Script.fromBuffer(prevOutScript).without(opcodes.OP_CODESEPARATOR).buffer
|
||||
var i
|
||||
|
||||
// blank out other inputs' signatures
|
||||
txTmp.ins.forEach(function (input) { input.script = Script.EMPTY })
|
||||
txTmp.ins.forEach(function (input) { input.script = EMPTY })
|
||||
txTmp.ins[inIndex].script = hashScript
|
||||
|
||||
// blank out some of the inputs
|
||||
|
@ -225,7 +223,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
|
|||
|
||||
// blank all other outputs (clear scriptPubKey, value === -1)
|
||||
var stubOut = {
|
||||
script: Script.EMPTY,
|
||||
script: EMPTY,
|
||||
valueBuffer: new Buffer('ffffffffffffffff', 'hex')
|
||||
}
|
||||
|
||||
|
@ -294,8 +292,8 @@ Transaction.prototype.toBuffer = function () {
|
|||
this.ins.forEach(function (txIn) {
|
||||
writeSlice(txIn.hash)
|
||||
writeUInt32(txIn.index)
|
||||
writeVarInt(txIn.script.buffer.length)
|
||||
writeSlice(txIn.script.buffer)
|
||||
writeVarInt(txIn.script.length)
|
||||
writeSlice(txIn.script)
|
||||
writeUInt32(txIn.sequence)
|
||||
})
|
||||
|
||||
|
@ -307,8 +305,8 @@ Transaction.prototype.toBuffer = function () {
|
|||
writeSlice(txOut.valueBuffer)
|
||||
}
|
||||
|
||||
writeVarInt(txOut.script.buffer.length)
|
||||
writeSlice(txOut.script.buffer)
|
||||
writeVarInt(txOut.script.length)
|
||||
writeSlice(txOut.script)
|
||||
})
|
||||
|
||||
writeUInt32(this.locktime)
|
||||
|
@ -321,7 +319,7 @@ Transaction.prototype.toHex = function () {
|
|||
}
|
||||
|
||||
Transaction.prototype.setInputScript = function (index, script) {
|
||||
typeforce(types.tuple(types.Number, types.Script), arguments)
|
||||
typeforce(types.tuple(types.Number, types.Buffer), arguments)
|
||||
|
||||
this.ins[index].script = script
|
||||
}
|
||||
|
|
|
@ -13,16 +13,20 @@ var Transaction = require('./transaction')
|
|||
function extractInput (txIn) {
|
||||
var redeemScript
|
||||
var scriptSig = txIn.script
|
||||
var scriptSigChunks = Script.fromBuffer(scriptSig).chunks
|
||||
|
||||
var prevOutScript
|
||||
var prevOutType = scripts.classifyInput(scriptSig, true)
|
||||
var scriptType
|
||||
|
||||
// Re-classify if scriptHash
|
||||
if (prevOutType === 'scripthash') {
|
||||
redeemScript = Script.fromBuffer(scriptSig.chunks.slice(-1)[0])
|
||||
prevOutScript = scripts.scriptHashOutput(redeemScript.getHash())
|
||||
redeemScript = scriptSigChunks.slice(-1)[0]
|
||||
prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript))
|
||||
|
||||
scriptSig = Script.fromChunks(scriptSigChunks.slice(0, -1)).buffer
|
||||
scriptSigChunks = scriptSigChunks.slice(0, -1)
|
||||
|
||||
scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1))
|
||||
scriptType = scripts.classifyInput(scriptSig, true)
|
||||
} else {
|
||||
scriptType = prevOutType
|
||||
|
@ -33,27 +37,27 @@ function extractInput (txIn) {
|
|||
|
||||
switch (scriptType) {
|
||||
case 'pubkeyhash':
|
||||
parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0])
|
||||
parsed = ECSignature.parseScriptSignature(scriptSigChunks[0])
|
||||
hashType = parsed.hashType
|
||||
pubKeys = scriptSig.chunks.slice(1)
|
||||
pubKeys = scriptSigChunks.slice(1)
|
||||
signatures = [parsed.signature]
|
||||
prevOutScript = scripts.pubKeyHashOutput(bcrypto.hash160(pubKeys[0]))
|
||||
|
||||
break
|
||||
|
||||
case 'pubkey':
|
||||
parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0])
|
||||
parsed = ECSignature.parseScriptSignature(scriptSigChunks[0])
|
||||
hashType = parsed.hashType
|
||||
signatures = [parsed.signature]
|
||||
|
||||
if (redeemScript) {
|
||||
pubKeys = redeemScript.chunks.slice(0, 1)
|
||||
pubKeys = Script.fromBuffer(redeemScript).chunks.slice(0, 1)
|
||||
}
|
||||
|
||||
break
|
||||
|
||||
case 'multisig':
|
||||
signatures = scriptSig.chunks.slice(1).map(function (chunk) {
|
||||
signatures = scriptSigChunks.slice(1).map(function (chunk) {
|
||||
if (chunk === ops.OP_0) return chunk
|
||||
|
||||
var parsed = ECSignature.parseScriptSignature(chunk)
|
||||
|
@ -63,7 +67,7 @@ function extractInput (txIn) {
|
|||
})
|
||||
|
||||
if (redeemScript) {
|
||||
pubKeys = redeemScript.chunks.slice(1, -2)
|
||||
pubKeys = Script.fromBuffer(redeemScript).chunks.slice(1, -2)
|
||||
}
|
||||
|
||||
break
|
||||
|
@ -115,7 +119,7 @@ TransactionBuilder.fromTransaction = function (transaction, network) {
|
|||
}
|
||||
|
||||
// Ignore empty scripts
|
||||
if (txIn.script.buffer.length === 0) return {}
|
||||
if (txIn.script.length === 0) return {}
|
||||
|
||||
return extractInput(txIn)
|
||||
})
|
||||
|
@ -139,15 +143,16 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
|
|||
var input = {}
|
||||
if (prevOutScript) {
|
||||
var prevOutType = scripts.classifyOutput(prevOutScript)
|
||||
var prevOutScriptChunks = Script.fromBuffer(prevOutScript).chunks
|
||||
|
||||
// if we can, extract pubKey information
|
||||
switch (prevOutType) {
|
||||
case 'multisig':
|
||||
input.pubKeys = prevOutScript.chunks.slice(1, -2)
|
||||
input.pubKeys = prevOutScriptChunks.slice(1, -2)
|
||||
break
|
||||
|
||||
case 'pubkey':
|
||||
input.pubKeys = prevOutScript.chunks.slice(0, 1)
|
||||
input.pubKeys = prevOutScriptChunks.slice(0, 1)
|
||||
break
|
||||
}
|
||||
|
||||
|
@ -308,21 +313,22 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
|
|||
if (input.prevOutScript) {
|
||||
if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH')
|
||||
|
||||
var scriptHash = input.prevOutScript.chunks[1]
|
||||
if (!bufferutils.equal(scriptHash, redeemScript.getHash())) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex'))
|
||||
var scriptHash = Script.fromBuffer(input.prevOutScript).chunks[1]
|
||||
if (!bufferutils.equal(scriptHash, bcrypto.hash160(redeemScript))) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex'))
|
||||
}
|
||||
|
||||
var scriptType = scripts.classifyOutput(redeemScript)
|
||||
if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')')
|
||||
|
||||
var redeemScriptChunks = Script.fromBuffer(redeemScript).chunks
|
||||
var pubKeys = []
|
||||
switch (scriptType) {
|
||||
case 'multisig':
|
||||
pubKeys = redeemScript.chunks.slice(1, -2)
|
||||
pubKeys = redeemScriptChunks.slice(1, -2)
|
||||
break
|
||||
|
||||
case 'pubkeyhash':
|
||||
var pkh1 = redeemScript.chunks[2]
|
||||
var pkh1 = redeemScriptChunks[2]
|
||||
var pkh2 = bcrypto.hash160(keyPair.getPublicKeyBuffer())
|
||||
|
||||
if (!bufferutils.equal(pkh1, pkh2)) throw new Error('privateKey cannot sign for this input')
|
||||
|
@ -330,12 +336,13 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
|
|||
break
|
||||
|
||||
case 'pubkey':
|
||||
pubKeys = redeemScript.chunks.slice(0, 1)
|
||||
pubKeys = redeemScriptChunks.slice(0, 1)
|
||||
break
|
||||
}
|
||||
|
||||
// if we don't have a prevOutScript, generate a P2SH script
|
||||
if (!input.prevOutScript) {
|
||||
input.prevOutScript = scripts.scriptHashOutput(redeemScript.getHash())
|
||||
input.prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript))
|
||||
input.prevOutType = 'scripthash'
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue