complete validation tests for txProposal SIGHASH

This commit is contained in:
Manuel Araoz 2014-07-24 10:42:47 -03:00
commit b5c144d48c
4 changed files with 97 additions and 5 deletions

View file

@ -16,6 +16,8 @@ var Network = require('./mocks/FakeNetwork');
var Blockchain = require('./mocks/FakeBlockchain');
var bitcore = bitcore || require('bitcore');
var TransactionBuilder = bitcore.TransactionBuilder;
var Transaction = bitcore.Transaction;
var Address = bitcore.Address;
var addCopayers = function(w) {
for (var i = 0; i < 4; i++) {
@ -1011,4 +1013,75 @@ describe('Wallet model', function() {
copayConfig.forceNetwork = backup;
});
});
describe('validate txProposals', function() {
var a1 = 'n1pKARYYUnZwxBuGj3y7WqVDu6VLN7n971';
var a2 = 'mtxYYJXZJmQc2iJRHQ4RZkfxU5K7TE2qMJ';
var utxos = [{
address: a1,
txid: '2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1',
vout: 1,
scriptPubKey: Address.getScriptPubKeyFor(a1).serialize().toString('hex'),
amount: 0.5,
confirmations: 200
}, {
address: a2,
txid: '88c4520ffd97ea565578afe0b40919120be704b36561c71ba4e450e83cb3c9fd',
vout: 1,
scriptPubKey: Address.getScriptPubKeyFor(a2).serialize().toString('hex'),
amount: 0.5001,
confirmations: 200
}];
var destAddress = 'myuAQcCc1REUgXGsCTiYhZvPPc3XxZ36G1';
var outs = [{
address: destAddress,
amount: 1.0
}];
var testValidate = function(signhash, result, done) {
var w = cachedCreateW();
var spy = sinon.spy();
w.on('txProposalEvent', spy);
w.on('txProposalEvent', function(e) {
e.type.should.equal(result);
done();
});
var opts = {};
opts.signhash = signhash;
var txb = new TransactionBuilder(opts)
.setUnspent(utxos)
.setOutputs(outs)
.sign(['cVBtNonMyTydnS3NnZyipbduXo9KZfF1aUZ3uQHcvJB6UARZbiWG',
'cRVF68hhZp1PUQCdjr2k6aVYb2cn6uabbySDPBizAJ3PXF7vDXTL'
]);
var txp = {
'txProposal': {
'builderObj': txb.toObj()
}
};
w._handleTxProposal('senderID', txp, true);
spy.callCount.should.equal(1);
};
it('should validate for SIGHASH_ALL', function(done) {
var result = 'new';
var signhash = Transaction.SIGHASH_ALL;
testValidate(signhash, result, done);
});
it('should not validate for different SIGHASH_NONE', function(done) {
var result = 'corrupt';
var signhash = Transaction.SIGHASH_NONE;
testValidate(signhash, result, done);
});
it('should not validate for different SIGHASH_SINGLE', function(done) {
var result = 'corrupt';
var signhash = Transaction.SIGHASH_SINGLE;
testValidate(signhash, result, done);
});
it('should not validate for different SIGHASH_ANYONECANPAY', function(done) {
var result = 'corrupt';
var signhash = Transaction.SIGHASH_ANYONECANPAY;
testValidate(signhash, result, done);
});
});
});