use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.
the class UpdateAccountPublicKeyExample 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);
client.setDefaultMaxTransactionFee(new Hbar(10));
// First, we create a new account so we don't affect our account
PrivateKey key1 = PrivateKey.generateED25519();
PrivateKey key2 = PrivateKey.generateED25519();
TransactionResponse acctTransactionResponse = new AccountCreateTransaction().setKey(key1.getPublicKey()).setInitialBalance(new Hbar(1)).execute(client);
System.out.println("transaction ID: " + acctTransactionResponse);
AccountId accountId = Objects.requireNonNull(acctTransactionResponse.getReceipt(client).accountId);
System.out.println("account = " + accountId);
System.out.println("key = " + key1.getPublicKey());
// Next, we update the key
System.out.println(" :: update public key of account " + accountId);
System.out.println("set key = " + key2.getPublicKey());
TransactionResponse accountUpdateTransactionResponse = new AccountUpdateTransaction().setAccountId(accountId).setKey(key2.getPublicKey()).freezeWith(client).sign(key1).sign(key2).execute(client);
System.out.println("transaction ID: " + accountUpdateTransactionResponse);
// (important!) wait for the transaction to complete by querying the receipt
accountUpdateTransactionResponse.getReceipt(client);
// Now we fetch the account information to check if the key was changed
System.out.println(" :: getAccount and check our current key");
AccountInfo info = new AccountInfoQuery().setAccountId(accountId).execute(client);
System.out.println("key = " + info.key);
}
use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.
the class TokenNftTransferIntegrationTest method cannotTransferUnownedNfts.
@Test
@DisplayName("Cannot transfer NFTs you don't own")
void cannotTransferUnownedNfts() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
var key = PrivateKey.generateED25519();
@Var TransactionResponse response = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client);
var accountId = response.getReceipt(testEnv.client).accountId;
assertThat(accountId).isNotNull();
response = new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setTokenType(TokenType.NON_FUNGIBLE_UNIQUE).setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).setFreezeKey(testEnv.operatorKey).setWipeKey(testEnv.operatorKey).setSupplyKey(testEnv.operatorKey).setFreezeDefault(false).execute(testEnv.client);
var tokenId = response.getReceipt(testEnv.client).tokenId;
assertThat(tokenId).isNotNull();
var mintReceipt = new TokenMintTransaction().setTokenId(tokenId).setMetadata(NftMetadataGenerator.generate((byte) 10)).execute(testEnv.client).getReceipt(testEnv.client);
new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).signWithOperator(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
var serialsToTransfer = new ArrayList<Long>(mintReceipt.serials.subList(0, 4));
var transfer = new TransferTransaction();
for (var serial : serialsToTransfer) {
// Try to transfer in wrong direction
transfer.addNftTransfer(tokenId.nft(serial), accountId, testEnv.operatorId);
}
transfer.freezeWith(testEnv.client).sign(key);
assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
transfer.execute(testEnv.client).getReceipt(testEnv.client);
}).withMessageContaining(Status.SENDER_DOES_NOT_OWN_NFT_SERIAL_NO.toString());
testEnv.close(tokenId, accountId, key);
}
use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.
the class TokenTransferIntegrationTest method tokenTransferTest.
@Test
@DisplayName("Can transfer tokens")
void tokenTransferTest() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
var key = PrivateKey.generateED25519();
@Var TransactionResponse response = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client);
var accountId = response.getReceipt(testEnv.client).accountId;
assertThat(accountId).isNotNull();
response = new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setDecimals(3).setInitialSupply(1000000).setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).setFreezeKey(testEnv.operatorKey).setWipeKey(testEnv.operatorKey).setKycKey(testEnv.operatorKey).setSupplyKey(testEnv.operatorKey).setFreezeDefault(false).execute(testEnv.client);
var tokenId = response.getReceipt(testEnv.client).tokenId;
assertThat(tokenId).isNotNull();
new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).signWithOperator(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
new TokenGrantKycTransaction().setAccountId(accountId).setTokenId(tokenId).execute(testEnv.client).getReceipt(testEnv.client);
new TransferTransaction().addTokenTransfer(tokenId, testEnv.operatorId, -10).addTokenTransfer(tokenId, accountId, 10).execute(testEnv.client).getReceipt(testEnv.client);
testEnv.close(tokenId, accountId, key);
}
use of com.hedera.hashgraph.sdk.TransactionResponse 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);
}
use of com.hedera.hashgraph.sdk.TransactionResponse 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);
}
Aggregations