github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/key_list.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 26 "github.com/hashgraph/hedera-protobufs-go/services" 27 ) 28 29 // KeyList is a list of keys 30 type KeyList struct { 31 keys []Key 32 threshold int 33 } 34 35 // NewKeyListWithThreshold creates a new KeyList with the given threshold 36 func KeyListWithThreshold(threshold uint) *KeyList { 37 return &KeyList{ 38 keys: make([]Key, 0), 39 threshold: int(threshold), 40 } 41 } 42 43 // NewKeyList creates a new KeyList with no threshold 44 func NewKeyList() *KeyList { 45 return &KeyList{ 46 keys: make([]Key, 0), 47 threshold: -1, 48 } 49 } 50 51 // SetThreshold sets the threshold of the KeyList 52 func (kl *KeyList) SetThreshold(threshold int) *KeyList { 53 kl.threshold = threshold 54 return kl 55 } 56 57 // Add adds a key to the KeyList 58 func (kl *KeyList) Add(key Key) *KeyList { 59 kl.keys = append(kl.keys, key) 60 return kl 61 } 62 63 // AddAll adds all the keys to the KeyList 64 func (kl *KeyList) AddAll(keys []Key) *KeyList { 65 for _, key := range keys { 66 kl.Add(key) 67 } 68 69 return kl 70 } 71 72 // AddAllPublicKeys adds all the public keys to the KeyList 73 func (kl *KeyList) AddAllPublicKeys(keys []PublicKey) *KeyList { 74 for _, key := range keys { 75 kl.Add(key) 76 } 77 78 return kl 79 } 80 81 // String returns a string representation of the KeyList 82 func (kl KeyList) String() string { 83 var s string 84 if kl.threshold > 0 { 85 s = "{threshold:" + fmt.Sprint(kl.threshold) + ",[" 86 } else { 87 s = "{[" 88 } 89 90 for i, key := range kl.keys { 91 s += key.String() 92 if i != len(kl.keys)-1 { 93 s += "," 94 } 95 } 96 97 s += "]}" 98 99 return s 100 } 101 102 func (kl KeyList) _ToProtoKey() *services.Key { 103 keys := make([]*services.Key, len(kl.keys)) 104 for i, key := range kl.keys { 105 keys[i] = key._ToProtoKey() 106 } 107 108 if kl.threshold >= 0 { 109 return &services.Key{ 110 Key: &services.Key_ThresholdKey{ 111 ThresholdKey: &services.ThresholdKey{ 112 Threshold: uint32(kl.threshold), 113 Keys: &services.KeyList{ 114 Keys: keys, 115 }, 116 }, 117 }, 118 } 119 } 120 121 return &services.Key{ 122 Key: &services.Key_KeyList{ 123 KeyList: &services.KeyList{ 124 Keys: keys, 125 }, 126 }, 127 } 128 } 129 130 func (kl *KeyList) _ToProtoKeyList() *services.KeyList { 131 keys := make([]*services.Key, len(kl.keys)) 132 for i, key := range kl.keys { 133 keys[i] = key._ToProtoKey() 134 } 135 136 return &services.KeyList{ 137 Keys: keys, 138 } 139 } 140 141 func _KeyListFromProtobuf(pb *services.KeyList) (KeyList, error) { 142 if pb == nil { 143 return KeyList{}, errParameterNull 144 } 145 var keys = make([]Key, len(pb.Keys)) 146 147 for i, pbKey := range pb.Keys { 148 key, err := _KeyFromProtobuf(pbKey) 149 150 if err != nil { 151 return KeyList{}, err 152 } 153 154 keys[i] = key 155 } 156 157 return KeyList{ 158 keys: keys, 159 threshold: -1, 160 }, nil 161 }