Search in sources :

Example 1 with SecretLockRepository

use of io.nem.symbol.sdk.api.SecretLockRepository in project nem2-sdk-java by nemtech.

the class StateProofServiceTest method secretLock.

@Test
void secretLock() throws Exception {
    SecretLockRepository repository = mock(SecretLockRepository.class);
    when(factory.createSecretLockRepository()).thenReturn(repository);
    String id = "secret";
    SecretLockInfo state = Mockito.mock(SecretLockInfo.class);
    when(state.getCompositeHash()).thenReturn(id);
    when(state.serialize()).thenReturn(ConvertUtils.fromHexToBytes(serialized));
    when(repository.getSecretLock(eq(id))).thenReturn(Observable.just(state));
    when(repository.getSecretLockMerkle(eq(id))).thenReturn(Observable.just(tree));
    StateMerkleProof<SecretLockInfo> proof = service.secretLock(id).toFuture().get();
    Assertions.assertTrue(proof.isValid());
    Assertions.assertEquals(state, proof.getState());
}
Also used : SecretLockRepository(io.nem.symbol.sdk.api.SecretLockRepository) SecretLockInfo(io.nem.symbol.sdk.model.transaction.SecretLockInfo) Test(org.junit.jupiter.api.Test)

Example 2 with SecretLockRepository

use of io.nem.symbol.sdk.api.SecretLockRepository in project nem2-sdk-java by nemtech.

the class MerkleLoaderIntegrationTest method secretLocks.

public List<Arguments> secretLocks() {
    RepositoryFactory repositoryFactory = getRepositoryFactory(DEFAULT_REPOSITORY_TYPE);
    SecretLockRepository repository = repositoryFactory.createSecretLockRepository();
    return getArguments(repository, new SecretLockSearchCriteria().order(ORDER_BY));
}
Also used : SecretLockSearchCriteria(io.nem.symbol.sdk.api.SecretLockSearchCriteria) SecretLockRepository(io.nem.symbol.sdk.api.SecretLockRepository) RepositoryFactory(io.nem.symbol.sdk.api.RepositoryFactory)

Example 3 with SecretLockRepository

use of io.nem.symbol.sdk.api.SecretLockRepository in project nem2-sdk-java by nemtech.

the class SecretLockIntegrationTest method secretLockAndProofTransaction.

@ParameterizedTest
@MethodSource("provider")
void secretLockAndProofTransaction(RepositoryType type, LockHashAlgorithm lockHashAlgorithm) {
    RepositoryFactory repositoryFactory = getRepositoryFactory(type);
    byte[] secretSeed = RandomUtils.generateRandomBytes(20);
    String secret = ConvertUtils.toHex(lockHashAlgorithm.hash(secretSeed));
    String storedSecret = ConvertUtils.padHex(secret, LockHashAlgorithm.DEFAULT_SECRET_HEX_SIZE);
    if (lockHashAlgorithm == LockHashAlgorithm.HASH_160) {
        Assertions.assertEquals(LockHashAlgorithm.DEFAULT_SECRET_HEX_SIZE, storedSecret.length());
        Assertions.assertEquals(40, secret.length());
    } else {
        Assertions.assertEquals(LockHashAlgorithm.DEFAULT_SECRET_HEX_SIZE, storedSecret.length());
        Assertions.assertEquals(LockHashAlgorithm.DEFAULT_SECRET_HEX_SIZE, secret.length());
    }
    String proof = ConvertUtils.toHex(secretSeed);
    Account account = config().getNemesisAccount1();
    Account account2 = config().getNemesisAccount2();
    Currency currency = get(repositoryFactory.getNetworkCurrency());
    Mosaic mosaic = currency.createAbsolute(BigInteger.valueOf(1));
    BigInteger amount = mosaic.getAmount();
    BigInteger duration = BigInteger.valueOf(10000);
    SecretLockTransaction secretLockTransaction = SecretLockTransactionFactory.create(getNetworkType(), getDeadline(), mosaic, duration, lockHashAlgorithm, secret, account2.getAddress()).maxFee(maxFee).build();
    announceAndValidate(type, account, secretLockTransaction);
    SecretProofTransaction secretProofTransaction = SecretProofTransactionFactory.create(getNetworkType(), getDeadline(), lockHashAlgorithm, account2.getAddress(), secret, proof).maxFee(maxFee).build();
    SecretProofTransaction secretProofTransactionAnnounced = announceAndValidate(type, account, secretProofTransaction);
    sleep(500);
    Assertions.assertEquals(lockHashAlgorithm, secretProofTransactionAnnounced.getHashType());
    Assertions.assertEquals(account2.getAddress(), secretProofTransactionAnnounced.getRecipient());
    Assertions.assertEquals(storedSecret, secretProofTransactionAnnounced.getSecret());
    Assertions.assertEquals(proof, secretProofTransactionAnnounced.getProof());
    SecretLockRepository hashLockRepository = getRepositoryFactory(type).createSecretLockRepository();
    SecretLockInfo info = get(hashLockRepository.search(new SecretLockSearchCriteria().address(account.getAddress()).secret(storedSecret))).getData().get(0);
    Assertions.assertNotNull(info);
    Assertions.assertEquals(account.getAddress(), info.getOwnerAddress());
    Assertions.assertEquals(account2.getAddress(), info.getRecipientAddress());
    Assertions.assertEquals(amount, info.getAmount());
    Assertions.assertEquals(storedSecret, info.getSecret());
    Assertions.assertEquals(lockHashAlgorithm, info.getHashAlgorithm());
    Assertions.assertEquals(LockStatus.USED, info.getStatus());
    Page<SecretLockInfo> page = get(hashLockRepository.search(new SecretLockSearchCriteria().address(account.getAddress()).order(OrderBy.DESC)));
    Assertions.assertTrue(page.getData().stream().anyMatch(m -> m.getSecret().equals(storedSecret)));
    Assertions.assertEquals(20, page.getPageSize());
    SecretLockInfo infoSearch = page.getData().stream().filter(m -> m.getSecret().equals(storedSecret)).findFirst().get();
    Assertions.assertNotNull(infoSearch);
    Assertions.assertEquals(account.getAddress(), infoSearch.getOwnerAddress());
    Assertions.assertEquals(account2.getAddress(), infoSearch.getRecipientAddress());
    Assertions.assertEquals(amount, infoSearch.getAmount());
    Assertions.assertEquals(lockHashAlgorithm, infoSearch.getHashAlgorithm());
    Assertions.assertEquals(LockStatus.USED, infoSearch.getStatus());
    Assertions.assertEquals(storedSecret, infoSearch.getSecret());
}
Also used : SecretLockTransactionFactory(io.nem.symbol.sdk.model.transaction.SecretLockTransactionFactory) SecretProofTransaction(io.nem.symbol.sdk.model.transaction.SecretProofTransaction) SecretLockInfo(io.nem.symbol.sdk.model.transaction.SecretLockInfo) LockHashAlgorithm(io.nem.symbol.sdk.model.transaction.LockHashAlgorithm) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic) Account(io.nem.symbol.sdk.model.account.Account) ConvertUtils(io.nem.symbol.core.utils.ConvertUtils) LockStatus(io.nem.symbol.sdk.model.transaction.LockStatus) Arguments(org.junit.jupiter.params.provider.Arguments) RepositoryFactory(io.nem.symbol.sdk.api.RepositoryFactory) Currency(io.nem.symbol.sdk.model.mosaic.Currency) ArrayList(java.util.ArrayList) SecretLockSearchCriteria(io.nem.symbol.sdk.api.SecretLockSearchCriteria) OrderBy(io.nem.symbol.sdk.api.OrderBy) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) TestInstance(org.junit.jupiter.api.TestInstance) Assertions(org.junit.jupiter.api.Assertions) BigInteger(java.math.BigInteger) SecretLockRepository(io.nem.symbol.sdk.api.SecretLockRepository) Page(io.nem.symbol.sdk.api.Page) SecretProofTransactionFactory(io.nem.symbol.sdk.model.transaction.SecretProofTransactionFactory) SecretLockTransaction(io.nem.symbol.sdk.model.transaction.SecretLockTransaction) MethodSource(org.junit.jupiter.params.provider.MethodSource) Account(io.nem.symbol.sdk.model.account.Account) SecretLockRepository(io.nem.symbol.sdk.api.SecretLockRepository) SecretLockInfo(io.nem.symbol.sdk.model.transaction.SecretLockInfo) SecretProofTransaction(io.nem.symbol.sdk.model.transaction.SecretProofTransaction) SecretLockTransaction(io.nem.symbol.sdk.model.transaction.SecretLockTransaction) SecretLockSearchCriteria(io.nem.symbol.sdk.api.SecretLockSearchCriteria) Currency(io.nem.symbol.sdk.model.mosaic.Currency) BigInteger(java.math.BigInteger) RepositoryFactory(io.nem.symbol.sdk.api.RepositoryFactory) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with SecretLockRepository

use of io.nem.symbol.sdk.api.SecretLockRepository in project nem2-sdk-java by nemtech.

the class SecretLockRepositoryIntegrationTest method searchWhenInvalidAddress.

@ParameterizedTest
@EnumSource(RepositoryType.class)
void searchWhenInvalidAddress(RepositoryType type) {
    SecretLockRepository repository = getRepositoryFactory(type).createSecretLockRepository();
    Address address = Address.generateRandom(getNetworkType());
    Page<SecretLockInfo> page = get(repository.search(new SecretLockSearchCriteria().address(address)));
    Assertions.assertTrue(page.isLast());
    Assertions.assertTrue(page.getData().isEmpty());
    Assertions.assertEquals(20, page.getPageSize());
}
Also used : SecretLockSearchCriteria(io.nem.symbol.sdk.api.SecretLockSearchCriteria) Address(io.nem.symbol.sdk.model.account.Address) SecretLockRepository(io.nem.symbol.sdk.api.SecretLockRepository) SecretLockInfo(io.nem.symbol.sdk.model.transaction.SecretLockInfo) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

SecretLockRepository (io.nem.symbol.sdk.api.SecretLockRepository)4 SecretLockSearchCriteria (io.nem.symbol.sdk.api.SecretLockSearchCriteria)3 SecretLockInfo (io.nem.symbol.sdk.model.transaction.SecretLockInfo)3 RepositoryFactory (io.nem.symbol.sdk.api.RepositoryFactory)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ConvertUtils (io.nem.symbol.core.utils.ConvertUtils)1 OrderBy (io.nem.symbol.sdk.api.OrderBy)1 Page (io.nem.symbol.sdk.api.Page)1 Account (io.nem.symbol.sdk.model.account.Account)1 Address (io.nem.symbol.sdk.model.account.Address)1 Currency (io.nem.symbol.sdk.model.mosaic.Currency)1 Mosaic (io.nem.symbol.sdk.model.mosaic.Mosaic)1 LockHashAlgorithm (io.nem.symbol.sdk.model.transaction.LockHashAlgorithm)1 LockStatus (io.nem.symbol.sdk.model.transaction.LockStatus)1 SecretLockTransaction (io.nem.symbol.sdk.model.transaction.SecretLockTransaction)1 SecretLockTransactionFactory (io.nem.symbol.sdk.model.transaction.SecretLockTransactionFactory)1 SecretProofTransaction (io.nem.symbol.sdk.model.transaction.SecretProofTransaction)1 SecretProofTransactionFactory (io.nem.symbol.sdk.model.transaction.SecretProofTransactionFactory)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1