signInput should test against keys.

This commit is contained in:
Christopher Jeffrey 2016-01-12 02:55:35 -08:00
parent 645a3a59b2
commit abc1f2b610

View File

@ -215,7 +215,7 @@ TX.prototype.scriptInput = function scriptInput(index, pub, redeem) {
// Sign the now-built scriptSigs
TX.prototype.signInput = function signInput(index, key, type) {
var input, s, hash, signature;
var len, redeem, m, n, keys, pub, pubn, ki, signatures, i;
var len, redeem, m, n, keys, pub, pubn, pkh, pkhn, ki, signatures, i;
if (typeof index !== 'number')
index = this.inputs.indexOf(index);
@ -254,6 +254,12 @@ TX.prototype.signInput = function signInput(index, key, type) {
// Add the sighash as a single byte to the signature
signature = signature.concat(type);
// Get compressed and uncompressed pubkeys.
pub = key.getPublic(true, 'array');
pubn = key.getPublic(false, 'array');
pkh = bcoin.wallet.key2hash(pub);
pkhn = bcoin.wallet.key2hash(pubn);
// Script length, needed for multisig
len = input.script.length;
@ -267,13 +273,32 @@ TX.prototype.signInput = function signInput(index, key, type) {
// Add signatures.
if (bcoin.script.isPubkey(s)) {
// P2PK
if (Array.isArray(input.script[0]) && !input.script[0].length)
input.script[0] = signature;
if (!Array.isArray(input.script[0]))
return false;
if (input.script[0].length)
return true;
// Make sure the pubkey is ours.
if (!utils.isEqual(s[0], pub) && !utils.isEqual(s[0], pubn))
return false;
input.script[0] = signature;
return true;
} else if (bcoin.script.isPubkeyhash(s)) {
// P2PKH
if (Array.isArray(input.script[0]) && !input.script[0].length)
input.script[0] = signature;
if (!Array.isArray(input.script[0]))
return false;
if (input.script[0].length)
return true;
// Make sure the pubkey hash is ours.
if (!utils.isEqual(s[2], pkh) && !utils.isEqual(s[2], pkhn))
return false;
input.script[0] = signature;
return true;
} else if (bcoin.script.isMultisig(s)) {
// Multisig
@ -315,8 +340,6 @@ TX.prototype.signInput = function signInput(index, key, type) {
// Grab the redeem script's keys to figure
// out where our key should go.
keys = s.slice(1, -2);
pub = key.getPublic(true, 'array');
pubn = key.getPublic(false, 'array');
// Find the key index so we can place
// the signature in the same index.