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

75 lines
3.4 KiB
JavaScript
Raw Normal View History

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; }) ||
2021-03-08 02:24:04 +03:00
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
2019-05-15 01:48:48 +03:00
return extendStatics(d, b);
};
return function (d, b) {
2021-03-08 02:24:04 +03:00
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
2019-05-15 01:48:48 +03:00
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 });
2021-03-08 02:24:04 +03:00
exports.decrypt = exports.CrowdsaleAccount = void 0;
2019-05-15 01:48:48 +03:00
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 09:10:58 +03:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2019-05-15 01:48:48 +03: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-12 00:57:04 +03:00
CrowdsaleAccount.prototype.isCrowdsaleAccount = function (value) {
return !!(value && value._isCrowdsaleAccount);
2019-05-15 01:48:48 +03:00
};
return CrowdsaleAccount;
}(properties_1.Description));
exports.CrowdsaleAccount = CrowdsaleAccount;
// See: https://github.com/ethereum/pyethsaletool
function decrypt(json, password) {
var data = JSON.parse(json);
2021-10-16 09:29:27 +03:00
password = (0, utils_1.getPassword)(password);
2019-05-15 01:48:48 +03:00
// Ethereum Address
2021-10-16 09:29:27 +03:00
var ethaddr = (0, address_1.getAddress)((0, utils_1.searchPath)(data, "ethaddr"));
2019-05-15 01:48:48 +03:00
// Encrypted Seed
2021-10-16 09:29:27 +03:00
var encseed = (0, utils_1.looseArrayify)((0, utils_1.searchPath)(data, "encseed"));
2019-05-15 01:48:48 +03:00
if (!encseed || (encseed.length % 16) !== 0) {
2019-08-02 09:10:58 +03:00
logger.throwArgumentError("invalid encseed", "json", json);
2019-05-15 01:48:48 +03:00
}
2021-10-16 09:29:27 +03:00
var key = (0, bytes_1.arrayify)((0, pbkdf2_1.pbkdf2)(password, password, 2000, 32, "sha256")).slice(0, 16);
2019-05-15 01:48:48 +03:00
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);
2021-10-16 09:29:27 +03:00
var seed = aes_js_1.default.padding.pkcs7.strip((0, bytes_1.arrayify)(aesCbc.decrypt(encryptedSeed)));
2019-05-15 01:48:48 +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]);
}
2021-10-16 09:29:27 +03:00
var seedHexBytes = (0, strings_1.toUtf8Bytes)(seedHex);
var privateKey = (0, keccak256_1.keccak256)(seedHexBytes);
2019-05-15 01:48:48 +03:00
return new CrowdsaleAccount({
2019-06-12 00:57:04 +03:00
_isCrowdsaleAccount: true,
2019-05-15 01:48:48 +03:00
address: ethaddr,
privateKey: privateKey
});
}
exports.decrypt = decrypt;
2020-07-13 15:03:56 +03:00
//# sourceMappingURL=crowdsale.js.map