尝试使用NBitcoin

时间:2015-11-17 18:39:49

标签: c# asp.net-mvc-4 transactions bitcoin

这是我第一次尝试NBitcoin库来创建交易。我正在尝试创建OP_RETURN事务,但每次尝试使用节点执行Version Handshake时,我都会收到错误:The node is not in a connected state

这是不言自明的,但我尝试了许多被认为是可靠的节点。

我想这是一个两部分问题:

  1. 在NBitcoin中尝试OP_RETURN交易时,是否有必要手动将矿工费添加到TransactionBuilder中?

  2. 有没有办法使用Node.Connect所以只搜索可用节点,而不是指定特定节点?

  3. 继承我的代码,这是在.NET MVC4控制器操作中:

    [HttpPost]
    public ActionResult NewTransaction(string hash)
    {
            var paymentSecret = new BitcoinSecret("//Private Key");
            var message = hash;
            var bytes = Encoding.UTF8.GetBytes(message);
            var blockr = new BlockrTransactionRepository();
            var fundingTransaction = blockr.Get("60db94bfb43dd0d98ba7fdfbbg4f29a850f7s5ea7d79a32c4r954096f2945b04");
            var payment = new Transaction();
    
            payment.Inputs.Add(new TxIn()
            {
                PrevOut = new OutPoint(fundingTransaction.GetHash(), 1)
            });
    
            payment.Outputs.Add(new TxOut()
            {
                Value = Money.Zero,
                ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)
            });
    
            payment.Inputs[0].ScriptSig = paymentSecret.ScriptPubKey;
            payment.Sign(paymentSecret, false);
    
            using (var node = Node.Connect(Network.Main, "212.47.228.216", new NodeConnectionParameters()
            {
                IsTrusted = true,
                IsRelay = false
            }))
            {
                // Always fails on the handshake
                node.VersionHandshake();
                node.SendMessage(new InvPayload(InventoryType.MSG_TX, payment.GetHash()));
                node.SendMessage(new TxPayload(payment));
                Thread.Sleep(500);
            }
            return View("Index");
    }
    

    修改

    我尝试实际使用TransactionBuilder,但仍然遇到了同样的错误。这次我实际设定了费用金额。我也在使用不同的节点并指定端口。这是尝试:

    var paymentSecret = new BitcoinSecret("//Private Key");
    var message = hash;
    var bytes = Encoding.UTF8.GetBytes(message);
    
    var funding = new Transaction()
    {
        Outputs =
        {
            new TxOut("0.01", paymentSecret.GetAddress())
        }
    };
    
    var coins = funding
                    .Outputs
                    .Select((o, i) => new Coin(new OutPoint(funding.GetHash(), i), o))
                    .ToArray();
    
    var txBuilder = new TransactionBuilder();
    var tx = txBuilder
                .AddCoins(coins)
                .AddKeys(paymentSecret.PrivateKey)
                .Send(TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes), Money.Zero)
                .SendFees("0.00001")
                .SetChange(paymentSecret.PubKey)
                .BuildTransaction(true);
    
    txBuilder.Verify(tx);
    
    using (var node = Node.Connect(Network.Main, "74.124.62.108:8333", new NodeConnectionParameters()
    {
        IsTrusted = true,
        IsRelay = false
    }))
    {
        node.VersionHandshake();
        node.SendMessage(new InvPayload(InventoryType.MSG_TX, tx.GetHash()));
        node.SendMessage(new TxPayload(tx));
        Thread.Sleep(500);
     }
    

    任何人都可以提供帮助。缺少指定如何进行OP_RETURN交易的文档。感谢。

0 个答案:

没有答案