Skip to content

Commit 29db66c

Browse files
committed
feat: refactor code
1 parent 4c7d625 commit 29db66c

File tree

15 files changed

+212
-150
lines changed

15 files changed

+212
-150
lines changed

account-kit/java/signer-sdk/pom.xml

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -173,30 +173,7 @@
173173
</execution>
174174
</executions>
175175
</plugin>
176-
177-
<plugin>
178-
<groupId>io.github.git-commit-id</groupId>
179-
<artifactId>git-commit-id-maven-plugin</artifactId>
180-
<version>5.0.0</version>
181-
<executions>
182-
<execution>
183-
<id>get-the-git-infos</id>
184-
<goals>
185-
<goal>revision</goal>
186-
</goals>
187-
<phase>initialize</phase>
188-
</execution>
189-
</executions>
190-
<configuration>
191-
<generateGitPropertiesFile>true</generateGitPropertiesFile>
192-
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
193-
<includeOnlyProperties>
194-
<includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
195-
<includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
196-
</includeOnlyProperties>
197-
<commitIdGenerationMode>full</commitIdGenerationMode>
198-
</configuration>
199-
</plugin>
176+
200177
<plugin>
201178
<groupId>com.hubspot.maven.plugins</groupId>
202179
<artifactId>prettier-maven-plugin</artifactId>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
package com.alchemy.aa.client;
22

3+
/**
4+
* Http configuration for signer client
5+
*
6+
* @param apiKey apiKey to call alchemy api
7+
* @param url alchemy api url
8+
*/
39
public record HttpConfig(String apiKey, String url) {
10+
/**
11+
* default HttpConfig with only alchemy url
12+
* @param apiKey apiKey to call alchemy api
13+
*/
414
public HttpConfig(String apiKey) {
515
this(apiKey, "https://api.g.alchemy.com/signer/v1/");
616
}
17+
18+
719
}

account-kit/java/signer-sdk/src/main/java/com/alchemy/aa/client/SignerClient.java

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package com.alchemy.aa.client;
22

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;
64
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;
148
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;
1512
import com.alchemy.aa.core.CredentialBundle;
13+
import com.alchemy.aa.core.Stamper;
14+
import com.alchemy.aa.core.Stamper.Stamp;
1615
import com.alchemy.aa.core.TekManager;
1716
import com.fasterxml.jackson.databind.ObjectMapper;
1817
import com.fasterxml.jackson.databind.ObjectWriter;
@@ -43,7 +42,7 @@ public record User(
4342
private enum PathName {
4443
LOOKUP("lookup"),
4544
AUTH("auth"),
46-
AUTH_JWT("auth-jwt"),
45+
AUTH_JWT("auth-JWT"),
4746
WHOAMI("whoami"),
4847
SIGN_PAYLOAD("sign-payload");
4948

@@ -77,7 +76,7 @@ public SignerClient(HttpConfig httpConfig) {
7776
*
7877
* @throws Exception
7978
*/
80-
public Stamper authenticateWithBundle(
79+
public UserStamper authenticateWithBundle(
8180
TekManager tekManager,
8281
String orgId,
8382
String bundle
@@ -90,18 +89,24 @@ public Stamper authenticateWithBundle(
9089

9190
Stamper stamper = new Stamper(credentialBundle);
9291
User user = authUser(stamper, orgId);
93-
stamper.setUser(user);
94-
return stamper;
92+
return new UserStamper(user, stamper);
9593
}
9694

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(
98104
TekManager tekManager,
99-
String jwt,
100-
String authProviderName,
101-
int expirationInSeconds
105+
String JWT,
106+
String authProviderName
102107
) throws Exception {
103-
Request request = Request.builder()
104-
.jwt(jwt)
108+
AuthJWT.Request request = AuthJWT.Request.builder()
109+
.jwt(JWT)
105110
.authProvider(authProviderName)
106111
.targetPublicKey(tekManager.publicKey())
107112
.build();
@@ -129,12 +134,12 @@ public Stamper authenticateWithJWT(
129134
* @param address
130135
* signer's address.
131136
*
132-
* @return signed data in bytes.
137+
* @return signature in string.
133138
*
134139
* @throws Exception
135140
*/
136141
public String signRawMessage(
137-
Stamper stamper,
142+
UserStamper userStamper,
138143
Bytes msg,
139144
String hashFunction,
140145
String address
@@ -146,53 +151,51 @@ public String signRawMessage(
146151
.signWith(address)
147152
.build();
148153

149-
SigningBody body = SigningBody.builder()
150-
.organizationId(stamper.getUser().orgId)
154+
SignBody body = SignBody.builder()
155+
.organizationId(userStamper.user().orgId)
151156
.type("ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2")
152157
.timestampMs(String.valueOf(Instant.now().toEpochMilli()))
153158
.parameters(signParameters)
154159
.build();
155160

156161
String jsonBody = mapper.writeValueAsString(body);
157162

158-
Stamp stamp = stamper.stamp(jsonBody);
163+
Stamp stamp = userStamper.stamper().stamp(jsonBody);
159164
StampedRequest stampedRequest = StampedRequest.builder()
160165
.url("https://api.turnkey.com/public/v1/submit/sign_raw_payload")
161166
.body(jsonBody)
162167
.stamp(stamp)
163168
.build();
164-
SignRawMessageRequest request = new SignRawMessageRequest(stampedRequest);
165-
SignedResponse response = request(
169+
SignMessage.Request request = new SignMessage.Request(stampedRequest);
170+
SignMessage.Response response = request(
166171
PathName.SIGN_PAYLOAD.getName(),
167172
request,
168-
SignedResponse.class
173+
SignMessage.Response.class
169174
);
170175
return response.signature();
171176
}
172177

173178
/**
174179
* 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
181183
* @throws Exception
182184
*/
183-
public String signSolanaTx(Stamper stamper, Bytes txBytes) throws Exception {
185+
public String signSolanaTx(UserStamper stamper, Bytes txBytes)
186+
throws Exception {
184187
return signRawMessage(
185188
stamper,
186189
txBytes,
187190
"HASH_FUNCTION_NOT_APPLICABLE",
188-
stamper.getUser().solanaAddress
191+
stamper.user().solanaAddress
189192
);
190193
}
191194

192195
/**
193196
* Sign an Eth transaction.
194197
*
195-
* @param stamper
198+
* @param userStamper
196199
* stamper to stamp transaction
197200
* @param txBytes
198201
* keccack256 hashed transaction byte
@@ -201,12 +204,13 @@ public String signSolanaTx(Stamper stamper, Bytes txBytes) throws Exception {
201204
*
202205
* @throws Exception
203206
*/
204-
public String signEthTx(Stamper stamper, Bytes txBytes) throws Exception {
207+
public String signEthTx(UserStamper userStamper, Bytes txBytes)
208+
throws Exception {
205209
return signRawMessage(
206-
stamper,
210+
userStamper,
207211
txBytes,
208212
"HASH_FUNCTION_NO_OP",
209-
stamper.getUser().address
213+
userStamper.user().address
210214
);
211215
}
212216

@@ -219,23 +223,21 @@ public String signEthTx(Stamper stamper, Bytes txBytes) throws Exception {
219223
*
220224
* @throws Exception
221225
*/
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);
224228
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);
227231
StampedRequest stampedRequestrequest = StampedRequest.builder()
228232
.url("https://api.whoami.com/v1/users/")
229233
.body(json_body)
230234
.stamp(stampedBody)
231235
.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(
236238
PathName.WHOAMI.getName(),
237239
request,
238-
AuthUser.Response.class
240+
WhoAmI.Response.class
239241
);
240242
return User.builder()
241243
.address(response.address())
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.alchemy.aa.client;
2+
3+
import com.alchemy.aa.client.SignerClient.User;
4+
import com.alchemy.aa.core.Stamper;
5+
6+
/**
7+
* A packet of user and stamper
8+
* @param user Authed user
9+
* @param stamper stamper of the user
10+
*/
11+
public record UserStamper(User user, Stamper stamper) {}

account-kit/java/signer-sdk/src/main/java/com/alchemy/aa/client/api/AuthJWT.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,33 @@
33
import java.util.Optional;
44
import lombok.Builder;
55

6+
/**
7+
* Types of Alchemy auth-jwt api
8+
*/
69
public class AuthJWT {
710

11+
/**
12+
* Request type of auth-jwt
13+
* @param jwt jwt token
14+
* @param authProvider auth provider name
15+
* @param targetPublicKey Turnkey public key for which cred bundle is issued.
16+
*/
817
@Builder
918
public record Request(
1019
String jwt,
1120
String authProvider,
1221
String targetPublicKey
1322
) {}
1423

24+
/**
25+
* Response type of auth-jwt
26+
* @param isSignUp indicating whether it was a sign-up/pre-generated
27+
* @param orgId org id for user
28+
* @param credentialBundle a credential bundle returned by KMS, used by the client to sign messages after
29+
* @param userId userId of the user
30+
* @param address evm wallet address
31+
* @param solanaAddress solana wallet address
32+
*/
1533
public record Response(
1634
boolean isSignUp,
1735
String orgId,

account-kit/java/signer-sdk/src/main/java/com/alchemy/aa/client/api/AuthUser.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

account-kit/java/signer-sdk/src/main/java/com/alchemy/aa/client/api/GetUser.java

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.alchemy.aa.client.api;
2+
3+
import lombok.Builder;
4+
5+
///
6+
7+
/**
8+
* Types of Alchemy signmessage api
9+
* https://docs.alchemy.com/reference/signmessage
10+
*/
11+
public class SignMessage {
12+
13+
/**
14+
* Sign parameter
15+
* @param encoding payload encoding method
16+
* @param hashFunction signature hash function
17+
* @param payload payload to sign
18+
* @param signWith wallet address to sign payload
19+
*/
20+
@Builder
21+
public record SignParameters(
22+
String encoding,
23+
String hashFunction,
24+
String payload,
25+
String signWith
26+
) {}
27+
28+
/**
29+
* Sign body
30+
* @param organizationId org id to identify the signer
31+
* @param type sign type. Always "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2"
32+
* @param timestampMs timestamp to sign
33+
* @param parameters sign parameter above
34+
*/
35+
@Builder
36+
public record SignBody(
37+
String organizationId,
38+
String type,
39+
String timestampMs,
40+
SignParameters parameters
41+
) {}
42+
43+
/**
44+
* Request type of signmessage
45+
* @param stampedRequest stapmed signmessage request
46+
*/
47+
@Builder
48+
public record Request(StampedRequest stampedRequest) {}
49+
50+
/**
51+
* Response type of signmessage
52+
* @param signature signature
53+
*/
54+
public record Response(String signature) {}
55+
}

0 commit comments

Comments
 (0)