use of io.nem.symbol.sdk.model.mosaic.Mosaic in project nem2-sdk-java by nemtech.
the class TestHelper method createMultisigAccountBonded.
public MultisigAccountInfo createMultisigAccountBonded(RepositoryType type, Account multisigAccount, Account... accounts) {
AccountRepository accountRepository = getRepositoryFactory(type).createAccountRepository();
MultisigRepository multisigRepository = getRepositoryFactory(type).createMultisigRepository();
AccountInfo accountInfo = get(accountRepository.getAccountInfo(multisigAccount.getAddress()));
System.out.println(getJsonHelper().print(accountInfo));
if (isMultisig(type, multisigAccount)) {
System.out.println("Multisig account with address " + multisigAccount.getAddress().plain() + " already exist");
return get(multisigRepository.getMultisigAccountInfo(multisigAccount.getAddress()));
}
System.out.println("Multisig account with address " + multisigAccount.getAddress().plain() + " does not exist. Creating");
System.out.println("Creating multisg account " + multisigAccount.getAddress().plain());
List<UnresolvedAddress> additions = Arrays.stream(accounts).map(Account::getAddress).collect(Collectors.toList());
MultisigAccountModificationTransaction convertIntoMultisigTransaction = MultisigAccountModificationTransactionFactory.create(getNetworkType(), getDeadline(), (byte) 1, (byte) 1, additions, Collections.emptyList()).maxFee(maxFee).build();
AggregateTransaction aggregateTransaction = AggregateTransactionFactory.createBonded(getNetworkType(), getDeadline(), Collections.singletonList(convertIntoMultisigTransaction.toAggregate(multisigAccount.getPublicAccount()))).maxFee(maxFee).build();
SignedTransaction signedAggregateTransaction = aggregateTransaction.signTransactionWithCosigners(multisigAccount, Arrays.asList(accounts), getGenerationHash());
Mosaic hashAmount = getCurrency().createRelative(BigInteger.valueOf(10));
HashLockTransaction hashLockTransaction = HashLockTransactionFactory.create(getNetworkType(), getDeadline(), hashAmount, BigInteger.valueOf(100), signedAggregateTransaction).maxFee(maxFee).build();
SignedTransaction signedHashLockTransaction = hashLockTransaction.signWith(multisigAccount, getGenerationHash());
getTransactionOrFail(getTransactionService(type).announceHashLockAggregateBonded(getListener(type), signedHashLockTransaction, signedAggregateTransaction), aggregateTransaction);
HashLockRepository hashLockRepository = getRepositoryFactory(type).createHashLockRepository();
HashLockInfo hashLockInfo = get(hashLockRepository.getHashLock(hashLockTransaction.getHash()));
Assertions.assertNotNull(hashLockInfo);
Assertions.assertEquals(multisigAccount.getAddress(), hashLockInfo.getOwnerAddress());
Assertions.assertEquals(hashAmount.getAmount(), hashLockInfo.getAmount());
Assertions.assertEquals(LockStatus.UNUSED, hashLockInfo.getStatus());
Assertions.assertEquals(hashLockTransaction.getHash(), hashLockInfo.getHash());
Page<HashLockInfo> page = get(hashLockRepository.search(new HashLockSearchCriteria().address(multisigAccount.getAddress())));
Assertions.assertTrue(page.getData().stream().anyMatch(m -> m.getHash().equals(hashLockTransaction.getHash())));
Assertions.assertEquals(20, page.getPageSize());
sleep(1000);
return get(multisigRepository.getMultisigAccountInfo(multisigAccount.getAddress()));
}
use of io.nem.symbol.sdk.model.mosaic.Mosaic in project nem2-sdk-java by nemtech.
the class TransactionServiceIntegrationTest method transferUsingAliases.
private TransferTransaction transferUsingAliases(Account sender, RepositoryType type, String mosaicAlias, String recipientAlias, BigInteger amount) {
NamespaceId recipientNamespace = NamespaceId.createFromName(recipientAlias);
NamespaceId mosaicNamespace = NamespaceId.createFromName(mosaicAlias);
System.out.println("Sending " + amount + " Mosaic to: " + mosaicAlias);
TransferTransactionFactory factory = TransferTransactionFactory.create(getNetworkType(), getDeadline(), recipientNamespace, Collections.singletonList(new Mosaic(mosaicNamespace, amount))).message(new PlainMessage("E2ETest:TransactionServiceIntegrationTest"));
factory.maxFee(maxFee);
TransferTransaction transferTransaction = factory.build();
Assertions.assertTrue(transferTransaction.getMosaics().get(0).getId() instanceof NamespaceId);
Assertions.assertTrue(transferTransaction.getRecipient() instanceof NamespaceId);
TransferTransaction processedTransferTransaction = announceAndValidate(type, sender, transferTransaction);
Assertions.assertEquals(amount, processedTransferTransaction.getMosaics().get(0).getAmount());
System.out.println(toJson(processedTransferTransaction));
Assertions.assertTrue(processedTransferTransaction.getMosaics().get(0).getId() instanceof NamespaceId);
Assertions.assertTrue(processedTransferTransaction.getRecipient() instanceof NamespaceId);
return processedTransferTransaction;
}
use of io.nem.symbol.sdk.model.mosaic.Mosaic 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());
}
use of io.nem.symbol.sdk.model.mosaic.Mosaic in project nem2-sdk-java by nemtech.
the class TransactionServiceTest method transferTransactionResolveAliasCannotAddressResolveAliases.
@Test
void transferTransactionResolveAliasCannotAddressResolveAliases() {
ArrayList<Mosaic> mosaics = new ArrayList<>();
mosaics.add(new Mosaic(mosaicId1, BigInteger.valueOf(1)));
mosaics.add(new Mosaic(mosaicNamespace2, BigInteger.valueOf(2)));
mosaics.add(new Mosaic(mosaicId3, BigInteger.valueOf(3)));
UnresolvedAddress recipient = addressNamespace1;
String transactionHash = "aaaa";
TransactionFactory<TransferTransaction> factory = TransferTransactionFactory.create(NetworkType.MIJIN_TEST, new Deadline(BigInteger.ONE), recipient, mosaics).transactionInfo(TransactionInfo.create(height, 0, "ABC", transactionHash, ""));
simulateStatement(height, 0, 0);
TransferTransaction transaction = factory.build();
List<String> hashes = Collections.singletonList(transactionHash);
Mockito.when(transactionRepositoryMock.getTransactions(Mockito.eq(TransactionGroup.CONFIRMED), Mockito.eq(hashes))).thenReturn(Observable.just(Collections.singletonList(transaction)));
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> ExceptionUtils.propagate(() -> service.resolveAliases(hashes).toFuture().get()));
Assertions.assertEquals("Address could not be resolved for alias " + addressNamespace1.getIdAsHex(), exception.getMessage());
}
use of io.nem.symbol.sdk.model.mosaic.Mosaic in project nem2-sdk-java by nemtech.
the class TransactionServiceTest method secretLockTransactionResolveAlias.
@Test
void secretLockTransactionResolveAlias() throws ExecutionException, InterruptedException {
Mosaic unresolvedMosaicId = new Mosaic(mosaicNamespace2, BigInteger.valueOf(2));
UnresolvedAddress recipient = addressNamespace1;
String transactionHash = "aaaa";
String secret = "3fc8ba10229ab5778d05d9c4b7f56676a88bf9295c185acfc0f961db5408cafe";
TransactionFactory<SecretLockTransaction> factory = SecretLockTransactionFactory.create(NetworkType.MIJIN_TEST, new Deadline(BigInteger.ONE), unresolvedMosaicId, BigInteger.TEN, LockHashAlgorithm.SHA3_256, secret, recipient).transactionInfo(TransactionInfo.create(height, 0, "ABC", transactionHash, ""));
simulateStatement(height, 1, 0);
SecretLockTransaction transaction = factory.build();
List<String> hashes = Collections.singletonList(transactionHash);
Mockito.when(transactionRepositoryMock.getTransactions(Mockito.eq(TransactionGroup.CONFIRMED), Mockito.eq(hashes))).thenReturn(Observable.just(Collections.singletonList(transaction)));
SecretLockTransaction resolvedTransaction = (SecretLockTransaction) service.resolveAliases(hashes).toFuture().get().get(0);
Assertions.assertEquals(mosaicId2, resolvedTransaction.getMosaic().getId());
Assertions.assertEquals(address1, resolvedTransaction.getRecipient());
}
Aggregations