use of com.hedera.hashgraph.sdk.AccountCreateTransaction 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.AccountCreateTransaction 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.AccountCreateTransaction 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.AccountCreateTransaction in project hedera-sdk-java by hashgraph.
the class TransferTokensExample 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);
// Generate a Ed25519 private, public key pair
PrivateKey key1 = PrivateKey.generateED25519();
PrivateKey key2 = PrivateKey.generateED25519();
System.out.println("private key = " + key1);
System.out.println("public key = " + key1.getPublicKey());
System.out.println("private key = " + key2);
System.out.println("public key = " + key2.getPublicKey());
@Var TransactionResponse response = new AccountCreateTransaction().setKey(key1.getPublicKey()).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);
// This will wait for the receipt to become available
@Var TransactionReceipt receipt = response.getReceipt(client);
AccountId accountId1 = Objects.requireNonNull(receipt.accountId);
System.out.println("accountId1 = " + accountId1);
response = new AccountCreateTransaction().setKey(key2.getPublicKey()).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);
// This will wait for the receipt to become available
receipt = response.getReceipt(client);
AccountId accountId2 = Objects.requireNonNull(receipt.accountId);
System.out.println("accountId2 = " + accountId1);
response = new TokenCreateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenName("ffff").setTokenSymbol("F").setDecimals(3).setInitialSupply(1000000).setTreasuryAccountId(OPERATOR_ID).setAdminKey(OPERATOR_KEY.getPublicKey()).setFreezeKey(OPERATOR_KEY.getPublicKey()).setWipeKey(OPERATOR_KEY.getPublicKey()).setKycKey(OPERATOR_KEY.getPublicKey()).setSupplyKey(OPERATOR_KEY.getPublicKey()).setFreezeDefault(false).execute(client);
TokenId tokenId = Objects.requireNonNull(response.getReceipt(client).tokenId);
System.out.println("token = " + tokenId);
new TokenAssociateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId1).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(OPERATOR_KEY).sign(key1).execute(client).getReceipt(client);
System.out.println("Associated account " + accountId1 + " with token " + tokenId);
new TokenAssociateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId2).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(OPERATOR_KEY).sign(key2).execute(client).getReceipt(client);
System.out.println("Associated account " + accountId2 + " with token " + tokenId);
new TokenGrantKycTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId1).setTokenId(tokenId).execute(client).getReceipt(client);
System.out.println("Granted KYC for account " + accountId1 + " on token " + tokenId);
new TokenGrantKycTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId2).setTokenId(tokenId).execute(client).getReceipt(client);
System.out.println("Granted KYC for account " + accountId2 + " on token " + tokenId);
new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, OPERATOR_ID, -10).addTokenTransfer(tokenId, accountId1, 10).execute(client).getReceipt(client);
System.out.println("Sent 10 tokens from account " + OPERATOR_ID + " to account " + accountId1 + " on token " + tokenId);
new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, accountId1, -10).addTokenTransfer(tokenId, accountId2, 10).freezeWith(client).sign(key1).execute(client).getReceipt(client);
System.out.println("Sent 10 tokens from account " + accountId1 + " to account " + accountId2 + " on token " + tokenId);
new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, accountId2, -10).addTokenTransfer(tokenId, accountId1, 10).freezeWith(client).sign(key2).execute(client).getReceipt(client);
System.out.println("Sent 10 tokens from account " + accountId2 + " to account " + accountId1 + " on token " + tokenId);
new TokenWipeTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenId(tokenId).setAccountId(accountId1).setAmount(10).execute(client).getReceipt(client);
System.out.println("Wiped balance of account " + accountId1);
new TokenDeleteTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenId(tokenId).execute(client).getReceipt(client);
System.out.println("Deleted token " + tokenId);
new AccountDeleteTransaction().setAccountId(accountId1).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(OPERATOR_KEY).sign(key1).execute(client).getReceipt(client);
System.out.println("Deleted accountId1 " + accountId1);
new AccountDeleteTransaction().setAccountId(accountId2).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(OPERATOR_KEY).sign(key2).execute(client).getReceipt(client);
System.out.println("Deleted accountId2" + accountId2);
}
use of com.hedera.hashgraph.sdk.AccountCreateTransaction in project hedera-sdk-java by hashgraph.
the class AccountRecordsIntegrationTest method canQueryAccountRecords.
@Test
@DisplayName("Can query account records")
void canQueryAccountRecords() throws Exception {
var testEnv = new IntegrationTestEnv(1);
var key = PrivateKey.generateED25519();
var response = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client);
var accountId = Objects.requireNonNull(response.getReceipt(testEnv.client).accountId);
new TransferTransaction().addHbarTransfer(testEnv.operatorId, new Hbar(1).negated()).addHbarTransfer(accountId, new Hbar(1)).execute(testEnv.client).getReceipt(testEnv.client);
new TransferTransaction().addHbarTransfer(testEnv.operatorId, new Hbar(1)).addHbarTransfer(accountId, new Hbar(1).negated()).freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
var records = new AccountRecordsQuery().setAccountId(testEnv.operatorId).execute(testEnv.client);
assertThat(records.isEmpty()).isFalse();
testEnv.close(accountId, key);
}
Aggregations