github.com/number571/tendermint@v0.34.11-gost/crypto/encoding/codec.go (about) 1 package encoding 2 3 import ( 4 "fmt" 5 6 gkeys "github.com/number571/go-cryptopro/gost_r_34_10_2012" 7 "github.com/number571/tendermint/crypto" 8 "github.com/number571/tendermint/crypto/gost256" 9 "github.com/number571/tendermint/crypto/gost512" 10 11 "github.com/number571/tendermint/libs/json" 12 pc "github.com/number571/tendermint/proto/tendermint/crypto" 13 ) 14 15 func init() { 16 json.RegisterType((*pc.PublicKey)(nil), "tendermint.crypto.PublicKey") 17 json.RegisterType((*pc.PublicKey_Gost512)(nil), "tendermint.crypto.PublicKey_Gost512") 18 json.RegisterType((*pc.PublicKey_Gost256)(nil), "tendermint.crypto.PublicKey_Gost256") 19 } 20 21 // PubKeyToProto takes crypto.PubKey and transforms it to a protobuf Pubkey 22 func PubKeyToProto(k crypto.PubKey) (pc.PublicKey, error) { 23 var kp pc.PublicKey 24 switch k := k.(type) { 25 case gost512.PubKey: 26 kp = pc.PublicKey{ 27 Sum: &pc.PublicKey_Gost512{ 28 Gost512: k.Bytes(), 29 }, 30 } 31 case gost256.PubKey: 32 kp = pc.PublicKey{ 33 Sum: &pc.PublicKey_Gost256{ 34 Gost256: k.Bytes(), 35 }, 36 } 37 default: 38 return kp, fmt.Errorf("toproto: key type %v is not supported", k) 39 } 40 return kp, nil 41 } 42 43 // PubKeyFromProto takes a protobuf Pubkey and transforms it to a crypto.Pubkey 44 func PubKeyFromProto(k pc.PublicKey) (crypto.PubKey, error) { 45 switch k := k.Sum.(type) { 46 case *pc.PublicKey_Gost512: 47 if len(k.Gost512) != gost512.PubKeySize { 48 return nil, fmt.Errorf("invalid size for PubKeyGost512. Got %d, expected %d", 49 len(k.Gost512), gost512.PubKeySize) 50 } 51 pk, err := gkeys.LoadPubKey(k.Gost512) 52 if err != nil { 53 return nil, err 54 } 55 return gost512.PubKey(pk.(gkeys.PubKey512)), nil 56 case *pc.PublicKey_Gost256: 57 if len(k.Gost256) != gost256.PubKeySize { 58 return nil, fmt.Errorf("invalid size for PubKeyGost256. Got %d, expected %d", 59 len(k.Gost256), gost256.PubKeySize) 60 } 61 pk, err := gkeys.LoadPubKey(k.Gost256) 62 if err != nil { 63 return nil, err 64 } 65 return gost256.PubKey(pk.(gkeys.PubKey256)), nil 66 default: 67 return nil, fmt.Errorf("fromproto: key type %v is not supported", k) 68 } 69 }