diff --git a/lib/model/address.js b/lib/model/address.js index 2590caf..ac95374 100644 --- a/lib/model/address.js +++ b/lib/model/address.js @@ -17,6 +17,7 @@ Address.create = function(opts) { x.isChange = opts.isChange; x.path = opts.path; x.publicKeys = opts.publicKeys; + x.network = Bitcore.Address(x.address).toObject().network; return x; }; @@ -26,6 +27,7 @@ Address.fromObj = function(obj) { x.createdOn = obj.createdOn; x.address = obj.address; x.walletId = obj.walletId; + x.network = obj.network; x.isChange = obj.isChange; x.path = obj.path; x.publicKeys = obj.publicKeys; diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index a20599e..f2b0f12 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -36,6 +36,7 @@ TxProposal.create = function(opts) { x.actions = []; x.outputOrder = _.shuffle(_.range(2)); x.fee = null; + x.network = Bitcore.Address(x.toAddress).toObject().network; return x; }; @@ -66,6 +67,7 @@ TxProposal.fromObj = function(obj) { }); x.outputOrder = obj.outputOrder; x.fee = obj.fee; + x.network = obj.network; return x; }; diff --git a/lib/server.js b/lib/server.js index 71caba6..136f3b6 100644 --- a/lib/server.js +++ b/lib/server.js @@ -84,13 +84,12 @@ WalletService.initialize = function(opts, cb) { WalletService.shutDown = function(cb) { - if (initialized) { - storage.disconnect(function(err) { - if (err) return cb(err); - initialized = false; - return cb(); - }); - } + if (!initialized) return cb(); + storage.disconnect(function(err) { + if (err) return cb(err); + initialized = false; + return cb(); + }); }; WalletService.getInstance = function() { diff --git a/scripts/level2mongo.js b/scripts/level2mongo.js index 4157fe6..e7614f8 100644 --- a/scripts/level2mongo.js +++ b/scripts/level2mongo.js @@ -2,7 +2,7 @@ var LevelStorage = require('../lib/storage_leveldb'); var MongoStorage = require('../lib/storage'); - +var Bitcore = require('bitcore'); var level = new LevelStorage({ dbPath: './db', @@ -37,7 +37,7 @@ function run(cb) { migrate(data.key, data.value, function(err) { if (err) throw err; pending--; - if (pending==0 && ended) { + if (pending == 0 && ended) { return cb(); } }); @@ -60,11 +60,13 @@ function migrate(key, value, cb) { mongo.db.collection('copayers_lookup').insert(value, cb); } else if (key.match(/!addr!/)) { value.walletId = key.substring(2, key.indexOf('!addr')); + value.network = Bitcore.Address(value.address).toObject().network; mongo.db.collection('addresses').insert(value, cb); } else if (key.match(/!not!/)) { mongo.db.collection('notifications').insert(value, cb); } else if (key.match(/!p?txp!/)) { value.isPending = key.indexOf('!ptxp!') != -1; + value.network = Bitcore.Address(value.toAddress).toObject().network; mongo.db.collection('txs').insert(value, cb); } else if (key.match(/!main$/)) { mongo.db.collection('wallets').insert(value, cb); diff --git a/test/integration/server.js b/test/integration/server.js index 165fc31..641204e 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -80,10 +80,16 @@ helpers.getSignedCopayerOpts = function(opts) { return opts; }; -helpers.createAndJoinWallet = function(m, n, cb) { +helpers.createAndJoinWallet = function(m, n, opts, cb) { + if (_.isFunction(opts)) { + cb = opts; + opts = {}; + } + opts = opts || {}; + var server = new WalletService(); var copayerIds = []; - var offset = helpers.offset || 0; + var offset = opts.offset || 0; var walletOpts = { name: 'a wallet', @@ -241,10 +247,7 @@ describe('Wallet service', function() { WalletService.initialize({ storage: storage, blockchainExplorer: blockchainExplorer, - }, function() { - helpers.offset = 0; - done(); - }); + }, done); }); }); after(function(done) { @@ -673,6 +676,8 @@ describe('Wallet service', function() { server.createAddress({}, function(err, address) { should.not.exist(err); address.should.exist; + address.walletId.should.equal(wallet.id); + address.network.should.equal('livenet'); address.address.should.equal('3KxttbKQQPWmpsnXZ3rB4mgJTuLnVR7frg'); address.isChange.should.be.false; address.path.should.equal('m/2147483647/0/0'); @@ -877,6 +882,7 @@ describe('Wallet service', function() { should.not.exist(err); should.exist(tx); tx.walletId.should.equal(wallet.id); + tx.network.should.equal('livenet'); tx.creatorId.should.equal(wallet.copayers[0].id); tx.message.should.equal('some message'); tx.isAccepted().should.equal.false; @@ -2138,8 +2144,9 @@ describe('Wallet service', function() { async.series([ function(next) { - helpers.offset = 1; - helpers.createAndJoinWallet(1, 1, function(s, w) { + helpers.createAndJoinWallet(1, 1, { + offset: 1 + }, function(s, w) { server2 = s; wallet2 = w; @@ -3057,10 +3064,7 @@ describe('Blockchain monitor', function() { WalletService.initialize({ storage: storage, blockchainExplorer: blockchainExplorer, - }, function() { - helpers.offset = 0; - done(); - }); + }, done); }); }); afterEach(function() { diff --git a/test/models/address.js b/test/models/address.js new file mode 100644 index 0000000..7a87ae1 --- /dev/null +++ b/test/models/address.js @@ -0,0 +1,32 @@ +'use strict'; + +var _ = require('lodash'); +var chai = require('chai'); +var sinon = require('sinon'); +var should = chai.should(); + +var Address = require('../../lib/model/address'); + +describe('Address', function() { + it('should create livenet address', function() { + var x = Address.create({ + address: '3KxttbKQQPWmpsnXZ3rB4mgJTuLnVR7frg', + walletId: '123', + isChange: false, + path: 'm/0/1', + publicKeys: ['123', '456'], + }); + should.exist(x.createdOn); + x.network.should.equal('livenet'); + }); + it('should create testnet address', function() { + var x = Address.create({ + address: 'mp5xaa4uBj16DJt1fuA3D9fejHuCzeb7hj', + walletId: '123', + isChange: false, + path: 'm/0/1', + publicKeys: ['123', '456'], + }); + x.network.should.equal('testnet'); + }); +}); diff --git a/test/models/txproposal.js b/test/models/txproposal.js index 142291e..da85a5e 100644 --- a/test/models/txproposal.js +++ b/test/models/txproposal.js @@ -94,6 +94,7 @@ var aTXP = function() { "walletId": "1", "creatorId": "1", "toAddress": "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7", + "network": "livenet", "amount": 50000000, "message": 'some message', "proposalSignature": '7035022100896aeb8db75fec22fddb5facf791927a996eb3aee23ee6deaa15471ea46047de02204c0c33f42a9d3ff93d62738712a8c8a5ecd21b45393fdd144e7b01b5a186f1f9', diff --git a/test/storage.js b/test/storage.js index 5f5ff87..f66ad46 100644 --- a/test/storage.js +++ b/test/storage.js @@ -133,6 +133,7 @@ describe('Storage', function() { proposals = _.map(_.range(4), function(i) { var tx = Model.TxProposal.create({ walletId: '123', + toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', creatorId: wallet.copayers[0].id, amount: i + 100, });