github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/ledger_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/hex" 25 26 "github.com/pkg/errors" 27 ) 28 29 type LedgerID struct { 30 _LedgerIDBytes []byte 31 } 32 33 // LedgerIDFromString returns a LedgerID from a string representation of a ledger ID. 34 func LedgerIDFromString(id string) (*LedgerID, error) { 35 switch id { 36 case "mainnet": //nolint 37 return &LedgerID{ 38 _LedgerIDBytes: []byte{0}, 39 }, nil 40 case "testnet": //nolint 41 return &LedgerID{ 42 _LedgerIDBytes: []byte{1}, 43 }, nil 44 case "previewnet": //nolint 45 return &LedgerID{ 46 _LedgerIDBytes: []byte{2}, 47 }, nil 48 default: 49 temp, err := hex.DecodeString(id) 50 if err != nil { 51 return &LedgerID{}, err 52 } 53 return &LedgerID{ 54 _LedgerIDBytes: temp, 55 }, nil 56 } 57 } 58 59 // LedgerIDFromBytes returns a LedgerID from a byte representation of a ledger ID. 60 func LedgerIDFromBytes(byt []byte) *LedgerID { 61 return &LedgerID{ 62 _LedgerIDBytes: byt, 63 } 64 } 65 66 // LedgerIDFromNetworkName returns a LedgerID from a NetworkName. 67 func LedgerIDFromNetworkName(network NetworkName) (*LedgerID, error) { 68 switch network.String() { 69 case "mainnet": //nolint 70 return &LedgerID{ 71 _LedgerIDBytes: []byte{0}, 72 }, nil 73 case "testnet": //nolint 74 return &LedgerID{ 75 _LedgerIDBytes: []byte{1}, 76 }, nil 77 case "previewnet": //nolint 78 return &LedgerID{ 79 _LedgerIDBytes: []byte{2}, 80 }, nil 81 default: 82 return &LedgerID{}, errors.New("unknown network in network name") 83 } 84 } 85 86 // LedgerIDMainnet returns a LedgerID for mainnet. 87 func NewLedgerIDMainnet() *LedgerID { 88 return &LedgerID{ 89 _LedgerIDBytes: []byte{0}, 90 } 91 } 92 93 // LedgerIDTestnet returns a LedgerID for testnet. 94 func NewLedgerIDTestnet() *LedgerID { 95 return &LedgerID{ 96 _LedgerIDBytes: []byte{1}, 97 } 98 } 99 100 // LedgerIDPreviewnet returns a LedgerID for previewnet. 101 func NewLedgerIDPreviewnet() *LedgerID { 102 return &LedgerID{ 103 _LedgerIDBytes: []byte{2}, 104 } 105 } 106 107 // IsMainnet returns true if the LedgerID is for mainnet. 108 func (id *LedgerID) IsMainnet() bool { 109 return hex.EncodeToString(id._LedgerIDBytes) == "00" 110 } 111 112 // IsTestnet returns true if the LedgerID is for testnet. 113 func (id *LedgerID) IsTestnet() bool { 114 return hex.EncodeToString(id._LedgerIDBytes) == "01" 115 } 116 117 // IsPreviewnet returns true if the LedgerID is for previewnet. 118 func (id *LedgerID) IsPreviewnet() bool { 119 return hex.EncodeToString(id._LedgerIDBytes) == "02" 120 } 121 122 // String returns a string representation of the LedgerID. 123 func (id *LedgerID) String() string { 124 h := hex.EncodeToString(id._LedgerIDBytes) 125 switch h { 126 case "00": 127 return "mainnet" 128 case "01": 129 return "testnet" 130 case "02": 131 return "previewnet" 132 default: 133 return h 134 } 135 } 136 137 // ToBytes returns a byte representation of the LedgerID. 138 func (id *LedgerID) ToBytes() []byte { 139 return id._LedgerIDBytes 140 } 141 142 // ToNetworkName returns a NetworkName from the LedgerID. 143 func (id *LedgerID) ToNetworkName() (NetworkName, error) { 144 switch hex.EncodeToString(id._LedgerIDBytes) { 145 case "00": 146 return NetworkNameMainnet, nil 147 case "01": 148 return NetworkNameTestnet, nil 149 case "02": 150 return NetworkNamePreviewnet, nil 151 default: 152 return NetworkNameOther, nil 153 } 154 }