2019-05-15 01:48:48 +03:00
|
|
|
"use strict";
|
|
|
|
var __extends = (this && this.__extends) || (function () {
|
|
|
|
var extendStatics = function (d, b) {
|
|
|
|
extendStatics = Object.setPrototypeOf ||
|
|
|
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
|
|
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
|
|
return extendStatics(d, b);
|
|
|
|
};
|
|
|
|
return function (d, b) {
|
|
|
|
extendStatics(d, b);
|
|
|
|
function __() { this.constructor = d; }
|
|
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
var ethers_1 = require("ethers");
|
2019-11-20 12:57:38 +03:00
|
|
|
var scrypt_js_1 = require("scrypt-js");
|
2019-08-02 09:32:32 +03:00
|
|
|
var _version_1 = require("./_version");
|
|
|
|
var logger = new ethers_1.ethers.utils.Logger(_version_1.version);
|
2019-05-15 01:48:48 +03:00
|
|
|
var warned = false;
|
|
|
|
var BrainWallet = /** @class */ (function (_super) {
|
|
|
|
__extends(BrainWallet, _super);
|
|
|
|
function BrainWallet() {
|
|
|
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
|
|
}
|
|
|
|
BrainWallet._generate = function (username, password, legacy, progressCallback) {
|
|
|
|
if (!warned) {
|
2019-08-02 09:32:32 +03:00
|
|
|
logger.warn("Warning: using Brain Wallets should be considered insecure (this warning will not be repeated)");
|
2019-05-15 01:48:48 +03:00
|
|
|
warned = true;
|
|
|
|
}
|
|
|
|
var usernameBytes = null;
|
|
|
|
var passwordBytes = null;
|
|
|
|
if (typeof (username) === 'string') {
|
2019-08-02 09:32:32 +03:00
|
|
|
logger.checkNormalize();
|
2019-05-15 01:48:48 +03:00
|
|
|
usernameBytes = ethers_1.ethers.utils.toUtf8Bytes(username.normalize('NFKC'));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
usernameBytes = ethers_1.ethers.utils.arrayify(username);
|
|
|
|
}
|
|
|
|
if (typeof (password) === 'string') {
|
2019-08-02 09:32:32 +03:00
|
|
|
logger.checkNormalize();
|
2019-05-15 01:48:48 +03:00
|
|
|
passwordBytes = ethers_1.ethers.utils.toUtf8Bytes(password.normalize('NFKC'));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
passwordBytes = ethers_1.ethers.utils.arrayify(password);
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
return scrypt_js_1.scrypt(passwordBytes, usernameBytes, (1 << 18), 8, 1, 32, progressCallback).then(function (key) {
|
|
|
|
if (legacy) {
|
|
|
|
return new BrainWallet(key);
|
|
|
|
}
|
|
|
|
var mnemonic = ethers_1.ethers.utils.entropyToMnemonic(ethers_1.ethers.utils.arrayify(key).slice(0, 16));
|
|
|
|
return new BrainWallet(ethers_1.ethers.Wallet.fromMnemonic(mnemonic));
|
2019-05-15 01:48:48 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
BrainWallet.generate = function (username, password, progressCallback) {
|
|
|
|
return BrainWallet._generate(username, password, false, progressCallback);
|
|
|
|
};
|
|
|
|
BrainWallet.generateLegacy = function (username, password, progressCallback) {
|
|
|
|
return BrainWallet._generate(username, password, true, progressCallback);
|
|
|
|
};
|
|
|
|
return BrainWallet;
|
|
|
|
}(ethers_1.ethers.Wallet));
|
|
|
|
exports.BrainWallet = BrainWallet;
|