-
Notifications
You must be signed in to change notification settings - Fork 0
Nametag mint tx data #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2970873
Add nametag mint transaction data. Update inMemoryClient test
martti007 69bcf27
Merge remote-tracking branch 'origin/main' into nametag-mint-tx-data
martti007 ccc4e32
Change uuid to real nametag
martti007 dfb342f
Improve the flow in nametag test to represent real world usecase
martti007 8af3f29
Fix bob secret in TokenInMemoryClientTest
martti007 a97252e
Add token split functional test
martti007 3f029fb
Update error wording in split test
martti007 44b49f6
Fix naming of abstract token split class
martti007 1276f02
Fix naming of abstract token split class
martti007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/unicity/sdk/transaction/NametagMintTransactionData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.unicity.sdk.transaction; | ||
|
|
||
| import com.unicity.sdk.address.Address; | ||
| import com.unicity.sdk.hash.DataHasher; | ||
| import com.unicity.sdk.hash.HashAlgorithm; | ||
| import com.unicity.sdk.token.TokenId; | ||
| import com.unicity.sdk.token.TokenType; | ||
| import com.unicity.sdk.token.fungible.TokenCoinData; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| public class NametagMintTransactionData<R extends MintTransactionReason> extends MintTransactionData<R> { | ||
| public NametagMintTransactionData( | ||
| String name, | ||
| TokenType tokenType, | ||
| byte[] tokenData, | ||
| TokenCoinData coinData, | ||
| Address recipient, | ||
| byte[] salt, | ||
| Address targetAddress | ||
| ) { | ||
| super( | ||
| TokenId.fromNameTag(name), | ||
| tokenType, | ||
| tokenData, | ||
| coinData, | ||
| recipient, | ||
| salt, | ||
| new DataHasher(HashAlgorithm.SHA256) | ||
| .update(targetAddress.getAddress().getBytes(StandardCharsets.UTF_8)) | ||
| .digest(), | ||
| null | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -52,7 +52,7 @@ public Transaction<TransferTransactionData> toTransaction(Token<?> token, | |||||
| throw new RuntimeException("Inclusion proof verification failed."); | ||||||
| } | ||||||
|
|
||||||
| if (!inclusionProof.getAuthenticator().isPresent()) { | ||||||
| if (inclusionProof.getAuthenticator().isEmpty()) { | ||||||
|
||||||
| if (inclusionProof.getAuthenticator().isEmpty()) { | |
| if (!inclusionProof.getAuthenticator().isPresent()) { |
202 changes: 202 additions & 0 deletions
202
src/test/java/com/unicity/sdk/common/split/BaseTokenSplitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| package com.unicity.sdk.common.split; | ||
|
|
||
| import static com.unicity.sdk.utils.TestUtils.randomBytes; | ||
|
|
||
| import com.unicity.sdk.StateTransitionClient; | ||
| import com.unicity.sdk.address.Address; | ||
| import com.unicity.sdk.address.DirectAddress; | ||
| import com.unicity.sdk.address.ProxyAddress; | ||
| import com.unicity.sdk.api.SubmitCommitmentResponse; | ||
| import com.unicity.sdk.api.SubmitCommitmentStatus; | ||
| import com.unicity.sdk.hash.DataHash; | ||
| import com.unicity.sdk.hash.DataHasher; | ||
| import com.unicity.sdk.hash.HashAlgorithm; | ||
| import com.unicity.sdk.predicate.MaskedPredicate; | ||
| import com.unicity.sdk.signing.SigningService; | ||
| import com.unicity.sdk.token.Token; | ||
| import com.unicity.sdk.token.TokenId; | ||
| import com.unicity.sdk.token.TokenState; | ||
| import com.unicity.sdk.token.TokenType; | ||
| import com.unicity.sdk.token.fungible.CoinId; | ||
| import com.unicity.sdk.token.fungible.TokenCoinData; | ||
| import com.unicity.sdk.transaction.MintCommitment; | ||
| import com.unicity.sdk.transaction.MintTransactionData; | ||
| import com.unicity.sdk.transaction.TransferCommitment; | ||
| import com.unicity.sdk.transaction.split.SplitMintReason; | ||
| import com.unicity.sdk.transaction.split.TokenSplitBuilder; | ||
| import com.unicity.sdk.transaction.split.TokenSplitBuilder.TokenSplit; | ||
| import com.unicity.sdk.util.InclusionProofUtils; | ||
| import com.unicity.sdk.utils.TokenUtils; | ||
| import java.math.BigInteger; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public abstract class BaseTokenSplitTest { | ||
|
|
||
| protected StateTransitionClient client; | ||
|
|
||
| @Test | ||
| void testTokenSplitFullAmounts() throws Exception { | ||
| byte[] secret = "SECRET".getBytes(StandardCharsets.UTF_8); | ||
|
|
||
| Token<?> token = TokenUtils.mintToken( | ||
| this.client, | ||
| secret, | ||
| new TokenId(randomBytes(32)), | ||
| new TokenType(randomBytes(32)), | ||
| randomBytes(32), | ||
| new TokenCoinData(Map.of( | ||
| new CoinId("test_eur".getBytes(StandardCharsets.UTF_8)), | ||
| BigInteger.valueOf(100), | ||
| new CoinId("test_usd".getBytes(StandardCharsets.UTF_8)), | ||
| BigInteger.valueOf(100) | ||
| )), | ||
| randomBytes(32), | ||
| randomBytes(32), | ||
| null | ||
| ); | ||
|
|
||
| String nametag = "my_nametag"; | ||
|
|
||
| Token<?> nametagToken = TokenUtils.mintNametagToken( | ||
| this.client, | ||
| secret, | ||
| nametag, | ||
| // Direct to random address in the beginning | ||
| DirectAddress.create(DataHash.fromImprint(new byte[34])) | ||
| ); | ||
|
|
||
| TokenSplitBuilder builder = new TokenSplitBuilder(); | ||
| TokenSplit split = builder | ||
| .createToken( | ||
| new TokenId(randomBytes(32)), | ||
| new TokenType(randomBytes(32)), | ||
| null, | ||
| new TokenCoinData(Map.of( | ||
| new CoinId("test_eur".getBytes(StandardCharsets.UTF_8)), | ||
| BigInteger.valueOf(100) | ||
| )), | ||
| ProxyAddress.create(nametag), | ||
| randomBytes(32), | ||
| null | ||
| ) | ||
| .createToken( | ||
| new TokenId(randomBytes(32)), | ||
| new TokenType(randomBytes(32)), | ||
| null, | ||
| new TokenCoinData(Map.of( | ||
| new CoinId("test_usd".getBytes(StandardCharsets.UTF_8)), | ||
| BigInteger.valueOf(100) | ||
| )), | ||
| ProxyAddress.create(nametag), | ||
| randomBytes(32), | ||
| null | ||
| ) | ||
| .build(token); | ||
|
|
||
| TransferCommitment burnCommitment = split.createBurnCommitment( | ||
| randomBytes(32), | ||
| SigningService.createFromSecret(secret, token.getState().getUnlockPredicate().getNonce()) | ||
| ); | ||
|
|
||
| SubmitCommitmentResponse burnCommitmentResponse = this.client | ||
| .submitCommitment(token, burnCommitment) | ||
| .get(); | ||
|
|
||
| if (burnCommitmentResponse.getStatus() != SubmitCommitmentStatus.SUCCESS) { | ||
| throw new Exception(String.format("Failed to submit burn commitment: %s", | ||
| burnCommitmentResponse.getStatus())); | ||
| } | ||
|
|
||
| List<MintCommitment<MintTransactionData<SplitMintReason>>> mintCommitments = split.createSplitMintCommitments( | ||
| burnCommitment.toTransaction( | ||
| token, | ||
| InclusionProofUtils.waitInclusionProof(this.client, burnCommitment).get() | ||
| ) | ||
| ); | ||
|
|
||
| for (MintCommitment<MintTransactionData<SplitMintReason>> commitment : mintCommitments) { | ||
| SubmitCommitmentResponse response = this.client | ||
| .submitCommitment(commitment) | ||
| .get(); | ||
|
|
||
| if (response.getStatus() != SubmitCommitmentStatus.SUCCESS) { | ||
| throw new Exception(String.format("Failed to submit burn commitment: %s", | ||
| response.getStatus())); | ||
| } | ||
|
|
||
| byte[] nonce = randomBytes(32); | ||
| TokenState state = new TokenState( | ||
| MaskedPredicate.create( | ||
| SigningService.createFromSecret(secret, nonce), | ||
| HashAlgorithm.SHA256, | ||
| nonce | ||
| ), | ||
| null | ||
| ); | ||
| Address address = state.getUnlockPredicate() | ||
| .getReference( | ||
| commitment.getTransactionData().getTokenType() | ||
| ) | ||
| .toAddress(); | ||
|
|
||
| byte[] nametagNonce = randomBytes(32); | ||
| TokenState nametagTokenState = new TokenState( | ||
| MaskedPredicate.create( | ||
| SigningService.createFromSecret(secret, nametagNonce), | ||
| HashAlgorithm.SHA256, | ||
| nametagNonce | ||
| ), | ||
| address.getAddress().getBytes(StandardCharsets.UTF_8) | ||
| ); | ||
|
|
||
| TransferCommitment nametagCommitment = TransferCommitment.create( | ||
| nametagToken, | ||
| nametagTokenState.getUnlockPredicate().getReference(nametagToken.getType()).toAddress(), | ||
| randomBytes(32), | ||
| new DataHasher(HashAlgorithm.SHA256) | ||
| .update(address.getAddress().getBytes(StandardCharsets.UTF_8)) | ||
| .digest(), | ||
| null, | ||
| SigningService.createFromSecret( | ||
| secret, | ||
| nametagToken.getState().getUnlockPredicate().getNonce() | ||
| ) | ||
| ); | ||
|
|
||
| SubmitCommitmentResponse nametagTransferResponse = this.client | ||
| .submitCommitment(nametagToken, nametagCommitment) | ||
| .get(); | ||
| if (nametagTransferResponse.getStatus() != SubmitCommitmentStatus.SUCCESS) { | ||
| throw new Exception(String.format("Failed to submit nametag transfer commitment: %s", | ||
| response.getStatus())); | ||
| } | ||
|
|
||
| nametagToken = this.client.finalizeTransaction( | ||
| nametagToken, | ||
| nametagTokenState, | ||
| nametagCommitment.toTransaction( | ||
| nametagToken, | ||
| InclusionProofUtils.waitInclusionProof(this.client, nametagCommitment).get() | ||
| ) | ||
| ); | ||
|
|
||
| Token<MintTransactionData<SplitMintReason>> splitToken = new Token<>( | ||
| state, | ||
| commitment.toTransaction( | ||
| InclusionProofUtils.waitInclusionProof(this.client, commitment).get() | ||
| ), | ||
| List.of(nametagToken) | ||
| ); | ||
|
|
||
| Assertions.assertTrue(splitToken.verify().isSuccessful()); | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
src/test/java/com/unicity/sdk/e2e/TokenInMemoryClientTest.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
src/test/java/com/unicity/sdk/functional/FunctionalTokenSplitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.unicity.sdk.functional; | ||
|
|
||
| import com.unicity.sdk.StateTransitionClient; | ||
| import com.unicity.sdk.TestAggregatorClient; | ||
| import com.unicity.sdk.common.split.BaseTokenSplitTest; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
|
|
||
| public class FunctionalTokenSplitTest extends BaseTokenSplitTest { | ||
| @BeforeEach | ||
| void setUp() { | ||
| this.client = new StateTransitionClient(new TestAggregatorClient()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The hashing logic for the target address should be documented. It's unclear why the target address is being hashed and used as the bearer hash parameter in the parent constructor.