Search in sources :

Example 1 with ScheduleSignTransaction

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

the class ScheduleCreateIntegrationTest method canScheduleTopicMessage.

@Test
@DisplayName("Can schedule topic message")
void canScheduleTopicMessage() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    // Generate 3 random keys
    var key1 = PrivateKey.generateED25519();
    // This is the submit key
    var key2 = PrivateKey.generateED25519();
    var key3 = PrivateKey.generateED25519();
    var keyList = new KeyList();
    keyList.add(key1.getPublicKey());
    keyList.add(key2.getPublicKey());
    keyList.add(key3.getPublicKey());
    var response = new AccountCreateTransaction().setInitialBalance(new Hbar(100)).setKey(keyList).execute(testEnv.client);
    assertThat(response.getReceipt(testEnv.client).accountId).isNotNull();
    var topicId = Objects.requireNonNull(new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setAutoRenewAccountId(testEnv.operatorId).setTopicMemo("HCS Topic_").setSubmitKey(key2.getPublicKey()).execute(testEnv.client).getReceipt(testEnv.client).topicId);
    var transaction = new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage("scheduled hcs message".getBytes(StandardCharsets.UTF_8));
    // create schedule
    var scheduledTx = transaction.schedule().setAdminKey(testEnv.operatorKey).setPayerAccountId(testEnv.operatorId).setScheduleMemo("mirror scheduled E2E signature on create and sign_" + Instant.now());
    var scheduled = scheduledTx.freezeWith(testEnv.client);
    var scheduleId = Objects.requireNonNull(scheduled.execute(testEnv.client).getReceipt(testEnv.client).scheduleId);
    // verify schedule has been created and has 1 of 2 signatures
    @Var var info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info).isNotNull();
    assertThat(info.scheduleId).isEqualTo(scheduleId);
    var infoTransaction = (TopicMessageSubmitTransaction) info.getScheduledTransaction();
    assertThat(transaction.getTopicId()).isEqualTo(infoTransaction.getTopicId());
    assertThat(transaction.getNodeAccountIds()).isEqualTo(infoTransaction.getNodeAccountIds());
    var scheduleSign = new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(testEnv.client);
    scheduleSign.sign(key2).execute(testEnv.client).getReceipt(testEnv.client);
    info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info.executedAt).isNotNull();
    testEnv.close();
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) Var(com.google.errorprone.annotations.Var) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) KeyList(com.hedera.hashgraph.sdk.KeyList) Hbar(com.hedera.hashgraph.sdk.Hbar) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with ScheduleSignTransaction

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

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

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

the class ScheduleSignTransactionSupplierTest method createWithMinimumData.

@Test
void createWithMinimumData() {
    ScheduleSignTransactionSupplier scheduleSignTransactionSupplier = new ScheduleSignTransactionSupplier();
    scheduleSignTransactionSupplier.setScheduleId(SCHEDULE_ID.toString());
    ScheduleSignTransaction actual = scheduleSignTransactionSupplier.get();
    assertThat(actual).returns(MAX_TRANSACTION_FEE_HBAR, ScheduleSignTransaction::getMaxTransactionFee).returns(SCHEDULE_ID, ScheduleSignTransaction::getScheduleId);
}
Also used : ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) Test(org.junit.jupiter.api.Test) AbstractTransactionSupplierTest(com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)

Example 5 with ScheduleSignTransaction

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

the class ScheduleSignTransactionSupplierTest method createWithCustomData.

@Test
void createWithCustomData() {
    ScheduleSignTransactionSupplier scheduleSignTransactionSupplier = new ScheduleSignTransactionSupplier();
    scheduleSignTransactionSupplier.setMaxTransactionFee(1);
    scheduleSignTransactionSupplier.setScheduleId(SCHEDULE_ID.toString());
    ScheduleSignTransaction actual = scheduleSignTransactionSupplier.get();
    assertThat(actual).returns(ONE_TINYBAR, ScheduleSignTransaction::getMaxTransactionFee).returns(SCHEDULE_ID, ScheduleSignTransaction::getScheduleId);
}
Also used : ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) Test(org.junit.jupiter.api.Test) AbstractTransactionSupplierTest(com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)

Aggregations

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