-
Notifications
You must be signed in to change notification settings - Fork 175
/
signatures.go
91 lines (73 loc) · 1.86 KB
/
signatures.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
package request
import (
"fmt"
"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/util"
"github.com/onflow/flow-go/model/flow"
)
type TransactionSignature flow.TransactionSignature
func (s *TransactionSignature) Parse(
rawAddress string,
rawKeyIndex string,
rawSignature string,
chain flow.Chain,
) error {
address, err := ParseAddress(rawAddress, chain)
if err != nil {
return err
}
keyIndex, err := util.ToUint64(rawKeyIndex)
if err != nil {
return fmt.Errorf("invalid key index: %w", err)
}
var signature Signature
err = signature.Parse(rawSignature)
if err != nil {
return fmt.Errorf("invalid signature: %w", err)
}
*s = TransactionSignature(flow.TransactionSignature{
Address: address,
KeyIndex: keyIndex,
Signature: signature,
})
return nil
}
func (s TransactionSignature) Flow() flow.TransactionSignature {
return flow.TransactionSignature(s)
}
type TransactionSignatures []TransactionSignature
func (t *TransactionSignatures) Parse(rawSigs []models.TransactionSignature, chain flow.Chain) error {
signatures := make([]TransactionSignature, len(rawSigs))
for i, sig := range rawSigs {
var signature TransactionSignature
err := signature.Parse(sig.Address, sig.KeyIndex, sig.Signature, chain)
if err != nil {
return err
}
signatures[i] = signature
}
*t = signatures
return nil
}
func (t TransactionSignatures) Flow() []flow.TransactionSignature {
sigs := make([]flow.TransactionSignature, len(t))
for i, sig := range t {
sigs[i] = sig.Flow()
}
return sigs
}
type Signature []byte
func (s *Signature) Parse(raw string) error {
if raw == "" {
return fmt.Errorf("missing value")
}
signatureBytes, err := util.FromBase64(raw)
if err != nil {
return fmt.Errorf("invalid encoding")
}
*s = signatureBytes
return nil
}
func (s Signature) Flow() []byte {
return s
}