Skip to content

Commit 5546cae

Browse files
committed
Removes type coercion for a stricter TX API
1 parent 9af8d95 commit 5546cae

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/transaction.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,23 +362,20 @@ Transaction.prototype.signWithKeys = function(keys, outputs, type) {
362362
* Signs a P2SH output at some index with the given key
363363
*/
364364
Transaction.prototype.p2shsign = function(index, script, key, type) {
365-
script = new Script(script)
366-
key = new ECKey(key)
367365
type = type || SIGHASH_ALL
368-
var hash = this.hashTransactionForSignature(script, index, type),
369-
sig = key.sign(hash).concat([type])
370-
return sig
366+
var hash = this.hashTransactionForSignature(script, index, type)
367+
return key.sign(hash).concat([type])
371368
}
372369

373370
Transaction.prototype.setScriptSig = function(index, script) {
374371
this.ins[index].script = script
375372
}
376373

377-
Transaction.prototype.validateSig = function(index, script, sig, pub) {
378-
script = new Script(script)
379-
var hash = this.hashTransactionForSignature(script,index,1)
380-
return ecdsa.verify(hash, convert.coerceToBytes(sig),
381-
convert.coerceToBytes(pub))
374+
Transaction.prototype.validateSig = function(index, script, pub, sig, type) {
375+
type = type || SIGHASH_ALL
376+
var hash = this.hashTransactionForSignature(script, index, type)
377+
378+
return pub.verify(hash, sig)
382379
}
383380

384381
Transaction.feePerKb = 20000

test/transaction.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,10 @@ describe('Transaction', function() {
171171
var key = ECKey.fromWIF('L44f7zxJ5Zw4EK9HZtyAnzCYz2vcZ5wiJf9AuwhJakiV4xVkxBeb')
172172
tx.sign(0, key)
173173

174-
var pub = key.pub.toBuffer()
175-
var script = prevTx.outs[0].script.buffer
174+
var script = prevTx.outs[0].script
176175
var sig = tx.ins[0].script.chunks[0]
177176

178-
assert.equal(tx.validateSig(0, script, sig, pub), true)
177+
assert.equal(tx.validateSig(0, script, key.pub, sig), true)
179178
})
180179
})
181180

@@ -188,11 +187,10 @@ describe('Transaction', function() {
188187

189188
it('returns true for valid signature', function(){
190189
var key = ECKey.fromWIF('L44f7zxJ5Zw4EK9HZtyAnzCYz2vcZ5wiJf9AuwhJakiV4xVkxBeb')
191-
var pub = key.pub.toBuffer()
192-
var script = prevTx.outs[0].script.buffer
190+
var script = prevTx.outs[0].script
193191
var sig = validTx.ins[0].script.chunks[0]
194192

195-
assert.equal(validTx.validateSig(0, script, sig, pub), true)
193+
assert.equal(validTx.validateSig(0, script, key.pub, sig), true)
196194
})
197195
})
198196

0 commit comments

Comments
 (0)