1
+ import type { Address } from "thirdweb" ;
1
2
import type { PrismaTransaction } from "../../schema/prisma" ;
2
3
import { encrypt } from "../../utils/crypto" ;
3
4
import { getPrismaWithPostgresTx } from "../client" ;
@@ -8,13 +9,17 @@ type CreateWalletDetailsParams = {
8
9
address : string ;
9
10
label ?: string ;
10
11
} & (
12
+ | {
13
+ type : "local" ;
14
+ encryptedJson : string ; // ENCRYPTION IS NOT HANDLED HERE, process privatekey with legacyLocalCrytpo before passing to this function
15
+ }
11
16
| {
12
17
type : "aws-kms" ;
13
18
awsKmsKeyId ?: string ; // depcrecated and unused, todo: remove with next breaking change
14
19
awsKmsArn : string ;
15
20
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 ;
18
23
}
19
24
| {
20
25
type : "gcp-kms" ;
@@ -24,11 +29,42 @@ type CreateWalletDetailsParams = {
24
29
gcpKmsKeyVersionId ?: string ; // depcrecated and unused, todo: remove with next breaking change
25
30
gcpKmsLocationId ?: string ; // depcrecated and unused, todo: remove with next breaking change
26
31
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 ;
29
62
}
30
63
) ;
31
64
65
+ /**
66
+ * Create a new WalletDetails row in DB
67
+ */
32
68
export const createWalletDetails = async ( {
33
69
pgtx,
34
70
...walletDetails
@@ -47,15 +83,23 @@ export const createWalletDetails = async ({
47
83
) ;
48
84
}
49
85
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
+
50
96
if ( walletDetails . type === "aws-kms" ) {
51
97
return prisma . walletDetails . create ( {
52
98
data : {
53
99
...walletDetails ,
54
100
address : walletDetails . address . toLowerCase ( ) ,
55
101
56
- awsKmsSecretAccessKey : walletDetails . awsKmsSecretAccessKey
57
- ? encrypt ( walletDetails . awsKmsSecretAccessKey )
58
- : undefined ,
102
+ awsKmsSecretAccessKey : encrypt ( walletDetails . awsKmsSecretAccessKey ) ,
59
103
} ,
60
104
} ) ;
61
105
}
@@ -66,11 +110,61 @@ export const createWalletDetails = async ({
66
110
...walletDetails ,
67
111
address : walletDetails . address . toLowerCase ( ) ,
68
112
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 ( ) ,
73
151
} ,
74
152
} ) ;
75
153
}
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" ) ;
76
170
} ;
0 commit comments