private static void StartTransation()
        {
            //還原主錢包

            var walletA = NBitcoin.Key.Parse("6PYQBZhumqnrhETBXNvqW61XHpwYVefbTmZtM2BZcekPjunUbPgUPGe8H2", "your_pass_word", NBitcoin.Network.TestNet).GetWif(NBitcoin.Network.TestNet);


            //準備被接收端的錢包(目的地)
            var reveiveAddress = NBitcoin.BitcoinAddress.Create("mshr22VWpq7XTTA3EhAoqoizPuqRAvZfvi", NBitcoin.Network.TestNet);

            var tx = NBitcoin.Transaction.Create(NBitcoin.Network.TestNet);

            var input = new NBitcoin.TxIn();

            // 帶入來源端的 Trasaction Id 還有 Index
            // Source from : https://no2don.blogspot.com/2019/02/c-bitcoin_96.html
            // https://live.blockcypher.com/btc-testnet/tx/d3425d5f912552a47358df8b6647330e914019b1745c88b89d376896d35864e5/
            input.PrevOut = new NBitcoin.OutPoint(new NBitcoin.uint256("d3425d5f912552a47358df8b6647330e914019b1745c88b89d376896d35864e5"), 0);

            input.ScriptSig = walletA.GetAddress().ScriptPubKey;
            tx.Inputs.Add(input);

            var output = new NBitcoin.TxOut();
            //這是 GAS Fee
            var gasFee = NBitcoin.Money.Coins(0.0001M);

            output.Value = NBitcoin.Money.Coins(0.01M) - gasFee;
            //設定轉出到指定的錢包至 Output
            output.ScriptPubKey = reveiveAddress.ScriptPubKey;
            tx.Outputs.Add(output);


            tx.Sign(walletA.PrivateKey, false);


            Console.WriteLine("========== TXINO ===========");
            Console.WriteLine(tx.ToString());


            var txBuilder = NBitcoin.Network.TestNet.CreateTransactionBuilder();
            var res       = txBuilder.Verify(tx);


            //對一個節點傳送交易 ,使其進行廣播

            var node = NBitcoin.Protocol.Node.Connect(NBitcoin.Network.TestNet, "testnet-seed.bitcoin.jonasschnelli.ch");

            node.VersionHandshake();
            node.SendMessage(new NBitcoin.Protocol.InvPayload(tx));
            node.SendMessage(new NBitcoin.Protocol.TxPayload(tx));

            System.Threading.Thread.Sleep(2000);
            node.Disconnect();


            Console.WriteLine("========== NODE TX ===========");
            Console.WriteLine("TXID:" + tx.GetHash().ToString());
        }
Beispiel #2
0
        protected override void BuildTransaction(JObject json, Transaction tx)
        {
            var hash = uint256.Parse((string)json.GetValue("hash"));

            tx.Version  = (uint)json.GetValue("ver");
            tx.LockTime = (uint)json.GetValue("lock_time");
            var size = (uint)json.GetValue("size");


            var vin      = (JArray)json.GetValue("in");
            int vinCount = (int)json.GetValue("vin_sz");

            for (int i = 0; i < vinCount; i++)
            {
                var jsonIn = (JObject)vin[i];
                var txin   = new TxIn();
                tx.Inputs.Add(txin);
                var prevout = (JObject)jsonIn.GetValue("prev_out");

                txin.PrevOut.Hash = uint256.Parse((string)prevout.GetValue("hash"));
                txin.PrevOut.N    = (uint)prevout.GetValue("n");


                var script = (string)jsonIn.GetValue("scriptSig");
                if (script != null)
                {
                    txin.ScriptSig = new Script(script);
                }
                else
                {
                    var coinbase = (string)jsonIn.GetValue("coinbase");
                    txin.ScriptSig = new Script(Encoders.Hex.DecodeData(coinbase));
                }

                var seq = jsonIn.GetValue("sequence");
                if (seq != null)
                {
                    txin.Sequence = (uint)seq;
                }
            }

            var vout      = (JArray)json.GetValue("out");
            int voutCount = (int)json.GetValue("vout_sz");

            for (int i = 0; i < voutCount; i++)
            {
                var jsonOut = (JObject)vout[i];
                var txout   = new NBitcoin.TxOut();
                tx.Outputs.Add(txout);

                txout.Value        = Money.Parse((string)jsonOut.GetValue("value"));
                txout.ScriptPubKey = new Script((string)jsonOut.GetValue("scriptPubKey"));
            }
        }
		protected override void BuildTransaction(JObject json, Transaction tx)
		{
			var hash = uint256.Parse((string)json.GetValue("hash"));
			tx.Version = (uint)json.GetValue("ver");
			tx.LockTime = (uint)json.GetValue("lock_time");
			var size = (uint)json.GetValue("size");


			var vin = (JArray)json.GetValue("in");
			int vinCount = (int)json.GetValue("vin_sz");
			for(int i = 0 ; i < vinCount ; i++)
			{
				var jsonIn = (JObject)vin[i];
				var txin = new TxIn();
				tx.Inputs.Add(txin);
				var prevout = (JObject)jsonIn.GetValue("prev_out");

				txin.PrevOut.Hash = uint256.Parse((string)prevout.GetValue("hash"));
				txin.PrevOut.N = (uint)prevout.GetValue("n");


				var script = (string)jsonIn.GetValue("scriptSig");
				if(script != null)
				{
					txin.ScriptSig = new Script(script);
				}
				else
				{
					var coinbase = (string)jsonIn.GetValue("coinbase");
					txin.ScriptSig = new Script(Encoders.Hex.DecodeData(coinbase));
				}

				var seq = jsonIn.GetValue("sequence");
				if(seq != null)
				{
					txin.Sequence = (uint)seq;
				}
			}

			var vout = (JArray)json.GetValue("out");
			int voutCount = (int)json.GetValue("vout_sz");
			for(int i = 0 ; i < voutCount ; i++)
			{
				var jsonOut = (JObject)vout[i];
				var txout = new NBitcoin.TxOut();
				tx.Outputs.Add(txout);

				txout.Value = Money.Parse((string)jsonOut.GetValue("value"));
				txout.ScriptPubKey = new Script((string)jsonOut.GetValue("scriptPubKey"));
			}
		}
        protected override void BuildTransaction(JObject json, Transaction tx)
        {
            var txid = new uint256((string)json.GetValue("txid"));

            tx.Version  = (uint)json.GetValue("version");
            tx.LockTime = (uint)json.GetValue("locktime");

            var vin = (JArray)json.GetValue("vin");

            for (int i = 0; i < vin.Count; i++)
            {
                var jsonIn = (JObject)vin[i];
                var txin   = new NBitcoin.TxIn();
                tx.Inputs.Add(txin);

                var script = (JObject)jsonIn.GetValue("scriptSig");
                if (script != null)
                {
                    txin.ScriptSig    = new Script(Encoders.Hex.DecodeData((string)script.GetValue("hex")));
                    txin.PrevOut.Hash = new uint256((string)jsonIn.GetValue("txid"));
                    txin.PrevOut.N    = (uint)jsonIn.GetValue("vout");
                }
                else
                {
                    var coinbase = (string)jsonIn.GetValue("coinbase");
                    txin.ScriptSig = new Script(Encoders.Hex.DecodeData(coinbase));
                }

                txin.Sequence = (uint)jsonIn.GetValue("sequence");
            }

            var vout = (JArray)json.GetValue("vout");

            for (int i = 0; i < vout.Count; i++)
            {
                var jsonOut = (JObject)vout[i];
                var txout   = new NBitcoin.TxOut();
                tx.Outputs.Add(txout);

                var btc      = (decimal)jsonOut.GetValue("value");
                var satoshis = btc * Money.COIN;
                txout.Value = new Money((long)(satoshis));

                var script = (JObject)jsonOut.GetValue("scriptPubKey");
                txout.ScriptPubKey = new Script(Encoders.Hex.DecodeData((string)script.GetValue("hex")));
            }
        }
Beispiel #5
0
        protected override void BuildTransaction(JObject json, Transaction tx)
        {
            var txid = new uint256((string)json.GetValue("txid"));
            tx.Version = (uint)json.GetValue("version");
            tx.LockTime = (uint)json.GetValue("locktime");

            var vin = (JArray)json.GetValue("vin");
            for(int i = 0 ; i < vin.Count ; i++)
            {
                var jsonIn = (JObject)vin[i];
                var txin = new NBitcoin.TxIn();
                tx.Inputs.Add(txin);

                var script = (JObject)jsonIn.GetValue("scriptSig");
                if(script != null)
                {
                    txin.ScriptSig = new Script(Encoders.Hex.DecodeData((string)script.GetValue("hex")));
                    txin.PrevOut.Hash = new uint256((string)jsonIn.GetValue("txid"));
                    txin.PrevOut.N = (uint)jsonIn.GetValue("vout");
                }
                else
                {
                    var coinbase = (string)jsonIn.GetValue("coinbase");
                    txin.ScriptSig = new Script(Encoders.Hex.DecodeData(coinbase));
                }

                txin.Sequence = (uint)jsonIn.GetValue("sequence");

            }

            var vout = (JArray)json.GetValue("vout");
            for(int i = 0 ; i < vout.Count ; i++)
            {
                var jsonOut = (JObject)vout[i];
                var txout = new NBitcoin.TxOut();
                tx.Outputs.Add(txout);

                var btc = (decimal)jsonOut.GetValue("value");
                var satoshis = btc * Money.COIN;
                txout.Value = new Money((long)(satoshis));

                var script = (JObject)jsonOut.GetValue("scriptPubKey");
                txout.ScriptPubKey = new Script(Encoders.Hex.DecodeData((string)script.GetValue("hex")));
            }
        }
Beispiel #6
0
 public TxInUndo(NBitcoin.TxOut txOut)
 {
     this.TxOut = txOut;
 }