Search in sources :

Example 1 with Key

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

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

the class CreateAccountThresholdKeyExample 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);
    // Generate three new Ed25519 private, public key pairs.
    // You do not need the private keys to create the Threshold Key List,
    // you only need the public keys, and if you're doing things correctly,
    // you probably shouldn't have these private keys.
    PrivateKey[] privateKeys = new PrivateKey[3];
    PublicKey[] publicKeys = new PublicKey[3];
    for (int i = 0; i < 3; i++) {
        PrivateKey key = PrivateKey.generateED25519();
        privateKeys[i] = key;
        publicKeys[i] = key.getPublicKey();
    }
    System.out.println("public keys: ");
    for (Key key : publicKeys) {
        System.out.println(key);
    }
    // require 2 of the 3 keys we generated to sign on anything modifying this account
    KeyList transactionKey = KeyList.withThreshold(2);
    Collections.addAll(transactionKey, publicKeys);
    TransactionResponse transactionResponse = new AccountCreateTransaction().setKey(transactionKey).setInitialBalance(new Hbar(10)).execute(client);
    // This will wait for the receipt to become available
    TransactionReceipt receipt = transactionResponse.getReceipt(client);
    AccountId newAccountId = Objects.requireNonNull(receipt.accountId);
    System.out.println("account = " + newAccountId);
    TransactionResponse transferTransactionResponse = new TransferTransaction().addHbarTransfer(newAccountId, new Hbar(10).negated()).addHbarTransfer(new AccountId(3), new Hbar(10)).freezeWith(client).sign(privateKeys[0]).sign(privateKeys[1]).execute(client);
    // (important!) wait for the transfer to go to consensus
    transferTransactionResponse.getReceipt(client);
    Hbar balanceAfter = new AccountBalanceQuery().setAccountId(newAccountId).execute(client).hbars;
    System.out.println("account balance after transfer: " + balanceAfter);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) KeyList(com.hedera.hashgraph.sdk.KeyList) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Key(com.hedera.hashgraph.sdk.Key) PublicKey(com.hedera.hashgraph.sdk.PublicKey) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey)

Aggregations

Key (com.hedera.hashgraph.sdk.Key)2 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)2 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)2 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)1 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)1 AccountId (com.hedera.hashgraph.sdk.AccountId)1 Client (com.hedera.hashgraph.sdk.Client)1 Hbar (com.hedera.hashgraph.sdk.Hbar)1 KeyList (com.hedera.hashgraph.sdk.KeyList)1 PublicKey (com.hedera.hashgraph.sdk.PublicKey)1 TransactionId (com.hedera.hashgraph.sdk.TransactionId)1 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)1 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)1 NetworkTransactionResponse (com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse)1