-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyStub.go
142 lines (121 loc) · 4.04 KB
/
keyStub.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
package mock
import (
crypto "github.com/multiversx/mx-chain-crypto-go"
)
// PrivateKeyStub provides stubs for a PrivateKey implementation
type PrivateKeyStub struct {
ToByteArrayStub func() ([]byte, error)
GeneratePublicStub func() crypto.PublicKey
ScalarStub func() crypto.Scalar
SuiteStub func() crypto.Suite
}
// PublicKeyStub provides stubs for a PublicKey implementation
type PublicKeyStub struct {
ToByteArrayStub func() ([]byte, error)
PointStub func() crypto.Point
SuiteStub func() crypto.Suite
}
// KeyGenStub mocks a key generation implementation
type KeyGenStub struct {
GeneratePairStub func() (crypto.PrivateKey, crypto.PublicKey)
PrivateKeyFromByteArrayStub func(b []byte) (crypto.PrivateKey, error)
PublicKeyFromByteArrayStub func(b []byte) (crypto.PublicKey, error)
CheckPublicKeyValidStub func(b []byte) error
SuiteStub func() crypto.Suite
}
// ToByteArray returns the byte array representation of the private key
func (privKey *PrivateKeyStub) ToByteArray() ([]byte, error) {
if privKey.ToByteArrayStub != nil {
return privKey.ToByteArrayStub()
}
return []byte{}, nil
}
// GeneratePublic builds a public key for the current private key
func (privKey *PrivateKeyStub) GeneratePublic() crypto.PublicKey {
if privKey.GeneratePublicStub != nil {
return privKey.GeneratePublicStub()
}
return &PublicKeyStub{}
}
// Suite returns the Suite (curve data) used for this private key
func (privKey *PrivateKeyStub) Suite() crypto.Suite {
if privKey.SuiteStub != nil {
return privKey.SuiteStub()
}
return nil
}
// Scalar returns the Scalar corresponding to this Private Key
func (privKey *PrivateKeyStub) Scalar() crypto.Scalar {
if privKey.ScalarStub != nil {
return privKey.ScalarStub()
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (privKey *PrivateKeyStub) IsInterfaceNil() bool {
return privKey == nil
}
// ToByteArray returns the byte array representation of the public key
func (pubKey *PublicKeyStub) ToByteArray() ([]byte, error) {
if pubKey.ToByteArrayStub != nil {
return pubKey.ToByteArrayStub()
}
return []byte("public key"), nil
}
// Suite returns the Suite (curve data) used for this private key
func (pubKey *PublicKeyStub) Suite() crypto.Suite {
if pubKey.SuiteStub != nil {
return pubKey.SuiteStub()
}
return nil
}
// Point returns the Point corresponding to this Public Key
func (pubKey *PublicKeyStub) Point() crypto.Point {
if pubKey.PointStub != nil {
return pubKey.PointStub()
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (pubKey *PublicKeyStub) IsInterfaceNil() bool {
return pubKey == nil
}
// GeneratePair generates a pair of private and public keys
func (keyGen *KeyGenStub) GeneratePair() (crypto.PrivateKey, crypto.PublicKey) {
if keyGen.GeneratePairStub != nil {
return keyGen.GeneratePairStub()
}
return &PrivateKeyStub{}, &PublicKeyStub{}
}
// PrivateKeyFromByteArray generates the private key from its byte array representation
func (keyGen *KeyGenStub) PrivateKeyFromByteArray(b []byte) (crypto.PrivateKey, error) {
if keyGen.PrivateKeyFromByteArrayStub != nil {
return keyGen.PrivateKeyFromByteArrayStub(b)
}
return &PrivateKeyStub{}, nil
}
// PublicKeyFromByteArray generates a public key from its byte array representation
func (keyGen *KeyGenStub) PublicKeyFromByteArray(b []byte) (crypto.PublicKey, error) {
if keyGen.PublicKeyFromByteArrayStub != nil {
return keyGen.PublicKeyFromByteArrayStub(b)
}
return &PublicKeyStub{}, nil
}
// CheckPublicKeyValid verifies the validity of the public key
func (keyGen *KeyGenStub) CheckPublicKeyValid(b []byte) error {
if keyGen.CheckPublicKeyValidStub != nil {
return keyGen.CheckPublicKeyValidStub(b)
}
return nil
}
// Suite -
func (keyGen *KeyGenStub) Suite() crypto.Suite {
if keyGen.SuiteStub != nil {
return keyGen.SuiteStub()
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (keyGen *KeyGenStub) IsInterfaceNil() bool {
return keyGen == nil
}