44import java .util .List ;
55import java .util .Objects ;
66import java .util .concurrent .CompletableFuture ;
7- import org .unicitylabs .sdk .api .IAggregatorClient ;
7+ import org .unicitylabs .sdk .api .AggregatorClient ;
88import org .unicitylabs .sdk .api .InclusionProofResponse ;
99import org .unicitylabs .sdk .api .RequestId ;
1010import org .unicitylabs .sdk .api .SubmitCommitmentResponse ;
1111import org .unicitylabs .sdk .bft .RootTrustBase ;
1212import org .unicitylabs .sdk .predicate .PredicateEngineService ;
1313import org .unicitylabs .sdk .token .Token ;
1414import org .unicitylabs .sdk .token .TokenState ;
15- import org .unicitylabs .sdk .verification .VerificationException ;
1615import org .unicitylabs .sdk .transaction .Commitment ;
1716import org .unicitylabs .sdk .transaction .InclusionProofVerificationStatus ;
1817import 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 ;
2119import 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 ;
2322
23+ /**
24+ * Client for handling state transitions of tokens, including submitting commitments and finalizing
25+ * transactions.
26+ */
2427public class StateTransitionClient {
2528
26- protected final IAggregatorClient client ;
29+ /**
30+ * The aggregator client used for submitting commitments and retrieving inclusion proofs.
31+ */
32+ protected final AggregatorClient client ;
2733
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 ) {
2940 this .client = client ;
3041 }
3142
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 ) {
3552 return this .client .submitCommitment (
3653 commitment .getRequestId (),
3754 commitment .getTransactionData ().calculateHash (),
3855 commitment .getAuthenticator ()
3956 );
4057 }
4158
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+ */
4266 public CompletableFuture <SubmitCommitmentResponse > submitCommitment (
4367 TransferCommitment commitment
4468 ) {
@@ -55,29 +79,63 @@ public CompletableFuture<SubmitCommitmentResponse> submitCommitment(
5579 .calculateHash (), commitment .getAuthenticator ());
5680 }
5781
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 (
5995 RootTrustBase trustBase ,
60- Token <T > token ,
96+ Token <R > token ,
6197 TokenState state ,
62- Transaction < TransferTransactionData > transaction
98+ TransferTransaction transaction
6399 ) throws VerificationException {
64100 return this .finalizeTransaction (trustBase , token , state , transaction , List .of ());
65101 }
66102
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 (
68117 RootTrustBase trustBase ,
69- Token <T > token ,
118+ Token <R > token ,
70119 TokenState state ,
71- Transaction < TransferTransactionData > transaction ,
120+ TransferTransaction transaction ,
72121 List <Token <?>> nametags
73122 ) throws VerificationException {
74123 Objects .requireNonNull (token , "Token is null" );
75124
76125 return token .update (trustBase , state , transaction , nametags );
77126 }
78127
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+ */
79137 public CompletableFuture <InclusionProofVerificationStatus > getTokenStatus (
80- Token <? extends MintTransactionData <?> > token ,
138+ Token <?> token ,
81139 byte [] publicKey ,
82140 RootTrustBase trustBase
83141 ) {
@@ -86,6 +144,12 @@ public CompletableFuture<InclusionProofVerificationStatus> getTokenStatus(
86144 .thenApply (response -> response .getInclusionProof ().verify (requestId , trustBase ));
87145 }
88146
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+ */
89153 public CompletableFuture <InclusionProofResponse > getInclusionProof (Commitment <?> commitment ) {
90154 return this .client .getInclusionProof (commitment .getRequestId ());
91155 }
0 commit comments