/
signature.go
167 lines (135 loc) · 3.76 KB
/
signature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package types
import (
"encoding/hex"
"fmt"
"math/big"
"github.com/realForbis/continuum-go-sdk/pkg/ed25519"
"github.com/realForbis/continuum-go-sdk/pkg/util"
"github.com/tinylib/msgp/msgp"
)
func init() {
msgp.RegisterExtension(SignatureExtensionType, func() msgp.Extension { return new(Signature) })
}
const (
//SignatureSize size of signature
SignatureSize = ed25519.SignatureSize
)
var ZeroSignature = Signature{}
// Signature of block
type Signature [SignatureSize]byte
func BytesToSignature(b []byte) (Signature, error) {
var sign Signature
if len(b) != ed25519.SignatureSize {
return ZeroSignature, fmt.Errorf("invalid signature size[%d]", len(b))
}
copy(sign[:], b)
return sign, nil
}
func NewSignature(hexStr string) (Signature, error) {
s := Signature{}
err := s.Of(hexStr)
if err != nil {
return s, err
}
return s, nil
}
//IsZero check signature is zero
func (s *Signature) IsZero() bool {
for _, b := range s {
if b != 0 {
return false
}
}
return true
}
// String implements the fmt.Stringer interface.
func (s Signature) String() string {
return hex.EncodeToString(s[:])
}
//Of convert hex string to Signature
func (s *Signature) Of(hexString string) error {
ss := util.TrimQuotes(hexString)
size := hex.DecodedLen(len(ss))
if size != SignatureSize {
return fmt.Errorf("bad signature size: %d", size)
}
var signature [SignatureSize]byte
if _, err := hex.Decode(signature[:], []byte(ss)); err != nil {
return err
}
*s = signature
return nil
}
//ExtensionType implements Extension.ExtensionType interface
func (s *Signature) ExtensionType() int8 {
return SignatureExtensionType
}
//ExtensionType implements Extension.Len interface
func (s *Signature) Len() int {
return SignatureSize
}
//ExtensionType implements Extension.MarshalBinaryTo interface
func (s Signature) MarshalBinaryTo(text []byte) error {
copy(text, s[:])
return nil
}
//ExtensionType implements Extension.UnmarshalBinary interface
func (s *Signature) UnmarshalBinary(text []byte) error {
size := len(text)
if len(text) != SignatureSize {
return fmt.Errorf("bad signature size: %d", size)
}
copy((*s)[:], text)
return nil
}
////MarshalJSON implements json.Marshaler interface
//func (s Signature) MarshalJSON() ([]byte, error) {
// return []byte(s.String()), nil
//}
//
//func (s *Signature) UnmarshalJSON(b []byte) error {
// return s.Of(string(b))
//}
func (s *Signature) UnmarshalText(text []byte) error {
return s.Of(string(text))
}
// MarshalText implements the encoding.TextMarshaler interface.
func (s Signature) MarshalText() (text []byte, err error) {
return []byte(s.String()), nil
}
func (s Signature) Bytes() []byte {
return s[:]
}
// ToBigInt converts a types.Signature into a big.Int that can be used to
// perform math comparisons.
func (s *Signature) ToBigInt() *big.Int {
// A Signature is in little-endian, but the big package wants the bytes in
// big-endian, so reverse them.
var sigBuf [SignatureSize]byte
copy(sigBuf[:], s[:])
buf := sigBuf
blen := len(buf)
for i := 0; i < blen/2; i++ {
buf[i], buf[blen-1-i] = buf[blen-1-i], buf[i]
}
//fmt.Println("ToBigInt", hex.EncodeToString(s[:]), hex.EncodeToString(sigBuf[:]), hex.EncodeToString(buf[:]))
return new(big.Int).SetBytes(buf[:])
}
// FromBigInt converts a big.Int into a types.Signature.
func (s *Signature) FromBigInt(num *big.Int) error {
// A big.Int is in big-endian, but a Signature is in little-endian, so reverse them.
numBuf := num.Bytes()
if len(numBuf) > SignatureSize {
return fmt.Errorf("bad big.Int bytes size: %d", len(numBuf))
}
var sigBuf [SignatureSize]byte
startPos := SignatureSize - len(numBuf)
copy(sigBuf[startPos:], numBuf)
buf := sigBuf
blen := len(buf)
for i := 0; i < blen/2; i++ {
buf[i], buf[blen-1-i] = buf[blen-1-i], buf[i]
}
*s = buf
return nil
}