Search in sources :

Example 1 with TransactionId

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);
}
Also used : AccountId(com.hedera.hashgraph.sdk.AccountId) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) Var(com.google.errorprone.annotations.Var) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) KeyList(com.hedera.hashgraph.sdk.KeyList) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) ScheduleId(com.hedera.hashgraph.sdk.ScheduleId) ScheduleInfo(com.hedera.hashgraph.sdk.ScheduleInfo) TransactionId(com.hedera.hashgraph.sdk.TransactionId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction)

Example 2 with TransactionId

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;
}
Also used : TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) TransactionRecord(com.hedera.hashgraph.sdk.TransactionRecord) TransactionId(com.hedera.hashgraph.sdk.TransactionId)

Example 3 with 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);
}
Also used : NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) TransactionId(com.hedera.hashgraph.sdk.TransactionId)

Example 4 with TransactionId

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());
}
Also used : TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) ReceiptStatusException(com.hedera.hashgraph.sdk.ReceiptStatusException) TransactionId(com.hedera.hashgraph.sdk.TransactionId) Test(org.junit.jupiter.api.Test)

Example 5 with TransactionId

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());
}
Also used : TransactionId(com.hedera.hashgraph.sdk.TransactionId) Retry(reactor.util.retry.Retry) SubscribeProperties(com.hedera.mirror.monitor.subscribe.SubscribeProperties) MirrorSubscriber(com.hedera.mirror.monitor.subscribe.MirrorSubscriber) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) Set(java.util.Set) WebClient(org.springframework.web.reactive.function.client.WebClient) RetryBackoffSpec(reactor.util.retry.RetryBackoffSpec) Instant(java.time.Instant) ArrayList(java.util.ArrayList) SecureRandom(java.security.SecureRandom) MirrorTransaction(com.hedera.mirror.monitor.subscribe.rest.response.MirrorTransaction) HttpStatus(org.springframework.http.HttpStatus) Flux(reactor.core.publisher.Flux) PublishResponse(com.hedera.mirror.monitor.publish.PublishResponse) List(java.util.List) Log4j2(lombok.extern.log4j.Log4j2) Schedulers(reactor.core.scheduler.Schedulers) WebClientResponseException(org.springframework.web.reactive.function.client.WebClientResponseException) Named(javax.inject.Named) MonitorProperties(com.hedera.mirror.monitor.MonitorProperties) SubscribeResponse(com.hedera.mirror.monitor.subscribe.SubscribeResponse) RetryBackoffSpec(reactor.util.retry.RetryBackoffSpec)

Aggregations

TransactionId (com.hedera.hashgraph.sdk.TransactionId)11 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)6 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)4 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)4 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)3 AccountId (com.hedera.hashgraph.sdk.AccountId)3 KeyList (com.hedera.hashgraph.sdk.KeyList)3 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)3 ScheduleInfo (com.hedera.hashgraph.sdk.ScheduleInfo)3 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)3 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)3 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)3 Test (org.junit.jupiter.api.Test)3 Var (com.google.errorprone.annotations.Var)2 Client (com.hedera.hashgraph.sdk.Client)2 Hbar (com.hedera.hashgraph.sdk.Hbar)2 ScheduleCreateTransaction (com.hedera.hashgraph.sdk.ScheduleCreateTransaction)2 TransactionRecord (com.hedera.hashgraph.sdk.TransactionRecord)2 TransactionRecordQuery (com.hedera.hashgraph.sdk.TransactionRecordQuery)2 NetworkTransactionResponse (com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse)2