ethers.js/packages/json-wallets/lib/crowdsale.js

72 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04: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 __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var aes_js_1 = __importDefault(require("aes-js"));
var address_1 = require("@ethersproject/address");
var bytes_1 = require("@ethersproject/bytes");
var keccak256_1 = require("@ethersproject/keccak256");
var pbkdf2_1 = require("@ethersproject/pbkdf2");
var strings_1 = require("@ethersproject/strings");
var properties_1 = require("@ethersproject/properties");
2019-08-02 02:10:58 -04:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2019-05-14 18:48:48 -04:00
var utils_1 = require("./utils");
var CrowdsaleAccount = /** @class */ (function (_super) {
__extends(CrowdsaleAccount, _super);
function CrowdsaleAccount() {
return _super !== null && _super.apply(this, arguments) || this;
}
2019-06-11 17:57:04 -04:00
CrowdsaleAccount.prototype.isCrowdsaleAccount = function (value) {
return !!(value && value._isCrowdsaleAccount);
2019-05-14 18:48:48 -04:00
};
return CrowdsaleAccount;
}(properties_1.Description));
exports.CrowdsaleAccount = CrowdsaleAccount;
// See: https://github.com/ethereum/pyethsaletool
function decrypt(json, password) {
var data = JSON.parse(json);
password = utils_1.getPassword(password);
// Ethereum Address
var ethaddr = address_1.getAddress(utils_1.searchPath(data, "ethaddr"));
// Encrypted Seed
var encseed = utils_1.looseArrayify(utils_1.searchPath(data, "encseed"));
if (!encseed || (encseed.length % 16) !== 0) {
2019-08-02 02:10:58 -04:00
logger.throwArgumentError("invalid encseed", "json", json);
2019-05-14 18:48:48 -04:00
}
var key = bytes_1.arrayify(pbkdf2_1.pbkdf2(password, password, 2000, 32, "sha256")).slice(0, 16);
var iv = encseed.slice(0, 16);
var encryptedSeed = encseed.slice(16);
// Decrypt the seed
var aesCbc = new aes_js_1.default.ModeOfOperation.cbc(key, iv);
2019-11-20 18:57:38 +09:00
var seed = aes_js_1.default.padding.pkcs7.strip(bytes_1.arrayify(aesCbc.decrypt(encryptedSeed)));
2019-05-14 18:48:48 -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]);
}
var seedHexBytes = strings_1.toUtf8Bytes(seedHex);
var privateKey = keccak256_1.keccak256(seedHexBytes);
return new CrowdsaleAccount({
2019-06-11 17:57:04 -04:00
_isCrowdsaleAccount: true,
2019-05-14 18:48:48 -04:00
address: ethaddr,
privateKey: privateKey
});
}
exports.decrypt = decrypt;