public async Task <bool> ConnectByIdAsync(string deviceId) { CurrentDevice = null; CurrentDevice = Devices.FirstOrDefault(d => d.Id.Equals(deviceId)); if (CurrentDevice != null) { return(true); } using (var cts = new CancellationTokenSource()) { var connectionTask = adapter.ConnectToKnownDeviceAsync(new Guid(deviceId), cancellationToken: cts.Token); uint delayCount = 0; while ((!connectionTask.IsCompleted) && (delayCount < 50)) { await Task.Delay(100); delayCount++; } if (connectionTask.IsCompleted) { CurrentDevice = new SoterDeviceBle(connectionTask.Result); return(true); } else { cts.Cancel(); return(false); } } }
public static void SaveCurrentDeviceToDb(ISoterDevice device) { if (_currentDevice == null) { LoadWalletDevices(); } using (var db = new DatabaseContext()) { if (!string.IsNullOrWhiteSpace(_currentDevice.Name) && _currentDevice.Name.Equals(device.Name)) { _currentDevice.BleGuid = device.Id; _currentDevice.Label = device.Features.Label; _currentDevice.Uuid = device.Features.DeviceId; _currentDevice.Initialized = device.Features.Initialized; _currentDevice.Model = device.Features.Model; _currentDevice.BootloaderHash = device.Features.BootloaderHash.ToHex(); _currentDevice.FirmwareHash = device.Features.FirmwareHash.ToHex(); _currentDevice.Language = device.Features.Language; _currentDevice.MajorVersion = device.Features.MajorVersion; _currentDevice.MinorVersion = device.Features.MinorVersion; _currentDevice.PatchVersion = device.Features.PatchVersion; db.WalletDevices.Update(_currentDevice); db.SaveChanges(); } else { db.WalletDevices.Add(new WalletDevice { Name = device.Name, BleGuid = device.Id, Label = device.Features.Label, Uuid = device.Features.DeviceId, Initialized = device.Features.Initialized, Model = device.Features.Model, BootloaderHash = device.Features.BootloaderHash.ToHex(), FirmwareHash = device.Features.FirmwareHash.ToHex(), Language = device.Features.Language, MajorVersion = device.Features.MajorVersion, MinorVersion = device.Features.MinorVersion, PatchVersion = device.Features.PatchVersion, }); db.SaveChanges(); _currentDevice = db.WalletDevices.First(w => w.Name.Equals(device.Name)); } } }
public async Task <bool> ConnectByNameAsync(string deviceName) { uint waitCount = 0; await StartDeviceSearchAsync(); while (!Devices.Any(d => d.Name.Equals(deviceName)) && (waitCount < 30)) { await Task.Delay(100); waitCount++; } await StopDeviceSearchAsync(); CurrentDevice = Devices.FirstOrDefault(d => d.Name.Equals(deviceName)); return(CurrentDevice != null); }
public static async Task LoadCoinTableFromDeviceAsync(ISoterDevice device) { using (var db = new DatabaseContext()) { db.Database.EnsureCreated(); db.Transactions.Clear(); db.Addresses.Clear(); db.Coins.Clear(); var coinTable = await device.GetCoinTableAsync(48); device.CoinUtility = new CoinUtility(coinTable); foreach (var coinType in coinTable) { if (SupportedCoins.Any(c => c.Equals(coinType.CoinShortcut))) { db.Coins.Add(new Coin(coinType) { WalletDeviceId = _currentDevice.Id }); } } db.SaveChanges(); foreach (var coin in db.Coins) { var addressPath = new BIP44AddressPath(coin.Segwit, AddressUtilities.UnhardenNumber(coin.Bip44AccountPath), 0, false, 0); var addressStr = await device.GetAddressAsync((IAddressPath)addressPath, false, false); var address = new Address() { CoinId = coin.Id, Account = addressPath.Account, Change = addressPath.Change, AddressIndex = addressPath.AddressIndex, AddressString = addressStr, CoinType = addressPath.CoinType, Purpose = addressPath.Purpose, }; db.Addresses.Add(address); } db.SaveChanges(); } }
protected override async void OnAppearing() { base.OnAppearing(); Device.StartTimer(TimeSpan.FromSeconds(0.3), () => { canvasView.InvalidateSurface(); lock (drawStageLock) { if (drawStage < 0) { return(false); } drawStage++; if (drawStage > MAX_DRAW_STAGE) { drawStage = 0; } } return(true); // True = Repeat again, False = Stop the timer }); if (Device.RuntimePlatform == Device.Android) { var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location); if (status != PermissionStatus.Granted) { if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location)) { await DisplayAlert("Need Location Permission", "Soter Wallet needs Location Permission in order to search for the device.", "OK"); } var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location); //Best practice to always check that the key exists if (results.ContainsKey(Permission.Location)) { status = results[Permission.Location]; } } if (status != PermissionStatus.Granted) { await DisplayAlert("Location Permission Denied", "Can not continue, try again.", "OK"); } } ISoterDevice device = null; await SoterDeviceFactoryBle.Instance.StartDeviceSearchAsync(); Log.Information("Wait for search results"); await Task.Delay(500); Log.Information("Wait finished"); await SoterDeviceFactoryBle.Instance.StopDeviceSearchAsync(); Log.Information("Search stopped"); if (SoterDeviceFactoryBle.Instance.Devices.Count == 0) { await DisplayAlert("Error", "Can't find any Soter Wallet device!", "OK"); } else if (SoterDeviceFactoryBle.Instance.Devices.Count == 1) { device = SoterDeviceFactoryBle.Instance.Devices.First(); } else { var action = await DisplayActionSheet("Select the device", "Cancel", null, SoterDeviceFactoryBle.Instance.Devices.Select(d => d.Name).ToArray()); device = SoterDeviceFactoryBle.Instance.Devices.FirstOrDefault(d => d.Name.Equals(action)); } if (device != null) { try { await SoterDeviceFactoryBle.Instance.ConnectByIdAsync(device.Id); await device.InitializeAsync(); lock (drawStageLock) { drawStage = -1; } if (device.Features.Initialized) { await DeviceCommPage.UpdateCoinTable(this); Application.Current.MainPage = new NavigationPage(new MainTabbedPage()); return; } else { Application.Current.MainPage = new NavigationPage(new DeviceLabelPage()); return; } } catch (Exception ex) { Log.Error(ex.ToString()); await DisplayAlert("Error", "Can't connect to the Soter Wallet!", "OK"); } } Application.Current.MainPage = new StartPairingPage(); }
static async Task ResetDevice() { IEnumerable <CoinType> coinTable; await SoterDeviceFactoryHid.Instance.StartDeviceSearchAsync(); await Task.Delay(1000); await SoterDeviceFactoryHid.Instance.StopDeviceSearchAsync(); if (SoterDeviceFactoryHid.Instance.Devices.Count == 0) { throw new Exception("Do Soter Wallet device detected!"); } _soterDevice = (SoterDeviceHid)SoterDeviceFactoryHid.Instance.Devices.FirstOrDefault(); _soterDevice.EnterPinCallback = _soterDevice_EnterPinCallback;; await _soterDevice.InitializeAsync(); await _soterDevice.CancelAsync(); if (_soterDevice.Features.Initialized) { coinTable = await _soterDevice.GetCoinTableAsync(24); _soterDevice.CoinUtility = new CoinUtility(coinTable); //Get Bitcoin Testnet Address Log.Information(await GetAddressAsync(1, false, 0, false, false, false)); await _soterDevice.WipeDeviceAsync(); } await _soterDevice.ResetDeviceAsync("Digbig Wallet"); await _soterDevice.InitializeAsync(); if (!_soterDevice.Features.Initialized) { Log.Error("Reset Device Failed!!!"); return; } await _soterDevice.ChangePinAsync(); await _soterDevice.ChangeAutoLockDelayAsync(1200000); await _soterDevice.ChangeDeviceNameAsync("Test Wallet"); coinTable = await _soterDevice.GetCoinTableAsync(); _soterDevice.CoinUtility = new CoinUtility(coinTable); //Get Bitcoin Address Log.Information(await GetAddressAsync(0, false, 0, false)); Log.Information(await GetAddressAsync(0, false, 0, false, true, false)); Log.Information(await GetAddressAsync(0, false, 0, false, true, true)); Log.Information(await GetAddressAsync(0, false, 0, true)); //Get Litecoin Address Log.Information(await GetAddressAsync(2, false, 0, false)); //Get Dodge Address Log.Information(await GetAddressAsync(3, false, 0, false)); //Get Dash Address Log.Information(await GetAddressAsync(5, false, 0, false)); //Get Ethereum Address Log.Information(await GetAddressAsync(60, false, 0, false)); //Get BitcoinCash Address Log.Information(await GetAddressAsync(145, false, 0, false)); //await SignBitcoinTransactionAsync(); Log.Information("All Done!"); }