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