Search in sources :

Example 1 with ScheduleCreateTransaction

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

the class ScheduleIdenticalTransactionExample method main.

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);
    System.out.println("threshold key example");
    System.out.println("Keys:");
    PrivateKey[] privKeys = new PrivateKey[3];
    PublicKey[] pubKeys = new PublicKey[3];
    Client[] clients = new Client[3];
    AccountId[] accounts = new AccountId[3];
    @Var ScheduleId scheduleID = null;
    // Loop to generate keys, clients, and accounts
    for (int i = 0; i < 3; i++) {
        PrivateKey newKey = PrivateKey.generateED25519();
        privKeys[i] = newKey;
        pubKeys[i] = newKey.getPublicKey();
        System.out.println("Key #" + i + ":");
        System.out.println("private = " + privKeys[i]);
        System.out.println("public = " + pubKeys[i]);
        TransactionResponse createResponse = new AccountCreateTransaction().setKey(newKey).setInitialBalance(new Hbar(1)).execute(client);
        // Make sure the transaction succeeded
        TransactionReceipt transactionReceipt = createResponse.getReceipt(client);
        Client newClient = Client.forName(HEDERA_NETWORK);
        newClient.setOperator(Objects.requireNonNull(transactionReceipt.accountId), newKey);
        clients[i] = newClient;
        accounts[i] = transactionReceipt.accountId;
        System.out.println("account = " + accounts[i]);
    }
    // Loop to generate keys, clients, and accounts
    // A threshold key with a threshold of 2 and length of 3 requires
    // at least 2 of the 3 keys to sign anything modifying the account
    KeyList keyList = KeyList.withThreshold(2);
    Collections.addAll(keyList, pubKeys);
    // We are using all of these keys, so the scheduled transaction doesn't automatically go through
    // It works perfectly fine with just one key
    TransactionResponse createResponse = new AccountCreateTransaction().setKey(keyList).setInitialBalance(new Hbar(10)).execute(client);
    // Make sure the transaction succeeded
    TransactionReceipt receipt = createResponse.getReceipt(client);
    AccountId thresholdAccount = receipt.accountId;
    System.out.println("threshold account = " + thresholdAccount);
    for (Client loopClient : clients) {
        AccountId operatorId = loopClient.getOperatorAccountId();
        // Each loopClient creates an identical transaction, sending 1 hbar to each of the created accounts,
        // sent from the threshold Account
        TransferTransaction tx = new TransferTransaction();
        for (AccountId account : accounts) {
            tx.addHbarTransfer(account, new Hbar(1));
        }
        tx.addHbarTransfer(Objects.requireNonNull(thresholdAccount), new Hbar(3).negated());
        ScheduleCreateTransaction scheduledTx = new ScheduleCreateTransaction().setScheduledTransaction(tx);
        scheduledTx.setPayerAccountId(thresholdAccount);
        TransactionResponse response = scheduledTx.execute(loopClient);
        TransactionReceipt loopReceipt = new TransactionReceiptQuery().setTransactionId(response.transactionId).setNodeAccountIds(Collections.singletonList(response.nodeId)).execute(loopClient);
        System.out.println("operator [" + operatorId + "]: scheduleID = " + loopReceipt.scheduleId);
        // Save the schedule ID, so that it can be asserted for each loopClient submission
        if (scheduleID == null) {
            scheduleID = loopReceipt.scheduleId;
        }
        if (!scheduleID.equals(Objects.requireNonNull(loopReceipt.scheduleId))) {
            System.out.println("invalid generated schedule id, expected " + scheduleID + ", got " + loopReceipt.scheduleId);
            return;
        }
        // If the status return by the receipt is related to already created, execute a schedule sign transaction
        if (loopReceipt.status == Status.IDENTICAL_SCHEDULE_ALREADY_CREATED) {
            TransactionResponse signTransaction = new ScheduleSignTransaction().setScheduleId(scheduleID).setNodeAccountIds(Collections.singletonList(createResponse.nodeId)).setScheduleId(loopReceipt.scheduleId).execute(loopClient);
            TransactionReceipt signReceipt = new TransactionReceiptQuery().setTransactionId(signTransaction.transactionId).execute(client);
            if (signReceipt.status != Status.SUCCESS && signReceipt.status != Status.SCHEDULE_ALREADY_EXECUTED) {
                System.out.println("Bad status while getting receipt of schedule sign with operator " + operatorId + ": " + signReceipt.status);
                return;
            }
        }
    }
    System.out.println(new ScheduleInfoQuery().setScheduleId(scheduleID).execute(client));
    AccountDeleteTransaction thresholdDeleteTx = new AccountDeleteTransaction().setAccountId(thresholdAccount).setTransferAccountId(OPERATOR_ID).freezeWith(client);
    for (int i = 0; i < 3; i++) {
        thresholdDeleteTx.sign(privKeys[i]);
        new AccountDeleteTransaction().setAccountId(accounts[i]).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(privKeys[i]).execute(client).getReceipt(client);
    }
    thresholdDeleteTx.execute(client).getReceipt(client);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) 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) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransactionReceiptQuery(com.hedera.hashgraph.sdk.TransactionReceiptQuery) 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 ScheduleCreateTransaction

use of com.hedera.hashgraph.sdk.ScheduleCreateTransaction 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 3 with ScheduleCreateTransaction

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

the class ScheduleCreateTransactionSupplierTest method createWithMinimumData.

@Test
void createWithMinimumData() {
    ScheduleCreateTransactionSupplier scheduleCreateTransactionSupplier = new ScheduleCreateTransactionSupplier();
    scheduleCreateTransactionSupplier.setOperatorAccountId(ACCOUNT_ID.toString());
    scheduleCreateTransactionSupplier.setPayerAccount(ACCOUNT_ID_2.toString());
    ScheduleCreateTransaction actual = scheduleCreateTransactionSupplier.get();
    assertThat(actual).returns(null, a -> a.getAdminKey()).returns(MAX_TRANSACTION_FEE_HBAR, ScheduleCreateTransaction::getMaxTransactionFee).returns(ACCOUNT_ID_2, ScheduleCreateTransaction::getPayerAccountId).extracting(ScheduleCreateTransaction::getScheduleMemo, STRING).contains("Mirror node created test schedule");
}
Also used : ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction) Test(org.junit.jupiter.api.Test) AbstractTransactionSupplierTest(com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)

Example 4 with ScheduleCreateTransaction

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

the class ScheduleCreateTransactionSupplier method get.

@Override
public ScheduleCreateTransaction get() {
    Hbar maxTransactionFee = Hbar.fromTinybars(getMaxTransactionFee());
    TransferTransaction innerTransaction = new TransferTransaction().setMaxTransactionFee(maxTransactionFee).addHbarTransfer(getOperatorId(), Hbar.fromTinybars(1L).negated()).addHbarTransfer(getPayerAccountId(), Hbar.fromTinybars(1L));
    ScheduleCreateTransaction scheduleCreateTransaction = new ScheduleCreateTransaction().setAdminKey(getAdminPublicKey()).setMaxTransactionFee(maxTransactionFee).setPayerAccountId(getPayerAccountId()).setScheduleMemo(Utility.getMemo("Mirror node created test schedule")).setScheduledTransaction(innerTransaction);
    return scheduleCreateTransaction;
}
Also used : Hbar(com.hedera.hashgraph.sdk.Hbar) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction)

Example 5 with ScheduleCreateTransaction

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

the class ScheduleClient method createSchedule.

public NetworkTransactionResponse createSchedule(ExpandedAccountId payerAccountId, Transaction transaction, KeyList signatureKeyList) {
    String memo = getMemo("Create schedule");
    ScheduleCreateTransaction scheduleCreateTransaction = new ScheduleCreateTransaction().setAdminKey(payerAccountId.getPublicKey()).setMaxTransactionFee(sdkClient.getMaxTransactionFee()).setPayerAccountId(payerAccountId.getAccountId()).setScheduleMemo(memo).setScheduledTransaction(transaction).setTransactionMemo(memo);
    if (signatureKeyList != null) {
        scheduleCreateTransaction.setNodeAccountIds(List.of(sdkClient.getRandomNodeAccountId())).freezeWith(client);
        // add initial set of required signatures to ScheduleCreate transaction
        signatureKeyList.forEach(k -> {
            PrivateKey pk = (PrivateKey) k;
            byte[] signature = pk.signTransaction(scheduleCreateTransaction);
            scheduleCreateTransaction.addSignature(pk.getPublicKey(), signature);
        });
    }
    NetworkTransactionResponse networkTransactionResponse = executeTransactionAndRetrieveReceipt(scheduleCreateTransaction);
    ScheduleId scheduleId = networkTransactionResponse.getReceipt().scheduleId;
    log.debug("Created new schedule {}", scheduleId);
    return networkTransactionResponse;
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) ScheduleId(com.hedera.hashgraph.sdk.ScheduleId) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction)

Aggregations

ScheduleCreateTransaction (com.hedera.hashgraph.sdk.ScheduleCreateTransaction)10 Hbar (com.hedera.hashgraph.sdk.Hbar)7 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)6 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)6 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)5 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)5 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)5 Test (org.junit.jupiter.api.Test)5 AccountId (com.hedera.hashgraph.sdk.AccountId)4 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)4 Var (com.google.errorprone.annotations.Var)3 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)3 Client (com.hedera.hashgraph.sdk.Client)3 KeyList (com.hedera.hashgraph.sdk.KeyList)3 ScheduleInfo (com.hedera.hashgraph.sdk.ScheduleInfo)3 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)3 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)3 DisplayName (org.junit.jupiter.api.DisplayName)3 PublicKey (com.hedera.hashgraph.sdk.PublicKey)2 TransactionId (com.hedera.hashgraph.sdk.TransactionId)2