evm-blockchain-tools
Version:
This is a collection of resuseable tools to support development for EVM-powered blockchains
92 lines • 4.94 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.recoverPrivateKey = exports.splitPrivateKeyToParts = exports.generateKeyPair = exports.getOptimizedGasPriceV2 = exports.getOptimizedGasPrice = exports.encodeFunctionSignature = void 0;
const web3_1 = __importDefault(require("web3"));
const libsodium_wrappers_1 = __importDefault(require("libsodium-wrappers"));
const ethers_1 = require("ethers");
const constants_1 = require("../common/constants");
const web3 = new web3_1.default();
function encodeFunctionSignature(fnCall, params) {
return web3.eth.abi.encodeFunctionCall(fnCall, params);
}
exports.encodeFunctionSignature = encodeFunctionSignature;
function getOptimizedGasPrice(gateway, addupGas = "1000000000", minGas = "5000000000") {
return __awaiter(this, void 0, void 0, function* () {
const gasPrice = yield gateway.provider.getGasPrice();
let newGas = gasPrice.add(addupGas);
return newGas.gte(minGas) ? newGas.toString() : minGas;
});
}
exports.getOptimizedGasPrice = getOptimizedGasPrice;
function getOptimizedGasPriceV2(provider, addupGas = "1000000000", minGas = "5000000000", option) {
return __awaiter(this, void 0, void 0, function* () {
const gasPrice = yield provider.getGasPrice();
let newGas = gasPrice.add(addupGas);
if (option === null || option === void 0 ? void 0 : option.useOverridingGas) {
newGas = newGas.add(addupGas);
}
return newGas.gte(minGas) ? newGas.toString() : minGas;
});
}
exports.getOptimizedGasPriceV2 = getOptimizedGasPriceV2;
function generateKeyPair() {
return __awaiter(this, void 0, void 0, function* () {
const wallet = ethers_1.ethers.Wallet.createRandom();
const privateKey = wallet.privateKey.toString();
const publicKey = yield wallet.getAddress();
return {
privateKey,
publicKey,
};
});
}
exports.generateKeyPair = generateKeyPair;
function splitPrivateKeyToParts(privateKey, nonce) {
const cipherKey = libsodium_wrappers_1.default.crypto_secretbox_easy(privateKey, nonce, this.encryptionKey);
const bufferCipherKey = Buffer.from(cipherKey);
const shardSize = Math.floor(bufferCipherKey.length / 3);
const firstPart = bufferCipherKey.subarray(0, shardSize);
const secondPart = bufferCipherKey.subarray(shardSize, shardSize * 2);
const thirdPart = bufferCipherKey.subarray(shardSize * 2);
const userSecret = Buffer.concat([firstPart, secondPart]);
const serverSecret = Buffer.concat([secondPart, thirdPart]);
const recoverySecret = Buffer.concat([firstPart, thirdPart]);
return [
userSecret.toString("hex"),
serverSecret.toString("hex"),
recoverySecret.toString("hex"),
];
}
exports.splitPrivateKeyToParts = splitPrivateKeyToParts;
function recoverPrivateKey(data) {
return __awaiter(this, void 0, void 0, function* () {
const isValid1 = data.recoverySecret && data.serverSecret;
const isValid2 = data.userSecret && data.serverSecret;
const isValid3 = data.userSecret && data.recoverySecret;
if (!isValid1 && !isValid2 && !isValid3) {
throw new Error("must provide 2 out of 3 keys");
}
const firstPart = Buffer.from(data.userSecret || data.recoverySecret, "hex").subarray(0, constants_1.PRIVATE_KEY_SHARD_SIZE);
const secondPart = data.userSecret
? Buffer.from(data.userSecret, "hex").subarray(constants_1.PRIVATE_KEY_SHARD_SIZE)
: Buffer.from(data.serverSecret, "hex").subarray(0, constants_1.PRIVATE_KEY_SHARD_SIZE);
const thirdPart = Buffer.from(data.serverSecret || data.recoverySecret, "hex").subarray(constants_1.PRIVATE_KEY_SHARD_SIZE);
const reunited = Buffer.concat([firstPart, secondPart, thirdPart]);
const decrypted = libsodium_wrappers_1.default.crypto_secretbox_open_easy(reunited, Buffer.from(data.nonce, "hex"), this.encryptionKey, "text");
return decrypted;
});
}
exports.recoverPrivateKey = recoverPrivateKey;
//# sourceMappingURL=web3-utils.js.map