github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/crypto/sr25519/pubkey.go (about)

     1  package sr25519
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/oasisprotocol/curve25519-voi/primitives/sr25519"
     8  
     9  	"github.com/ari-anchor/sei-tendermint/crypto"
    10  )
    11  
    12  var _ crypto.PubKey = PubKey{}
    13  
    14  const (
    15  	// PubKeySize is the size of a sr25519 public key in bytes.
    16  	PubKeySize = sr25519.PublicKeySize
    17  
    18  	// SignatureSize is the size of a sr25519 signature in bytes.
    19  	SignatureSize = sr25519.SignatureSize
    20  )
    21  
    22  // PubKey implements crypto.PubKey.
    23  type PubKey []byte
    24  
    25  // TypeTag satisfies the jsontypes.Tagged interface.
    26  func (PubKey) TypeTag() string { return PubKeyName }
    27  
    28  // Address is the SHA256-20 of the raw pubkey bytes.
    29  func (pubKey PubKey) Address() crypto.Address {
    30  	if len(pubKey) != PubKeySize {
    31  		panic("pubkey is incorrect size")
    32  	}
    33  	return crypto.AddressHash(pubKey)
    34  }
    35  
    36  // Bytes returns the PubKey byte format.
    37  func (pubKey PubKey) Bytes() []byte {
    38  	return []byte(pubKey)
    39  }
    40  
    41  func (pubKey PubKey) Equals(other crypto.PubKey) bool {
    42  	if otherSr, ok := other.(PubKey); ok {
    43  		return bytes.Equal(pubKey[:], otherSr[:])
    44  	}
    45  
    46  	return false
    47  }
    48  
    49  func (pubKey PubKey) VerifySignature(msg []byte, sigBytes []byte) bool {
    50  	var srpk sr25519.PublicKey
    51  	if err := srpk.UnmarshalBinary(pubKey); err != nil {
    52  		return false
    53  	}
    54  
    55  	var sig sr25519.Signature
    56  	if err := sig.UnmarshalBinary(sigBytes); err != nil {
    57  		return false
    58  	}
    59  
    60  	st := signingCtx.NewTranscriptBytes(msg)
    61  	return srpk.Verify(st, &sig)
    62  }
    63  
    64  func (pubKey PubKey) Type() string {
    65  	return KeyType
    66  }
    67  
    68  func (pubKey PubKey) String() string {
    69  	return fmt.Sprintf("PubKeySr25519{%X}", []byte(pubKey))
    70  }