add encrypt/decrypt methods
This commit is contained in:
parent
aa9c8e7ce6
commit
78033f7c97
|
@ -5,6 +5,7 @@ var PrivateKey = Bitcore.PrivateKey;
|
||||||
var PublicKey = Bitcore.PublicKey;
|
var PublicKey = Bitcore.PublicKey;
|
||||||
var crypto = Bitcore.crypto;
|
var crypto = Bitcore.crypto;
|
||||||
var HDPath = require('./hdpath');
|
var HDPath = require('./hdpath');
|
||||||
|
var sjcl = require('sjcl');
|
||||||
|
|
||||||
function WalletUtils() {};
|
function WalletUtils() {};
|
||||||
|
|
||||||
|
@ -61,8 +62,9 @@ WalletUtils.xPubToCopayerId = function(xpub) {
|
||||||
return (new Bitcore.HDPublicKey(xpub)).derive(HDPath.IdBranch).publicKey.toString();
|
return (new Bitcore.HDPublicKey(xpub)).derive(HDPath.IdBranch).publicKey.toString();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
<< << << < HEAD
|
||||||
WalletUtils.toSecret = function(walletId, walletPrivKey, network) {
|
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) {
|
WalletUtils.fromSecret = function(secret) {
|
||||||
|
@ -72,7 +74,7 @@ WalletUtils.fromSecret = function(secret) {
|
||||||
var networkChar = secretSplit[2];
|
var networkChar = secretSplit[2];
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
walletId: walletId,
|
walletId: walletId,
|
||||||
walletPrivKey: walletPrivKey,
|
walletPrivKey: walletPrivKey,
|
||||||
network: networkChar == 'T' ? 'testnet' : 'livenet',
|
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;
|
module.exports = WalletUtils;
|
||||||
|
|
|
@ -30,8 +30,9 @@
|
||||||
"morgan": "*",
|
"morgan": "*",
|
||||||
"npmlog": "^0.1.1",
|
"npmlog": "^0.1.1",
|
||||||
"preconditions": "^1.0.7",
|
"preconditions": "^1.0.7",
|
||||||
"request": "^2.53.0",
|
|
||||||
"qr-image": "*",
|
"qr-image": "*",
|
||||||
|
"request": "^2.53.0",
|
||||||
|
"sjcl": "^1.0.2",
|
||||||
"uuid": "*"
|
"uuid": "*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -65,4 +65,13 @@ describe('WalletUtils', function() {
|
||||||
WalletUtils.verifyMessage(aLongerText, sig, aPubKey).should.equal(true);
|
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');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue