Search in sources :

Example 6 with KeyList

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

the class TopicWithAdminKeyExample method createTopicWithAdminKey.

private void createTopicWithAdminKey() throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
    // Generate the initial keys that are part of the adminKey's thresholdKey.
    // 3 ED25519 keys part of a 2-of-3 threshold key.
    initialAdminKeys = new PrivateKey[3];
    J8Arrays.setAll(initialAdminKeys, i -> PrivateKey.generate());
    KeyList thresholdKey = KeyList.withThreshold(2);
    Collections.addAll(thresholdKey, initialAdminKeys);
    Transaction<?> transaction = new TopicCreateTransaction().setTopicMemo("demo topic").setAdminKey(thresholdKey).freezeWith(hapiClient);
    // Sign the transaction with 2 of 3 keys that are part of the adminKey threshold key.
    J8Arrays.stream(initialAdminKeys, 0, 2).forEach(k -> {
        System.out.println("Signing ConsensusTopicCreateTransaction with key " + k);
        transaction.sign(k);
    });
    TransactionResponse transactionResponse = transaction.execute(hapiClient);
    topicId = transactionResponse.getReceipt(hapiClient).topicId;
    System.out.println("Created new topic " + topicId + " with 2-of-3 threshold key as adminKey.");
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) KeyList(com.hedera.hashgraph.sdk.KeyList)

Example 7 with KeyList

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

the class MultiSigOfflineExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InvalidProtocolBufferException {
    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();
    System.out.println("private key for user 1 = " + user1Key);
    System.out.println("public key for user 1 = " + user1Key.getPublicKey());
    System.out.println("private key for user 2 = " + user2Key);
    System.out.println("public key for user 2 = " + user2Key.getPublicKey());
    // create a multi-sig account
    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);
    // create a transfer from new account to 0.0.3
    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);
    // convert transaction to bytes to send to signatories
    byte[] transactionBytes = transferTransaction.toBytes();
    Transaction<?> transactionToExecute = Transaction.fromBytes(transactionBytes);
    // ask users to sign and return signature
    byte[] user1Signature = user1Key.signTransaction(Transaction.fromBytes(transactionBytes));
    byte[] user2Signature = user2Key.signTransaction(Transaction.fromBytes(transactionBytes));
    // recreate the transaction from bytes
    transactionToExecute.signWithOperator(client);
    transactionToExecute.addSignature(user1Key.getPublicKey(), user1Signature);
    transactionToExecute.addSignature(user2Key.getPublicKey(), user2Signature);
    TransactionResponse result = transactionToExecute.execute(client);
    receipt = result.getReceipt(client);
    System.out.println(receipt.status);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountId(com.hedera.hashgraph.sdk.AccountId) Var(com.google.errorprone.annotations.Var) KeyList(com.hedera.hashgraph.sdk.KeyList) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Example 8 with KeyList

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

the class ScheduleCreateTransactionSupplier method getPublicKeys.

private KeyList getPublicKeys() {
    KeyList keys = new KeyList();
    getFullSignatoryList().forEach(key -> keys.add(key.getPublicKey()));
    return keys;
}
Also used : KeyList(com.hedera.hashgraph.sdk.KeyList)

Example 9 with KeyList

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

the class ScheduleFeature method createNewCryptoAccountSchedule.

@Given("I schedule a crypto transfer with {int} initial signatures but require an additional signature from " + "{string}")
public void createNewCryptoAccountSchedule(int initSignatureCount, String accountName) {
    // new account, accountName and initSignatureCount
    expectedSignersCount = 2 + initSignatureCount;
    currentSignersCount = initSignatureCount + signatoryCountOffset;
    ExpandedAccountId finalSignatory = accountClient.getAccount(AccountClient.AccountNameEnum.valueOf(accountName));
    KeyList privateKeyList = new KeyList();
    KeyList publicKeyList = new KeyList();
    for (int i = 0; i < initSignatureCount; i++) {
        PrivateKey accountKey = PrivateKey.generate();
        privateKeyList.add(accountKey);
        publicKeyList.add(accountKey.getPublicKey());
    }
    // additional signatory not provided up front to prevent schedule from executing
    publicKeyList.add(finalSignatory.getPublicKey());
    ExpandedAccountId newAccountId = accountClient.createCryptoAccount(Hbar.fromTinybars(DEFAULT_TINY_HBAR), false, publicKeyList, "scheduled transfer");
    scheduledTransaction = accountClient.getCryptoTransferTransaction(newAccountId.getAccountId(), accountClient.getSdkClient().getExpandedOperatorAccountId().getAccountId(), Hbar.fromTinybars(DEFAULT_TINY_HBAR));
    // add sender private key to ensure only Alice's signature is the only signature left that is required
    privateKeyList.add(newAccountId.getPrivateKey());
    createNewSchedule(scheduledTransaction, privateKeyList);
    currentSignersCount++;
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) ExpandedAccountId(com.hedera.mirror.test.e2e.acceptance.props.ExpandedAccountId) KeyList(com.hedera.hashgraph.sdk.KeyList) Given(io.cucumber.java.en.Given)

Example 10 with KeyList

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

the class TokenClient method updateTokenTreasury.

public NetworkTransactionResponse updateTokenTreasury(TokenId tokenId, ExpandedAccountId newTreasuryId) {
    AccountId treasuryAccountId = newTreasuryId.getAccountId();
    String memo = getMemo("Update token");
    TokenUpdateTransaction tokenUpdateTransaction = new TokenUpdateTransaction().setTokenId(tokenId).setTokenMemo(memo).setTreasuryAccountId(treasuryAccountId).setTransactionMemo(memo);
    KeyList keyList = KeyList.of(newTreasuryId.getPrivateKey());
    NetworkTransactionResponse networkTransactionResponse = executeTransactionAndRetrieveReceipt(tokenUpdateTransaction, keyList);
    log.debug("Updated token {} treasury account {}.", tokenId, treasuryAccountId);
    return networkTransactionResponse;
}
Also used : AccountId(com.hedera.hashgraph.sdk.AccountId) ExpandedAccountId(com.hedera.mirror.test.e2e.acceptance.props.ExpandedAccountId) NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) KeyList(com.hedera.hashgraph.sdk.KeyList) TokenUpdateTransaction(com.hedera.hashgraph.sdk.TokenUpdateTransaction)

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