@@ -22,10 +22,12 @@ import (
22
22
"os/exec"
23
23
"runtime"
24
24
"strings"
25
+ "time"
25
26
26
27
backendPkg "github.com/BitBoxSwiss/bitbox-wallet-app/backend"
27
28
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/arguments"
28
29
btctypes "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/types"
30
+ "github.com/BitBoxSwiss/bitbox-wallet-app/backend/devices/bitbox02/simulator"
29
31
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/devices/usb"
30
32
backendHandlers "github.com/BitBoxSwiss/bitbox-wallet-app/backend/handlers"
31
33
"github.com/BitBoxSwiss/bitbox-wallet-app/util/config"
@@ -69,6 +71,10 @@ func (webdevEnvironment) NotifyUser(text string) {
69
71
70
72
// DeviceInfos implements backend.Environment.
71
73
func (webdevEnvironment ) DeviceInfos () []usb.DeviceInfo {
74
+ if backend .HasTestDevice () {
75
+ // We are in "test device" mode.
76
+ return []usb.DeviceInfo {simulator.DeviceInfo {}}
77
+ }
72
78
return usb .DeviceInfos ()
73
79
}
74
80
@@ -130,6 +136,41 @@ func (webdevEnvironment) DetectDarkTheme() bool {
130
136
return false
131
137
}
132
138
139
+ func handleBitboxSimulator (log * logrus.Entry , simulatorPort int , simulatorVersion string ) error {
140
+ portIsSet := simulatorPort != - 1
141
+ versionIsSet := simulatorVersion != ""
142
+
143
+ if portIsSet != versionIsSet {
144
+ return fmt .Errorf ("flags -simulatorPort and -simulatorVersion must be set together or not at all" )
145
+ }
146
+
147
+ if ! portIsSet {
148
+ return nil
149
+ }
150
+ go func () {
151
+ for {
152
+ if ! simulator .IsRunning (simulatorVersion ) {
153
+ // Simulator not running. Unset test device.
154
+ backend .SetTestDevice (nil )
155
+ continue
156
+ }
157
+ // If the testdevice is already set, do nothing.
158
+ if backend .HasTestDevice () {
159
+ continue
160
+ }
161
+
162
+ // Otherwise, connect to the simulator.
163
+ device , err := simulator .NewDevice (simulatorPort , simulatorVersion )
164
+ if err != nil {
165
+ log .WithError (err ).Fatal ("Could not create new device from running simulator" )
166
+ }
167
+ backend .SetTestDevice (device )
168
+ time .Sleep (time .Millisecond * 50 )
169
+ }
170
+ }()
171
+ return nil
172
+ }
173
+
133
174
func main () {
134
175
config .SetAppDir ("appfolder.dev" )
135
176
@@ -138,6 +179,8 @@ func main() {
138
179
devservers := flag .Bool ("devservers" , true , "switch to dev servers" )
139
180
gapLimitsReceive := flag .Uint ("gapLimitReceive" , 0 , "gap limit for receive addresses" )
140
181
gapLimitsChange := flag .Uint ("gapLimitChange" , 0 , "gap limit for change addresses" )
182
+ simulatorPort := flag .Int ("simulatorPort" , - 1 , "port for the BitBox02 simulator" )
183
+ simulatorVersion := flag .String ("simulatorVersion" , "" , "version of the BitBox02 simulator" )
141
184
flag .Parse ()
142
185
143
186
var gapLimits * btctypes.GapLimits
@@ -161,6 +204,7 @@ func main() {
161
204
log .Info ("--------------- Started application --------------" )
162
205
// since we are in dev-mode, we can drop the authorization token
163
206
connectionData := backendHandlers .NewConnectionData (- 1 , "" )
207
+
164
208
newBackend , err := backendPkg .NewBackend (
165
209
arguments .NewArguments (
166
210
config .AppDir (),
@@ -177,6 +221,11 @@ func main() {
177
221
handlers := backendHandlers .NewHandlers (backend , connectionData )
178
222
log .WithFields (logrus.Fields {"address" : address , "port" : port }).Info ("Listening for HTTP" )
179
223
fmt .Printf ("Listening on: http://localhost:%d\n " , port )
224
+
225
+ if err := handleBitboxSimulator (log , * simulatorPort , * simulatorVersion ); err != nil {
226
+ log .WithError (err ).Fatal ("Invalid simulator flags" )
227
+ }
228
+
180
229
if err := http .ListenAndServe (fmt .Sprintf ("%s:%d" , address , port ), handlers .Router ); err != nil {
181
230
log .WithFields (logrus.Fields {"address" : address , "port" : port , "error" : err .Error ()}).Fatal ("Failed to listen for HTTP" )
182
231
}
0 commit comments