use baddress/bcrypto/bscript for ambuigities

This commit is contained in:
Daniel Cousens 2015-08-20 13:37:19 +10:00
parent 6ec687deba
commit 8934de591c
9 changed files with 113 additions and 113 deletions

View file

@ -1,5 +1,5 @@
var bs58check = require('bs58check')
var script = require('./script')
var bscript = require('./script')
var networks = require('./networks')
var typeforce = require('typeforce')
var types = require('./types')
@ -18,11 +18,11 @@ function fromBase58Check (address) {
function fromOutputScript (scriptPubKey, network) {
network = network || networks.bitcoin
var chunks = script.decompile(scriptPubKey)
if (script.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash)
if (script.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash)
var chunks = bscript.decompile(scriptPubKey)
if (bscript.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash)
if (bscript.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash)
throw new Error(script.toASM(chunks) + ' has no matching Address')
throw new Error(bscript.toASM(chunks) + ' has no matching Address')
}
function toBase58Check (hash, version) {
@ -45,8 +45,8 @@ function toOutputScript (address, network) {
var version = payload.readUInt8(0)
var hash = payload.slice(1)
if (version === network.pubKeyHash) return script.pubKeyHashOutput(hash)
if (version === network.scriptHash) return script.scriptHashOutput(hash)
if (version === network.pubKeyHash) return bscript.pubKeyHashOutput(hash)
if (version === network.scriptHash) return bscript.scriptHashOutput(hash)
throw new Error(address + ' has no matching Script')
}

View file

@ -1,5 +1,5 @@
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var bcrypto = require('./crypto')
var Transaction = require('./transaction')
@ -66,7 +66,7 @@ Block.fromHex = function (hex) {
}
Block.prototype.getHash = function () {
return crypto.hash256(this.toBuffer(true))
return bcrypto.hash256(this.toBuffer(true))
}
Block.prototype.getId = function () {

View file

@ -1,5 +1,5 @@
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var bcrypto = require('./crypto')
var ecdsa = require('./ecdsa')
var networks = require('./networks')
@ -16,7 +16,7 @@ function magicHash (message, network) {
var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length)
var buffer = Buffer.concat([messagePrefix, lengthBuffer, messageBuffer])
return crypto.hash256(buffer)
return bcrypto.hash256(buffer)
}
function sign (keyPair, message, network) {

View file

@ -1,7 +1,7 @@
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var bcrypto = require('./crypto')
var opcodes = require('./opcodes')
var script = require('./script')
var bscript = require('./script')
var typeforce = require('typeforce')
var types = require('./types')
@ -191,7 +191,7 @@ 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 = script.compile(script.decompile(prevOutScript).filter(function (x) {
var hashScript = bscript.compile(bscript.decompile(prevOutScript).filter(function (x) {
return x !== opcodes.OP_CODESEPARATOR
}))
var i
@ -250,11 +250,11 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
buffer.writeInt32LE(hashType, buffer.length - 4)
txTmp.toBuffer().copy(buffer, 0)
return crypto.hash256(buffer)
return bcrypto.hash256(buffer)
}
Transaction.prototype.getHash = function () {
return crypto.hash256(this.toBuffer())
return bcrypto.hash256(this.toBuffer())
}
Transaction.prototype.getId = function () {

View file

@ -1,9 +1,9 @@
var address = require('./address')
var baddress = require('./address')
var bcrypto = require('./crypto')
var bscript = require('./script')
var bufferutils = require('./bufferutils')
var networks = require('./networks')
var ops = require('./opcodes')
var script = require('./script')
var ECPair = require('./ecpair')
var ECSignature = require('./ecsignature')
@ -12,21 +12,21 @@ var Transaction = require('./transaction')
function extractInput (txIn) {
var redeemScript
var scriptSig = txIn.script
var scriptSigChunks = script.decompile(scriptSig)
var scriptSigChunks = bscript.decompile(scriptSig)
var prevOutScript
var prevOutType = script.classifyInput(scriptSig, true)
var prevOutType = bscript.classifyInput(scriptSig, true)
var scriptType
// Re-classify if scriptHash
if (prevOutType === 'scripthash') {
redeemScript = scriptSigChunks.slice(-1)[0]
prevOutScript = script.scriptHashOutput(bcrypto.hash160(redeemScript))
prevOutScript = bscript.scriptHashOutput(bcrypto.hash160(redeemScript))
scriptSig = script.compile(scriptSigChunks.slice(0, -1))
scriptSig = bscript.compile(scriptSigChunks.slice(0, -1))
scriptSigChunks = scriptSigChunks.slice(0, -1)
scriptType = script.classifyInput(scriptSig, true)
scriptType = bscript.classifyInput(scriptSig, true)
} else {
scriptType = prevOutType
}
@ -34,7 +34,7 @@ function extractInput (txIn) {
// pre-empt redeemScript decompilation
var redeemScriptChunks
if (redeemScript) {
redeemScriptChunks = script.decompile(redeemScript)
redeemScriptChunks = bscript.decompile(redeemScript)
}
// Extract hashType, pubKeys and signatures
@ -46,7 +46,7 @@ function extractInput (txIn) {
hashType = parsed.hashType
pubKeys = scriptSigChunks.slice(1)
signatures = [parsed.signature]
prevOutScript = script.pubKeyHashOutput(bcrypto.hash160(pubKeys[0]))
prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(pubKeys[0]))
break
@ -147,8 +147,8 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
var input = {}
if (prevOutScript) {
var prevOutScriptChunks = script.decompile(prevOutScript)
var prevOutType = script.classifyOutput(prevOutScriptChunks)
var prevOutScriptChunks = bscript.decompile(prevOutScript)
var prevOutType = bscript.classifyOutput(prevOutScriptChunks)
// if we can, extract pubKey information
switch (prevOutType) {
@ -198,7 +198,7 @@ TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) {
// Attempt to get a script if it's a base58 address string
if (typeof scriptPubKey === 'string') {
scriptPubKey = address.toOutputScript(scriptPubKey, this.network)
scriptPubKey = baddress.toOutputScript(scriptPubKey, this.network)
}
return this.tx.addOutput(scriptPubKey, value)
@ -240,7 +240,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
switch (scriptType) {
case 'pubkeyhash':
var pkhSignature = input.signatures[0].toScriptSignature(input.hashType)
scriptSig = script.pubKeyHashInput(pkhSignature, input.pubKeys[0])
scriptSig = bscript.pubKeyHashInput(pkhSignature, input.pubKeys[0])
break
case 'multisig':
@ -262,12 +262,12 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
}
var redeemScript = allowIncomplete ? undefined : input.redeemScript
scriptSig = script.multisigInput(msSignatures, redeemScript)
scriptSig = bscript.multisigInput(msSignatures, redeemScript)
break
case 'pubkey':
var pkSignature = input.signatures[0].toScriptSignature(input.hashType)
scriptSig = script.pubKeyInput(pkSignature)
scriptSig = bscript.pubKeyInput(pkSignature)
break
}
}
@ -276,7 +276,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
if (scriptSig) {
// wrap as scriptHash if necessary
if (input.prevOutType === 'scripthash') {
scriptSig = script.scriptHashInput(scriptSig, input.redeemScript)
scriptSig = bscript.scriptHashInput(scriptSig, input.redeemScript)
}
tx.setInputScript(index, scriptSig)
@ -318,14 +318,14 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
if (input.prevOutScript) {
if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH')
var scriptHash = script.decompile(input.prevOutScript)[1]
var scriptHash = bscript.decompile(input.prevOutScript)[1]
if (!bufferutils.equal(scriptHash, bcrypto.hash160(redeemScript))) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex'))
}
var scriptType = script.classifyOutput(redeemScript)
var scriptType = bscript.classifyOutput(redeemScript)
if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')')
var redeemScriptChunks = script.decompile(redeemScript)
var redeemScriptChunks = bscript.decompile(redeemScript)
var pubKeys = []
switch (scriptType) {
case 'multisig':
@ -347,7 +347,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
// if we don't have a prevOutScript, generate a P2SH script
if (!input.prevOutScript) {
input.prevOutScript = script.scriptHashOutput(bcrypto.hash160(redeemScript))
input.prevOutScript = bscript.scriptHashOutput(bcrypto.hash160(redeemScript))
input.prevOutType = 'scripthash'
}
@ -365,7 +365,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
// we know nothin' Jon Snow, assume pubKeyHash
} else {
input.prevOutScript = script.pubKeyHashOutput(bcrypto.hash160(keyPair.getPublicKeyBuffer()))
input.prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(keyPair.getPublicKeyBuffer()))
input.prevOutType = 'pubkeyhash'
input.pubKeys = [kpPubKey]
input.scriptType = input.prevOutType