github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/schedule_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 "fmt" 25 "strings" 26 27 "github.com/pkg/errors" 28 29 "github.com/hashgraph/hedera-protobufs-go/services" 30 ) 31 32 // ScheduleID is the ID for a Hedera account 33 type ScheduleID struct { 34 Shard uint64 35 Realm uint64 36 Schedule uint64 37 checksum *string 38 } 39 40 // ScheduleIDFromString constructs an ScheduleID from a string formatted as 41 // `Shard.Realm.Account` (for example "0.0.3") 42 func ScheduleIDFromString(data string) (ScheduleID, error) { 43 shard, realm, num, checksum, err := _IdFromString(data) 44 if err != nil { 45 return ScheduleID{}, err 46 } 47 48 return ScheduleID{ 49 Shard: uint64(shard), 50 Realm: uint64(realm), 51 Schedule: uint64(num), 52 checksum: checksum, 53 }, nil 54 } 55 56 // ValidateChecksum validates the checksum of the account ID 57 func (id *ScheduleID) ValidateChecksum(client *Client) error { 58 if !id._IsZero() && client != nil { 59 var tempChecksum _ParseAddressResult 60 var err error 61 tempChecksum, err = _ChecksumParseAddress(client.GetLedgerID(), fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.Schedule)) 62 if err != nil { 63 return err 64 } 65 err = _ChecksumVerify(tempChecksum.status) 66 if err != nil { 67 return err 68 } 69 if id.checksum == nil { 70 return errChecksumMissing 71 } 72 if tempChecksum.correctChecksum != *id.checksum { 73 networkName := NetworkNameOther 74 if client.network.ledgerID != nil { 75 networkName, _ = client.network.ledgerID.ToNetworkName() 76 } 77 return errors.New(fmt.Sprintf("network mismatch or wrong checksum given, given checksum: %s, correct checksum %s, network: %s", 78 *id.checksum, 79 tempChecksum.correctChecksum, 80 networkName)) 81 } 82 } 83 84 return nil 85 } 86 87 // Deprecated - use ValidateChecksum instead 88 func (id *ScheduleID) Validate(client *Client) error { 89 return id.ValidateChecksum(client) 90 } 91 92 // String returns the string representation of an ScheduleID in 93 // `Shard.Realm.Account` (for example "0.0.3") 94 func (id ScheduleID) String() string { 95 return fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.Schedule) 96 } 97 98 // ToStringWithChecksum returns the string representation of an ScheduleID in 99 // `Shard.Realm.Account-checksum` (for example "0.0.3-laujm") 100 func (id ScheduleID) ToStringWithChecksum(client Client) (string, error) { 101 if client.GetNetworkName() == nil && client.GetLedgerID() == nil { 102 return "", errNetworkNameMissing 103 } 104 var checksum _ParseAddressResult 105 var err error 106 if client.network.ledgerID != nil { 107 checksum, err = _ChecksumParseAddress(client.GetLedgerID(), fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.Schedule)) 108 } 109 if err != nil { 110 return "", err 111 } 112 return fmt.Sprintf("%d.%d.%d-%s", id.Shard, id.Realm, id.Schedule, checksum.correctChecksum), nil 113 } 114 115 func (id ScheduleID) _ToProtobuf() *services.ScheduleID { 116 return &services.ScheduleID{ 117 ShardNum: int64(id.Shard), 118 RealmNum: int64(id.Realm), 119 ScheduleNum: int64(id.Schedule), 120 } 121 } 122 123 // UnmarshalJSON implements the encoding.JSON interface. 124 func (id *ScheduleID) UnmarshalJSON(data []byte) error { 125 scheduleID, err := ScheduleIDFromString(strings.Replace(string(data), "\"", "", 2)) 126 127 if err != nil { 128 return err 129 } 130 131 id.Shard = scheduleID.Shard 132 id.Realm = scheduleID.Realm 133 id.Schedule = scheduleID.Schedule 134 id.checksum = scheduleID.checksum 135 136 return nil 137 } 138 139 func _ScheduleIDFromProtobuf(scheduleID *services.ScheduleID) *ScheduleID { 140 if scheduleID == nil { 141 return nil 142 } 143 144 return &ScheduleID{ 145 Shard: uint64(scheduleID.ShardNum), 146 Realm: uint64(scheduleID.RealmNum), 147 Schedule: uint64(scheduleID.ScheduleNum), 148 } 149 } 150 151 func (id ScheduleID) _IsZero() bool { 152 return id.Shard == 0 && id.Realm == 0 && id.Schedule == 0 153 } 154 155 func (id ScheduleID) _Equals(other ScheduleID) bool { // nolint 156 return id.Shard == other.Shard && id.Realm == other.Realm && id.Schedule == other.Schedule 157 }