-
Notifications
You must be signed in to change notification settings - Fork 5
/
mocked_flow_export_public.go
180 lines (155 loc) · 4.94 KB
/
mocked_flow_export_public.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
package statuskeycardgo
import (
"math/rand"
"strconv"
"strings"
"github.com/status-im/status-keycard-go/signal"
)
func (mkf *MockedKeycardFlow) handleExportPublicFlow() {
flowStatus := FlowStatus{}
if mkf.insertedKeycard.NotStatusKeycard {
flowStatus[ErrorKey] = ErrorNotAKeycard
flowStatus[InstanceUID] = ""
flowStatus[KeyUID] = ""
flowStatus[FreeSlots] = 0
mkf.state = Paused
signal.Send(SwapCard, flowStatus)
return
}
flowStatus = FlowStatus{
InstanceUID: mkf.insertedKeycard.InstanceUID,
KeyUID: mkf.insertedKeycard.KeyUID,
}
if mkf.insertedKeycard.InstanceUID == "" || mkf.insertedKeycard.KeyUID == "" {
flowStatus[ErrorKey] = ErrorNoKeys
flowStatus[FreeSlots] = 0
mkf.state = Paused
signal.Send(SwapCard, flowStatus)
return
}
var (
enteredPIN string
enteredNewPIN string
enteredPUK string
exportMaster bool
exportPrivate bool
)
if v, ok := mkf.params[PIN]; ok {
enteredPIN = v.(string)
}
if v, ok := mkf.params[NewPIN]; ok {
enteredNewPIN = v.(string)
}
if v, ok := mkf.params[PUK]; ok {
enteredPUK = v.(string)
}
if v, ok := mkf.params[ExportMaster]; ok {
exportMaster = v.(bool)
}
if v, ok := mkf.params[ExportPriv]; ok {
exportPrivate = v.(bool)
}
finalType := EnterPIN
if mkf.insertedKeycard.PukRetries == 0 {
flowStatus[ErrorKey] = PUKRetries
finalType = SwapCard
} else {
if mkf.insertedKeycard.PinRetries == 0 {
if len(enteredPUK) == defPUKLen {
if len(enteredPIN) == defPINLen && enteredPIN == enteredNewPIN {
if enteredPUK != mkf.insertedKeycard.Puk {
mkf.insertedKeycard.PukRetries--
if mkf.insertedKeycard.PukRetries == 0 {
flowStatus[ErrorKey] = PUKRetries
finalType = SwapCard
} else {
flowStatus[ErrorKey] = PUK
finalType = EnterPUK
}
}
} else {
flowStatus[ErrorKey] = ErrorUnblocking
finalType = EnterNewPIN
}
} else {
flowStatus[ErrorKey] = ""
finalType = EnterPUK
}
} else {
if len(enteredNewPIN) == 0 && len(enteredPIN) == defPINLen && enteredPIN != mkf.insertedKeycard.Pin {
mkf.insertedKeycard.PinRetries--
flowStatus[ErrorKey] = PIN
finalType = EnterPIN
if mkf.insertedKeycard.PinRetries == 0 {
flowStatus[ErrorKey] = ""
finalType = EnterPUK
}
}
}
}
if mkf.insertedKeycard.PinRetries > 0 && len(enteredPIN) == defPINLen && enteredPIN == mkf.insertedKeycard.Pin ||
mkf.insertedKeycard.PinRetries == 0 && mkf.insertedKeycard.PukRetries > 0 && len(enteredPUK) == defPUKLen &&
enteredPUK == mkf.insertedKeycard.Puk && len(enteredPIN) == defPINLen && enteredPIN == enteredNewPIN {
mkf.insertedKeycard.PinRetries = maxPINRetries
mkf.insertedKeycard.PukRetries = maxPUKRetries
mkf.insertedKeycard.Pin = enteredPIN
if exportMaster {
if mkf.insertedKeycardHelper.MasterKeyAddress == "" {
iAsStr := strconv.Itoa(rand.Intn(100) + 100)
mkf.insertedKeycardHelper.MasterKeyAddress = "0x" + strings.Repeat("0", 40-len(iAsStr)) + iAsStr
}
flowStatus[MasterAddr] = mkf.insertedKeycardHelper.MasterKeyAddress
}
if path, ok := mkf.params[BIP44Path]; ok {
if mkf.insertedKeycardHelper.ExportedKey == nil {
mkf.insertedKeycardHelper.ExportedKey = make(map[string]KeyPair)
}
if pathStr, ok := path.(string); ok {
keyPair, _ := mkf.insertedKeycardHelper.ExportedKey[pathStr]
if keyPair.Address == "" {
keyPair.Address = "0x" + strings.Repeat("0", 39) + "1"
}
if len(keyPair.PublicKey) == 0 {
keyPair.PublicKey = []byte(strings.Repeat("0", 129) + "1")
}
if !exportPrivate {
keyPair.PrivateKey = []byte("")
} else if len(keyPair.PrivateKey) == 0 {
keyPair.PrivateKey = []byte(strings.Repeat("0", 63) + "1")
}
mkf.insertedKeycardHelper.ExportedKey[pathStr] = keyPair
flowStatus[ExportedKey] = keyPair
} else if paths, ok := path.([]interface{}); ok {
keys := make([]*KeyPair, len(paths))
for i, path := range paths {
keyPair, _ := mkf.insertedKeycardHelper.ExportedKey[path.(string)]
if keyPair.Address == "" {
iAsStr := strconv.Itoa(i + 1)
keyPair.Address = "0x" + strings.Repeat("0", 40-len(iAsStr)) + iAsStr
}
if len(keyPair.PublicKey) == 0 {
iAsStr := strconv.Itoa(i + 1)
keyPair.PublicKey = []byte(strings.Repeat("0", 130-len(iAsStr)) + iAsStr)
}
if !exportPrivate {
keyPair.PrivateKey = []byte("")
} else if len(keyPair.PrivateKey) == 0 {
iAsStr := strconv.Itoa(i + 1)
keyPair.PrivateKey = []byte(strings.Repeat("0", 64-len(iAsStr)) + iAsStr)
}
mkf.insertedKeycardHelper.ExportedKey[path.(string)] = keyPair
keys[i] = &keyPair
}
flowStatus[ExportedKey] = keys
}
}
mkf.state = Idle
signal.Send(FlowResult, flowStatus)
return
}
flowStatus[FreeSlots] = mkf.insertedKeycard.FreePairingSlots
flowStatus[PINRetries] = mkf.insertedKeycard.PinRetries
flowStatus[PUKRetries] = mkf.insertedKeycard.PukRetries
mkf.state = Paused
signal.Send(finalType, flowStatus)
}