OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / go-crypto / signature.go
1 package crypto
2
3 import (
4         "bytes"
5         "fmt"
6
7         "github.com/tendermint/go-wire"
8         data "github.com/tendermint/go-wire/data"
9         . "github.com/tendermint/tmlibs/common"
10 )
11
12 func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
13         err = wire.ReadBinaryBytes(sigBytes, &sig)
14         return
15 }
16
17 //----------------------------------------
18
19 // DO NOT USE THIS INTERFACE.
20 // You probably want to use Signature.
21 // +gen wrapper:"Signature,Impl[SignatureEd25519,SignatureSecp256k1],ed25519,secp256k1"
22 type SignatureInner interface {
23         AssertIsSignatureInner()
24         Bytes() []byte
25         IsZero() bool
26         Equals(Signature) bool
27         Wrap() Signature
28 }
29
30 //-------------------------------------
31
32 var _ SignatureInner = SignatureEd25519{}
33
34 // Implements Signature
35 type SignatureEd25519 [64]byte
36
37 func (sig SignatureEd25519) AssertIsSignatureInner() {}
38
39 func (sig SignatureEd25519) Bytes() []byte {
40         return wire.BinaryBytes(Signature{sig})
41 }
42
43 func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
44
45 func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
46
47 func (sig SignatureEd25519) Equals(other Signature) bool {
48         if otherEd, ok := other.Unwrap().(SignatureEd25519); ok {
49                 return bytes.Equal(sig[:], otherEd[:])
50         } else {
51                 return false
52         }
53 }
54
55 func (sig SignatureEd25519) MarshalJSON() ([]byte, error) {
56         return data.Encoder.Marshal(sig[:])
57 }
58
59 func (sig *SignatureEd25519) UnmarshalJSON(enc []byte) error {
60         var ref []byte
61         err := data.Encoder.Unmarshal(&ref, enc)
62         copy(sig[:], ref)
63         return err
64 }
65
66 func SignatureEd25519FromBytes(data []byte) Signature {
67         var sig SignatureEd25519
68         copy(sig[:], data)
69         return sig.Wrap()
70 }
71
72 //-------------------------------------
73
74 var _ SignatureInner = SignatureSecp256k1{}
75
76 // Implements Signature
77 type SignatureSecp256k1 []byte
78
79 func (sig SignatureSecp256k1) AssertIsSignatureInner() {}
80
81 func (sig SignatureSecp256k1) Bytes() []byte {
82         return wire.BinaryBytes(Signature{sig})
83 }
84
85 func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
86
87 func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
88
89 func (sig SignatureSecp256k1) Equals(other Signature) bool {
90         if otherEd, ok := other.Unwrap().(SignatureSecp256k1); ok {
91                 return bytes.Equal(sig[:], otherEd[:])
92         } else {
93                 return false
94         }
95 }
96
97 func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) {
98         return data.Encoder.Marshal(sig)
99 }
100
101 func (sig *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
102         return data.Encoder.Unmarshal((*[]byte)(sig), enc)
103 }