UNPKG

near-ca

Version:

An SDK for controlling Ethereum Accounts from a Near Account.

54 lines (53 loc) 1.8 kB
import { requestRouter, toPayload } from "./utils"; import { isSignMethod, signMethods } from "./types"; /** * Removes the EIP-155 prefix from an address string * * @param eip155Address - The EIP-155 formatted address * @returns The address without the EIP-155 prefix */ function stripEip155Prefix(eip155Address) { return eip155Address.split(":").pop() ?? ""; } /** * Features currently under development that will be migrated into the adapter class once refined. * These features are accessible through the adapter class as `adapter.beta.methodName(...)` */ export class Beta { adapter; /** * Creates a new Beta instance * * @param adapter - The NearEthAdapter instance to use */ constructor(adapter) { this.adapter = adapter; } /** * Handles a WalletConnect session request by encoding it for NEAR * * @param request - The WalletConnect session request * @returns The encoded request for NEAR * @throws Error if the sign method is not supported */ async handleSessionRequest(request) { const { chainId, request: { method, params }, } = request.params; if (!isSignMethod(method)) { throw new Error(`Unsupported sign method ${method}: Available sign methods ${signMethods}`); } const { evmMessage, hashToSign } = await requestRouter({ method, chainId: parseInt(stripEip155Prefix(chainId)), params, }); return { nearPayload: await this.adapter.mpcContract.encodeSignatureRequestTx({ path: this.adapter.derivationPath, payload: toPayload(hashToSign), key_version: 0, }), evmMessage, hashToSign, }; } }