public ActionResult CoinBalance(string coin, string addresses) { List <string> addressList = new List <string>( addresses.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)); var invalid = addressList.Where(a => !Bitcoin.IsValidAddress(a)).ToList(); var addressesToCheck = addressList.Except(invalid).ToList(); var scanner = new BlockScanner(); var claimAddresses = Bitcoin.ParseAddresses(addressesToCheck); Tuple <List <ICoin>, Dictionary <string, double> > claimcoins; bool usedExplorer = false; double TotalValue = 0.0; bool searchError = false; Dictionary <string, double> balances; try { claimcoins = scanner.GetUnspentTransactionOutputs(claimAddresses, coin, out usedExplorer); TotalValue = Convert.ToDouble(claimcoins.Item1.Sum(o => ((Money)o.Amount).ToDecimal(MoneyUnit.BTC))); balances = claimcoins.Item2; } catch (Exception e) { balances = new Dictionary <string, double>(); searchError = true; } using (CoinpanicContext db = new CoinpanicContext()) { db.IndexCoinInfo.Where(i => i.Coin == coin).ToList(); } AddressSummary result = new AddressSummary() { InvalidAddresses = invalid, CoinName = BitcoinForks.ForkByShortName[coin].LongName, Empty = TotalValue <= 0, Coin = coin, Balance = Convert.ToString(TotalValue), UsedExplorer = usedExplorer, Addresses = addressesToCheck, SearchError = searchError, }; return(PartialView(result)); }
public ActionResult InitializeClaim(string claimId, string PublicKeys, string depositAddress, string emailAddress) { using (CoinpanicContext db = new CoinpanicContext()) { CoinClaim userclaim = db.Claims.Where(c => c.ClaimId == claimId).Include(c => c.InputAddresses).First(); // Clean up input depositAddress = depositAddress.Replace("\n", String.Empty); depositAddress = depositAddress.Replace("\r", String.Empty); depositAddress = depositAddress.Replace("\t", String.Empty); depositAddress = depositAddress.Trim().Replace(" ", ""); userclaim.DepositAddress = depositAddress; userclaim.Email = emailAddress; List <string> list = new List <string>( PublicKeys.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)); if (list.Count < 1) { return(RedirectToAction("ClaimError", new { message = "You must enter at least one address to claim", claimId = claimId })); } // Convert depositAddress if BCH if (userclaim.CoinShortName == "BCH" && depositAddress.StartsWith("bitcoincash:")) { // Convert depositAddress = SharpCashAddr.Converter.cashAddrToOldAddr(depositAddress, out bool isP2PKH, out _); } if (!Bitcoin.IsValidAddress(depositAddress, userclaim.CoinShortName, BitcoinForks.ForkByShortName[userclaim.CoinShortName].Network)) { return(RedirectToAction("ClaimError", new { message = "Deposit Address not valid", claimId = claimId })); } var invalid = list.Where(a => !Bitcoin.IsValidAddress(a)); if (invalid.Count() > 0) { return(RedirectToAction("ClaimError", new { message = String.Join(", ", invalid) + (invalid.Count() < 2 ? " is" : " are") + " invalid.", claimId = claimId })); } var scanner = new BlockScanner(); var claimAddresses = Bitcoin.ParseAddresses(list); Tuple <List <ICoin>, Dictionary <string, double> > claimcoins; try { claimcoins = scanner.GetUnspentTransactionOutputs(claimAddresses, userclaim.CoinShortName, out bool usedExplorer); } catch (Exception e) { return(RedirectToAction("ClaimError", new { message = "Error searching for your addresses in the blockchain", claimId = claimId })); } var amounts = scanner.CalculateOutputAmounts_Their_My_Fee(claimcoins.Item1, 0.05, 0.0003 * claimcoins.Item1.Count); var balances = claimcoins.Item2; // Return converted addresses to user List <InputAddress> inputs; inputs = GetInputAddresses(userclaim, list, balances); userclaim.InputAddresses = inputs; userclaim.Deposited = Convert.ToDouble(amounts[0].ToDecimal(MoneyUnit.BTC)); userclaim.MyFee = Convert.ToDouble(amounts[1].ToDecimal(MoneyUnit.BTC)); userclaim.MinerFee = Convert.ToDouble(amounts[2].ToDecimal(MoneyUnit.BTC)); userclaim.TotalValue = userclaim.Deposited + userclaim.MyFee + userclaim.MinerFee; userclaim.InitializeDate = DateTime.Now; if (userclaim.Deposited < 0) { userclaim.Deposited = 0; } if (userclaim.MyFee < 0) { userclaim.MyFee = 0; } // Generate unsigned tx var mydepaddr = ConfigurationManager.AppSettings[userclaim.CoinShortName + "Deposit"]; if (userclaim.CoinShortName == "BCH" && mydepaddr.StartsWith("bitcoincash:")) { // Convert mydepaddr = SharpCashAddr.Converter.cashAddrToOldAddr(mydepaddr, out bool isP2PKH, out _); } var utx = Bitcoin.GenerateUnsignedTX( UTXOs: claimcoins.Item1, amounts: amounts, clientDepAddr: Bitcoin.ParseAddress(depositAddress, userclaim.CoinShortName, BitcoinForks.ForkByShortName[userclaim.CoinShortName].Network), MyDepositAddr: Bitcoin.ParseAddress(mydepaddr, userclaim.CoinShortName, BitcoinForks.ForkByShortName[userclaim.CoinShortName].Network), forkShortName: userclaim.CoinShortName); userclaim.UnsignedTX = utx; // Generate witness data var w = Bitcoin.GetBlockData(claimcoins.Item1); userclaim.BlockData = w; // New format of message BlockData bd = new BlockData() { fork = userclaim.CoinShortName, coins = claimcoins.Item1, utx = utx, addresses = balances.Select(kvp => kvp.Key).ToList(), }; string bdstr = NBitcoin.JsonConverters.Serializer.ToString(bd); userclaim.ClaimData = bdstr; db.SaveChanges(); MonitoringService.SendMessage("New " + userclaim.CoinShortName + " claim", "new claim Initialized. https://www.coinpanic.com/Claim/ClaimConfirm?claimId=" + claimId + " " + " for " + userclaim.CoinShortName); } return(RedirectToAction("ClaimConfirm", new { claimId = claimId })); }