github.com/koko1123/flow-go-1@v0.29.6/model/encodable/keys.go (about) 1 package encodable 2 3 import ( 4 "encoding/hex" 5 "encoding/json" 6 "fmt" 7 "io" 8 9 "github.com/ethereum/go-ethereum/rlp" 10 "github.com/fxamacker/cbor/v2" 11 "github.com/vmihailenco/msgpack" 12 13 "github.com/onflow/flow-go/crypto" 14 ) 15 16 // ConsensusVoteSigLen is the length of a consensus vote as well as aggregated consensus votes. 17 const ConsensusVoteSigLen = uint(crypto.SignatureLenBLSBLS12381) 18 19 // RandomBeaconSigLen is the length of a random beacon signature share as well as the random beacon resonstructed signature. 20 const RandomBeaconSigLen = uint(crypto.SignatureLenBLSBLS12381) 21 22 func toHex(bs []byte) string { 23 return fmt.Sprintf("%x", bs) 24 } 25 26 func fromJSONHex(b []byte) ([]byte, error) { 27 var x string 28 if err := json.Unmarshal(b, &x); err != nil { 29 return nil, fmt.Errorf("could not unmarshal the key: %w", err) 30 } 31 return hex.DecodeString(x) 32 } 33 34 func fromMsgPackHex(b []byte) ([]byte, error) { 35 var x string 36 if err := msgpack.Unmarshal(b, &x); err != nil { 37 return nil, fmt.Errorf("could not unmarshal the key: %w", err) 38 } 39 return hex.DecodeString(x) 40 } 41 42 func fromCBORPackHex(b []byte) ([]byte, error) { 43 var x string 44 if err := cbor.Unmarshal(b, &x); err != nil { 45 return nil, fmt.Errorf("could not unmarshal the key: %w", err) 46 } 47 return hex.DecodeString(x) 48 } 49 50 // NetworkPubKey wraps a public key and allows it to be JSON encoded and decoded. It is not defined in the 51 // crypto package since the crypto package should not know about the different key types. 52 type NetworkPubKey struct { 53 crypto.PublicKey 54 } 55 56 func (pub NetworkPubKey) MarshalJSON() ([]byte, error) { 57 if pub.PublicKey == nil { 58 return json.Marshal(nil) 59 } 60 return json.Marshal(toHex(pub.Encode())) 61 } 62 63 func (pub *NetworkPubKey) UnmarshalJSON(b []byte) error { 64 bz, err := fromJSONHex(b) 65 if err != nil { 66 return err 67 } 68 69 if len(bz) == 0 { 70 return nil 71 } 72 73 pub.PublicKey, err = crypto.DecodePublicKey(crypto.ECDSAP256, bz) 74 return err 75 } 76 77 // NetworkPrivKey wraps a private key and allows it to be JSON encoded and decoded. It is not defined in the 78 // crypto package since the crypto package should not know about the different key types. More importantly, private 79 // keys should not be automatically encodable/serializable to prevent accidental secret sharing. The bootstrapping 80 // package is an exception, since it generates private keys that need to be serialized. 81 type NetworkPrivKey struct { 82 crypto.PrivateKey 83 } 84 85 func (priv NetworkPrivKey) MarshalJSON() ([]byte, error) { 86 if priv.PrivateKey == nil { 87 return json.Marshal(nil) 88 } 89 return json.Marshal(toHex(priv.Encode())) 90 } 91 92 func (priv *NetworkPrivKey) UnmarshalJSON(b []byte) error { 93 bz, err := fromJSONHex(b) 94 if err != nil { 95 return err 96 } 97 98 if len(bz) == 0 { 99 return nil 100 } 101 priv.PrivateKey, err = crypto.DecodePrivateKey(crypto.ECDSAP256, bz) 102 return err 103 } 104 105 // StakingPubKey wraps a public key and allows it to be JSON encoded and decoded. It is not defined in the 106 // crypto package since the crypto package should not know about the different key types. 107 type StakingPubKey struct { 108 crypto.PublicKey 109 } 110 111 func (pub StakingPubKey) MarshalJSON() ([]byte, error) { 112 if pub.PublicKey == nil { 113 return json.Marshal(nil) 114 } 115 return json.Marshal(toHex(pub.Encode())) 116 } 117 118 func (pub *StakingPubKey) UnmarshalJSON(b []byte) error { 119 bz, err := fromJSONHex(b) 120 if err != nil { 121 return err 122 } 123 124 if len(bz) == 0 { 125 return nil 126 } 127 pub.PublicKey, err = crypto.DecodePublicKey(crypto.BLSBLS12381, bz) 128 return err 129 } 130 131 // StakingPrivKey wraps a private key and allows it to be JSON encoded and decoded. It is not defined in the 132 // crypto package since the crypto package should not know about the different key types. More importantly, private 133 // keys should not be automatically encodable/serializable to prevent accidental secret sharing. The bootstrapping 134 // package is an exception, since it generates private keys that need to be serialized. 135 type StakingPrivKey struct { 136 crypto.PrivateKey 137 } 138 139 func (priv StakingPrivKey) MarshalJSON() ([]byte, error) { 140 if priv.PrivateKey == nil { 141 return json.Marshal(nil) 142 } 143 return json.Marshal(toHex(priv.Encode())) 144 } 145 146 func (priv *StakingPrivKey) UnmarshalJSON(b []byte) error { 147 bz, err := fromJSONHex(b) 148 if err != nil { 149 return err 150 } 151 152 if len(bz) == 0 { 153 return nil 154 } 155 priv.PrivateKey, err = crypto.DecodePrivateKey(crypto.BLSBLS12381, bz) 156 return err 157 } 158 159 // RandomBeaconPubKey wraps a public key and allows it to be JSON encoded and decoded. It is not defined in the 160 // crypto package since the crypto package should not know about the different key types. 161 type RandomBeaconPubKey struct { 162 crypto.PublicKey 163 } 164 165 func (pub RandomBeaconPubKey) MarshalJSON() ([]byte, error) { 166 if pub.PublicKey == nil { 167 return json.Marshal(nil) 168 } 169 return json.Marshal(toHex(pub.Encode())) 170 } 171 172 func (pub *RandomBeaconPubKey) UnmarshalJSON(b []byte) error { 173 bz, err := fromJSONHex(b) 174 if err != nil { 175 return err 176 } 177 178 if len(bz) == 0 { 179 return nil 180 } 181 pub.PublicKey, err = crypto.DecodePublicKey(crypto.BLSBLS12381, bz) 182 return err 183 } 184 185 func (pub RandomBeaconPubKey) MarshalCBOR() ([]byte, error) { 186 if pub.PublicKey == nil { 187 return cbor.Marshal(nil) 188 } 189 return cbor.Marshal(toHex(pub.Encode())) 190 } 191 192 func (pub *RandomBeaconPubKey) UnmarshalCBOR(b []byte) error { 193 bz, err := fromCBORPackHex(b) 194 if err != nil { 195 return err 196 } 197 198 if len(bz) == 0 { 199 return nil 200 } 201 pub.PublicKey, err = crypto.DecodePublicKey(crypto.BLSBLS12381, bz) 202 return err 203 } 204 205 func (pub RandomBeaconPubKey) MarshalMsgpack() ([]byte, error) { 206 if pub.PublicKey == nil { 207 return nil, fmt.Errorf("empty public key") 208 } 209 return msgpack.Marshal(toHex(pub.PublicKey.Encode())) 210 } 211 212 func (pub *RandomBeaconPubKey) UnmarshalMsgpack(b []byte) error { 213 bz, err := fromMsgPackHex(b) 214 if err != nil { 215 return err 216 } 217 218 pub.PublicKey, err = crypto.DecodePublicKey(crypto.BLSBLS12381, bz) 219 return err 220 } 221 222 func (pub *RandomBeaconPubKey) EncodeRLP(w io.Writer) error { 223 return rlp.Encode(w, pub.PublicKey.Encode()) 224 } 225 226 // RandomBeaconPrivKey wraps a private key and allows it to be JSON encoded and decoded. It is not defined in 227 // the crypto package since the crypto package should not know about the different key types. More importantly, private 228 // keys should not be automatically encodable/serializable to prevent accidental secret sharing. The bootstrapping 229 // package is an exception, since it generates private keys that need to be serialized. 230 type RandomBeaconPrivKey struct { 231 crypto.PrivateKey 232 } 233 234 func (priv RandomBeaconPrivKey) MarshalJSON() ([]byte, error) { 235 if priv.PrivateKey == nil { 236 return json.Marshal(nil) 237 } 238 return json.Marshal(toHex(priv.Encode())) 239 } 240 241 func (priv *RandomBeaconPrivKey) UnmarshalJSON(b []byte) error { 242 bz, err := fromJSONHex(b) 243 if err != nil { 244 return err 245 } 246 247 if len(bz) == 0 { 248 return nil 249 } 250 priv.PrivateKey, err = crypto.DecodePrivateKey(crypto.BLSBLS12381, bz) 251 return err 252 } 253 254 func (priv RandomBeaconPrivKey) MarshalMsgpack() ([]byte, error) { 255 if priv.PrivateKey == nil { 256 return nil, fmt.Errorf("empty private key") 257 } 258 return msgpack.Marshal(toHex(priv.PrivateKey.Encode())) 259 } 260 261 func (priv *RandomBeaconPrivKey) UnmarshalMsgpack(b []byte) error { 262 bz, err := fromMsgPackHex(b) 263 if err != nil { 264 return err 265 } 266 267 priv.PrivateKey, err = crypto.DecodePrivateKey(crypto.BLSBLS12381, bz) 268 return err 269 } 270 271 // MachineAccountPrivKey wraps a private key and allows it to be JSON encoded and decoded. It is not defined in the 272 // crypto package since the crypto package should not know about the different key types. More importantly, private 273 // keys should not be automatically encodable/serializable to prevent accidental secret sharing. The bootstrapping 274 // package is an exception, since it generates private keys that need to be serialized. 275 type MachineAccountPrivKey struct { 276 crypto.PrivateKey 277 } 278 279 func (priv MachineAccountPrivKey) MarshalJSON() ([]byte, error) { 280 if priv.PrivateKey == nil { 281 return json.Marshal(nil) 282 } 283 return json.Marshal(toHex(priv.Encode())) 284 } 285 286 func (priv *MachineAccountPrivKey) UnmarshalJSON(b []byte) error { 287 bz, err := fromJSONHex(b) 288 if err != nil { 289 return err 290 } 291 292 if len(bz) == 0 { 293 return nil 294 } 295 priv.PrivateKey, err = crypto.DecodePrivateKey(crypto.ECDSAP256, bz) 296 return err 297 }