@@ -6,7 +6,11 @@ import {
66 DEFAULT_ACCOUNT_FACTORY_V0_7 ,
77 ENTRYPOINT_ADDRESS_v0_7 ,
88} from "thirdweb/wallets/smart" ;
9- import { WalletType } from "../../../shared/schemas/wallet" ;
9+ import {
10+ LegacyWalletType ,
11+ WalletType ,
12+ CircleWalletType ,
13+ } from "../../../shared/schemas/wallet" ;
1014import { getConfig } from "../../../shared/utils/cache/get-config" ;
1115import { createCustomError } from "../../middleware/error" ;
1216import { AddressSchema } from "../../schemas/address" ;
@@ -25,16 +29,26 @@ import {
2529 createSmartGcpWalletDetails ,
2630 createSmartLocalWalletDetails ,
2731} from "../../utils/wallets/create-smart-wallet" ;
32+ import {
33+ CircleWalletError ,
34+ createCircleWalletDetails ,
35+ } from "../../utils/wallets/circle" ;
2836
29- const requestBodySchema = Type . Object ( {
30- label : Type . Optional ( Type . String ( ) ) ,
31- type : Type . Optional (
32- Type . Enum ( WalletType , {
33- description :
34- "Type of new wallet to create. It is recommended to always provide this value. If not provided, the default wallet type will be used." ,
35- } ) ,
36- ) ,
37- } ) ;
37+ const requestBodySchema = Type . Union ( [
38+ // Base schema for non-circle wallet types
39+ Type . Object ( {
40+ label : Type . Optional ( Type . String ( ) ) ,
41+ type : Type . Optional ( Type . Union ( [ Type . Enum ( LegacyWalletType ) ] ) ) ,
42+ } ) ,
43+
44+ // Schema for circle and smart:circle wallet types
45+ Type . Object ( {
46+ label : Type . Optional ( Type . String ( ) ) ,
47+ type : Type . Union ( [ Type . Enum ( CircleWalletType ) ] ) ,
48+ credentialId : Type . String ( ) ,
49+ walletSetId : Type . Optional ( Type . String ( ) ) ,
50+ } ) ,
51+ ] ) ;
3852
3953const responseSchema = Type . Object ( {
4054 result : Type . Object ( {
@@ -73,7 +87,7 @@ export const createBackendWallet = async (fastify: FastifyInstance) => {
7387 handler : async ( req , reply ) => {
7488 const { label } = req . body ;
7589
76- let walletAddress : string ;
90+ let walletAddress : string | undefined = undefined ;
7791 const config = await getConfig ( ) ;
7892
7993 const walletType =
@@ -112,6 +126,66 @@ export const createBackendWallet = async (fastify: FastifyInstance) => {
112126 throw e ;
113127 }
114128 break ;
129+ case CircleWalletType . circle :
130+ {
131+ // we need this if here for typescript to statically type the credentialId and walletSetId
132+ if ( req . body . type !== "circle" )
133+ throw new Error ( "Invalid Circle wallet type" ) ; // invariant
134+
135+ const { credentialId, walletSetId } = req . body ;
136+
137+ try {
138+ const wallet = await createCircleWalletDetails ( {
139+ label,
140+ isSmart : false ,
141+ credentialId,
142+ walletSetId,
143+ } ) ;
144+
145+ walletAddress = getAddress ( wallet . address ) ;
146+ } catch ( e ) {
147+ if ( e instanceof CircleWalletError ) {
148+ throw createCustomError (
149+ e . message ,
150+ StatusCodes . BAD_REQUEST ,
151+ "CREATE_CIRCLE_WALLET_ERROR" ,
152+ ) ;
153+ }
154+ throw e ;
155+ }
156+ }
157+ break ;
158+
159+ case CircleWalletType . smartCircle :
160+ {
161+ // we need this if here for typescript to statically type the credentialId and walletSetId
162+ if ( req . body . type !== "smart:circle" )
163+ throw new Error ( "Invalid Circle wallet type" ) ; // invariant
164+
165+ const { credentialId, walletSetId } = req . body ;
166+
167+ try {
168+ const wallet = await createCircleWalletDetails ( {
169+ label,
170+ isSmart : true ,
171+ credentialId,
172+ walletSetId,
173+ } ) ;
174+
175+ walletAddress = getAddress ( wallet . address ) ;
176+ } catch ( e ) {
177+ if ( e instanceof CircleWalletError ) {
178+ throw createCustomError (
179+ e . message ,
180+ StatusCodes . BAD_REQUEST ,
181+ "CREATE_CIRCLE_WALLET_ERROR" ,
182+ ) ;
183+ }
184+ throw e ;
185+ }
186+ }
187+ break ;
188+
115189 case WalletType . smartAwsKms :
116190 try {
117191 const smartAwsWallet = await createSmartAwsWalletDetails ( {
@@ -163,10 +237,14 @@ export const createBackendWallet = async (fastify: FastifyInstance) => {
163237 break ;
164238 }
165239
240+ if ( ! walletAddress ) {
241+ throw new Error ( "Invalid state" ) ; // invariant, typescript cannot exhaustive check because enums
242+ }
243+
166244 reply . status ( StatusCodes . OK ) . send ( {
167245 result : {
168246 walletAddress,
169- type : walletType ,
247+ type : walletType as WalletType ,
170248 status : "success" ,
171249 } ,
172250 } ) ;
0 commit comments