-
Notifications
You must be signed in to change notification settings - Fork 114
/
signature.go
344 lines (279 loc) · 11.6 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package ledgerstate
import (
"bytes"
"github.com/cockroachdb/errors"
"github.com/iotaledger/hive.go/byteutils"
"github.com/iotaledger/hive.go/cerrors"
"github.com/iotaledger/hive.go/crypto/bls"
"github.com/iotaledger/hive.go/crypto/ed25519"
"github.com/iotaledger/hive.go/marshalutil"
"github.com/iotaledger/hive.go/stringify"
"github.com/mr-tron/base58"
"golang.org/x/crypto/blake2b"
)
// region SignatureType ////////////////////////////////////////////////////////////////////////////////////////////////
const (
// ED25519SignatureType represents an ED25519 Signature.
ED25519SignatureType SignatureType = iota
// BLSSignatureType represents a BLS Signature.
BLSSignatureType
)
// SignatureType represents the type of the signature scheme.
type SignatureType uint8
// String returns a human readable representation of the SignatureType.
func (s SignatureType) String() string {
return [...]string{
"ED25519SignatureType",
"BLSSignatureType",
}[s]
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region Signature ////////////////////////////////////////////////////////////////////////////////////////////////////
// Signature is an interface for the different kinds of Signatures that are supported by the ledger state.
type Signature interface {
// Type returns the SignatureType of this Signature.
Type() SignatureType
// SignatureValid returns true if the Signature signs the given data.
SignatureValid(data []byte) bool
// AddressSignatureValid returns true if the Signature signs the given Address.
AddressSignatureValid(address Address, data []byte) bool
// Bytes returns a marshaled version of the Signature.
Bytes() []byte
// Base58 returns a base58 encoded version of the Signature.
Base58() string
// String returns a human readable version of the Signature.
String() string
}
// SignatureFromBytes unmarshals a Signature from a sequence of bytes.
func SignatureFromBytes(bytes []byte) (signature Signature, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes)
if signature, err = SignatureFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse Signature from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// SignatureFromBase58EncodedString creates a Signature from a base58 encoded string.
func SignatureFromBase58EncodedString(base58String string) (signature Signature, err error) {
decodedBytes, err := base58.Decode(base58String)
if err != nil {
err = errors.Errorf("error while decoding base58 encoded Signature (%v): %w", err, cerrors.ErrBase58DecodeFailed)
return
}
if signature, _, err = SignatureFromBytes(decodedBytes); err != nil {
err = errors.Errorf("failed to parse Signature from bytes: %w", err)
return
}
return
}
// SignatureFromMarshalUtil unmarshals a Signature using a MarshalUtil (for easier unmarshaling).
func SignatureFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (signature Signature, err error) {
signatureType, err := marshalUtil.ReadByte()
if err != nil {
err = errors.Errorf("failed to parse SignatureType (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
marshalUtil.ReadSeek(-1)
switch SignatureType(signatureType) {
case ED25519SignatureType:
if signature, err = ED25519SignatureFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse ED25519Signature: %w", err)
return
}
case BLSSignatureType:
if signature, err = BLSSignatureFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse BLSSignature: %w", err)
return
}
default:
err = errors.Errorf("unsupported SignatureType (%X): %w", signatureType, cerrors.ErrParseBytesFailed)
return
}
return
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region ED25519Signature /////////////////////////////////////////////////////////////////////////////////////////////
// ED25519Signature represents a Signature created with the ed25519 signature scheme.
type ED25519Signature struct {
PublicKey ed25519.PublicKey
Signature ed25519.Signature
}
// NewED25519Signature is the constructor of an ED25519Signature.
func NewED25519Signature(publicKey ed25519.PublicKey, signature ed25519.Signature) *ED25519Signature {
return &ED25519Signature{
PublicKey: publicKey,
Signature: signature,
}
}
// ED25519SignatureFromBytes unmarshals a ED25519Signature from a sequence of bytes.
func ED25519SignatureFromBytes(bytes []byte) (signature *ED25519Signature, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes)
if signature, err = ED25519SignatureFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse ED25519Signature from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// ED25519SignatureFromBase58EncodedString creates an ED25519Signature from a base58 encoded string.
func ED25519SignatureFromBase58EncodedString(base58String string) (signature *ED25519Signature, err error) {
decodedBytes, err := base58.Decode(base58String)
if err != nil {
err = errors.Errorf("error while decoding base58 encoded ED25519Signature (%v): %w", err, cerrors.ErrBase58DecodeFailed)
return
}
if signature, _, err = ED25519SignatureFromBytes(decodedBytes); err != nil {
err = errors.Errorf("failed to parse ED25519Signature from bytes: %w", err)
return
}
return
}
// ED25519SignatureFromMarshalUtil unmarshals a ED25519Signature using a MarshalUtil (for easier unmarshaling).
func ED25519SignatureFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (signature *ED25519Signature, err error) {
signatureType, err := marshalUtil.ReadByte()
if err != nil {
err = errors.Errorf("failed to parse SignatureType (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
if SignatureType(signatureType) != ED25519SignatureType {
err = errors.Errorf("invalid SignatureType (%X): %w", signatureType, cerrors.ErrParseBytesFailed)
return
}
signature = &ED25519Signature{}
if signature.PublicKey, err = ed25519.ParsePublicKey(marshalUtil); err != nil {
err = errors.Errorf("failed to parse public key (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
if signature.Signature, err = ed25519.ParseSignature(marshalUtil); err != nil {
err = errors.Errorf("failed to parse signature (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
return
}
// Type returns the SignatureType of this Signature.
func (e *ED25519Signature) Type() SignatureType {
return ED25519SignatureType
}
// SignatureValid returns true if the Signature signs the given data.
func (e *ED25519Signature) SignatureValid(data []byte) bool {
return e.PublicKey.VerifySignature(data, e.Signature)
}
// AddressSignatureValid returns true if the Signature signs the given Address.
func (e *ED25519Signature) AddressSignatureValid(address Address, data []byte) bool {
if address.Type() != ED25519AddressType {
return false
}
hashedPublicKey := blake2b.Sum256(e.PublicKey.Bytes())
if !bytes.Equal(hashedPublicKey[:], address.Digest()) {
return false
}
return e.SignatureValid(data)
}
// Bytes returns a marshaled version of the Signature.
func (e *ED25519Signature) Bytes() []byte {
return byteutils.ConcatBytes([]byte{byte(ED25519SignatureType)}, e.PublicKey.Bytes(), e.Signature.Bytes())
}
// Base58 returns a base58 encoded version of the Signature.
func (e *ED25519Signature) Base58() string {
return base58.Encode(e.Bytes())
}
// String returns a human readable version of the Signature.
func (e *ED25519Signature) String() string {
return stringify.Struct("ED25519Signature",
stringify.StructField("publicKey", e.PublicKey),
stringify.StructField("signature", e.Signature),
)
}
// code contract (make sure the type implements all required methods)
var _ Signature = &ED25519Signature{}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region BLSSignature /////////////////////////////////////////////////////////////////////////////////////////////////
// BLSSignature represents a Signature created with the BLS signature scheme.
type BLSSignature struct {
Signature bls.SignatureWithPublicKey
}
// NewBLSSignature is the constructor of a BLSSignature.
func NewBLSSignature(signature bls.SignatureWithPublicKey) *BLSSignature {
return &BLSSignature{
Signature: signature,
}
}
// BLSSignatureFromBytes unmarshals a BLSSignature from a sequence of bytes.
func BLSSignatureFromBytes(bytes []byte) (signature *BLSSignature, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes)
if signature, err = BLSSignatureFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse BLSSignature from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// BLSSignatureFromBase58EncodedString creates a BLSSignature from a base58 encoded string.
func BLSSignatureFromBase58EncodedString(base58String string) (signature *BLSSignature, err error) {
decodedBytes, err := base58.Decode(base58String)
if err != nil {
err = errors.Errorf("error while decoding base58 encoded BLSSignature (%v): %w", err, cerrors.ErrBase58DecodeFailed)
return
}
if signature, _, err = BLSSignatureFromBytes(decodedBytes); err != nil {
err = errors.Errorf("failed to parse BLSSignature from bytes: %w", err)
return
}
return
}
// BLSSignatureFromMarshalUtil unmarshals a BLSSignature using a MarshalUtil (for easier unmarshaling).
func BLSSignatureFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (signature *BLSSignature, err error) {
signatureType, err := marshalUtil.ReadByte()
if err != nil {
err = errors.Errorf("failed to parse SignatureType (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
if SignatureType(signatureType) != BLSSignatureType {
err = errors.Errorf("invalid SignatureType (%X): %w", signatureType, cerrors.ErrParseBytesFailed)
return
}
signature = &BLSSignature{}
if signature.Signature, err = bls.SignatureWithPublicKeyFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse SignatureWithPublicKey from MarshalUtil (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
return
}
// Type returns the SignatureType of this Signature.
func (b *BLSSignature) Type() SignatureType {
return BLSSignatureType
}
// SignatureValid returns true if the Signature signs the given data.
func (b *BLSSignature) SignatureValid(data []byte) bool {
return b.Signature.IsValid(data)
}
// AddressSignatureValid returns true if the Signature signs the given Address.
func (b *BLSSignature) AddressSignatureValid(address Address, data []byte) bool {
if address.Type() != BLSAddressType {
return false
}
hashedPublicKey := blake2b.Sum256(b.Signature.PublicKey.Bytes())
if !bytes.Equal(hashedPublicKey[:], address.Digest()) {
return false
}
return b.SignatureValid(data)
}
// Bytes returns a marshaled version of the Signature.
func (b *BLSSignature) Bytes() []byte {
return byteutils.ConcatBytes([]byte{byte(BLSSignatureType)}, b.Signature.Bytes())
}
// Base58 returns a base58 encoded version of the Signature.
func (b *BLSSignature) Base58() string {
return base58.Encode(b.Bytes())
}
// String returns a human readable version of the Signature.
func (b *BLSSignature) String() string {
return stringify.Struct("BLSSignature",
stringify.StructField("publicKey", b.Signature.PublicKey),
stringify.StructField("signature", b.Signature.Signature),
)
}
// code contract (make sure the type implements all required methods)
var _ Signature = &BLSSignature{}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////