github.com/evdatsion/aphelion-dpos-bft@v0.32.1/crypto/encoding/amino/amino.go (about)

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