Search in sources :

Example 11 with TransactionResponse

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

the class DeleteFileExample 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);
    // The file is required to be a byte array,
    // you can easily use the bytes of a file instead.
    String fileContents = "Hedera hashgraph is great!";
    TransactionResponse transactionResponse = new FileCreateTransaction().setKeys(OPERATOR_KEY).setContents(fileContents).setMaxTransactionFee(new Hbar(2)).execute(client);
    TransactionReceipt receipt = transactionResponse.getReceipt(client);
    FileId newFileId = Objects.requireNonNull(receipt.fileId);
    System.out.println("file: " + newFileId);
    // now delete the file
    TransactionResponse fileDeleteTransactionResponse = new FileDeleteTransaction().setFileId(newFileId).execute(client);
    // if this doesn't throw then the transaction was a success
    fileDeleteTransactionResponse.getReceipt(client);
    System.out.println("File deleted successfully.");
    new FileInfoQuery().setFileId(newFileId).execute(client);
// note the above fileInfo will fail with FILE_DELETED due to a known issue on Hedera
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) FileInfoQuery(com.hedera.hashgraph.sdk.FileInfoQuery) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) FileDeleteTransaction(com.hedera.hashgraph.sdk.FileDeleteTransaction) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) FileId(com.hedera.hashgraph.sdk.FileId) Client(com.hedera.hashgraph.sdk.Client)

Example 12 with TransactionResponse

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

the class MultiSigOfflineExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InvalidProtocolBufferException {
    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);
    PrivateKey user1Key = PrivateKey.generateED25519();
    PrivateKey user2Key = PrivateKey.generateED25519();
    System.out.println("private key for user 1 = " + user1Key);
    System.out.println("public key for user 1 = " + user1Key.getPublicKey());
    System.out.println("private key for user 2 = " + user2Key);
    System.out.println("public key for user 2 = " + user2Key.getPublicKey());
    // create a multi-sig account
    KeyList keylist = new KeyList();
    keylist.add(user1Key);
    keylist.add(user2Key);
    TransactionResponse createAccountTransaction = new AccountCreateTransaction().setInitialBalance(new Hbar(2)).setKey(keylist).execute(client);
    @Var TransactionReceipt receipt = createAccountTransaction.getReceipt(client);
    System.out.println("account id = " + receipt.accountId);
    // create a transfer from new account to 0.0.3
    TransferTransaction transferTransaction = new TransferTransaction().setNodeAccountIds(Collections.singletonList(new AccountId(3))).addHbarTransfer(Objects.requireNonNull(receipt.accountId), Hbar.from(-1)).addHbarTransfer(new AccountId(3), new Hbar(1)).freezeWith(client);
    // convert transaction to bytes to send to signatories
    byte[] transactionBytes = transferTransaction.toBytes();
    Transaction<?> transactionToExecute = Transaction.fromBytes(transactionBytes);
    // ask users to sign and return signature
    byte[] user1Signature = user1Key.signTransaction(Transaction.fromBytes(transactionBytes));
    byte[] user2Signature = user2Key.signTransaction(Transaction.fromBytes(transactionBytes));
    // recreate the transaction from bytes
    transactionToExecute.signWithOperator(client);
    transactionToExecute.addSignature(user1Key.getPublicKey(), user1Signature);
    transactionToExecute.addSignature(user2Key.getPublicKey(), user2Signature);
    TransactionResponse result = transactionToExecute.execute(client);
    receipt = result.getReceipt(client);
    System.out.println(receipt.status);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountId(com.hedera.hashgraph.sdk.AccountId) Var(com.google.errorprone.annotations.Var) KeyList(com.hedera.hashgraph.sdk.KeyList) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Example 13 with TransactionResponse

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

the class AbstractNetworkClient method executeTransaction.

public TransactionId executeTransaction(Transaction transaction, KeyList keyList, ExpandedAccountId payer) {
    int numSignatures = 0;
    // set max retries on sdk
    transaction.setMaxAttempts(sdkClient.getAcceptanceTestProperties().getSdkProperties().getMaxAttempts());
    if (payer != null) {
        transaction.setTransactionId(TransactionId.generate(payer.getAccountId()));
        transaction.freezeWith(client);
        transaction.sign(payer.getPrivateKey());
        numSignatures++;
    }
    if (keyList != null) {
        // Signing requires transaction to be frozen
        transaction.freezeWith(client);
        for (Key k : keyList) {
            transaction.sign((PrivateKey) k);
        }
        log.debug("{} additional signatures added to transaction", keyList.size());
        numSignatures += keyList.size();
    }
    TransactionResponse transactionResponse = retryTemplate.execute(x -> executeTransaction(transaction));
    TransactionId transactionId = transactionResponse.transactionId;
    log.debug("Executed transaction {} with {} signatures.", transactionId, numSignatures);
    return transactionId;
}
Also used : NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) Key(com.hedera.hashgraph.sdk.Key) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionId(com.hedera.hashgraph.sdk.TransactionId)

Example 14 with TransactionResponse

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

use of com.hedera.hashgraph.sdk.TransactionResponse 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)

Aggregations

TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)28 Client (com.hedera.hashgraph.sdk.Client)19 Hbar (com.hedera.hashgraph.sdk.Hbar)19 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)17 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)15 AccountId (com.hedera.hashgraph.sdk.AccountId)13 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)13 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)13 Var (com.google.errorprone.annotations.Var)9 KeyList (com.hedera.hashgraph.sdk.KeyList)9 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)6 FileId (com.hedera.hashgraph.sdk.FileId)6 PublicKey (com.hedera.hashgraph.sdk.PublicKey)6 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)4 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)4 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)4 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)4 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)4 TokenAssociateTransaction (com.hedera.hashgraph.sdk.TokenAssociateTransaction)4 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)4