mirror of https://github.com/BTCPrivate/copay.git
add more tests and fix some bugs
This commit is contained in:
parent
fabc8aac85
commit
697dea673a
|
@ -241,24 +241,26 @@ PublicKeyRing.prototype.getRedeemScriptMap = function () {
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
PublicKeyRing.prototype._checkInPRK = function(inPKR, ignoreId) {
|
PublicKeyRing.prototype._checkInPKR = function(inPKR, ignoreId) {
|
||||||
|
|
||||||
if (!ignoreId && this.walletId !== inPKR.walletId) {
|
if (!ignoreId && this.walletId !== inPKR.walletId) {
|
||||||
throw new Error('inPRK walletId mismatch');
|
throw new Error('inPKR walletId mismatch');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.network.name !== inPKR.network.name)
|
if (this.network.name !== inPKR.network.name) {
|
||||||
throw new Error('inPRK network mismatch');
|
throw new Error('inPKR network mismatch. Should be '+this.network.name +
|
||||||
|
' and found '+inPKR.network.name);
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.requiredCopayers && inPKR.requiredCopayers &&
|
this.requiredCopayers && inPKR.requiredCopayers &&
|
||||||
(this.requiredCopayers !== inPKR.requiredCopayers))
|
(this.requiredCopayers !== inPKR.requiredCopayers))
|
||||||
throw new Error('inPRK requiredCopayers mismatch '+this.requiredCopayers+'!='+inPKR.requiredCopayers);
|
throw new Error('inPKR requiredCopayers mismatch '+this.requiredCopayers+'!='+inPKR.requiredCopayers);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.totalCopayers && inPKR.totalCopayers &&
|
this.totalCopayers && inPKR.totalCopayers &&
|
||||||
(this.totalCopayers !== inPKR.totalCopayers))
|
(this.totalCopayers !== inPKR.totalCopayers))
|
||||||
throw new Error('inPRK totalCopayers mismatch'+this.totalCopayers+'!='+inPKR.requiredCopayers);
|
throw new Error('inPKR totalCopayers mismatch'+this.totalCopayers+'!='+inPKR.requiredCopayers);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -296,7 +298,7 @@ PublicKeyRing.prototype._mergePubkeys = function(inPKR) {
|
||||||
PublicKeyRing.prototype.merge = function(inPKR, ignoreId) {
|
PublicKeyRing.prototype.merge = function(inPKR, ignoreId) {
|
||||||
var hasChanged = false;
|
var hasChanged = false;
|
||||||
|
|
||||||
this._checkInPRK(inPKR, ignoreId);
|
this._checkInPKR(inPKR, ignoreId);
|
||||||
|
|
||||||
if (this.indexes.merge(inPKR.indexes))
|
if (this.indexes.merge(inPKR.indexes))
|
||||||
hasChanged = true;
|
hasChanged = true;
|
||||||
|
|
|
@ -32,6 +32,7 @@ function Wallet(opts) {
|
||||||
this.id = opts.id || Wallet.getRandomId();
|
this.id = opts.id || Wallet.getRandomId();
|
||||||
this.name = opts.name;
|
this.name = opts.name;
|
||||||
this.netKey = opts.netKey || SecureRandom.getRandomBuffer(8).toString('base64');
|
this.netKey = opts.netKey || SecureRandom.getRandomBuffer(8).toString('base64');
|
||||||
|
this.networkName = opts.networkName;
|
||||||
|
|
||||||
// Renew token every 24hs
|
// Renew token every 24hs
|
||||||
if (opts.tokenTime && new Date().getTime() - opts.tokenTime < 86400000) {
|
if (opts.tokenTime && new Date().getTime() - opts.tokenTime < 86400000) {
|
||||||
|
@ -318,7 +319,7 @@ Wallet.prototype.toObj = function() {
|
||||||
opts: optsObj,
|
opts: optsObj,
|
||||||
publicKeyRing: this.publicKeyRing.toObj(),
|
publicKeyRing: this.publicKeyRing.toObj(),
|
||||||
txProposals: this.txProposals.toObj(),
|
txProposals: this.txProposals.toObj(),
|
||||||
privateKey: this.privateKey.toObj()
|
privateKey: this.privateKey?this.privateKey.toObj():undefined
|
||||||
};
|
};
|
||||||
|
|
||||||
return walletObj;
|
return walletObj;
|
||||||
|
@ -459,7 +460,6 @@ Wallet.prototype.sign = function(ntxid, cb) {
|
||||||
var txp = self.txProposals.txps[ntxid];
|
var txp = self.txProposals.txps[ntxid];
|
||||||
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) {
|
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) {
|
||||||
if (cb) cb(false);
|
if (cb) cb(false);
|
||||||
throw new Error('Invalid transaction to sign: '+ntxid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var pkr = self.publicKeyRing;
|
var pkr = self.publicKeyRing;
|
||||||
|
|
|
@ -18,6 +18,7 @@ var Wallet = require('./Wallet');
|
||||||
function WalletFactory(config, version) {
|
function WalletFactory(config, version) {
|
||||||
var self = this;
|
var self = this;
|
||||||
config = config || {};
|
config = config || {};
|
||||||
|
console.log('asd');
|
||||||
|
|
||||||
this.storage = new Storage(config.storage);
|
this.storage = new Storage(config.storage);
|
||||||
this.network = new Network(config.network);
|
this.network = new Network(config.network);
|
||||||
|
|
|
@ -395,6 +395,7 @@ var _dumpChunks = function (scriptSig, label) {
|
||||||
tx = w.txps[k].builder.build();
|
tx = w.txps[k].builder.build();
|
||||||
tx.isComplete().should.equal(false);
|
tx.isComplete().should.equal(false);
|
||||||
tx.countInputMissingSignatures(0).should.equal(1);
|
tx.countInputMissingSignatures(0).should.equal(1);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,8 @@ var Wallet = require('../js/models/core/Wallet');
|
||||||
var Storage = require('./mocks/FakeStorage');
|
var Storage = require('./mocks/FakeStorage');
|
||||||
var Network = require('./mocks/FakeNetwork');
|
var Network = require('./mocks/FakeNetwork');
|
||||||
var Blockchain = require('./mocks/FakeBlockchain');
|
var Blockchain = require('./mocks/FakeBlockchain');
|
||||||
|
var bitcore = bitcore || require('bitcore');
|
||||||
|
var TransactionBuilder = bitcore.TransactionBuilder;
|
||||||
|
|
||||||
var addCopayers = function(w) {
|
var addCopayers = function(w) {
|
||||||
for (var i = 0; i < 4; i++) {
|
for (var i = 0; i < 4; i++) {
|
||||||
|
@ -22,10 +24,6 @@ describe('Wallet model', function() {
|
||||||
totalCopayers: 5,
|
totalCopayers: 5,
|
||||||
spendUnconfirmed: 1,
|
spendUnconfirmed: 1,
|
||||||
reconnectDelay: 100,
|
reconnectDelay: 100,
|
||||||
blockchain: {
|
|
||||||
host: 'test.insight.is',
|
|
||||||
port: 80
|
|
||||||
},
|
|
||||||
networkName: 'testnet',
|
networkName: 'testnet',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -241,14 +239,14 @@ describe('Wallet model', function() {
|
||||||
var w = createW2();
|
var w = createW2();
|
||||||
var testTime = 1000;
|
var testTime = 1000;
|
||||||
var callCount = 0;
|
var callCount = 0;
|
||||||
var cT=w.reconnectDelay;
|
var cT = w.reconnectDelay;
|
||||||
var t=0;
|
var t = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
callCount++;
|
callCount++;
|
||||||
t += cT;
|
t += cT;
|
||||||
cT *= 2;
|
cT *= 2;
|
||||||
} while (t<testTime);
|
} while (t < testTime);
|
||||||
|
|
||||||
var spy = sinon.spy(w, 'scheduleConnect');
|
var spy = sinon.spy(w, 'scheduleConnect');
|
||||||
w.netStart();
|
w.netStart();
|
||||||
|
@ -272,10 +270,11 @@ describe('Wallet model', function() {
|
||||||
|
|
||||||
it('handle network pubKeyRings correctly', function() {
|
it('handle network pubKeyRings correctly', function() {
|
||||||
var w = createW();
|
var w = createW();
|
||||||
|
w.networkName.should.equal('testnet');
|
||||||
var cepk = [
|
var cepk = [
|
||||||
w.publicKeyRing.toObj().copayersExtPubKeys[0],
|
w.publicKeyRing.toObj().copayersExtPubKeys[0],
|
||||||
'xpub68cA58zTvAve3wDNS7UkY3zaS45iAsqtg6Syxcf4jDR7JtsX4EarofaRHCHqJZJbfyDS1dxuinMTBNiJ6Rx4YEtAvo8StqGGCNa1AV9Zeh9',
|
'tpubDEqHs8LoCB1MDfXs1y2WaLJqPkKsgt8mDoQUFsQ4aKHvho5oFJkF7UrZnfFXKMhA1MuVPwq8a5VhFHvCquYcCVHeCrW4ZCWoDDE9K95e8rP',
|
||||||
'xpub695Ak6GSoEtCQJbwpw17sEPSNqecs15m6FAu7kFk12MCpWyCeMCQ8RmUcCwyfP1KhENZidA6s8nhBWaT1x5n9L8KZshLUscckwbZhSNQtYq',
|
'tpubDEqHs8LoCB1MGGKRyouphPdFNNuay5PBzCuJkgDSiWeAST8m7y4nwPZ7M27mUNWLLPDp6n8kp4P57sd8xHXNnZvap8PxWrUMvXzkxFNgCh7',
|
||||||
];
|
];
|
||||||
var pkrObj = {
|
var pkrObj = {
|
||||||
walletId: w.id,
|
walletId: w.id,
|
||||||
|
@ -300,6 +299,58 @@ describe('Wallet model', function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('handle network txProposals correctly', function() {
|
||||||
|
var w = createW();
|
||||||
|
var txps = {
|
||||||
|
'txProposals': {
|
||||||
|
"txps": [{
|
||||||
|
"seenBy": {
|
||||||
|
"undefined": 1402337282806
|
||||||
|
},
|
||||||
|
"signedBy": {
|
||||||
|
"undefined": 1402337282841
|
||||||
|
},
|
||||||
|
"rejectedBy": {},
|
||||||
|
"sentTs": null,
|
||||||
|
"sentTxid": null,
|
||||||
|
"inputChainPaths": [],
|
||||||
|
"builderObj": {
|
||||||
|
"valueInSat": "1000000000",
|
||||||
|
"valueOutSat": "123456789",
|
||||||
|
"feeSat": "10000",
|
||||||
|
"remainderSat": "876533211",
|
||||||
|
"hashToScriptMap": {
|
||||||
|
"3QjgeBsNeiDkoVQxDAw4sSEe9BVaqhmpnd": "5321025c2951a7e94e39f2c3210fb2d71b7d79044f7daa6b007556a6feb4b2059091e921026c56eea4ef632b24c4efe9d5391ea0e15e306450e5b7db26cd1a7a33d9d4e82821026d06e532385186793121633fb365fa2f0c7246b30590db78cba795641c94734521035051d2b742263bc5257c5a4461e1a6fa32c3f1965f5ab5e8b0f0f80c9e28bc702103ddb0d39c1cd88295e7b81050dcab11ca4419bf64fba3ef9d9130ea64d6604ab055ae",
|
||||||
|
"365dPtmHPAdUUNvPBcnfidYt2Snyaka7AH": "5321020faaf739abda51e822621d42062470c5743a127186369a67fd3303f71727fb1d210237063d3b27c21767054dad203298bfed90cb08dca371950a08b88aa979c33bd22102a3780cb275b6dcb8b379514679387d4578068745f08c5799e4ee1a90aa3a70922102f4fea0cd5bc2418892278f480682690857f4d963deeb95d88febfa255e4ce0b821035d53fa74fee26873c3f5a4acf4dce53ff9d50db59e1ca4acddb82168dd429a3355ae",
|
||||||
|
"372DueknmmjVR3zcWfEfADp8FwJ3ARhGaN": "532102666c2873c9e6f58ca594cd6c8327a1515db32ebadbaff4fe75e63b917560b7d021031ca2d159ae8868a5eef6d67a5a8a5bcc6fb2e8b711669f1c1e8b05327236a3cf2103798a4ce34929cb450bf88557dd90cae538c67f9d0c76314ae18679200be9c17021039624dec23dc1bb628cea55e949ea26225949a3349346a0732fef9def6f1f75292103d1edaadb8555012b752dd7a5c6672c379ce827e4bc0d4e992ee1ab3488b445b255ae",
|
||||||
|
"3GLJbHv7RqPeFk2SqWapqJ3XibVibiKi8f": "5321021a33d48b9f5d3adc41004709313277d4c6969268cf41e3f5b695a934c676829a21031b87321307db7a0f6ea22847a538505188f1a2231eba68867e17c5f0e4434c0721035dee0a6e1df66a6c9c7592ef03aa02bba9b06742134d27bd08f356e33f21259c2103768a109d682a76c09f865912e9e64a8689b03c1a231c613d87ec9bd535fd74c22103911038638b9fc52b2d375ce207c8388bd5ee7f2d54ab9b4148bd406c6d7dcad355ae",
|
||||||
|
"3E1d1z7gJSFeZM2d3k12MJ1jGwrBRy1YTw": "5321027da98ce0407138461f4ad803a1fb67afa4246f06ad0e1256a087daeffd82a8642102e7f87f2b094ec322be2fb99d3ca81fd6ac0ab26ce081e51ab18c507f8b6d1d162102ed5d256036e10568c33e0a15021cc1806e04d7d24c8f020aaae23ec2deecb4302103b6f88231bb5a5f4629e4d097492321f86968c6aeb012196a1fe3b217fe4ae0ac2103f6b5e2c1db3124a5058a0e3e83d5d0c412b9a5b50e2ef97e2b7e1c0af57ab3e355ae",
|
||||||
|
"3QYueyPXq2QDRQCDo7agTimz9673NKV25E": "5321021543292c2942708ccc83354ebf507044b310ed0d33a19e2327a49be53e0f314221024a1a83f8c50f6cad7c134b9cded509dabf196ae49eca157a39ad95798943dc95210292698fbb97e8f6e67296b22b36367ba85c8101fcbc27bb4b00b43623639212ac2102d86980796027a00ba7aa9b53671762c908962654f969f5dec340071bb30e8d7621038fb3fa5e77dafd24c09d819dbdc7c11dca55b350511bf3bc499c09316a89286e55ae",
|
||||||
|
"372BzC1GGjziT8zGYbryja3kF2KaTeobRK": "53210214ec25e5cb42e51883d4e615316748feefe91133fcfc0f93f0b5a24a55e0a347210262336210b3173aa4ca90d292990f463e42bdeb2e73112925dc712c5a2e749bcb210277517855f512564f225e63c650dad7720565aa563901d50743be4b0f0267dcc72102d2777a9faf5d2e2b2363e1270d61021bc2e36e8cb19ca9d29dedbba9a0348b532103a57db80b6ae573e5cef2115e36e73e5ef41f8a099bfb5087d80320c04a7db72e55ae"
|
||||||
|
},
|
||||||
|
"selectedUtxos": [{
|
||||||
|
"address": "3E1d1z7gJSFeZM2d3k12MJ1jGwrBRy1YTw",
|
||||||
|
"scriptPubKey": "a91487264aa41e3df76f3156c7fa587fd7d5b1f7b96b87",
|
||||||
|
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
|
||||||
|
"vout": 1,
|
||||||
|
"amount": 10,
|
||||||
|
"confirmations": 7
|
||||||
|
}],
|
||||||
|
"inputsSigned": 0,
|
||||||
|
"signaturesAdded": 1,
|
||||||
|
"signhash": 1,
|
||||||
|
"spendUnconfirmed": false,
|
||||||
|
"tx": "0100000001c1cf12ab89729d19d3cdec8ae531b5038d56c741006a105d532b3a7afa65c12a01000000fd40010047304402201aad0ea92f929be21d60afb741b76bfcf2aa9614079cb9da76b15b3a1210f07b02205bc5c1895105da3ee682e532d2d49dfd1214fa6123fb9c9d552336f135c77eff0147304402203f02f15bb4ad4bc7d0ca4612fd9e9ae05a1331f3d62f302cf08dba56695f5dcf0220700110562fe228ebba324b120de64a7bbaddf049d67a313f749e9b32ee88787d014cad5321027da98ce0407138461f4ad803a1fb67afa4246f06ad0e1256a087daeffd82a8642102e7f87f2b094ec322be2fb99d3ca81fd6ac0ab26ce081e51ab18c507f8b6d1d162102ed5d256036e10568c33e0a15021cc1806e04d7d24c8f020aaae23ec2deecb4302103b6f88231bb5a5f4629e4d097492321f86968c6aeb012196a1fe3b217fe4ae0ac2103f6b5e2c1db3124a5058a0e3e83d5d0c412b9a5b50e2ef97e2b7e1c0af57ab3e355aeffffffff0215cd5b07000000001976a91434f8e0c5be216025a52addf18a987543cad23f7a88acdbd53e340000000017a914a09f10bc42b61ecf9d3d09550765c228f1cb808a8700000000"
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
"networkName": "testnet"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
w._handleTxProposals('senderID', txps, true);
|
||||||
|
Object.keys(w.txProposals.txps).length.should.equal(1);
|
||||||
|
w.getTxProposals().length.should.equal(1);
|
||||||
|
});
|
||||||
|
|
||||||
var newId = '00bacacafe';
|
var newId = '00bacacafe';
|
||||||
it('handle new connections', function(done) {
|
it('handle new connections', function(done) {
|
||||||
var w = createW();
|
var w = createW();
|
||||||
|
@ -345,25 +396,68 @@ describe('Wallet model', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('should create transaction', function(done) {
|
|
||||||
var w = createW2();
|
|
||||||
w.blockchain.fixUnspent([{
|
|
||||||
'address': w.generateAddress(),
|
// tx handling
|
||||||
|
|
||||||
|
var createUTXO = function(w) {
|
||||||
|
var utxo = [{
|
||||||
'txid': '0be0fb4579911be829e3077202e1ab47fcc12cf3ab8f8487ccceae768e1f95fa',
|
'txid': '0be0fb4579911be829e3077202e1ab47fcc12cf3ab8f8487ccceae768e1f95fa',
|
||||||
'vout': 0,
|
'vout': 0,
|
||||||
'ts': 1402323949,
|
'ts': 1402323949,
|
||||||
'scriptPubKey': '21032ca453c1d9a93b7de8cf3d44d7bb8d52a45dbdf8fff63f69de4e51b740bb1da3ac',
|
|
||||||
'amount': 25.0001,
|
'amount': 25.0001,
|
||||||
'confirmations': 10,
|
'confirmations': 10,
|
||||||
'confirmationsFromCache': false
|
'confirmationsFromCache': false
|
||||||
}]);
|
}];
|
||||||
|
var addr = w.generateAddress().toString();
|
||||||
|
utxo[0].address = addr;
|
||||||
|
utxo[0].scriptPubKey = TransactionBuilder.scriptForAddress(addr).serialize().toString('hex');
|
||||||
|
return utxo;
|
||||||
|
};
|
||||||
var toAddress = 'mjfAe7YrzFujFf8ub5aUrCaN5GfSABdqjh';
|
var toAddress = 'mjfAe7YrzFujFf8ub5aUrCaN5GfSABdqjh';
|
||||||
var amountSatStr = '1000';
|
var amountSatStr = '1000';
|
||||||
|
|
||||||
|
it('should create transaction', function(done) {
|
||||||
|
var w = createW2();
|
||||||
|
var utxo = createUTXO(w);
|
||||||
|
w.blockchain.fixUnspent(utxo);
|
||||||
w.createTx(toAddress, amountSatStr, function(ntxid) {
|
w.createTx(toAddress, amountSatStr, function(ntxid) {
|
||||||
ntxid.length.should.equal(64);
|
ntxid.length.should.equal(64);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
it('should create & sign transaction from received funds', function(done) {
|
||||||
|
var w = createW2();
|
||||||
|
var pk = w.privateKey;
|
||||||
|
w.privateKey = null;
|
||||||
|
var utxo = createUTXO(w);
|
||||||
|
w.blockchain.fixUnspent(utxo);
|
||||||
|
w.createTx(toAddress, amountSatStr, function(ntxid) {
|
||||||
|
w.on('txProposalsUpdated', function() {
|
||||||
|
w.getTxProposals()[0].signedByUs.should.equal(true);
|
||||||
|
w.getTxProposals()[0].rejectedByUs.should.equal(false);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
w.privateKey = pk;
|
||||||
|
w.sign(ntxid, function(success) {
|
||||||
|
success.should.equal(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('should create & reject transaction', function(done) {
|
||||||
|
var w = createW2();
|
||||||
|
w.privateKey = null;
|
||||||
|
var utxo = createUTXO(w);
|
||||||
|
w.blockchain.fixUnspent(utxo);
|
||||||
|
w.createTx(toAddress, amountSatStr, function(ntxid) {
|
||||||
|
w.on('txProposalsUpdated', function() {
|
||||||
|
w.getTxProposals()[0].signedByUs.should.equal(false);
|
||||||
|
w.getTxProposals()[0].rejectedByUs.should.equal(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
w.reject(ntxid);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -13,6 +13,10 @@ var WalletFactory = typeof copay === 'undefined' ? require('soop').load('../js/m
|
||||||
Storage: FakeStorage,
|
Storage: FakeStorage,
|
||||||
}) : copay.WalletFactory;
|
}) : copay.WalletFactory;
|
||||||
|
|
||||||
|
var blanket = require("blanket")({
|
||||||
|
"pattern": "/js/"
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
var addCopayers = function(w) {
|
var addCopayers = function(w) {
|
||||||
for (var i = 0; i < 4; i++) {
|
for (var i = 0; i < 4; i++) {
|
||||||
|
|
Loading…
Reference in New Issue