add support for 8 byte big endian nonce to Message
...network protocol is backwards incompatible with previous network protocl. also includes a second version number for backwards-compatible changes (the original version number was for backwards-incompatible changes).
This commit is contained in:
parent
9b3ce1ebe9
commit
a1155c2798
4 changed files with 149 additions and 29 deletions
|
|
@ -7,9 +7,20 @@ var bitcore = require('bitcore');
|
|||
var Message = function() {
|
||||
};
|
||||
|
||||
Message.encode = function(topubkey, fromkey, payload) {
|
||||
var version = new Buffer([0]);
|
||||
var toencrypt = Buffer.concat([version, payload]);
|
||||
Message.encode = function(topubkey, fromkey, payload, opts) {
|
||||
var version1 = new Buffer([1]); //peers will reject messges containing not-understood version1
|
||||
//i.e., increment version1 to prevent communications with old clients
|
||||
var version2 = new Buffer([0]); //peers will not reject messages containing not-understood version2
|
||||
//i.e., increment version2 to allow communication with old clients, but signal new clients
|
||||
|
||||
if (opts && opts.nonce && Buffer.isBuffer(opts.nonce) && opts.nonce.length == 8) {
|
||||
var nonce = opts.nonce;
|
||||
} else {
|
||||
var nonce = new Buffer(8);
|
||||
nonce.fill(0); //nonce is a big endian 8 byte number
|
||||
}
|
||||
|
||||
var toencrypt = Buffer.concat([version1, version2, nonce, payload]);
|
||||
var encrypted = Message._encrypt(topubkey, toencrypt);
|
||||
var sig = Message._sign(fromkey, encrypted);
|
||||
var encoded = {
|
||||
|
|
@ -20,7 +31,14 @@ Message.encode = function(topubkey, fromkey, payload) {
|
|||
return encoded;
|
||||
};
|
||||
|
||||
Message.decode = function(key, encoded) {
|
||||
Message.decode = function(key, encoded, opts) {
|
||||
if (opts && opts.prevnonce && Buffer.isBuffer(opts.prevnonce) && opts.prevnonce.length == 8) {
|
||||
var prevnonce = opts.prevnonce;
|
||||
} else {
|
||||
var prevnonce = new Buffer(8);
|
||||
prevnonce.fill(0); //nonce is a big endian 8 byte number
|
||||
}
|
||||
|
||||
try {
|
||||
var frompubkey = new Buffer(encoded.pubkey, 'hex');
|
||||
} catch (e) {
|
||||
|
|
@ -40,8 +58,9 @@ Message.decode = function(key, encoded) {
|
|||
throw new Error('Error verifying signature: ' + e);
|
||||
}
|
||||
|
||||
if (!v)
|
||||
if (!v) {
|
||||
throw new Error('Invalid signature');
|
||||
}
|
||||
|
||||
try {
|
||||
var decrypted = Message._decrypt(key.private, encrypted);
|
||||
|
|
@ -49,14 +68,59 @@ Message.decode = function(key, encoded) {
|
|||
throw new Error('Cannot decrypt data: ' + e);
|
||||
}
|
||||
|
||||
if (decrypted[0] !== 0)
|
||||
throw new Error('Invalid version number');
|
||||
try {
|
||||
var version1 = decrypted[0];
|
||||
var version2 = decrypted[1];
|
||||
var nonce = decrypted.slice(2, 10);
|
||||
var payload = decrypted.slice(10);
|
||||
} catch (e) {
|
||||
throw new Error('Cannot parse decrypted data: ' + e);
|
||||
}
|
||||
|
||||
if (decrypted.length === 0)
|
||||
if (payload.length === 0) {
|
||||
throw new Error('No data present');
|
||||
}
|
||||
|
||||
var payload = decrypted.slice(1);
|
||||
return payload;
|
||||
if (version1 !== 1) {
|
||||
throw new Error('Invalid version number');
|
||||
}
|
||||
|
||||
if (version2 !== 0) {
|
||||
//put special version2 handling code here, if ever needed
|
||||
}
|
||||
|
||||
if (!Message._noncegt(nonce, prevnonce) && prevnonce.toString('hex') !== '0000000000000000') {
|
||||
throw new Error('Nonce not equal to zero and not greater than the previous nonce');
|
||||
}
|
||||
|
||||
var decoded = {
|
||||
version1: version1,
|
||||
version2: version2,
|
||||
nonce: nonce,
|
||||
payload: payload
|
||||
};
|
||||
|
||||
return decoded;
|
||||
};
|
||||
|
||||
//return true if nonce > prevnonce; false otherwise
|
||||
Message._noncegt = function(nonce, prevnonce) {
|
||||
var noncep1 = nonce.slice(0, 4).readUInt32BE(0);
|
||||
var prevnoncep1 = prevnonce.slice(0, 4).readUInt32BE(0);
|
||||
|
||||
if (noncep1 > prevnoncep1)
|
||||
return true;
|
||||
|
||||
if (noncep1 < prevnoncep1)
|
||||
return false;
|
||||
|
||||
var noncep2 = nonce.slice(4, 8).readUInt32BE(0);
|
||||
var prevnoncep2 = prevnonce.slice(4, 8).readUInt32BE(0);
|
||||
|
||||
if (noncep2 > prevnoncep2)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
Message._encrypt = function(topubkey, payload, r, iv) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue