github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/id.go (about) 1 package hedera 2 3 /*- 4 * 5 * Hedera Go SDK 6 * 7 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 */ 22 23 import ( 24 "encoding/binary" 25 "encoding/hex" 26 "fmt" 27 "strconv" 28 "strings" 29 30 "github.com/hashgraph/hedera-protobufs-go/services" 31 protobuf "google.golang.org/protobuf/proto" 32 ) 33 34 func _AccountIDFromString(s string) (shard int, realm int, num int, checksum *string, alias *PublicKey, evmAddress *[]byte, err error) { 35 if _Has0xPrefix(s) { 36 s = _Without0x(s) 37 } 38 if _IsHex(s) { 39 bytes := _Hex2Bytes(s) 40 if err == nil { 41 if len(bytes) == 20 { 42 return 0, 0, -1, nil, nil, &bytes, nil 43 } 44 } 45 } 46 47 if strings.Contains(s, "-") { 48 values := strings.SplitN(s, "-", 2) 49 50 if len(values) > 2 { 51 return 0, 0, 0, nil, nil, nil, fmt.Errorf("expected {shard}.{realm}.{num}-{checksum}") 52 } 53 54 checksum = &values[1] 55 s = values[0] 56 } 57 58 values := strings.SplitN(s, ".", 3) 59 if len(values) != 3 { 60 // Was not three values separated by periods 61 return 0, 0, 0, nil, nil, nil, fmt.Errorf("expected {shard}.{realm}.{num}") 62 } 63 64 shard, err = strconv.Atoi(values[0]) 65 if err != nil { 66 return 0, 0, 0, nil, nil, nil, err 67 } 68 69 realm, err = strconv.Atoi(values[1]) 70 if err != nil { 71 return 0, 0, 0, nil, nil, nil, err 72 } 73 74 if len(values[2]) < 20 { 75 num, err = strconv.Atoi(values[2]) 76 if err != nil { 77 return 0, 0, 0, nil, nil, nil, err 78 } 79 80 return shard, realm, num, checksum, nil, nil, nil 81 } else if len(values[2]) == 40 { 82 temp, err2 := hex.DecodeString(values[2]) 83 if err2 != nil { 84 return 0, 0, 0, nil, nil, nil, err2 85 } 86 var key services.Key 87 err2 = protobuf.Unmarshal(temp, &key) 88 if err2 != nil { 89 return shard, realm, -1, checksum, nil, &temp, nil 90 } 91 aliasKey, err2 := _KeyFromProtobuf(&key) 92 if err2 != nil { 93 return shard, realm, -1, checksum, nil, &temp, nil 94 } 95 96 if aliasPublicKey, ok := aliasKey.(PublicKey); ok { 97 return shard, realm, -1, checksum, &aliasPublicKey, nil, nil 98 } 99 100 return shard, realm, -1, checksum, nil, &temp, nil 101 } 102 103 key, err := PublicKeyFromString(values[2]) 104 if err != nil { 105 return 0, 0, 0, nil, nil, nil, err 106 } 107 108 return shard, realm, -1, checksum, &key, nil, nil 109 } 110 111 func _ContractIDFromString(s string) (shard int, realm int, num int, checksum *string, evmAddress []byte, err error) { 112 if strings.Contains(s, "-") { 113 values := strings.SplitN(s, "-", 2) 114 115 if len(values) > 2 { 116 return 0, 0, 0, nil, nil, fmt.Errorf("expected {shard}.{realm}.{num}-{checksum}") 117 } 118 119 checksum = &values[1] 120 s = values[0] 121 } 122 123 values := strings.SplitN(s, ".", 3) 124 if len(values) != 3 { 125 // Was not three values separated by periods 126 return 0, 0, 0, nil, nil, fmt.Errorf("expected {shard}.{realm}.{num}") 127 } 128 129 shard, err = strconv.Atoi(values[0]) 130 if err != nil { 131 return 0, 0, 0, nil, nil, err 132 } 133 134 realm, err = strconv.Atoi(values[1]) 135 if err != nil { 136 return 0, 0, 0, nil, nil, err 137 } 138 139 num, err = strconv.Atoi(values[2]) 140 if err != nil { 141 temp, err2 := hex.DecodeString(values[2]) 142 if err2 != nil { 143 return 0, 0, 0, nil, nil, err 144 } 145 return shard, realm, -1, checksum, temp, nil 146 } 147 148 return shard, realm, num, checksum, nil, nil 149 } 150 151 func _IdFromString(s string) (shard int, realm int, num int, checksum *string, err error) { 152 if strings.Contains(s, "-") { 153 values := strings.SplitN(s, "-", 2) 154 155 if len(values) > 2 { 156 return 0, 0, 0, nil, fmt.Errorf("expected {shard}.{realm}.{num}-{checksum}") 157 } 158 159 checksum = &values[1] 160 s = values[0] 161 } 162 163 values := strings.SplitN(s, ".", 3) 164 if len(values) != 3 { 165 // Was not three values separated by periods 166 return 0, 0, 0, nil, fmt.Errorf("expected {shard}.{realm}.{num}") 167 } 168 169 shard, err = strconv.Atoi(values[0]) 170 if err != nil { 171 return 0, 0, 0, nil, err 172 } 173 174 realm, err = strconv.Atoi(values[1]) 175 if err != nil { 176 return 0, 0, 0, nil, err 177 } 178 179 num, err = strconv.Atoi(values[2]) 180 if err != nil { 181 return 0, 0, 0, nil, err 182 } 183 184 return shard, realm, num, checksum, nil 185 } 186 187 func _IdFromSolidityAddress(s string) (uint64, uint64, uint64, error) { 188 bytes, err := hex.DecodeString(s) 189 if err != nil { 190 return 0, 0, 0, err 191 } 192 193 if len(bytes) != 20 { 194 return 0, 0, 0, fmt.Errorf("_Solidity address must be 20 bytes") 195 } 196 197 return uint64(binary.BigEndian.Uint32(bytes[0:4])), binary.BigEndian.Uint64(bytes[4:12]), binary.BigEndian.Uint64(bytes[12:20]), nil 198 } 199 200 func _IdToSolidityAddress(shard uint64, realm uint64, num uint64) string { 201 bytes := make([]byte, 20) 202 binary.BigEndian.PutUint32(bytes[0:4], uint32(shard)) 203 binary.BigEndian.PutUint64(bytes[4:12], realm) 204 binary.BigEndian.PutUint64(bytes[12:20], num) 205 return hex.EncodeToString(bytes) 206 } 207 208 func _Has0xPrefix(str string) bool { 209 return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') 210 } 211 212 func _Without0x(s string) string { 213 if _Has0xPrefix(s) { 214 s = s[2:] 215 } 216 if len(s)%2 == 1 { 217 s = "0" + s 218 } 219 return s 220 } 221 222 func _Hex2Bytes(str string) []byte { 223 h, _ := hex.DecodeString(str) 224 return h 225 } 226 227 func _IsHexCharacter(c byte) bool { 228 return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') 229 } 230 231 func _IsHex(str string) bool { 232 if len(str)%2 != 0 { 233 return false 234 } 235 for _, c := range []byte(str) { 236 if !_IsHexCharacter(c) { 237 return false 238 } 239 } 240 return true 241 }