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;
}
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);
}
Aggregations