@jovijovi/ether-keystore
Version:
A keystore toolkit for Ethereum ecosystem
136 lines • 6.32 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.params = exports.errors = exports.ksCache = exports.types = exports.ksUtil = exports.ClearCache = exports.InspectKeystorePKWithoutPrefix = exports.InspectKeystorePK = exports.GetPK = exports.InspectKeystoreWallet = exports.GetWallet = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const util_1 = require("util");
const ethers_1 = require("ethers");
const pedrojs_common_1 = require("@jovijovi/pedrojs-common");
const ksutil_1 = require("./ksutil");
const cache_1 = require("./cache");
const params_1 = require("./params");
const errors = __importStar(require("./errors"));
// getCipherText returns cipher text from key file
async function getCipherText(address, keystoreType, keystoreDir = params_1.DefaultKeystoreDir) {
const keyPath = path_1.default.resolve(keystoreDir, keystoreType, address.toLowerCase());
pedrojs_common_1.auditor.Check(fs_1.default.existsSync(keyPath), (0, util_1.format)('%s, keyPath=%s', errors.ErrorKeyFileNotExist, keyPath));
return await pedrojs_common_1.util.retry.Run(() => {
return fs_1.default.readFileSync(keyPath, 'utf8');
}, params_1.DefaultRetryTimes, params_1.DefaultRetryInterval);
}
// GetWallet returns wallet from cipherText
async function GetWallet(cipherText, passphrase) {
return await ethers_1.Wallet.fromEncryptedJson(cipherText, passphrase);
}
exports.GetWallet = GetWallet;
// InspectKeystoreWallet returns the wallet from keystore and the keystore filename must match the lowercase address
async function InspectKeystoreWallet(address, keystoreType, passphrase, opts = {
isBase64: true,
useCache: true
}) {
pedrojs_common_1.auditor.Check(ethers_1.utils.isAddress(address), errors.ErrorInvalidAddress);
const key = (0, ksutil_1.CombinationKey)([cache_1.CacheType.Wallet, address]);
if (opts.useCache) {
if (cache_1.ksCache.has(key)) {
return cache_1.ksCache.get(key);
}
}
const cipherText = await getCipherText(address, keystoreType, opts.keystoreDir);
const wallet = await GetWallet(cipherText, opts.isBase64 ? new util_1.TextDecoder().decode(ethers_1.utils.base64.decode(passphrase)) : passphrase);
pedrojs_common_1.auditor.Check(address.toLowerCase() === wallet.address.toLowerCase(), `wallet address doesn't match`);
if (opts.useCache) {
cache_1.ksCache.set(key, wallet);
}
return wallet;
}
exports.InspectKeystoreWallet = InspectKeystoreWallet;
// GetPK returns wallet private key from cipherText
async function GetPK(cipherText, passphrase) {
const wallet = await GetWallet(cipherText, passphrase);
return wallet.privateKey;
}
exports.GetPK = GetPK;
// InspectKeystorePK returns the address PK from keystore
// and the keystore filename must match the lowercase address
async function InspectKeystorePK(address, keystoreType, passphrase, opts = {
isBase64: true,
useCache: true
}) {
pedrojs_common_1.auditor.Check(ethers_1.utils.isAddress(address), errors.ErrorInvalidAddress);
const key = (0, ksutil_1.CombinationKey)([cache_1.CacheType.PK, address]);
if (opts.useCache) {
if (cache_1.ksCache.has(key)) {
return cache_1.ksCache.get(key);
}
}
const cipherText = await getCipherText(address, keystoreType, opts.keystoreDir);
const pk = await GetPK(cipherText, opts.isBase64 ? new util_1.TextDecoder().decode(ethers_1.utils.base64.decode(passphrase)) : passphrase);
pedrojs_common_1.auditor.Check(ethers_1.utils.isHexString(pk), errors.ErrorInvalidPK);
if (opts.useCache) {
cache_1.ksCache.set(key, pk);
}
return pk;
}
exports.InspectKeystorePK = InspectKeystorePK;
// InspectKeystorePKWithoutPrefix returns the address PK (without prefix '0x) from keystore
// and the keystore filename must match the lowercase address
async function InspectKeystorePKWithoutPrefix(address, keystoreType, passphrase, opts = {
isBase64: true,
useCache: true
}) {
const key = (0, ksutil_1.CombinationKey)([cache_1.CacheType.PKWithoutPrefix, address]);
if (opts.useCache) {
if (cache_1.ksCache.has(key)) {
return cache_1.ksCache.get(key);
}
}
// Remove '0x' prefix of PK
const pk = (0, ksutil_1.Remove0xPrefix)(await InspectKeystorePK(address, keystoreType, passphrase, opts));
if (opts.useCache) {
cache_1.ksCache.set(key, pk);
}
return pk;
}
exports.InspectKeystorePKWithoutPrefix = InspectKeystorePKWithoutPrefix;
// Clear cache
function ClearCache() {
if (cache_1.ksCache) {
if (cache_1.ksCache.size > 0) {
cache_1.ksCache.clear();
}
}
}
exports.ClearCache = ClearCache;
exports.ksUtil = __importStar(require("./ksutil"));
exports.types = __importStar(require("./types"));
var cache_2 = require("./cache");
Object.defineProperty(exports, "ksCache", { enumerable: true, get: function () { return cache_2.ksCache; } });
exports.errors = __importStar(require("./errors"));
exports.params = __importStar(require("./params"));
//# sourceMappingURL=keystore.js.map