From 697dea673a6d9a6ad8c2e4187eeb1732324da186 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 9 Jun 2014 17:28:56 -0300 Subject: [PATCH 1/2] add more tests and fix some bugs --- js/models/core/PublicKeyRing.js | 16 ++-- js/models/core/Wallet.js | 4 +- js/models/core/WalletFactory.js | 1 + test/test.TxProposals.js | 1 + test/test.Wallet.js | 130 +++++++++++++++++++++++++++----- test/test.WalletFactory.js | 4 + 6 files changed, 129 insertions(+), 27 deletions(-) diff --git a/js/models/core/PublicKeyRing.js b/js/models/core/PublicKeyRing.js index 38fc912e0..f6f3909a0 100644 --- a/js/models/core/PublicKeyRing.js +++ b/js/models/core/PublicKeyRing.js @@ -241,24 +241,26 @@ PublicKeyRing.prototype.getRedeemScriptMap = function () { return ret; }; -PublicKeyRing.prototype._checkInPRK = function(inPKR, ignoreId) { +PublicKeyRing.prototype._checkInPKR = function(inPKR, ignoreId) { 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) - throw new Error('inPRK network mismatch'); + if (this.network.name !== inPKR.network.name) { + throw new Error('inPKR network mismatch. Should be '+this.network.name + + ' and found '+inPKR.network.name); + } if ( 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 ( 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) { var hasChanged = false; - this._checkInPRK(inPKR, ignoreId); + this._checkInPKR(inPKR, ignoreId); if (this.indexes.merge(inPKR.indexes)) hasChanged = true; diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 202cdff91..78e79e8ee 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -32,6 +32,7 @@ function Wallet(opts) { this.id = opts.id || Wallet.getRandomId(); this.name = opts.name; this.netKey = opts.netKey || SecureRandom.getRandomBuffer(8).toString('base64'); + this.networkName = opts.networkName; // Renew token every 24hs if (opts.tokenTime && new Date().getTime() - opts.tokenTime < 86400000) { @@ -318,7 +319,7 @@ Wallet.prototype.toObj = function() { opts: optsObj, publicKeyRing: this.publicKeyRing.toObj(), txProposals: this.txProposals.toObj(), - privateKey: this.privateKey.toObj() + privateKey: this.privateKey?this.privateKey.toObj():undefined }; return walletObj; @@ -459,7 +460,6 @@ Wallet.prototype.sign = function(ntxid, cb) { var txp = self.txProposals.txps[ntxid]; if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) { if (cb) cb(false); - throw new Error('Invalid transaction to sign: '+ntxid); } var pkr = self.publicKeyRing; diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index 58054502b..c94537b33 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -18,6 +18,7 @@ var Wallet = require('./Wallet'); function WalletFactory(config, version) { var self = this; config = config || {}; + console.log('asd'); this.storage = new Storage(config.storage); this.network = new Network(config.network); diff --git a/test/test.TxProposals.js b/test/test.TxProposals.js index 35360d5c7..78bd50196 100644 --- a/test/test.TxProposals.js +++ b/test/test.TxProposals.js @@ -395,6 +395,7 @@ var _dumpChunks = function (scriptSig, label) { tx = w.txps[k].builder.build(); tx.isComplete().should.equal(false); tx.countInputMissingSignatures(0).should.equal(1); + }); diff --git a/test/test.Wallet.js b/test/test.Wallet.js index 925d7df5c..1cb665281 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -8,6 +8,8 @@ var Wallet = require('../js/models/core/Wallet'); var Storage = require('./mocks/FakeStorage'); var Network = require('./mocks/FakeNetwork'); var Blockchain = require('./mocks/FakeBlockchain'); +var bitcore = bitcore || require('bitcore'); +var TransactionBuilder = bitcore.TransactionBuilder; var addCopayers = function(w) { for (var i = 0; i < 4; i++) { @@ -22,10 +24,6 @@ describe('Wallet model', function() { totalCopayers: 5, spendUnconfirmed: 1, reconnectDelay: 100, - blockchain: { - host: 'test.insight.is', - port: 80 - }, networkName: 'testnet', }; @@ -241,14 +239,14 @@ describe('Wallet model', function() { var w = createW2(); var testTime = 1000; var callCount = 0; - var cT=w.reconnectDelay; - var t=0; + var cT = w.reconnectDelay; + var t = 0; do { callCount++; t += cT; cT *= 2; - } while (t Date: Mon, 9 Jun 2014 17:52:08 -0300 Subject: [PATCH 2/2] add test for sendTx --- js/models/core/Wallet.js | 6 +++--- js/models/core/WalletFactory.js | 1 - test/test.Wallet.js | 27 +++++++++++++++++++++------ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 78e79e8ee..5e32c1f6b 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -487,17 +487,17 @@ Wallet.prototype.sendTx = function(ntxid, cb) { var tx = txp.builder.build(); if (!tx.isComplete()) return; - this.log('[Wallet.js.231] BROADCASTING TX!!!'); //TODO + this.log('Broadcasting Transaction'); var scriptSig = tx.ins[0].getScript(); var size = scriptSig.serialize().length; var txHex = tx.serialize().toString('hex'); - this.log('[Wallet.js.261:txHex:]', txHex); //TODO + this.log('Raw transaction: ', txHex); var self = this; this.blockchain.sendRawTransaction(txHex, function(txid) { - self.log('BITCOIND txid:', txid); //TODO + self.log('BITCOIND txid:', txid); if (txid) { self.txProposals.setSent(ntxid, txid); self.sendTxProposals(null, ntxid); diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index c94537b33..58054502b 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -18,7 +18,6 @@ var Wallet = require('./Wallet'); function WalletFactory(config, version) { var self = this; config = config || {}; - console.log('asd'); this.storage = new Storage(config.storage); this.network = new Network(config.network); diff --git a/test/test.Wallet.js b/test/test.Wallet.js index 1cb665281..a243c6850 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -34,8 +34,10 @@ describe('Wallet model', function() { throw (); }); - var createW = function(netKey) { + var createW = function(netKey, N) { + var c = JSON.parse(JSON.stringify(config)); + if (!N) N = c.totalCopayers; if (netKey) c.netKey = netKey; c.privateKey = new copay.PrivateKey({ @@ -44,8 +46,8 @@ describe('Wallet model', function() { c.publicKeyRing = new copay.PublicKeyRing({ networkName: c.networkName, - requiredCopayers: c.requiredCopayers, - totalCopayers: c.totalCopayers, + requiredCopayers: Math.min(N, c.requiredCopayers), + totalCopayers: N, }); var copayerEPK = c.privateKey.deriveBIP45Branch().extendedPublicKeyString() c.publicKeyRing.addCopayer(copayerEPK); @@ -101,14 +103,15 @@ describe('Wallet model', function() { "confirmations": 7 }]; - var createW2 = function(privateKeys) { + var createW2 = function(privateKeys, N) { + if (!N) N = 3; var netKey = 'T0FbU2JLby0='; - var w = createW(netKey); + var w = createW(netKey, N); should.exist(w); var pkr = w.publicKeyRing; - for (var i = 0; i < 4; i++) { + for (var i = 0; i < N-1; i++) { if (privateKeys) { var k = privateKeys[i]; pkr.addCopayer(k ? k.deriveBIP45Branch().extendedPublicKeyString() : null); @@ -459,5 +462,17 @@ describe('Wallet model', function() { w.reject(ntxid); }); }); + it('should create & sign & send a transaction', function(done) { + + var w = createW2(null, 1); + var utxo = createUTXO(w); + w.blockchain.fixUnspent(utxo); + w.createTx(toAddress, amountSatStr, function(ntxid) { + w.sendTx(ntxid, function(txid) { + txid.length.should.equal(64); + done(); + }); + }); + }); });