-
Notifications
You must be signed in to change notification settings - Fork 15
/
signer_test.go
194 lines (165 loc) · 5 KB
/
signer_test.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
package aptos
import (
"github.com/aptos-labs/aptos-go-sdk/crypto"
"math/rand/v2"
)
/* This is a collection of test signers, that don't make sense in the real world, but are used for testing */
type MultiEd25519TestSigner struct {
Keys []*crypto.Ed25519PrivateKey
SignaturesRequired uint8
}
func NewMultiEd25519Signer(numKeys uint8, signaturesRequired uint8) (*MultiEd25519TestSigner, error) {
keys := make([]*crypto.Ed25519PrivateKey, numKeys)
for i := range keys {
key, err := crypto.GenerateEd25519PrivateKey(nil)
if err != nil {
return nil, err
}
keys[i] = key
}
return &MultiEd25519TestSigner{
Keys: keys,
SignaturesRequired: signaturesRequired,
}, nil
}
func (s *MultiEd25519TestSigner) AccountAddress() AccountAddress {
address := AccountAddress{}
address.FromAuthKey(s.AuthKey())
return address
}
func (s *MultiEd25519TestSigner) Sign(msg []byte) (authenticator *crypto.AccountAuthenticator, err error) {
signature, err := s.SignMessage(msg)
if err != nil {
return nil, err
}
return &crypto.AccountAuthenticator{
Variant: crypto.AccountAuthenticatorMultiEd25519,
Auth: &crypto.MultiEd25519Authenticator{
PubKey: s.PubKey().(*crypto.MultiEd25519PublicKey),
Sig: signature.(*crypto.MultiEd25519Signature),
},
}, nil
}
func (s *MultiEd25519TestSigner) SignMessage(msg []byte) (crypto.Signature, error) {
signatures := make([]*crypto.Ed25519Signature, s.SignaturesRequired)
for i := 0; i < int(s.SignaturesRequired); i++ {
sig, err := s.Keys[i].SignMessage(msg)
if err != nil {
return nil, err
}
signatures[i] = sig.(*crypto.Ed25519Signature)
}
return &crypto.MultiEd25519Signature{
Signatures: signatures,
Bitmap: [4]byte{},
}, nil
}
func (s *MultiEd25519TestSigner) AuthKey() *crypto.AuthenticationKey {
return s.PubKey().AuthKey()
}
func (s *MultiEd25519TestSigner) PubKey() crypto.PublicKey {
pubKeys := make([]*crypto.Ed25519PublicKey, len(s.Keys))
for i, key := range s.Keys {
pubKeys[i] = key.PubKey().(*crypto.Ed25519PublicKey)
}
key := &crypto.MultiEd25519PublicKey{
PubKeys: pubKeys,
SignaturesRequired: s.SignaturesRequired,
}
return key
}
// This is an example for testing, a real signer would be signing and collecting the signatures in a different way
type MultiKeyTestSigner struct {
Signers []crypto.Signer
SignaturesRequired uint8
MultiKey *crypto.MultiKey
}
func NewMultiKeyTestSigner(numKeys uint8, signaturesRequired uint8) (*MultiKeyTestSigner, error) {
signers := make([]crypto.Signer, numKeys)
for i := range signers {
switch i % 2 {
case 0:
signer, err := crypto.GenerateEd25519PrivateKey()
if err != nil {
return nil, err
}
// Wrap in a SingleSigner
signers[i] = &crypto.SingleSigner{Signer: signer}
case 1:
signer, err := crypto.GenerateSecp256k1Key()
if err != nil {
return nil, err
}
// Wrap in a SingleSigner
signers[i] = &crypto.SingleSigner{Signer: signer}
}
}
pubKeys := make([]*crypto.AnyPublicKey, len(signers))
for i, key := range signers {
pubKeys[i] = key.PubKey().(*crypto.AnyPublicKey)
}
pubKey := &crypto.MultiKey{
PubKeys: pubKeys,
SignaturesRequired: signaturesRequired,
}
return &MultiKeyTestSigner{
Signers: signers,
SignaturesRequired: signaturesRequired,
MultiKey: pubKey,
}, nil
}
func (s *MultiKeyTestSigner) AccountAddress() AccountAddress {
address := AccountAddress{}
address.FromAuthKey(s.AuthKey())
return address
}
func (s *MultiKeyTestSigner) Sign(msg []byte) (authenticator *crypto.AccountAuthenticator, err error) {
signature, err := s.SignMessage(msg)
if err != nil {
return nil, err
}
return &crypto.AccountAuthenticator{
Variant: crypto.AccountAuthenticatorMultiKey,
Auth: &crypto.MultiKeyAuthenticator{
PubKey: s.PubKey().(*crypto.MultiKey),
Sig: signature.(*crypto.MultiKeySignature),
},
}, nil
}
func (s *MultiKeyTestSigner) SignMessage(msg []byte) (crypto.Signature, error) {
indexedSigs := make([]crypto.IndexedAnySignature, s.SignaturesRequired)
alreadyUsed := make(map[int]bool)
for i := uint8(0); i < s.SignaturesRequired; i++ {
// Find a random key
index := 0
for {
index = rand.IntN(len(s.Signers))
_, present := alreadyUsed[index]
if !present {
alreadyUsed[index] = true
break
}
}
sig, err := s.Signers[index].SignMessage(msg)
if err != nil {
return nil, err
}
indexedSigs[i] = crypto.IndexedAnySignature{Signature: sig.(*crypto.AnySignature), Index: uint8(index)}
}
return crypto.NewMultiKeySignature(indexedSigs)
}
func (s *MultiKeyTestSigner) SimulationAuthenticator() *crypto.AccountAuthenticator {
return &crypto.AccountAuthenticator{
Variant: crypto.AccountAuthenticatorMultiKey,
Auth: &crypto.MultiKeyAuthenticator{
PubKey: s.PubKey().(*crypto.MultiKey),
Sig: &crypto.MultiKeySignature{},
},
}
}
func (s *MultiKeyTestSigner) AuthKey() *crypto.AuthenticationKey {
return s.PubKey().AuthKey()
}
func (s *MultiKeyTestSigner) PubKey() crypto.PublicKey {
return s.MultiKey
}