UNPKG

near-ca

Version:

An SDK for controlling Ethereum Accounts from a Near Account.

69 lines (68 loc) 2.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createNearAccount = exports.nearAccountFromAccountId = exports.nearAccountFromKeyPair = exports.TGAS = void 0; exports.getNetworkId = getNetworkId; exports.configFromNetworkId = configFromNetworkId; const near_api_js_1 = require("near-api-js"); const providers_1 = require("near-api-js/lib/providers"); /** Gas unit constant for NEAR transactions (1 TeraGas) */ exports.TGAS = 1000000000000n; /** * Extracts the network ID from a given NEAR account ID * * @param accountId - The NEAR account ID to analyze * @returns The network ID ("mainnet" or "testnet") * @remarks If the account ID doesn't end with "near" or "testnet", defaults to "mainnet" */ function getNetworkId(accountId) { const accountExt = accountId.split(".").pop() || ""; // Consider anything that isn't testnet as mainnet. return accountExt !== "testnet" ? "mainnet" : accountExt; } /** * Generates a NEAR configuration object for a specific network * * @param networkId - The target network identifier * @returns Configuration object for NEAR connection */ function configFromNetworkId(networkId) { return { networkId, nodeUrl: `https://rpc.${networkId}.near.org`, }; } /** * Creates a NEAR Account instance from provided credentials * * @param config - Configuration containing account ID, network, and key pair * @returns A NEAR Account instance */ const nearAccountFromKeyPair = async (config) => { return (0, exports.createNearAccount)(config.accountId, config.network, config.keyPair); }; exports.nearAccountFromKeyPair = nearAccountFromKeyPair; /** * Creates a read-only NEAR Account instance from an account ID * * @param accountId - The NEAR account identifier * @param network - The NEAR network configuration * @returns A read-only NEAR Account instance * @remarks This account cannot perform write operations */ const nearAccountFromAccountId = async (accountId, network) => { return (0, exports.createNearAccount)(accountId, network); }; exports.nearAccountFromAccountId = nearAccountFromAccountId; /** * Creates a NEAR Account instance with optional write capabilities * * @param accountId - The NEAR account identifier * @param network - The NEAR network configuration * @param keyPair - Optional key pair for write access * @returns A NEAR Account instance */ const createNearAccount = async (accountId, network, keyPair) => { const provider = new providers_1.JsonRpcProvider({ url: network.nodeUrl }); return new near_api_js_1.Account(accountId, provider, keyPair ? near_api_js_1.KeyPairSigner.fromSecretKey(keyPair.toString()) : undefined); }; exports.createNearAccount = createNearAccount;