etkframework
Version:
First test release of Etk over colored coins SDK
77 lines (70 loc) • 2.78 kB
JavaScript
/**
* Etk Helper library.
* Core Developer(s): @akurnya Akul Mathur
* Maintainer(s):
* @akurnya Akul Mathur
* Description:
* Key generation using BIP 38 and extremely randomized key
*/
const
/*
encrypt = require("./kdfCrypt").encrypt,
decrypt = require("./kdfCrypt").decrypt,
hashCipherText = require("./kdfCrypt").hashCipherText,
dehashCipherText = require("./kdfCrypt").dehashCipherText,
wifPvtKeyDecode = require("wifCrypto").wifPvtKeyDecode,
pvtKeyWIFEncode = require("wifCrypto").pvtKeyWIFEncode,
*/
Bip38 = require('bip38'),
bip38 = new Bip38(),
bipEncrypt = function bipEncrypt (privateKeyWif, passphrase, pubAddr) {
/**
* Get the randomized generated key from the inputs:
* - password and;
* - The Pvt string to encrypt
* as input pvtKey to this function.
*/
return bip38.encrypt(privateKeyWif, passphrase, pubAddr);
},
bipDecrypt = function bipDecrypt (encKey, passphrase) {
//yield the privateKeyWif
return bip38.decrypt(encKey, passphrase);
},
//OBE
keyEncrypt = function keyEncrypt (password, pvtkey, pubAddr, storeCB) {
/**encrypt and generate hashed cipherText as the input passphrase
* to BIP38 Base58 WIF Encoding
*/
encrypt(password, pvtkey, function encryptCB(cipherText, buf, iv, err) {
if (cipherText === iv && iv === null) {
throw Error(err);
}
const
privateKeyWif = pvtKeyWIFEncode(pvtkey),
/**Base58 WIF Encoded
* Attacker has to know 3 things to get pvtkey
* encryptedWIFKey, privateKeyWif, cipherText.
* If I don't know cipherText, I cannot retrieve the pvtkey.
*/
encryptedWIFKey = bipEncrypt(privateKeyWif, cipherText, pubAddr);
/**
* store buf:iv:encryptedWIFKey
* additionally,
* the same user DB store can store the cryptic cipherText using:
* hashCipherText(cipherText, password)
* buf:iv:cryptedText:encryptedWIFKey
*/
storeCB(buf, iv, encryptedWIFKey);
});
},
keyDecrypt = function keyDecrypt (encryptedWIFKey, cryptedText, password, cb) {
//regenerate the passphrase key
const
cipherText = dehashCipherText (cryptedText, password),
privateKeyWif = bipDecrypt (encryptedWIFKey, cipherText),
pvtkey = wifPvtKeyDecode(privateKeyWif);
//Perform Operation with this pvtkey
cb(pvtkey);
};
exports.bipEncrypt = bipEncrypt;
exports.bipDecrypt = bipDecrypt;