1- import { z } from "zod" ;
21import { encrypt } from "../../utils/crypto" ;
32import { registerEntitySecretCiphertext } from "@circle-fin/developer-controlled-wallets" ;
43import { prisma } from "../client" ;
54import { getConfig } from "../../utils/cache/get-config" ;
65import { WalletCredentialsError } from "./get-wallet-credential" ;
76import { randomBytes } from "node:crypto" ;
8-
9- export const entitySecretSchema = z . string ( ) . regex ( / ^ [ 0 - 9 a - f A - F ] { 64 } $ / , {
10- message : "entitySecret must be a 32-byte hex string" ,
11- } ) ;
7+ import { cirlceEntitySecretZodSchema } from "../../schemas/wallet" ;
128
139// will be expanded to be a discriminated union of all supported wallet types
1410export type CreateWalletCredentialsParams = {
@@ -24,55 +20,60 @@ export const createWalletCredential = async ({
2420 entitySecret,
2521 isDefault,
2622} : CreateWalletCredentialsParams ) => {
27- // not handling other wallet types because we only support circle for now
2823 const { walletConfiguration } = await getConfig ( ) ;
29- const circleApiKey = walletConfiguration . circle ?. apiKey ;
3024
31- if ( ! circleApiKey ) {
32- throw new WalletCredentialsError ( "No Circle API Key Configured" ) ;
33- }
25+ switch ( type ) {
26+ case "circle" : {
27+ const circleApiKey = walletConfiguration . circle ?. apiKey ;
3428
35- if ( entitySecret ) {
36- const { error } = entitySecretSchema . safeParse ( entitySecret ) ;
37- if ( error ) {
38- throw new WalletCredentialsError (
39- "Invalid provided entity secret for Circle" ,
40- ) ;
41- }
42- }
29+ if ( ! circleApiKey ) {
30+ throw new WalletCredentialsError ( "No Circle API Key Configured" ) ;
31+ }
4332
44- // If entitySecret is not provided, generate a random one
45- const finalEntitySecret = entitySecret ?? randomBytes ( 32 ) . toString ( "hex" ) ;
46- // Create the wallet credentials
47- const walletCredentials = await prisma . walletCredentials . create ( {
48- data : {
49- type,
50- label,
51- isDefault : isDefault ?? false ,
52- data : {
53- entitySecret : encrypt ( finalEntitySecret ) ,
54- } ,
55- } ,
56- } ) ;
33+ if ( entitySecret ) {
34+ const { error } = cirlceEntitySecretZodSchema . safeParse ( entitySecret ) ;
35+ if ( error ) {
36+ throw new WalletCredentialsError (
37+ "Invalid provided entity secret for Circle" ,
38+ ) ;
39+ }
40+ }
5741
58- // try registering the entity secret. See: https://developers.circle.com/w3s/developer-controlled-create-your-first-wallet
59- try {
60- await registerEntitySecretCiphertext ( {
61- apiKey : circleApiKey ,
62- entitySecret : finalEntitySecret ,
63- } ) ;
64- } catch ( e : unknown ) {
65- // If failed to registeer, permanently delete erroneously created credential
66- await prisma . walletCredentials . delete ( {
67- where : {
68- id : walletCredentials . id ,
69- } ,
70- } ) ;
42+ // If entitySecret is not provided, generate a random one
43+ const finalEntitySecret = entitySecret ?? randomBytes ( 32 ) . toString ( "hex" ) ;
44+ // Create the wallet credentials
45+ const walletCredentials = await prisma . walletCredentials . create ( {
46+ data : {
47+ type ,
48+ label ,
49+ isDefault : isDefault ?? false ,
50+ data : {
51+ entitySecret : encrypt ( finalEntitySecret ) ,
52+ } ,
53+ } ,
54+ } ) ;
7155
72- throw new WalletCredentialsError (
73- `Could not register Entity Secret with Circle\n${ JSON . stringify ( e ) } ` ,
74- ) ;
75- }
56+ // try registering the entity secret. See: https://developers.circle.com/w3s/developer-controlled-create-your-first-wallet
57+ try {
58+ await registerEntitySecretCiphertext ( {
59+ apiKey : circleApiKey ,
60+ entitySecret : finalEntitySecret ,
61+ recoveryFileDownloadPath : "/dev/null" ,
62+ } ) ;
63+ } catch ( e : unknown ) {
64+ // If failed to registeer, permanently delete erroneously created credential
65+ await prisma . walletCredentials . delete ( {
66+ where : {
67+ id : walletCredentials . id ,
68+ } ,
69+ } ) ;
70+
71+ throw new WalletCredentialsError (
72+ `Could not register Entity Secret with Circle\n${ JSON . stringify ( e ) } ` ,
73+ ) ;
74+ }
7675
77- return walletCredentials ;
76+ return walletCredentials ;
77+ }
78+ }
7879} ;
0 commit comments