diff --git a/lib/walletutils.js b/lib/walletutils.js index bfecf60..823f25e 100644 --- a/lib/walletutils.js +++ b/lib/walletutils.js @@ -5,6 +5,7 @@ var PrivateKey = Bitcore.PrivateKey; var PublicKey = Bitcore.PublicKey; var crypto = Bitcore.crypto; var HDPath = require('./hdpath'); +var sjcl = require('sjcl'); function WalletUtils() {}; @@ -61,8 +62,9 @@ WalletUtils.xPubToCopayerId = function(xpub) { return (new Bitcore.HDPublicKey(xpub)).derive(HDPath.IdBranch).publicKey.toString(); }; +<< << << < HEAD WalletUtils.toSecret = function(walletId, walletPrivKey, network) { - return walletId + ':' + walletPrivKey.toWIF() + ':' + (network == 'testnet' ? 'T' : 'L'); + return walletId + ':' + walletPrivKey.toWIF() + ':' + (network == 'testnet' ? 'T' : 'L'); }; WalletUtils.fromSecret = function(secret) { @@ -72,7 +74,7 @@ WalletUtils.fromSecret = function(secret) { var networkChar = secretSplit[2]; - return { + return { walletId: walletId, walletPrivKey: walletPrivKey, network: networkChar == 'T' ? 'testnet' : 'livenet', @@ -80,6 +82,19 @@ WalletUtils.fromSecret = function(secret) { }; +WalletUtils.encryptMessage = function(message, password) { + var key = sjcl.codec.utf8String.toBits(password); + key = sjcl.bitArray.clamp(key, 256); + return sjcl.encrypt(key, message, { + ks: 256, + iter: 1 + }); +}; +WalletUtils.decryptMessage = function(cyphertextJson, password) { + var key = sjcl.codec.utf8String.toBits(password); + key = sjcl.bitArray.clamp(key, 256); + return sjcl.decrypt(key, cyphertextJson); +}; module.exports = WalletUtils; diff --git a/package.json b/package.json index 4298698..bcca48e 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,9 @@ "morgan": "*", "npmlog": "^0.1.1", "preconditions": "^1.0.7", - "request": "^2.53.0", "qr-image": "*", + "request": "^2.53.0", + "sjcl": "^1.0.2", "uuid": "*" }, "devDependencies": { diff --git a/test/walletutils.js b/test/walletutils.js index 0addd17..1383885 100644 --- a/test/walletutils.js +++ b/test/walletutils.js @@ -65,4 +65,13 @@ describe('WalletUtils', function() { WalletUtils.verifyMessage(aLongerText, sig, aPubKey).should.equal(true); }); }); + + describe('#encryptMessage #decryptMessage round trip', function() { + it('should encrypt and decrypt', function() { + var pwd = '0dea92f1df6675085b5cdd965487bb862f84f2755bcb56fa45dbf5b387a6c4a0'; + var ct = WalletUtils.encryptMessage('hello world', pwd); + var msg = WalletUtils.decryptMessage(ct, pwd); + msg.should.equal('hello world'); + }); + }); });