diff --git a/examples/send-tx.ts b/examples/send-tx.ts index 91533ef..6368c9c 100644 --- a/examples/send-tx.ts +++ b/examples/send-tx.ts @@ -4,6 +4,7 @@ import { loadArgs } from "./cli"; import { TransactionManager } from "../src"; import { nearAccountFromKeyPair } from "near-ca"; import { KeyPair } from "near-api-js"; +import { KeyPairString } from "near-api-js/lib/utils"; dotenv.config(); @@ -11,7 +12,9 @@ async function main(): Promise { const options = await loadArgs(); const nearAccount = await nearAccountFromKeyPair({ accountId: process.env.NEAR_ACCOUNT_ID!, - keyPair: KeyPair.fromString(process.env.NEAR_ACCOUNT_PRIVATE_KEY!), + keyPair: KeyPair.fromString( + process.env.NEAR_ACCOUNT_PRIVATE_KEY! as KeyPairString + ), network: { networkId: "testnet", nodeUrl: "https://rpc.testnet.near.org", @@ -40,11 +43,12 @@ async function main(): Promise { transactions.push(recoveryTx); } - const { unsignedUserOp, safeOpHash } = await txManager.buildTransaction({ + const unsignedUserOp = await txManager.buildTransaction({ transactions, - options, + usePaymaster: options.usePaymaster, }); console.log("Unsigned UserOp", unsignedUserOp); + const safeOpHash = await txManager.opHash(unsignedUserOp); console.log("Safe Op Hash", safeOpHash); // TODO: Evaluate gas cost (in ETH) diff --git a/src/lib/safe.ts b/src/lib/safe.ts index 8fb032a..61784ef 100644 --- a/src/lib/safe.ts +++ b/src/lib/safe.ts @@ -8,12 +8,7 @@ import { getSafeModuleSetupDeployment, } from "@safe-global/safe-modules-deployments"; import { PLACEHOLDER_SIG, packGas, packPaymasterData } from "../util"; -import { - GasPrice, - PaymasterData, - UnsignedUserOperation, - UserOperation, -} from "../types"; +import { GasPrice, UnsignedUserOperation, UserOperation } from "../types"; import { MetaTransaction } from "ethers-multisend"; /** @@ -121,8 +116,8 @@ export class ContractSuite { } async getOpHash( - unsignedUserOp: UserOperation, - paymasterData: PaymasterData + unsignedUserOp: UserOperation + // paymasterData: PaymasterData ): Promise { return this.m4337.getOperationHash({ ...unsignedUserOp, @@ -140,7 +135,7 @@ export class ContractSuite { unsignedUserOp.maxPriorityFeePerGas, unsignedUserOp.maxFeePerGas ), - paymasterAndData: packPaymasterData(paymasterData), + paymasterAndData: packPaymasterData(unsignedUserOp), signature: PLACEHOLDER_SIG, }); } diff --git a/src/tx-manager.ts b/src/tx-manager.ts index 34e70f9..ff87ef6 100644 --- a/src/tx-manager.ts +++ b/src/tx-manager.ts @@ -1,9 +1,9 @@ import { ethers } from "ethers"; -import { NearEthAdapter, MpcContract } from "near-ca"; +import { NearEthAdapter, MpcContract, NearEthTxData } from "near-ca"; import { Erc4337Bundler } from "./lib/bundler"; import { packSignature } from "./util"; import { getNearSignature } from "./lib/near"; -import { UserOperation, UserOperationReceipt, UserOptions } from "./types"; +import { UserOperation, UserOperationReceipt } from "./types"; import { MetaTransaction, encodeMulti } from "ethers-multisend"; import { ContractSuite } from "./lib/safe"; import { Account } from "near-api-js"; @@ -92,9 +92,9 @@ export class TransactionManager { async buildTransaction(args: { transactions: MetaTransaction[]; - options: UserOptions; - }): Promise<{ safeOpHash: string; unsignedUserOp: UserOperation }> { - const { transactions, options } = args; + usePaymaster: boolean; + }): Promise { + const { transactions, usePaymaster } = args; const gasFees = (await this.bundler.getGasPrice()).fast; // const gasFees = await this.provider.getFeeData(); // Build Singular MetaTransaction for Multisend from transaction list. @@ -114,20 +114,13 @@ export class TransactionManager { const paymasterData = await this.bundler.getPaymasterData( rawUserOp, - options.usePaymaster, + usePaymaster, this.safeNotDeployed ); const unsignedUserOp = { ...rawUserOp, ...paymasterData }; - const safeOpHash = await this.safePack.getOpHash( - unsignedUserOp, - paymasterData - ); - return { - safeOpHash, - unsignedUserOp, - }; + return unsignedUserOp; } async signTransaction(safeOpHash: string): Promise { @@ -135,6 +128,25 @@ export class TransactionManager { return packSignature(signature); } + async opHash(userOp: UserOperation): Promise { + return this.safePack.getOpHash(userOp); + } + async encodeSignatureRequest( + unsignedUserOp: UserOperation + ): Promise { + const safeOpHash = (await this.opHash(unsignedUserOp)) as `0x${string}`; + const txData = this.nearAdapter.encodeSignRequest({ + method: "hash", + chainId: 0, + params: safeOpHash as `0x${string}`, + }); + + return { + ...txData, + evmMessage: unsignedUserOp, + }; + } + async executeTransaction( userOp: UserOperation ): Promise { diff --git a/yarn.lock b/yarn.lock index dd97e6f..1444dab 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2289,14 +2289,14 @@ near-api-js@^5.0.0: node-fetch "2.6.7" near-ca@^0.5.2: - version "0.5.2" - resolved "https://registry.yarnpkg.com/near-ca/-/near-ca-0.5.2.tgz#02ab0e7f0c86ab3e37b54ef0f72e2d4b5c76ae56" - integrity sha512-MbZo5IsG7YY6v3WVtestqjLz/THzc0UdOpx1KLUbmPO9CuNAMvMN5HhMIr2ZlPq+VuUui3tRVlOyilWt/x6vRg== + version "0.5.4" + resolved "https://registry.yarnpkg.com/near-ca/-/near-ca-0.5.4.tgz#04d0d6f50005e312c3ac0aec3ccf85c93e940bfb" + integrity sha512-Q5hY6uY3/2qYOqiXT23an3hyKeqPKG1RJRGIn/v55I+8FdlIvJYLd/gllB4crDn2psYwjUoevwTUbRLMP33bmA== dependencies: "@walletconnect/web3wallet" "^1.13.0" elliptic "^6.5.6" near-api-js "^5.0.0" - viem "^2.17.5" + viem "^2.21.6" node-addon-api@^5.0.0: version "5.1.0" @@ -2860,7 +2860,7 @@ util-deprecate@^1.0.1: resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw== -viem@^2.17.5: +viem@^2.21.6: version "2.21.6" resolved "https://registry.yarnpkg.com/viem/-/viem-2.21.6.tgz#741f9e579e069335d1540051dde19aea272ee1d7" integrity sha512-YX48IVl6nZ4FRsY4ypv2RrxtQVWysIY146/lBW53tma8u32h8EsiA7vecw9ZbrueNUy/asHR4Egu68Z6FOvDzQ==