private async Task <bool> RestoreWalletAsync(int apiPort, string chainName, string walletName) { Console.WriteLine($"You have chosen to restore your {chainName} wallet."); string mnemonic; string passphrase; string password; do { Console.WriteLine($"Please enter your 12-Words used to recover your wallet:"); mnemonic = Console.ReadLine(); Console.WriteLine("Please enter your wallet passphrase:"); passphrase = Console.ReadLine(); Console.WriteLine("Please enter the wallet password used to encrypt the wallet:"); password = Console.ReadLine(); if (!string.IsNullOrEmpty(mnemonic) && !string.IsNullOrEmpty(passphrase) && !string.IsNullOrEmpty(password)) { break; } Console.WriteLine("ERROR: Please ensure that you enter all the wallet details."); } while (true); var walletRecoveryRequest = new WalletRecoveryRequest() { CreationDate = new DateTime(2020, 11, 1), Mnemonic = mnemonic, Name = walletName, Passphrase = passphrase, Password = password }; try { await $"http://localhost:{apiPort}/api".AppendPathSegment("wallet/recover").PostJsonAsync(walletRecoveryRequest); } catch (Exception ex) { Console.WriteLine($"ERROR: An exception occurred trying to recover your {chainName} wallet: {ex}"); return(false); } WalletInfoModel walletInfoModel = await $"http://localhost:{apiPort}/api".AppendPathSegment("Wallet/list-wallets").GetJsonAsync <WalletInfoModel>(); if (walletInfoModel.WalletNames.Contains(walletName)) { Console.WriteLine($"SUCCESS: {chainName} wallet has been restored."); } else { Console.WriteLine($"ERROR: {chainName} wallet failed to be restored, exiting the registration process."); return(false); } try { Console.WriteLine($"Your {chainName} wallet will now be resynced, please be patient..."); var walletSyncRequest = new WalletSyncRequest() { All = true, WalletName = walletName }; await $"http://localhost:{apiPort}/api".AppendPathSegment("wallet/sync-from-date").PostJsonAsync(walletSyncRequest); } catch (Exception ex) { Console.WriteLine($"ERROR: An exception occurred trying to resync your {chainName} wallet: {ex}"); return(false); } return(true); }
private async Task <bool> CheckWalletRequirementsAsync(NodeType nodeType, int apiPort) { var chainName = nodeType == NodeType.MainChain ? "STRAX" : "CIRRUS"; var amountToCheck = nodeType == NodeType.MainChain ? CollateralRequirement : FeeRequirement; var chainTicker = nodeType == NodeType.MainChain ? this.mainchainNetwork.CoinTicker : this.sidechainNetwork.CoinTicker; Console.WriteLine($"Please enter the name of the {chainName} wallet that contains the required collateral of {amountToCheck} {chainTicker}:"); var walletName = Console.ReadLine(); WalletInfoModel walletInfoModel = await $"http://localhost:{apiPort}/api".AppendPathSegment("Wallet/list-wallets").GetJsonAsync <WalletInfoModel>(); if (walletInfoModel.WalletNames.Contains(walletName)) { Console.WriteLine($"SUCCESS: Wallet with name '{chainName}' found."); } else { Console.WriteLine($"{chainName} wallet with name '{walletName}' does not exist."); ConsoleKeyInfo key; do { Console.WriteLine($"Would you like to restore you {chainName} wallet that holds the required amount of {amountToCheck} {chainTicker} now? Enter (Y) to continue or (N) to exit."); key = Console.ReadKey(); if (key.Key == ConsoleKey.Y || key.Key == ConsoleKey.N) { break; } } while (true); if (key.Key == ConsoleKey.N) { Console.WriteLine($"You have chosen to exit the registration script."); return(false); } if (!await RestoreWalletAsync(apiPort, chainName, walletName)) { return(false); } } // Check wallet height (sync) status. do { var walletNameRequest = new WalletName() { Name = walletName }; WalletGeneralInfoModel walletInfo = await $"http://localhost:{apiPort}/api".AppendPathSegment("wallet/general-info").SetQueryParams(walletNameRequest).GetJsonAsync <WalletGeneralInfoModel>(); StatusModel blockModel = await $"http://localhost:{apiPort}/api".AppendPathSegment("node/status").GetJsonAsync <StatusModel>(); if (walletInfo.LastBlockSyncedHeight > (blockModel.ConsensusHeight - 50)) { Console.WriteLine($"{chainName} wallet is synced."); break; } Console.WriteLine($"Syncing {chainName} wallet, current height {walletInfo.LastBlockSyncedHeight}..."); await Task.Delay(TimeSpan.FromSeconds(3)); } while (true); // Check wallet balance. try { var walletBalanceRequest = new WalletBalanceRequest() { WalletName = walletName }; WalletBalanceModel walletBalanceModel = await $"http://localhost:{apiPort}/api" .AppendPathSegment("wallet/balance") .SetQueryParams(walletBalanceRequest) .GetJsonAsync <WalletBalanceModel>(); if (walletBalanceModel.AccountsBalances[0].SpendableAmount / 100000000 > amountToCheck) { Console.WriteLine($"SUCCESS: The {chainName} wallet contains the required amount of {amountToCheck} {chainTicker}."); return(true); } Console.WriteLine($"ERROR: The {chainName} wallet does not contain the required amount of {amountToCheck} {chainTicker}."); } catch (Exception ex) { Console.WriteLine($"ERROR: An exception occurred trying to check the wallet balance: {ex}"); } return(false); }