tx: fix verification

This commit is contained in:
Fedor Indutny 2014-05-05 04:30:39 +04:00
parent 9fd54758a2
commit 113e7dcdc3
4 changed files with 30 additions and 13 deletions

View File

@ -142,8 +142,8 @@ Object.keys(exports.opcodes).forEach(function(name) {
// Little-endian hash type
exports.hashType = {
all: [ 1, 0, 0, 0 ],
none: [ 2, 0, 0, 0 ],
single: [ 3, 0, 0, 0 ],
anyonecaypay: [ 0x80, 0, 0, 0 ],
all: 1,
none: 2,
single: 3,
anyonecaypay: 0x80
};

View File

@ -115,19 +115,30 @@ script.execute = function execute(s, stack, tx) {
return false;
var res = bcoin.utils.isEqual(stack.pop(), stack.pop());
stack.push([ res ? 1 : 0 ]);
if (!res && o === 'eqverify')
return false;
if (o === 'eqverify') {
if (!res)
return false;
} else {
stack.push([ res ? 1 : 0 ]);
}
} else if (o === 'checksigverify' || o === 'checksig') {
if (!tx || stack.length < 2)
return false;
var pub = stack.pop();
var sig = stack.pop();
var res = bcoin.ecdsa.verify(tx, sig, pub);
stack.push([ res ? 1 : 0 ]);
if (!res && o ==='checksigverify')
var type = sig.pop();
if (type !== 1)
return false;
var res = bcoin.ecdsa.verify(tx, sig, pub);
if (o === 'checksigverify') {
if (!res)
return false;
} else {
stack.push([ res ? 1 : 0 ]);
}
} else {
// Unknown operation
return false;

View File

@ -121,7 +121,9 @@ TX.prototype.subscriptHash = function subscriptHash(index, s, type) {
input.script = index === i ? s : [];
});
var verifyStr = copy.render();
verifyStr = verifyStr.concat(bcoin.protocol.constants.hashType[type]);
verifyStr = verifyStr.concat(
bcoin.protocol.constants.hashType[type], 0, 0, 0
);
var hash = utils.dsha256(verifyStr);
return hash;
@ -138,6 +140,10 @@ TX.prototype.validate = function validate() {
var stack = [];
bcoin.script.execute(input.script, stack);
var prev = input.out.tx.outputs[input.out.index].script;
return bcoin.script.execute(prev, stack, hash);
var res = bcoin.script.execute(prev, stack, hash);
if (!res)
return false;
return stack.length > 0 && utils.isEqual(stack.pop(), [ 1 ]);
}, this);
};

View File

@ -81,7 +81,7 @@ Wallet.prototype.sign = function sign(tx, type) {
var signature = bcoin.ecdsa.sign(hash, this.key).toDER();
input.script = [
signature,
signature.concat(bcoin.protocol.constants.hashType[type]),
pub
];
}, this);