etkframework
Version:
First test release of Etk over colored coins SDK
148 lines (121 loc) • 5.76 kB
JavaScript
/**
* Etk Helper library.
* Secure Private Key encryption and storage over server.
*
* Copyright (C) 2015 Akul Mathur
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Parts of the software are provided under separate licenses, as follows:
"colu-nodejs" SDK is under the MIT License
"pbkdf2-sha256" is under the BSD License
"bip38" is under the MIT License
"scryptsy" is under the MIT License
"coinstring" is under the MIT License
* Core Developer(s): @codecakes Akul Mathur
* Maintainer(s):
* @codecakes Akul Mathur
* Description:
* User Pvt Key Encryption Method and User Pvt Key Decrytion Method;
*
* How To Store User's Pvt Key.
* Input Params
* This is a one time Encryption Method since Pvt Key is generated once:
* - Password and Unique Inputs
* - PvtKey generated during registration
*/
const
compoundedSalt = require("./passCrypt").compoundedSalt,
pvtKeyWIFEncode = require("./wifCrypto").pvtKeyWIFEncode,
pvtKeyWIFDecode = require("./wifCrypto").pvtKeyWIFDecode,
hashedLen = require("./kdfCrypt").hashedLen,
encrypt = require("./kdfCrypt").encrypt,
hashCipherText = require("./kdfCrypt").hashCipherText,
bipEncrypt = require("./bipCrypt").bipEncrypt,
bipDecrypt = require("./bipCrypt").bipDecrypt,
// TODO: Test it
pvtKeyEnc = function pvtKeyEnc (masterpassword, pubAddr, pvtKey, secret, ivInputs, bufInputs, storeCB) {
/**
* Params in arguments:
* masterpassword
* inputs
*/
//if ( arguments.length < 2 ) {throw Error("Insufficient Arguments supplied"); }
ivInputs.unshift(masterpassword);
bufInputs.unshift(masterpassword);
var
ivSalt = hashedLen( hashCipherText( masterpassword, compoundedSalt.apply(this,ivInputs) ) ),
bufSalt = hashedLen( hashCipherText( masterpassword, compoundedSalt.apply(this,bufInputs) ) ),
privateKeyWif = pvtKeyWIFEncode(pvtKey);
// TODO: FIX iv and buf Salt length
encrypt(masterpassword, secret, ivSalt, bufSalt, function encryptCB(err, cipherText, iv) {
if (err) throw Error(err);
var
ivCipher = hashCipherText(cipherText, iv),
storeKey = bipEncrypt(privateKeyWif, ivCipher, pubAddr);
storeCB( storeKey );
});
},
// TODO: Test it
pvtKeyDec = function pvtKeyDec (masterpassword, secret, storeKey, ivInputs, bufInputs, cb) {
ivInputs.unshift(masterpassword);
bufInputs.unshift(masterpassword);
var
ivSalt = hashedLen( hashCipherText( masterpassword, compoundedSalt.apply(this,ivInputs) ) ),
bufSalt = hashedLen( hashCipherText( masterpassword, compoundedSalt.apply(this,bufInputs) ) );
encrypt(masterpassword, secret, ivSalt, bufSalt, function decryptCB(err, cipherText, iv) {
if (err) throw Error(err);
var
ivCipher = hashCipherText(cipherText, iv),
privateKeyWif = bipDecrypt (storeKey, ivCipher),
pvtKey = pvtKeyWIFDecode(privateKeyWif);
cb( pvtKey );
});
};
exports.pvtKeyEnc = pvtKeyEnc;
exports.pvtKeyDec = pvtKeyDec;
// For testing and debugging only
// var pvtKey = "8010B1BB119AD37D4B65A1022A314897B1B3614B345974332CB1B9582CF03536",
// pubAddr = "0409BA8621AEFD3B6BA4CA6D11A4746E8DF8D35D9B51B383338F627BA7FC7327318C3A6EC6ACD33C36328B8FB4349B31671BCD3A192316EA4F6236EE1AE4A7D8C9";
// var password = "breakItDown",
// secret = "Scotty too Notty",
// crypto = require("crypto"),
// randomBytesSize = 16,
// buf = new Buffer(crypto.randomBytes(randomBytesSize)),//this is the salt
// iv = new Buffer(crypto.randomBytes(randomBytesSize));
// console.log(buf.length);
// console.log(buf, iv);
// console.log(buf.toString('hex'), iv.toString('hex'));
// encrypt(password, secret, iv, buf, function(err, cipherText, iv) {
// if (err) throw(err);
// console.log(cipherText) ;
// });
// var secret = "Notty 2 Hotty",
// masterpassword = "HaiIsPass",
// ivInputs = ["band", "ho", "gaya", "Nee"],
// bufInputs = ["jhona", "launa"];
// var ivSalt = ( hashedLen( hashCipherText( masterpassword, compoundedSalt.apply(this,ivInputs) ) ) ),
// bufSalt = ( hashedLen( hashCipherText( masterpassword, compoundedSalt.apply(this,bufInputs) ) ) );
// console.log(ivSalt, bufSalt);
// console.log(ivSalt.toString('hex'), bufSalt.toString('hex'));
// encrypt(password, secret, ivSalt, bufSalt, function(err, cipherText, iv) {
// if (err) throw(err);
// console.log(cipherText) ;
// });
// ivInputs = ["band", "ho", "gaya", "Nee"];
// bufInputs = ["jhona", "launa"];
// pvtKeyEnc(masterpassword, pubAddr, pvtKey, secret, ivInputs, bufInputs, function (storeKey) {
// console.log(storeKey);
// ivInputs = ["band", "ho", "gaya", "Nee"];
// bufInputs = ["jhona", "launa"];
// pvtKeyDec (masterpassword, secret, storeKey, ivInputs, bufInputs, function (key) {
// console.log(key);
// })
// });