2019-05-15 01:25:46 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import aes from "aes-js";
|
2019-11-20 12:29:42 +03:00
|
|
|
import * as scrypt from "scrypt-js";
|
2019-05-15 01:25:46 +03:00
|
|
|
import uuid from "uuid";
|
|
|
|
|
|
|
|
import { ExternallyOwnedAccount } from "@ethersproject/abstract-signer";
|
|
|
|
import { getAddress } from "@ethersproject/address";
|
|
|
|
import { arrayify, Bytes, BytesLike, concat, hexlify } from "@ethersproject/bytes";
|
|
|
|
import { defaultPath, entropyToMnemonic, HDNode, mnemonicToEntropy } from "@ethersproject/hdnode";
|
|
|
|
import { keccak256 } from "@ethersproject/keccak256";
|
|
|
|
import { pbkdf2 } from "@ethersproject/pbkdf2";
|
|
|
|
import { randomBytes } from "@ethersproject/random";
|
|
|
|
import { Description } from "@ethersproject/properties";
|
|
|
|
import { computeAddress } from "@ethersproject/transactions";
|
|
|
|
|
|
|
|
import { getPassword, looseArrayify, searchPath, zpad } from "./utils";
|
|
|
|
|
|
|
|
// Exported Types
|
|
|
|
|
|
|
|
export class KeystoreAccount extends Description implements ExternallyOwnedAccount {
|
|
|
|
readonly address: string;
|
|
|
|
readonly privateKey: string;
|
|
|
|
readonly mnemonic?: string;
|
|
|
|
readonly path?: string;
|
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
readonly _isKeystoreAccount: boolean;
|
|
|
|
|
|
|
|
isKeystoreAccount(value: any): value is KeystoreAccount {
|
|
|
|
return !!(value && value._isKeystoreAccount);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ProgressCallback = (percent: number) => void;
|
|
|
|
|
|
|
|
export type EncryptOptions = {
|
|
|
|
iv?: BytesLike;
|
|
|
|
entropy?: BytesLike;
|
|
|
|
client?: string;
|
|
|
|
salt?: BytesLike;
|
|
|
|
uuid?: string;
|
|
|
|
scrypt?: {
|
|
|
|
N?: number;
|
|
|
|
r?: number;
|
|
|
|
p?: number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:47:08 +03:00
|
|
|
export async function decrypt(json: string, password: Bytes | string, progressCallback?: ProgressCallback): Promise<KeystoreAccount> {
|
2019-11-01 17:33:51 +03:00
|
|
|
const data = JSON.parse(json);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const passwordBytes = getPassword(password);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const decrypt = function(key: Uint8Array, ciphertext: Uint8Array): Uint8Array {
|
|
|
|
const cipher = searchPath(data, "crypto/cipher");
|
2019-05-15 01:25:46 +03:00
|
|
|
if (cipher === "aes-128-ctr") {
|
2019-11-01 17:33:51 +03:00
|
|
|
const iv = looseArrayify(searchPath(data, "crypto/cipherparams/iv"))
|
|
|
|
const counter = new aes.Counter(iv);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const aesCtr = new aes.ModeOfOperation.ctr(key, counter);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
return arrayify(aesCtr.decrypt(ciphertext));
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const computeMAC = function(derivedHalf: Uint8Array, ciphertext: Uint8Array) {
|
2019-05-15 01:25:46 +03:00
|
|
|
return keccak256(concat([ derivedHalf, ciphertext ]));
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:47:08 +03:00
|
|
|
const getAccount = async function(key: Uint8Array): Promise<KeystoreAccount> {
|
2019-11-01 17:33:51 +03:00
|
|
|
const ciphertext = looseArrayify(searchPath(data, "crypto/ciphertext"));
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const computedMAC = hexlify(computeMAC(key.slice(16, 32), ciphertext)).substring(2);
|
2019-05-15 01:25:46 +03:00
|
|
|
if (computedMAC !== searchPath(data, "crypto/mac").toLowerCase()) {
|
2019-11-13 15:47:08 +03:00
|
|
|
throw new Error("invalid password");
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const privateKey = decrypt(key.slice(0, 16), ciphertext);
|
|
|
|
const mnemonicKey = key.slice(32, 64);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
if (!privateKey) {
|
2019-11-13 15:47:08 +03:00
|
|
|
throw new Error("unsupported cipher");
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const address = computeAddress(privateKey);
|
2019-08-22 23:00:53 +03:00
|
|
|
if (data.address) {
|
|
|
|
let check = data.address.toLowerCase();
|
|
|
|
if (check.substring(0, 2) !== "0x") { check = "0x" + check; }
|
|
|
|
|
2019-11-13 15:47:08 +03:00
|
|
|
if (getAddress(check) !== address) {
|
|
|
|
throw new Error("address mismatch");
|
|
|
|
}
|
2019-08-22 23:00:53 +03:00
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const account: any = {
|
2019-06-11 05:25:46 +03:00
|
|
|
_isKeystoreAccount: true,
|
2019-05-15 01:25:46 +03:00
|
|
|
address: address,
|
|
|
|
privateKey: hexlify(privateKey)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Version 0.1 x-ethers metadata must contain an encrypted mnemonic phrase
|
|
|
|
if (searchPath(data, "x-ethers/version") === "0.1") {
|
2019-11-01 17:33:51 +03:00
|
|
|
const mnemonicCiphertext = looseArrayify(searchPath(data, "x-ethers/mnemonicCiphertext"));
|
|
|
|
const mnemonicIv = looseArrayify(searchPath(data, "x-ethers/mnemonicCounter"));
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const mnemonicCounter = new aes.Counter(mnemonicIv);
|
|
|
|
const mnemonicAesCtr = new aes.ModeOfOperation.ctr(mnemonicKey, mnemonicCounter);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const path = searchPath(data, "x-ethers/path") || defaultPath;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const entropy = arrayify(mnemonicAesCtr.decrypt(mnemonicCiphertext));
|
|
|
|
const mnemonic = entropyToMnemonic(entropy);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const node = HDNode.fromMnemonic(mnemonic).derivePath(path);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
if (node.privateKey != account.privateKey) {
|
2019-11-13 15:47:08 +03:00
|
|
|
throw new Error("mnemonic mismatch");
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
account.mnemonic = node.mnemonic;
|
|
|
|
account.path = node.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new KeystoreAccount(account);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-13 15:47:08 +03:00
|
|
|
const kdf = searchPath(data, "crypto/kdf");
|
|
|
|
if (kdf && typeof(kdf) === "string") {
|
|
|
|
if (kdf.toLowerCase() === "scrypt") {
|
|
|
|
const salt = looseArrayify(searchPath(data, "crypto/kdfparams/salt"));
|
|
|
|
const N = parseInt(searchPath(data, "crypto/kdfparams/n"));
|
|
|
|
const r = parseInt(searchPath(data, "crypto/kdfparams/r"));
|
|
|
|
const p = parseInt(searchPath(data, "crypto/kdfparams/p"));
|
|
|
|
if (!N || !r || !p) {
|
|
|
|
throw new Error("unsupported key-derivation function parameters");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure N is a power of 2
|
|
|
|
if ((N & (N - 1)) !== 0) {
|
|
|
|
throw new Error("unsupported key-derivation function parameter value for N");
|
|
|
|
}
|
|
|
|
|
|
|
|
const dkLen = parseInt(searchPath(data, "crypto/kdfparams/dklen"));
|
|
|
|
if (dkLen !== 32) {
|
|
|
|
throw new Error("unsupported key-derivation derived-key length");
|
|
|
|
}
|
|
|
|
|
2019-11-20 12:29:42 +03:00
|
|
|
const key = await scrypt.scrypt(passwordBytes, salt, N, r, p, 64, progressCallback);
|
2019-11-13 15:47:08 +03:00
|
|
|
//key = arrayify(key);
|
|
|
|
|
|
|
|
return getAccount(key);
|
|
|
|
|
|
|
|
} else if (kdf.toLowerCase() === "pbkdf2") {
|
|
|
|
|
|
|
|
const salt = looseArrayify(searchPath(data, "crypto/kdfparams/salt"));
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-13 15:47:08 +03:00
|
|
|
let prfFunc: string = null;
|
|
|
|
const prf = searchPath(data, "crypto/kdfparams/prf");
|
|
|
|
if (prf === "hmac-sha256") {
|
|
|
|
prfFunc = "sha256";
|
|
|
|
} else if (prf === "hmac-sha512") {
|
|
|
|
prfFunc = "sha512";
|
2019-05-15 01:25:46 +03:00
|
|
|
} else {
|
2019-11-13 15:47:08 +03:00
|
|
|
throw new Error("unsupported prf");
|
|
|
|
}
|
|
|
|
|
|
|
|
const c = parseInt(searchPath(data, "crypto/kdfparams/c"));
|
|
|
|
|
|
|
|
const dkLen = parseInt(searchPath(data, "crypto/kdfparams/dklen"));
|
|
|
|
if (dkLen !== 32) {
|
|
|
|
throw new Error("unsupported key-derivation derived-key length");
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-13 15:47:08 +03:00
|
|
|
const key = arrayify(pbkdf2(passwordBytes, salt, c, dkLen, prfFunc));
|
|
|
|
|
|
|
|
return getAccount(key);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
2019-11-13 15:47:08 +03:00
|
|
|
}
|
|
|
|
throw new Error("unsupported key-derivation function");
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function encrypt(account: ExternallyOwnedAccount, password: Bytes | string, options?: EncryptOptions, progressCallback?: ProgressCallback): Promise<string> {
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (getAddress(account.address) !== computeAddress(account.privateKey)) {
|
|
|
|
throw new Error("address/privateKey mismatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (account.mnemonic != null){
|
2019-11-01 17:33:51 +03:00
|
|
|
const node = HDNode.fromMnemonic(account.mnemonic).derivePath(account.path || defaultPath);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
if (node.privateKey != account.privateKey) {
|
|
|
|
throw new Error("mnemonic mismatch");
|
|
|
|
}
|
|
|
|
} else if (account.path != null) {
|
|
|
|
throw new Error("cannot specify path without mnemonic");
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
return Promise.reject(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the options are optional, so adjust the call as needed
|
|
|
|
if (typeof(options) === "function" && !progressCallback) {
|
|
|
|
progressCallback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
if (!options) { options = {}; }
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const privateKey: Uint8Array = arrayify(account.privateKey);
|
|
|
|
const passwordBytes = getPassword(password);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
let entropy: Uint8Array = null
|
|
|
|
let path: string = account.path;
|
|
|
|
if (account.mnemonic) {
|
|
|
|
entropy = arrayify(mnemonicToEntropy(account.mnemonic));
|
|
|
|
if (!path) { path = defaultPath; }
|
|
|
|
}
|
|
|
|
|
|
|
|
let client = options.client;
|
|
|
|
if (!client) { client = "ethers.js"; }
|
|
|
|
|
|
|
|
// Check/generate the salt
|
|
|
|
let salt: Uint8Array = null;
|
|
|
|
if (options.salt) {
|
|
|
|
salt = arrayify(options.salt);
|
|
|
|
} else {
|
|
|
|
salt = randomBytes(32);;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override initialization vector
|
|
|
|
let iv: Uint8Array = null;
|
|
|
|
if (options.iv) {
|
|
|
|
iv = arrayify(options.iv);
|
|
|
|
if (iv.length !== 16) { throw new Error("invalid iv"); }
|
|
|
|
} else {
|
|
|
|
iv = randomBytes(16);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override the uuid
|
|
|
|
let uuidRandom: Uint8Array = null;
|
|
|
|
if (options.uuid) {
|
|
|
|
uuidRandom = arrayify(options.uuid);
|
|
|
|
if (uuidRandom.length !== 16) { throw new Error("invalid uuid"); }
|
|
|
|
} else {
|
|
|
|
uuidRandom = randomBytes(16);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override the scrypt password-based key derivation function parameters
|
|
|
|
let N = (1 << 17), r = 8, p = 1;
|
|
|
|
if (options.scrypt) {
|
|
|
|
if (options.scrypt.N) { N = options.scrypt.N; }
|
|
|
|
if (options.scrypt.r) { r = options.scrypt.r; }
|
|
|
|
if (options.scrypt.p) { p = options.scrypt.p; }
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:47:08 +03:00
|
|
|
// We take 64 bytes:
|
|
|
|
// - 32 bytes As normal for the Web3 secret storage (derivedKey, macPrefix)
|
|
|
|
// - 32 bytes AES key to encrypt mnemonic with (required here to be Ethers Wallet)
|
2019-11-20 12:29:42 +03:00
|
|
|
return scrypt.scrypt(passwordBytes, salt, N, r, p, 64, progressCallback).then((key) => {
|
2019-11-13 15:47:08 +03:00
|
|
|
key = arrayify(key);
|
|
|
|
|
|
|
|
// This will be used to encrypt the wallet (as per Web3 secret storage)
|
|
|
|
const derivedKey = key.slice(0, 16);
|
|
|
|
const macPrefix = key.slice(16, 32);
|
|
|
|
|
|
|
|
// This will be used to encrypt the mnemonic phrase (if any)
|
|
|
|
const mnemonicKey = key.slice(32, 64);
|
|
|
|
|
|
|
|
// Encrypt the private key
|
|
|
|
const counter = new aes.Counter(iv);
|
|
|
|
const aesCtr = new aes.ModeOfOperation.ctr(derivedKey, counter);
|
|
|
|
const ciphertext = arrayify(aesCtr.encrypt(privateKey));
|
|
|
|
|
|
|
|
// Compute the message authentication code, used to check the password
|
|
|
|
const mac = keccak256(concat([macPrefix, ciphertext]))
|
|
|
|
|
|
|
|
// See: https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition
|
|
|
|
const data: { [key: string]: any } = {
|
|
|
|
address: account.address.substring(2).toLowerCase(),
|
|
|
|
id: uuid.v4({ random: uuidRandom }),
|
|
|
|
version: 3,
|
|
|
|
Crypto: {
|
|
|
|
cipher: "aes-128-ctr",
|
|
|
|
cipherparams: {
|
|
|
|
iv: hexlify(iv).substring(2),
|
|
|
|
},
|
|
|
|
ciphertext: hexlify(ciphertext).substring(2),
|
|
|
|
kdf: "scrypt",
|
|
|
|
kdfparams: {
|
|
|
|
salt: hexlify(salt).substring(2),
|
|
|
|
n: N,
|
|
|
|
dklen: 32,
|
|
|
|
p: p,
|
|
|
|
r: r
|
|
|
|
},
|
|
|
|
mac: mac.substring(2)
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
2019-11-13 15:47:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// If we have a mnemonic, encrypt it into the JSON wallet
|
|
|
|
if (entropy) {
|
|
|
|
const mnemonicIv = randomBytes(16);
|
|
|
|
const mnemonicCounter = new aes.Counter(mnemonicIv);
|
|
|
|
const mnemonicAesCtr = new aes.ModeOfOperation.ctr(mnemonicKey, mnemonicCounter);
|
|
|
|
const mnemonicCiphertext = arrayify(mnemonicAesCtr.encrypt(entropy));
|
|
|
|
const now = new Date();
|
|
|
|
const 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),
|
|
|
|
mnemonicCounter: hexlify(mnemonicIv).substring(2),
|
|
|
|
mnemonicCiphertext: hexlify(mnemonicCiphertext).substring(2),
|
|
|
|
path: path,
|
|
|
|
version: "0.1"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(data);
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
|
|
|
}
|