tx: fix verification
This commit is contained in:
parent
9fd54758a2
commit
113e7dcdc3
@ -142,8 +142,8 @@ Object.keys(exports.opcodes).forEach(function(name) {
|
|||||||
|
|
||||||
// Little-endian hash type
|
// Little-endian hash type
|
||||||
exports.hashType = {
|
exports.hashType = {
|
||||||
all: [ 1, 0, 0, 0 ],
|
all: 1,
|
||||||
none: [ 2, 0, 0, 0 ],
|
none: 2,
|
||||||
single: [ 3, 0, 0, 0 ],
|
single: 3,
|
||||||
anyonecaypay: [ 0x80, 0, 0, 0 ],
|
anyonecaypay: 0x80
|
||||||
};
|
};
|
||||||
|
|||||||
@ -115,19 +115,30 @@ script.execute = function execute(s, stack, tx) {
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
var res = bcoin.utils.isEqual(stack.pop(), stack.pop());
|
var res = bcoin.utils.isEqual(stack.pop(), stack.pop());
|
||||||
stack.push([ res ? 1 : 0 ]);
|
if (o === 'eqverify') {
|
||||||
if (!res && o === 'eqverify')
|
if (!res)
|
||||||
return false;
|
return false;
|
||||||
|
} else {
|
||||||
|
stack.push([ res ? 1 : 0 ]);
|
||||||
|
}
|
||||||
|
|
||||||
} else if (o === 'checksigverify' || o === 'checksig') {
|
} else if (o === 'checksigverify' || o === 'checksig') {
|
||||||
if (!tx || stack.length < 2)
|
if (!tx || stack.length < 2)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var pub = stack.pop();
|
var pub = stack.pop();
|
||||||
var sig = stack.pop();
|
var sig = stack.pop();
|
||||||
var res = bcoin.ecdsa.verify(tx, sig, pub);
|
var type = sig.pop();
|
||||||
stack.push([ res ? 1 : 0 ]);
|
if (type !== 1)
|
||||||
if (!res && o ==='checksigverify')
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
var res = bcoin.ecdsa.verify(tx, sig, pub);
|
||||||
|
if (o === 'checksigverify') {
|
||||||
|
if (!res)
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
stack.push([ res ? 1 : 0 ]);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Unknown operation
|
// Unknown operation
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@ -121,7 +121,9 @@ TX.prototype.subscriptHash = function subscriptHash(index, s, type) {
|
|||||||
input.script = index === i ? s : [];
|
input.script = index === i ? s : [];
|
||||||
});
|
});
|
||||||
var verifyStr = copy.render();
|
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);
|
var hash = utils.dsha256(verifyStr);
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
@ -138,6 +140,10 @@ TX.prototype.validate = function validate() {
|
|||||||
var stack = [];
|
var stack = [];
|
||||||
bcoin.script.execute(input.script, stack);
|
bcoin.script.execute(input.script, stack);
|
||||||
var prev = input.out.tx.outputs[input.out.index].script;
|
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);
|
}, this);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -81,7 +81,7 @@ Wallet.prototype.sign = function sign(tx, type) {
|
|||||||
var signature = bcoin.ecdsa.sign(hash, this.key).toDER();
|
var signature = bcoin.ecdsa.sign(hash, this.key).toDER();
|
||||||
|
|
||||||
input.script = [
|
input.script = [
|
||||||
signature,
|
signature.concat(bcoin.protocol.constants.hashType[type]),
|
||||||
pub
|
pub
|
||||||
];
|
];
|
||||||
}, this);
|
}, this);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user