diff --git a/js/models/core/PrivateKey.js b/js/models/core/PrivateKey.js index 1efe93694..09411ab05 100644 --- a/js/models/core/PrivateKey.js +++ b/js/models/core/PrivateKey.js @@ -20,13 +20,25 @@ function PrivateKey(opts) { PrivateKey.prototype.getId = function() { if (!this.id) { - var path = Structure.IdFullBranch; - var idhk = this.bip.derive(path); - this.id= idhk.eckey.public.toString('hex'); + this.cacheId(); } return this.id; }; +PrivateKey.prototype.getIdPriv = function() { + if (!this.idpriv) { + this.cacheId(); + } + return this.idpriv; +}; + +PrivateKey.prototype.cacheId = function() { + var path = Structure.IdFullBranch; + var idhk = this.bip.derive(path); + this.id = idhk.eckey.public.toString('hex'); + this.idpriv = idhk.eckey.private.toString('hex'); +}; + PrivateKey.prototype.deriveBIP45Branch = function() { if (!this.bip45Branch) { this.bip45Branch = this.bip.derive(Structure.BIP45_PUBLIC_PREFIX); diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 98a19d416..f0e94993d 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -38,7 +38,6 @@ function Wallet(opts) { this.id = opts.id || Wallet.getRandomId(); this.name = opts.name; - this.netKey = opts.netKey || SecureRandom.getRandomBuffer(8).toString('base64'); // Renew token every 24hs if (opts.tokenTime && new Date().getTime() - opts.tokenTime < 86400000) { @@ -216,7 +215,6 @@ Wallet.prototype._optsToObj = function() { requiredCopayers: this.requiredCopayers, totalCopayers: this.totalCopayers, name: this.name, - netKey: this.netKey, version: this.version, }; @@ -235,26 +233,26 @@ Wallet.prototype.getCopayerId = function(index) { Wallet.prototype.getMyCopayerId = function() { - return this.getCopayerId(0); + return this.getCopayerId(0); //copayer id is hex of a public key +}; + +Wallet.prototype.getMyCopayerIdPriv = function() { + return this.privateKey.getIdPriv(); //copayer idpriv is hex of a private key }; Wallet.prototype.getSecret = function() { - var i = new Buffer(this.getMyCopayerId(), 'hex'); - var k = new Buffer(this.netKey, 'base64'); - var b = Buffer.concat([i, k]); - var str = Base58Check.encode(b); + var pubkeybuf = new Buffer(this.getMyCopayerId(), 'hex'); + var str = Base58Check.encode(pubkeybuf); return str; }; Wallet.decodeSecret = function(secretB) { var secret = Base58Check.decode(secretB); - var netKeyBuf = secret.slice(-8); var pubKeyBuf = secret.slice(0, 33); return { - pubKey: pubKeyBuf.toString('hex'), - netKey: netKeyBuf.toString('base64'), + pubKey: pubKeyBuf.toString('hex') } }; @@ -262,7 +260,7 @@ Wallet.prototype._lockIncomming = function() { this.network.lockIncommingConnections(this.publicKeyRing.getAllCopayerIds()); }; -Wallet.prototype.netStart = function() { +Wallet.prototype.netStart = function(callback) { var self = this; var net = this.network; net.removeAllListeners(); @@ -277,11 +275,12 @@ Wallet.prototype.netStart = function() { }); var myId = self.getMyCopayerId(); + var myIdPriv = self.getMyCopayerIdPriv(); var startOpts = { copayerId: myId, + privkey: myIdPriv, token: self.token, - maxPeers: self.totalCopayers, - netKey: this.netKey, + maxPeers: self.totalCopayers }; if (this.publicKeyRing.isComplete()) { diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index df0674297..03d59e2ea 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -222,7 +222,7 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras this.log('\t### PrivateKey Initialized'); var opts = { copayerId: privateKey.getId(), - netKey: s.netKey, + privkey: privateKey.getIdPriv() }; self.network.cleanUp(); self.network.start(opts, function() { diff --git a/js/models/network/WebRTC.js b/js/models/network/WebRTC.js index fad273b1f..169bfe570 100644 --- a/js/models/network/WebRTC.js +++ b/js/models/network/WebRTC.js @@ -1,3 +1,4 @@ +'use strict'; var imports = require('soop').imports(); var EventEmitter= imports.EventEmitter || require('events').EventEmitter; @@ -43,7 +44,7 @@ Network.prototype.cleanUp = function() { this.started = false; this.connectedPeers = []; this.peerId = null; - this.netKey = null; + this.privkey = null; //TODO: hide privkey in a closure this.copayerId = null; this.signingKey = null; this.allowedCopayerIds=null; @@ -125,11 +126,11 @@ Network.prototype._onClose = function(peerID) { Network.prototype.connectToCopayers = function(copayerIds) { var self = this; - var arrayDiff= Network._arrayDiff(copayerIds, this.connectedCopayers()); + var arrayDiff= Network._arrayDiff(copayerIds, self.connectedCopayers()); arrayDiff.forEach(function(copayerId) { - if (this.allowedCopayerIds && !this.allowedCopayerIds[copayerId]) { - this._deletePeer(this.peerFromCopayer(copayerId)); + if (self.allowedCopayerIds && !self.allowedCopayerIds[copayerId]) { + self._deletePeer(self.peerFromCopayer(copayerId)); } else { self.connectTo(copayerId); } @@ -150,11 +151,15 @@ Network.prototype._addConnectedCopayer = function(copayerId, isInbound) { this.emit('connect', copayerId); }; -Network.prototype._onData = function(encStr, isInbound, peerId) { +Network.prototype._onData = function(enchex, isInbound, peerId) { var sig, payload; + var encUint8Array = new Uint8Array(enchex); + var encbuf = new Buffer(encUint8Array); + + var privkey = this.privkey; try { - var data = this._decrypt(encStr); + var data = this._decrypt(privkey, encbuf); payload= JSON.parse(data); } catch (e) { this._deletePeer(peerId); @@ -300,7 +305,9 @@ Network.prototype.start = function(opts, openCallback) { if (this.started) return openCallback(); - this.netKey = opts.netKey; + if (!this.privkey) + this.privkey = opts.privkey; + this.maxPeers = opts.maxPeers || this.maxPeers; if (opts.token) @@ -344,27 +351,15 @@ Network.prototype.getPeer = function() { return this.peer; }; -Network.prototype._encrypt = function(payloadStr) { - var plainText = sjcl.codec.utf8String.toBits(payloadStr); - var p = this.sjclParams; - ct = sjcl.encrypt(this.netKey, plainText, p);//,p, rp); - var c = JSON.parse(ct); - var toSend = { - iv: c.iv, - ct: c.ct, - }; - return JSON.stringify(toSend); +Network.prototype._encrypt = function(pubkey, payload) { + var encrypted = bitcore.ECIES.encrypt(pubkey, payload); + return encrypted; }; -Network.prototype._decrypt = function(encStr) { - var i = JSON.parse(encStr); - for (var k in this.sjclParams) { - i[k] = this.sjclParams[k]; - } - var str= JSON.stringify(i); - var pt = sjcl.decrypt(this.netKey, str); - return pt; +Network.prototype._decrypt = function(privkey, encrypted) { + var decrypted = bitcore.ECIES.decrypt(privkey, encrypted); + return decrypted; }; Network.prototype._sendToOne = function(copayerId, payload, sig, cb) { @@ -379,7 +374,7 @@ Network.prototype._sendToOne = function(copayerId, payload, sig, cb) { }; Network.prototype.send = function(copayerIds, payload, cb) { - if (!payload || !this.netKey) return cb(); + if (!payload) return cb(); var self=this; if (!copayerIds) { @@ -387,20 +382,22 @@ Network.prototype.send = function(copayerIds, payload, cb) { payload.isBroadcast = 1; } + if (typeof copayerIds === 'string') + copayerIds = [copayerIds]; + var sig; var payloadStr = JSON.stringify(payload); - var encPayload = this._encrypt(payloadStr); - if (Array.isArray(copayerIds)) { - var l = copayerIds.length; - var i = 0; - copayerIds.forEach(function(copayerId) { - self._sendToOne(copayerId, encPayload, sig, function () { - if (++i === l && typeof cb === 'function') cb(); - }); + var payloadBuf = new Buffer(payloadStr); + + var l = copayerIds.length; + var i = 0; + copayerIds.forEach(function(copayerId) { + var copayerIdBuf = new Buffer(copayerId, 'hex'); + var encPayload = self._encrypt(copayerIdBuf, payloadBuf); + self._sendToOne(copayerId, encPayload, sig, function () { + if (++i === l && typeof cb === 'function') cb(); }); - } - else if (typeof copayerIds === 'string') - self._sendToOne(copayerIds, encPayload, sig, cb); + }); }; diff --git a/package.json b/package.json index bec53ce4f..8fde93aa3 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,6 @@ "mocha": "1.18.2", "mocha-lcov-reporter": "0.0.1", "node-cryptojs-aes": "0.4.0", - "sinon": "1.9.1", "soop": "0.1.5", "travis-cov": "0.2.5", "uglifyify": "1.2.3" @@ -60,6 +59,7 @@ "dependencies": { "mocha": "^1.18.2", "mocha-lcov-reporter": "0.0.1", - "preconditions": "^1.0.7" + "preconditions": "^1.0.7", + "sinon": "1.9.1" } } diff --git a/test/index.html b/test/index.html index f34a867bf..127df85f3 100644 --- a/test/index.html +++ b/test/index.html @@ -15,11 +15,11 @@ + - diff --git a/test/test.PrivateKey.js b/test/test.PrivateKey.js index 7e002759c..661b6860c 100644 --- a/test/test.PrivateKey.js +++ b/test/test.PrivateKey.js @@ -70,11 +70,6 @@ describe('PrivateKey model', function() { } }); - it('should calculate .id', function () { - var w1 = new PrivateKey(config); - should.exist(w1.getId()); - w1.getId().length.should.equal(66); - }); it('fromObj toObj roundtrip', function () { var w1 = new PrivateKey(config); var o = JSON.parse(JSON.stringify(w1.toObj())) @@ -89,5 +84,45 @@ describe('PrivateKey model', function() { .equal(JSON.stringify(w1.get(1,0).storeObj())); }); + describe('#getId', function() { + it('should calculate the copayerId', function() { + var w1 = new PrivateKey(config); + should.exist(w1.getId()); + w1.getId().length.should.equal(33*2); + }); + }); + + describe('#getIdPriv', function() { + it('should calculate .id', function() { + var w1 = new PrivateKey(config); + should.exist(w1.getIdPriv()); + w1.getIdPriv().length.should.equal(32*2); + }); + }); + + describe('#cacheId', function() { + it('should set .id and .idpriv', function() { + var w1 = new PrivateKey(config); + w1.cacheId(); + var pub = w1.id; + var priv = w1.idpriv; + pub.length.should.equal(33*2); + priv.length.should.equal(32*2); + }); + + it('should set the id equal to the public key of the idpriv private key', function() { + var w1 = new PrivateKey(config); + w1.cacheId(); + var pub = w1.id; + var priv = w1.idpriv; + pub.length.should.equal(33*2); + priv.length.should.equal(32*2); + + var key1 = new bitcore.Key(); + key1.private = new bitcore.Buffer(priv, 'hex'); + key1.regenerateSync(); + key1.public.toString('hex').should.equal(pub); + }); + }); }); diff --git a/test/test.Wallet.js b/test/test.Wallet.js index 16605da98..4c89a54b4 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -97,10 +97,7 @@ describe('Wallet model', function() { should.exist(w.publicKeyRing); should.exist(w.privateKey); should.exist(w.txProposals); - should.exist(w.netKey); should.exist(w.addressBook); - var b = new bitcore.Buffer(w.netKey, 'base64'); - b.toString('hex').length.should.equal(16); }); it('should provide some basic features', function(done) { @@ -284,13 +281,11 @@ describe('Wallet model', function() { it('#getSecret decodeSecret', function() { var w = createW2(); var id = w.getMyCopayerId(); - var nk = w.netKey; var sb = w.getSecret(); should.exist(sb); var s = Wallet.decodeSecret(sb); s.pubKey.should.equal(id); - s.netKey.should.equal(nk); }); it('decodeSecret check', function() { @@ -760,4 +755,49 @@ describe('Wallet model', function() { Object.keys(w.addressBook).length.should.equal(3); }); + it('#getNetworkName', function() { + var w = createW(); + w.getNetworkName().should.equal('testnet'); + }); + + describe('#getMyCopayerId', function() { + it('should call getCopayerId', function() { + //this.timeout(10000); + var w = createW2(); + w.getCopayerId = sinon.spy(); + w.getMyCopayerId(); + w.getCopayerId.calledOnce.should.equal(true); + }); + }); + + describe('#getMyCopayerIdPriv', function() { + it('should call privateKey.getIdPriv', function() { + //this.timeout(10000); + var w = createW2(); + w.privateKey.getIdPriv = sinon.spy(); + w.getMyCopayerIdPriv(); + w.privateKey.getIdPriv.calledOnce.should.equal(true); + }); + }); + + describe('#netStart', function() { + + it('should call Network.start', function() { + //this.timeout(10000); + var w = createW2(); + w.network.start = sinon.spy(); + w.netStart(); + w.network.start.calledOnce.should.equal(true); + }); + + it('should call Network.start with a private key', function() { + //this.timeout(10000); + var w = createW2(); + w.network.start = sinon.spy(); + w.netStart(); + w.network.start.getCall(0).args[0].privkey.length.should.equal(64); + }); + + }); + }); diff --git a/test/test.WalletFactory.js b/test/test.WalletFactory.js index 93adb7812..c9f8bde5c 100644 --- a/test/test.WalletFactory.js +++ b/test/test.WalletFactory.js @@ -80,7 +80,7 @@ describe('WalletFactory model', function() { }); it('#fromObj #toObj round trip', function() { - var o = '{"opts":{"id":"dbfe10c3fae71cea","spendUnconfirmed":1,"requiredCopayers":3,"totalCopayers":5,"netKey":"LppzFYqlgT0=","version":"0.0.5"},"publicKeyRing":{"walletId":"dbfe10c3fae71cea","networkName":"testnet","requiredCopayers":3,"totalCopayers":5,"indexes":{"changeIndex":0,"receiveIndex":0},"copayersExtPubKeys":["tpubD6NzVbkrYhZ4YGK8ZhZ8WVeBXNAAoTYjjpw9twCPiNGrGQYFktP3iVQkKmZNiFnUcAFMJRxJVJF6Nq9MDv2kiRceExJaHFbxUCGUiRhmy97","tpubD6NzVbkrYhZ4YKGDJkzWdQsQV3AcFemaQKiwNhV4RL8FHnBFvinidGdQtP8RKj3h34E65RkdtxjrggZYqsEwJ8RhhN2zz9VrjLnrnwbXYNc","tpubD6NzVbkrYhZ4YkDiewjb32Pp3Sz9WK2jpp37KnL7RCrHAyPpnLfgdfRnTdpn6DTWmPS7niywfgWiT42aJb1J6CjWVNmkgsMCxuw7j9DaGKB","tpubD6NzVbkrYhZ4XEtUAz4UUTWbprewbLTaMhR8NUvSJUEAh4Sidxr6rRPFdqqVRR73btKf13wUjds2i8vVCNo8sbKrAnyoTr3o5Y6QSbboQjk","tpubD6NzVbkrYhZ4Yj9AAt6xUVuGPVd8jXCrEE6V2wp7U3PFh8jYYvVad31b4VUXEYXzSnkco4fktu8r4icBsB2t3pCR3WnhVLedY2hxGcPFLKD"],"nicknameFor":{},"publicKeysCache":{"m/0/1/0":["0314368b8efa07e8c7dad30498d0a7e3aa575db1fef833347c6d381c1a33a17b17","02cfd95f89ab46bd3bd86954dd9f83dbab0cd2e4466dee587e8e4d8d733fc0d748","02568969eb6212fe946450be6c5b3353fc754a40b2cdc4aed501a8976fec371da8","0360f870a088ae0ef1c37035a9b6a462ca8dcdd5da275f4e2dcd19f44b81d3e7e4","0300ad8f1bded838b02e127bb25961fbcee718db2df81f680f889692acdcbdd73d"],"m/0/1/1":["024f97a9adb2fa9306c4e3d9244f5e5355c7e2c6b3dd4122ba804e17dc9729df5d","0214834a5adcbc4ad0f3bbbc1c280b8ac480387fcc9a1fd988c1526ed496d923c4","024e72338bd5e976375d076bd71a9649e9141b4cbfc9e16cb7109b354b3e913a05","0322045ea35c3118aa7ab9f2c9f182b0120956b0aa65cc72b9d093f145327a4b17","030dc2450c72df366c1960739c577a2efd4451070bd78effcb6f71d1bcd7dfc7a8"],"m/0/1/2":["0247de59deb66783b8f9b0c326234a9569d00866c2a73f599e77a4d0cab5cbce8f","0376e49f0ac3647404034aae0dc8dd927c34a634ef24ea36f56a272f75fce9539b","032fbaa2593bd1eea4a46e7ac15f15802cdd1eb65a7d5bc4364ddd9d52f0838234","03a81f2a7e1f7191aa0b0c6e0a4ccefc71edd3564e86014972fe338045f68d5a5a","02eb8a012ea9a709392502cacda6ef5115d6d2319ab470d546d9068ab941621a99"],"m/0/0/0":["036dcbd378b4352120d6b720b6294dd2d0dd02801fcf010bb69dadbec1f3999279","022089eedb85dc45d1efa418e1ea226588deedebc1d85acca15ff72783e33636c0","0388aa5fd432b74c56427396f350d236c3ca8f7b2f62da513ce4c2e6ff04a67e9c","02fc4caa7449db7483d2e1fccdacac6fa2f736278c758af9966402589b5632f13e","02e4a15b885d8b2d586f82fa85d16179644e60a154674bde0ec3004810b1bdab99"],"m/0/0/1":["039afa26b2f341c76c7b3c3d0672438f35ac6ebb67b1ddfefac9cd79b7b24418c1","021acaaf500d431ebc396f50630767b01c91ce98ae48e968775ceaad932b7e3b8e","022a947259c4a9f76d5e95c0849df31d01233df41d0d75d631b89317a48d8cddce","03d38d9f94217da780303d9a8987c86d737ef39683febc0cd6632cddbfa62186fd","0394d2581b307fe2af19721888d922aab58ab198ef88cedf9506177e30d807811e"],"m/0/0/2":["037825ffce15d34f9bd6c02bcda7701826706471a4d6ab5004eb965f98811c2098","023768dd6d3c71b7df5733ccda5b2d8b454d5b4c4179d91a6fda74db8b869a2406","021a79e91f003f308764d43039e9b5d56bc8f33ca2f4d30ec6cc5a37c0d09dc273","02437f1e388b273936319f79a5d22958ef5ebff9c8cd7b6f6f72518445b1e30867","0373b0881cb4fd02baa62589023fdfe9739c6148cf104d907549f2528eb80146f5"]}},"txProposals":{"txps":[],"walletId":"dbfe10c3fae71cea","networkName":"testnet"},"privateKey":{"extendedPrivateKeyString":"tprv8ZgxMBicQKsPeoHLg3tY75z4xLeEe8MqAXLNcRA6J6UTRvHV8VZTXznt9eoTmSk1fwSrwZtMhY3XkNsceJ14h6sCXHSWinRqMSSbY8tfhHi","networkName":"testnet","privateKeyCache":{}},"addressBook":{}}'; + var o = '{"opts":{"id":"dbfe10c3fae71cea","spendUnconfirmed":1,"requiredCopayers":3,"totalCopayers":5,"version":"0.0.5"},"publicKeyRing":{"walletId":"dbfe10c3fae71cea","networkName":"testnet","requiredCopayers":3,"totalCopayers":5,"indexes":{"changeIndex":0,"receiveIndex":0},"copayersExtPubKeys":["tpubD6NzVbkrYhZ4YGK8ZhZ8WVeBXNAAoTYjjpw9twCPiNGrGQYFktP3iVQkKmZNiFnUcAFMJRxJVJF6Nq9MDv2kiRceExJaHFbxUCGUiRhmy97","tpubD6NzVbkrYhZ4YKGDJkzWdQsQV3AcFemaQKiwNhV4RL8FHnBFvinidGdQtP8RKj3h34E65RkdtxjrggZYqsEwJ8RhhN2zz9VrjLnrnwbXYNc","tpubD6NzVbkrYhZ4YkDiewjb32Pp3Sz9WK2jpp37KnL7RCrHAyPpnLfgdfRnTdpn6DTWmPS7niywfgWiT42aJb1J6CjWVNmkgsMCxuw7j9DaGKB","tpubD6NzVbkrYhZ4XEtUAz4UUTWbprewbLTaMhR8NUvSJUEAh4Sidxr6rRPFdqqVRR73btKf13wUjds2i8vVCNo8sbKrAnyoTr3o5Y6QSbboQjk","tpubD6NzVbkrYhZ4Yj9AAt6xUVuGPVd8jXCrEE6V2wp7U3PFh8jYYvVad31b4VUXEYXzSnkco4fktu8r4icBsB2t3pCR3WnhVLedY2hxGcPFLKD"],"nicknameFor":{},"publicKeysCache":{"m/0/1/0":["0314368b8efa07e8c7dad30498d0a7e3aa575db1fef833347c6d381c1a33a17b17","02cfd95f89ab46bd3bd86954dd9f83dbab0cd2e4466dee587e8e4d8d733fc0d748","02568969eb6212fe946450be6c5b3353fc754a40b2cdc4aed501a8976fec371da8","0360f870a088ae0ef1c37035a9b6a462ca8dcdd5da275f4e2dcd19f44b81d3e7e4","0300ad8f1bded838b02e127bb25961fbcee718db2df81f680f889692acdcbdd73d"],"m/0/1/1":["024f97a9adb2fa9306c4e3d9244f5e5355c7e2c6b3dd4122ba804e17dc9729df5d","0214834a5adcbc4ad0f3bbbc1c280b8ac480387fcc9a1fd988c1526ed496d923c4","024e72338bd5e976375d076bd71a9649e9141b4cbfc9e16cb7109b354b3e913a05","0322045ea35c3118aa7ab9f2c9f182b0120956b0aa65cc72b9d093f145327a4b17","030dc2450c72df366c1960739c577a2efd4451070bd78effcb6f71d1bcd7dfc7a8"],"m/0/1/2":["0247de59deb66783b8f9b0c326234a9569d00866c2a73f599e77a4d0cab5cbce8f","0376e49f0ac3647404034aae0dc8dd927c34a634ef24ea36f56a272f75fce9539b","032fbaa2593bd1eea4a46e7ac15f15802cdd1eb65a7d5bc4364ddd9d52f0838234","03a81f2a7e1f7191aa0b0c6e0a4ccefc71edd3564e86014972fe338045f68d5a5a","02eb8a012ea9a709392502cacda6ef5115d6d2319ab470d546d9068ab941621a99"],"m/0/0/0":["036dcbd378b4352120d6b720b6294dd2d0dd02801fcf010bb69dadbec1f3999279","022089eedb85dc45d1efa418e1ea226588deedebc1d85acca15ff72783e33636c0","0388aa5fd432b74c56427396f350d236c3ca8f7b2f62da513ce4c2e6ff04a67e9c","02fc4caa7449db7483d2e1fccdacac6fa2f736278c758af9966402589b5632f13e","02e4a15b885d8b2d586f82fa85d16179644e60a154674bde0ec3004810b1bdab99"],"m/0/0/1":["039afa26b2f341c76c7b3c3d0672438f35ac6ebb67b1ddfefac9cd79b7b24418c1","021acaaf500d431ebc396f50630767b01c91ce98ae48e968775ceaad932b7e3b8e","022a947259c4a9f76d5e95c0849df31d01233df41d0d75d631b89317a48d8cddce","03d38d9f94217da780303d9a8987c86d737ef39683febc0cd6632cddbfa62186fd","0394d2581b307fe2af19721888d922aab58ab198ef88cedf9506177e30d807811e"],"m/0/0/2":["037825ffce15d34f9bd6c02bcda7701826706471a4d6ab5004eb965f98811c2098","023768dd6d3c71b7df5733ccda5b2d8b454d5b4c4179d91a6fda74db8b869a2406","021a79e91f003f308764d43039e9b5d56bc8f33ca2f4d30ec6cc5a37c0d09dc273","02437f1e388b273936319f79a5d22958ef5ebff9c8cd7b6f6f72518445b1e30867","0373b0881cb4fd02baa62589023fdfe9739c6148cf104d907549f2528eb80146f5"]}},"txProposals":{"txps":[],"walletId":"dbfe10c3fae71cea","networkName":"testnet"},"privateKey":{"extendedPrivateKeyString":"tprv8ZgxMBicQKsPeoHLg3tY75z4xLeEe8MqAXLNcRA6J6UTRvHV8VZTXznt9eoTmSk1fwSrwZtMhY3XkNsceJ14h6sCXHSWinRqMSSbY8tfhHi","networkName":"testnet","privateKeyCache":{}},"addressBook":{}}'; var wf = new WalletFactory(config, '0.0.5'); var w = wf.fromObj(JSON.parse(o)); @@ -169,4 +169,21 @@ describe('WalletFactory model', function() { }); }); + describe('#joinCreateSession', function() { + it('should call network.start', function() { + var wf = new WalletFactory(config, '0.0.1'); + wf.network.cleanUp = sinon.spy(); + wf.network.start = sinon.spy(); + wf.joinCreateSession('8WtTuiFTkhP5ao7AF2QErSwV39Cbur6pdMebKzQXFqL59RscXM', 'test', null, undefined); + wf.network.start.calledOnce.should.equal(true); + }); + it('should call network.start with private key', function() { + var wf = new WalletFactory(config, '0.0.1'); + wf.network.cleanUp = sinon.spy(); + wf.network.start = sinon.spy(); + wf.joinCreateSession('8WtTuiFTkhP5ao7AF2QErSwV39Cbur6pdMebKzQXFqL59RscXM', 'test', null, undefined); + wf.network.start.getCall(0).args[0].privkey.length.should.equal(64); //privkey is hex of private key buffer + }); + }); + }); diff --git a/test/test.network.WebRTC.js b/test/test.network.WebRTC.js new file mode 100644 index 000000000..0eb21b247 --- /dev/null +++ b/test/test.network.WebRTC.js @@ -0,0 +1,150 @@ +'use strict'; + +var chai = chai || require('chai'); +var should = chai.should(); +var expect = chai.expect; +var sinon = sinon || require('sinon'); +var bitcore = bitcore || require('bitcore'); +var WebRTC = require('../js/models/network/WebRTC'); + +describe('Network / WebRTC', function() { + + it('should create an instance', function () { + var n = new WebRTC(); + should.exist(n); + }); + + describe('#WebRTC constructor', function() { + + it('should set reconnect attempts', function() { + var n = new WebRTC(); + n.reconnectAttempts.should.equal(3); + }); + + it('should call cleanUp', function() { + var save = WebRTC.prototype.cleanUp; + WebRTC.prototype.cleanUp = sinon.spy(); + var n = new WebRTC(); + n.cleanUp.calledOnce.should.equal(true); + WebRTC.prototype.cleanUp = save; + }); + + }); + + describe('#cleanUp', function() { + + it('should not set netKey', function() { + var n = new WebRTC(); + (n.netKey === undefined).should.equal(true); + }); + + it('should set privkey to null', function() { + var n = new WebRTC(); + n.cleanUp(); + expect(n.privkey).to.equal(null); + }); + + }); + + describe('#_encrypt', function() { + + it('should encrypt data successfully', function() { + var n = new WebRTC(); + var data = new bitcore.Buffer('my data to encrypt'); + var privkeystr = new bitcore.Buffer('test privkey'); + var privkey = bitcore.util.sha256(privkeystr); + var key = new bitcore.Key(); + key.private = privkey; + key.regenerateSync(); + var encrypted = n._encrypt(key.public, data); + encrypted.length.should.not.equal(0); + encrypted.length.should.equal(145); + }); + + }); + + describe('#_decrypt', function() { + + it('should decrypt that which was encrypted', function() { + var n = new WebRTC(); + var data = new bitcore.Buffer('my data to encrypt'); + var privkeystr = new bitcore.Buffer('test privkey'); + var privkey = bitcore.util.sha256(privkeystr); + var key = new bitcore.Key(); + key.private = privkey; + key.regenerateSync(); + var encrypted = n._encrypt(key.public, data); + var decrypted = n._decrypt(key.private, encrypted); + encrypted.length.should.not.equal(0); + decrypted.toString().should.equal('my data to encrypt'); + }); + + }); + + describe('#send', function() { + + it('should call _sendToOne for a copayer', function(done) { + var n = new WebRTC(); + + var data = new bitcore.Buffer('my data to send'); + + var privkeystr = new bitcore.Buffer('test privkey'); + var privkey = bitcore.util.sha256(privkeystr); + var key = new bitcore.Key(); + key.private = privkey; + key.regenerateSync(); + + var copayerId = key.public.toString('hex'); + n._sendToOne = function(a1, a2, a3, cb) {cb();}; + var sig = undefined; + n.send(copayerId, data, function() { + done(); + }); + + }); + + it('should call _sendToOne with encrypted data for a copayer', function(done) { + var n = new WebRTC(); + + var data = new bitcore.Buffer('my data to send'); + + var privkeystr = new bitcore.Buffer('test privkey'); + var privkey = bitcore.util.sha256(privkeystr); + var key = new bitcore.Key(); + key.private = privkey; + key.regenerateSync(); + + var copayerId = key.public.toString('hex'); + n._sendToOne = function(a1, encPayload, a3, cb) { + encPayload.length.should.be.greaterThan(0); + cb(); + }; + var sig = undefined; + n.send(copayerId, data, function() { + done(); + }); + + }); + + it('should call _sendToOne for a list of copayers', function(done) { + var n = new WebRTC(); + + var data = new bitcore.Buffer('my data to send'); + + var privkeystr = new bitcore.Buffer('test privkey'); + var privkey = bitcore.util.sha256(privkeystr); + var key = new bitcore.Key(); + key.private = privkey; + key.regenerateSync(); + + var copayerIds = [key.public.toString('hex')]; + n._sendToOne = function(a1, a2, a3, cb) {cb();}; + var sig = undefined; + n.send(copayerIds, data, function() { + done(); + }); + + }); + }); + +});