use of com.hedera.hashgraph.sdk.KeyList 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();
}
use of com.hedera.hashgraph.sdk.KeyList 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.KeyList 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);
}
use of com.hedera.hashgraph.sdk.KeyList in project hedera-sdk-java by hashgraph.
the class SignTransactionExample 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);
PrivateKey user1Key = PrivateKey.generateED25519();
PrivateKey user2Key = PrivateKey.generateED25519();
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);
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);
transferTransaction.signWithOperator(client);
user1Key.signTransaction(transferTransaction);
user2Key.signTransaction(transferTransaction);
TransactionResponse result = transferTransaction.execute(client);
receipt = result.getReceipt(client);
System.out.println(receipt.status);
}
use of com.hedera.hashgraph.sdk.KeyList in project hedera-sdk-java by hashgraph.
the class TopicWithAdminKeyExample method updateTopicAdminKeyAndMemo.
private void updateTopicAdminKeyAndMemo() throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
// Generate the new keys that are part of the adminKey's thresholdKey.
// 4 ED25519 keys part of a 3-of-4 threshold key.
PrivateKey[] newAdminKeys = new PrivateKey[4];
J8Arrays.setAll(newAdminKeys, i -> PrivateKey.generate());
KeyList thresholdKey = KeyList.withThreshold(3);
Collections.addAll(thresholdKey, newAdminKeys);
Transaction<?> transaction = new TopicUpdateTransaction().setTopicId(topicId).setTopicMemo("updated demo topic").setAdminKey(thresholdKey).freezeWith(hapiClient);
// Sign with the initial adminKey. 2 of the 3 keys already part of the topic's adminKey.
J8Arrays.stream(initialAdminKeys, 0, 2).forEach(k -> {
System.out.println("Signing ConsensusTopicUpdateTransaction with initial admin key " + k);
transaction.sign(k);
});
// Sign with the new adminKey. 3 of 4 keys already part of the topic's adminKey.
J8Arrays.stream(newAdminKeys, 0, 3).forEach(k -> {
System.out.println("Signing ConsensusTopicUpdateTransaction with new admin key " + k);
transaction.sign(k);
});
TransactionResponse transactionResponse = transaction.execute(hapiClient);
// Retrieve results post-consensus.
transactionResponse.getReceipt(hapiClient);
System.out.println("Updated topic " + topicId + " with 3-of-4 threshold key as adminKey");
TopicInfo topicInfo = new TopicInfoQuery().setTopicId(topicId).execute(hapiClient);
System.out.println(topicInfo);
}
Aggregations