From 643cad3a393b49ba839cd6daac543e25f370a57a Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Wed, 9 Jul 2014 01:13:42 -0700 Subject: [PATCH] change encryption to work on hex strings ..instead of binary, to work around an issue with bitcore/sjcl. I'm not sure what the issue is exactly, except that encryption of binary data isn't working correctly due to some kind of string stuff involving decodeURIComponent inside sjcl. I haven't fully figured it out. For now I am changing the network protocol to hex to workaround the issue. See this: https://github.com/bitpay/bitcore/pull/416 --- js/models/core/Message.js | 6 ++++-- test/test.Message.js | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/js/models/core/Message.js b/js/models/core/Message.js index 7c9de9c27..79b94d8e3 100644 --- a/js/models/core/Message.js +++ b/js/models/core/Message.js @@ -21,7 +21,8 @@ Message.encode = function(topubkey, fromkey, payload, opts) { } var toencrypt = Buffer.concat([version1, version2, nonce, payload]); - var encrypted = Message._encrypt(topubkey, toencrypt); + var toencrypthexbuf = new Buffer(toencrypt.toString('hex')); //due to bug in sjcl/bitcore, must use hex string + var encrypted = Message._encrypt(topubkey, toencrypthexbuf); var sig = Message._sign(fromkey, encrypted); var encoded = { pubkey: fromkey.public.toString('hex'), @@ -63,7 +64,8 @@ Message.decode = function(key, encoded, opts) { } try { - var decrypted = Message._decrypt(key.private, encrypted); + var decryptedhexbuf = Message._decrypt(key.private, encrypted); + var decrypted = new Buffer(decryptedhexbuf.toString(), 'hex'); //workaround for bug in bitcore/sjcl } catch (e) { throw new Error('Cannot decrypt data: ' + e); } diff --git a/test/test.Message.js b/test/test.Message.js index 10134b736..9bad687d1 100644 --- a/test/test.Message.js +++ b/test/test.Message.js @@ -100,7 +100,8 @@ describe('Message model', function() { var version2 = new Buffer([0]); var nonce = new Buffer([0, 0, 0, 0, 0, 0, 0, 0]); var toencrypt = Buffer.concat([version1, version2, nonce, payload]); - var encrypted = Message._encrypt(topubkey, toencrypt); + var toencrypt_workaround = new Buffer(toencrypt.toString('hex')); + var encrypted = Message._encrypt(topubkey, toencrypt_workaround); var sig = Message._sign(fromkey, encrypted); var encoded = { pubkey: fromkey.public.toString('hex'),