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