ethers.js/lib.commonjs/wallet/json-crowdsale.js

60 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2022-09-05 16:57:11 -04:00
"use strict";
2022-11-30 15:44:23 -05:00
/**
* @_subsection: api/wallet:JSON Wallets [json-wallets]
*/
2022-09-05 16:57:11 -04:00
Object.defineProperty(exports, "__esModule", { value: true });
exports.decryptCrowdsaleJson = exports.isCrowdsaleJson = void 0;
const aes_js_1 = require("aes-js");
const index_js_1 = require("../address/index.js");
const index_js_2 = require("../crypto/index.js");
2022-09-15 22:58:45 -04:00
const index_js_3 = require("../hash/index.js");
const index_js_4 = require("../utils/index.js");
2022-09-05 16:57:11 -04:00
const utils_js_1 = require("./utils.js");
2022-11-30 15:44:23 -05:00
/**
* Returns true if %%json%% is a valid JSON Crowdsale wallet.
*/
2022-09-05 16:57:11 -04:00
function isCrowdsaleJson(json) {
try {
const data = JSON.parse(json);
if (data.encseed) {
return true;
}
}
catch (error) { }
return false;
}
exports.isCrowdsaleJson = isCrowdsaleJson;
// See: https://github.com/ethereum/pyethsaletool
2022-11-30 15:44:23 -05:00
/**
* Before Ethereum launched, it was necessary to create a wallet
* format for backers to use, which would be used to receive ether
* as a reward for contributing to the project.
*
* The [[link-crowdsale]] format is now obsolete, but it is still
* useful to support and the additional code is fairly trivial as
* all the primitives required are used through core portions of
* the library.
*/
2022-09-05 16:57:11 -04:00
function decryptCrowdsaleJson(json, _password) {
const data = JSON.parse(json);
const password = (0, utils_js_1.getPassword)(_password);
// Ethereum Address
const address = (0, index_js_1.getAddress)((0, utils_js_1.spelunk)(data, "ethaddr:string!"));
// Encrypted Seed
const encseed = (0, utils_js_1.looseArrayify)((0, utils_js_1.spelunk)(data, "encseed:string!"));
2022-11-09 02:57:02 -05:00
(0, index_js_4.assertArgument)(encseed && (encseed.length % 16) === 0, "invalid encseed", "json", json);
2022-09-15 22:58:45 -04:00
const key = (0, index_js_4.getBytes)((0, index_js_2.pbkdf2)(password, password, 2000, 32, "sha256")).slice(0, 16);
2022-09-05 16:57:11 -04:00
const iv = encseed.slice(0, 16);
const encryptedSeed = encseed.slice(16);
// Decrypt the seed
const aesCbc = new aes_js_1.CBC(key, iv);
2022-09-15 22:58:45 -04:00
const seed = (0, aes_js_1.pkcs7Strip)((0, index_js_4.getBytes)(aesCbc.decrypt(encryptedSeed)));
2022-09-05 16:57:11 -04:00
// This wallet format is weird... Convert the binary encoded hex to a string.
let seedHex = "";
for (let i = 0; i < seed.length; i++) {
seedHex += String.fromCharCode(seed[i]);
}
2022-09-15 22:58:45 -04:00
return { address, privateKey: (0, index_js_3.id)(seedHex) };
2022-09-05 16:57:11 -04:00
}
exports.decryptCrowdsaleJson = decryptCrowdsaleJson;
//# sourceMappingURL=json-crowdsale.js.map