UNPKG

near-ca

Version:

An SDK for controlling Ethereum Accounts from a Near Account.

112 lines (111 loc) 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MockMpcContract = void 0; exports.mockAdapter = mockAdapter; const accounts_1 = require("viem/accounts"); const __1 = require(".."); /** * Converts a raw hexadecimal signature into a structured Signature object * * @param hexSignature - The raw hexadecimal signature (e.g., '0x...') * @returns A structured Signature object with fields r, s, v, and yParity * @throws Error if signature length is invalid */ function hexToSignature(hexSignature) { const cleanedHex = hexSignature.slice(2); if (cleanedHex.length !== 130) { throw new Error(`Invalid hex signature length: ${cleanedHex.length}. Expected 130 characters (65 bytes).`); } const v = BigInt(`0x${cleanedHex.slice(128, 130)}`); return { r: `0x${cleanedHex.slice(0, 64)}`, s: `0x${cleanedHex.slice(64, 128)}`, v, yParity: v === 27n ? 0 : v === 28n ? 1 : undefined, }; } /** Mock implementation of the MPC Contract interface for testing */ class MockMpcContract { /** * Creates a new mock MPC contract instance * * @param account - The NEAR account to use * @param privateKey - Optional private key (defaults to deterministic test key) */ constructor(account, privateKey) { /** * Returns the mock Ethereum address * * @returns The Ethereum address associated with the private key */ this.deriveEthAddress = async (_unused) => { return this.ethAccount.address; }; /** * Returns a mock deposit amount * * @returns A constant deposit value of "1" */ this.getDeposit = async () => { return "1"; }; /** * Signs a message using the mock private key * * @param signArgs - The signature request arguments * @returns The signature */ this.requestSignature = async (signArgs) => { const hexSignature = await this.ethAccount.sign({ hash: (0, __1.fromPayload)(signArgs.payload), }); return hexToSignature(hexSignature); }; this.connectedAccount = account; this.ethAccount = (0, accounts_1.privateKeyToAccount)(privateKey || "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"); } /** Gets the mock contract ID */ accountId() { return "mock-mpc.offline"; } /** * Encodes a mock signature request transaction * * @param signArgs - The signature request arguments * @param gas - Optional gas limit * @returns The encoded transaction */ async encodeSignatureRequestTx(signArgs, gas) { return { signerId: this.connectedAccount.accountId, receiverId: this.accountId(), actions: [ { type: "FunctionCall", params: { methodName: "sign", args: { request: signArgs }, gas: gas ? gas.toString() : "1", deposit: await this.getDeposit(), }, }, ], }; } } exports.MockMpcContract = MockMpcContract; /** * Creates a mock adapter instance for testing * * @param privateKey - Optional private key for the mock contract * @returns A configured NearEthAdapter instance */ async function mockAdapter(privateKey) { const account = await (0, __1.nearAccountFromAccountId)("mock-user.offline", { networkId: "testnet", nodeUrl: "https://rpc.testnet.near.org", }); const mpcContract = new MockMpcContract(account, privateKey); return __1.NearEthAdapter.fromConfig({ mpcContract }); }