-
Notifications
You must be signed in to change notification settings - Fork 110
maintenance: make BBApp able to talk to simulator. #3560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Copyright 2025 Shift Devices AG | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package simulator | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
"os/exec" | ||
"time" | ||
|
||
"github.com/BitBoxSwiss/bitbox-wallet-app/util/logging" | ||
bitbox02common "github.com/BitBoxSwiss/bitbox02-api-go/api/common" | ||
bitbox02firmware "github.com/BitBoxSwiss/bitbox02-api-go/api/firmware" | ||
"github.com/BitBoxSwiss/bitbox02-api-go/communication/u2fhid" | ||
"github.com/BitBoxSwiss/bitbox02-api-go/util/semver" | ||
) | ||
|
||
var testDeviceInfo *deviceInfo | ||
|
||
// Init processes the simulator flags, and starts a goroutine to monitor the simulator process | ||
// and update the testDeviceInfo accordingly. | ||
func Init(simulatorPort int) { | ||
go func() { | ||
log := logging.Get().WithGroup("simulator") | ||
for { | ||
if !isRunning(simulatorPort) { | ||
// Simulator not running. Unset test device info. | ||
testDeviceInfo = nil | ||
continue | ||
} | ||
// If the testdevice info is already set, do nothing. | ||
if testDeviceInfo != nil { | ||
continue | ||
} | ||
|
||
var err error | ||
// Otherwise, set the test device info. | ||
testDeviceInfo, err = newDeviceInfo(simulatorPort) | ||
if err != nil { | ||
log.WithError(err).Error("Failed to get simulator device info") | ||
testDeviceInfo = nil | ||
} | ||
time.Sleep(50 * time.Millisecond) | ||
} | ||
}() | ||
} | ||
|
||
// isRunning checks if a process is listening at the provided port. | ||
func isRunning(port int) bool { | ||
cmd := exec.Command("lsof", "-i", fmt.Sprintf(":%d", port), "-sTCP:LISTEN") | ||
if err := cmd.Run(); err != nil { | ||
return false // No process listening on the port | ||
} | ||
return true | ||
} | ||
|
||
// TestDeviceInfo returns the deviceInfo of the running simulator, or nil if no simulator is running. | ||
func TestDeviceInfo() *deviceInfo { | ||
return testDeviceInfo | ||
} | ||
|
||
// deviceInfo implements usb.deviceInfo for the simulator. | ||
type deviceInfo struct { | ||
port int | ||
version semver.SemVer | ||
product bitbox02common.Product | ||
} | ||
|
||
func newDeviceInfo(simulatorPort int) (*deviceInfo, error) { | ||
info := deviceInfo{ | ||
port: simulatorPort, | ||
} | ||
hidDevice, err := info.Open() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { | ||
if err := hidDevice.Close(); err != nil { | ||
logging.Get().WithError(err).Error("Failed to close hidDevice") | ||
} | ||
}() | ||
|
||
bitboxCMD := 0x80 + 0x40 + 0x01 | ||
version, product, _, err := bitbox02firmware.Info(u2fhid.NewCommunication(hidDevice, byte(bitboxCMD))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
info.version = *version | ||
info.product = product | ||
return &info, nil | ||
} | ||
|
||
// IsBluetooth implements usb.DeviceInfo. | ||
func (d deviceInfo) IsBluetooth() bool { | ||
return false | ||
} | ||
|
||
// VendorID implements usb.DeviceInfo. | ||
func (d deviceInfo) VendorID() int { | ||
return 0x03eb | ||
} | ||
|
||
// ProductID implements usb.DeviceInfo. | ||
func (d deviceInfo) ProductID() int { | ||
return 0x2403 | ||
} | ||
|
||
// UsagePage implements usb.DeviceInfo. | ||
func (d deviceInfo) UsagePage() int { | ||
return 0xfffff | ||
} | ||
|
||
// Interface implements usb.DeviceInfo. | ||
func (d deviceInfo) Interface() int { | ||
return 0 | ||
} | ||
|
||
// Serial implements usb.DeviceInfo. | ||
func (d deviceInfo) Serial() string { | ||
return fmt.Sprintf("v%s", d.version.String()) | ||
} | ||
|
||
// Product implements usb.DeviceInfo. | ||
func (d deviceInfo) Product() string { | ||
switch d.product { | ||
case bitbox02common.ProductBitBox02Multi: | ||
return bitbox02common.FirmwareDeviceProductStringBitBox02Multi | ||
case bitbox02common.ProductBitBox02BTCOnly: | ||
return bitbox02common.FirmwareDeviceProductStringBitBox02BTCOnly | ||
case bitbox02common.ProductBitBox02PlusMulti: | ||
return bitbox02common.FirmwareDeviceProductStringBitBox02PlusMulti | ||
case bitbox02common.ProductBitBox02PlusBTCOnly: | ||
return bitbox02common.FirmwareDeviceProductStringBitBox02PlusBTCOnly | ||
default: | ||
panic("unrecognized product") | ||
} | ||
} | ||
|
||
// Identifier implements usb.DeviceInfo. | ||
func (d deviceInfo) Identifier() string { | ||
return "bitbox02-simulator" | ||
} | ||
|
||
// Open implements usb.DeviceInfo. | ||
func (d deviceInfo) Open() (io.ReadWriteCloser, error) { | ||
var err error | ||
var conn net.Conn | ||
for range 200 { | ||
conn, err = net.Dial("tcp", fmt.Sprintf("localhost:%d", d.port)) | ||
if err == nil { | ||
break | ||
} | ||
time.Sleep(50 * time.Millisecond) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
return conn, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 7 additions & 4 deletions
11
vendor/github.com/BitBoxSwiss/bitbox02-api-go/api/firmware/device.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
vendor/github.com/BitBoxSwiss/bitbox02-api-go/api/firmware/psbt.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.