use of com.hedera.hashgraph.sdk.AccountCreateTransaction in project hedera-sdk-java by hashgraph.
the class TokenAssociateIntegrationTest method cannotAssociateAccountWhenAccountDoesNotSignTransaction.
@Test
@DisplayName("Cannot associate account with tokens when account does not sign transaction")
void cannotAssociateAccountWhenAccountDoesNotSignTransaction() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
var key = PrivateKey.generateED25519();
var response = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client);
var accountId = Objects.requireNonNull(response.getReceipt(testEnv.client).accountId);
var tokenId = Objects.requireNonNull(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).getReceipt(testEnv.client).tokenId);
assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).execute(testEnv.client).getReceipt(testEnv.client);
}).withMessageContaining(Status.INVALID_SIGNATURE.toString());
testEnv.close(tokenId, accountId, key);
}
use of com.hedera.hashgraph.sdk.AccountCreateTransaction in project hedera-sdk-java by hashgraph.
the class TokenAssociateIntegrationTest method cannotAssociateAccountWithTokensWhenAccountIDIsNotSet.
@Test
@DisplayName("Cannot associate account with tokens when account ID is not set")
void cannotAssociateAccountWithTokensWhenAccountIDIsNotSet() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
var key = PrivateKey.generateED25519();
var response = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client);
var accountId = Objects.requireNonNull(response.getReceipt(testEnv.client).accountId);
assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
new TokenAssociateTransaction().freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
}).withMessageContaining(Status.INVALID_ACCOUNT_ID.toString());
testEnv.close(accountId, key);
}
use of com.hedera.hashgraph.sdk.AccountCreateTransaction in project hedera-sdk-java by hashgraph.
the class TokenAssociateIntegrationTest method canAssociateAccountWithToken.
@Test
@DisplayName("Can associate account with token")
void canAssociateAccountWithToken() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
var key = PrivateKey.generateED25519();
var response = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client);
var accountId = Objects.requireNonNull(response.getReceipt(testEnv.client).accountId);
var tokenId = Objects.requireNonNull(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).getReceipt(testEnv.client).tokenId);
new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
testEnv.close(tokenId, accountId, key);
}
use of com.hedera.hashgraph.sdk.AccountCreateTransaction in project hedera-sdk-java by hashgraph.
the class TokenBurnIntegrationTest method cannotBurnNftsWhenNftIsNotOwned.
@Test
@DisplayName("Cannot burn NFTs when NFT is not owned by treasury")
void cannotBurnNftsWhenNftIsNotOwned() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
var createReceipt = 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).getReceipt(testEnv.client);
var tokenId = Objects.requireNonNull(createReceipt.tokenId);
var serials = new TokenMintTransaction().setTokenId(tokenId).setMetadata(NftMetadataGenerator.generate((byte) 1)).execute(testEnv.client).getReceipt(testEnv.client).serials;
var key = PrivateKey.generateED25519();
var accountId = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client).getReceipt(testEnv.client).accountId;
new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).signWithOperator(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
new TransferTransaction().addNftTransfer(tokenId.nft(serials.get(0)), testEnv.operatorId, accountId).execute(testEnv.client).getReceipt(testEnv.client);
assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
new TokenBurnTransaction().setSerials(serials).setTokenId(tokenId).execute(testEnv.client).getReceipt(testEnv.client);
}).withMessageContaining(Status.TREASURY_MUST_OWN_BURNED_NFT.toString());
testEnv.close(tokenId, accountId, key);
}
use of com.hedera.hashgraph.sdk.AccountCreateTransaction 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);
}
Aggregations