Search in sources :

Example 1 with PublicKey

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);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) Var(com.google.errorprone.annotations.Var) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) KeyList(com.hedera.hashgraph.sdk.KeyList) AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) ScheduleId(com.hedera.hashgraph.sdk.ScheduleId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransactionReceiptQuery(com.hedera.hashgraph.sdk.TransactionReceiptQuery) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction)

Example 2 with PublicKey

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);
}
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) AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Example 3 with PublicKey

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);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) PublicKey(com.hedera.hashgraph.sdk.PublicKey) Mnemonic(com.hedera.hashgraph.sdk.Mnemonic) BadMnemonicException(com.hedera.hashgraph.sdk.BadMnemonicException)

Example 4 with PublicKey

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;
}
Also used : PublicKey(com.hedera.hashgraph.sdk.PublicKey) TokenCreateTransaction(com.hedera.hashgraph.sdk.TokenCreateTransaction)

Example 5 with PublicKey

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);
}
Also used : NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) PublicKey(com.hedera.hashgraph.sdk.PublicKey) TopicId(com.hedera.hashgraph.sdk.TopicId) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) Given(io.cucumber.java.en.Given)

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