1
1
package com .alchemy .aa .client ;
2
2
3
- import com .alchemy .aa .Stamper ;
4
- import com .alchemy .aa .Stamper .Stamp ;
5
- import com .alchemy .aa .client .api .AuthJWT .Request ;
3
+ import com .alchemy .aa .client .api .AuthJWT ;
6
4
import com .alchemy .aa .client .api .AuthJWT .Response ;
7
- import com .alchemy .aa .client .api .AuthUser ;
8
- import com .alchemy .aa .client .api .AuthUser .TurnKeyWhoAmIRequest ;
9
- import com .alchemy .aa .client .api .AuthUser .WhoAmIRequest ;
10
- import com .alchemy .aa .client .api .SignRawMessage .SignParameters ;
11
- import com .alchemy .aa .client .api .SignRawMessage .SignRawMessageRequest ;
12
- import com .alchemy .aa .client .api .SignRawMessage .SignedResponse ;
13
- import com .alchemy .aa .client .api .SignRawMessage .SigningBody ;
5
+ import com .alchemy .aa .client .api .SignMessage ;
6
+ import com .alchemy .aa .client .api .SignMessage .SignBody ;
7
+ import com .alchemy .aa .client .api .SignMessage .SignParameters ;
14
8
import com .alchemy .aa .client .api .StampedRequest ;
9
+ import com .alchemy .aa .client .api .WhoAmI ;
10
+ import com .alchemy .aa .client .api .WhoAmI .RawWhoAmIRequest ;
11
+ import com .alchemy .aa .client .api .WhoAmI .Request ;
15
12
import com .alchemy .aa .core .CredentialBundle ;
13
+ import com .alchemy .aa .core .Stamper ;
14
+ import com .alchemy .aa .core .Stamper .Stamp ;
16
15
import com .alchemy .aa .core .TekManager ;
17
16
import com .fasterxml .jackson .databind .ObjectMapper ;
18
17
import com .fasterxml .jackson .databind .ObjectWriter ;
@@ -43,7 +42,7 @@ public record User(
43
42
private enum PathName {
44
43
LOOKUP ("lookup" ),
45
44
AUTH ("auth" ),
46
- AUTH_JWT ("auth-jwt " ),
45
+ AUTH_JWT ("auth-JWT " ),
47
46
WHOAMI ("whoami" ),
48
47
SIGN_PAYLOAD ("sign-payload" );
49
48
@@ -77,7 +76,7 @@ public SignerClient(HttpConfig httpConfig) {
77
76
*
78
77
* @throws Exception
79
78
*/
80
- public Stamper authenticateWithBundle (
79
+ public UserStamper authenticateWithBundle (
81
80
TekManager tekManager ,
82
81
String orgId ,
83
82
String bundle
@@ -90,18 +89,24 @@ public Stamper authenticateWithBundle(
90
89
91
90
Stamper stamper = new Stamper (credentialBundle );
92
91
User user = authUser (stamper , orgId );
93
- stamper .setUser (user );
94
- return stamper ;
92
+ return new UserStamper (user , stamper );
95
93
}
96
94
97
- public Stamper authenticateWithJWT (
95
+ /**
96
+ * Authticate user with JWT and get a stamper
97
+ * @param tekManager client's tek keys
98
+ * @param JWT JWT token
99
+ * @param authProviderName auth provider
100
+ * @return authenticated user
101
+ * @throws Exception if JWT and tekManager mis match
102
+ */
103
+ public UserStamper authenticateWithJWT (
98
104
TekManager tekManager ,
99
- String jwt ,
100
- String authProviderName ,
101
- int expirationInSeconds
105
+ String JWT ,
106
+ String authProviderName
102
107
) throws Exception {
103
- Request request = Request .builder ()
104
- .jwt (jwt )
108
+ AuthJWT . Request request = AuthJWT . Request .builder ()
109
+ .jwt (JWT )
105
110
.authProvider (authProviderName )
106
111
.targetPublicKey (tekManager .publicKey ())
107
112
.build ();
@@ -129,12 +134,12 @@ public Stamper authenticateWithJWT(
129
134
* @param address
130
135
* signer's address.
131
136
*
132
- * @return signed data in bytes .
137
+ * @return signature in string .
133
138
*
134
139
* @throws Exception
135
140
*/
136
141
public String signRawMessage (
137
- Stamper stamper ,
142
+ UserStamper userStamper ,
138
143
Bytes msg ,
139
144
String hashFunction ,
140
145
String address
@@ -146,53 +151,51 @@ public String signRawMessage(
146
151
.signWith (address )
147
152
.build ();
148
153
149
- SigningBody body = SigningBody .builder ()
150
- .organizationId (stamper . getUser ().orgId )
154
+ SignBody body = SignBody .builder ()
155
+ .organizationId (userStamper . user ().orgId )
151
156
.type ("ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2" )
152
157
.timestampMs (String .valueOf (Instant .now ().toEpochMilli ()))
153
158
.parameters (signParameters )
154
159
.build ();
155
160
156
161
String jsonBody = mapper .writeValueAsString (body );
157
162
158
- Stamp stamp = stamper .stamp (jsonBody );
163
+ Stamp stamp = userStamper . stamper () .stamp (jsonBody );
159
164
StampedRequest stampedRequest = StampedRequest .builder ()
160
165
.url ("https://api.turnkey.com/public/v1/submit/sign_raw_payload" )
161
166
.body (jsonBody )
162
167
.stamp (stamp )
163
168
.build ();
164
- SignRawMessageRequest request = new SignRawMessageRequest (stampedRequest );
165
- SignedResponse response = request (
169
+ SignMessage . Request request = new SignMessage . Request (stampedRequest );
170
+ SignMessage . Response response = request (
166
171
PathName .SIGN_PAYLOAD .getName (),
167
172
request ,
168
- SignedResponse .class
173
+ SignMessage . Response .class
169
174
);
170
175
return response .signature ();
171
176
}
172
177
173
178
/**
174
179
* Sign a Solana transcation.
175
- *
176
- * @param txBytes
177
- * transaction bytes
178
- *
179
- * @return signature
180
- *
180
+ * @param stamper stamper to sign
181
+ * @param txBytes transaction bytes
182
+ * @return signature in string
181
183
* @throws Exception
182
184
*/
183
- public String signSolanaTx (Stamper stamper , Bytes txBytes ) throws Exception {
185
+ public String signSolanaTx (UserStamper stamper , Bytes txBytes )
186
+ throws Exception {
184
187
return signRawMessage (
185
188
stamper ,
186
189
txBytes ,
187
190
"HASH_FUNCTION_NOT_APPLICABLE" ,
188
- stamper .getUser ().solanaAddress
191
+ stamper .user ().solanaAddress
189
192
);
190
193
}
191
194
192
195
/**
193
196
* Sign an Eth transaction.
194
197
*
195
- * @param stamper
198
+ * @param userStamper
196
199
* stamper to stamp transaction
197
200
* @param txBytes
198
201
* keccack256 hashed transaction byte
@@ -201,12 +204,13 @@ public String signSolanaTx(Stamper stamper, Bytes txBytes) throws Exception {
201
204
*
202
205
* @throws Exception
203
206
*/
204
- public String signEthTx (Stamper stamper , Bytes txBytes ) throws Exception {
207
+ public String signEthTx (UserStamper userStamper , Bytes txBytes )
208
+ throws Exception {
205
209
return signRawMessage (
206
- stamper ,
210
+ userStamper ,
207
211
txBytes ,
208
212
"HASH_FUNCTION_NO_OP" ,
209
- stamper . getUser ().address
213
+ userStamper . user ().address
210
214
);
211
215
}
212
216
@@ -219,23 +223,21 @@ public String signEthTx(Stamper stamper, Bytes txBytes) throws Exception {
219
223
*
220
224
* @throws Exception
221
225
*/
222
- private User authUser (Stamper stamper , String orgId ) throws Exception {
223
- WhoAmIRequest whoAmIRequest = new WhoAmIRequest (orgId );
226
+ private User authUser (Stamper Stamper , String orgId ) throws Exception {
227
+ RawWhoAmIRequest rawWhoAmIRequest = new RawWhoAmIRequest (orgId );
224
228
ObjectWriter writer = mapper .writerWithDefaultPrettyPrinter ();
225
- String json_body = writer .writeValueAsString (whoAmIRequest );
226
- Stamp stampedBody = stamper .stamp (json_body );
229
+ String json_body = writer .writeValueAsString (rawWhoAmIRequest );
230
+ Stamp stampedBody = Stamper .stamp (json_body );
227
231
StampedRequest stampedRequestrequest = StampedRequest .builder ()
228
232
.url ("https://api.whoami.com/v1/users/" )
229
233
.body (json_body )
230
234
.stamp (stampedBody )
231
235
.build ();
232
- TurnKeyWhoAmIRequest request = new TurnKeyWhoAmIRequest (
233
- stampedRequestrequest
234
- );
235
- AuthUser .Response response = request (
236
+ Request request = new Request (stampedRequestrequest );
237
+ WhoAmI .Response response = request (
236
238
PathName .WHOAMI .getName (),
237
239
request ,
238
- AuthUser .Response .class
240
+ WhoAmI .Response .class
239
241
);
240
242
return User .builder ()
241
243
.address (response .address ())
0 commit comments