gpt4 book ai didi

c# - 无法通过 JSON-RPC 将 CREATEAWTRANSACTION 发布到比特币核心

转载 作者:太空狗 更新时间:2023-10-29 23:19:55 30 4
gpt4 key购买 nike

我正尝试通过 json-rpc 发布到本地比特币全节点,但我从服务器收到错误消息。

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

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

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

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

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

但是报错:

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 引用中获得:

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;
}

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

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));
}

诸如此类的其他命令确实有效:

// {"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 序列化对象。

这是我第二次尝试使用来自 https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#.NET_.28C.23.29 的 API 引用代码:

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:

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

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

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

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

最佳答案

认为问题出在你的params array 正在双重序列化,因此服务器不知道如何解释请求。我确实意识到您的 JSON 看起来与示例相同,所以我在这里很可能是错误的;我绝对不是使用 Bitcoin Core 的 API 的专家。但是,我确实查看了 third-party library 的源代码。它应该与比特币核心兼容,并且它似乎没有对 createrawtransation 的参数进行双重序列化。要求。这让我相信双序列化参数是问题所在。

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

  1. 更改现有 RequestServer 的方法签名方法来自:

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

    为此:

    public static string RequestServer(string methodName, List<JToken> parameters)
  2. 创建 RequestServer 的新重载使用旧签名的方法调用您刚刚更改的现有签名。这将允许您已经工作的其他方法(例如 sendtoaddresslistunspent)继续工作而无需更改。

    public static string RequestServer(string methodName, List<string> parameters)
    {
    return RequestServer(methodName, parameters.Select(p => new JValue(p)).ToList<JToken>());
    }
  3. 最后,更改 button1_Click 中的代码方法使其不序列化jArrayjArray2 ,而是将它们传递给 List<JToken>RequestServer .换句话说,更改此代码:

    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应该最终看起来像这样:

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

请注意 params 中的额外引号和反斜杠数组不见了。与之前的比较:

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

关于c# - 无法通过 JSON-RPC 将 CREATEAWTRANSACTION 发布到比特币核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53603131/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com