@flarenetwork/flare-stake-tool
Version:
Utilities for staking on the Flare network
226 lines • 8.6 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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__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.rpcUrlFromNetworkConfig = rpcUrlFromNetworkConfig;
exports.readContextFile = readContextFile;
exports.contextEnv = contextEnv;
exports.contextFile = contextFile;
exports.networkFromContextFile = networkFromContextFile;
exports.getContext = getContext;
exports.getNetworkConfig = getNetworkConfig;
exports.context = context;
exports.isDurango = isDurango;
const fs_1 = __importDefault(require("fs"));
const web3_1 = __importDefault(require("web3"));
const flarejs_1 = require("@flarenetwork/flarejs");
const network_1 = require("./constants/network");
const utils_1 = require("./utils");
const dotenv = __importStar(require("dotenv"));
/**
* @param network
* @returns {string} The RPC url of the given network
*/
function rpcUrlFromNetworkConfig(network) {
const config = getNetworkConfig(network);
return `${config.protocol}://${config.ip}/ext/bc/C/rpc`;
}
/**
* @description - parses the file and returns the context of ctx.json
* @param ctxFile - path to the context file
* @returns - context
*/
function readContextFile(ctxFile) {
const file = fs_1.default.readFileSync(ctxFile, 'utf8');
return JSON.parse(file);
}
/**
* @description Returns the context from .env file
* @param path - path to the .env file
* @param network - network info. can be localflare, costwo, flare
* @returns - returns the context from .env file
*/
function contextEnv(path, network) {
dotenv.config({ path: path });
return getContext(network, process.env.PUBLIC_KEY, process.env.PRIVATE_KEY_HEX, process.env.PRIVATE_KEY_CB58);
}
/**
* @description - returns context from the file
* @param ctxFile - path to the context file
* @returns returns the context
*/
function contextFile(ctxFile) {
const ctx = readContextFile(ctxFile);
return getContext(ctx.network, ctx.publicKey);
}
/**
* @description - returns the network from the context file
* @param ctxFile - context file
* @returns returns the network from the context
*/
function networkFromContextFile(ctxFile) {
const ctx = readContextFile(ctxFile);
return ctx.network;
}
/**
* @description returns the context
* @param network - network name: flare/localflare/costwo
* @param publicKey - public key
* @param privateKeyHex - private key in hex format
* @param privateKeyCB58 - private key in cb58 format
* @returns context
*/
function getContext(network, publicKey, privateKeyHex, privateKeyCB58) {
return context(getNetworkConfig(network), publicKey, privateKeyHex, privateKeyCB58, network);
}
/**
* @description - returns the network config based on network that was passed
* @param network - network name: flare/localflare/costwo
* @returns the network configuration
*/
function getNetworkConfig(network) {
let networkConfig;
if (network == 'flare' || network === undefined) {
networkConfig = network_1.flare;
}
else if (network == 'songbird') {
networkConfig = network_1.songbird;
}
else if (network == 'costwo') {
networkConfig = network_1.costwo;
}
else if (network == 'coston') {
networkConfig = network_1.coston;
}
else if (network == 'localflare') {
networkConfig = network_1.localflare;
}
else if (network == 'local') {
networkConfig = network_1.local;
}
else
throw Error('Invalid network');
return networkConfig;
}
/**
* The main function that returns the cotext
* @param config - network configuration
* @param publicKey - public key
* @param privkHex - private key in hex format
* @param privkCB58 - private key in cb58 format
* @param network - network name
* @returns the context object
*/
function context(config, publicKey, privkHex, privkCB58, network) {
const { protocol, ip, port, networkID: _, chainID } = config;
// those two addresses should be derived for most cli applications
let cAddressHex;
let addressBech32;
// derive private key in both cb58 and hex if only one is provided
if (privkHex !== undefined && privkHex !== "") {
privkHex = (0, utils_1.unPrefix0x)(privkHex);
const privkBuf = Buffer.from(privkHex, "hex");
privkCB58 = flarejs_1.utils.base58check.encode(privkBuf);
}
else if (privkCB58 !== undefined && privkCB58 !== "") {
const privkBuf = Buffer.from(flarejs_1.utils.base58check.decode(privkCB58));
privkHex = privkBuf.toString("hex");
}
// derive the public key coords if private key is present and check that they match
// the public key if provided
let publicKeyPair;
if (publicKey) {
publicKeyPair = (0, utils_1.decodePublicKey)(publicKey);
publicKey = '04' + Buffer.concat(publicKeyPair).toString('hex'); // standardize
}
if (privkHex) {
const [pubX, pubY] = (0, utils_1.privateKeyToPublicKey)(Buffer.from(privkHex, 'hex'));
if (publicKey) {
if (!publicKeyPair) {
throw Error('public key pair is not defined');
}
if (!publicKeyPair[0].equals(pubX) || !publicKeyPair[1].equals(pubY)) {
throw Error('provided private key does not match the public key');
}
}
publicKeyPair = [pubX, pubY];
if (!publicKey) {
publicKey = '04' + Buffer.concat(publicKeyPair).toString('hex'); // standardize
}
}
const path = '/ext/bc/C/rpc';
const iport = port ? `${ip}:${port}` : `${ip}`;
const rpcurl = `${protocol}://${iport}`;
const web3 = new web3_1.default(`${rpcurl}${path}`);
// derive addresses from public key if provided (bech32 is later derived again)
if (publicKey) {
cAddressHex = (0, utils_1.publicKeyToEthereumAddressString)(publicKey);
cAddressHex = web3.utils.toChecksumAddress(cAddressHex); // add checksum
addressBech32 = (0, utils_1.publicKeyToBech32AddressString)(publicKey, config.hrp);
}
const pAddressBech32 = /*pAddressStrings[0] ||*/ `P-${addressBech32}`;
const cAddressBech32 = /*cAddressStrings[0] ||*/ `C-${addressBech32}`;
if (privkHex) {
const prefixPrivkHex = privkHex.startsWith('0x') ? privkHex : `0x${privkHex}`;
const cAccount = web3.eth.accounts.privateKeyToAccount(prefixPrivkHex);
const _cAddressHex = cAccount.address;
if (cAddressHex && cAddressHex.toLowerCase() !== _cAddressHex.toLowerCase()) {
throw Error('C-chain address does not match private key');
}
cAddressHex = _cAddressHex;
}
return {
privkHex: privkHex,
privkCB58: privkCB58,
publicKey: publicKeyPair,
rpcurl: rpcurl,
web3: web3,
pAddressBech32: pAddressBech32,
cAddressBech32: cAddressBech32,
cAddressHex: cAddressHex,
config: config,
chainID: chainID,
network: network
};
}
function isDurango(network) {
const cfg = getNetworkConfig(network);
if (!cfg)
return false;
return Date.now() >= cfg.DurangoTime.getTime();
}
//# sourceMappingURL=context.js.map