2016-07-25 03:55:16 -04:00
|
|
|
'use strict';
|
2018-06-22 20:30:50 -04:00
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
2018-06-13 15:39:39 -04:00
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
|
|
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
|
|
result["default"] = mod;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2018-06-22 20:30:50 -04:00
|
|
|
var aes_js_1 = __importDefault(require("aes-js"));
|
|
|
|
var scrypt_js_1 = __importDefault(require("scrypt-js"));
|
|
|
|
var uuid_1 = __importDefault(require("uuid"));
|
2018-07-14 17:21:32 -04:00
|
|
|
var signing_key_1 = require("./signing-key");
|
|
|
|
var HDNode = __importStar(require("./hdnode"));
|
2018-09-24 16:07:14 -04:00
|
|
|
var address_1 = require("./address");
|
|
|
|
var bytes_1 = require("./bytes");
|
|
|
|
var pbkdf2_1 = require("./pbkdf2");
|
|
|
|
var keccak256_1 = require("./keccak256");
|
|
|
|
var utf8_1 = require("./utf8");
|
|
|
|
var random_bytes_1 = require("./random-bytes");
|
2018-06-13 15:39:39 -04:00
|
|
|
function looseArrayify(hexString) {
|
|
|
|
if (typeof (hexString) === 'string' && hexString.substring(0, 2) !== '0x') {
|
2017-02-24 14:41:24 -05:00
|
|
|
hexString = '0x' + hexString;
|
|
|
|
}
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.arrayify(hexString);
|
2017-02-24 14:41:24 -05:00
|
|
|
}
|
2018-01-26 21:56:20 -05:00
|
|
|
function zpad(value, length) {
|
|
|
|
value = String(value);
|
2018-06-13 15:39:39 -04:00
|
|
|
while (value.length < length) {
|
|
|
|
value = '0' + value;
|
|
|
|
}
|
2018-01-26 21:56:20 -05:00
|
|
|
return value;
|
|
|
|
}
|
2017-02-24 14:41:24 -05:00
|
|
|
function getPassword(password) {
|
2018-06-13 15:39:39 -04:00
|
|
|
if (typeof (password) === 'string') {
|
|
|
|
return utf8_1.toUtf8Bytes(password, utf8_1.UnicodeNormalizationForm.NFKC);
|
2017-02-24 14:41:24 -05:00
|
|
|
}
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.arrayify(password);
|
2017-02-24 14:41:24 -05:00
|
|
|
}
|
2016-07-23 03:27:14 -04:00
|
|
|
// Search an Object and its children recursively, caselessly.
|
|
|
|
function searchPath(object, path) {
|
|
|
|
var currentChild = object;
|
|
|
|
var comps = path.toLowerCase().split('/');
|
|
|
|
for (var i = 0; i < comps.length; i++) {
|
|
|
|
// Search for a child object with a case-insensitive matching key
|
|
|
|
var matchingChild = null;
|
|
|
|
for (var key in currentChild) {
|
2018-06-13 15:39:39 -04:00
|
|
|
if (key.toLowerCase() === comps[i]) {
|
|
|
|
matchingChild = currentChild[key];
|
|
|
|
break;
|
|
|
|
}
|
2016-07-23 03:27:14 -04:00
|
|
|
}
|
|
|
|
// Didn't find one. :'(
|
|
|
|
if (matchingChild === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Now check this child...
|
|
|
|
currentChild = matchingChild;
|
|
|
|
}
|
|
|
|
return currentChild;
|
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
// @TODO: Make a type for string or arrayish
|
2016-07-23 03:27:14 -04:00
|
|
|
// See: https://github.com/ethereum/pyethsaletool
|
2018-06-13 15:39:39 -04:00
|
|
|
function decryptCrowdsale(json, password) {
|
2016-07-23 03:27:14 -04:00
|
|
|
var data = JSON.parse(json);
|
2017-02-24 14:41:24 -05:00
|
|
|
password = getPassword(password);
|
2016-07-23 03:27:14 -04:00
|
|
|
// Ethereum Address
|
2018-06-13 15:39:39 -04:00
|
|
|
var ethaddr = address_1.getAddress(searchPath(data, 'ethaddr'));
|
2016-07-23 03:27:14 -04:00
|
|
|
// Encrypted Seed
|
2018-06-13 15:39:39 -04:00
|
|
|
var encseed = looseArrayify(searchPath(data, 'encseed'));
|
2016-07-23 03:27:14 -04:00
|
|
|
if (!encseed || (encseed.length % 16) !== 0) {
|
|
|
|
throw new Error('invalid encseed');
|
|
|
|
}
|
2018-06-19 02:12:57 -04:00
|
|
|
var key = pbkdf2_1.pbkdf2(password, password, 2000, 32, 'sha256').slice(0, 16);
|
2016-07-23 03:27:14 -04:00
|
|
|
var iv = encseed.slice(0, 16);
|
|
|
|
var encryptedSeed = encseed.slice(16);
|
|
|
|
// Decrypt the seed
|
2018-06-22 20:30:50 -04:00
|
|
|
var aesCbc = new aes_js_1.default.ModeOfOperation.cbc(key, iv);
|
2018-06-17 16:47:28 -04:00
|
|
|
var seed = bytes_1.arrayify(aesCbc.decrypt(encryptedSeed));
|
2018-06-22 20:30:50 -04:00
|
|
|
seed = aes_js_1.default.padding.pkcs7.strip(seed);
|
2016-07-23 03:27:14 -04:00
|
|
|
// This wallet format is weird... Convert the binary encoded hex to a string.
|
|
|
|
var seedHex = '';
|
|
|
|
for (var i = 0; i < seed.length; i++) {
|
|
|
|
seedHex += String.fromCharCode(seed[i]);
|
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
var seedHexBytes = utf8_1.toUtf8Bytes(seedHex);
|
|
|
|
var signingKey = new signing_key_1.SigningKey(keccak256_1.keccak256(seedHexBytes));
|
2016-07-23 03:27:14 -04:00
|
|
|
if (signingKey.address !== ethaddr) {
|
|
|
|
throw new Error('corrupt crowdsale wallet');
|
|
|
|
}
|
|
|
|
return signingKey;
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
exports.decryptCrowdsale = decryptCrowdsale;
|
|
|
|
//@TODO: string or arrayish
|
|
|
|
function decrypt(json, password, progressCallback) {
|
2016-07-23 03:27:14 -04:00
|
|
|
var data = JSON.parse(json);
|
2018-06-22 20:30:50 -04:00
|
|
|
var passwordBytes = getPassword(password);
|
2018-06-13 15:39:39 -04:00
|
|
|
var decrypt = function (key, ciphertext) {
|
2016-07-23 03:27:14 -04:00
|
|
|
var cipher = searchPath(data, 'crypto/cipher');
|
|
|
|
if (cipher === 'aes-128-ctr') {
|
2018-06-13 15:39:39 -04:00
|
|
|
var iv = looseArrayify(searchPath(data, 'crypto/cipherparams/iv'));
|
2018-06-22 20:30:50 -04:00
|
|
|
var counter = new aes_js_1.default.Counter(iv);
|
|
|
|
var aesCtr = new aes_js_1.default.ModeOfOperation.ctr(key, counter);
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.arrayify(aesCtr.decrypt(ciphertext));
|
2016-07-23 03:27:14 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2018-06-13 15:39:39 -04:00
|
|
|
var computeMAC = function (derivedHalf, ciphertext) {
|
2018-06-17 16:47:28 -04:00
|
|
|
return keccak256_1.keccak256(bytes_1.concat([derivedHalf, ciphertext]));
|
2018-06-13 15:39:39 -04:00
|
|
|
};
|
|
|
|
var getSigningKey = function (key, reject) {
|
|
|
|
var ciphertext = looseArrayify(searchPath(data, 'crypto/ciphertext'));
|
2018-06-17 16:47:28 -04:00
|
|
|
var computedMAC = bytes_1.hexlify(computeMAC(key.slice(16, 32), ciphertext)).substring(2);
|
2017-05-28 15:46:57 -05:00
|
|
|
if (computedMAC !== searchPath(data, 'crypto/mac').toLowerCase()) {
|
|
|
|
reject(new Error('invalid password'));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var privateKey = decrypt(key.slice(0, 16), ciphertext);
|
2018-01-26 21:56:20 -05:00
|
|
|
var mnemonicKey = key.slice(32, 64);
|
2017-05-28 15:46:57 -05:00
|
|
|
if (!privateKey) {
|
|
|
|
reject(new Error('unsupported cipher'));
|
|
|
|
return null;
|
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
var signingKey = new signing_key_1.SigningKey(privateKey);
|
2019-08-22 15:27:28 -04:00
|
|
|
if (data.address && signingKey.address !== address_1.getAddress(data.address)) {
|
2017-05-28 15:46:57 -05:00
|
|
|
reject(new Error('address mismatch'));
|
|
|
|
return null;
|
|
|
|
}
|
2018-01-26 21:56:20 -05:00
|
|
|
// Version 0.1 x-ethers metadata must contain an encrypted mnemonic phrase
|
2020-02-16 16:34:49 -05:00
|
|
|
var locale = searchPath(data, 'x-ethers/locale');
|
|
|
|
if (searchPath(data, 'x-ethers/version') === '0.1' && (locale == null || locale === "en")) {
|
2018-06-13 15:39:39 -04:00
|
|
|
var mnemonicCiphertext = looseArrayify(searchPath(data, 'x-ethers/mnemonicCiphertext'));
|
|
|
|
var mnemonicIv = looseArrayify(searchPath(data, 'x-ethers/mnemonicCounter'));
|
2018-06-22 20:30:50 -04:00
|
|
|
var mnemonicCounter = new aes_js_1.default.Counter(mnemonicIv);
|
|
|
|
var mnemonicAesCtr = new aes_js_1.default.ModeOfOperation.ctr(mnemonicKey, mnemonicCounter);
|
|
|
|
var path = searchPath(data, 'x-ethers/path') || HDNode.defaultPath;
|
2018-06-17 16:47:28 -04:00
|
|
|
var entropy = bytes_1.arrayify(mnemonicAesCtr.decrypt(mnemonicCiphertext));
|
2018-01-26 21:56:20 -05:00
|
|
|
var mnemonic = HDNode.entropyToMnemonic(entropy);
|
2018-06-13 15:39:39 -04:00
|
|
|
var node = HDNode.fromMnemonic(mnemonic).derivePath(path);
|
2018-06-17 16:47:28 -04:00
|
|
|
if (node.privateKey != bytes_1.hexlify(privateKey)) {
|
2018-01-26 21:56:20 -05:00
|
|
|
reject(new Error('mnemonic mismatch'));
|
|
|
|
return null;
|
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
signingKey = new signing_key_1.SigningKey(node);
|
2018-01-26 21:56:20 -05:00
|
|
|
}
|
2017-05-28 15:46:57 -05:00
|
|
|
return signingKey;
|
2018-06-13 15:39:39 -04:00
|
|
|
};
|
|
|
|
return new Promise(function (resolve, reject) {
|
2016-08-15 23:04:02 -04:00
|
|
|
var kdf = searchPath(data, 'crypto/kdf');
|
2018-06-13 15:39:39 -04:00
|
|
|
if (kdf && typeof (kdf) === 'string') {
|
2017-05-28 15:46:57 -05:00
|
|
|
if (kdf.toLowerCase() === 'scrypt') {
|
2018-06-13 15:39:39 -04:00
|
|
|
var salt = looseArrayify(searchPath(data, 'crypto/kdfparams/salt'));
|
2017-05-28 15:46:57 -05:00
|
|
|
var N = parseInt(searchPath(data, 'crypto/kdfparams/n'));
|
|
|
|
var r = parseInt(searchPath(data, 'crypto/kdfparams/r'));
|
|
|
|
var p = parseInt(searchPath(data, 'crypto/kdfparams/p'));
|
|
|
|
if (!N || !r || !p) {
|
|
|
|
reject(new Error('unsupported key-derivation function parameters'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Make sure N is a power of 2
|
|
|
|
if ((N & (N - 1)) !== 0) {
|
|
|
|
reject(new Error('unsupported key-derivation function parameter value for N'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var dkLen = parseInt(searchPath(data, 'crypto/kdfparams/dklen'));
|
|
|
|
if (dkLen !== 32) {
|
2018-06-13 15:39:39 -04:00
|
|
|
reject(new Error('unsupported key-derivation derived-key length'));
|
2017-05-28 15:46:57 -05:00
|
|
|
return;
|
|
|
|
}
|
2018-07-14 17:21:32 -04:00
|
|
|
if (progressCallback) {
|
|
|
|
progressCallback(0);
|
|
|
|
}
|
2018-06-22 20:30:50 -04:00
|
|
|
scrypt_js_1.default(passwordBytes, salt, N, r, p, 64, function (error, progress, key) {
|
2017-05-28 15:46:57 -05:00
|
|
|
if (error) {
|
|
|
|
error.progress = progress;
|
|
|
|
reject(error);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
else if (key) {
|
2018-06-17 16:47:28 -04:00
|
|
|
key = bytes_1.arrayify(key);
|
2017-05-29 23:08:47 -05:00
|
|
|
var signingKey = getSigningKey(key, reject);
|
2018-06-13 15:39:39 -04:00
|
|
|
if (!signingKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (progressCallback) {
|
|
|
|
progressCallback(1);
|
|
|
|
}
|
2017-05-28 15:46:57 -05:00
|
|
|
resolve(signingKey);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
else if (progressCallback) {
|
2017-05-28 15:46:57 -05:00
|
|
|
return progressCallback(progress);
|
2016-08-15 23:04:02 -04:00
|
|
|
}
|
2017-05-28 15:46:57 -05:00
|
|
|
});
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
else if (kdf.toLowerCase() === 'pbkdf2') {
|
|
|
|
var salt = looseArrayify(searchPath(data, 'crypto/kdfparams/salt'));
|
2017-05-28 15:46:57 -05:00
|
|
|
var prfFunc = null;
|
|
|
|
var prf = searchPath(data, 'crypto/kdfparams/prf');
|
|
|
|
if (prf === 'hmac-sha256') {
|
2018-06-19 02:12:57 -04:00
|
|
|
prfFunc = 'sha256';
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
else if (prf === 'hmac-sha512') {
|
2018-06-19 02:12:57 -04:00
|
|
|
prfFunc = 'sha512';
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
else {
|
2017-05-28 15:46:57 -05:00
|
|
|
reject(new Error('unsupported prf'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var c = parseInt(searchPath(data, 'crypto/kdfparams/c'));
|
|
|
|
var dkLen = parseInt(searchPath(data, 'crypto/kdfparams/dklen'));
|
|
|
|
if (dkLen !== 32) {
|
2018-06-13 15:39:39 -04:00
|
|
|
reject(new Error('unsupported key-derivation derived-key length'));
|
2017-05-28 15:46:57 -05:00
|
|
|
return;
|
|
|
|
}
|
2018-06-22 20:30:50 -04:00
|
|
|
var key = pbkdf2_1.pbkdf2(passwordBytes, salt, c, dkLen, prfFunc);
|
2017-05-29 23:08:47 -05:00
|
|
|
var signingKey = getSigningKey(key, reject);
|
2018-06-13 15:39:39 -04:00
|
|
|
if (!signingKey) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-28 15:46:57 -05:00
|
|
|
resolve(signingKey);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
else {
|
2017-05-28 15:46:57 -05:00
|
|
|
reject(new Error('unsupported key-derivation function'));
|
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-08-15 23:04:02 -04:00
|
|
|
reject(new Error('unsupported key-derivation function'));
|
|
|
|
}
|
|
|
|
});
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
exports.decrypt = decrypt;
|
|
|
|
function encrypt(privateKey, password, options, progressCallback) {
|
2016-07-23 03:27:14 -04:00
|
|
|
// the options are optional, so adjust the call as needed
|
2018-06-13 15:39:39 -04:00
|
|
|
if (typeof (options) === 'function' && !progressCallback) {
|
2016-08-15 23:04:02 -04:00
|
|
|
progressCallback = options;
|
2016-07-23 03:27:14 -04:00
|
|
|
options = {};
|
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
2016-07-23 03:27:14 -04:00
|
|
|
// Check the private key
|
2018-06-15 04:18:17 -04:00
|
|
|
var privateKeyBytes = null;
|
2018-07-25 21:20:21 -04:00
|
|
|
if (signing_key_1.SigningKey.isSigningKey(privateKey)) {
|
2018-06-17 16:47:28 -04:00
|
|
|
privateKeyBytes = bytes_1.arrayify(privateKey.privateKey);
|
2016-07-23 03:27:14 -04:00
|
|
|
}
|
2018-06-15 04:18:17 -04:00
|
|
|
else {
|
2018-06-17 16:47:28 -04:00
|
|
|
privateKeyBytes = bytes_1.arrayify(privateKey);
|
2018-06-15 04:18:17 -04:00
|
|
|
}
|
|
|
|
if (privateKeyBytes.length !== 32) {
|
2018-06-13 15:39:39 -04:00
|
|
|
throw new Error('invalid private key');
|
|
|
|
}
|
2018-06-22 20:30:50 -04:00
|
|
|
var passwordBytes = getPassword(password);
|
|
|
|
var entropy = null;
|
|
|
|
if (options.entropy) {
|
|
|
|
entropy = bytes_1.arrayify(options.entropy);
|
|
|
|
}
|
2018-01-26 21:56:20 -05:00
|
|
|
if (options.mnemonic) {
|
|
|
|
if (entropy) {
|
|
|
|
if (HDNode.entropyToMnemonic(entropy) !== options.mnemonic) {
|
|
|
|
throw new Error('entropy and mnemonic mismatch');
|
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
else {
|
2020-02-16 16:34:49 -05:00
|
|
|
entropy = bytes_1.arrayify(HDNode.mnemonicToEntropy(options.mnemonic, options.wordlist));
|
2018-01-26 21:56:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var path = options.path;
|
|
|
|
if (entropy && !path) {
|
2018-06-22 20:30:50 -04:00
|
|
|
path = HDNode.defaultPath;
|
2018-01-26 21:56:20 -05:00
|
|
|
}
|
|
|
|
var client = options.client;
|
2018-06-13 15:39:39 -04:00
|
|
|
if (!client) {
|
|
|
|
client = "ethers.js";
|
|
|
|
}
|
2016-08-01 14:14:53 -04:00
|
|
|
// Check/generate the salt
|
2018-06-22 20:30:50 -04:00
|
|
|
var salt = null;
|
|
|
|
if (options.salt) {
|
|
|
|
salt = bytes_1.arrayify(options.salt);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
salt = random_bytes_1.randomBytes(32);
|
|
|
|
;
|
2016-07-23 03:27:14 -04:00
|
|
|
}
|
|
|
|
// Override initialization vector
|
|
|
|
var iv = null;
|
|
|
|
if (options.iv) {
|
2018-06-17 16:47:28 -04:00
|
|
|
iv = bytes_1.arrayify(options.iv);
|
2018-06-13 15:39:39 -04:00
|
|
|
if (iv.length !== 16) {
|
|
|
|
throw new Error('invalid iv');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
iv = random_bytes_1.randomBytes(16);
|
2016-07-23 03:27:14 -04:00
|
|
|
}
|
|
|
|
// Override the uuid
|
2018-06-22 20:30:50 -04:00
|
|
|
var uuidRandom = null;
|
|
|
|
if (options.uuid) {
|
|
|
|
uuidRandom = bytes_1.arrayify(options.uuid);
|
2018-06-13 15:39:39 -04:00
|
|
|
if (uuidRandom.length !== 16) {
|
|
|
|
throw new Error('invalid uuid');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uuidRandom = random_bytes_1.randomBytes(16);
|
2016-07-23 03:27:14 -04:00
|
|
|
}
|
|
|
|
// Override the scrypt password-based key derivation function parameters
|
2016-08-01 14:14:53 -04:00
|
|
|
var N = (1 << 17), r = 8, p = 1;
|
2016-07-23 03:27:14 -04:00
|
|
|
if (options.scrypt) {
|
2018-06-13 15:39:39 -04:00
|
|
|
if (options.scrypt.N) {
|
|
|
|
N = options.scrypt.N;
|
|
|
|
}
|
|
|
|
if (options.scrypt.r) {
|
|
|
|
r = options.scrypt.r;
|
|
|
|
}
|
|
|
|
if (options.scrypt.p) {
|
|
|
|
p = options.scrypt.p;
|
|
|
|
}
|
2016-07-23 03:27:14 -04:00
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
return new Promise(function (resolve, reject) {
|
2018-07-14 17:21:32 -04:00
|
|
|
if (progressCallback) {
|
|
|
|
progressCallback(0);
|
|
|
|
}
|
2016-08-15 23:04:02 -04:00
|
|
|
// We take 64 bytes:
|
|
|
|
// - 32 bytes As normal for the Web3 secret storage (derivedKey, macPrefix)
|
2018-01-26 21:56:20 -05:00
|
|
|
// - 32 bytes AES key to encrypt mnemonic with (required here to be Ethers Wallet)
|
2018-06-22 20:30:50 -04:00
|
|
|
scrypt_js_1.default(passwordBytes, salt, N, r, p, 64, function (error, progress, key) {
|
2016-08-15 23:04:02 -04:00
|
|
|
if (error) {
|
|
|
|
error.progress = progress;
|
|
|
|
reject(error);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
else if (key) {
|
2018-06-17 16:47:28 -04:00
|
|
|
key = bytes_1.arrayify(key);
|
2018-01-26 21:56:20 -05:00
|
|
|
// This will be used to encrypt the wallet (as per Web3 secret storage)
|
2016-08-15 23:04:02 -04:00
|
|
|
var derivedKey = key.slice(0, 16);
|
|
|
|
var macPrefix = key.slice(16, 32);
|
2018-01-26 21:56:20 -05:00
|
|
|
// This will be used to encrypt the mnemonic phrase (if any)
|
|
|
|
var mnemonicKey = key.slice(32, 64);
|
2016-08-15 23:04:02 -04:00
|
|
|
// Get the address for this private key
|
2018-06-15 04:18:17 -04:00
|
|
|
var address = (new signing_key_1.SigningKey(privateKeyBytes)).address;
|
2016-08-15 23:04:02 -04:00
|
|
|
// Encrypt the private key
|
2018-06-22 20:30:50 -04:00
|
|
|
var counter = new aes_js_1.default.Counter(iv);
|
|
|
|
var aesCtr = new aes_js_1.default.ModeOfOperation.ctr(derivedKey, counter);
|
2018-06-17 16:47:28 -04:00
|
|
|
var ciphertext = bytes_1.arrayify(aesCtr.encrypt(privateKeyBytes));
|
2016-08-15 23:04:02 -04:00
|
|
|
// Compute the message authentication code, used to check the password
|
2018-06-17 16:47:28 -04:00
|
|
|
var mac = keccak256_1.keccak256(bytes_1.concat([macPrefix, ciphertext]));
|
2016-08-15 23:04:02 -04:00
|
|
|
// See: https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition
|
|
|
|
var data = {
|
2016-10-05 00:06:05 +08:00
|
|
|
address: address.substring(2).toLowerCase(),
|
2018-06-22 20:30:50 -04:00
|
|
|
id: uuid_1.default.v4({ random: uuidRandom }),
|
2016-08-15 23:04:02 -04:00
|
|
|
version: 3,
|
|
|
|
Crypto: {
|
|
|
|
cipher: 'aes-128-ctr',
|
|
|
|
cipherparams: {
|
2018-06-17 16:47:28 -04:00
|
|
|
iv: bytes_1.hexlify(iv).substring(2),
|
2016-08-15 23:04:02 -04:00
|
|
|
},
|
2018-06-17 16:47:28 -04:00
|
|
|
ciphertext: bytes_1.hexlify(ciphertext).substring(2),
|
2016-08-15 23:04:02 -04:00
|
|
|
kdf: 'scrypt',
|
|
|
|
kdfparams: {
|
2018-06-17 16:47:28 -04:00
|
|
|
salt: bytes_1.hexlify(salt).substring(2),
|
2016-08-15 23:04:02 -04:00
|
|
|
n: N,
|
|
|
|
dklen: 32,
|
|
|
|
p: p,
|
|
|
|
r: r
|
|
|
|
},
|
2017-02-24 14:41:24 -05:00
|
|
|
mac: mac.substring(2)
|
2016-08-15 23:04:02 -04:00
|
|
|
}
|
|
|
|
};
|
2018-01-26 21:56:20 -05:00
|
|
|
// If we have a mnemonic, encrypt it into the JSON wallet
|
|
|
|
if (entropy) {
|
2018-06-13 15:39:39 -04:00
|
|
|
var mnemonicIv = random_bytes_1.randomBytes(16);
|
2018-06-22 20:30:50 -04:00
|
|
|
var mnemonicCounter = new aes_js_1.default.Counter(mnemonicIv);
|
|
|
|
var mnemonicAesCtr = new aes_js_1.default.ModeOfOperation.ctr(mnemonicKey, mnemonicCounter);
|
2018-06-17 16:47:28 -04:00
|
|
|
var mnemonicCiphertext = bytes_1.arrayify(mnemonicAesCtr.encrypt(entropy));
|
2018-01-26 21:56:20 -05:00
|
|
|
var now = new Date();
|
|
|
|
var timestamp = (now.getUTCFullYear() + '-' +
|
2018-06-13 15:39:39 -04:00
|
|
|
zpad(now.getUTCMonth() + 1, 2) + '-' +
|
|
|
|
zpad(now.getUTCDate(), 2) + 'T' +
|
|
|
|
zpad(now.getUTCHours(), 2) + '-' +
|
|
|
|
zpad(now.getUTCMinutes(), 2) + '-' +
|
|
|
|
zpad(now.getUTCSeconds(), 2) + '.0Z');
|
2018-01-26 21:56:20 -05:00
|
|
|
data['x-ethers'] = {
|
|
|
|
client: client,
|
|
|
|
gethFilename: ('UTC--' + timestamp + '--' + data.address),
|
2018-06-17 16:47:28 -04:00
|
|
|
mnemonicCounter: bytes_1.hexlify(mnemonicIv).substring(2),
|
|
|
|
mnemonicCiphertext: bytes_1.hexlify(mnemonicCiphertext).substring(2),
|
2019-01-17 16:34:17 -05:00
|
|
|
path: path,
|
2018-01-26 21:56:20 -05:00
|
|
|
version: "0.1"
|
|
|
|
};
|
2020-02-16 16:34:49 -05:00
|
|
|
if (options.wordlist && typeof (options.wordlist.locale) === "string") {
|
|
|
|
data['x-ethers'].locale = options.wordlist.locale;
|
|
|
|
}
|
2018-01-26 21:56:20 -05:00
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
if (progressCallback) {
|
|
|
|
progressCallback(1);
|
|
|
|
}
|
2016-08-15 23:04:02 -04:00
|
|
|
resolve(JSON.stringify(data));
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
else if (progressCallback) {
|
2016-08-15 23:04:02 -04:00
|
|
|
return progressCallback(progress);
|
|
|
|
}
|
|
|
|
});
|
2016-07-23 03:27:14 -04:00
|
|
|
});
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
exports.encrypt = encrypt;
|