github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/delegate_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/require" 33 ) 34 35 func TestUnitDelegatableContractIDChecksumFromString(t *testing.T) { 36 t.Parallel() 37 38 id, err := DelegatableContractIDFromString("0.0.123-rmkyk") 39 require.NoError(t, err) 40 41 client, err := _NewMockClient() 42 client.SetLedgerID(*NewLedgerIDTestnet()) 43 require.NoError(t, err) 44 err = id.ValidateChecksum(client) 45 require.Error(t, err) 46 require.Equal(t, id.Contract, uint64(123)) 47 strChecksum, err := id.ToStringWithChecksum(*client) 48 require.NoError(t, err) 49 // different checksum because of different network 50 require.Equal(t, strChecksum, "0.0.123-esxsf") 51 } 52 53 func TestUnitDelegatableContractIDChecksumToString(t *testing.T) { 54 t.Parallel() 55 56 id := DelegatableContractID{ 57 Shard: 50, 58 Realm: 150, 59 Contract: 520, 60 } 61 require.Equal(t, "50.150.520", id.String()) 62 } 63 64 func TestUnitDelegatableContractIDFromStringEVM(t *testing.T) { 65 t.Parallel() 66 67 id, err := DelegatableContractIDFromString("0.0.0011223344556677889900112233445577889900") 68 require.NoError(t, err) 69 70 require.Equal(t, "0.0.0011223344556677889900112233445577889900", id.String()) 71 } 72 73 func TestUnitDelegatableContractIDProtobuf(t *testing.T) { 74 t.Parallel() 75 76 id, err := DelegatableContractIDFromString("0.0.0011223344556677889900112233445577889900") 77 require.NoError(t, err) 78 79 pb := id._ToProtobuf() 80 81 decoded, err := hex.DecodeString("0011223344556677889900112233445577889900") 82 require.NoError(t, err) 83 84 require.Equal(t, pb, &services.ContractID{ 85 ShardNum: 0, 86 RealmNum: 0, 87 Contract: &services.ContractID_EvmAddress{EvmAddress: decoded}, 88 }) 89 90 pbFrom := _DelegatableContractIDFromProtobuf(pb) 91 92 require.Equal(t, id, *pbFrom) 93 } 94 95 func TestUnitDelegatableContractIDEvm(t *testing.T) { 96 t.Parallel() 97 98 hexString, err := PrivateKeyGenerateEd25519() 99 require.NoError(t, err) 100 id, err := DelegatableContractIDFromString(fmt.Sprintf("0.0.%s", hexString.PublicKey().String())) 101 require.NoError(t, err) 102 require.Equal(t, hex.EncodeToString(id.EvmAddress), hexString.PublicKey().String()) 103 104 pb := id._ToProtobuf() 105 require.Equal(t, pb, &services.ContractID{ 106 ShardNum: 0, 107 RealmNum: 0, 108 Contract: &services.ContractID_EvmAddress{EvmAddress: id.EvmAddress}, 109 }) 110 111 id, err = DelegatableContractIDFromString("0.0.123") 112 require.NoError(t, err) 113 require.Equal(t, id.Contract, uint64(123)) 114 require.Nil(t, id.EvmAddress) 115 116 pb = id._ToProtobuf() 117 require.Equal(t, pb, &services.ContractID{ 118 ShardNum: 0, 119 RealmNum: 0, 120 Contract: &services.ContractID_ContractNum{ContractNum: 123}, 121 }) 122 } 123 124 func TestUnitDelegatableContractIDToFromBytes(t *testing.T) { 125 t.Parallel() 126 127 id, err := DelegatableContractIDFromString("0.0.123") 128 require.NoError(t, err) 129 require.Equal(t, id.Contract, uint64(123)) 130 require.Nil(t, id.EvmAddress) 131 132 idBytes := id.ToBytes() 133 idFromBytes, err := DelegatableContractIDFromBytes(idBytes) 134 require.NoError(t, err) 135 require.Equal(t, id, idFromBytes) 136 } 137 138 func TestUnitDelegatableContractIDFromEvmAddress(t *testing.T) { 139 t.Parallel() 140 141 id, err := DelegatableContractIDFromEvmAddress(0, 0, "0011223344556677889900112233445566778899") 142 require.NoError(t, err) 143 require.Equal(t, id.Contract, uint64(0)) 144 require.Equal(t, id.EvmAddress, []byte{0x0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}) 145 } 146 147 func TestUnitDelegatableContractIDFromSolidityAddress(t *testing.T) { 148 t.Parallel() 149 150 id, err := DelegatableContractIDFromString("0.0.123-rmkyk") 151 require.NoError(t, err) 152 sol := id.ToSolidityAddress() 153 idFromSolidity, err := DelegatableContractIDFromSolidityAddress(sol) 154 require.NoError(t, err) 155 require.Equal(t, idFromSolidity.Contract, uint64(123)) 156 } 157 158 func TestUnitDelegatableContractIDToProtoKey(t *testing.T) { 159 t.Parallel() 160 161 id, err := DelegatableContractIDFromString("0.0.123-rmkyk") 162 require.NoError(t, err) 163 pb := id._ToProtoKey() 164 require.Equal(t, pb.GetContractID().GetContractNum(), int64(123)) 165 }