Skip to content

Instantly share code, notes, and snippets.

@AnthonyLaw
Last active June 15, 2018 09:32
Show Gist options
  • Save AnthonyLaw/2a8b903c647db3fb5550c0e198d2ec94 to your computer and use it in GitHub Desktop.
Save AnthonyLaw/2a8b903c647db3fb5550c0e198d2ec94 to your computer and use it in GitHub Desktop.
nanowallet .wlt Process
//create nem keypair. (get private key with public key)
// Encrypt private key using password
let encrypted = nem.crypto.helpers.encodePrivKey(privateKey, walletPassword);
/**
* Encode a private key using a password
*
* @param {string} privateKey - An hex private key
* @param {string} password - A password
*
* @return {object} - The encoded data
*/
var encodePrivKey = function encodePrivKey(privateKey, password) {
// Errors
if (!privateKey || !password) throw new Error('Missing argument !');
if (!_helpers2.default.isPrivateKeyValid(privateKey)) throw new Error('Private key is not valid !');
// Processing
var pass = derivePassSha(password, 20);
var r = encrypt(privateKey, _convert2.default.hex2ua(pass.priv));
// Result
return {
ciphertext: _cryptoJs2.default.enc.Hex.stringify(r.ciphertext),
iv: _convert2.default.ua2hex(r.iv)
};
};
/**
* Derive a private key from a password using count iterations of SHA3-256
*
* @param {string} password - A wallet password
* @param {number} count - A number of iterations above 0
*
* @return {object} - The derived private key
*/
var derivePassSha = function derivePassSha(password, count) {
// Errors
if (!password) throw new Error('Missing argument !');
if (!count || count <= 0) throw new Error('Please provide a count number above 0');
// Processing
var data = password;
console.time('sha3^n generation time');
for (var i = 0; i < count; ++i) {
data = _cryptoJs2.default.SHA3(data, {
outputLength: 256
});
}
console.timeEnd('sha3^n generation time');
// Result
return {
'priv': _cryptoJs2.default.enc.Hex.stringify(data)
};
};
/**
* Encrypt hex data using a key
*
* @param {string} data - An hex string
* @param {Uint8Array} key - An Uint8Array key
*
* @return {object} - The encrypted data
*/
var encrypt = function encrypt(data, key) {
// Errors
if (!data || !key) throw new Error('Missing argument !');
// Processing
var iv = _naclFast2.default.randomBytes(16);
var encKey = _convert2.default.ua2words(key, 32);
var encIv = {
iv: _convert2.default.ua2words(iv, 16)
};
var encrypted = _cryptoJs2.default.AES.encrypt(_cryptoJs2.default.enc.Hex.parse(data), encKey, encIv);
// Result
return {
ciphertext: encrypted.ciphertext,
iv: iv,
key: key
};
};
// Create bip32 remote amount using generated private key
return resolve(CryptoHelpers.generateBIP32Data(privateKey, walletPassword, 0, network).then((data) => {
// Construct the wallet object
let wallet = this.buildWallet(walletName, addr, true, "pass:bip32", encrypted, network, data.publicKey);
return wallet;
},
/**
* Create a wallet object
*
* @param {string} walletName - The wallet name
* @param {string} addr - The main account address
* @param {boolean} brain - Is brain or not
* @param {string} algo - The wallet algorithm
* @param {object} encrypted - The encrypted private key object
* @param {number} network - The network id
* @param {string} child - The public key of the account derived from seed
*
* @return {object} - A wallet object
*/
buildWallet(walletName, addr, brain, algo, encrypted, network, child) {
let wallet = {
"name": walletName,
"accounts": {
"0": {
"brain": brain,
"algo": algo,
"encrypted": encrypted.ciphertext || "",
"iv": encrypted.iv || "",
"address": addr.toUpperCase().replace(/-/g, ''),
"label": 'Primary',
"network": network,
"child": child
}
}
};
return wallet;
}
// Mobile Version
/**
* Encrypt a private key for mobile apps (AES_PBKF2)
*
* @param {string} password - A wallet password
* @param {string} privateKey - An account private key
*
* @return {object} - The encrypted data
*/
var toMobileKey = function toMobileKey(password, privateKey) {
// Errors
if (!password || !privateKey) throw new Error('Missing argument !');
if (!_helpers2.default.isPrivateKeyValid(privateKey)) throw new Error('Private key is not valid !');
// Processing
var salt = _cryptoJs2.default.lib.WordArray.random(256 / 8);
var key = _cryptoJs2.default.PBKDF2(password, salt, {
keySize: 256 / 32,
iterations: 2000
});
var iv = _naclFast2.default.randomBytes(16);
var encIv = {
iv: _convert2.default.ua2words(iv, 16)
};
var encrypted = _cryptoJs2.default.AES.encrypt(_cryptoJs2.default.enc.Hex.parse(privateKey), key, encIv);
// Result
return {
encrypted: _convert2.default.ua2hex(iv) + encrypted.ciphertext,
salt: salt.toString()
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment