public async Task <IBcnCredentialsRecord> GenerateWallets(Guid clientId) { var network = _btcSettings.NetworkType == NetworkType.Main ? Network.Main : Network.TestNet; var wallets = await GetWalletsForPubKey(); IBcnCredentialsRecord bcnCreds; var currentWalletCreds = await _walletCredentialsRepository.GetAsync(clientId); if (currentWalletCreds == null) { var btcConvertionWallet = GetNewAddressAndPrivateKey(network); IWalletCredentials walletCreds = WalletCredentials.Create( clientId.ToString(), null, null, null, wallets.ColoredMultiSigAddress, btcConvertionWallet.PrivateKey, btcConvertionWallet.Address, encodedPk: "", pubKey: ""); bcnCreds = BcnCredentialsRecord.Create(SpecialAssetIds.BitcoinAssetId, clientId.ToString(), null, wallets.SegwitAddress, ""); await Task.WhenAll( _walletCredentialsRepository.SaveAsync(walletCreds), _walletCredentialsRepository.SaveAsync(bcnCreds) ); } else { //walletCreds = WalletCredentials.Create( // clientId.ToString(), // null, // null, // null, // wallets.ColoredMultiSigAddress, // null, // null, // encodedPk: "", // pubKey: ""); bcnCreds = await _walletCredentialsRepository.GetBcnCredsAsync(SpecialAssetIds.BitcoinAssetId, clientId); if (bcnCreds == null) { bcnCreds = BcnCredentialsRecord.Create( SpecialAssetIds.BitcoinAssetId, clientId.ToString(), null, wallets.SegwitAddress, "" ); await _walletCredentialsRepository.SaveAsync(bcnCreds); } //await _walletCredentialsHistoryRepository.InsertHistoryRecord(currentWalletCreds); //await _walletCredentialsRepository.Merge(walletCreds); } return(bcnCreds); }
private static async Task Execute(string bitcoinCashDataConnString, string settingsUrl, string bitcoinCashNetwork, string blockchainType) { BCash.Instance.EnsureRegistered(); var network = Network.GetNetwork(bitcoinCashNetwork); var bcashNetwork = network == Network.Main ? BCash.Instance.Mainnet : BCash.Instance.Regtest; var logConsole = LogFactory.Create().AddConsole(); var settings = new SettingsServiceReloadingManager <AppSettings>(settingsUrl, p => {}); var walletRepo = BlockchainWalletsRepository.Create(settings.Nested(p => p.BlockchainWalletsService.Db.DataConnString), logConsole); var firstGenerationBlockchainWalletRepository = FirstGenerationBlockchainWalletRepository.Create(settings.Nested(x => x.BlockchainWalletsService.Db.ClientPersonalInfoConnString), logConsole); var observableWalletsRepo = ObservableWalletRepository.Create(new ReloadingManagerAdapter <string>(bitcoinCashDataConnString), logConsole); var addressValidator = new AddressValidator(network, bcashNetwork); Console.WriteLine("Retrieving observable wallets"); var observableWallets = (await observableWalletsRepo.GetAll()).ToList(); Console.WriteLine("Processing items"); var counter = 0; foreach (var observableWallet in observableWallets) { counter++; var address = addressValidator.GetBitcoinAddress(observableWallet.Address); if (address == null) { throw new ArgumentException($"Unrecognized address {observableWallet.Address}", nameof(observableWallet.Address)); } var oldAdrr = address.ScriptPubKey.GetDestinationAddress(network).ToString(); var newAddr = address.ScriptPubKey.GetDestinationAddress(bcashNetwork).ToString(); Console.WriteLine($"Processing {observableWallet.Address}:{oldAdrr}:{newAddr} -- {counter} of {observableWallets.Count}"); var wallet = await walletRepo.TryGetAsync(blockchainType, oldAdrr); if (wallet == null) { var prevColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"Wallet not found {observableWallet.Address} -{blockchainType}. Queued by {oldAdrr}"); Console.ForegroundColor = prevColor; continue; } var oldCredsRecord = new BcnCredentialsRecord { Address = string.Empty, AssetAddress = oldAdrr, ClientId = wallet.ClientId.ToString(), EncodedKey = string.Empty, PublicKey = string.Empty, AssetId = $"{blockchainType} ({wallet.AssetId})" }; var newCredsRecord = new BcnCredentialsRecord { Address = string.Empty, AssetAddress = newAddr, ClientId = wallet.ClientId.ToString(), EncodedKey = string.Empty, PublicKey = string.Empty, AssetId = $"{blockchainType} ({wallet.AssetId})" }; await firstGenerationBlockchainWalletRepository.DeleteIfExistAsync(oldCredsRecord); await firstGenerationBlockchainWalletRepository.InsertOrReplaceAsync(newCredsRecord); await walletRepo.DeleteIfExistsAsync(wallet.BlockchainType, wallet.ClientId, oldAdrr); await walletRepo.AddAsync(wallet.BlockchainType, wallet.ClientId, newAddr, wallet.CreatorType); } Console.WriteLine("All done"); }
public async Task SetSolarCoinWallet(Guid clientId, string address) { await SaveAsync(BcnCredentialsRecord.Create(SpecialAssetIds.SolarAssetId, clientId.ToString(), null, address, null)); }