2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { ethers } from "ethers";
|
|
|
|
|
2019-11-13 21:47:08 +09:00
|
|
|
import { scrypt } from "scrypt-js";
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-08-02 02:32:32 -04:00
|
|
|
import { version } from "./_version";
|
|
|
|
|
|
|
|
const logger = new ethers.utils.Logger(version);
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
let warned = false;
|
|
|
|
|
|
|
|
export class BrainWallet extends ethers.Wallet {
|
|
|
|
|
|
|
|
static _generate(username: ethers.Bytes | string, password: ethers.Bytes | string, legacy: boolean, progressCallback?: ethers.utils.ProgressCallback): Promise<BrainWallet> {
|
|
|
|
if (!warned) {
|
2019-08-02 02:32:32 -04:00
|
|
|
logger.warn("Warning: using Brain Wallets should be considered insecure (this warning will not be repeated)");
|
2019-05-14 18:25:46 -04:00
|
|
|
warned = true;
|
|
|
|
}
|
|
|
|
let usernameBytes: Uint8Array = null;
|
|
|
|
let passwordBytes: Uint8Array = null;
|
|
|
|
|
|
|
|
if (typeof(username) === 'string') {
|
2019-08-02 02:32:32 -04:00
|
|
|
logger.checkNormalize();
|
2019-05-14 18:25:46 -04:00
|
|
|
usernameBytes = ethers.utils.toUtf8Bytes(username.normalize('NFKC'));
|
|
|
|
} else {
|
|
|
|
usernameBytes = ethers.utils.arrayify(username);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(password) === 'string') {
|
2019-08-02 02:32:32 -04:00
|
|
|
logger.checkNormalize();
|
2019-05-14 18:25:46 -04:00
|
|
|
passwordBytes = ethers.utils.toUtf8Bytes(password.normalize('NFKC'));
|
|
|
|
} else {
|
|
|
|
passwordBytes = ethers.utils.arrayify(password);
|
|
|
|
}
|
|
|
|
|
2019-11-13 21:47:08 +09:00
|
|
|
return scrypt(passwordBytes, usernameBytes, (1 << 18), 8, 1, 32, progressCallback).then((key: Uint8Array) => {
|
|
|
|
if (legacy) {
|
|
|
|
return new BrainWallet(key);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-11-13 21:47:08 +09:00
|
|
|
}
|
|
|
|
const mnemonic = ethers.utils.entropyToMnemonic(ethers.utils.arrayify(key).slice(0, 16));
|
|
|
|
return new BrainWallet(ethers.Wallet.fromMnemonic(mnemonic));
|
2019-05-14 18:25:46 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static generate(username: ethers.Bytes | string, password: ethers.Bytes | string, progressCallback?: ethers.utils.ProgressCallback): Promise<BrainWallet> {
|
|
|
|
return BrainWallet._generate(username, password, false, progressCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static generateLegacy(username: ethers.Bytes | string, password: ethers.Bytes | string, progressCallback?: ethers.utils.ProgressCallback): Promise<BrainWallet> {
|
|
|
|
return BrainWallet._generate(username, password, true, progressCallback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// Test Legacy correctly matches our old test-vector:
|
|
|
|
// See: https://github.com/ethers-io/ethers.js/blob/3bf39b3bee0834566243211783ed8ec052c2f950/tests/test-wallet.js#L13
|
|
|
|
BrainWallet.generateLegacy("ricmoo", "password").then((wallet) => {
|
|
|
|
console.log("Expected:", "0xbed9d2E41BdD066f702C4bDB86eB3A3740101acC");
|
|
|
|
console.log(wallet);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
BrainWallet.generate("ricmoo", "password").then((wallet) => {
|
|
|
|
console.log("Expected:", "0xbed9d2E41BdD066f702C4bDB86eB3A3740101acC");
|
|
|
|
console.log(wallet);
|
|
|
|
});
|
|
|
|
*/
|