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,39 @@ 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 ;
43+ }
44+ | {
45+ type : "smart:gcp-kms" ;
46+ gcpKmsResourcePath : string ;
47+ gcpApplicationCredentialPrivateKey : string ; // will be encrypted and stored, pass plaintext to this function
48+ gcpApplicationCredentialEmail : string ;
49+ accountSignerAddress : Address ;
50+
51+ accountFactoryAddress ?: Address ;
52+ }
53+ | {
54+ type : "smart:local" ;
55+ encryptedJson : string ; // ENCRYPTION IS NOT HANDLED HERE, process privatekey with legacyLocalCrytpo before passing to this function
56+ accountSignerAddress : Address ;
57+
58+ accountFactoryAddress ?: Address ;
2959 }
3060) ;
3161
62+ /**
63+ * Create a new WalletDetails row in DB
64+ */
3265export const createWalletDetails = async ( {
3366 pgtx,
3467 ...walletDetails
@@ -47,15 +80,23 @@ export const createWalletDetails = async ({
4780 ) ;
4881 }
4982
83+ if ( walletDetails . type === "local" ) {
84+ return prisma . walletDetails . create ( {
85+ data : {
86+ ...walletDetails ,
87+ address : walletDetails . address . toLowerCase ( ) ,
88+ encryptedJson : walletDetails . encryptedJson ,
89+ } ,
90+ } ) ;
91+ }
92+
5093 if ( walletDetails . type === "aws-kms" ) {
5194 return prisma . walletDetails . create ( {
5295 data : {
5396 ...walletDetails ,
5497 address : walletDetails . address . toLowerCase ( ) ,
5598
56- awsKmsSecretAccessKey : walletDetails . awsKmsSecretAccessKey
57- ? encrypt ( walletDetails . awsKmsSecretAccessKey )
58- : undefined ,
99+ awsKmsSecretAccessKey : encrypt ( walletDetails . awsKmsSecretAccessKey ) ,
59100 } ,
60101 } ) ;
61102 }
@@ -66,11 +107,60 @@ export const createWalletDetails = async ({
66107 ...walletDetails ,
67108 address : walletDetails . address . toLowerCase ( ) ,
68109
69- gcpApplicationCredentialPrivateKey :
70- walletDetails . gcpApplicationCredentialPrivateKey
71- ? encrypt ( walletDetails . gcpApplicationCredentialPrivateKey )
72- : undefined ,
110+ gcpApplicationCredentialPrivateKey : encrypt (
111+ walletDetails . gcpApplicationCredentialPrivateKey ,
112+ ) ,
113+ } ,
114+ } ) ;
115+ }
116+
117+ if ( walletDetails . type === "smart:aws-kms" ) {
118+ return prisma . walletDetails . create ( {
119+ data : {
120+ ...walletDetails ,
121+
122+ address : walletDetails . address . toLowerCase ( ) ,
123+ awsKmsSecretAccessKey : encrypt ( walletDetails . awsKmsSecretAccessKey ) ,
124+ accountSignerAddress : walletDetails . accountSignerAddress . toLowerCase ( ) ,
125+
126+ accountFactoryAddress :
127+ walletDetails . accountFactoryAddress ?. toLowerCase ( ) ,
128+ } ,
129+ } ) ;
130+ }
131+
132+ if ( walletDetails . type === "smart:gcp-kms" ) {
133+ return prisma . walletDetails . create ( {
134+ data : {
135+ ...walletDetails ,
136+
137+ address : walletDetails . address . toLowerCase ( ) ,
138+ accountSignerAddress : walletDetails . accountSignerAddress . toLowerCase ( ) ,
139+
140+ gcpApplicationCredentialPrivateKey : encrypt (
141+ walletDetails . gcpApplicationCredentialPrivateKey ,
142+ ) ,
143+
144+ accountFactoryAddress :
145+ walletDetails . accountFactoryAddress ?. toLowerCase ( ) ,
73146 } ,
74147 } ) ;
75148 }
149+
150+ if ( walletDetails . type === "smart:local" ) {
151+ return prisma . walletDetails . create ( {
152+ data : {
153+ ...walletDetails ,
154+ address : walletDetails . address . toLowerCase ( ) ,
155+ accountSignerAddress : walletDetails . accountSignerAddress . toLowerCase ( ) ,
156+
157+ accountFactoryAddress :
158+ walletDetails . accountFactoryAddress ?. toLowerCase ( ) ,
159+ } ,
160+ } ) ;
161+ }
162+
163+ // we will never reach here
164+ // this helps typescript understand that this function will always return
165+ throw new Error ( "Unsupported wallet type" ) ;
76166} ;
0 commit comments