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.");
}
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);
}
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;
}
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++;
}
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;
}
Aggregations