use of com.hedera.hashgraph.sdk.AccountCreateTransaction in project hedera-sdk-java by hashgraph.
the class CreateAccountExample 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(newPublicKey).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);
// This will wait for the receipt to become available
TransactionReceipt receipt = transactionResponse.getReceipt(client);
AccountId newAccountId = receipt.accountId;
System.out.println("account = " + newAccountId);
}
use of com.hedera.hashgraph.sdk.AccountCreateTransaction 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);
}
use of com.hedera.hashgraph.sdk.AccountCreateTransaction in project hedera-sdk-java by hashgraph.
the class MultiAppTransferExample method main.
public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException, InvalidProtocolBufferException {
// the exchange creates an account for the user to transfer funds to
AccountId exchangeAccountId = new AccountCreateTransaction().setReceiverSignatureRequired(true).setKey(exchangeKey).freezeWith(client).sign(exchangeKey).execute(client).getReceipt(client).accountId;
assert exchangeAccountId != null;
// for the purpose of this example we create an account for
// the user with a balance of 5 h
AccountId userAccountId = new AccountCreateTransaction().setInitialBalance(new Hbar(5)).setKey(userKey).execute(client).getReceipt(client).accountId;
assert userAccountId != null;
// next we make a transfer from the user account to the
// exchange account, this requires signing by both parties
TransferTransaction transferTxn = new TransferTransaction().addHbarTransfer(userAccountId, new Hbar(2).negated()).addHbarTransfer(exchangeAccountId, new Hbar(2)).setTransactionMemo("https://some-exchange.com/user1/account1").freezeWith(client).sign(userKey);
// the exchange must sign the transaction in order for it to be accepted by the network
// assume this is some REST call to the exchange API server
byte[] signedTxnBytes = exchangeSignsTransaction(transferTxn.toBytes());
// parse the transaction bytes returned from the exchange
Transaction<?> signedTransferTxn = Transaction.fromBytes(signedTxnBytes);
// get the amount we are about to transfer
// we built this with +2, -2
Hbar transferAmount = ((TransferTransaction) signedTransferTxn).getHbarTransfers().values().toArray(new Hbar[0])[0];
System.out.println("about to transfer " + transferAmount + "...");
// we now execute the signed transaction and wait for it to be accepted
TransactionResponse transactionResponse = signedTransferTxn.execute(client);
// (important!) wait for consensus by querying for the receipt
transactionResponse.getReceipt(client);
Hbar senderBalanceAfter = new AccountBalanceQuery().setAccountId(userAccountId).execute(client).hbars;
Hbar receiptBalanceAfter = new AccountBalanceQuery().setAccountId(exchangeAccountId).execute(client).hbars;
System.out.println("" + userAccountId + " balance = " + senderBalanceAfter);
System.out.println("" + exchangeAccountId + " balance = " + receiptBalanceAfter);
}
Aggregations