Search in sources :

Example 21 with PublicKey

use of com.hedera.hashgraph.sdk.PublicKey in project hedera-sdk-java by hashgraph.

the class ConsensusPubSubWithSubmitKeyExample method createTopicWithSubmitKey.

/**
 * Generate a brand new ED25519 key pair.
 * <p>
 * Create a new topic with that key as the topic's submitKey; required to sign all future
 * ConsensusMessageSubmitTransactions for that topic.
 */
private void createTopicWithSubmitKey() throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
    // Generate a Ed25519 private, public key pair
    submitKey = PrivateKey.generateED25519();
    PublicKey submitPublicKey = submitKey.getPublicKey();
    TransactionResponse transactionResponse = new TopicCreateTransaction().setTopicMemo("HCS topic with submit key").setSubmitKey(submitPublicKey).execute(client);
    topicId = Objects.requireNonNull(transactionResponse.getReceipt(client).topicId);
    System.out.println("Created new topic " + topicId + " with ED25519 submitKey of " + submitKey);
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) PublicKey(com.hedera.hashgraph.sdk.PublicKey)

Example 22 with PublicKey

use of com.hedera.hashgraph.sdk.PublicKey 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);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Client(com.hedera.hashgraph.sdk.Client) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Example 23 with PublicKey

use of com.hedera.hashgraph.sdk.PublicKey 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);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) KeyList(com.hedera.hashgraph.sdk.KeyList) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Key(com.hedera.hashgraph.sdk.Key) PublicKey(com.hedera.hashgraph.sdk.PublicKey) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey)

Example 24 with PublicKey

use of com.hedera.hashgraph.sdk.PublicKey in project hedera-sdk-java by hashgraph.

the class AccountAliasExample 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);
    /*
         * Hedera supports a form of lazy account creation.
         *
         * You can "create" an account by generating a private key, and then deriving the public key,
         * without any need to interact with the Hedera network.  The public key more or less acts as the user's
         * account ID.  This public key is an account's aliasKey: a public key that aliases (or will eventually alias)
         * to a Hedera account.
         *
         * An AccountId takes one of two forms: a normal AccountId with a null aliasKey member takes the form 0.0.123,
         * while an account ID with a non-null aliasKey member takes the form
         * 0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777
         * Note the prefix of "0.0." indicating the shard and realm.  Also note that the aliasKey is stringified
         * as a hex-encoded ASN1 DER representation of the key.
         *
         * An AccountId with an aliasKey can be used just like a normal AccountId for the purposes of queries and
         * transactions, however most queries and transactions involving such an AccountId won't work until Hbar has
         * been transferred to the aliasKey account.
         *
         * There is no record in the Hedera network of an account associated with a given aliasKey
         * until an amount of Hbar is transferred to the account.  The moment that Hbar is transferred to that aliasKey
         * AccountId is the moment that that account actually begins to exist in the Hedera ledger.
         */
    System.out.println("\"Creating\" a new account");
    PrivateKey privateKey = PrivateKey.generateED25519();
    PublicKey publicKey = privateKey.getPublicKey();
    // Assuming that the target shard and realm are known.
    // For now they are virtually always 0 and 0.
    AccountId aliasAccountId = publicKey.toAccountId(0, 0);
    System.out.println("New account ID: " + aliasAccountId);
    System.out.println("Just the aliasKey: " + aliasAccountId.aliasKey);
    /*
         * Note that no queries or transactions have taken place yet.
         * This account "creation" process is entirely local.
         *
         * AccountId.fromString() can construct an AccountId with an aliasKey.
         * It expects a string of the form 0.0.123 in the case of a normal AccountId, or of the form
         * 0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777
         * in the case of an AccountId with aliasKey.  Note the prefix of "0.0." to indicate the shard and realm.
         *
         * If the shard and realm are known, you may use PublicKey.fromString().toAccountId() to construct the
         * aliasKey AccountId
         */
    AccountId fromString = AccountId.fromString("0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777");
    AccountId fromKeyString = PublicKey.fromString("302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777").toAccountId(0, 0);
    System.out.println("Transferring some Hbar to the new account");
    new TransferTransaction().addHbarTransfer(OPERATOR_ID, new Hbar(10).negated()).addHbarTransfer(aliasAccountId, new Hbar(10)).execute(client).getReceipt(client);
    AccountBalance balance = new AccountBalanceQuery().setAccountId(aliasAccountId).execute(client);
    System.out.println("Balances of the new account: " + balance);
    AccountInfo info = new AccountInfoQuery().setAccountId(aliasAccountId).execute(client);
    System.out.println("Info about the new account: " + info);
    /*
         * Note that once an account exists in the ledger, it is assigned a normal AccountId, which can be retrieved
         * via an AccountInfoQuery.
         *
         * Users may continue to refer to the account by its aliasKey AccountId, but they may also
         * now refer to it by its normal AccountId
         */
    System.out.println("The normal account ID: " + info.accountId);
    System.out.println("The alias key: " + info.aliasKey);
    System.out.println("Example complete!");
    client.close();
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) AccountBalance(com.hedera.hashgraph.sdk.AccountBalance) AccountInfoQuery(com.hedera.hashgraph.sdk.AccountInfoQuery) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountInfo(com.hedera.hashgraph.sdk.AccountInfo)

Example 25 with PublicKey

use of com.hedera.hashgraph.sdk.PublicKey in project hedera-sdk-java by hashgraph.

the class GenerateKeyExample method main.

public static void main(String[] args) {
    PrivateKey privateKey = PrivateKey.generateED25519();
    PublicKey publicKey = privateKey.getPublicKey();
    System.out.println("private = " + privateKey);
    System.out.println("public = " + publicKey);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) PublicKey(com.hedera.hashgraph.sdk.PublicKey)

Aggregations

PublicKey (com.hedera.hashgraph.sdk.PublicKey)25 AccountId (com.hedera.hashgraph.sdk.AccountId)9 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)9 AbstractTransactionSupplierTest (com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)8 Test (org.junit.jupiter.api.Test)8 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)7 Client (com.hedera.hashgraph.sdk.Client)6 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)6 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)6 Hbar (com.hedera.hashgraph.sdk.Hbar)4 KeyList (com.hedera.hashgraph.sdk.KeyList)4 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)4 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)4 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)3 TokenUpdateTransaction (com.hedera.hashgraph.sdk.TokenUpdateTransaction)3 TopicCreateTransaction (com.hedera.hashgraph.sdk.TopicCreateTransaction)3 NetworkTransactionResponse (com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse)3 AccountBalance (com.hedera.hashgraph.sdk.AccountBalance)2 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)2 ScheduleCreateTransaction (com.hedera.hashgraph.sdk.ScheduleCreateTransaction)2