4
4
import java .util .List ;
5
5
import java .util .Objects ;
6
6
import java .util .concurrent .CompletableFuture ;
7
- import org .unicitylabs .sdk .api .IAggregatorClient ;
7
+ import org .unicitylabs .sdk .api .AggregatorClient ;
8
8
import org .unicitylabs .sdk .api .InclusionProofResponse ;
9
9
import org .unicitylabs .sdk .api .RequestId ;
10
10
import org .unicitylabs .sdk .api .SubmitCommitmentResponse ;
11
11
import org .unicitylabs .sdk .bft .RootTrustBase ;
12
12
import org .unicitylabs .sdk .predicate .PredicateEngineService ;
13
13
import org .unicitylabs .sdk .token .Token ;
14
14
import org .unicitylabs .sdk .token .TokenState ;
15
- import org .unicitylabs .sdk .verification .VerificationException ;
16
15
import org .unicitylabs .sdk .transaction .Commitment ;
17
16
import org .unicitylabs .sdk .transaction .InclusionProofVerificationStatus ;
18
17
import org .unicitylabs .sdk .transaction .MintCommitment ;
19
- import org .unicitylabs .sdk .transaction .MintTransactionData ;
20
- import org .unicitylabs .sdk .transaction .Transaction ;
18
+ import org .unicitylabs .sdk .transaction .MintTransactionReason ;
21
19
import org .unicitylabs .sdk .transaction .TransferCommitment ;
22
- import org .unicitylabs .sdk .transaction .TransferTransactionData ;
20
+ import org .unicitylabs .sdk .transaction .TransferTransaction ;
21
+ import org .unicitylabs .sdk .verification .VerificationException ;
23
22
23
+ /**
24
+ * Client for handling state transitions of tokens, including submitting commitments and finalizing
25
+ * transactions.
26
+ */
24
27
public class StateTransitionClient {
25
28
26
- protected final IAggregatorClient client ;
29
+ /**
30
+ * The aggregator client used for submitting commitments and retrieving inclusion proofs.
31
+ */
32
+ protected final AggregatorClient client ;
27
33
28
- public StateTransitionClient (IAggregatorClient client ) {
34
+ /**
35
+ * Creates a new StateTransitionClient with the specified aggregator client.
36
+ *
37
+ * @param client The aggregator client to use for communication.
38
+ */
39
+ public StateTransitionClient (AggregatorClient client ) {
29
40
this .client = client ;
30
41
}
31
42
32
- public <T extends MintTransactionData <?>> CompletableFuture <SubmitCommitmentResponse > submitCommitment (
33
- MintCommitment <T > commitment
34
- ) {
43
+ /**
44
+ * Submits a mint commitment to the aggregator.
45
+ *
46
+ * @param commitment The mint commitment to submit.
47
+ * @param <R> The type of mint transaction data.
48
+ * @return A CompletableFuture that resolves to the response from the aggregator.
49
+ */
50
+ public <R extends MintTransactionReason >
51
+ CompletableFuture <SubmitCommitmentResponse > submitCommitment (MintCommitment <R > commitment ) {
35
52
return this .client .submitCommitment (
36
53
commitment .getRequestId (),
37
54
commitment .getTransactionData ().calculateHash (),
38
55
commitment .getAuthenticator ()
39
56
);
40
57
}
41
58
59
+ /**
60
+ * Submits a transfer commitment to the aggregator after verifying ownership.
61
+ *
62
+ * @param commitment The transfer commitment to submit.
63
+ * @return A CompletableFuture that resolves to the response from the aggregator.
64
+ * @throws IllegalArgumentException if ownership verification fails.
65
+ */
42
66
public CompletableFuture <SubmitCommitmentResponse > submitCommitment (
43
67
TransferCommitment commitment
44
68
) {
@@ -55,29 +79,63 @@ public CompletableFuture<SubmitCommitmentResponse> submitCommitment(
55
79
.calculateHash (), commitment .getAuthenticator ());
56
80
}
57
81
58
- public <T extends MintTransactionData <?>> Token <T > finalizeTransaction (
82
+ /**
83
+ * Finalizes a transaction by updating the token state based on the provided transaction data
84
+ * without nametags.
85
+ *
86
+ * @param trustBase The root trust base for inclusion proof verification.
87
+ * @param token The token to be updated.
88
+ * @param state The current state of the token.
89
+ * @param transaction The transaction containing transfer data.
90
+ * @param <R> The type of mint transaction data.
91
+ * @return The updated token after applying the transaction.
92
+ * @throws VerificationException if verification fails during the update process.
93
+ */
94
+ public <R extends MintTransactionReason > Token <R > finalizeTransaction (
59
95
RootTrustBase trustBase ,
60
- Token <T > token ,
96
+ Token <R > token ,
61
97
TokenState state ,
62
- Transaction < TransferTransactionData > transaction
98
+ TransferTransaction transaction
63
99
) throws VerificationException {
64
100
return this .finalizeTransaction (trustBase , token , state , transaction , List .of ());
65
101
}
66
102
67
- public <T extends MintTransactionData <?>> Token <T > finalizeTransaction (
103
+ /**
104
+ * Finalizes a transaction by updating the token state based on the provided transaction data and
105
+ * nametags.
106
+ *
107
+ * @param trustBase The root trust base for inclusion proof verification.
108
+ * @param token The token to be updated.
109
+ * @param state The current state of the token.
110
+ * @param transaction The transaction containing transfer data.
111
+ * @param nametags A list of tokens used as nametags in the transaction.
112
+ * @param <R> The type of mint transaction data of token.
113
+ * @return The updated token after applying the transaction.
114
+ * @throws VerificationException if verification fails during the update process.
115
+ */
116
+ public <R extends MintTransactionReason > Token <R > finalizeTransaction (
68
117
RootTrustBase trustBase ,
69
- Token <T > token ,
118
+ Token <R > token ,
70
119
TokenState state ,
71
- Transaction < TransferTransactionData > transaction ,
120
+ TransferTransaction transaction ,
72
121
List <Token <?>> nametags
73
122
) throws VerificationException {
74
123
Objects .requireNonNull (token , "Token is null" );
75
124
76
125
return token .update (trustBase , state , transaction , nametags );
77
126
}
78
127
128
+ /**
129
+ * Retrieves the inclusion proof for a token and verifies its status against the provided public
130
+ * key and trust base.
131
+ *
132
+ * @param token The token for which to retrieve the inclusion proof.
133
+ * @param publicKey The public key associated with the token.
134
+ * @param trustBase The root trust base for verification.
135
+ * @return A CompletableFuture that resolves to the inclusion proof verification status.
136
+ */
79
137
public CompletableFuture <InclusionProofVerificationStatus > getTokenStatus (
80
- Token <? extends MintTransactionData <?> > token ,
138
+ Token <?> token ,
81
139
byte [] publicKey ,
82
140
RootTrustBase trustBase
83
141
) {
@@ -86,6 +144,12 @@ public CompletableFuture<InclusionProofVerificationStatus> getTokenStatus(
86
144
.thenApply (response -> response .getInclusionProof ().verify (requestId , trustBase ));
87
145
}
88
146
147
+ /**
148
+ * Retrieves the inclusion proof for a given commitment.
149
+ *
150
+ * @param commitment The commitment for which to retrieve the inclusion proof.
151
+ * @return A CompletableFuture that resolves to the inclusion proof response from the aggregator.
152
+ */
89
153
public CompletableFuture <InclusionProofResponse > getInclusionProof (Commitment <?> commitment ) {
90
154
return this .client .getInclusionProof (commitment .getRequestId ());
91
155
}
0 commit comments