github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_id_unit_test.go (about)

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestUnitAccountIDChecksumFromString(t *testing.T) {
    35  	t.Parallel()
    36  
    37  	id, err := AccountIDFromString("0.0.123-rmkyk")
    38  	require.NoError(t, err)
    39  	client, err := _NewMockClient()
    40  	client.SetLedgerID(*NewLedgerIDTestnet())
    41  	require.NoError(t, err)
    42  	id.ToStringWithChecksum(client)
    43  	id.GetChecksum()
    44  	sol := id.ToSolidityAddress()
    45  	AccountIDFromSolidityAddress(sol)
    46  	err = id.Validate(client)
    47  	require.Error(t, err)
    48  	evmID, err := AccountIDFromEvmAddress(0, 0, "ace082947b949651c703ff0f02bc1541")
    49  	require.NoError(t, err)
    50  	pb := evmID._ToProtobuf()
    51  	_AccountIDFromProtobuf(pb)
    52  
    53  	idByte := id.ToBytes()
    54  	AccountIDFromBytes(idByte)
    55  
    56  	key, err := PrivateKeyGenerateEd25519()
    57  	require.NoError(t, err)
    58  
    59  	alias := key.ToAccountID(0, 0)
    60  	pb = alias._ToProtobuf()
    61  	_AccountIDFromProtobuf(pb)
    62  
    63  	require.NoError(t, err)
    64  	assert.Equal(t, id.Account, uint64(123))
    65  }
    66  
    67  func TestUnitAccountIDChecksumToString(t *testing.T) {
    68  	t.Parallel()
    69  
    70  	id := AccountID{
    71  		Shard:   50,
    72  		Realm:   150,
    73  		Account: 520,
    74  	}
    75  	assert.Equal(t, "50.150.520", id.String())
    76  }
    77  
    78  func TestUnitAccountIDFromStringAlias(t *testing.T) {
    79  	t.Parallel()
    80  
    81  	key, err := GeneratePrivateKey()
    82  	require.NoError(t, err)
    83  	id, err := AccountIDFromString("0.0." + key.PublicKey().String())
    84  	require.NoError(t, err)
    85  	id2 := key.ToAccountID(0, 0)
    86  
    87  	assert.Equal(t, id.String(), id2.String())
    88  }
    89  
    90  func TestUnitChecksum(t *testing.T) {
    91  	t.Parallel()
    92  
    93  	id, err := LedgerIDFromString("01")
    94  	require.NoError(t, err)
    95  	ad1, err := _ChecksumParseAddress(id, "0.0.3")
    96  	require.NoError(t, err)
    97  	id, err = LedgerIDFromString("10")
    98  	require.NoError(t, err)
    99  	ad2, err := _ChecksumParseAddress(id, "0.0.3")
   100  	require.NoError(t, err)
   101  
   102  	require.NotEqual(t, ad1.correctChecksum, ad2.correctChecksum)
   103  }
   104  
   105  func TestUnitAccountIDEvm(t *testing.T) {
   106  	t.Parallel()
   107  
   108  	id, err := AccountIDFromString("0.0.0011223344556677889900112233445566778899")
   109  	require.NoError(t, err)
   110  
   111  	require.Equal(t, id.String(), "0.0.0011223344556677889900112233445566778899")
   112  }
   113  
   114  func TestUnitAccountIDPopulateFailForWrongMirrorHost(t *testing.T) {
   115  	t.Parallel()
   116  
   117  	client, err := _NewMockClient()
   118  	require.NoError(t, err)
   119  	client.SetLedgerID(*NewLedgerIDTestnet())
   120  	privateKey, err := PrivateKeyGenerateEcdsa()
   121  	require.NoError(t, err)
   122  	publicKey := privateKey.PublicKey()
   123  	evmAddress := publicKey.ToEvmAddress()
   124  	evmAddressAccountID, err := AccountIDFromEvmPublicAddress(evmAddress)
   125  	require.NoError(t, err)
   126  	err = evmAddressAccountID.PopulateAccount(client)
   127  	require.Error(t, err)
   128  }
   129  
   130  func TestUnitAccountIDPopulateFailWithNoMirror(t *testing.T) {
   131  	t.Parallel()
   132  
   133  	client, err := _NewMockClient()
   134  	require.NoError(t, err)
   135  	client.mirrorNetwork = nil
   136  	client.SetLedgerID(*NewLedgerIDTestnet())
   137  	privateKey, err := PrivateKeyGenerateEcdsa()
   138  	require.NoError(t, err)
   139  	publicKey := privateKey.PublicKey()
   140  	evmAddress := publicKey.ToEvmAddress()
   141  	evmAddressAccountID, err := AccountIDFromEvmPublicAddress(evmAddress)
   142  	require.NoError(t, err)
   143  	err = evmAddressAccountID.PopulateAccount(client)
   144  	require.Error(t, err)
   145  }
   146  
   147  func TestUnitAccountIDPopulateEvmFailForWrongMirrorHost(t *testing.T) {
   148  	t.Parallel()
   149  
   150  	client, err := _NewMockClient()
   151  	require.NoError(t, err)
   152  	client.SetLedgerID(*NewLedgerIDTestnet())
   153  	id, err := AccountIDFromString("0.0.3")
   154  	require.NoError(t, err)
   155  	err = id.PopulateEvmAddress(client)
   156  	require.Error(t, err)
   157  }
   158  
   159  func TestUnitAccountIDPopulateEvmFailWithNoMirror(t *testing.T) {
   160  	t.Parallel()
   161  
   162  	client, err := _NewMockClient()
   163  	require.NoError(t, err)
   164  	client.mirrorNetwork = nil
   165  	client.SetLedgerID(*NewLedgerIDTestnet())
   166  	id, err := AccountIDFromString("0.0.3")
   167  	require.NoError(t, err)
   168  	err = id.PopulateEvmAddress(client)
   169  	require.Error(t, err)
   170  }
   171  
   172  func TestUnitAccountIDPopulateEvmFailWithNoMirrorNetwork(t *testing.T) {
   173  	t.Parallel()
   174  
   175  	client, err := _NewMockClient()
   176  	require.NoError(t, err)
   177  	client.mirrorNetwork = nil
   178  	client.SetLedgerID(*NewLedgerIDTestnet())
   179  	id, err := AccountIDFromString("0.0.3")
   180  	require.NoError(t, err)
   181  	err = id.PopulateEvmAddress(client)
   182  	require.Error(t, err)
   183  }