Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update main #824

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v2.32.0

### Added

* `PopulateAccount` to `AccountID`

## v2.31.0

### Added
Expand Down
67 changes: 58 additions & 9 deletions account_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,32 +337,60 @@ func AccountIDFromBytes(data []byte) (AccountID, error) {
return *_AccountIDFromProtobuf(&pb), nil
}

// PopulateAccount gets the actual `Account` field of the `AccountId` from the Mirror Node.
// Should be used after generating `AccountId.FromEvmAddress()` because it sets the `Account` field to `0`
// automatically since there is no connection between the `Account` and the `evmAddress`
func (id *AccountID) PopulateAccount(client *Client) error {
type PopulateType int

const (
Account PopulateType = iota
EvmAddress
)

func (id *AccountID) _MirrorNodeRequest(client *Client, populateType string) (map[string]interface{}, error) {
if client.mirrorNetwork == nil || len(client.GetMirrorNetwork()) == 0 {
return errors.New("mirror node is not set")
return nil, errors.New("mirror node is not set")
}

mirrorUrl := client.GetMirrorNetwork()[0]
index := strings.Index(mirrorUrl, ":")
if index == -1 {
return errors.New("invalid mirrorUrl format")
return nil, errors.New("invalid mirrorUrl format")
}
mirrorUrl = mirrorUrl[:index]
url := fmt.Sprintf("https://%s/api/v1/accounts/%s", mirrorUrl, hex.EncodeToString(*id.AliasEvmAddress))

var url string
protocol := "https"
port := ""

if client.GetLedgerID().String() == "" {
url = fmt.Sprintf("http://%s:5551/api/v1/accounts/%s", mirrorUrl, hex.EncodeToString(*id.AliasEvmAddress))
protocol = "http"
port = ":5551"
}

if populateType == "account" {
url = fmt.Sprintf("%s://%s%s/api/v1/accounts/%s", protocol, mirrorUrl, port, hex.EncodeToString(*id.AliasEvmAddress))
} else {
url = fmt.Sprintf("%s://%s%s/api/v1/accounts/%s", protocol, mirrorUrl, port, id.String())
}

resp, err := http.Get(url) // #nosec
if err != nil {
return err
return nil, err
}
defer resp.Body.Close()

var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}

return result, nil
}

// PopulateAccount gets the actual `Account` field of the `AccountId` from the Mirror Node.
// Should be used after generating `AccountId.FromEvmAddress()` because it sets the `Account` field to `0`
// automatically since there is no connection between the `Account` and the `evmAddress`
func (id *AccountID) PopulateAccount(client *Client) error {
result, err := id._MirrorNodeRequest(client, "account")
if err != nil {
return err
}

Expand All @@ -380,6 +408,27 @@ func (id *AccountID) PopulateAccount(client *Client) error {
return nil
}

// PopulateEvmAddress gets the actual `AliasEvmAddress` field of the `AccountId` from the Mirror Node.
func (id *AccountID) PopulateEvmAddress(client *Client) error {
result, err := id._MirrorNodeRequest(client, "evmAddress")
if err != nil {
return err
}

mirrorEvmAddress, ok := result["evm_address"].(string)
if !ok {
return errors.New("unexpected response format")
}

mirrorEvmAddress = strings.TrimPrefix(mirrorEvmAddress, "0x")
asd, err := hex.DecodeString(mirrorEvmAddress)
if err != nil {
return err
}
id.AliasEvmAddress = &asd
return nil
}

// Compare returns 0 if the two AccountID are identical, -1 if not.
func (id AccountID) Compare(given AccountID) int {
if id.Shard > given.Shard { //nolint
Expand Down
89 changes: 89 additions & 0 deletions account_id_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package hedera
*/

import (
"encoding/hex"
"testing"
"time"

Expand Down Expand Up @@ -53,3 +54,91 @@ func TestIntegrationAccountIDCanPopulateAccountNumber(t *testing.T) {
require.NoError(t, error)
require.Equal(t, newAccountId.Account, idMirror.Account)
}

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

privateKey, err := PrivateKeyGenerateEcdsa()
require.NoError(t, err)
publicKey := privateKey.PublicKey()
evmAddress := publicKey.ToEvmAddress()
evmAddressAccount, err := AccountIDFromEvmPublicAddress(evmAddress)
require.NoError(t, err)
tx, err := NewTransferTransaction().AddHbarTransfer(evmAddressAccount, NewHbar(1)).
AddHbarTransfer(env.OperatorID, NewHbar(-1)).Execute(env.Client)
require.NoError(t, err)
receipt, err := tx.GetReceiptQuery().SetIncludeChildren(true).Execute(env.Client)
require.NoError(t, err)
newAccountId := *receipt.Children[0].AccountID
time.Sleep(5 * time.Second)
error:= newAccountId.PopulateEvmAddress(env.Client)
require.NoError(t, error)
require.Equal(t, evmAddress, hex.EncodeToString(*newAccountId.AliasEvmAddress))
}

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

privateKey, err := PrivateKeyGenerateEcdsa()
require.NoError(t, err)
publicKey := privateKey.PublicKey()
evmAddress := publicKey.ToEvmAddress()
evmAddressAccount, err := AccountIDFromEvmPublicAddress(evmAddress)
require.NoError(t, err)
tx, err := NewTransferTransaction().AddHbarTransfer(evmAddressAccount, NewHbar(1)).
AddHbarTransfer(env.OperatorID, NewHbar(-1)).Execute(env.Client)
require.NoError(t, err)
receipt, err := tx.GetReceiptQuery().SetIncludeChildren(true).Execute(env.Client)
require.NoError(t, err)
newAccountId := *receipt.Children[0].AccountID
time.Sleep(5 * time.Second)
error:= newAccountId.PopulateEvmAddress(env.Client)
require.NoError(t, error)
require.Equal(t, evmAddress, hex.EncodeToString(*newAccountId.AliasEvmAddress))
}

func TestIntegrationAccountIDCanPopulateAccountAliasEvmAddressWithNoMirror(t *testing.T){
t.Parallel()
env := NewIntegrationTestEnv(t)
env.Client.mirrorNetwork = nil
privateKey, err := PrivateKeyGenerateEcdsa()
require.NoError(t, err)
publicKey := privateKey.PublicKey()
evmAddress := publicKey.ToEvmAddress()
evmAddressAccount, err := AccountIDFromEvmPublicAddress(evmAddress)
require.NoError(t, err)
tx, err := NewTransferTransaction().AddHbarTransfer(evmAddressAccount, NewHbar(1)).
AddHbarTransfer(env.OperatorID, NewHbar(-1)).Execute(env.Client)
require.NoError(t, err)
receipt, err := tx.GetReceiptQuery().SetIncludeChildren(true).Execute(env.Client)
require.NoError(t, err)
newAccountId := *receipt.Children[0].AccountID
env.Client.mirrorNetwork = nil
time.Sleep(5 * time.Second)
error:= newAccountId.PopulateEvmAddress(env.Client)
require.Error(t, error)
}

func TestIntegrationAccountIDCanPopulateAccountAliasEvmAddressWithMirrorAndNoEvmAddress(t *testing.T){
t.Parallel()
env := NewIntegrationTestEnv(t)
env.Client.mirrorNetwork = nil
privateKey, err := PrivateKeyGenerateEcdsa()
require.NoError(t, err)
publicKey := privateKey.PublicKey()
evmAddress := publicKey.ToEvmAddress()
evmAddressAccount, err := AccountIDFromEvmPublicAddress(evmAddress)
require.NoError(t, err)
tx, err := NewTransferTransaction().AddHbarTransfer(evmAddressAccount, NewHbar(1)).
AddHbarTransfer(env.OperatorID, NewHbar(-1)).Execute(env.Client)
require.NoError(t, err)
receipt, err := tx.GetReceiptQuery().SetIncludeChildren(true).Execute(env.Client)
require.NoError(t, err)
newAccountId := *receipt.Children[0].AccountID
env.Client.mirrorNetwork = nil
time.Sleep(5 * time.Second)
error:= newAccountId.PopulateAccount(env.Client)
require.Error(t, error)
}
39 changes: 39 additions & 0 deletions account_id_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,42 @@ func TestUnitAccountIDPopulateFailWithNoMirror(t *testing.T) {
err = evmAddressAccountID.PopulateAccount(client)
require.Error(t, err)
}

func TestUnitAccountIDPopulateEvmFailForWrongMirrorHost(t *testing.T) {
t.Parallel()

client, err := _NewMockClient()
require.NoError(t, err)
client.SetLedgerID(*NewLedgerIDTestnet())
id, err := AccountIDFromString("0.0.3")
require.NoError(t, err)
err = id.PopulateEvmAddress(client)
require.Error(t, err)
}

func TestUnitAccountIDPopulateEvmFailWithNoMirror(t *testing.T) {
t.Parallel()

client, err := _NewMockClient()
require.NoError(t, err)
client.mirrorNetwork = nil
client.SetLedgerID(*NewLedgerIDTestnet())
id, err := AccountIDFromString("0.0.3")
require.NoError(t, err)
err = id.PopulateEvmAddress(client)
require.Error(t, err)
}

func TestUnitAccountIDPopulateEvmFailWithNoMirrorNetwork(t *testing.T) {
t.Parallel()

client, err := _NewMockClient()
require.NoError(t, err)
client.mirrorNetwork = nil
client.SetLedgerID(*NewLedgerIDTestnet())
id, err := AccountIDFromString("0.0.3")
require.NoError(t, err)
err = id.PopulateEvmAddress(client)
require.Error(t, err)
}

10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/cenkalti/backoff/v4 v4.2.1
github.com/cockroachdb/errors v1.11.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.6.0 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/ethereum/go-ethereum v1.13.4
github.com/getsentry/sentry-go v0.25.0 // indirect
github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20230720072335-ed5726877e99
github.com/json-iterator/go v1.1.12
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rs/zerolog v1.31.0
github.com/stretchr/testify v1.8.4
Expand All @@ -26,8 +26,8 @@ require (
golang.org/x/crypto v0.14.0
golang.org/x/sync v0.4.0 // indirect
golang.org/x/text v0.13.0
google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/grpc v1.58.3
google.golang.org/genproto v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/grpc v1.59.0
google.golang.org/protobuf v1.31.0
)
Loading
Loading