use of io.nem.symbol.sdk.model.transaction.Transaction in project nem2-sdk-java by nemtech.
the class ListenerBase method createListenerMessage.
/**
* It create the ListenerMessage for the message subject.
*
* @param wsPayload the generic json with the wsPayload.
*/
private Observable<ListenerMessage<?>> createListenerMessage(Object wsPayload) {
String topic = jsonHelper.getString(wsPayload, "topic");
Validate.notNull(topic, "Topic must be included in the WebSocket payload!");
ListenerChannel channel = ListenerChannel.rawValueOf(StringUtils.substringBefore(topic, "/"));
String channelParams = StringUtils.substringAfter(topic, "/");
Object message = jsonHelper.getObject(wsPayload, "data");
Validate.notNull(message, "Data must be included in the WebSocket payload!");
switch(channel) {
case CONFIRMED_ADDED:
case UNCONFIRMED_ADDED:
case AGGREGATE_BONDED_ADDED:
final Transaction transaction = toTransaction(toGroup(channel), message);
final String transactionHash = transaction.getTransactionInfo().get().getHash().get();
return Observable.just(new ListenerMessage<>(topic, channel, channelParams, transaction, transactionHash));
case BLOCK:
return Observable.just(new ListenerMessage<>(topic, channel, channelParams, toBlockInfo(message), null));
case FINALIZED_BLOCK:
return Observable.just(new ListenerMessage<>(topic, channel, channelParams, toFinalizedBlock(message), null));
case STATUS:
final TransactionStatusError status = toStatus(message, channelParams);
return Observable.just(new ListenerMessage<>(topic, channel, channelParams, status, status.getHash()));
case COSIGNATURE:
return networkTypeObservable.map(networkType -> {
final CosignatureSignedTransaction cosignature = toCosignatureSignedTransaction(message, networkType);
return new ListenerMessage<>(topic, channel, channelParams, cosignature, cosignature.getParentHash());
});
case AGGREGATE_BONDED_REMOVED:
case UNCONFIRMED_REMOVED:
final String hash = jsonHelper.getString(message, "meta", "hash");
return Observable.just(new ListenerMessage<>(topic, channel, channelParams, hash, hash));
default:
throw new IllegalArgumentException("Channel " + channel + "is not supported.");
}
}
use of io.nem.symbol.sdk.model.transaction.Transaction in project nem2-sdk-java by nemtech.
the class ListenerOkHttpTest method confirmedUsingHash.
@Test
public void confirmedUsingHash() throws InterruptedException, ExecutionException, TimeoutException {
simulateWebSocketStartup();
TransactionInfoDTO transactionInfo = TestHelperOkHttp.loadTransactionInfoDTO("aggregateMosaicCreationTransaction.json");
JsonObject transactionInfoDtoJsonObject = jsonHelper.convert(transactionInfo, JsonObject.class);
Address address = Address.createFromPublicKey(jsonHelper.getString(transactionInfoDtoJsonObject, "transaction", "signerPublicKey"), networkType);
String channelName = ListenerChannel.CONFIRMED_ADDED.toString();
List<Transaction> transactions = new ArrayList<>();
List<Throwable> exceptions = new ArrayList<>();
listener.confirmedOrError(address, getHash(transactionInfo)).doOnError(exceptions::add).forEach(transactions::add);
handle(transactionInfoDtoJsonObject, channelName + "/" + address.plain());
Assertions.assertEquals(1, transactions.size());
Assertions.assertEquals(0, exceptions.size());
final Address address1 = transactions.get(0).getSigner().get().getAddress();
Assertions.assertEquals(address, address1);
Mockito.verify(webSocketMock).send(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, channelName + "/" + address.plain())));
Mockito.verify(webSocketMock).send(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, "status" + "/" + address.plain())));
}
use of io.nem.symbol.sdk.model.transaction.Transaction in project nem2-sdk-java by nemtech.
the class ListenerOkHttpTest method aggregateBondedAddedRaisingError.
@Test
public void aggregateBondedAddedRaisingError() throws InterruptedException, ExecutionException, TimeoutException {
simulateWebSocketStartup();
TransactionInfoDTO transactionInfo = TestHelperOkHttp.loadTransactionInfoDTO("aggregateMosaicCreationTransaction.json");
JsonObject transactionInfoDtoJsonObject = jsonHelper.convert(transactionInfo, JsonObject.class);
Address address = Address.createFromPublicKey(jsonHelper.getString(transactionInfoDtoJsonObject, "transaction", "signerPublicKey"), networkType);
String channelName = ListenerChannel.AGGREGATE_BONDED_ADDED.toString();
Map<String, Object> transactionStatusError = new HashMap<>();
transactionStatusError.put("address", address.encoded());
transactionStatusError.put("code", "Fail 666");
transactionStatusError.put("hash", getHash(transactionInfo));
transactionStatusError.put("deadline", 123);
List<Transaction> transactions = new ArrayList<>();
List<Throwable> exceptions = new ArrayList<>();
listener.aggregateBondedAddedOrError(address, getHash(transactionInfo)).doOnError(exceptions::add).forEach(transactions::add);
Mockito.verify(webSocketMock).send(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, channelName + "/" + address.plain())));
Mockito.verify(webSocketMock).send(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, "status" + "/" + address.plain())));
handle(transactionStatusError, "status/" + address.plain());
Assertions.assertEquals(0, transactions.size());
Assertions.assertEquals(1, exceptions.size());
Assertions.assertEquals(TransactionStatusException.class, exceptions.get(0).getClass());
Assertions.assertEquals("Fail 666 processing transaction " + getHash(transactionInfo), exceptions.get(0).getMessage());
}
use of io.nem.symbol.sdk.model.transaction.Transaction in project nem2-sdk-java by nemtech.
the class CreatingAnEscrowContractWithAggregateBondedTransactionIntegrationTest method executeTransfer.
@Test
@Disabled
void executeTransfer() {
Account ticketDistributorAccount = this.config().getTestAccount();
Account aliceAccount = this.config().getTestAccount2();
RepositoryType type = DEFAULT_REPOSITORY_TYPE;
MosaicId mosaicId = createMosaic(ticketDistributorAccount, type, BigInteger.ZERO, null);
TransferTransaction aliceToTicketDistributorTx = TransferTransactionFactory.create(getNetworkType(), getDeadline(), ticketDistributorAccount.getAddress(), Collections.singletonList(getNetworkCurrency().createRelative(BigInteger.valueOf(100)))).message(new PlainMessage("send 100 cat.currency to distributor")).maxFee(maxFee).build();
TransferTransaction ticketDistributorToAliceTx = TransferTransactionFactory.create(getNetworkType(), getDeadline(), aliceAccount.getAddress(), Collections.singletonList(new Mosaic(mosaicId, BigInteger.ONE))).message(new PlainMessage("send 1 museum ticket to alice")).maxFee(maxFee).build();
/* end block 01 */
/* start block 02 */
AggregateTransaction aggregateTransaction = AggregateTransactionFactory.createBonded(getNetworkType(), getDeadline(), Arrays.asList(aliceToTicketDistributorTx.toAggregate(aliceAccount.getPublicAccount()), ticketDistributorToAliceTx.toAggregate(ticketDistributorAccount.getPublicAccount()))).maxFee(maxFee).build();
String networkGenerationHash = getGenerationHash();
SignedTransaction signedTransaction = aliceAccount.sign(aggregateTransaction, networkGenerationHash);
System.out.println("Aggregate Transaction Hash: " + signedTransaction.getHash());
/* end block 02 */
/* start block 03 */
HashLockTransaction hashLockTransaction = HashLockTransactionFactory.create(getNetworkType(), getDeadline(), getNetworkCurrency().createRelative(BigInteger.TEN), BigInteger.valueOf(480), signedTransaction).maxFee(maxFee).build();
SignedTransaction signedHashLockTransaction = aliceAccount.sign(hashLockTransaction, networkGenerationHash);
System.out.println("Hash Transaction Hash: " + hashLockTransaction.getHash());
TransactionService transactionService = getTransactionService(type);
Transaction transaction = get(transactionService.announceHashLockAggregateBonded(getListener(type), signedHashLockTransaction, signedTransaction));
Assertions.assertNotNull(transaction);
}
use of io.nem.symbol.sdk.model.transaction.Transaction in project nem2-sdk-java by nemtech.
the class ListenerIntegrationTest method shouldReturnCosignatureAddedViaListener.
@ParameterizedTest
@EnumSource(RepositoryType.class)
@Disabled
void shouldReturnCosignatureAddedViaListener(RepositoryType type) {
Listener listener = getListener(type);
RepositoryFactory repositoryFactory = getRepositoryFactory(type);
TransactionService transactionService = new TransactionServiceImpl(repositoryFactory);
Pair<SignedTransaction, SignedTransaction> pair = this.createAggregateBondedTransaction(type);
System.out.println("Announcing HashLock transaction " + pair.getRight().getHash());
get(transactionService.announce(listener, pair.getRight()));
SignedTransaction signedTransaction = pair.getLeft();
AggregateTransaction announcedTransaction = get(transactionService.announceAggregateBonded(listener, signedTransaction));
assertEquals(signedTransaction.getHash(), announcedTransaction.getTransactionInfo().get().getHash().get());
TransactionRepository transactionRepository = getRepositoryFactory(type).createTransactionRepository();
List<Transaction> transactions = get(transactionRepository.search(new TransactionSearchCriteria(TransactionGroup.CONFIRMED).transactionTypes(Collections.singletonList(TransactionType.AGGREGATE_BONDED)).signerPublicKey(this.cosignatoryAccount.getPublicAccount().getPublicKey()))).getData();
AggregateTransaction transactionToCosign = (AggregateTransaction) transactions.get(0);
this.announceCosignatureTransaction(transactionToCosign, type);
CosignatureSignedTransaction cosignatureSignedTransaction = get(listener.cosignatureAdded(this.cosignatoryAccount.getAddress()).take(1));
assertEquals(cosignatureSignedTransaction.getSigner(), this.cosignatoryAccount2.getPublicAccount());
}
Aggregations