@@ -4,13 +4,15 @@ import { RequestId } from './api/RequestId.js';
44import { SubmitCommitmentResponse } from './api/SubmitCommitmentResponse.js' ;
55import { RootTrustBase } from './bft/RootTrustBase.js' ;
66import { PredicateEngineService } from './predicate/PredicateEngineService.js' ;
7+ import { MintSigningService } from './sign/MintSigningService.js' ;
78import { Token } from './token/Token.js' ;
9+ import { TokenId } from './token/TokenId.js' ;
810import { TokenState } from './token/TokenState.js' ;
911import { Commitment } from './transaction/Commitment.js' ;
1012import { IMintTransactionReason } from './transaction/IMintTransactionReason.js' ;
1113import { InclusionProofVerificationStatus } from './transaction/InclusionProof.js' ;
1214import { MintCommitment } from './transaction/MintCommitment.js' ;
13- import { MintTransactionData } from './transaction/MintTransactionData .js' ;
15+ import { MintTransactionState } from './transaction/MintTransactionState .js' ;
1416import { TransferTransaction } from './transaction/TransferTransaction.js' ;
1517import { TransferTransactionData } from './transaction/TransferTransactionData.js' ;
1618
@@ -71,11 +73,11 @@ export class StateTransitionClient {
7173 * Finalizes a transaction by updating the token state based on the provided transaction data and
7274 * nametags.
7375 *
74- * @param trustBase The root trust base for inclusion proof verification.
75- * @param token The token to be updated.
76- * @param state The current state of the token.
77- * @param transaction The transaction containing transfer data.
78- * @param nametags A list of tokens used as nametags in the transaction.
76+ * @param { RootTrustBase } trustBase The root trust base for inclusion proof verification.
77+ * @param { Token } token The token to be updated.
78+ * @param { TokenState } state The current state of the token.
79+ * @param { TransferTransaction } transaction The transaction containing transfer data.
80+ * @param { Token } nametags A list of tokens used as nametags in the transaction.
7981 * @return The updated token after applying the transaction.
8082 */
8183 public finalizeTransaction < R extends IMintTransactionReason > (
@@ -89,34 +91,70 @@ export class StateTransitionClient {
8991 }
9092
9193 /**
92- * Retrieves the inclusion proof for a token and verifies its status against the provided public
93- * key and trust base.
94+ * Retrieves the inclusion proof for a given commitment.
9495 *
95- * @param token The token for which to retrieve the inclusion proof.
96- * @param publicKey The public key associated with the token.
97- * @param trustBase The root trust base for verification.
98- * @return inclusion proof verification status.
96+ * @param {RequestId } requestId The request ID of inclusion proof to retrieve.
97+ * @return inclusion proof response from the aggregator.
9998 */
100- public async getTokenStatus (
99+ public getInclusionProof ( requestId : RequestId ) : Promise < InclusionProofResponse > {
100+ return this . client . getInclusionProof ( requestId ) ;
101+ }
102+
103+ /**
104+ * Check if state is already spent for given request id.
105+ *
106+ * @param {RootTrustBase } trustBase root trust base
107+ * @param {RequestId } requestId request id
108+ * @return true if state is spent, false otherwise.
109+ */
110+ public async isStateSpent ( trustBase : RootTrustBase , requestId : RequestId ) : Promise < boolean > {
111+ const response = await this . getInclusionProof ( requestId ) ;
112+ const result = await response . inclusionProof . verify ( trustBase , requestId ) ;
113+ switch ( result ) {
114+ case InclusionProofVerificationStatus . OK :
115+ return true ;
116+ case InclusionProofVerificationStatus . PATH_NOT_INCLUDED :
117+ return false ;
118+ default :
119+ throw new Error ( `Inclusion proof verification failed with status ${ result } ` ) ;
120+ }
121+ }
122+
123+ /**
124+ * Check if token state is already spent.
125+ * @param {RootTrustBase } trustBase trustBase
126+ * @param {Token } token token
127+ * @param {Uint8Array } publicKey public key
128+ * @return true if token state is spent, false otherwise
129+ */
130+ public async isTokenStateSpent (
101131 trustBase : RootTrustBase ,
102132 token : Token < IMintTransactionReason > ,
103133 publicKey : Uint8Array ,
104- ) : Promise < InclusionProofVerificationStatus > {
105- const requestId = await RequestId . create ( publicKey , await token . state . calculateHash ( ) ) ;
106- return this . client
107- . getInclusionProof ( requestId )
108- . then ( ( response ) => response . inclusionProof . verify ( trustBase , requestId ) ) ;
134+ ) : Promise < boolean > {
135+ const pk = new Uint8Array ( publicKey ) ;
136+ const predicate = await PredicateEngineService . createPredicate ( token . state . predicate ) ;
137+ if ( ! ( await predicate . isOwner ( pk ) ) ) {
138+ throw new Error ( 'Given key is not owner of the token.' ) ;
139+ }
140+
141+ return this . isStateSpent ( trustBase , await RequestId . create ( pk , await token . state . calculateHash ( ) ) ) ;
109142 }
110143
111144 /**
112- * Retrieves the inclusion proof for a given commitment .
145+ * Check if token id is already minted .
113146 *
114- * @param commitment The commitment for which to retrieve the inclusion proof.
115- * @return inclusion proof response from the aggregator.
147+ * @param {RootTrustBase } trustBase root trust base
148+ * @param {TokenId } tokenId token id
149+ * @return true if token id is spent, false otherwise.
116150 */
117- public getInclusionProof (
118- commitment : Commitment < TransferTransactionData | MintTransactionData < IMintTransactionReason > > ,
119- ) : Promise < InclusionProofResponse > {
120- return this . client . getInclusionProof ( commitment . requestId ) ;
151+ public async isMinted ( trustBase : RootTrustBase , tokenId : TokenId ) : Promise < boolean > {
152+ return this . isStateSpent (
153+ trustBase ,
154+ await RequestId . create (
155+ await MintSigningService . create ( tokenId ) . then ( ( signingService ) => signingService . publicKey ) ,
156+ await MintTransactionState . create ( tokenId ) ,
157+ ) ,
158+ ) ;
121159 }
122160}
0 commit comments