Search in sources :

Example 11 with KeyList

use of com.hedera.hashgraph.sdk.KeyList in project hedera-mirror-node by hashgraph.

the class AccountClient method createCryptoAccount.

public ExpandedAccountId createCryptoAccount(Hbar initialBalance, boolean receiverSigRequired, KeyList keyList, String memo) {
    // 1. Generate a Ed25519 private, public key pair
    PrivateKey privateKey = PrivateKey.generate();
    PublicKey publicKey = privateKey.getPublicKey();
    log.trace("Private key = {}", privateKey);
    log.trace("Public key = {}", publicKey);
    KeyList publicKeyList = KeyList.of(privateKey.getPublicKey());
    if (keyList != null) {
        publicKeyList.addAll(keyList);
    }
    AccountCreateTransaction accountCreateTransaction = getAccountCreateTransaction(initialBalance, publicKeyList, receiverSigRequired, memo == null ? "" : memo);
    NetworkTransactionResponse networkTransactionResponse = executeTransactionAndRetrieveReceipt(accountCreateTransaction, receiverSigRequired ? KeyList.of(privateKey) : null);
    TransactionReceipt receipt = networkTransactionResponse.getReceipt();
    AccountId newAccountId = receipt.accountId;
    // verify accountId
    if (receipt.accountId == null) {
        throw new NetworkException(String.format("Receipt for %s returned no accountId, receipt: %s", networkTransactionResponse.getTransactionId(), receipt));
    }
    log.debug("Created new account {}, receiverSigRequired: {}", newAccountId, receiverSigRequired);
    return new ExpandedAccountId(newAccountId, privateKey, privateKey.getPublicKey());
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) AccountId(com.hedera.hashgraph.sdk.AccountId) ExpandedAccountId(com.hedera.mirror.test.e2e.acceptance.props.ExpandedAccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) KeyList(com.hedera.hashgraph.sdk.KeyList) ExpandedAccountId(com.hedera.mirror.test.e2e.acceptance.props.ExpandedAccountId) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Example 12 with KeyList

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

the class ScheduleCreateIntegrationTest method canSignSchedule2.

@Test
@DisplayName("Can sign schedule")
void canSignSchedule2() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    PrivateKey key1 = PrivateKey.generateED25519();
    PrivateKey key2 = PrivateKey.generateED25519();
    PrivateKey key3 = PrivateKey.generateED25519();
    KeyList keyList = new KeyList();
    keyList.add(key1.getPublicKey());
    keyList.add(key2.getPublicKey());
    keyList.add(key3.getPublicKey());
    // Creat the account with the `KeyList`
    TransactionResponse response = new AccountCreateTransaction().setKey(keyList).setInitialBalance(new Hbar(10)).execute(testEnv.client);
    // This will wait for the receipt to become available
    @Var TransactionReceipt receipt = response.getReceipt(testEnv.client);
    AccountId accountId = Objects.requireNonNull(receipt.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(testEnv.operatorId);
    // Create a transfer transaction with 2/3 signatures.
    TransferTransaction transfer = new TransferTransaction().setTransactionId(transactionId).addHbarTransfer(accountId, new Hbar(1).negated()).addHbarTransfer(testEnv.operatorId, new Hbar(1));
    // Schedule the transactoin
    ScheduleCreateTransaction scheduled = transfer.schedule();
    receipt = scheduled.execute(testEnv.client).getReceipt(testEnv.client);
    // Get the schedule ID from the receipt
    ScheduleId scheduleId = Objects.requireNonNull(receipt.scheduleId);
    // Get the schedule info to see if `signatories` is populated with 2/3 signatures
    @Var ScheduleInfo info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info.executedAt).isNull();
    // Finally send this last signature to Hedera. This last signature _should_ mean the transaction executes
    // since all 3 signatures have been provided.
    ScheduleSignTransaction signTransaction = new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(testEnv.client);
    signTransaction.sign(key1).sign(key2).sign(key3).execute(testEnv.client).getReceipt(testEnv.client);
    info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info.executedAt).isNotNull();
    new AccountDeleteTransaction().setAccountId(accountId).setTransferAccountId(testEnv.operatorId).freezeWith(testEnv.client).sign(key1).sign(key2).sign(key3).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) 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) ScheduleInfo(com.hedera.hashgraph.sdk.ScheduleInfo) TransactionId(com.hedera.hashgraph.sdk.TransactionId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 13 with KeyList

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

the class ScheduledTransactionMultiSigThresholdExample method main.

// public static void main(String[] args) throws PrecheckStatusException, IOException, TimeoutException, ReceiptStatusException {
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 four new Ed25519 private, public key pairs.
    PrivateKey[] privateKeys = new PrivateKey[4];
    PublicKey[] publicKeys = new PublicKey[4];
    for (int i = 0; i < 4; i++) {
        PrivateKey key = PrivateKey.generateED25519();
        privateKeys[i] = key;
        publicKeys[i] = key.getPublicKey();
        System.out.println("public key " + (i + 1) + ": " + publicKeys[i]);
        System.out.println("private key " + (i + 1) + ": " + privateKeys[i]);
    }
    // require 3 of the 4 keys we generated to sign on anything modifying this account
    KeyList transactionKey = KeyList.withThreshold(3);
    Collections.addAll(transactionKey, publicKeys);
    TransactionResponse transactionResponse = new AccountCreateTransaction().setKey(transactionKey).setInitialBalance(Hbar.fromTinybars(1)).setAccountMemo("3-of-4 multi-sig account").execute(client);
    // This will wait for the receipt to become available
    TransactionReceipt txAccountCreateReceipt = transactionResponse.getReceipt(client);
    AccountId multiSigAccountId = Objects.requireNonNull(txAccountCreateReceipt.accountId);
    System.out.println("3-of-4 multi-sig account ID: " + multiSigAccountId);
    AccountBalance balance = new AccountBalanceQuery().setAccountId(multiSigAccountId).execute(client);
    System.out.println("Balance of account " + multiSigAccountId + ": " + balance.hbars.toTinybars() + " tinybar.");
    // schedule crypto transfer from multi-sig account to operator account
    TransactionResponse transferToSchedule = new TransferTransaction().addHbarTransfer(multiSigAccountId, Hbar.fromTinybars(-1)).addHbarTransfer(Objects.requireNonNull(client.getOperatorAccountId()), Hbar.fromTinybars(1)).schedule().freezeWith(client).sign(// add 1 signature`
    privateKeys[0]).execute(client);
    TransactionReceipt txScheduleReceipt = transferToSchedule.getReceipt(client);
    System.out.println("Schedule status: " + txScheduleReceipt.status);
    ScheduleId scheduleId = Objects.requireNonNull(txScheduleReceipt.scheduleId);
    System.out.println("Schedule ID: " + scheduleId);
    TransactionId scheduledTxId = Objects.requireNonNull(txScheduleReceipt.scheduledTransactionId);
    System.out.println("Scheduled tx ID: " + scheduledTxId);
    // add 2 signature
    TransactionResponse txScheduleSign1 = new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(client).sign(privateKeys[1]).execute(client);
    TransactionReceipt txScheduleSign1Receipt = txScheduleSign1.getReceipt(client);
    System.out.println("1. ScheduleSignTransaction status: " + txScheduleSign1Receipt.status);
    // add 3 signature
    TransactionResponse txScheduleSign2 = new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(client).sign(privateKeys[2]).execute(client);
    TransactionReceipt txScheduleSign2Receipt = txScheduleSign2.getReceipt(client);
    System.out.println("2. ScheduleSignTransaction status: " + txScheduleSign2Receipt.status);
    // query schedule
    ScheduleInfo scheduleInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
    System.out.println(scheduleInfo);
    // query triggered scheduled tx
    TransactionRecord recordScheduledTx = new TransactionRecordQuery().setTransactionId(scheduledTxId).execute(client);
    System.out.println(recordScheduledTx);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) KeyList(com.hedera.hashgraph.sdk.KeyList) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) ScheduleId(com.hedera.hashgraph.sdk.ScheduleId) ScheduleInfo(com.hedera.hashgraph.sdk.ScheduleInfo) TransactionId(com.hedera.hashgraph.sdk.TransactionId) TransactionRecordQuery(com.hedera.hashgraph.sdk.TransactionRecordQuery) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountBalance(com.hedera.hashgraph.sdk.AccountBalance) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) TransactionRecord(com.hedera.hashgraph.sdk.TransactionRecord)

Example 14 with KeyList

use of com.hedera.hashgraph.sdk.KeyList 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)

Aggregations

KeyList (com.hedera.hashgraph.sdk.KeyList)14 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)10 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)9 AccountId (com.hedera.hashgraph.sdk.AccountId)9 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)9 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)8 Hbar (com.hedera.hashgraph.sdk.Hbar)7 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)7 Var (com.google.errorprone.annotations.Var)6 Client (com.hedera.hashgraph.sdk.Client)6 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)5 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)5 PublicKey (com.hedera.hashgraph.sdk.PublicKey)4 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)4 ScheduleCreateTransaction (com.hedera.hashgraph.sdk.ScheduleCreateTransaction)3 ScheduleInfo (com.hedera.hashgraph.sdk.ScheduleInfo)3 TransactionId (com.hedera.hashgraph.sdk.TransactionId)3 ExpandedAccountId (com.hedera.mirror.test.e2e.acceptance.props.ExpandedAccountId)3 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)2 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)2