github.com/datachainlab/burrow@v0.25.0/crypto/crypto.go (about) 1 package crypto 2 3 import ( 4 "fmt" 5 ) 6 7 type CurveType uint32 8 9 const ( 10 CurveTypeUnset CurveType = iota 11 CurveTypeEd25519 12 CurveTypeSecp256k1 13 ) 14 15 func (k CurveType) String() string { 16 switch k { 17 case CurveTypeSecp256k1: 18 return "secp256k1" 19 case CurveTypeEd25519: 20 return "ed25519" 21 case CurveTypeUnset: 22 return "" 23 default: 24 return "unknown" 25 } 26 } 27 func (k CurveType) ABCIType() string { 28 switch k { 29 case CurveTypeSecp256k1: 30 return "secp256k1" 31 case CurveTypeEd25519: 32 return "ed25519" 33 case CurveTypeUnset: 34 return "" 35 default: 36 return "unknown" 37 } 38 } 39 40 // Get this CurveType's 8 bit identifier as a byte 41 func (k CurveType) Byte() byte { 42 return byte(k) 43 } 44 45 func CurveTypeFromString(s string) (CurveType, error) { 46 switch s { 47 case "secp256k1": 48 return CurveTypeSecp256k1, nil 49 case "ed25519": 50 return CurveTypeEd25519, nil 51 case "": 52 return CurveTypeUnset, nil 53 default: 54 return CurveTypeUnset, ErrInvalidCurve(s) 55 } 56 } 57 58 type ErrInvalidCurve string 59 60 func (err ErrInvalidCurve) Error() string { 61 return fmt.Sprintf("invalid curve type") 62 } 63 64 // The types in this file allow us to control serialisation of keys and signatures, as well as the interface 65 // exposed regardless of crypto library 66 67 type Signer interface { 68 Sign(msg []byte) (*Signature, error) 69 } 70 71 // Signable is an interface for all signable things. 72 // It typically removes signatures before serializing. 73 type Signable interface { 74 SignBytes(chainID string) ([]byte, error) 75 }