From c1881d5fbb1f887f19778c8067e87afaa0165c4a Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 23 Apr 2014 13:44:20 -0300 Subject: [PATCH 1/4] peerId hashing WIP2 --- index.html | 34 +++++-- js/models/core/PrivateKey.js | 1 - js/models/core/Wallet.js | 40 ++++---- js/models/core/WalletFactory.js | 8 +- js/models/network/WebRTC.js | 168 +++++++++++++++++++++----------- 5 files changed, 162 insertions(+), 89 deletions(-) diff --git a/index.html b/index.html index a8105b78c..7065e3c27 100644 --- a/index.html +++ b/index.html @@ -57,16 +57,30 @@
-
- - Note: Your wallet is not complete yet. - - {{$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers() }} keys are - - - One key is - - missing. Share this secret with your other copayers for them to join your wallet: {{$root.wallet.getMyPeerId()}} +
+ + +
+ +
+ +
+ Note: Your wallet is not complete yet. + + {{$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers() }} keys are + + + One key is + + missing. +
+ +
+ Share this secret with your other copayers for them to join your wallet
+
+
+ {{$root.wallet.getMyCopayerId()}} +
diff --git a/js/models/core/PrivateKey.js b/js/models/core/PrivateKey.js index 0e2d87631..589dfaae0 100644 --- a/js/models/core/PrivateKey.js +++ b/js/models/core/PrivateKey.js @@ -27,7 +27,6 @@ PrivateKey.prototype.getId = function(prefix) { return hash.substring(0, hash.length/2); }; - PrivateKey.fromObj = function(obj) { return new PrivateKey(obj); }; diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index b90b71040..13a7e6e94 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -122,10 +122,10 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) { } }; -Wallet.prototype._handleNetworkChange = function(newPeerId) { +Wallet.prototype._handleNetworkChange = function(newCopayerId) { if (newPeerId) { - this.log('#### Setting new PEER:', newPeerId); - this.sendWalletId(newPeerId); + this.log('#### Setting new PEER:', newCopayerId); + this.sendWalletId(newCopayerId); } this.emit('refresh'); }; @@ -143,13 +143,13 @@ Wallet.prototype._optsToObj = function () { }; -Wallet.prototype.getPeerId = function(index) { +Wallet.prototype.getCopayerId = function(index) { return this.publicKeyRing.getCopayerId(index || 0); }; -Wallet.prototype.getMyPeerId = function() { - return this.getPeerId(0); +Wallet.prototype.getMyCopayerId = function() { + return this.getCopayerId(0); }; Wallet.prototype.netStart = function() { @@ -167,19 +167,21 @@ Wallet.prototype.netStart = function() { self.emit('close'); }); - var myPeerId = self.getMyPeerId(); + var myId = self.getMyCopayerId(); var startOpts = { - peerId: myPeerId + copayerId: myId }; net.start(function() { self.emit('created'); for (var i=0; i Date: Wed, 23 Apr 2014 16:02:23 -0300 Subject: [PATCH 2/4] hashing working --- index.html | 23 ++++----------- js/controllers/transactions.js | 2 +- js/models/core/Wallet.js | 2 +- js/models/network/WebRTC.js | 51 +++++++++++++++++----------------- 4 files changed, 34 insertions(+), 44 deletions(-) diff --git a/index.html b/index.html index 7065e3c27..3e3b8e05f 100644 --- a/index.html +++ b/index.html @@ -193,28 +193,17 @@ + diff --git a/js/controllers/transactions.js b/js/controllers/transactions.js index af3404056..fed674948 100644 --- a/js/controllers/transactions.js +++ b/js/controllers/transactions.js @@ -60,7 +60,7 @@ angular.module('copay.transactions').controller('TransactionsController', return; } var p = w.txProposals.getTxProposal(ntxid); - if (p.txp.builder.isFullySigned()) { + if (p.builder.isFullySigned()) { $scope.send(ntxid); _updateTxs(); $rootScope.$digest(); diff --git a/js/models/core/PrivateKey.js b/js/models/core/PrivateKey.js index 589dfaae0..6fdc21a44 100644 --- a/js/models/core/PrivateKey.js +++ b/js/models/core/PrivateKey.js @@ -18,13 +18,22 @@ function PrivateKey(opts) { this.privateKeyCache = opts.privateKeyCache || {}; }; -PrivateKey.prototype.getId = function(prefix) { - var buf = this.bip.extendedPublicKey; - if (prefix) { - buf = Buffer.concat([prefix, buf]); +PrivateKey.prototype.getId = function() { + if (!this.id) { + var path = PublicKeyRing.SIGNING_BRANCH; + var bip32 = this.bip.derive(path); + this.id= bip32.eckey.public.toString('hex'); } - var hash = util.sha256(buf).toString('hex'); - return hash.substring(0, hash.length/2); + return this.id; +}; + +PrivateKey.prototype.getSigningKey = function() { + if (!this.sid) { + var path = PublicKeyRing.SIGNING_BRANCH; + var bip32 = this.bip.derive(path); + this.sid= bip32.eckey.private.toString('hex'); + } + return this.sid; }; PrivateKey.fromObj = function(obj) { diff --git a/js/models/core/PublicKeyRing.js b/js/models/core/PublicKeyRing.js index a9a1327d5..6d757671f 100644 --- a/js/models/core/PublicKeyRing.js +++ b/js/models/core/PublicKeyRing.js @@ -44,9 +44,12 @@ function PublicKeyRing(opts) { */ PublicKeyRing.Branch = function (index, isChange) { - return 'm/'+(isChange?1:0)+'/'+index; + // first 0 is for future use: could be copayerId. + return 'm/0/'+(isChange?1:0)+'/'+index; }; +PublicKeyRing.SIGNING_BRANCH = 'm/100/0/0'; + PublicKeyRing.fromObj = function (data) { if (data instanceof PublicKeyRing) { throw new Error('bad data format: Did you use .toObj()?'); @@ -77,17 +80,20 @@ PublicKeyRing.prototype.serialize = function () { return JSON.stringify(this.toObj()); }; -PublicKeyRing.prototype.getCopayerId = function(i, prefix) { - var buf = this.copayersBIP32[i].extendedPublicKey; - if (prefix) { - buf = Buffer.concat([prefix, buf]); +PublicKeyRing.prototype.getCopayerId = function(i) { + this.copayerIds = this.copayerIds || []; + + if (!this.copayerIds[i]) { + var path = PublicKeyRing.SIGNING_BRANCH; + var bip32 = this.copayersBIP32[i].derive(path); + this.copayerIds[i]= bip32.eckey.public.toString('hex'); } - var hash = util.sha256(buf).toString('hex'); - return hash.substring(0, hash.length/2); + + return this.copayerIds[i]; }; -PublicKeyRing.prototype.myCopayerId = function(i, prefix) { - return this.getCopayerId(0,prefix); +PublicKeyRing.prototype.myCopayerId = function(i) { + return this.getCopayerId(0); }; PublicKeyRing.prototype.registeredCopayers = function () { diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 0e1d12348..fdbfe36ee 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -169,9 +169,10 @@ Wallet.prototype.netStart = function() { var myId = self.getMyCopayerId(); var startOpts = { - copayerId: myId + copayerId: myId, + signingKeyHex: self.privateKey.getSigningKey(), }; - net.start(function() { + net.start(startOpts, function() { self.emit('created'); for (var i=0; i Date: Thu, 24 Apr 2014 11:58:29 -0300 Subject: [PATCH 4/4] replace HMAC with bitcore\'s hmac --- index.html | 1 - js/models/network/WebRTC.js | 24 ++++++------------------ 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/index.html b/index.html index b8b22c012..d863c928f 100644 --- a/index.html +++ b/index.html @@ -491,7 +491,6 @@ - diff --git a/js/models/network/WebRTC.js b/js/models/network/WebRTC.js index 24fbeb3ac..a5569c52b 100644 --- a/js/models/network/WebRTC.js +++ b/js/models/network/WebRTC.js @@ -4,7 +4,6 @@ var EventEmitter= imports.EventEmitter || require('events').EventEmitter; var bitcore = require('bitcore'); var util = bitcore.util; var Key = bitcore.Key; - /* * Emits * 'networkChange' @@ -83,8 +82,6 @@ Network.prototype.connectedCopayers = function() { }; Network.prototype._onClose = function(peerId) { -console.log('[WebRTC.js.72:_onClose:]'); - delete this.connections[peerId]; this.connectedPeers = Network._arrayRemove(peerId, this.connectedPeers); this._notifyNetworkChange(); @@ -92,11 +89,7 @@ console.log('[WebRTC.js.72:_onClose:]'); Network.prototype._connectToCopayers = function(copayerIds) { var self = this; - -console.log('[WebRTC.js.96] _connectToCopayers', copayerIds, this.connectedCopayers() ); //TODO - var arrayDiff= Network._arrayDiff(copayerIds, this.connectedCopayers()); -console.log('[WebRTC.js.99:arrayDiff:]',arrayDiff); //TODO arrayDiff.forEach(function(copayerId) { console.log('### CONNECTING TO:', copayerId); self.connectTo(copayerId); @@ -291,7 +284,6 @@ Network.prototype.setCopayerId = function(copayerId) { if (this.started) { throw new Error ('network already started: can not change peerId') } -console.log('[WebRTC.js.295] SETING COPAYER ID:' + copayerId); //TODO this.copayerId = copayerId; this.copayerIdBuf = new Buffer(copayerId,'hex'); this.peerId = this.peerFromCopayer(this.copayerId); @@ -331,7 +323,6 @@ Network.prototype.start = function(opts, openCallback) { this.connectTo(otherPeerId); } this.started = true; -console.log('[WebRTC.js.237] started TRUE'); //TODO }; @@ -339,18 +330,15 @@ Network.prototype._sign = function(payload, copayerId) { var ret=''; var str = JSON.stringify(payload); if (payload.type ==='hello') { -console.log('[WebRTC.js.331] SIGNING WITH HMAC:', copayerId); //TODO - ret = CryptoJS.enc.Base64.stringify( - CryptoJS.HmacSHA256( - str, - copayerId - )); + ret = ( + util.sha512hmac( + new Buffer(str), + new Buffer(copayerId,'hex') + )).toString('hex'); } else { if (!this.signingKey) throw new Error ('no key to sign messages :('); - -console.log('[WebRTC.js.341] SIGNING WITH ECDSA'); //TODO ret = bitcore.Message.sign( str, this.signingKey @@ -371,7 +359,7 @@ Network.prototype._sendToOne = function(copayerId, payload, cb) { dataConn.send(str); } else { -console.log('[WebRTC.js.255] WARN: NO CONNECTION TO:', peerId); //TODO + console.log('[WebRTC.js.255] WARN: NO CONNECTION TO:', peerId); //TODO } } if (typeof cb === 'function') cb();