- package sr25519
-
- import (
- "bytes"
- "fmt"
-
- "github.com/tendermint/tendermint/crypto"
- "github.com/tendermint/tendermint/crypto/tmhash"
-
- schnorrkel "github.com/ChainSafe/go-schnorrkel"
- )
-
- var _ crypto.PubKey = PubKey{}
-
- // PubKeySize is the number of bytes in an Sr25519 public key.
- const PubKeySize = 32
-
- // PubKey implements crypto.PubKey for the Sr25519 signature scheme.
- type PubKey [PubKeySize]byte
-
- // Address is the SHA256-20 of the raw pubkey bytes.
- func (pubKey PubKey) Address() crypto.Address {
- return crypto.Address(tmhash.SumTruncated(pubKey[:]))
- }
-
- // Bytes marshals the PubKey using amino encoding.
- func (pubKey PubKey) Bytes() []byte {
- bz, err := cdc.MarshalBinaryBare(pubKey)
- if err != nil {
- panic(err)
- }
- return bz
- }
-
- func (pubKey PubKey) VerifyBytes(msg []byte, sig []byte) bool {
- // make sure we use the same algorithm to sign
- if len(sig) != SignatureSize {
- return false
- }
- var sig64 [SignatureSize]byte
- copy(sig64[:], sig)
-
- publicKey := &(schnorrkel.PublicKey{})
- err := publicKey.Decode(pubKey)
- if err != nil {
- return false
- }
-
- signingContext := schnorrkel.NewSigningContext([]byte{}, msg)
-
- signature := &(schnorrkel.Signature{})
- err = signature.Decode(sig64)
- if err != nil {
- return false
- }
-
- return publicKey.Verify(signature, signingContext)
- }
-
- func (pubKey PubKey) String() string {
- return fmt.Sprintf("PubKey{%X}", pubKey[:])
- }
-
- // Equals - checks that two public keys are the same time
- // Runs in constant time based on length of the keys.
- func (pubKey PubKey) Equals(other crypto.PubKey) bool {
- if otherEd, ok := other.(PubKey); ok {
- return bytes.Equal(pubKey[:], otherEd[:])
- }
- return false
- }
|