Search in sources :

Example 21 with AccountId

use of com.hedera.hashgraph.sdk.AccountId in project hedera-mirror-node by hashgraph.

the class TransactionPublisher method validateNode.

boolean validateNode(NodeProperties node) {
    try {
        log.info("Validating node {}", node);
        Hbar hbar = Hbar.fromTinybars(1L);
        AccountId nodeAccountId = AccountId.fromString(node.getAccountId());
        Client client = validationClient.get();
        Status receiptStatus = new TransferTransaction().addHbarTransfer(nodeAccountId, hbar).addHbarTransfer(client.getOperatorAccountId(), hbar.negated()).setNodeAccountIds(node.getAccountIds()).execute(client).getReceipt(client).status;
        if (receiptStatus == SUCCESS) {
            log.info("Validated node {} successfully", nodeAccountId);
            nodes.addIfAbsent(node);
            return true;
        }
        log.warn("Unable to validate node {}: invalid status code {}", node, receiptStatus);
    } catch (TimeoutException e) {
        log.warn("Unable to validate node {}: Timed out", node);
    } catch (Exception e) {
        log.warn("Unable to validate node {}: ", node, e);
    }
    nodes.remove(node);
    return false;
}
Also used : Status(com.hedera.hashgraph.sdk.Status) AccountId(com.hedera.hashgraph.sdk.AccountId) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 22 with AccountId

use of com.hedera.hashgraph.sdk.AccountId in project hedera-sdk-java by hashgraph.

the class ScheduleCreateIntegrationTest method canSignSchedule2.

@Test
@DisplayName("Can sign schedule")
void canSignSchedule2() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    PrivateKey key1 = PrivateKey.generateED25519();
    PrivateKey key2 = PrivateKey.generateED25519();
    PrivateKey key3 = PrivateKey.generateED25519();
    KeyList keyList = new KeyList();
    keyList.add(key1.getPublicKey());
    keyList.add(key2.getPublicKey());
    keyList.add(key3.getPublicKey());
    // Creat the account with the `KeyList`
    TransactionResponse response = new AccountCreateTransaction().setKey(keyList).setInitialBalance(new Hbar(10)).execute(testEnv.client);
    // This will wait for the receipt to become available
    @Var TransactionReceipt receipt = response.getReceipt(testEnv.client);
    AccountId accountId = Objects.requireNonNull(receipt.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(testEnv.operatorId);
    // Create a transfer transaction with 2/3 signatures.
    TransferTransaction transfer = new TransferTransaction().setTransactionId(transactionId).addHbarTransfer(accountId, new Hbar(1).negated()).addHbarTransfer(testEnv.operatorId, new Hbar(1));
    // Schedule the transactoin
    ScheduleCreateTransaction scheduled = transfer.schedule();
    receipt = scheduled.execute(testEnv.client).getReceipt(testEnv.client);
    // Get the schedule ID from the receipt
    ScheduleId scheduleId = Objects.requireNonNull(receipt.scheduleId);
    // Get the schedule info to see if `signatories` is populated with 2/3 signatures
    @Var ScheduleInfo info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info.executedAt).isNull();
    // Finally send this last signature to Hedera. This last signature _should_ mean the transaction executes
    // since all 3 signatures have been provided.
    ScheduleSignTransaction signTransaction = new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(testEnv.client);
    signTransaction.sign(key1).sign(key2).sign(key3).execute(testEnv.client).getReceipt(testEnv.client);
    info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info.executedAt).isNotNull();
    new AccountDeleteTransaction().setAccountId(accountId).setTransferAccountId(testEnv.operatorId).freezeWith(testEnv.client).sign(key1).sign(key2).sign(key3).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) Var(com.google.errorprone.annotations.Var) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) KeyList(com.hedera.hashgraph.sdk.KeyList) AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) 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) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 23 with AccountId

use of com.hedera.hashgraph.sdk.AccountId in project hedera-sdk-java by hashgraph.

the class ScheduledTransactionMultiSigThresholdExample method main.

// public static void main(String[] args) throws PrecheckStatusException, IOException, TimeoutException, ReceiptStatusException {
public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException {
    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);
    // Generate four new Ed25519 private, public key pairs.
    PrivateKey[] privateKeys = new PrivateKey[4];
    PublicKey[] publicKeys = new PublicKey[4];
    for (int i = 0; i < 4; i++) {
        PrivateKey key = PrivateKey.generateED25519();
        privateKeys[i] = key;
        publicKeys[i] = key.getPublicKey();
        System.out.println("public key " + (i + 1) + ": " + publicKeys[i]);
        System.out.println("private key " + (i + 1) + ": " + privateKeys[i]);
    }
    // require 3 of the 4 keys we generated to sign on anything modifying this account
    KeyList transactionKey = KeyList.withThreshold(3);
    Collections.addAll(transactionKey, publicKeys);
    TransactionResponse transactionResponse = new AccountCreateTransaction().setKey(transactionKey).setInitialBalance(Hbar.fromTinybars(1)).setAccountMemo("3-of-4 multi-sig account").execute(client);
    // This will wait for the receipt to become available
    TransactionReceipt txAccountCreateReceipt = transactionResponse.getReceipt(client);
    AccountId multiSigAccountId = Objects.requireNonNull(txAccountCreateReceipt.accountId);
    System.out.println("3-of-4 multi-sig account ID: " + multiSigAccountId);
    AccountBalance balance = new AccountBalanceQuery().setAccountId(multiSigAccountId).execute(client);
    System.out.println("Balance of account " + multiSigAccountId + ": " + balance.hbars.toTinybars() + " tinybar.");
    // schedule crypto transfer from multi-sig account to operator account
    TransactionResponse transferToSchedule = new TransferTransaction().addHbarTransfer(multiSigAccountId, Hbar.fromTinybars(-1)).addHbarTransfer(Objects.requireNonNull(client.getOperatorAccountId()), Hbar.fromTinybars(1)).schedule().freezeWith(client).sign(// add 1 signature`
    privateKeys[0]).execute(client);
    TransactionReceipt txScheduleReceipt = transferToSchedule.getReceipt(client);
    System.out.println("Schedule status: " + txScheduleReceipt.status);
    ScheduleId scheduleId = Objects.requireNonNull(txScheduleReceipt.scheduleId);
    System.out.println("Schedule ID: " + scheduleId);
    TransactionId scheduledTxId = Objects.requireNonNull(txScheduleReceipt.scheduledTransactionId);
    System.out.println("Scheduled tx ID: " + scheduledTxId);
    // add 2 signature
    TransactionResponse txScheduleSign1 = new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(client).sign(privateKeys[1]).execute(client);
    TransactionReceipt txScheduleSign1Receipt = txScheduleSign1.getReceipt(client);
    System.out.println("1. ScheduleSignTransaction status: " + txScheduleSign1Receipt.status);
    // add 3 signature
    TransactionResponse txScheduleSign2 = new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(client).sign(privateKeys[2]).execute(client);
    TransactionReceipt txScheduleSign2Receipt = txScheduleSign2.getReceipt(client);
    System.out.println("2. ScheduleSignTransaction status: " + txScheduleSign2Receipt.status);
    // query schedule
    ScheduleInfo scheduleInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
    System.out.println(scheduleInfo);
    // query triggered scheduled tx
    TransactionRecord recordScheduledTx = new TransactionRecordQuery().setTransactionId(scheduledTxId).execute(client);
    System.out.println(recordScheduledTx);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) KeyList(com.hedera.hashgraph.sdk.KeyList) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) ScheduleId(com.hedera.hashgraph.sdk.ScheduleId) ScheduleInfo(com.hedera.hashgraph.sdk.ScheduleInfo) TransactionId(com.hedera.hashgraph.sdk.TransactionId) TransactionRecordQuery(com.hedera.hashgraph.sdk.TransactionRecordQuery) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountBalance(com.hedera.hashgraph.sdk.AccountBalance) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) TransactionRecord(com.hedera.hashgraph.sdk.TransactionRecord)

Example 24 with AccountId

use of com.hedera.hashgraph.sdk.AccountId in project hedera-sdk-java by hashgraph.

the class ScheduledTransferExample method main.

public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
    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);
    Objects.requireNonNull(client.getOperatorAccountId());
    /*
         * A scheduled transaction is a transaction that has been proposed by an account,
         * but which requires more signatures before it will actually execute on the Hedera network.
         *
         * For example, if Alice wants to transfer an amount of Hbar to Bob, and Bob has
         * receiverSignatureRequired set to true, then that transaction must be signed by
         * both Alice and Bob before the transaction will be executed.
         *
         * To solve this problem, Alice can propose the transaction by creating a scheduled
         * transaction on the Hedera network which, if executed, would transfer Hbar from
         * Alice to Bob.  That scheduled transaction will have a ScheduleId by which we can
         * refer to that scheduled transaction.  Alice can communicate the ScheduleId to Bob, and
         * then Bob can use a ScheduleSignTransaction to sign that scheduled transaction.
         *
         * Bob has a 30 minute window in which to sign the scheduled transaction, starting at the
         * moment that Alice creates the scheduled transaction.  If a scheduled transaction
         * is not signed by all of the necessary signatories within the 30 minute window,
         * that scheduled transaction will expire, and will not be executed.
         *
         * Once a scheduled transaction has all of the signatures necessary to execute, it will
         * be executed on the Hedera network automatically.  If you create a scheduled transaction
         * on the Hedera network, but that transaction only requires your signature in order to
         * execute and no one else's, that scheduled transaction will be automatically
         * executed immediately.
         */
    PrivateKey bobsKey = PrivateKey.generateED25519();
    AccountId bobsId = new AccountCreateTransaction().setReceiverSignatureRequired(true).setKey(bobsKey).setInitialBalance(new Hbar(10)).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client).accountId;
    Objects.requireNonNull(bobsId);
    System.out.println("Alice's ID: " + client.getOperatorAccountId().toStringWithChecksum(client));
    System.out.println("Bob's ID: " + bobsId.toStringWithChecksum(client));
    AccountBalance bobsInitialBalance = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
    System.out.println("Bob's initial balance:");
    System.out.println(bobsInitialBalance);
    TransferTransaction transferToSchedule = new TransferTransaction().addHbarTransfer(client.getOperatorAccountId(), new Hbar(-10)).addHbarTransfer(bobsId, new Hbar(10));
    System.out.println("Transfer to be scheduled:");
    System.out.println(transferToSchedule);
    /*
         * The payerAccountId is the account that will be charged the fee
         * for executing the scheduled transaction if/when it is executed.
         * That fee is separate from the fee that we will pay to execute the
         * ScheduleCreateTransaction itself.
         *
         * To clarify: Alice pays a fee to execute the ScheduleCreateTransaction,
         * which creates the scheduled transaction on the Hedera network.
         * She specifies when creating the scheduled transaction that Bob will pay
         * the fee for the scheduled transaction when it is executed.
         *
         * If payerAccountId is not specified, the account who creates the scheduled transaction
         * will be charged for executing the scheduled transaction.
         */
    ScheduleId scheduleId = new ScheduleCreateTransaction().setScheduledTransaction(transferToSchedule).setPayerAccountId(bobsId).execute(client).getReceipt(client).scheduleId;
    Objects.requireNonNull(scheduleId);
    System.out.println("The scheduleId is: " + scheduleId.toStringWithChecksum(client));
    /*
         * Bob's balance should be unchanged.  The transfer has been scheduled, but it hasn't been executed yet
         * because it requires Bob's signature.
         */
    AccountBalance bobsBalanceAfterSchedule = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
    System.out.println("Bob's balance after scheduling the transfer (should be unchanged):");
    System.out.println(bobsBalanceAfterSchedule);
    /*
         * Once Alice has communicated the scheduleId to Bob, Bob can query for information about the
         * scheduled transaction.
         */
    ScheduleInfo scheduledTransactionInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
    System.out.println("Info about scheduled transaction:");
    System.out.println(scheduledTransactionInfo);
    /*
         * getScheduledTransaction() will return an SDK Transaction object identical to the transaction
         * that was scheduled, which Bob can then inspect like a normal transaction.
         */
    Transaction<?> scheduledTransaction = scheduledTransactionInfo.getScheduledTransaction();
    // We happen to know that this transaction is (or certainly ought to be) a TransferTransaction
    if (scheduledTransaction instanceof TransferTransaction) {
        TransferTransaction scheduledTransfer = (TransferTransaction) scheduledTransaction;
        System.out.println("The scheduled transfer transaction from Bob's POV:");
        System.out.println(scheduledTransfer);
    } else {
        System.out.println("The scheduled transaction was not a transfer transaction.");
        System.out.println("Something has gone horribly wrong.  Crashing...");
        System.exit(-1);
    }
    new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client);
    AccountBalance balanceAfterSigning = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
    System.out.println("Bob's balance after signing the scheduled transaction:");
    System.out.println(balanceAfterSigning);
    ScheduleInfo postTransactionInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
    System.out.println("Info on the scheduled transaction, executedAt should no longer be null:");
    System.out.println(postTransactionInfo);
    // Clean up
    new AccountDeleteTransaction().setTransferAccountId(client.getOperatorAccountId()).setAccountId(bobsId).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client);
    client.close();
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) ScheduleId(com.hedera.hashgraph.sdk.ScheduleId) ScheduleInfo(com.hedera.hashgraph.sdk.ScheduleInfo) AccountBalance(com.hedera.hashgraph.sdk.AccountBalance) 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 25 with AccountId

use of com.hedera.hashgraph.sdk.AccountId in project hedera-sdk-java by hashgraph.

the class UpdateAccountPublicKeyExample method main.

public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
    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);
    client.setDefaultMaxTransactionFee(new Hbar(10));
    // First, we create a new account so we don't affect our account
    PrivateKey key1 = PrivateKey.generateED25519();
    PrivateKey key2 = PrivateKey.generateED25519();
    TransactionResponse acctTransactionResponse = new AccountCreateTransaction().setKey(key1.getPublicKey()).setInitialBalance(new Hbar(1)).execute(client);
    System.out.println("transaction ID: " + acctTransactionResponse);
    AccountId accountId = Objects.requireNonNull(acctTransactionResponse.getReceipt(client).accountId);
    System.out.println("account = " + accountId);
    System.out.println("key = " + key1.getPublicKey());
    // Next, we update the key
    System.out.println(" :: update public key of account " + accountId);
    System.out.println("set key = " + key2.getPublicKey());
    TransactionResponse accountUpdateTransactionResponse = new AccountUpdateTransaction().setAccountId(accountId).setKey(key2.getPublicKey()).freezeWith(client).sign(key1).sign(key2).execute(client);
    System.out.println("transaction ID: " + accountUpdateTransactionResponse);
    // (important!) wait for the transaction to complete by querying the receipt
    accountUpdateTransactionResponse.getReceipt(client);
    // Now we fetch the account information to check if the key was changed
    System.out.println(" :: getAccount and check our current key");
    AccountInfo info = new AccountInfoQuery().setAccountId(accountId).execute(client);
    System.out.println("key = " + info.key);
}
Also used : AccountUpdateTransaction(com.hedera.hashgraph.sdk.AccountUpdateTransaction) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountId(com.hedera.hashgraph.sdk.AccountId) AccountInfoQuery(com.hedera.hashgraph.sdk.AccountInfoQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) AccountInfo(com.hedera.hashgraph.sdk.AccountInfo)

Aggregations

AccountId (com.hedera.hashgraph.sdk.AccountId)35 Client (com.hedera.hashgraph.sdk.Client)20 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)17 Hbar (com.hedera.hashgraph.sdk.Hbar)16 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)16 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)15 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)14 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)11 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)10 KeyList (com.hedera.hashgraph.sdk.KeyList)9 PublicKey (com.hedera.hashgraph.sdk.PublicKey)9 Var (com.google.errorprone.annotations.Var)8 ExpandedAccountId (com.hedera.mirror.test.e2e.acceptance.props.ExpandedAccountId)7 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)6 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)5 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)5 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)5 TimeoutException (java.util.concurrent.TimeoutException)5 AccountBalance (com.hedera.hashgraph.sdk.AccountBalance)4 ScheduleCreateTransaction (com.hedera.hashgraph.sdk.ScheduleCreateTransaction)4