github.com/okex/exchain@v1.8.0/libs/tendermint/crypto/sr25519/pubkey.go (about) 1 package sr25519 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "github.com/tendermint/go-amino" 8 9 "github.com/okex/exchain/libs/tendermint/crypto" 10 "github.com/okex/exchain/libs/tendermint/crypto/tmhash" 11 12 schnorrkel "github.com/ChainSafe/go-schnorrkel" 13 ) 14 15 var _ crypto.PubKey = PubKeySr25519{} 16 17 // PubKeySr25519Size is the number of bytes in an Sr25519 public key. 18 const PubKeySr25519Size = 32 19 20 // PubKeySr25519 implements crypto.PubKey for the Sr25519 signature scheme. 21 type PubKeySr25519 [PubKeySr25519Size]byte 22 23 func (pubKey PubKeySr25519) AminoSize(_ *amino.Codec) int { 24 return 1 + PubKeySr25519Size 25 } 26 27 // Address is the SHA256-20 of the raw pubkey bytes. 28 func (pubKey PubKeySr25519) Address() crypto.Address { 29 return crypto.Address(tmhash.SumTruncated(pubKey[:])) 30 } 31 32 // Bytes marshals the PubKey using amino encoding. 33 func (pubKey PubKeySr25519) Bytes() []byte { 34 bz, err := cdc.MarshalBinaryBare(pubKey) 35 if err != nil { 36 panic(err) 37 } 38 return bz 39 } 40 41 func (pubKey PubKeySr25519) VerifyBytes(msg []byte, sig []byte) bool { 42 // make sure we use the same algorithm to sign 43 if len(sig) != SignatureSize { 44 return false 45 } 46 var sig64 [SignatureSize]byte 47 copy(sig64[:], sig) 48 49 publicKey := &(schnorrkel.PublicKey{}) 50 err := publicKey.Decode(pubKey) 51 if err != nil { 52 return false 53 } 54 55 signingContext := schnorrkel.NewSigningContext([]byte{}, msg) 56 57 signature := &(schnorrkel.Signature{}) 58 err = signature.Decode(sig64) 59 if err != nil { 60 return false 61 } 62 63 return publicKey.Verify(signature, signingContext) 64 } 65 66 func (pubKey PubKeySr25519) String() string { 67 return fmt.Sprintf("PubKeySr25519{%X}", pubKey[:]) 68 } 69 70 // Equals - checks that two public keys are the same time 71 // Runs in constant time based on length of the keys. 72 func (pubKey PubKeySr25519) Equals(other crypto.PubKey) bool { 73 if otherEd, ok := other.(PubKeySr25519); ok { 74 return bytes.Equal(pubKey[:], otherEd[:]) 75 } 76 return false 77 }