near-ca-test
Version:
An SDK for controlling Ethereum Accounts from a Near Account.
77 lines (76 loc) • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNearAccount = exports.nearAccountFromAccountId = exports.nearAccountFromKeyPair = exports.NO_DEPOSIT = exports.TGAS = void 0;
exports.getNetworkId = getNetworkId;
exports.configFromNetworkId = configFromNetworkId;
const near_api_js_1 = require("near-api-js");
exports.TGAS = 1000000000000n;
exports.NO_DEPOSIT = "0";
/**
* Extracts the network ID from a given NEAR account ID.
* If the account ID does not end with "near" or "testnet", it logs a warning.
* Defaults to "mainnet" if the network ID is not "testnet".
*
* @param accountId - The NEAR account ID to extract the network ID from.
* @returns The network ID, either "mainnet" or "testnet".
*/
function getNetworkId(accountId) {
const accountExt = accountId.split(".").pop() || "";
if (!["near", "testnet"].includes(accountExt)) {
console.warn(`Unusual or invalid network extracted from accountId ${accountId}`);
}
// Consider anything that isn't testnet as mainnet.
return accountExt !== "testnet" ? "mainnet" : accountExt;
}
/**
* Generates a NEAR configuration object based on the provided network ID.
*
* @param networkId - The network ID, either "mainnet" or "testnet".
* @returns A NearConfig object containing the network ID and node URL.
*/
function configFromNetworkId(networkId) {
return {
networkId,
nodeUrl: `https://rpc.${networkId}.near.org`,
};
}
/**
* Loads Near Account from provided keyPair and accountId
*
* @param keyPair {KeyPair}
* @param accountId {string}
* @param network {NearConfig} network settings
* @returns A Promise that resolves to a NEAR Account instance.
*/
const nearAccountFromKeyPair = async (config) => {
return (0, exports.createNearAccount)(config.accountId, config.network, config.keyPair);
};
exports.nearAccountFromKeyPair = nearAccountFromKeyPair;
/** Minimally sufficient Account instance to construct readonly MpcContract connection.
* Can't be used to change methods.
*
* @param accountId {string}
* @param network {NearConfig} network settings
* @returns A Promise that resolves to a NEAR Account instance.
*/
const nearAccountFromAccountId = async (accountId, network) => {
return (0, exports.createNearAccount)(accountId, network);
};
exports.nearAccountFromAccountId = nearAccountFromAccountId;
/**
* Creates a NEAR account instance using the provided account ID, network configuration, and optional key pair.
*
* @param accountId - The NEAR account ID.
* @param network - The NEAR network configuration.
* @param keyPair - (Optional) The key pair for the account.
* @returns A Promise that resolves to a NEAR Account instance.
*/
const createNearAccount = async (accountId, network, keyPair) => {
const keyStore = new near_api_js_1.keyStores.InMemoryKeyStore();
if (keyPair) {
await keyStore.setKey(network.networkId, accountId, keyPair);
}
const near = await (0, near_api_js_1.connect)({ ...network, keyStore });
return near.account(accountId);
};
exports.createNearAccount = createNearAccount;