Added secret number feature

This commit is contained in:
Matias Pando 2014-09-03 16:51:02 -03:00
commit 89dba4c616
6 changed files with 90 additions and 14 deletions

View file

@ -43,6 +43,7 @@ function Wallet(opts) {
' and tried to create a Wallet with network ' + this.getNetworkName());
this.id = opts.id || Wallet.getRandomId();
this.secretNumber = opts.secretNumber || Wallet.getRandomNumber();
this.lock = new WalletLock(this.storage, this.id, opts.lockTimeOutMin);
this.name = opts.name;
@ -50,6 +51,7 @@ function Wallet(opts) {
this.publicKeyRing.walletId = this.id;
this.txProposals.walletId = this.id;
this.network.maxPeers = this.totalCopayers;
this.network.secretNumber = this.secretNumber;
this.registeredPeerIds = [];
this.addressBook = opts.addressBook || {};
this.publicKey = this.privateKey.publicHex;
@ -78,6 +80,11 @@ Wallet.getRandomId = function() {
return r;
};
Wallet.getRandomNumber = function() {
var r = bitcore.SecureRandom.getPseudoRandomBuffer(5).toString('hex');
return r;
};
Wallet.prototype.seedCopayer = function(pubKey) {
this.seededCopayerId = pubKey;
};
@ -408,18 +415,28 @@ Wallet.prototype.getMyCopayerIdPriv = function() {
return this.privateKey.getIdPriv(); //copayer idpriv is hex of a private key
};
Wallet.prototype.getSecretNumber = function() {
if (this.secretNumber) return this.secretNumber;
this.secretNumber = Wallet.getRandomNumber();
return this.secretNumber;
};
Wallet.prototype.getSecret = function() {
var pubkeybuf = new Buffer(this.getMyCopayerId(), 'hex');
var str = Base58Check.encode(pubkeybuf);
var buf = new Buffer(this.getMyCopayerId() + this.getSecretNumber(), 'hex');
var str = Base58Check.encode(buf);
return str;
};
Wallet.decodeSecret = function(secretB) {
var secret = Base58Check.decode(secretB);
var pubKeyBuf = secret.slice(0, 33);
var secretNumber = secret.slice(33, 38);
return {
pubKey: pubKeyBuf.toString('hex')
pubKey: pubKeyBuf.toString('hex'),
secretNumber : secretNumber.toString('hex')
}
};
@ -445,6 +462,7 @@ Wallet.prototype.netStart = function(callback) {
privkey: myIdPriv,
maxPeers: self.totalCopayers,
lastTimestamp: this.lastTimestamp,
secretNumber: self.secretNumber,
};
if (this.publicKeyRing.isComplete()) {

View file

@ -233,7 +233,8 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
var opts = {
copayerId: privateKey.getId(),
privkey: privateKey.getIdPriv(),
key: privateKey.getIdKey()
key: privateKey.getIdKey(),
secretNumber : s.secretNumber,
};
self.network.cleanUp();
@ -248,7 +249,7 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
});
self.network.start(opts, function() {
self.network.greet(s.pubKey);
self.network.greet(s.pubKey,opts.secretNumber);
self.network.on('data', function(sender, data) {
if (data.type === 'walletId') {
if (data.networkName !== self.networkName) {

View file

@ -17,6 +17,7 @@ function Network(opts) {
this.host = opts.host || 'localhost';
this.port = opts.port || 3001;
this.schema = opts.schema || 'https';
this.secretNumber = opts.secretNumber;
this.cleanUp();
}
@ -73,11 +74,12 @@ Network.prototype.connectedCopayers = function() {
return ret;
};
Network.prototype._sendHello = function(copayerId) {
Network.prototype._sendHello = function(copayerId,secretNumber) {
this.send(copayerId, {
type: 'hello',
copayerId: this.copayerId,
secretNumber : secretNumber
});
};
@ -193,6 +195,12 @@ Network.prototype._onMessage = function(enc) {
var self = this;
switch (payload.type) {
case 'hello':
if (typeof payload.secretNumber === 'undefined' || payload.secretNumber !== this.secretNumber)
{
this._deletePeer(enc.pubkey, 'incorrect secret number');
return;
}
// if we locked allowed copayers, check if it belongs
if (this.allowedCopayerIds && !this.allowedCopayerIds[payload.copayerId]) {
this._deletePeer(sender);
@ -240,8 +248,8 @@ Network.prototype._onError = function(err) {
this.criticalError = err.message;
};
Network.prototype.greet = function(copayerId) {
this._sendHello(copayerId);
Network.prototype.greet = function(copayerId,secretNumber) {
this._sendHello(copayerId,secretNumber);
var peerId = this.peerFromCopayer(copayerId);
this._addCopayerMap(peerId, copayerId);
};
@ -350,6 +358,7 @@ Network.prototype.send = function(dest, payload, cb) {
if (to == this.copayerId)
continue;
log.debug('SEND to: ' + to, this.copayerId, payload);
var message = this.encode(to, payload);
this.socket.emit('message', message);
}