UNPKG

near-ca-test

Version:

An SDK for controlling Ethereum Accounts from a Near Account.

48 lines (47 loc) 1.89 kB
import { serializeSignature } from "viem"; import { addSignature, relaySignedTransaction, toPayload, } from "./utils/transaction"; import { isSignMethod, signMethods } from "./types"; import { requestRouter } from "./utils/request"; function stripEip155Prefix(eip155Address) { return eip155Address.split(":").pop() ?? ""; } /** * Features currently underdevelopment 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; constructor(adapter) { this.adapter = adapter; } async handleSessionRequest(request) { const { chainId, request: { method, params }, } = request.params; console.log(`Session Request of type ${method} for chainId ${chainId}`); 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, }); console.log("Parsed Request:", evmMessage, hashToSign); return { nearPayload: await this.adapter.mpcContract.encodeSignatureRequestTx({ path: this.adapter.derivationPath, payload: toPayload(hashToSign), key_version: 0, }), evmMessage, hashToSign, }; } async respondSessionRequest(signature, transaction) { if (transaction) { const signedTx = addSignature({ transaction, signature }); // Returns relayed transaction hash (without waiting for confirmation). return relaySignedTransaction(signedTx, false); } return serializeSignature(signature); } }