UNPKG

near-ca

Version:

An SDK for controlling Ethereum Accounts from a Near Account.

45 lines (44 loc) 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupAdapter = setupAdapter; const near_api_js_1 = require("near-api-js"); const mpcContract_1 = require("./mpcContract"); const chains_1 = require("./chains"); const types_1 = require("./types"); /** * Sets up the NEAR-Ethereum adapter using the provided configuration * * This function establishes a connection to the NEAR network using the given * account details, configures the Multi-Party Computation (MPC) contract, and * returns an instance of the NearEthAdapter. * * @param args - The configuration parameters for setting up the adapter * @returns An instance of NearEthAdapter configured with the provided settings * @throws Error if the `accountId` does not match the networkId of the provided or inferred `network` * @throws Error if there is a failure in creating a NEAR account */ async function setupAdapter(args) { const { accountId, privateKey, mpcContractId, derivationPath = "ethereum,1", } = args; // Load near config from provided accountId if not provided const accountNetwork = (0, chains_1.getNetworkId)(accountId); const config = args.network ?? (0, chains_1.configFromNetworkId)(accountNetwork); if (accountNetwork !== config.networkId) { throw new Error(`accountId ${accountId} doesn't match the networkId ${config.networkId}. Please ensure that your accountId is correct and corresponds to the intended network.`); } let account; try { account = await (0, chains_1.createNearAccount)(accountId, config, // Without private key, MPC contract connection is read-only. privateKey && (0, types_1.isKeyPairString)(privateKey) ? near_api_js_1.KeyPair.fromString(privateKey) : undefined); } catch (error) { console.error(`Failed to create NEAR account: ${error}`); throw error; } return chains_1.NearEthAdapter.fromConfig({ mpcContract: new mpcContract_1.MpcContract(account, mpcContractId, args.rootPublicKey), derivationPath: derivationPath, }); }