use of com.hedera.hashgraph.sdk.PublicKey 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);
}
use of com.hedera.hashgraph.sdk.PublicKey in project hedera-sdk-java by hashgraph.
the class DeleteAccountExample method main.
public static void main(String[] args) throws TimeoutException, PrecheckStatusException, 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 a Ed25519 private, public key pair
PrivateKey newKey = PrivateKey.generateED25519();
PublicKey newPublicKey = newKey.getPublicKey();
System.out.println("private key = " + newKey);
System.out.println("public key = " + newPublicKey);
TransactionResponse transactionResponse = new AccountCreateTransaction().setKey(newKey).setInitialBalance(new Hbar(2)).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);
new AccountDeleteTransaction().setTransactionId(TransactionId.generate(newAccountId)).setAccountId(newAccountId).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(newKey).execute(client).getReceipt(client);
}
use of com.hedera.hashgraph.sdk.PublicKey in project hedera-sdk-java by hashgraph.
the class GenerateKeyWithMnemonicExample method main.
public static void main(String[] args) {
Mnemonic mnemonic = Mnemonic.generate24();
PrivateKey privateKey;
try {
privateKey = mnemonic.toPrivateKey();
} catch (BadMnemonicException e) {
throw new Error(e.reason.toString());
}
PublicKey publicKey = privateKey.getPublicKey();
Mnemonic mnemonic12 = Mnemonic.generate12();
PrivateKey privateKey12;
try {
privateKey12 = mnemonic12.toPrivateKey();
} catch (BadMnemonicException e) {
throw new Error(e.reason.toString());
}
PublicKey publicKey12 = privateKey12.getPublicKey();
System.out.println("mnemonic 24 word = " + mnemonic);
System.out.println("private key = " + privateKey);
System.out.println("public key = " + publicKey);
System.out.println("mnemonic 12 word = " + mnemonic12);
System.out.println("private key = " + privateKey12);
System.out.println("public key = " + publicKey12);
}
use of com.hedera.hashgraph.sdk.PublicKey in project hedera-mirror-node by hashgraph.
the class TokenClient method getTokenCreateTransaction.
private TokenCreateTransaction getTokenCreateTransaction(ExpandedAccountId expandedAccountId, String symbol, int freezeStatus, int kycStatus, ExpandedAccountId treasuryAccount, TokenType tokenType, TokenSupplyType tokenSupplyType, long maxSupply, List<CustomFee> customFees) {
String memo = getMemo("Create token");
PublicKey adminKey = expandedAccountId.getPublicKey();
TokenCreateTransaction transaction = new TokenCreateTransaction().setAutoRenewAccountId(expandedAccountId.getAccountId()).setAutoRenewPeriod(Duration.ofSeconds(6_999_999L)).setMaxTransactionFee(sdkClient.getMaxTransactionFee()).setTokenMemo(memo).setTokenName(symbol + "_name").setSupplyType(tokenSupplyType).setTokenSymbol(symbol).setTokenType(tokenType).setTreasuryAccountId(treasuryAccount.getAccountId()).setTransactionMemo(memo);
if (tokenSupplyType == TokenSupplyType.FINITE) {
transaction.setMaxSupply(maxSupply);
}
if (adminKey != null) {
transaction.setAdminKey(adminKey).setPauseKey(adminKey).setSupplyKey(adminKey).setWipeKey(adminKey);
}
if (freezeStatus > 0 && adminKey != null) {
transaction.setFreezeDefault(freezeStatus == TokenFreezeStatus.Frozen_VALUE).setFreezeKey(adminKey);
}
if (kycStatus > 0 && adminKey != null) {
transaction.setKycKey(adminKey);
}
if (customFees != null && adminKey != null) {
transaction.setCustomFees(customFees).setFeeScheduleKey(adminKey);
}
return transaction;
}
use of com.hedera.hashgraph.sdk.PublicKey in project hedera-mirror-node by hashgraph.
the class TopicFeature method createNewTopic.
@Given("I successfully create a new topic id")
public void createNewTopic() {
testInstantReference = Instant.now();
submitKey = PrivateKey.generate();
PublicKey submitPublicKey = submitKey.getPublicKey();
log.trace("Topic creation PrivateKey : {}, PublicKey : {}", submitKey, submitPublicKey);
NetworkTransactionResponse networkTransactionResponse = topicClient.createTopic(topicClient.getSdkClient().getExpandedOperatorAccountId(), submitPublicKey);
assertNotNull(networkTransactionResponse.getReceipt());
TopicId topicId = networkTransactionResponse.getReceipt().topicId;
assertNotNull(topicId);
consensusTopicId = topicId;
topicMessageQuery = new TopicMessageQuery().setTopicId(consensusTopicId).setStartTime(Instant.EPOCH);
log.debug("Set TopicMessageQuery with topic: {}, startTime: {}", consensusTopicId, Instant.EPOCH);
}
Aggregations