Skip to content

Commit 2635491

Browse files
committed
Fix predicate creation issues
1 parent c6bae18 commit 2635491

6 files changed

Lines changed: 64 additions & 91 deletions

File tree

src/predicate/embedded/MaskedPredicate.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import { HashAlgorithm } from '../../hash/HashAlgorithm.js';
55
import { CborDeserializer } from '../../serializer/cbor/CborDeserializer.js';
66
import { CborError } from '../../serializer/cbor/CborError.js';
77
import { SigningService } from '../../sign/SigningService.js';
8-
import { Token } from '../../token/Token.js';
98
import { TokenId } from '../../token/TokenId.js';
109
import { TokenType } from '../../token/TokenType.js';
11-
import { MintTransaction } from '../../transaction/MintTransaction.js';
1210

1311
/**
1412
* Predicate for masked address transaction.
@@ -34,41 +32,24 @@ export class MaskedPredicate extends DefaultPredicate {
3432
}
3533

3634
/**
37-
* Create masked predicate from token and signing service.
35+
* Create masked predicate.
3836
*
39-
* @param {Token} token token
37+
* @param {TokenId} tokenId token id
38+
* @param {TokenType} tokenType token type
4039
* @param {SigningService} signingService signing service
4140
* @param {HashAlgorithm} hashAlgorithm hash algorithm
4241
* @param {Uint8Array} nonce predicate nonce
43-
* @return predicate
4442
*/
45-
public static createFromToken(
46-
token: Token,
47-
signingService: SigningService,
48-
hashAlgorithm: HashAlgorithm,
49-
nonce: Uint8Array,
50-
): MaskedPredicate {
51-
return MaskedPredicate.createFromMintTransaction(token.genesis, signingService, hashAlgorithm, nonce);
52-
}
53-
54-
/**
55-
* Create masked predicate from mint transaction and signing service.
56-
*
57-
* @param {MintTransaction} transaction mint transaction
58-
* @param {SigningService} signingService signing service
59-
* @param {HashAlgorithm} hashAlgorithm hash algorithm
60-
* @param {Uint8Array} nonce predicate nonce
61-
* @return predicate
62-
*/
63-
public static createFromMintTransaction(
64-
transaction: MintTransaction,
43+
public static create(
44+
tokenId: TokenId,
45+
tokenType: TokenType,
6546
signingService: SigningService,
6647
hashAlgorithm: HashAlgorithm,
6748
nonce: Uint8Array,
6849
): MaskedPredicate {
6950
return new MaskedPredicate(
70-
transaction.data.tokenId,
71-
transaction.data.tokenType,
51+
tokenId,
52+
tokenType,
7253
signingService.publicKey,
7354
signingService.algorithm,
7455
hashAlgorithm,

src/predicate/embedded/UnmaskedPredicate.ts

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,30 @@ export class UnmaskedPredicate extends DefaultPredicate {
3939
/**
4040
* Create unmasked predicate.
4141
*
42-
* @param {Token} token token
42+
* @param {TokenId} tokenId token id
43+
* @param {TokenType} tokenType token type
44+
* @param {MintTransaction | TransferTransaction} transaction transaction
4345
* @param {SigningService} signingService signing service
4446
* @param {HashAlgorithm} hashAlgorithm hash algorithm
4547
*/
46-
public static createFromToken(
47-
token: Token,
48+
public static async create(
49+
tokenId: TokenId,
50+
tokenType: TokenType,
51+
transaction: MintTransaction | TransferTransaction,
4852
signingService: SigningService,
4953
hashAlgorithm: HashAlgorithm,
5054
): Promise<UnmaskedPredicate> {
51-
return UnmaskedPredicate.create(token.id, token.type, token.latestTransaction, signingService, hashAlgorithm);
52-
}
55+
const nonce = await signingService.sign(
56+
await new DataHasher(HashAlgorithm.SHA256).update(transaction.data.salt).digest(),
57+
);
5358

54-
/**
55-
* Create unmasked predicate from mint transaction.
56-
*
57-
* @param {MintTransaction} transaction mint transaction
58-
* @param {SigningService} signingService signing service
59-
* @param {HashAlgorithm} hashAlgorithm hash algorithm
60-
* @return predicate
61-
*/
62-
public static createFromMintTransaction(
63-
transaction: MintTransaction,
64-
signingService: SigningService,
65-
hashAlgorithm: HashAlgorithm,
66-
): Promise<UnmaskedPredicate> {
67-
return UnmaskedPredicate.create(
68-
transaction.data.tokenId,
69-
transaction.data.tokenType,
70-
transaction,
71-
signingService,
59+
return new UnmaskedPredicate(
60+
tokenId,
61+
tokenType,
62+
signingService.publicKey,
63+
signingService.algorithm,
7264
hashAlgorithm,
65+
nonce.bytes,
7366
);
7467
}
7568

@@ -97,36 +90,6 @@ export class UnmaskedPredicate extends DefaultPredicate {
9790
);
9891
}
9992

100-
/**
101-
* Create unmasked predicate.
102-
*
103-
* @param {TokenId} tokenId token id
104-
* @param {TokenType} tokenType token type
105-
* @param {MintTransaction | TransferTransaction} transaction transaction
106-
* @param {SigningService} signingService signing service
107-
* @param {HashAlgorithm} hashAlgorithm hash algorithm
108-
*/
109-
private static async create(
110-
tokenId: TokenId,
111-
tokenType: TokenType,
112-
transaction: MintTransaction | TransferTransaction,
113-
signingService: SigningService,
114-
hashAlgorithm: HashAlgorithm,
115-
): Promise<UnmaskedPredicate> {
116-
const nonce = await signingService.sign(
117-
await new DataHasher(HashAlgorithm.SHA256).update(transaction.data.salt).digest(),
118-
);
119-
120-
return new UnmaskedPredicate(
121-
tokenId,
122-
tokenType,
123-
signingService.publicKey,
124-
signingService.algorithm,
125-
hashAlgorithm,
126-
nonce.bytes,
127-
);
128-
}
129-
13093
/**
13194
* Verify token state for current transaction.
13295
*

src/transaction/MintTransaction.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ export class MintTransaction extends Transaction<MintTransactionData> {
8888
await StateId.create(signingService.publicKey, this.data.sourceState),
8989
);
9090
if (inclusionProofVerificationResult !== InclusionProofVerificationStatus.OK) {
91-
return new VerificationResult(VerificationResultCode.FAIL, 'Inclusion proof verification failed.');
91+
return new VerificationResult(
92+
VerificationResultCode.FAIL,
93+
`Inclusion proof verification failed with status ${inclusionProofVerificationResult}.`,
94+
);
9295
}
9396

9497
return new VerificationResult(VerificationResultCode.OK);

tests/MintTokenUtils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ export async function mintToken(
9292
trustBase,
9393
mintReasonFactory,
9494
new TokenState(
95-
MaskedPredicate.createFromMintTransaction(transaction, signingService, HashAlgorithm.SHA256, data.nonce),
95+
MaskedPredicate.create(
96+
transaction.data.tokenId,
97+
transaction.data.tokenType,
98+
signingService,
99+
HashAlgorithm.SHA256,
100+
data.nonce,
101+
),
96102
data.data,
97103
),
98104
transaction,

tests/functional/token/TokenUsageExampleTest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Transition', function () {
4949
const signingService = await SigningService.createFromSecret(new Uint8Array(32), data.nonce);
5050

5151
const nonce = crypto.getRandomValues(new Uint8Array(32));
52-
const predicate = MaskedPredicate.createFromToken(token, signingService, HashAlgorithm.SHA256, nonce);
52+
const predicate = MaskedPredicate.create(token.id, token.type, signingService, HashAlgorithm.SHA256, nonce);
5353
const reference = await predicate.getReference();
5454
const transaction = await sendToken(
5555
trustBase,
@@ -83,7 +83,7 @@ describe('Transition', function () {
8383
trustBase,
8484
mintReasonFactory,
8585
new TokenState(
86-
MaskedPredicate.createFromToken(token, signingService, HashAlgorithm.SHA256, new Uint8Array(30)),
86+
MaskedPredicate.create(token.id, token.type, signingService, HashAlgorithm.SHA256, new Uint8Array(30)),
8787
new TextEncoder().encode('test data'),
8888
),
8989
transaction,

tests/token/CommonTestFlow.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ export async function testTransferFlow(trustBase: RootTrustBase, client: StateTr
7676
const bobTokenState = "Bob's custom data"; // Bob gives this custom data to the Alice to use in the transfer
7777
const bobNonce = crypto.getRandomValues(new Uint8Array(32));
7878
const bobSigningService = await SigningService.createFromSecret(bobSecret, bobNonce);
79-
const bobPredicate = MaskedPredicate.createFromToken(aliceToken, bobSigningService, HashAlgorithm.SHA256, bobNonce);
79+
const bobPredicate = MaskedPredicate.create(
80+
aliceToken.id,
81+
aliceToken.type,
82+
bobSigningService,
83+
HashAlgorithm.SHA256,
84+
bobNonce,
85+
);
8086

8187
const bobAddress = await bobPredicate.getReference().then((reference) => reference.toAddress());
8288

@@ -151,7 +157,13 @@ export async function testTransferFlow(trustBase: RootTrustBase, client: StateTr
151157
const carolTransaction = await TransferTransaction.fromJSON(txToCarol.toJSON());
152158

153159
// now Carol can create an UnmaskedPredicate knowing token information
154-
const carolPredicate = await UnmaskedPredicate.createFromToken(carolToken, carolSigningService, HashAlgorithm.SHA256);
160+
const carolPredicate = await UnmaskedPredicate.create(
161+
carolToken.id,
162+
carolToken.type,
163+
carolTransaction,
164+
carolSigningService,
165+
HashAlgorithm.SHA256,
166+
);
155167

156168
// Finish the transaction with the Carol predicate
157169
expect(carolTransaction.data.recipientDataHash).toBeNull();
@@ -181,7 +193,13 @@ export async function testOfflineTransferFlow(trustBase: RootTrustBase, client:
181193
// Recipient prepares the info for the transfer
182194
const nonce = crypto.getRandomValues(new Uint8Array(32));
183195
const bobSigningService = await SigningService.createFromSecret(bobSecret, nonce);
184-
const recipientPredicate = MaskedPredicate.createFromToken(token, bobSigningService, HashAlgorithm.SHA256, nonce);
196+
const recipientPredicate = MaskedPredicate.create(
197+
token.id,
198+
token.type,
199+
bobSigningService,
200+
HashAlgorithm.SHA256,
201+
nonce,
202+
);
185203

186204
const receivingAddress = await recipientPredicate.getReference().then((reference) => reference.toAddress());
187205

@@ -344,8 +362,9 @@ export async function testSplitFlowAfterTransfer(
344362

345363
const importedTransaction = await TransferTransaction.fromJSON(JSON.parse(JSON.stringify(sendTokenTx.toJSON())));
346364

347-
const maskedPredicate = MaskedPredicate.createFromToken(
348-
receiverImportedToken,
365+
const maskedPredicate = MaskedPredicate.create(
366+
receiverImportedToken.id,
367+
receiverImportedToken.type,
349368
recipientSigningService,
350369
HashAlgorithm.SHA256,
351370
receiverNonce,
@@ -459,8 +478,9 @@ async function splitToken(
459478
trustBase,
460479
mintReasonFactory,
461480
new TokenState(
462-
MaskedPredicate.createFromMintTransaction(
463-
transaction,
481+
MaskedPredicate.create(
482+
transaction.data.tokenId,
483+
transaction.data.tokenType,
464484
await SigningService.createFromSecret(ownerSecret, nonce),
465485
HashAlgorithm.SHA256,
466486
nonce,

0 commit comments

Comments
 (0)