Skip to content

Commit

Permalink
fix: Account Alias Mirror Node Queries (#950)
Browse files Browse the repository at this point in the history
* fix: raise an error if the txn is not frozen while signing

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* test: fix integration tests

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* test: fix integration tests

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* fix: use account alias when possible

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* test: add tests

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* fix: use protobuf account num

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* chore: create global timeout for localhost queries

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* chore: address PR comments

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* chore: revert taskfile changes

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

---------

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>
  • Loading branch information
0xivanov authored Jun 6, 2024
1 parent 8bb1063 commit 7701475
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 128 deletions.
11 changes: 4 additions & 7 deletions account_balance_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package hedera
*/

import (
"fmt"
"time"

"github.com/hashgraph/hedera-protobufs-go/services"
Expand Down Expand Up @@ -113,15 +114,11 @@ func (q *AccountBalanceQuery) Execute(client *Client) (AccountBalance, error) {
return AccountBalance{}, err
}

balance := _AccountBalanceFromProtobuf(resp.(*services.Response).GetCryptogetAccountBalance())
protobufResponse := resp.(*services.Response).GetCryptogetAccountBalance()
balance := _AccountBalanceFromProtobuf(protobufResponse)

// accountId value could be either in q.accountID or q.contractID
accountId := q.accountID.String()
if accountId == "" {
accountId = q.contractID.String()
}
err = fetchTokenBalances(fetchMirrorNodeUrlFromClient(client), fmt.Sprint(protobufResponse.GetAccountID().GetAccountNum()), &balance)

err = fetchTokenBalances(fetchMirrorNodeUrlFromClient(client), accountId, &balance)
if err != nil {
return balance, err
}
Expand Down
36 changes: 32 additions & 4 deletions account_balance_query_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package hedera

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -78,9 +77,6 @@ func TestIntegrationAccountBalanceQueryCanGetTokenBalance(t *testing.T) {

tokenID := receipt.TokenID

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

balance, err := NewAccountBalanceQuery().
SetNodeAccountIDs(env.NodeAccountIDs).
SetAccountID(env.Client.GetOperatorAccountID()).
Expand All @@ -89,6 +85,7 @@ func TestIntegrationAccountBalanceQueryCanGetTokenBalance(t *testing.T) {

assert.Equal(t, uint64(1000000), balance.Tokens.Get(*tokenID))
assert.Equal(t, uint64(3), balance.TokenDecimals.Get(*tokenID))

err = CloseIntegrationTestEnv(env, tokenID)
require.NoError(t, err)
}
Expand Down Expand Up @@ -203,3 +200,34 @@ func TestIntegrationAccountBalanceQueryNoAccountIDError(t *testing.T) {
err = CloseIntegrationTestEnv(env, nil)
require.NoError(t, err)
}
func TestIntegrationAccountBalanceQueryWorksWithHollowAccountAlias(t *testing.T) {
t.Parallel()
env := NewIntegrationTestEnv(t)

privateKey, err := PrivateKeyGenerateEcdsa()
require.NoError(t, err)

// Extract the ECDSA public key public key
publicKey := privateKey.PublicKey()
// Extract the Ethereum public address
aliasAccountId := *publicKey.ToAccountID(0, 0)
evmAddress := publicKey.ToEvmAddress()

evmAddressAccount, err := AccountIDFromEvmPublicAddress(evmAddress)
require.NoError(t, err)

// Transfer tokens using the `TransferTransaction` to the Etherum Account Address
tx, err := NewTransferTransaction().AddHbarTransfer(evmAddressAccount, NewHbar(4)).
AddHbarTransfer(env.OperatorID, NewHbar(-4)).Execute(env.Client)
require.NoError(t, err)

// Get the child receipt or child record to return the Hedera Account ID for the new account that was created
_, err = tx.GetReceiptQuery().SetIncludeChildren(true).Execute(env.Client)
require.NoError(t, err)

_, err = NewAccountBalanceQuery().SetAccountID(aliasAccountId).Execute(env.Client)
require.NoError(t, err)

err = CloseIntegrationTestEnv(env, nil)
require.NoError(t, err)
}
13 changes: 0 additions & 13 deletions account_create_transaction_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ package hedera
import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -322,9 +321,6 @@ func TestIntegrationAccountCreateTransactionWithAliasFromAdminKey(t *testing.T)

accountID := *receipt.AccountID

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err := NewAccountInfoQuery().
SetAccountID(accountID).
Execute(env.Client)
Expand Down Expand Up @@ -370,9 +366,6 @@ func TestIntegrationAccountCreateTransactionWithAliasFromAdminKeyWithReceiverSig

accountID := *receipt.AccountID

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err := NewAccountInfoQuery().
SetAccountID(accountID).
Execute(env.Client)
Expand Down Expand Up @@ -454,9 +447,6 @@ func TestIntegrationAccountCreateTransactionWithAlias(t *testing.T) {

accountID := *receipt.AccountID

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err := NewAccountInfoQuery().
SetAccountID(accountID).
Execute(env.Client)
Expand Down Expand Up @@ -538,9 +528,6 @@ func TestIntegrationAccountCreateTransactionWithAliasWithReceiverSigRequired(t *

accountID := *receipt.AccountID

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err := NewAccountInfoQuery().
SetAccountID(accountID).
Execute(env.Client)
Expand Down
7 changes: 5 additions & 2 deletions account_info_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package hedera
*/

import (
"fmt"
"time"

"github.com/hashgraph/hedera-protobufs-go/services"
Expand Down Expand Up @@ -56,12 +57,14 @@ func (q *AccountInfoQuery) Execute(client *Client) (AccountInfo, error) {
return AccountInfo{}, err
}

info, err := _AccountInfoFromProtobuf(resp.GetCryptoGetInfo().AccountInfo)
protobufResponse := resp.GetCryptoGetInfo().AccountInfo
info, err := _AccountInfoFromProtobuf(protobufResponse)
if err != nil {
return AccountInfo{}, err
}

err = fetchAccountInfoTokenRelationships(fetchMirrorNodeUrlFromClient(client), q.accountID.String(), &info)
err = fetchAccountInfoTokenRelationships(fetchMirrorNodeUrlFromClient(client), fmt.Sprint(protobufResponse.AccountID.GetAccountNum()), &info)

if err != nil {
return info, err
}
Expand Down
91 changes: 66 additions & 25 deletions account_info_query_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package hedera

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -54,9 +53,6 @@ func TestIntegrationAccountInfoQueryCanExecute(t *testing.T) {
accountID := *receipt.AccountID
require.NoError(t, err)

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err := NewAccountInfoQuery().
SetAccountID(accountID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
Expand Down Expand Up @@ -113,9 +109,6 @@ func TestIntegrationAccountInfoQueryGetCost(t *testing.T) {
accountID := *receipt.AccountID
require.NoError(t, err)

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

accountInfo := NewAccountInfoQuery().
SetAccountID(accountID).
SetMaxQueryPayment(NewHbar(1)).
Expand Down Expand Up @@ -229,9 +222,6 @@ func TestIntegrationAccountInfoQuerySetBigMaxPayment(t *testing.T) {
accountID := *receipt.AccountID
require.NoError(t, err)

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

accountInfo := NewAccountInfoQuery().
SetAccountID(accountID).
SetMaxQueryPayment(NewHbar(1000000)).
Expand Down Expand Up @@ -399,9 +389,6 @@ func TestIntegrationAccountInfoQueryTokenRelationshipStatuses(t *testing.T) {
_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err := NewAccountInfoQuery().
SetNodeAccountIDs(env.NodeAccountIDs).
SetAccountID(accountID).
Expand Down Expand Up @@ -441,9 +428,6 @@ func TestIntegrationAccountInfoQueryTokenRelationshipStatuses(t *testing.T) {
_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err = NewAccountInfoQuery().
SetNodeAccountIDs(env.NodeAccountIDs).
SetAccountID(accountID).
Expand Down Expand Up @@ -515,9 +499,6 @@ func TestIntegrationAccountInfoQueryTokenRelationship(t *testing.T) {
_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err := NewAccountInfoQuery().
SetNodeAccountIDs(env.NodeAccountIDs).
SetAccountID(accountID).
Expand Down Expand Up @@ -562,9 +543,6 @@ func TestIntegrationAccountInfoQueryTokenRelationship(t *testing.T) {
_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err = NewAccountInfoQuery().
SetNodeAccountIDs(env.NodeAccountIDs).
SetAccountID(accountID).
Expand All @@ -588,9 +566,6 @@ func TestIntegrationAccountInfoQueryTokenRelationship(t *testing.T) {
_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err = NewAccountInfoQuery().
SetNodeAccountIDs(env.NodeAccountIDs).
SetAccountID(accountID).
Expand All @@ -600,3 +575,69 @@ func TestIntegrationAccountInfoQueryTokenRelationship(t *testing.T) {
assert.Equal(t, accountID, info.AccountID)
assert.Equal(t, 0, len(info.TokenRelationships))
}

func TestIntegrationAccountInfoQueryWorksWithHollowAccountAlias(t *testing.T) {
t.Parallel()
env := NewIntegrationTestEnv(t)

// Create NFT
cid := []string{"QmNPCiNA3Dsu3K5FxDPMG5Q3fZRwVTg14EXA92uqEeSRXn"}
// Creating the transaction for token creation
nftCreateTransaction, err := NewTokenCreateTransaction().
SetTokenName("HIP-542 Example Collection").SetTokenSymbol("HIP-542").
SetTokenType(TokenTypeNonFungibleUnique).SetDecimals(0).
SetInitialSupply(0).SetMaxSupply(int64(len(cid))).
SetTreasuryAccountID(env.OperatorID).SetSupplyType(TokenSupplyTypeFinite).
SetAdminKey(env.OperatorKey).SetFreezeKey(env.OperatorKey).SetWipeKey(env.OperatorKey).SetSupplyKey(env.OperatorKey).FreezeWith(env.Client)

// Sign the transaction with the operator key
nftSignTransaction := nftCreateTransaction.Sign(env.OperatorKey)
// Submit the transaction to the Hedera network
nftCreateSubmit, err := nftSignTransaction.Execute(env.Client)
require.NoError(t, err)

// Get transaction receipt information
nftCreateReceipt, err := nftCreateSubmit.GetReceipt(env.Client)
require.NoError(t, err)

// Get token id from the transaction
nftTokenID := *nftCreateReceipt.TokenID

nftCollection := []TransactionReceipt{}

for _, s := range cid {
mintTransaction, err := NewTokenMintTransaction().SetTokenID(nftTokenID).SetMetadata([]byte(s)).FreezeWith(env.Client)
require.NoError(t, err)
mintTransactionSubmit, err := mintTransaction.Sign(env.OperatorKey).Execute(env.Client)
require.NoError(t, err)
receipt, err := mintTransactionSubmit.GetReceipt(env.Client)
require.NoError(t, err)
nftCollection = append(nftCollection, receipt)
}
exampleNftId := nftTokenID.Nft(nftCollection[0].SerialNumbers[0])

privateKey, err := PrivateKeyGenerateEcdsa()
require.NoError(t, err)

// Extract the ECDSA public key public key
publicKey := privateKey.PublicKey()
// Extract the Ethereum public address
aliasAccountId := publicKey.ToAccountID(0, 0)

nftTransferTransaction, err := NewTransferTransaction().AddNftTransfer(exampleNftId, env.OperatorID, *aliasAccountId).FreezeWith(env.Client)
require.NoError(t, err)

// Sign the transaction with the operator key
nftTransferTransactionSign := nftTransferTransaction.Sign(env.OperatorKey)
// Submit the transaction to the Hedera network
nftTransferTransactionSubmit, err := nftTransferTransactionSign.Execute(env.Client)
require.NoError(t, err)
_, err = nftTransferTransactionSubmit.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)

_, err = NewAccountInfoQuery().
SetNodeAccountIDs(env.NodeAccountIDs).
SetAccountID(*aliasAccountId).
Execute(env.Client)
assert.NoError(t, err)
}
6 changes: 0 additions & 6 deletions account_update_transaction_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ func TestIntegrationAccountUpdateTransactionCanExecute(t *testing.T) {
_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err := NewAccountInfoQuery().
SetAccountID(accountID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
Expand Down Expand Up @@ -148,9 +145,6 @@ func TestIntegrationAccountUpdateTransactionNoSigning(t *testing.T) {
_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
require.NoError(t, err)

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err := NewAccountInfoQuery().
SetAccountID(accountID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
Expand Down
4 changes: 0 additions & 4 deletions contract_create_transaction_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package hedera

import (
"encoding/hex"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -127,9 +126,6 @@ func TestIntegrationContractCreateTransactionNoAdminKey(t *testing.T) {
assert.NotNil(t, receipt.ContractID)
contractID := *receipt.ContractID

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

info, err := NewContractInfoQuery().
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetContractID(contractID).
Expand Down
3 changes: 0 additions & 3 deletions contract_id_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ func TestIntegrationContractIDCanPopulateAccountNumber(t *testing.T) {

require.NotNil(t, receipt.ContractID)

// sleep in order for mirror node information to update
time.Sleep(3 * time.Second)

contractID := *receipt.ContractID
info, err := NewContractInfoQuery().SetContractID(contractID).Execute(env.Client)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 7701475

Please sign in to comment.