diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c3fa11..14585d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed - Change `protobuf` file definitions from http://github.com/skycoin/hardware-wallet-protob.git to http://github.com/fibercrypto/skywallet-go.git +- Be able to set the emulator ip as argument. ### Removed diff --git a/src/skywallet/helper.go b/src/skywallet/helper.go index 0cb629a..596bf99 100644 --- a/src/skywallet/helper.go +++ b/src/skywallet/helper.go @@ -88,15 +88,23 @@ func initUsb() []usb.Bus { } // NewDriver create a new device driver -func NewDriver(deviceType DeviceType) (*Driver, error) { +func NewDriver(deviceType DeviceType, emulatorAddress ...string) (*Driver, error) { switch deviceType { case DeviceTypeUSB: + if len(emulatorAddress) > 0 { + log.WithField("emulatorAddress", emulatorAddress).Warningln("ip addresses ignored as this does not make sense for physical device") + } return &Driver{ deviceType: deviceType, bus: usb.Init(initUsb()...), }, nil case DeviceTypeEmulator: - udpBus, err := usb.InitUDP([]int{EmulatorPort}) + if len(emulatorAddress) == 0 { + emulatorAddress = []string{"127.0.0.1"} + } else if len(emulatorAddress) > 1 { + return nil, WrrInvalidArgCountForEmulatorIpAddress + } + udpBus, err := usb.InitUDP([]int{EmulatorPort}, emulatorAddress[0]) if err != nil { return nil, err } diff --git a/src/skywallet/skywallet.go b/src/skywallet/skywallet.go index 09a0ca7..416ee2e 100644 --- a/src/skywallet/skywallet.go +++ b/src/skywallet/skywallet.go @@ -46,6 +46,8 @@ var ( ErrDeviceTypeEmulator = errors.New("device type cannot be emulator") // ErrInvalidWordCount is returned if word count is not valid mnemonic word length ErrInvalidWordCount = errors.New("word count must be 12 or 24") + // WrrInvalidArgCountForEmulatorIpAddress emulator ip address is the only one expected argument for emulatorAddress + WrrInvalidArgCountForEmulatorIpAddress = errors.New("emulator ip address is the only one expected argument for emulatorAddress") // ErrNoDeviceConnected is returned if no device is connected to the system ErrNoDeviceConnected = errors.New("no device connected") // ErrInvalidWalletType a valid wallet type should be specified @@ -141,8 +143,8 @@ func DeviceTypeFromString(deviceType string) DeviceType { var devSingleCreator sync.Once var devSingleInstance *Device -func newDevice(deviceType DeviceType) *Device { - driver, err := NewDriver(deviceType) +func newDevice(deviceType DeviceType, emulatorAddress ...string) *Device { + driver, err := NewDriver(deviceType, emulatorAddress...) if err != nil { log.Fatalf("failed to create driver: %s", err) } @@ -158,11 +160,11 @@ func newDevice(deviceType DeviceType) *Device { } // NewDevice returns a new device instance -func NewDevice(deviceType DeviceType) *Device { +func NewDevice(deviceType DeviceType, emulatorAddress ...string) *Device { // TODO rename NewDevice to DeviceInstance as this is a singleton // implementation. devSingleCreator.Do(func() { - devSingleInstance = newDevice(deviceType) + devSingleInstance = newDevice(deviceType, emulatorAddress...) }) return devSingleInstance } diff --git a/src/skywallet/usb/udp.go b/src/skywallet/usb/udp.go index 56e6249..a990aa8 100644 --- a/src/skywallet/usb/udp.go +++ b/src/skywallet/usb/udp.go @@ -1,6 +1,7 @@ package usb import ( + "fmt" "io" "net" "strconv" @@ -10,16 +11,17 @@ import ( const ( emulatorPrefix = "emulator" - emulatorAddress = "127.0.0.1" ) type UDP struct { ports []int + emulatorAddress string } -func InitUDP(ports []int) (*UDP, error) { +func InitUDP(ports []int, emulatorAddress string) (*UDP, error) { udp := UDP{ ports: ports, + emulatorAddress: emulatorAddress, } return &udp, nil @@ -51,7 +53,7 @@ func (udp *UDP) Connect(path string) (Device, error) { return nil, err } - address := emulatorAddress + ":" + strconv.Itoa(port) + address := fmt.Sprintf("%s:%d", udp.emulatorAddress, port) dev, err := net.Dial("udp", address) if err != nil { return nil, err