@@ -2,9 +2,9 @@ import { StatusCodes } from "http-status-codes";
22import { randomUUID } from "node:crypto" ;
33import { TransactionDB } from "../../db/transactions/db" ;
44import {
5+ ParsedWalletDetails ,
56 getWalletDetails ,
67 isSmartBackendWallet ,
7- type ParsedWalletDetails ,
88} from "../../db/wallets/getWalletDetails" ;
99import { doesChainSupportService } from "../../lib/chain/chain-capabilities" ;
1010import { createCustomError } from "../../server/middleware/error" ;
@@ -43,13 +43,40 @@ export const insertTransaction = async (
4343 }
4444 }
4545
46+ // Get wallet details. For EOA and SBW (v5 endpoints), `from` should return a valid backend wallet.
47+ // For SBW (v4 endpoints), `accountAddress` should return a valid backend wallet.
48+ // Else the provided details are incorrect (user error).
49+ let walletDetails : ParsedWalletDetails | undefined ;
50+ let isSmartBackendWalletV4 = false ;
51+ try {
52+ walletDetails = await getWalletDetails ( {
53+ walletAddress : insertedTransaction . from ,
54+ } ) ;
55+ } catch { }
56+ if ( ! walletDetails && insertedTransaction . accountAddress ) {
57+ try {
58+ walletDetails = await getWalletDetails ( {
59+ walletAddress : insertedTransaction . accountAddress ,
60+ } ) ;
61+ isSmartBackendWalletV4 = true ;
62+ } catch { }
63+ }
64+ if ( ! walletDetails ) {
65+ throw createCustomError (
66+ "Account not found" ,
67+ StatusCodes . BAD_REQUEST ,
68+ "ACCOUNT_NOT_FOUND" ,
69+ ) ;
70+ }
71+
4672 let queuedTransaction : QueuedTransaction = {
4773 ...insertedTransaction ,
4874 status : "queued" ,
4975 queueId,
5076 queuedAt : new Date ( ) ,
5177 resendCount : 0 ,
5278
79+ walletType : walletDetails . type ,
5380 from : getChecksumAddress ( insertedTransaction . from ) ,
5481 to : getChecksumAddress ( insertedTransaction . to ) ,
5582 signerAddress : getChecksumAddress ( insertedTransaction . signerAddress ) ,
@@ -60,37 +87,34 @@ export const insertTransaction = async (
6087 value : insertedTransaction . value ?? 0n ,
6188 } ;
6289
63- let walletDetails : ParsedWalletDetails | undefined ;
90+ // Handle smart backend wallets details.
91+ if ( isSmartBackendWallet ( walletDetails ) ) {
92+ if (
93+ ! ( await doesChainSupportService (
94+ queuedTransaction . chainId ,
95+ "account-abstraction" ,
96+ ) )
97+ ) {
98+ throw createCustomError (
99+ `Smart backend wallets do not support chain ${ queuedTransaction . chainId } .` ,
100+ StatusCodes . BAD_REQUEST ,
101+ "INVALID_SMART_BACKEND_WALLET_TRANSACTION" ,
102+ ) ;
103+ }
64104
65- try {
66- walletDetails = await getWalletDetails ( {
67- address : queuedTransaction . from ,
68- } ) ;
105+ queuedTransaction = {
106+ ...queuedTransaction ,
107+ accountFactoryAddress : walletDetails . accountFactoryAddress ?? undefined ,
108+ entrypointAddress : walletDetails . entrypointAddress ?? undefined ,
109+ } ;
69110
70- // when using the v5 SDK with smart backend wallets, the following values are not set correctly:
71- // isUserOp is set to false
72- // account address is blank or the user provided value (this should be the SBW account address)
73- // from is set to the SBW account address (this should be the SBW signer address)
74- // these values need to be corrected so the worker can process the transaction
75- if ( isSmartBackendWallet ( walletDetails ) ) {
111+ if ( ! isSmartBackendWalletV4 ) {
76112 if ( queuedTransaction . accountAddress ) {
113+ // Disallow smart backend wallets from sending userOps.
77114 throw createCustomError (
78- "Smart backend wallets do not support interacting with other smart accounts" ,
115+ "Smart backend wallets do not support sending transactions with other smart accounts" ,
79116 StatusCodes . BAD_REQUEST ,
80- "INVALID_SMART_BACKEND_WALLET_INTERACTION" ,
81- ) ;
82- }
83-
84- if (
85- ! ( await doesChainSupportService (
86- queuedTransaction . chainId ,
87- "account-abstraction" ,
88- ) )
89- ) {
90- throw createCustomError (
91- "Chain does not support smart backend wallets" ,
92- StatusCodes . BAD_REQUEST ,
93- "SBW_CHAIN_NOT_SUPPORTED" ,
117+ "INVALID_SMART_BACKEND_WALLET_TRANSACTION" ,
94118 ) ;
95119 }
96120
@@ -101,52 +125,8 @@ export const insertTransaction = async (
101125 from : walletDetails . accountSignerAddress ,
102126 accountAddress : queuedTransaction . from ,
103127 target : queuedTransaction . to ,
104- accountFactoryAddress : walletDetails . accountFactoryAddress ?? undefined ,
105- entrypointAddress : walletDetails . entrypointAddress ?? undefined ,
106128 } ;
107129 }
108- } catch {
109- // if wallet details are not found, this is a smart backend wallet using a v4 endpoint
110- }
111-
112- if ( ! walletDetails && queuedTransaction . accountAddress ) {
113- try {
114- walletDetails = await getWalletDetails ( {
115- address : queuedTransaction . accountAddress ,
116- } ) ;
117-
118- // when using v4 SDK with smart backend wallets, the following values are not set correctly:
119- // entrypointAddress is not set
120- // accountFactoryAddress is not set
121- if ( walletDetails && isSmartBackendWallet ( walletDetails ) ) {
122- if (
123- ! ( await doesChainSupportService (
124- queuedTransaction . chainId ,
125- "account-abstraction" ,
126- ) )
127- ) {
128- throw createCustomError (
129- "Chain does not support smart backend wallets" ,
130- StatusCodes . BAD_REQUEST ,
131- "SBW_CHAIN_NOT_SUPPORTED" ,
132- ) ;
133- }
134-
135- queuedTransaction = {
136- ...queuedTransaction ,
137- entrypointAddress : walletDetails . entrypointAddress ?? undefined ,
138- accountFactoryAddress :
139- walletDetails . accountFactoryAddress ?? undefined ,
140- } ;
141- }
142- } catch {
143- // if wallet details are not found for this either, this backend wallet does not exist at all
144- throw createCustomError (
145- "Account not found" ,
146- StatusCodes . BAD_REQUEST ,
147- "ACCOUNT_NOT_FOUND" ,
148- ) ;
149- }
150130 }
151131
152132 // Simulate the transaction.
0 commit comments