github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/contract_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 "encoding/hex" 28 "fmt" 29 "testing" 30 31 "github.com/hashgraph/hedera-protobufs-go/services" 32 "github.com/stretchr/testify/assert" 33 "github.com/stretchr/testify/require" 34 ) 35 36 func TestUnitContractIDChecksumFromString(t *testing.T) { 37 t.Parallel() 38 39 id, err := ContractIDFromString("0.0.123-rmkyk") 40 require.NoError(t, err) 41 42 client, err := _NewMockClient() 43 client.SetLedgerID(*NewLedgerIDTestnet()) 44 require.NoError(t, err) 45 sol := id.ToSolidityAddress() 46 ContractIDFromSolidityAddress(sol) 47 err = id.Validate(client) 48 require.Error(t, err) 49 evmID, err := ContractIDFromEvmAddress(0, 0, "ace082947b949651c703ff0f02bc1541") 50 require.NoError(t, err) 51 pb := evmID._ToProtobuf() 52 _ContractIDFromProtobuf(pb) 53 54 idByte := id.ToBytes() 55 ContractIDFromBytes(idByte) 56 57 id._ToProtoKey() 58 59 assert.Equal(t, id.Contract, uint64(123)) 60 } 61 62 func TestUnitContractIDChecksumToString(t *testing.T) { 63 t.Parallel() 64 65 id := AccountID{ 66 Shard: 50, 67 Realm: 150, 68 Account: 520, 69 } 70 assert.Equal(t, "50.150.520", id.String()) 71 } 72 73 func TestUnitContractIDFromStringEVM(t *testing.T) { 74 t.Parallel() 75 76 id, err := ContractIDFromString("0.0.0011223344556677889900112233445577889900") 77 require.NoError(t, err) 78 79 require.Equal(t, "0.0.0011223344556677889900112233445577889900", id.String()) 80 } 81 82 func TestUnitContractIDProtobuf(t *testing.T) { 83 t.Parallel() 84 85 id, err := ContractIDFromString("0.0.0011223344556677889900112233445577889900") 86 require.NoError(t, err) 87 88 pb := id._ToProtobuf() 89 90 decoded, err := hex.DecodeString("0011223344556677889900112233445577889900") 91 require.NoError(t, err) 92 93 require.Equal(t, pb, &services.ContractID{ 94 ShardNum: 0, 95 RealmNum: 0, 96 Contract: &services.ContractID_EvmAddress{EvmAddress: decoded}, 97 }) 98 99 pbFrom := _ContractIDFromProtobuf(pb) 100 101 require.Equal(t, id, *pbFrom) 102 } 103 104 func TestUnitContractIDEvm(t *testing.T) { 105 t.Parallel() 106 107 hexString, err := PrivateKeyGenerateEd25519() 108 require.NoError(t, err) 109 id, err := ContractIDFromString(fmt.Sprintf("0.0.%s", hexString.PublicKey().String())) 110 require.NoError(t, err) 111 require.Equal(t, hex.EncodeToString(id.EvmAddress), hexString.PublicKey().String()) 112 113 pb := id._ToProtobuf() 114 require.Equal(t, pb, &services.ContractID{ 115 ShardNum: 0, 116 RealmNum: 0, 117 Contract: &services.ContractID_EvmAddress{EvmAddress: id.EvmAddress}, 118 }) 119 120 id, err = ContractIDFromString("0.0.123") 121 require.NoError(t, err) 122 require.Equal(t, id.Contract, uint64(123)) 123 require.Nil(t, id.EvmAddress) 124 125 pb = id._ToProtobuf() 126 require.Equal(t, pb, &services.ContractID{ 127 ShardNum: 0, 128 RealmNum: 0, 129 Contract: &services.ContractID_ContractNum{ContractNum: 123}, 130 }) 131 } 132 133 func TestUnitContractIDPopulateFailForWrongMirrorHost(t *testing.T) { 134 t.Parallel() 135 136 client, err := _NewMockClient() 137 require.NoError(t, err) 138 client.SetLedgerID(*NewLedgerIDTestnet()) 139 privateKey, err := PrivateKeyGenerateEcdsa() 140 require.NoError(t, err) 141 publicKey := privateKey.PublicKey() 142 evmAddress := publicKey.ToEvmAddress() 143 evmAddressAccountID, err := ContractIDFromEvmAddress(0, 0, evmAddress) 144 require.NoError(t, err) 145 err = evmAddressAccountID.PopulateContract(client) 146 require.Error(t, err) 147 } 148 149 func TestUnitContractIDPopulateFailWithNoMirror(t *testing.T) { 150 t.Parallel() 151 152 client, err := _NewMockClient() 153 require.NoError(t, err) 154 client.mirrorNetwork = nil 155 client.SetLedgerID(*NewLedgerIDTestnet()) 156 privateKey, err := PrivateKeyGenerateEcdsa() 157 require.NoError(t, err) 158 publicKey := privateKey.PublicKey() 159 evmAddress := publicKey.ToEvmAddress() 160 evmAddressAccountID, err := ContractIDFromEvmAddress(0, 0, evmAddress) 161 require.NoError(t, err) 162 err = evmAddressAccountID.PopulateContract(client) 163 require.Error(t, err) 164 }