1+ import type { Address } from "thirdweb" ;
12import type { PrismaTransaction } from "../../schema/prisma" ;
23import { encrypt } from "../../utils/crypto" ;
34import { getPrismaWithPostgresTx } from "../client" ;
@@ -8,13 +9,17 @@ type CreateWalletDetailsParams = {
89 address : string ;
910 label ?: string ;
1011} & (
12+ | {
13+ type : "local" ;
14+ encryptedJson : string ; // ENCRYPTION IS NOT HANDLED HERE, process privatekey with legacyLocalCrytpo before passing to this function
15+ }
1116 | {
1217 type : "aws-kms" ;
1318 awsKmsKeyId ?: string ; // depcrecated and unused, todo: remove with next breaking change
1419 awsKmsArn : string ;
1520
16- awsKmsSecretAccessKey ? : string ; // will be encrypted and stored, pass plaintext to this function
17- awsKmsAccessKeyId ? : string ;
21+ awsKmsSecretAccessKey : string ; // will be encrypted and stored, pass plaintext to this function
22+ awsKmsAccessKeyId : string ;
1823 }
1924 | {
2025 type : "gcp-kms" ;
@@ -24,11 +29,42 @@ type CreateWalletDetailsParams = {
2429 gcpKmsKeyVersionId ?: string ; // depcrecated and unused, todo: remove with next breaking change
2530 gcpKmsLocationId ?: string ; // depcrecated and unused, todo: remove with next breaking change
2631
27- gcpApplicationCredentialPrivateKey ?: string ; // encrypted
28- gcpApplicationCredentialEmail ?: string ;
32+ gcpApplicationCredentialPrivateKey : string ; // will be encrypted and stored, pass plaintext to this function
33+ gcpApplicationCredentialEmail : string ;
34+ }
35+ | {
36+ type : "smart:aws-kms" ;
37+ awsKmsArn : string ;
38+ awsKmsSecretAccessKey : string ; // will be encrypted and stored, pass plaintext to this function
39+ awsKmsAccessKeyId : string ;
40+ accountSignerAddress : Address ;
41+
42+ accountFactoryAddress : Address | undefined ;
43+ entrypointAddress : Address | undefined ;
44+ }
45+ | {
46+ type : "smart:gcp-kms" ;
47+ gcpKmsResourcePath : string ;
48+ gcpApplicationCredentialPrivateKey : string ; // will be encrypted and stored, pass plaintext to this function
49+ gcpApplicationCredentialEmail : string ;
50+ accountSignerAddress : Address ;
51+
52+ accountFactoryAddress : Address | undefined ;
53+ entrypointAddress : Address | undefined ;
54+ }
55+ | {
56+ type : "smart:local" ;
57+ encryptedJson : string ; // ENCRYPTION IS NOT HANDLED HERE, process privatekey with legacyLocalCrytpo before passing to this function
58+ accountSignerAddress : Address ;
59+
60+ accountFactoryAddress : Address | undefined ;
61+ entrypointAddress : Address | undefined ;
2962 }
3063) ;
3164
65+ /**
66+ * Create a new WalletDetails row in DB
67+ */
3268export const createWalletDetails = async ( {
3369 pgtx,
3470 ...walletDetails
@@ -47,15 +83,23 @@ export const createWalletDetails = async ({
4783 ) ;
4884 }
4985
86+ if ( walletDetails . type === "local" ) {
87+ return prisma . walletDetails . create ( {
88+ data : {
89+ ...walletDetails ,
90+ address : walletDetails . address . toLowerCase ( ) ,
91+ encryptedJson : walletDetails . encryptedJson ,
92+ } ,
93+ } ) ;
94+ }
95+
5096 if ( walletDetails . type === "aws-kms" ) {
5197 return prisma . walletDetails . create ( {
5298 data : {
5399 ...walletDetails ,
54100 address : walletDetails . address . toLowerCase ( ) ,
55101
56- awsKmsSecretAccessKey : walletDetails . awsKmsSecretAccessKey
57- ? encrypt ( walletDetails . awsKmsSecretAccessKey )
58- : undefined ,
102+ awsKmsSecretAccessKey : encrypt ( walletDetails . awsKmsSecretAccessKey ) ,
59103 } ,
60104 } ) ;
61105 }
@@ -66,11 +110,61 @@ export const createWalletDetails = async ({
66110 ...walletDetails ,
67111 address : walletDetails . address . toLowerCase ( ) ,
68112
69- gcpApplicationCredentialPrivateKey :
70- walletDetails . gcpApplicationCredentialPrivateKey
71- ? encrypt ( walletDetails . gcpApplicationCredentialPrivateKey )
72- : undefined ,
113+ gcpApplicationCredentialPrivateKey : encrypt (
114+ walletDetails . gcpApplicationCredentialPrivateKey ,
115+ ) ,
116+ } ,
117+ } ) ;
118+ }
119+
120+ if ( walletDetails . type === "smart:aws-kms" ) {
121+ return prisma . walletDetails . create ( {
122+ data : {
123+ ...walletDetails ,
124+
125+ address : walletDetails . address . toLowerCase ( ) ,
126+ awsKmsSecretAccessKey : encrypt ( walletDetails . awsKmsSecretAccessKey ) ,
127+ accountSignerAddress : walletDetails . accountSignerAddress . toLowerCase ( ) ,
128+
129+ accountFactoryAddress :
130+ walletDetails . accountFactoryAddress ?. toLowerCase ( ) ,
131+ entrypointAddress : walletDetails . entrypointAddress ?. toLowerCase ( ) ,
132+ } ,
133+ } ) ;
134+ }
135+
136+ if ( walletDetails . type === "smart:gcp-kms" ) {
137+ return prisma . walletDetails . create ( {
138+ data : {
139+ ...walletDetails ,
140+
141+ address : walletDetails . address . toLowerCase ( ) ,
142+ accountSignerAddress : walletDetails . accountSignerAddress . toLowerCase ( ) ,
143+
144+ gcpApplicationCredentialPrivateKey : encrypt (
145+ walletDetails . gcpApplicationCredentialPrivateKey ,
146+ ) ,
147+
148+ accountFactoryAddress :
149+ walletDetails . accountFactoryAddress ?. toLowerCase ( ) ,
150+ entrypointAddress : walletDetails . entrypointAddress ?. toLowerCase ( ) ,
73151 } ,
74152 } ) ;
75153 }
154+
155+ if ( walletDetails . type === "smart:local" ) {
156+ return prisma . walletDetails . create ( {
157+ data : {
158+ ...walletDetails ,
159+ address : walletDetails . address . toLowerCase ( ) ,
160+ accountSignerAddress : walletDetails . accountSignerAddress . toLowerCase ( ) ,
161+
162+ accountFactoryAddress :
163+ walletDetails . accountFactoryAddress ?. toLowerCase ( ) ,
164+ entrypointAddress : walletDetails . entrypointAddress ?. toLowerCase ( ) ,
165+ } ,
166+ } ) ;
167+ }
168+
169+ throw new Error ( "Unsupported wallet type" ) ;
76170} ;
0 commit comments