예제 #1
0
        public void TestGenerate()
        {
            string coin = "BTP";

            //Create an unsigned transaction and encode
            var scanner = new BlockScanner();

            List <string> addresslist = new List <string>()
            {
                "123qWRaufkCnUfh7WMAmJpKdFDw6zBQkn9",
            };
            var mydepaddr = "15Lo7GRtK7b8WaYQeynwRFU479FJBSyewr";

            var claimAddresses = Bitcoin.ParseAddresses(addresslist);
            Tuple <List <ICoin>, Dictionary <string, double> > claimcoins;

            claimcoins = scanner.GetUnspentTransactionOutputs(claimAddresses, coin, out bool usedExplorer, estimate: true);
            var amounts = scanner.CalculateOutputAmounts_Their_My_Fee(claimcoins.Item1, 0.05, 0.0003 * claimcoins.Item1.Count);
            // Generate unsigned tx

            var utx = Bitcoin.GenerateUnsignedTX(claimcoins.Item1, amounts, Bitcoin.ParseAddress(mydepaddr),
                                                 Bitcoin.ParseAddress(mydepaddr),
                                                 coin);

            //var w = Bitcoin.GetBlockData(claimcoins.Item1);

            BlockData bd = new BlockData()
            {
                fork  = coin,
                coins = claimcoins.Item1,
                utx   = utx,
            };

            string bdstr = NBitcoin.JsonConverters.Serializer.ToString(bd);

            System.IO.File.WriteAllText("D:\\ClaimData.txt", bdstr);
            Console.WriteLine(bdstr);
        }
예제 #2
0
        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 }));
        }