OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / tendermint / go-crypto / pub_key.go
1 package crypto
2
3 import (
4         "bytes"
5
6         "github.com/tendermint/ed25519"
7         data "github.com/tendermint/go-wire/data"
8         . "github.com/tendermint/tmlibs/common"
9 )
10
11 //----------------------------------------
12
13 // DO NOT USE THIS INTERFACE.
14 // You probably want to use PubKey
15 // +gen wrapper:"PubKey,Impl[PubKeyEd25519,PubKeySecp256k1],ed25519,secp256k1"
16 type PubKeyInner interface {
17         KeyString() string
18         VerifyBytes(msg []byte, sig Signature) bool
19         Equals(PubKey) bool
20         Wrap() PubKey
21 }
22
23 //-------------------------------------
24
25 var _ PubKeyInner = PubKeyEd25519{}
26
27 // Implements PubKeyInner
28 type PubKeyEd25519 [32]byte
29
30 func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
31         // make sure we use the same algorithm to sign
32         sig, ok := sig_.Unwrap().(SignatureEd25519)
33         if !ok {
34                 return false
35         }
36         pubKeyBytes := [32]byte(pubKey)
37         sigBytes := [64]byte(sig)
38         return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
39 }
40
41 func (p PubKeyEd25519) MarshalJSON() ([]byte, error) {
42         return data.Encoder.Marshal(p[:])
43 }
44
45 func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error {
46         var ref []byte
47         err := data.Encoder.Unmarshal(&ref, enc)
48         copy(p[:], ref)
49         return err
50 }
51
52 func (pubKey PubKeyEd25519) String() string {
53         return Fmt("PubKeyEd25519{%X}", pubKey[:])
54 }
55
56 // Must return the full bytes in hex.
57 // Used for map keying, etc.
58 func (pubKey PubKeyEd25519) KeyString() string {
59         return Fmt("%X", pubKey[:])
60 }
61
62 func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
63         if otherEd, ok := other.Unwrap().(PubKeyEd25519); ok {
64                 return bytes.Equal(pubKey[:], otherEd[:])
65         } else {
66                 return false
67         }
68 }