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

55 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-11-30 23:44:23 +03:00
/**
* @_subsection: api/wallet:JSON Wallets [json-wallets]
*/
2022-09-05 23:57:11 +03:00
import { CBC, pkcs7Strip } from "aes-js";
import { getAddress } from "../address/index.js";
import { pbkdf2 } from "../crypto/index.js";
2022-09-16 05:58:45 +03:00
import { id } from "../hash/index.js";
2022-11-09 10:57:02 +03:00
import { getBytes, assertArgument } from "../utils/index.js";
2022-09-05 23:57:11 +03:00
import { getPassword, looseArrayify, spelunk } from "./utils.js";
2022-11-30 23:44:23 +03:00
/**
* Returns true if %%json%% is a valid JSON Crowdsale wallet.
*/
2022-09-05 23:57:11 +03:00
export function isCrowdsaleJson(json) {
try {
const data = JSON.parse(json);
if (data.encseed) {
return true;
}
}
catch (error) { }
return false;
}
// See: https://github.com/ethereum/pyethsaletool
2022-11-30 23:44:23 +03: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 23:57:11 +03:00
export function decryptCrowdsaleJson(json, _password) {
const data = JSON.parse(json);
const password = getPassword(_password);
// Ethereum Address
const address = getAddress(spelunk(data, "ethaddr:string!"));
// Encrypted Seed
const encseed = looseArrayify(spelunk(data, "encseed:string!"));
2022-11-09 10:57:02 +03:00
assertArgument(encseed && (encseed.length % 16) === 0, "invalid encseed", "json", json);
2022-09-16 05:58:45 +03:00
const key = getBytes(pbkdf2(password, password, 2000, 32, "sha256")).slice(0, 16);
2022-09-05 23:57:11 +03:00
const iv = encseed.slice(0, 16);
const encryptedSeed = encseed.slice(16);
// Decrypt the seed
const aesCbc = new CBC(key, iv);
2022-09-16 05:58:45 +03:00
const seed = pkcs7Strip(getBytes(aesCbc.decrypt(encryptedSeed)));
2022-09-05 23:57:11 +03: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]);
}
return { address, privateKey: id(seedHex) };
}
//# sourceMappingURL=json-crowdsale.js.map