use of com.hedera.hashgraph.sdk.TransactionId in project hedera-sdk-java by hashgraph.
the class ScheduleMultiSigTransactionExample method main.
public static void main(String[] args) throws Exception {
Client client = Client.forName(HEDERA_NETWORK);
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
AccountId operatorId = Objects.requireNonNull(client.getOperatorAccountId());
// Generate 3 random keys
PrivateKey key1 = PrivateKey.generateED25519();
PrivateKey key2 = PrivateKey.generateED25519();
PrivateKey key3 = PrivateKey.generateED25519();
// Create a keylist from those keys. This key will be used as the new account's key
// The reason we want to use a `KeyList` is to simulate a multi-party system where
// multiple keys are required to sign.
KeyList keyList = new KeyList();
keyList.add(key1.getPublicKey());
keyList.add(key2.getPublicKey());
keyList.add(key3.getPublicKey());
System.out.println("key1 private = " + key1);
System.out.println("key1 public = " + key1.getPublicKey());
System.out.println("key1 private = " + key2);
System.out.println("key2 public = " + key2.getPublicKey());
System.out.println("key1 private = " + key3);
System.out.println("key3 public = " + key3.getPublicKey());
System.out.println("keyList = " + keyList);
// Creat the account with the `KeyList`
TransactionResponse response = new AccountCreateTransaction().setNodeAccountIds(Collections.singletonList(new AccountId(3))).setKey(keyList).setInitialBalance(new Hbar(10)).execute(client);
// This will wait for the receipt to become available
@Var TransactionReceipt receipt = response.getReceipt(client);
AccountId accountId = Objects.requireNonNull(receipt.accountId);
System.out.println("accountId = " + accountId);
// Generate a `TransactionId`. This id is used to query the inner scheduled transaction
// after we expect it to have been executed
TransactionId transactionId = TransactionId.generate(operatorId);
System.out.println("transactionId for scheduled transaction = " + transactionId);
// Create a transfer transaction with 2/3 signatures.
@Var TransferTransaction transfer = new TransferTransaction().addHbarTransfer(accountId, new Hbar(1).negated()).addHbarTransfer(operatorId, new Hbar(1));
// Schedule the transactoin
ScheduleCreateTransaction scheduled = transfer.schedule().setPayerAccountId(client.getOperatorAccountId()).setAdminKey(client.getOperatorPublicKey()).freezeWith(client).sign(key2);
receipt = scheduled.execute(client).getReceipt(client);
// Get the schedule ID from the receipt
ScheduleId scheduleId = Objects.requireNonNull(receipt.scheduleId);
System.out.println("scheduleId = " + scheduleId);
// Get the schedule info to see if `signatories` is populated with 2/3 signatures
ScheduleInfo info = new ScheduleInfoQuery().setNodeAccountIds(Collections.singletonList(response.nodeId)).setScheduleId(scheduleId).execute(client);
System.out.println("Schedule Info = " + info);
transfer = (TransferTransaction) info.getScheduledTransaction();
Map<AccountId, Hbar> transfers = transfer.getHbarTransfers();
// Make sure the transfer transaction is what we expect
if (transfers.size() != 2) {
throw new Exception("more transfers than expected");
}
if (!transfers.get(accountId).equals(new Hbar(1).negated())) {
throw new Exception("transfer for " + accountId + " is not what is expected " + transfers.get(accountId));
}
if (!transfers.get(operatorId).equals(new Hbar(1))) {
throw new Exception("transfer for " + operatorId + " is not what is expected " + transfers.get(operatorId));
}
System.out.println("sending schedule sign transaction");
// Finally send this last signature to Hedera. This last signature _should_ mean the transaction executes
// since all 3 signatures have been provided.
new ScheduleSignTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setScheduleId(scheduleId).freezeWith(client).sign(key3).execute(client).getReceipt(client);
// Query the schedule info again
new ScheduleInfoQuery().setNodeAccountIds(Collections.singletonList(response.nodeId)).setScheduleId(scheduleId).execute(client);
}
use of com.hedera.hashgraph.sdk.TransactionId in project hedera-mirror-node by hashgraph.
the class TopicClient method publishMessageToTopic.
public TransactionId publishMessageToTopic(TopicId topicId, byte[] message, KeyList submitKeys) {
TopicMessageSubmitTransaction consensusMessageSubmitTransaction = new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage(message).setTransactionMemo(getMemo("Publish topic message"));
TransactionId transactionId = executeTransaction(consensusMessageSubmitTransaction, submitKeys);
TransactionRecord transactionRecord = getTransactionRecord(transactionId);
// get only the 1st sequence number
if (recordPublishInstants.size() == 0) {
recordPublishInstants.put(0L, transactionRecord.consensusTimestamp);
}
if (log.isTraceEnabled()) {
log.trace("Published message : '{}' to topicId : {} with consensusTimestamp: {}", new String(message, StandardCharsets.UTF_8), topicId, transactionRecord.consensusTimestamp);
}
return transactionId;
}
use of com.hedera.hashgraph.sdk.TransactionId in project hedera-mirror-node by hashgraph.
the class AbstractNetworkClient method executeTransactionAndRetrieveReceipt.
public NetworkTransactionResponse executeTransactionAndRetrieveReceipt(Transaction transaction, KeyList keyList, ExpandedAccountId payer) {
long startBalance = getBalance();
TransactionId transactionId = executeTransaction(transaction, keyList, payer);
TransactionReceipt transactionReceipt = null;
try {
transactionReceipt = getTransactionReceipt(transactionId);
} catch (Exception e) {
log.error("Failed to get transaction receipt for {}: {}", transactionId, e.getMessage());
}
log.trace("Executed transaction {} cost {} tℏ", transactionId, startBalance - getBalance());
return new NetworkTransactionResponse(transactionId, transactionReceipt);
}
use of com.hedera.hashgraph.sdk.TransactionId in project hedera-mirror-node by hashgraph.
the class PublishMetricsTest method onErrorReceiptStatusException.
@Test
void onErrorReceiptStatusException() throws Exception {
TransactionId transactionId = TransactionId.withValidStart(AccountId.fromString("0.0.3"), Instant.now());
TransactionReceipt transactionReceipt = receipt(ResponseCodeEnum.SUCCESS);
Constructor<ReceiptStatusException> constructor = getDeclaredConstructor(ReceiptStatusException.class);
constructor.setAccessible(true);
ReceiptStatusException receiptStatusException = constructor.newInstance(transactionId, transactionReceipt);
onError(receiptStatusException, ResponseCodeEnum.SUCCESS.toString());
}
use of com.hedera.hashgraph.sdk.TransactionId in project hedera-mirror-node by hashgraph.
the class RestSubscriber method clientSubscribe.
private Flux<SubscribeResponse> clientSubscribe(RestSubscription subscription) {
RestSubscriberProperties properties = subscription.getProperties();
RetryBackoffSpec retrySpec = Retry.backoff(properties.getRetry().getMaxAttempts(), properties.getRetry().getMinBackoff()).maxBackoff(properties.getRetry().getMaxBackoff()).filter(this::shouldRetry).doBeforeRetry(r -> log.debug("Retry attempt #{} after failure: {}", r.totalRetries() + 1, r.failure().getMessage()));
return subscription.getSink().asFlux().publishOn(Schedulers.parallel()).doFinally(s -> subscription.onComplete()).doOnNext(publishResponse -> log.trace("Querying REST API: {}", publishResponse)).flatMap(publishResponse -> webClient.get().uri("/transactions/{transactionId}", toString(publishResponse.getTransactionId())).retrieve().bodyToMono(MirrorTransaction.class).name("rest").metrics().timeout(properties.getTimeout()).retryWhen(retrySpec).onErrorContinue((t, o) -> subscription.onError(t)).doOnNext(subscription::onNext).map(transaction -> toResponse(subscription, publishResponse, transaction))).take(properties.getLimit(), true).take(properties.getDuration());
}
Aggregations