示例#1
0
        static async Task GetOneBlock(NBitcoin.RPC.RPCClient rpcC, int index)
        {
            var block = await rpcC.GetBlockAsync(index);

            Console.WriteLine("block 123 hashquick=" + block.Header.HashMerkleRoot.ToString());
            Console.WriteLine("tran count=" + block.Transactions.Count);
            for (var i = 0; i < block.Transactions.Count; i++)
            {
                var tran = block.Transactions[i];
                Console.WriteLine("==tran " + i + "==:" + tran.GetHash());
                Console.WriteLine("--Input--");
                for (var vi = 0; vi < tran.Inputs.Count; vi++)
                {
                    Console.WriteLine("Input" + vi + ":  ref=" + tran.Inputs[vi].PrevOut.Hash.ToString() + " n=" + tran.Inputs[vi].PrevOut.N.ToString("X08"));
                }
                Console.WriteLine("--Output--");
                for (var vo = 0; vo < tran.Outputs.Count; vo++)
                {
                    var vout    = tran.Outputs[vo];
                    var address = vout.ScriptPubKey.GetDestinationAddress(rpcC.Network);//注意比特币地址和网络有关,testnet 和mainnet地址不通用
                    userAddrList.ForEach(x =>
                    {
                        if (address.ToString() == x)
                        {
                            Console.WriteLine("Have a transfer for :" + address);
                        }
                    });
                    Console.WriteLine("Output" + vo + ":  recvier=" + address + " money=" + vout.Value.ToString());
                }
            }
        }
示例#2
0
 public static NBitcoin.RPC.RPCClient GetLocalRPCClient()
 {
     if (_rpcclient == null)
     {
         NBitcoin.RPC.RPCCredentialString r = new NBitcoin.RPC.RPCCredentialString();
         System.Net.NetworkCredential     t = new System.Net.NetworkCredential(GetBMSConfigurationKeyValue("rpcuser"), GetBMSConfigurationKeyValue("rpcpassword"));
         r.UserPassword = t;
         string sHost             = GetBMSConfigurationKeyValue("rpchost");
         NBitcoin.RPC.RPCClient n = new NBitcoin.RPC.RPCClient(r, sHost, NBitcoin.Network.BiblepayMain);
         _rpcclient = n;
         return(n);
     }
     else
     {
         try
         {
             var nbal = _rpcclient.GetBalance();
         }
         catch (Exception)
         {
             _rpcclient = null;
             return(GetLocalRPCClient());
         }
         return(_rpcclient);
     }
 }
示例#3
0
        public static string GetNewDepositAddress()
        {
            NBitcoin.RPC.RPCClient n = GetLocalRPCClient();
            string sAddress          = n.GetNewAddress().ToString();

            return(sAddress);
        }
示例#4
0
 public static bool gobject_submit(bool fTestNet, string sID, int nProposalTimeStamp, string sHex, string sPrepareTXID)
 {
     try
     {
         if (sPrepareTXID == "")
         {
             return(false);
         }
         // Submit the gobject to the network - gobject submit parenthash revision time datahex collateraltxid
         string   sArgs   = "0 1 " + nProposalTimeStamp.ToString() + " " + sHex + " " + sPrepareTXID;
         string   sCmd1   = "gobject submit " + sArgs;
         object[] oParams = new object[6];
         oParams[0] = "submit";
         oParams[1] = "0";
         oParams[2] = "1";
         oParams[3] = nProposalTimeStamp.ToString();
         oParams[4] = sHex;
         oParams[5] = sPrepareTXID;
         NBitcoin.RPC.RPCClient n = fTestNet ? WebRPC.GetTestNetRPCClient() : WebRPC.GetLocalRPCClient();
         dynamic oOut             = n.SendCommand("gobject", oParams);
         string  sSubmitTXID      = oOut.Result.ToString();
         if (sSubmitTXID.Length > 20)
         {
             // Update the record allowing us to know this has been submitted
             string sql = "Update Proposal set Submitted=GetDate(),SubmitTXID='" + sSubmitTXID + "' where id = '" + sID + "'";
             Common.gData.Exec(sql);
             return(true);
         }
         return(false);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
示例#5
0
        public static int GetHeight()
        {
            object[] oParams         = new object[1];
            NBitcoin.RPC.RPCClient n = GetLocalRPCClient();
            dynamic oOut             = n.SendCommand("getmininginfo");
            int     nBlocks          = (int)GetDouble(oOut.Result["blocks"]);

            return(nBlocks);
        }
示例#6
0
        public static NBitcoin.RPC.RPCClient GetTestNetRPCClient()
        {
            NBitcoin.RPC.RPCCredentialString r = new NBitcoin.RPC.RPCCredentialString();
            System.Net.NetworkCredential     t = new System.Net.NetworkCredential(GetBMSConfigurationKeyValue("testnetrpcuser"), GetBMSConfigurationKeyValue("testnetrpcpassword"));
            r.UserPassword = t;
            string sHost = GetBMSConfigurationKeyValue("testnetrpchost");

            NBitcoin.RPC.RPCClient n = new NBitcoin.RPC.RPCClient(r, sHost, NBitcoin.Network.BiblepayTest);
            return(n);
        }
示例#7
0
        /// <summary>
        /// 解析比特币区块
        /// </summary>
        /// <param name="rpcC"></param>
        /// <param name="index">被解析区块</param>
        /// <param name="height">区块高度</param>
        /// <returns></returns>
        private static void ParseBtcBlock(NBitcoin.RPC.RPCClient rpcC, ulong index)
        {
            var block = rpcC.GetBlockAsync((int)index).Result;

            if (block.Transactions.Count > 0 && Config._btcAddrList.Count > 0)
            {
                for (var i = 0; i < block.Transactions.Count; i++)
                {
                    var tran = block.Transactions[i];
                    var txid = tran.GetHash().ToString();

                    //如果存在该 txid 了,说明这笔交易已经解析过了
                    if (btcTransRspList.Exists(x => x.txid == txid))
                    {
                        continue;
                    }
                    for (var vo = 0; vo < tran.Outputs.Count; vo++)
                    {
                        var vout    = tran.Outputs[vo];
                        var address = vout.ScriptPubKey.GetDestinationAddress(Config._nettype); //比特币地址和网络有关

                        for (int j = 0; j < Config._btcAddrList.Count; j++)
                        {
                            if (address?.ToString() == Config._btcAddrList[j])
                            {
                                var btcTrans = new TransactionInfo();
                                btcTrans.coinType     = "btc";
                                btcTrans.toAddress    = address.ToString();
                                btcTrans.value        = vout.Value.ToDecimal(MoneyUnit.BTC);
                                btcTrans.confirmCount = 1;
                                btcTrans.height       = index;
                                btcTrans.txid         = txid;

                                btcTransRspList.Add(btcTrans);
                                Logger.Info(index + " Have A BTC Transaction To:" + address + "; Value:" + btcTrans.value + "; Txid:" + btcTrans.txid);
                            }
                        }
                    }
                }
            }

            if (btcTransRspList.Count > 0)
            {
                //更新确认次数
                CheckBtcConfirm(btcTransRspList, index, rpcC);
                //发送和保存交易信息
                TransSender.SendTransTimer(btcTransRspList);
                //移除确认次数为 设定数量 和 0 的交易
                btcTransRspList.RemoveAll(x => x.confirmCount >= Config._confirmCountDict["btc"] || x.confirmCount == 0);
            }
        }
示例#8
0
        public static void SyncFastlyNicknames()
        {
            try
            {
                string   path      = GetFolderUnchained("nicknames.dat");
                DateTime diMod     = System.IO.File.GetLastWriteTime(path);
                TimeSpan tsElapsed = System.DateTime.Now - diMod;
                if (dicNicknames.Count() == 0)
                {
                    MemorizeNickNames();
                }

                if (tsElapsed.TotalSeconds < (60 * 30))
                {
                    return;
                }

                NBitcoin.RPC.RPCClient c = WebRPC.GetLocalRPCClient();
                object[] oParams         = new object[2];
                oParams[0] = "cpk";
                oParams[1] = "9999999";

                dynamic j    = c.SendCommand("datalist", oParams);
                JObject j1   = j.Result;
                JArray  ja   = (JArray)j.Result.ChildrenTokens;
                string  data = "";

                foreach (var jcpk in j1)
                {
                    string skey    = jcpk.Key;
                    string sValue  = jcpk.Value.ToString();
                    string sType   = GetEle(skey, "[-]", 0);
                    string sPriKey = GetEle(skey, "[-]", 1);
                    string sCPK    = GetEle(sValue, "|", 0);
                    string sNN     = GetEle(sValue, "|", 1);
                    if (sType == "CPK" && sCPK != "" && sNN != "")
                    {
                        string sRow = sType + "|" + sCPK + "|" + sNN + "\r\n";
                        data += sRow;
                    }
                }

                Unchained.WriteToFile(path, data);
                MemorizeNickNames();
            }
            catch (Exception ex)
            {
                Log("SyncFastlyNicknames " + ex.Message);
            }
        }
示例#9
0
        static async void AsyncMain()
        {
            //使用rpcClient 需要配置验证用户名和密码,可用如下参数配置
            //bitcoin-qt -server -rest -testnet -rpcuser=1 -rpcpassword=1
            var key = new System.Net.NetworkCredential("1", "1");
            var uri = new Uri("http://127.0.0.1:8332");

            NBitcoin.RPC.RPCClient rpcC = new NBitcoin.RPC.RPCClient(key, uri);
            var binfo = await rpcC.GetBlockchainInfoAsync();

            Console.WriteLine("chain=" + binfo.Chain.Name);
            Console.WriteLine("blockcount=" + binfo.Blocks);

            var count = await rpcC.GetBlockCountAsync();

            Console.WriteLine("blockcount quick=" + count);

            for (var i = count - 1; i < count; i++)
            {
                await GetOneBlock(rpcC, i);
            }
        }
示例#10
0
        public static string SendRawTx(string hex)
        {
            try
            {
                object[] oParams = new object[1];
                oParams[0] = hex;
                NBitcoin.RPC.RPCClient n = GetLocalRPCClient();
                dynamic oOut             = n.SendCommand("sendrawtransaction", oParams);
                string  result           = oOut.Result.Value;
                // To do return binary response code here; check response for fail and success
                if (result == null)
                {
                    return("");
                }

                return(result);
            }
            catch (Exception ex)
            {
                Common.Log("SendRawTx:: " + ex.Message);
                return("");
            }
        }
示例#11
0
        public static void Start()
        {
            ulong btcStartHeight = Program.btcStartHeight;

            DbHelper.GetTransList(ref btcTransRspList, Config._confirmCountDict["btc"], "btc");
            Logger.Info("Btc watcher start! index: " + btcStartHeight);

            var key = new System.Net.NetworkCredential("1", "1");
            var uri = new Uri(Config._apiDict["btc"]);

            NBitcoin.RPC.RPCClient rpcC = new NBitcoin.RPC.RPCClient(key, uri);

            while (true)
            {
                try
                {
                    var count = (ulong)rpcC.GetBlockCount();

                    while (btcStartHeight < count)
                    {
                        ParseBtcBlock(rpcC, btcStartHeight);
                        DbHelper.SaveIndex(btcStartHeight, "btc");

                        Logger.Info("Parse BTC Height:" + btcStartHeight);

                        btcStartHeight++;
                    }

                    Thread.Sleep(10000);
                }
                catch (Exception e)
                {
                    Logger.Error("btc: " + e.Message);
                    Logger.Error("stack: " + e.StackTrace);
                }
            }
        }
示例#12
0
        public static void gobject_prepare(bool fTestNet, string sID, int StartTimeStamp, string sHex)
        {
            // gobject prepare
            string sArgs = "0 1 " + StartTimeStamp.ToString() + " " + sHex;
            string sCmd1 = "gobject prepare " + sArgs;

            object[] oParams = new object[5];
            oParams[0] = "prepare";
            oParams[1] = "0";
            oParams[2] = "1";
            oParams[3] = StartTimeStamp.ToString();
            oParams[4] = sHex;

            NBitcoin.RPC.RPCClient n = fTestNet ? WebRPC.GetTestNetRPCClient() : WebRPC.GetLocalRPCClient();

            dynamic oOut         = n.SendCommand("gobject", oParams);
            string  sPrepareTXID = oOut.Result.ToString();

            string sql4 = "Update Proposal Set PrepareTxId='" +
                          sPrepareTXID + "',Updated=getdate() where id = '"
                          + sID + "'";

            Common.gData.Exec(sql4);
        }
示例#13
0
 /// <summary>
 /// 检查 BTC 确认次数
 /// </summary>
 /// <param name="num">需确认次数</param>
 /// <param name="btcTransRspList">交易列表</param>
 /// <param name="index">当前解析区块</param>
 /// <param name="rpcC"></param>
 private static void CheckBtcConfirm(List <TransactionInfo> btcTransRspList, ulong index, NBitcoin.RPC.RPCClient rpcC)
 {
     foreach (var btcTran in btcTransRspList)
     {
         if (index > btcTran.height)
         {
             var block = rpcC.GetBlockAsync((int)btcTran.height).Result;
             //如果原区块中还包含该交易,则确认数 = 当前区块高度 - 交易所在区块高度 + 1,不包含该交易,确认数统一记为 0
             if (block.Transactions.Count > 0 && block.Transactions.Exists(x => x.GetHash().ToString() == btcTran.txid))
             {
                 btcTran.confirmCount = (uint)(index - btcTran.height + 1);
             }
             else
             {
                 btcTran.confirmCount = 0;
             }
         }
     }
 }