Transaction: deprecate Tx signing methods

This commit is contained in:
Daniel Cousens 2014-07-25 16:11:45 +10:00
parent 897bbf4eb4
commit 884fd542fe
3 changed files with 28 additions and 14 deletions

View file

@ -162,7 +162,17 @@ Transaction.prototype.toHex = function() {
* hashType, serializes and finally hashes the result. This hash can then be * hashType, serializes and finally hashes the result. This hash can then be
* used to sign the transaction input in question. * used to sign the transaction input in question.
*/ */
Transaction.prototype.hashForSignature = function(prevOutScript, inIndex, hashType) { Transaction.prototype.hashForSignature = function(inIndex, prevOutScript, hashType) {
// FIXME: remove in 2.x.y
if (arguments[0] instanceof Script) {
console.warn('hashForSignature(prevOutScript, inIndex, ...) has been deprecated. Use hashForSignature(inIndex, prevOutScript, ...)')
// swap the arguments (must be stored in tmp, arguments is special)
var tmp = arguments[0]
inIndex = arguments[1]
prevOutScript = tmp
}
assert(inIndex >= 0, 'Invalid vin index') assert(inIndex >= 0, 'Invalid vin index')
assert(inIndex < this.ins.length, 'Invalid vin index') assert(inIndex < this.ins.length, 'Invalid vin index')
assert(prevOutScript instanceof Script, 'Invalid Script object') assert(prevOutScript instanceof Script, 'Invalid Script object')
@ -296,35 +306,39 @@ Transaction.fromHex = function(hex) {
return Transaction.fromBuffer(new Buffer(hex, 'hex')) return Transaction.fromBuffer(new Buffer(hex, 'hex'))
} }
/** Transaction.prototype.setInputScript = function(index, script) {
* Signs a pubKeyHash output at some index with the given key this.ins[index].script = script
*/ }
// FIXME: remove in 2.x.y
Transaction.prototype.sign = function(index, privKey, hashType) { Transaction.prototype.sign = function(index, privKey, hashType) {
console.warn("Transaction.prototype.sign is deprecated. Use TransactionBuilder instead.")
var prevOutScript = privKey.pub.getAddress().toOutputScript() var prevOutScript = privKey.pub.getAddress().toOutputScript()
var signature = this.signInput(index, prevOutScript, privKey, hashType) var signature = this.signInput(index, prevOutScript, privKey, hashType)
// FIXME: Assumed prior TX was pay-to-pubkey-hash
var scriptSig = scripts.pubKeyHashInput(signature, privKey.pub) var scriptSig = scripts.pubKeyHashInput(signature, privKey.pub)
this.setInputScript(index, scriptSig) this.setInputScript(index, scriptSig)
} }
// FIXME: remove in 2.x.y
Transaction.prototype.signInput = function(index, prevOutScript, privKey, hashType) { Transaction.prototype.signInput = function(index, prevOutScript, privKey, hashType) {
console.warn("Transaction.prototype.signInput is deprecated. Use TransactionBuilder instead.")
hashType = hashType || Transaction.SIGHASH_ALL hashType = hashType || Transaction.SIGHASH_ALL
var hash = this.hashForSignature(prevOutScript, index, hashType) var hash = this.hashForSignature(index, prevOutScript, hashType)
var signature = privKey.sign(hash) var signature = privKey.sign(hash)
return signature.toScriptSignature(hashType) return signature.toScriptSignature(hashType)
} }
Transaction.prototype.setInputScript = function(index, script) { // FIXME: remove in 2.x.y
this.ins[index].script = script
}
// FIXME: could be validateInput(index, prevTxOut, pub)
Transaction.prototype.validateInput = function(index, prevOutScript, pubKey, buffer) { Transaction.prototype.validateInput = function(index, prevOutScript, pubKey, buffer) {
console.warn("Transaction.prototype.validateInput is deprecated. Use TransactionBuilder instead.")
var parsed = ECSignature.parseScriptSignature(buffer) var parsed = ECSignature.parseScriptSignature(buffer)
var hash = this.hashForSignature(prevOutScript, index, parsed.hashType) var hash = this.hashForSignature(index, prevOutScript, parsed.hashType)
return pubKey.verify(hash, parsed.signature) return pubKey.verify(hash, parsed.signature)
} }

View file

@ -183,7 +183,7 @@ describe('Bitcoin-core', function() {
var actualHash var actualHash
try { try {
actualHash = transaction.hashForSignature(script, inIndex, hashType) actualHash = transaction.hashForSignature(inIndex, script, hashType)
} catch (e) { } catch (e) {
// don't fail if we don't support it yet, TODO // don't fail if we don't support it yet, TODO
if (!e.message.match(/not yet supported/)) throw e if (!e.message.match(/not yet supported/)) throw e

View file

@ -221,7 +221,7 @@ describe('Transaction', function() {
// TODO: // TODO:
// hashForSignature: [Function], // hashForSignature: [Function],
// FIXME: could be better // FIXME: remove in 2.x.y
describe('signInput/validateInput', function() { describe('signInput/validateInput', function() {
it('works for multi-sig redeem script', function() { it('works for multi-sig redeem script', function() {
var tx = new Transaction() var tx = new Transaction()