github.com/devwanda/aphelion-staking@v0.33.9/crypto/encoding/amino/amino.go (about) 1 package cryptoamino 2 3 import ( 4 "reflect" 5 6 amino "github.com/evdatsion/go-amino" 7 8 "github.com/devwanda/aphelion-staking/crypto" 9 "github.com/devwanda/aphelion-staking/crypto/ed25519" 10 "github.com/devwanda/aphelion-staking/crypto/multisig" 11 "github.com/devwanda/aphelion-staking/crypto/secp256k1" 12 "github.com/devwanda/aphelion-staking/crypto/sr25519" 13 ) 14 15 var cdc = amino.NewCodec() 16 17 // nameTable is used to map public key concrete types back 18 // to their registered amino names. This should eventually be handled 19 // by amino. Example usage: 20 // nameTable[reflect.TypeOf(ed25519.PubKeyEd25519{})] = ed25519.PubKeyAminoName 21 var nameTable = make(map[reflect.Type]string, 3) 22 23 func init() { 24 // NOTE: It's important that there be no conflicts here, 25 // as that would change the canonical representations, 26 // and therefore change the address. 27 // TODO: Remove above note when 28 // https://github.com/evdatsion/go-amino/issues/9 29 // is resolved 30 RegisterAmino(cdc) 31 32 // TODO: Have amino provide a way to go from concrete struct to route directly. 33 // Its currently a private API 34 nameTable[reflect.TypeOf(ed25519.PubKeyEd25519{})] = ed25519.PubKeyAminoName 35 nameTable[reflect.TypeOf(sr25519.PubKeySr25519{})] = sr25519.PubKeyAminoName 36 nameTable[reflect.TypeOf(secp256k1.PubKeySecp256k1{})] = secp256k1.PubKeyAminoName 37 nameTable[reflect.TypeOf(multisig.PubKeyMultisigThreshold{})] = multisig.PubKeyMultisigThresholdAminoRoute 38 } 39 40 // PubkeyAminoName returns the amino route of a pubkey 41 // cdc is currently passed in, as eventually this will not be using 42 // a package level codec. 43 func PubkeyAminoName(cdc *amino.Codec, key crypto.PubKey) (string, bool) { 44 route, found := nameTable[reflect.TypeOf(key)] 45 return route, found 46 } 47 48 // RegisterAmino registers all crypto related types in the given (amino) codec. 49 func RegisterAmino(cdc *amino.Codec) { 50 // These are all written here instead of 51 cdc.RegisterInterface((*crypto.PubKey)(nil), nil) 52 cdc.RegisterConcrete(ed25519.PubKeyEd25519{}, 53 ed25519.PubKeyAminoName, nil) 54 cdc.RegisterConcrete(sr25519.PubKeySr25519{}, 55 sr25519.PubKeyAminoName, nil) 56 cdc.RegisterConcrete(secp256k1.PubKeySecp256k1{}, 57 secp256k1.PubKeyAminoName, nil) 58 cdc.RegisterConcrete(multisig.PubKeyMultisigThreshold{}, 59 multisig.PubKeyMultisigThresholdAminoRoute, nil) 60 61 cdc.RegisterInterface((*crypto.PrivKey)(nil), nil) 62 cdc.RegisterConcrete(ed25519.PrivKeyEd25519{}, 63 ed25519.PrivKeyAminoName, nil) 64 cdc.RegisterConcrete(sr25519.PrivKeySr25519{}, 65 sr25519.PrivKeyAminoName, nil) 66 cdc.RegisterConcrete(secp256k1.PrivKeySecp256k1{}, 67 secp256k1.PrivKeyAminoName, nil) 68 } 69 70 // RegisterKeyType registers an external key type to allow decoding it from bytes 71 func RegisterKeyType(o interface{}, name string) { 72 cdc.RegisterConcrete(o, name, nil) 73 nameTable[reflect.TypeOf(o)] = name 74 } 75 76 // PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey 77 func PrivKeyFromBytes(privKeyBytes []byte) (privKey crypto.PrivKey, err error) { 78 err = cdc.UnmarshalBinaryBare(privKeyBytes, &privKey) 79 return 80 } 81 82 // PubKeyFromBytes unmarshals public key bytes and returns a PubKey 83 func PubKeyFromBytes(pubKeyBytes []byte) (pubKey crypto.PubKey, err error) { 84 err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey) 85 return 86 }