evm-blockchain-tools
Version:
This is a collection of resuseable tools to support development for EVM-powered blockchains
117 lines • 5.98 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.encodeFunctionSignature = encodeFunctionSignature;
exports.getOptimizedGasPrice = getOptimizedGasPrice;
exports.getOptimizedGasPriceV2 = getOptimizedGasPriceV2;
exports.generateKeyPair = generateKeyPair;
exports.aeadEnc = aeadEnc;
exports.aeadDec = aeadDec;
exports.norm24 = norm24;
exports.argonKey = argonKey;
exports.norm32KeyHex = norm32KeyHex;
exports.splitPrivateKeyToParts = splitPrivateKeyToParts;
exports.recoverPrivateKey = recoverPrivateKey;
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);
}
function getOptimizedGasPrice(gateway_1) {
return __awaiter(this, arguments, void 0, function* (gateway, addupGas = "1000000000", minGas = "5000000000") {
const gasPrice = yield gateway.provider.getGasPrice();
let newGas = gasPrice.add(addupGas);
return newGas.gte(minGas) ? newGas.toString() : minGas;
});
}
function getOptimizedGasPriceV2(provider_1) {
return __awaiter(this, arguments, void 0, function* (provider, addupGas = "1000000000", minGas = "5000000000", option) {
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;
});
}
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,
};
});
}
function aeadEnc(msgUtf8, nonce24, key32) {
const ad = undefined;
return libsodium_wrappers_1.default.crypto_secretbox_easy(libsodium_wrappers_1.default.from_string(msgUtf8), nonce24, key32);
}
function aeadDec(ct, nonce24, key32) {
return libsodium_wrappers_1.default.crypto_secretbox_open_easy(ct, nonce24, key32);
}
function norm24(n) {
if (n.length !== libsodium_wrappers_1.default.crypto_secretbox_NONCEBYTES)
throw new Error("nonce must be 24 bytes");
return new Uint8Array(n);
}
function argonKey(pass, salt16) {
return libsodium_wrappers_1.default.crypto_pwhash(32, pass, salt16, libsodium_wrappers_1.default.crypto_pwhash_OPSLIMIT_INTERACTIVE, libsodium_wrappers_1.default.crypto_pwhash_MEMLIMIT_INTERACTIVE, libsodium_wrappers_1.default.crypto_pwhash_ALG_DEFAULT);
}
function norm32KeyHex(hex) {
const clean = hex.startsWith("0x") ? hex.slice(2) : hex;
if (!/^[0-9a-fA-F]{64}$/.test(clean))
throw new Error("server key must be 32-byte hex");
return libsodium_wrappers_1.default.from_hex(clean);
}
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"),
];
}
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;
});
}
//# sourceMappingURL=web3-utils.js.map