commit
4d6e9f8a6d
10 changed files with 318 additions and 68 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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()) {
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue