无法通过JSON-RPC将CREATERAWTRANSACTION发布到Bitcoin Core [英] Trouble posting CREATERAWTRANSACTION to Bitcoin Core via JSON-RPC

查看:83
本文介绍了无法通过JSON-RPC将CREATERAWTRANSACTION发布到Bitcoin Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过json-rpc发布到本地比特币完整节点,但是服务器出现错误.

I'm trying to post to a local bitcoin full node via json-rpc but I'm getting an error from the server.

按照此处的文档进行操作: https://bitcoincore.org/en/doc/0.17.0/rpc/rawtransactions/createrawtransaction/

Following the documentation here: https://bitcoincore.org/en/doc/0.17.0/rpc/rawtransactions/createrawtransaction/

我可以看到createrawtransaction请求的以下示例结构:

I can see the following example structure for a createrawtransaction request:

{"jsonrpc": "1.0", "id":"curltest", "method": "createrawtransaction", "params": ["[{\"txid\":\"myid\",\"vout\":0}]", "[{\"address\":0.01}]"] }

我的代码创建了以下结构,该结构似乎与bitcoincore.org中示例的结构相匹配:

My code creates the following structure, which seems to match the structure of the example from bitcoincore.org:

{"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":["[{\"txid\":\"1a43a1f27c5837d5319a45217aa948a4d39c1d89faf497ce59de5bd570a64a26\",\"vout\":1}]","[{\"2NAZpRsvj9BstxxCDkKoe1FVjmPPxdmvqKj\":0.01}]"]}

但是它给出了一个错误:

But it gives an error:

System.Net.WebException
  HResult=0x80131509
  Message=The remote server returned an error: (500) Internal Server Error.
  Source=RawTransactions
  StackTrace:
   at RawTransactions.Form1.RequestServer(String methodName, List`1 parameters) in C:\Users\userthree\Documents\Visual Studio 2017\Projects\RawTransactions\RawTransactions\Form1.cs:line 132
   at RawTransactions.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\userthree\Documents\Visual Studio 2017\Projects\RawTransactions\RawTransactions\Form1.cs:line 77
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at RawTransactions.Program.Main() in C:\Users\userthree\Documents\Visual Studio 2017\Projects\RawTransactions\RawTransactions\Program.cs:line 19

下面是我用来发出RPC请求的方法,该方法是从此处的API参考中获得的:

Below is the method I am using to make the RPC request, which I got from the API reference here:

https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#.NET_.28C.23.29

public static string RequestServer(string methodName, List<string> parameters)
{
    string ServerIp = "http://localhost:18332";
    string UserName = "USERNAME";
    string Password = "PASSWORD";

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ServerIp);
    webRequest.Credentials = new NetworkCredential(UserName, Password);

    webRequest.ContentType = "application/json-rpc";
    webRequest.Method = "POST";

    string respVal = string.Empty;

    JObject joe = new JObject();
    joe.Add(new JProperty("jsonrpc", "1.0"));
    joe.Add(new JProperty("id", "1"));
    joe.Add(new JProperty("method", methodName));

    JArray props = new JArray();
    foreach (var parameter in parameters)
    {
        props.Add(parameter);
    }

    joe.Add(new JProperty("params", props));

    // serialize json for the request
    string s = JsonConvert.SerializeObject(joe);
    byte[] byteArray = Encoding.UTF8.GetBytes(s);
    webRequest.ContentLength = byteArray.Length;

    Stream dataStream = webRequest.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    StreamReader streamReader = null;
    try
    {
        WebResponse webResponse = webRequest.GetResponse();

        streamReader = new StreamReader(webResponse.GetResponseStream(), true);

        respVal = streamReader.ReadToEnd();
        var data = JsonConvert.DeserializeObject(respVal).ToString();
        return data;
    }
    catch (Exception exp)
    {
        throw (exp);
    }
    finally
    {
        if (streamReader != null)
        {
            streamReader.Close();
        }
    }

    return string.Empty;
}

这是我尝试使用上述方法的方法:

Here is my attempt to use the above method:

private void button1_Click(object sender, EventArgs e)
{
    StringBuilder sb1 = new StringBuilder();

    sb1.Append("[{\"");
    sb1.Append("txid");
    sb1.Append("\":\"");
    sb1.Append(Convert.ToString(data["result"][Convert.ToInt32(txtFromJSON.Text)]["txid"]));
    sb1.Append("\",\"");
    sb1.Append("vout");
    sb1.Append("\":");
    sb1.Append(Convert.ToString(data["result"][Convert.ToInt32(txtFromJSON.Text)]["vout"]));
    sb1.Append("}]");

    StringBuilder sb2 = new StringBuilder();

    sb2.Append("[{\"");
    sb2.Append(Convert.ToString(data["result"][Convert.ToInt32(txtToJSON.Text)]["address"]));
    sb2.Append("\":");
    sb2.Append(txtAmountToSpend.Text);
    sb2.Append("}]");

    // {"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":["[{\"txid\":\"1a43a1f27c5837d5319a45217aa948a4d39c1d89faf497ce59de5bd570a64a26\",\"vout\":1}]","[{\"2NAZpRsvj9BstxxCDkKoe1FVjmPPxdmvqKj\":0.01}]"]}
    data = JObject.Parse(RequestServer("createrawtransaction", new List<string>() { Convert.ToString(sb1), Convert.ToString(sb2) }));

    MessageBox.Show(Convert.ToString(data));
}

其他类似的命令也可以工作:

Other commands such these as do work:

// {"jsonrpc":"1.0","id":"1","method":"sendtoaddress","params":["2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF","0.1"]}
data = JObject.Parse(RequestServer("sendtoaddress", new List<string>() { "2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF", Convert.ToString(0.1) } ));

这也有效:

// {"jsonrpc":"1.0","id":"1","method":"listunspent","params":[]}
data = JObject.Parse(RequestServer("listunspent", new List<String>() { }));

我的问题:

createrawtransaction我做了什么错事?

更新1:

如评论中所建议,我已经更改了StringBuilder,现在正在使用对象,然后使用Newtonsoft.Json序列化对象.

As suggested in the comments, I have changed the StringBuilder, and am now using objects and then serializing the objects using Newtonsoft.Json.

这是我第二次尝试使用

Here is my second attempt to use the API reference code from https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#.NET_.28C.23.29:

private void button1_Click(object sender, EventArgs e)
{
    JContainer jArray = new JArray();

    JObject jFromTx = new JObject
    {
        { "txid", data["result"][Convert.ToInt32(txtFromJSON.Text)]["txid"] },
        { "vout", data["result"][Convert.ToInt32(txtFromJSON.Text)]["vout"] }
    };

    jArray.Add(jFromTx);

    JObject jToTx = new JObject
    {
        { Convert.ToString(data["result"][Convert.ToInt32(txtToJSON.Text)]["address"]), Convert.ToDouble(txtAmountToSpend.Text) }
    };

    JContainer jArray2 = new JArray
    {
        jToTx
    };

    string strFrom = JsonConvert.SerializeObject(jArray);
    string strTo = JsonConvert.SerializeObject(jArray2);

    data = JObject.Parse(RequestServer("createrawtransaction", new List<string>() { strFrom, strTo }));
}

这是新的序列化JSON:

Here is the new serialized JSON:

{"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":["[{\"txid\":\"1a43a1f27c5837d5319a45217aa948a4d39c1d89faf497ce59de5bd570a64a26\",\"vout\":1}]","[{\"2NAZpRsvj9BstxxCDkKoe1FVjmPPxdmvqKj\":0.01}]"]}

与我第一次尝试时使用的旧StringBuilder JSON相比:

Compared to the old StringBuilder JSON from my first attempt:

{"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":["[{\"txid\":\"1a43a1f27c5837d5319a45217aa948a4d39c1d89faf497ce59de5bd570a64a26\",\"vout\":1}]","[{\"2NAZpRsvj9BstxxCDkKoe1FVjmPPxdmvqKj\":0.01}]"]}

我仍然收到与以前相同的错误消息(见上文).

I am still getting the same error message as before (see above).

推荐答案

问题是您的params数组正在进行双序列化,因此服务器不知道如何解释请求.我确实意识到您的JSON与示例相同,因此在这里我很可能是错误的.我绝对不是使用Bitcoin Core的API的专家.但是,我确实查看了应该兼容的第三方库的源代码使用Bitcoin Core,并且似乎不会对createrawtransation请求的参数进行双序列化.这使我相信问题在于双序列化参数.

I think the problem is your params array is getting double-serialized, so the server doesn't know how to interpret the request. I do realize that your JSON looks the same as the example, so I could very well be wrong here; I'm definitely not an expert on using Bitcoin Core's API. However, I did look at a the source code for a third-party library which is supposed to be compatible with Bitcoin Core, and it does not appear to double-serialize the parameters for the createrawtransation request. This leads me to believe that the double-serialized parameters are the problem.

要修复,请尝试以下操作:

To fix, try the following:

  1. 为此更改现有RequestServer方法的方法签名:

public static string RequestServer(string methodName, List<string> parameters)

对此:

public static string RequestServer(string methodName, List<JToken> parameters)

  • 使用旧签名创建RequestServer方法的新重载,该旧签名将调用刚刚更改的现有签名.这将使您已经可用的其他方法(例如sendtoaddresslistunspent)保持不变.

  • Create a new overload of the RequestServer method using the old signature which calls the existing one you just changed. This will allow your other methods which already work (e.g. sendtoaddress and listunspent) to keep working with no changes.

    public static string RequestServer(string methodName, List<string> parameters)
    {
        return RequestServer(methodName, parameters.Select(p => new JValue(p)).ToList<JToken>());
    }
    

  • 最后,更改您的button1_Click方法中的代码,以便它不序列化jArrayjArray2,而是将它们在List<JToken>中传递给RequestServer.换句话说,更改此代码:

  • Lastly, change the code in your button1_Click method so that it does not serialize jArray and jArray2, but instead passes them in a List<JToken> to RequestServer. In other words, change this code:

    string strFrom = JsonConvert.SerializeObject(jArray);
    string strTo = JsonConvert.SerializeObject(jArray2);
    
    data = JObject.Parse(RequestServer("createrawtransaction", new List<string>() { strFrom, strTo }));
    

    对此:

    data = JObject.Parse(RequestServer("createrawtransaction", new List<JToken>() { jArray, jArray2 }));
    

  • 有了这些更改,createrawtransaction的RPC JSON应该最终看起来像这样:

    With these changes, the RPC JSON for createrawtransaction should end up looking like this:

    {"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":[[{"txid":"1a43a1f27c5837d5319a45217aa948a4d39c1d89faf497ce59de5bd570a64a26","vout":1}],[{"2NAZpRsvj9BstxxCDkKoe1FVjmPPxdmvqKj":0.01}]]}
    

    请注意,params数组中多余的引号和反斜杠均已消失.与以前相比:

    Notice that the extra quotes and backslashes in the params array are gone. Compare to what you had before:

    {"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":["[{\"txid\":\"1a43a1f27c5837d5319a45217aa948a4d39c1d89faf497ce59de5bd570a64a26\",\"vout\":1}]","[{\"2NAZpRsvj9BstxxCDkKoe1FVjmPPxdmvqKj\":0.01}]"]}
    

    这篇关于无法通过JSON-RPC将CREATERAWTRANSACTION发布到Bitcoin Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆