Removes type coercion for a stricter TX API

This commit is contained in:
Daniel Cousens 2014-04-23 05:33:11 +10:00
parent 9af8d95daf
commit 5546cae928
2 changed files with 11 additions and 16 deletions

View file

@ -362,23 +362,20 @@ Transaction.prototype.signWithKeys = function(keys, outputs, type) {
* Signs a P2SH output at some index with the given key * Signs a P2SH output at some index with the given key
*/ */
Transaction.prototype.p2shsign = function(index, script, key, type) { Transaction.prototype.p2shsign = function(index, script, key, type) {
script = new Script(script)
key = new ECKey(key)
type = type || SIGHASH_ALL type = type || SIGHASH_ALL
var hash = this.hashTransactionForSignature(script, index, type), var hash = this.hashTransactionForSignature(script, index, type)
sig = key.sign(hash).concat([type]) return key.sign(hash).concat([type])
return sig
} }
Transaction.prototype.setScriptSig = function(index, script) { Transaction.prototype.setScriptSig = function(index, script) {
this.ins[index].script = script this.ins[index].script = script
} }
Transaction.prototype.validateSig = function(index, script, sig, pub) { Transaction.prototype.validateSig = function(index, script, pub, sig, type) {
script = new Script(script) type = type || SIGHASH_ALL
var hash = this.hashTransactionForSignature(script,index,1) var hash = this.hashTransactionForSignature(script, index, type)
return ecdsa.verify(hash, convert.coerceToBytes(sig),
convert.coerceToBytes(pub)) return pub.verify(hash, sig)
} }
Transaction.feePerKb = 20000 Transaction.feePerKb = 20000

View file

@ -171,11 +171,10 @@ describe('Transaction', function() {
var key = ECKey.fromWIF('L44f7zxJ5Zw4EK9HZtyAnzCYz2vcZ5wiJf9AuwhJakiV4xVkxBeb') var key = ECKey.fromWIF('L44f7zxJ5Zw4EK9HZtyAnzCYz2vcZ5wiJf9AuwhJakiV4xVkxBeb')
tx.sign(0, key) tx.sign(0, key)
var pub = key.pub.toBuffer() var script = prevTx.outs[0].script
var script = prevTx.outs[0].script.buffer
var sig = tx.ins[0].script.chunks[0] var sig = tx.ins[0].script.chunks[0]
assert.equal(tx.validateSig(0, script, sig, pub), true) assert.equal(tx.validateSig(0, script, key.pub, sig), true)
}) })
}) })
@ -188,11 +187,10 @@ describe('Transaction', function() {
it('returns true for valid signature', function(){ it('returns true for valid signature', function(){
var key = ECKey.fromWIF('L44f7zxJ5Zw4EK9HZtyAnzCYz2vcZ5wiJf9AuwhJakiV4xVkxBeb') var key = ECKey.fromWIF('L44f7zxJ5Zw4EK9HZtyAnzCYz2vcZ5wiJf9AuwhJakiV4xVkxBeb')
var pub = key.pub.toBuffer() var script = prevTx.outs[0].script
var script = prevTx.outs[0].script.buffer
var sig = validTx.ins[0].script.chunks[0] var sig = validTx.ins[0].script.chunks[0]
assert.equal(validTx.validateSig(0, script, sig, pub), true) assert.equal(validTx.validateSig(0, script, key.pub, sig), true)
}) })
}) })