fix browser tests

This commit is contained in:
Manuel Araoz 2014-08-04 15:51:53 -03:00
parent ca16817a1c
commit 895046cc32
3 changed files with 68 additions and 21 deletions

View File

@ -468,16 +468,36 @@ module.exports = Armory;
var Message = require('./Message'); var Message = require('./Message');
var ECIES = require('./ECIES'); var ECIES = require('./ECIES');
var preconditions = require('preconditions').singleton();
var Key = require('./Key');
var majorVersion = 1;
var minorVersion = 0;
/* Encrypted, authenticated messages to be shared between copayers */ /* Encrypted, authenticated messages to be shared between copayers */
var AuthMessage = function() { var AuthMessage = function() {};
AuthMessage.setVersion = function(major, minor) {
majorVersion = major;
minorVersion = minor;
}; };
AuthMessage.encode = function(topubkey, fromkey, payload, opts) { AuthMessage.encode = function(topubkey, fromkey, payload, opts) {
var version1 = new Buffer([1]); //peers will reject messges containing not-understood version1 preconditions.checkArgument(fromkey instanceof Key, 'fromkey');
//i.e., increment version1 to prevent communications with old clients if (typeof topubkey === 'string') {
var version2 = new Buffer([0]); //peers will not reject messages containing not-understood version2 topubkey = new Buffer(topubkey, 'hex');
//i.e., increment version2 to allow communication with old clients, but signal new clients }
if (!(payload instanceof Buffer)) {
payload = new Buffer(JSON.stringify(payload));
}
//peers should reject messges containing bigger major version
//i.e., increment to prevent communications with old clients
var version1 = new Buffer([majorVersion]);
//peers should not reject messages containing not-understood minorversion
//i.e., increment to allow communication with old clients, but signal new clients
var version2 = new Buffer([minorVersion]);
if (opts && opts.nonce && Buffer.isBuffer(opts.nonce) && opts.nonce.length == 8) { if (opts && opts.nonce && Buffer.isBuffer(opts.nonce) && opts.nonce.length == 8) {
var nonce = opts.nonce; var nonce = opts.nonce;
@ -493,7 +513,8 @@ AuthMessage.encode = function(topubkey, fromkey, payload, opts) {
var encoded = { var encoded = {
pubkey: fromkey.public.toString('hex'), pubkey: fromkey.public.toString('hex'),
sig: sig.toString('hex'), sig: sig.toString('hex'),
encrypted: encrypted.toString('hex') encrypted: encrypted.toString('hex'),
to: topubkey.toString('hex')
}; };
return encoded; return encoded;
}; };
@ -518,7 +539,7 @@ AuthMessage.decode = function(key, encoded, opts) {
} catch (e) { } catch (e) {
throw new Error('Error decoding data: ' + e); throw new Error('Error decoding data: ' + e);
} }
try { try {
var v = AuthMessage._verify(frompubkey, sig, encrypted); var v = AuthMessage._verify(frompubkey, sig, encrypted);
} catch (e) { } catch (e) {
@ -549,11 +570,11 @@ AuthMessage.decode = function(key, encoded, opts) {
throw new Error('No data present'); throw new Error('No data present');
} }
if (version1 !== 1) { if (version1 !== majorVersion) {
throw new Error('Invalid version number'); throw new Error('Invalid version number');
} }
if (version2 !== 0) { if (version2 !== minorVersion) {
//put special version2 handling code here, if ever needed //put special version2 handling code here, if ever needed
} }
@ -561,6 +582,16 @@ AuthMessage.decode = function(key, encoded, opts) {
throw new Error('Nonce not equal to zero and not greater than the previous nonce'); throw new Error('Nonce not equal to zero and not greater than the previous nonce');
} }
try {
payload = JSON.parse(payload);
} catch (e) {
if (e instanceof SyntaxError) {
// if we can't parse a JSON, just return what we found
} else {
throw e;
}
}
var decoded = { var decoded = {
version1: version1, version1: version1,
version2: version2, version2: version2,
@ -578,7 +609,7 @@ AuthMessage._noncegt = function(nonce, prevnonce) {
if (noncep1 > prevnoncep1) if (noncep1 > prevnoncep1)
return true; return true;
if (noncep1 < prevnoncep1) if (noncep1 < prevnoncep1)
return false; return false;
@ -614,7 +645,7 @@ AuthMessage._verify = function(pubkey, signature, payload) {
module.exports = AuthMessage; module.exports = AuthMessage;
}).call(this,require("buffer").Buffer) }).call(this,require("buffer").Buffer)
},{"./ECIES":"0Qraa1","./Message":"CBDCgz","buffer":95}],"./lib/AuthMessage":[function(require,module,exports){ },{"./ECIES":"0Qraa1","./Key":"ALJ4PS","./Message":"CBDCgz","buffer":95,"preconditions":163}],"./lib/AuthMessage":[function(require,module,exports){
module.exports=require('cBnJMk'); module.exports=require('cBnJMk');
},{}],"./lib/BIP39":[function(require,module,exports){ },{}],"./lib/BIP39":[function(require,module,exports){
module.exports=require('82LilS'); module.exports=require('82LilS');

View File

@ -2,14 +2,15 @@
var Message = require('./Message'); var Message = require('./Message');
var ECIES = require('./ECIES'); var ECIES = require('./ECIES');
var preconditions = require('preconditions').singleton();
var Key = require('./Key');
var majorVersion = 1; var majorVersion = 1;
var minorVersion = 0; var minorVersion = 0;
/* Encrypted, authenticated messages to be shared between copayers */ /* Encrypted, authenticated messages to be shared between copayers */
var AuthMessage = function() { var AuthMessage = function() {};
};
AuthMessage.setVersion = function(major, minor) { AuthMessage.setVersion = function(major, minor) {
majorVersion = major; majorVersion = major;
@ -17,6 +18,13 @@ AuthMessage.setVersion = function(major, minor) {
}; };
AuthMessage.encode = function(topubkey, fromkey, payload, opts) { AuthMessage.encode = function(topubkey, fromkey, payload, opts) {
preconditions.checkArgument(fromkey instanceof Key, 'fromkey');
if (typeof topubkey === 'string') {
topubkey = new Buffer(topubkey, 'hex');
}
if (!(payload instanceof Buffer)) {
payload = new Buffer(JSON.stringify(payload));
}
//peers should reject messges containing bigger major version //peers should reject messges containing bigger major version
//i.e., increment to prevent communications with old clients //i.e., increment to prevent communications with old clients
var version1 = new Buffer([majorVersion]); var version1 = new Buffer([majorVersion]);
@ -39,7 +47,8 @@ AuthMessage.encode = function(topubkey, fromkey, payload, opts) {
var encoded = { var encoded = {
pubkey: fromkey.public.toString('hex'), pubkey: fromkey.public.toString('hex'),
sig: sig.toString('hex'), sig: sig.toString('hex'),
encrypted: encrypted.toString('hex') encrypted: encrypted.toString('hex'),
to: topubkey.toString('hex')
}; };
return encoded; return encoded;
}; };
@ -64,7 +73,7 @@ AuthMessage.decode = function(key, encoded, opts) {
} catch (e) { } catch (e) {
throw new Error('Error decoding data: ' + e); throw new Error('Error decoding data: ' + e);
} }
try { try {
var v = AuthMessage._verify(frompubkey, sig, encrypted); var v = AuthMessage._verify(frompubkey, sig, encrypted);
} catch (e) { } catch (e) {
@ -107,6 +116,16 @@ AuthMessage.decode = function(key, encoded, opts) {
throw new Error('Nonce not equal to zero and not greater than the previous nonce'); throw new Error('Nonce not equal to zero and not greater than the previous nonce');
} }
try {
payload = JSON.parse(payload);
} catch (e) {
if (e instanceof SyntaxError) {
// if we can't parse a JSON, just return what we found
} else {
throw e;
}
}
var decoded = { var decoded = {
version1: version1, version1: version1,
version2: version2, version2: version2,
@ -124,7 +143,7 @@ AuthMessage._noncegt = function(nonce, prevnonce) {
if (noncep1 > prevnoncep1) if (noncep1 > prevnoncep1)
return true; return true;
if (noncep1 < prevnoncep1) if (noncep1 < prevnoncep1)
return false; return false;

View File

@ -16,6 +16,8 @@ describe('AuthMessage model', function() {
var key2 = new Key(); var key2 = new Key();
key2.private = util.sha256(new Buffer('test 2')); key2.private = util.sha256(new Buffer('test 2'));
key2.regenerateSync(); key2.regenerateSync();
var message = 'some message';
describe('#encode', function() { describe('#encode', function() {
@ -32,7 +34,6 @@ describe('AuthMessage model', function() {
describe('#decode', function() { describe('#decode', function() {
it('should decode an encoded message', function() { it('should decode an encoded message', function() {
var message = new Buffer('message');
var messagehex = message.toString('hex'); var messagehex = message.toString('hex');
var encoded = AuthMessage.encode(key2.public, key, message); var encoded = AuthMessage.encode(key2.public, key, message);
@ -42,7 +43,6 @@ describe('AuthMessage model', function() {
}); });
it('should decode an encoded message with proper prevnonce', function() { it('should decode an encoded message with proper prevnonce', function() {
var message = new Buffer('message');
var messagehex = message.toString('hex'); var messagehex = message.toString('hex');
var nonce = new Buffer([0, 0, 0, 0, 0, 0, 0, 2]); var nonce = new Buffer([0, 0, 0, 0, 0, 0, 0, 2]);
var opts = {nonce: nonce}; var opts = {nonce: nonce};
@ -56,7 +56,6 @@ describe('AuthMessage model', function() {
}); });
it('should decode an encoded message with proper prevnonce - for first part', function() { it('should decode an encoded message with proper prevnonce - for first part', function() {
var message = new Buffer('message');
var messagehex = message.toString('hex'); var messagehex = message.toString('hex');
var nonce = new Buffer([0, 0, 0, 2, 0, 0, 0, 0]); var nonce = new Buffer([0, 0, 0, 2, 0, 0, 0, 0]);
var opts = {nonce: nonce}; var opts = {nonce: nonce};
@ -70,7 +69,6 @@ describe('AuthMessage model', function() {
}); });
it('should fail if prevnonce is too high', function() { it('should fail if prevnonce is too high', function() {
var message = new Buffer('message');
var messagehex = message.toString('hex'); var messagehex = message.toString('hex');
var nonce = new Buffer([0, 0, 0, 0, 0, 0, 0, 1]); var nonce = new Buffer([0, 0, 0, 0, 0, 0, 0, 1]);
var opts = {nonce: nonce}; var opts = {nonce: nonce};
@ -82,7 +80,6 @@ describe('AuthMessage model', function() {
}); });
it('should fail if prevnonce is too high - for first part', function() { it('should fail if prevnonce is too high - for first part', function() {
var message = new Buffer('message');
var messagehex = message.toString('hex'); var messagehex = message.toString('hex');
var nonce = new Buffer([0, 0, 0, 1, 0, 0, 0, 0]); var nonce = new Buffer([0, 0, 0, 1, 0, 0, 0, 0]);
var opts = {nonce: nonce}; var opts = {nonce: nonce};