UNPKG

near-ca-test

Version:

An SDK for controlling Ethereum Accounts from a Near Account.

67 lines (66 loc) 3.19 kB
"use strict"; 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 __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupAdapter = setupAdapter; const near_api_js_1 = require("near-api-js"); const ethereum_1 = require("./chains/ethereum"); const mpcContract_1 = require("./mpcContract"); const near_1 = require("./chains/near"); __exportStar(require("./chains/ethereum"), exports); __exportStar(require("./chains/near"), exports); __exportStar(require("./mpcContract"), exports); __exportStar(require("./network"), exports); __exportStar(require("./types"), exports); __exportStar(require("./utils"), exports); /// Beta features __exportStar(require("./beta"), exports); /** * 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 {SetupConfig} args - The configuration parameters for setting up the adapter. * * @returns {Promise<NearEthAdapter>} - A promise that resolves to an instance of NearEthAdapter configured with the provided settings. * * @throws {Error} - Throws an error if the `accountId` does not match the networkId of the provided or inferred `network`. * @throws {Error} - Throws an 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, near_1.getNetworkId)(accountId); const config = args.network ?? (0, near_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, near_1.createNearAccount)(accountId, config, // Without private key, MPC contract connection is read-only. privateKey ? near_api_js_1.KeyPair.fromString(privateKey) : undefined); } catch (error) { console.error(`Failed to create NEAR account: ${error}`); throw error; } return ethereum_1.NearEthAdapter.fromConfig({ mpcContract: new mpcContract_1.MpcContract(account, mpcContractId, args.rootPublicKey), derivationPath: derivationPath, }); }