Merge pull request #768 from ryanxcharles/feature/authentication

Authentication
This commit is contained in:
Matias Alejo Garcia 2014-06-26 23:12:46 -03:00
commit c16531dfb7
9 changed files with 330 additions and 32 deletions

82
js/models/core/Message.js Normal file
View file

@ -0,0 +1,82 @@
'use strict';
var imports = require('soop').imports();
var bitcore = require('bitcore');
/* Encrypted, authenticated messages to be shared between copayers */
var Message = function() {
};
Message.encode = function(topubkey, fromkey, payload) {
var version = new Buffer([0]);
var toencrypt = Buffer.concat([version, payload]);
var encrypted = Message._encrypt(topubkey, toencrypt);
var sig = Message._sign(fromkey, encrypted);
var encoded = {
pubkey: fromkey.public.toString('hex'),
sig: sig.toString('hex'),
encrypted: encrypted.toString('hex')
};
return encoded;
};
Message.decode = function(key, encoded) {
try {
var frompubkey = new Buffer(encoded.pubkey, 'hex');
} catch (e) {
throw new Error('Error decoding public key: ' + e);
}
try {
var sig = new Buffer(encoded.sig, 'hex');
var encrypted = new Buffer(encoded.encrypted, 'hex');
} catch (e) {
throw new Error('Error decoding data: ' + e);
}
try {
var v = Message._verify(frompubkey, sig, encrypted);
} catch (e) {
throw new Error('Error verifying signature: ' + e);
}
if (!v)
throw new Error('Invalid signature');
try {
var decrypted = Message._decrypt(key.private, encrypted);
} catch (e) {
throw new Error('Cannot decrypt data: ' + e);
}
if (decrypted[0] !== 0)
throw new Error('Invalid version number');
if (decrypted.length === 0)
throw new Error('No data present');
var payload = decrypted.slice(1);
return payload;
};
Message._encrypt = function(topubkey, payload, r, iv) {
var encrypted = bitcore.ECIES.encrypt(topubkey, payload, r, iv);
return encrypted;
};
Message._decrypt = function(privkey, encrypted) {
var decrypted = bitcore.ECIES.decrypt(privkey, encrypted);
return decrypted;
};
Message._sign = function(key, payload) {
var sig = bitcore.Message.sign(payload, key);
return sig;
};
Message._verify = function(pubkey, signature, payload) {
var v = bitcore.Message.verifyWithPubKey(pubkey, payload, signature);
return v;
};
module.exports = require('soop')(Message);

View file

@ -32,9 +32,17 @@ PrivateKey.prototype.getIdPriv = function() {
return this.idpriv;
};
PrivateKey.prototype.getIdKey = function() {
if (!this.idkey) {
this.cacheId();
}
return this.idkey;
};
PrivateKey.prototype.cacheId = function() {
var path = Structure.IdFullBranch;
var idhk = this.bip.derive(path);
this.idkey = idhk.eckey;
this.id = idhk.eckey.public.toString('hex');
this.idpriv = idhk.eckey.private.toString('hex');
};

View file

@ -280,6 +280,7 @@ Wallet.prototype.netStart = function(callback) {
var myId = self.getMyCopayerId();
var myIdPriv = self.getMyCopayerIdPriv();
var startOpts = {
copayerId: myId,
privkey: myIdPriv,

View file

@ -224,7 +224,8 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
this.log('\t### PrivateKey Initialized');
var opts = {
copayerId: privateKey.getId(),
privkey: privateKey.getIdPriv()
privkey: privateKey.getIdPriv(),
key: privateKey.getIdKey()
};
self.network.cleanUp();
self.network.start(opts, function() {