Skip to content

Commit

Permalink
use baddress/bcrypto/bscript for ambuigities
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Aug 20, 2015
1 parent 54083b6 commit a38bef3
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 113 deletions.
14 changes: 7 additions & 7 deletions src/address.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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) {
Expand All @@ -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')
}
Expand Down
4 changes: 2 additions & 2 deletions src/block.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var bcrypto = require('./crypto')

var Transaction = require('./transaction')

Expand Down Expand Up @@ -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 () {
Expand Down
4 changes: 2 additions & 2 deletions src/message.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var bcrypto = require('./crypto')
var ecdsa = require('./ecdsa')
var networks = require('./networks')

Expand All @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/transaction.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 () {
Expand Down
42 changes: 21 additions & 21 deletions src/transaction_builder.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -12,29 +12,29 @@ 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
}

// pre-empt redeemScript decompilation
var redeemScriptChunks
if (redeemScript) {
redeemScriptChunks = script.decompile(redeemScript)
redeemScriptChunks = bscript.decompile(redeemScript)
}

// Extract hashType, pubKeys and signatures
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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':
Expand All @@ -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
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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':
Expand All @@ -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'
}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/crypto.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* global describe, it */

var assert = require('assert')
var crypto = require('../src/crypto')
var bcrypto = require('../src/crypto')

var fixtures = require('./fixtures/crypto')

describe('Crypto', function () {
['hash160', 'hash256', 'ripemd160', 'sha1', 'sha256'].forEach(function (algorithm) {
describe(algorithm, function () {
fixtures.valid.forEach(function (f) {
var fn = crypto[algorithm]
var fn = bcrypto[algorithm]
var expected = f[algorithm]

it('returns ' + expected + ' for ' + f.hex, function () {
Expand Down
16 changes: 8 additions & 8 deletions test/ecdsa.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global describe, it */

var assert = require('assert')
var crypto = require('../src/crypto')
var bcrypto = require('../src/crypto')
var ecdsa = require('../src/ecdsa')
var message = require('../src/message')
var networks = require('../src/networks')
Expand All @@ -24,7 +24,7 @@ describe('ecdsa', function () {
fixtures.valid.ecdsa.forEach(function (f) {
it('for "' + f.message + '"', function () {
var d = BigInteger.fromHex(f.d)
var h1 = crypto.sha256(f.message)
var h1 = bcrypto.sha256(f.message)

var k = ecdsa.deterministicGenerateK(curve, h1, d, checkSig)
assert.strictEqual(k.toHex(), f.k)
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('ecdsa', function () {
fixtures.valid.rfc6979.forEach(function (f) {
it('produces the expected k values for ' + f.message + " if k wasn't suitable", function () {
var d = BigInteger.fromHex(f.d)
var h1 = crypto.sha256(f.message)
var h1 = bcrypto.sha256(f.message)

var results = []
ecdsa.deterministicGenerateK(curve, h1, d, function (k) {
Expand All @@ -90,7 +90,7 @@ describe('ecdsa', function () {
var d = BigInteger.fromHex(f.d)
var Q = curve.G.multiply(d)
var signature = ECSignature.fromDER(new Buffer(f.signature, 'hex'))
var h1 = crypto.sha256(f.message)
var h1 = bcrypto.sha256(f.message)
var e = BigInteger.fromBuffer(h1)
var Qprime = ecdsa.recoverPubKey(curve, e, signature, f.i)

Expand Down Expand Up @@ -137,15 +137,15 @@ describe('ecdsa', function () {
fixtures.valid.ecdsa.forEach(function (f) {
it('produces a deterministic signature for "' + f.message + '"', function () {
var d = BigInteger.fromHex(f.d)
var hash = crypto.sha256(f.message)
var hash = bcrypto.sha256(f.message)
var signature = ecdsa.sign(curve, hash, d).toDER()

assert.strictEqual(signature.toString('hex'), f.signature)
})
})

it('should sign with low S value', function () {
var hash = crypto.sha256('Vires in numeris')
var hash = bcrypto.sha256('Vires in numeris')
var sig = ecdsa.sign(curve, hash, BigInteger.ONE)

// See BIP62 for more information
Expand All @@ -158,7 +158,7 @@ describe('ecdsa', function () {
fixtures.valid.ecdsa.forEach(function (f) {
it('verifies a valid signature for "' + f.message + '"', function () {
var d = BigInteger.fromHex(f.d)
var H = crypto.sha256(f.message)
var H = bcrypto.sha256(f.message)
var signature = ECSignature.fromDER(new Buffer(f.signature, 'hex'))
var Q = curve.G.multiply(d)

Expand All @@ -168,7 +168,7 @@ describe('ecdsa', function () {

fixtures.invalid.verify.forEach(function (f) {
it('fails to verify with ' + f.description, function () {
var H = crypto.sha256(f.message)
var H = bcrypto.sha256(f.message)
var d = BigInteger.fromHex(f.d)

var signature
Expand Down
Loading

0 comments on commit a38bef3

Please sign in to comment.