replace ECKey/ECPubKey with ECPair
This commit is contained in:
parent
31832293dd
commit
4c8b0f38ea
20 changed files with 266 additions and 670 deletions
|
@ -1,9 +1,10 @@
|
|||
var assert = require('assert')
|
||||
var bufferutils = require('./bufferutils')
|
||||
var ops = require('./opcodes')
|
||||
var scripts = require('./scripts')
|
||||
|
||||
var Address = require('./address')
|
||||
var ECPubKey = require('./ecpubkey')
|
||||
var ECPair = require('./ecpair')
|
||||
var ECSignature = require('./ecsignature')
|
||||
var Script = require('./script')
|
||||
var Transaction = require('./transaction')
|
||||
|
@ -33,9 +34,9 @@ function extractInput (txIn) {
|
|||
case 'pubkeyhash': {
|
||||
parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0])
|
||||
hashType = parsed.hashType
|
||||
pubKeys = [ECPubKey.fromBuffer(scriptSig.chunks[1])]
|
||||
pubKeys = scriptSig.chunks.slice(1)
|
||||
signatures = [parsed.signature]
|
||||
prevOutScript = pubKeys[0].getAddress().toOutputScript()
|
||||
prevOutScript = ECPair.fromPublicKeyBuffer(pubKeys[0]).getAddress().toOutputScript()
|
||||
|
||||
break
|
||||
}
|
||||
|
@ -46,7 +47,7 @@ function extractInput (txIn) {
|
|||
signatures = [parsed.signature]
|
||||
|
||||
if (redeemScript) {
|
||||
pubKeys = [ECPubKey.fromBuffer(redeemScript.chunks[0])]
|
||||
pubKeys = redeemScript.chunks.slice(0, 1)
|
||||
}
|
||||
|
||||
break
|
||||
|
@ -63,7 +64,7 @@ function extractInput (txIn) {
|
|||
})
|
||||
|
||||
if (redeemScript) {
|
||||
pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer)
|
||||
pubKeys = redeemScript.chunks.slice(1, -2)
|
||||
}
|
||||
|
||||
break
|
||||
|
@ -141,12 +142,12 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
|
|||
// if we can, extract pubKey information
|
||||
switch (prevOutType) {
|
||||
case 'multisig': {
|
||||
input.pubKeys = prevOutScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer)
|
||||
input.pubKeys = prevOutScript.chunks.slice(1, -2)
|
||||
break
|
||||
}
|
||||
|
||||
case 'pubkey': {
|
||||
input.pubKeys = prevOutScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer)
|
||||
input.pubKeys = prevOutScript.chunks.slice(0, 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -280,7 +281,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
|
|||
return tx
|
||||
}
|
||||
|
||||
TransactionBuilder.prototype.sign = function (index, privKey, redeemScript, hashType) {
|
||||
TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hashType) {
|
||||
assert(index in this.inputs, 'No input at index: ' + index)
|
||||
hashType = hashType || Transaction.SIGHASH_ALL
|
||||
|
||||
|
@ -292,6 +293,8 @@ TransactionBuilder.prototype.sign = function (index, privKey, redeemScript, hash
|
|||
input.scriptType &&
|
||||
input.signatures
|
||||
|
||||
var kpPubKey = keyPair.getPublicKeyBuffer()
|
||||
|
||||
// are we almost ready to sign?
|
||||
if (canSign) {
|
||||
// if redeemScript was provided, enforce consistency
|
||||
|
@ -319,21 +322,21 @@ TransactionBuilder.prototype.sign = function (index, privKey, redeemScript, hash
|
|||
var pubKeys = []
|
||||
switch (scriptType) {
|
||||
case 'multisig': {
|
||||
pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer)
|
||||
pubKeys = redeemScript.chunks.slice(1, -2)
|
||||
break
|
||||
}
|
||||
|
||||
case 'pubkeyhash': {
|
||||
var pkh1 = redeemScript.chunks[2]
|
||||
var pkh2 = privKey.pub.getAddress().hash
|
||||
var pkh2 = keyPair.getAddress().hash
|
||||
|
||||
assert.deepEqual(pkh1, pkh2, 'privateKey cannot sign for this input')
|
||||
pubKeys = [privKey.pub]
|
||||
pubKeys = [kpPubKey]
|
||||
break
|
||||
}
|
||||
|
||||
case 'pubkey': {
|
||||
pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer)
|
||||
pubKeys = redeemScript.chunks.slice(0, 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -357,9 +360,9 @@ TransactionBuilder.prototype.sign = function (index, privKey, redeemScript, hash
|
|||
|
||||
// we know nothin' Jon Snow, assume pubKeyHash
|
||||
} else {
|
||||
input.prevOutScript = privKey.pub.getAddress().toOutputScript()
|
||||
input.prevOutScript = keyPair.getAddress().toOutputScript()
|
||||
input.prevOutType = 'pubkeyhash'
|
||||
input.pubKeys = [privKey.pub]
|
||||
input.pubKeys = [kpPubKey]
|
||||
input.scriptType = input.prevOutType
|
||||
}
|
||||
}
|
||||
|
@ -378,10 +381,11 @@ TransactionBuilder.prototype.sign = function (index, privKey, redeemScript, hash
|
|||
|
||||
input.signatures = input.pubKeys.map(function (pubKey) {
|
||||
var match
|
||||
var keyPair2 = ECPair.fromPublicKeyBuffer(pubKey)
|
||||
|
||||
// check for any matching signatures
|
||||
unmatched.some(function (signature, i) {
|
||||
if (!pubKey.verify(signatureHash, signature)) return false
|
||||
if (!keyPair2.verify(signatureHash, signature)) return false
|
||||
match = signature
|
||||
|
||||
// remove matched signature from unmatched
|
||||
|
@ -396,14 +400,15 @@ TransactionBuilder.prototype.sign = function (index, privKey, redeemScript, hash
|
|||
|
||||
// enforce in order signing of public keys
|
||||
assert(input.pubKeys.some(function (pubKey, i) {
|
||||
if (!privKey.pub.Q.equals(pubKey.Q)) return false
|
||||
if (!bufferutils.equal(kpPubKey, pubKey)) return false
|
||||
|
||||
assert(!input.signatures[i], 'Signature already exists')
|
||||
var signature = privKey.sign(signatureHash)
|
||||
|
||||
var signature = keyPair.sign(signatureHash)
|
||||
input.signatures[i] = signature
|
||||
|
||||
return true
|
||||
}, this), 'privateKey cannot sign for this input')
|
||||
}, this), 'key pair cannot sign for this input')
|
||||
}
|
||||
|
||||
module.exports = TransactionBuilder
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue