Пример #1
0
        private void Code5()
        {
            var         blockr      = new BlockrTransactionRepository();
            Transaction transaction = blockr.Get("0b948b0674a3dbd229b2a0b436e0fce8aa84e6de28b088c610d110c2bf54acb4");

            Console.WriteLine(transaction.ToString());
        }
Пример #2
0
        public void Lesson1()
        {
            var         blockr      = new BlockrTransactionRepository();
            Transaction transaction = blockr.Get("d6a57b2ca44327acae03eb408eedab50ea6d4fb304f03aeb5d5251ec37b4baf5");

            // Console.WriteLine(transaction.ToString());
            Console.WriteLine(transaction.Outputs[0].ScriptPubKey.GetDestinationAddress(Network.Main));
        }
Пример #3
0
        private void Code6()
        {
            var         blockr             = new BlockrTransactionRepository();
            Transaction fundingTransaction = blockr.Get("0b948b0674a3dbd229b2a0b436e0fce8aa84e6de28b088c610d110c2bf54acb4");

            Transaction payment = new Transaction();

            payment.Inputs.Add(new TxIn()
            {
                PrevOut = new OutPoint(fundingTransaction.GetHash(), 1)
            });

            var programmingBlockchain = new BitcoinPubKeyAddress("1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB");

            payment.Outputs.Add(new TxOut()
            {
                Value        = Money.Coins(0.004m),
                ScriptPubKey = programmingBlockchain.ScriptPubKey
            });
            payment.Outputs.Add(new TxOut()
            {
                Value        = Money.Coins(0.0059m),
                ScriptPubKey = paymentAddress.ScriptPubKey
            });

            //Feedback !
            var message = "Thanks ! :)";
            var bytes   = Encoding.UTF8.GetBytes(message);

            payment.Outputs.Add(new TxOut()
            {
                Value        = Money.Zero,
                ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)
            });

            Console.WriteLine(payment);

            payment.Inputs[0].ScriptSig = paymentAddress.ScriptPubKey;
            //also OK :
            //payment.Inputs[0].ScriptSig = fundingTransaction.Outputs[1].ScriptPubKey;
            payment.Sign(paymentSecret, false);
            Console.WriteLine(payment);

            using (var node = Node.ConnectToLocal(Network.Main)) //Connect to the node
            {
                node.VersionHandshake();                         //Say hello
                //Advertize your transaction (send just the hash)
                node.SendMessage(new InvPayload(InventoryType.MSG_TX, payment.GetHash()));
                //Send it
                node.SendMessage(new TxPayload(payment));
                Thread.Sleep(500); //Wait a bit
            }
            return;
        }
Пример #4
0
    public void doubleSpend(string address, string change, Decimal fee)
    {
        var         blockr             = new BlockrTransactionRepository();
        Transaction fundingTransaction = blockr.Get(txTextBox.Text);

        Transaction payment = new Transaction();

        payment.Inputs.Add(new TxIn()
        {
            PrevOut = new OutPoint(fundingTransaction.GetHash(), 1)
        });

        BitcoinSecret s = new BitcoinSecret(privateKeyTextBox.Text, Network.Main);

        var destination   = BitcoinAddress.Create(address);
        var changeAddress = BitcoinAddress.Create(change);

        payment.Outputs.Add(new TxOut()
        {
            Value        = Money.Coins(Decimal.Parse(amountTextBox.Text)),
            ScriptPubKey = destination.ScriptPubKey //goes to destination address
        });
        payment.Outputs.Add(new TxOut()
        {
            Value = Money.Coins(Decimal.Parse(remainderTextBox.Text) - fee),
            //fee is subtracted from change amount and goes to the miners as the difference
            ScriptPubKey = changeAddress.ScriptPubKey //goes back as change
        });

        var message = "Double Spend!";
        var bytes   = Encoding.UTF8.GetBytes(message);

        payment.Outputs.Add(new TxOut()
        {
            Value        = Money.Zero,
            ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)
        });
        Console.WriteLine(payment);

        payment.Inputs[0].ScriptSig = fundingTransaction.Outputs[1].ScriptPubKey;
        payment.Sign(s, false);


        using (var node = Node.ConnectToLocal(Network.Main)) //Connect to the node
        {
            node.VersionHandshake();                         //Say hello
            //Advertize your transaction (send just the hash)
            node.SendMessage(new InvPayload(InventoryType.MSG_TX, payment.GetHash()));
            //Send it
            node.SendMessage(new TxPayload(payment));
            Thread.Sleep(500); //Wait a bit
        }
    }
Пример #5
0
        /// <summary>
        /// Creates the buy in tx
        /// </summary>
        /// <param name="address"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static async Task <String> CreateBuyInTx(String tableAddress, UInt64 amount)
        {
            BlockrTransactionRepository repo  = new BlockrTransactionRepository(NBitcoin.Network.TestNet);
            IEnumerable <Coin>          utxos = await repo.GetUnspentAsync(carol.ToString());

            TransactionBuilder builder = new TransactionBuilder();

            builder.AddCoins(utxos)
            .AddKeys(carol_secret)
            .BuildTransaction(true);

            //return builder.trans

            return("");
        }
Пример #6
0
        public static void CanRequestBlockr()
        {
            var repo   = new BlockrTransactionRepository(Network.Main);
            var result = repo.Get("c3462373f1a722c66cbb1b93712df94aa7b3731f4142cd8413f10c9e872927de");

            Assert.NotNull(result);
            Assert.Equal("c3462373f1a722c66cbb1b93712df94aa7b3731f4142cd8413f10c9e872927de", result.GetHash().ToString());

            result = repo.Get("c3462373f1a722c66cbb1b93712df94aa7b3731f4142cd8413f10c9e872927df");
            Assert.Null(result);

            repo   = new BlockrTransactionRepository(Network.TestNet);
            result = repo.Get("7d4c5d69a85c70ff70daff789114b9b76fb6d2613ac18764bd96f0a2b9358782");
            Assert.NotNull(result);
        }
Пример #7
0
        // P2PKH(Pay-To-Public-Key-Hash)による支払い
        public static void PayToPublicKeyHash(String TxId)
        {
            // テストネットから目的TxIdのトランザクションを検索
            var         blockr             = new BlockrTransactionRepository(Network.TestNet);
            Transaction fundingTransaction = blockr.Get(TxId);

            // 今回の支払い用トランザクションの作成
            Transaction payment = new Transaction();

            // 未使用アウトプットをインプットへ追加
            // 使用する未使用アウトプットは、インデックスで選択(mUtXoIndex)
            payment.Inputs.Add(new TxIn()
            {
                PrevOut = new OutPoint(fundingTransaction.GetHash(), mUtXoIndex)
            });

            // 自分宛のお釣りアウトプットを作成
            // 送金金額と自分のアドレスを設定
            BitcoinAddress myBitcoinAddress = BitcoinAddress.Create(mMyBitcoinAddress, Network.TestNet);

            payment.Outputs.Add(new TxOut()
            {
                Value        = Money.Coins(mChange),
                ScriptPubKey = myBitcoinAddress.ScriptPubKey
            });

            // 送金先のアウトプットを作成
            BitcoinAddress bhcBitcoinAddress = BitcoinAddress.Create(mBCHBitcoinAddress1, Network.TestNet);

            payment.Outputs.Add(new TxOut()
            {
                Value        = Money.Coins(mNumTranfer1),
                ScriptPubKey = bhcBitcoinAddress.ScriptPubKey
            });

            //利用するインプットにサインを行う
            payment.Inputs[0].ScriptSig = myBitcoinAddress.ScriptPubKey;
            payment.Sign(new BitcoinSecret(mMyWif), false);

            // 作成した支払いトランザクションのhex値を出力
            Console.WriteLine(payment.ToHex());
        }
Пример #8
0
        //OpenAssets, Colored Coin等で使われてるOP_RETURNを少し学んでみましょう(時間が余った人or宿題です)
        public static void PayToPublicKeyHashWithOP_RETURN(String TxId)
        {
            //Colored Coin, OpenAssetsで使われているOP_RETURN(最大値80Bytes)を利用したトランズアクションの生成
            var         blockr             = new BlockrTransactionRepository(Network.TestNet);
            Transaction fundingTransaction = blockr.Get(TxId);
            Transaction payment            = new Transaction();

            payment.Inputs.Add(new TxIn()
            {
                PrevOut = new OutPoint(fundingTransaction.GetHash(), 0)                 //参照するアウトプット(TxOut)のインデックス
            });

            BitcoinAddress myBitcoinAddress = BitcoinAddress.Create(mMyBitcoinAddress, Network.TestNet);

            payment.Outputs.Add(new TxOut()
            {
                Value        = Money.Coins(0.0003m),
                ScriptPubKey = myBitcoinAddress.ScriptPubKey
            });

            BitcoinAddress bhcBitcoinAddress = BitcoinAddress.Create(mBCHBitcoinAddress, Network.TestNet);

            payment.Outputs.Add(new TxOut()
            {
                Value        = Money.Coins(0.0001m),
                ScriptPubKey = bhcBitcoinAddress.ScriptPubKey
            });

            //ここがOP_RETURNを作る部分
            var message = "Thanks ! :)";
            var bytes   = Encoding.UTF8.GetBytes(message);

            payment.Outputs.Add(new TxOut()
            {
                Value        = Money.Zero,
                ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)
            });

            payment.Inputs[0].ScriptSig = myBitcoinAddress.ScriptPubKey;
            payment.Sign(new BitcoinSecret(mMyWif), false);
            Console.WriteLine(payment.ToHex());
        }
Пример #9
0
        //[Trait("UnitTest", "UnitTest")]
        public static void CanPushTxBlockr()
        {
            var repo   = new BlockrTransactionRepository(Network.Main);
            var result = repo.Get("c3462373f1a722c66cbb1b93712df94aa7b3731f4142cd8413f10c9e872927de");

            Assert.NotNull(result);
            Assert.Equal("c3462373f1a722c66cbb1b93712df94aa7b3731f4142cd8413f10c9e872927de", result.GetHash().ToString());

            var pushPath = BlockrTransactionRepository.BroadcastPath;

            try
            {
                BlockrTransactionRepository.BroadcastPath = "tx/decode";
                repo.BroadcastAsync(result).GetAwaiter().GetResult();
            }
            finally
            {
                BlockrTransactionRepository.BroadcastPath = pushPath;
            }
        }
Пример #10
0
        private static async Task <Transaction> CreateTransaction(BitcoinSecret secret, IDestination address, Int64 amount)
        {
            //Alice tx
            //d74b4bfc99dd46adb7c30877cc3ce7ea13feb51a6fab3b9b15f75f4e213ac0da
            BlockrTransactionRepository repo  = new BlockrTransactionRepository(Network.TestNet);
            IEnumerable <Coin>          utxos = await repo.GetUnspentAsync(secret.GetAddress().ToString());

            //Coin[] coins = utxos.OrderByDescending(u => u.Amount).Select(u => new Coin(u.Outpoint, u.TxOut)).ToArray();

            TransactionBuilder txBuilder = new TransactionBuilder();
            Transaction        tx        = txBuilder
                                           .AddCoins(utxos)
                                           .AddKeys(secret)
                                           .Send(address, new Money(amount))
                                           .SendFees(new Money(10000))
                                           .SetChange(address)
                                           .BuildTransaction(true);

            return(tx);
        }
Пример #11
0
        static void Main(string[] args)
        {
            BitcoinSecret  paymentSecret = new BitcoinSecret("L51eDMcsRUFSReQpz587UgcBSXGgskc6ssUGibXXHcYK6aMsAVzr");
            BitcoinAddress address       = paymentSecret.GetAddress();

            Console.WriteLine("Address: {0}", address);
            var         blockr             = new BlockrTransactionRepository();
            Transaction fundingTransaction = blockr.Get("2ab7d787b02ebd40f5de87aa27569b118ff1cc801088fbf7bab3eec304011878");

            Pay(paymentSecret, new BitcoinAddress("1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB"), Money.Coins(0.004m), fundingTransaction);

            //Select the chapter here:
            //var chapter = new Chapter2();

            ////call the lesson here
            //chapter.Lesson1();

            //hold open the output window
            Console.WriteLine("\n\n\nPress enter to continue");
            Console.ReadLine();
        }
Пример #12
0
        //[Trait("UnitTest", "UnitTest")]
        public static void CanRequestBlockr()
        {
            var repo   = new BlockrTransactionRepository(Network.Main);
            var result = repo.Get("c3462373f1a722c66cbb1b93712df94aa7b3731f4142cd8413f10c9e872927de");

            Assert.NotNull(result);
            Assert.Equal("c3462373f1a722c66cbb1b93712df94aa7b3731f4142cd8413f10c9e872927de", result.GetHash().ToString());

            result = repo.Get("c3462373f1a722c66cbb1b93712df94aa7b3731f4142cd8413f10c9e872927df");
            Assert.Null(result);

            var unspent = repo.GetUnspentAsync("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa").Result;

            Assert.True(unspent.Count != 0);

            repo   = new BlockrTransactionRepository(Network.TestNet);
            result = repo.Get("7d4c5d69a85c70ff70daff789114b9b76fb6d2613ac18764bd96f0a2b9358782");
            Assert.NotNull(result);

            unspent = repo.GetUnspentAsync("2N66DDrmjDCMM3yMSYtAQyAqRtasSkFhbmX").Result;
            Assert.True(unspent.Count != 0);
        }
Пример #13
0
        //Multisigを学んでみる
        public static void PayToScriptHash(String TxId)
        {
            var         blockr             = new BlockrTransactionRepository(Network.TestNet);
            Transaction fundingTransaction = blockr.Get(TxId);
            Transaction payment            = new Transaction();

            payment.Inputs.Add(new TxIn()
            {
                PrevOut = new OutPoint(fundingTransaction.GetHash(), 0)                 //参照するアウトプット(TxOut)のインデックス
            });

            Key bob   = new BitcoinSecret(mMyWif).PrivateKey;
            Key alice = new BitcoinSecret(mBCHWif).PrivateKey;

            //BitcoinAddress myBitcoinAddress = BitcoinAddress.Create(mMyBitcoinAddress, Network.TestNet);
            var scriptPubKey = PayToMultiSigTemplate
                               .Instance
                               .GenerateScriptPubKey(2, new[] { alice.PubKey, bob.PubKey });

            //paymentScript.GetScriptAddress
            Console.WriteLine(scriptPubKey);
        }
Пример #14
0
        //PublicKeyHashにお支払いをしてみましょう (P2PKH)
        public static void PayToPublicKeyHash(String TxId)
        {
            var         blockr             = new BlockrTransactionRepository(Network.TestNet);
            Transaction fundingTransaction = blockr.Get(TxId);

            //すでに利用されたトランズアクションのアウトプットから、今回利用するトランズアクションを選択します。
            Transaction payment = new Transaction();

            payment.Inputs.Add(new TxIn()
            {
                PrevOut = new OutPoint(fundingTransaction.GetHash(), mUtXoIndex)                 //参照するアウトプット(TxOut)のインデックス
            });

            // 自分に返ってくるお釣りをいくらするかにし、自分の公開鍵を設定します
            BitcoinAddress myBitcoinAddress = BitcoinAddress.Create(mMyBitcoinAddress, Network.TestNet);

            payment.Outputs.Add(new TxOut()
            {
                Value        = Money.Coins(mChange),
                ScriptPubKey = myBitcoinAddress.ScriptPubKey
            });

            // 相手側に送るコインの量と相手側の公開鍵をいれます。
            BitcoinAddress bhcBitcoinAddress = BitcoinAddress.Create(mBCHBitcoinAddress, Network.TestNet);

            payment.Outputs.Add(new TxOut()
            {
                Value        = Money.Coins(mNumTranfer),
                ScriptPubKey = bhcBitcoinAddress.ScriptPubKey
            });

            //利用するインプットにサインをします
            payment.Inputs[0].ScriptSig = myBitcoinAddress.ScriptPubKey;
            payment.Sign(new BitcoinSecret(mMyWif), false);
            Console.WriteLine(payment.ToHex());
        }
Пример #15
0
		public static void CanRequestBlockr()
		{
			var repo = new BlockrTransactionRepository(Network.Main);
			var result = repo.Get("c3462373f1a722c66cbb1b93712df94aa7b3731f4142cd8413f10c9e872927de");
			Assert.NotNull(result);
			Assert.Equal("c3462373f1a722c66cbb1b93712df94aa7b3731f4142cd8413f10c9e872927de", result.GetHash().ToString());

			result = repo.Get("c3462373f1a722c66cbb1b93712df94aa7b3731f4142cd8413f10c9e872927df");
			Assert.Null(result);

			var unspent = repo.GetUnspentAsync("15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe").Result;
			Assert.True(unspent.Count != 0);

			repo = new BlockrTransactionRepository(Network.TestNet);
			result = repo.Get("7d4c5d69a85c70ff70daff789114b9b76fb6d2613ac18764bd96f0a2b9358782");
			Assert.NotNull(result);

			unspent = repo.GetUnspentAsync("2N66DDrmjDCMM3yMSYtAQyAqRtasSkFhbmX").Result;
			Assert.True(unspent.Count != 0);
		}