1
1
I am trying to build a C# application working on JSON-RPC with Bitcoin-qt (server mode). I am using Bitsharp, though I don't think this should matter at all.
First off the InvokeMethod:
public JObject InvokeMethod(string a_sMethod, params object[] a_params)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
webRequest.Credentials = Credentials;
webRequest.ContentType = "application/json-rpc";
webRequest.Method = "POST";
JObject joe = new JObject();
joe["jsonrpc"] = "1.0";
joe["id"] = "1";
joe["method"] = a_sMethod;
if (a_params != null)
{
if (a_params.Length > 0)
{
JArray props = new JArray();
foreach (var p in a_params)
{
props.Add(p);
}
joe.Add(new JProperty("params", props));
}
}
string s = JsonConvert.SerializeObject(joe);
// serialize json for the request
byte[] byteArray = Encoding.UTF8.GetBytes(s);
webRequest.ContentLength = byteArray.Length;
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\WriteLines2.txt", true))
{
//file.WriteLine(s);
}
using (Stream dataStream = webRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
using (WebResponse webResponse = webRequest.GetResponse())
{
using (Stream str = webResponse.GetResponseStream())
{
using (StreamReader sr = new StreamReader(str))
{
return JsonConvert.DeserializeObject<JObject>(sr.ReadToEnd());
}
}
}
}
Secondly, this is the object I am building:
JContainer jArray = new JArray();
JArray jArrayTx = new JArray();
foreach (var ObjectTx in inputs.result)
{
JObject jObjectTx = new JObject();
jObjectTx.Add("txid", ObjectTx.txid);
jObjectTx.Add("vout", ObjectTx.vout);
jArrayTx.Add(jObjectTx);
}
jArray.Add(jArrayTx);
JObject jObjectAddress = new JObject();
foreach (var ObjectAddress in designatedAddresses)
{
jObjectAddress.Add(ObjectAddress.Key, ObjectAddress.Value);
}
if (returnAddress != null)
{
jObjectAddress.Add(returnAddress, totalUnspent - totalOutput - 0.001);
}
jArray.Add(jObjectAddress);
Then I do this:
bc.InvokeMethod("createrawtransaction", jArray);
I am trying to pass an object containing 2 arrays. 1. the array of txid's + vout's 2. the array of addresses I want to send to + the amounts.
This for some reason doesn't work, and i can't seem to put my finger on the issue.
Thanks for your help!
- EDIT -
Tried posting my JSON-RPC to file and then running the command directly in the bitcoin-qt command line.
This is the JSON-RPC object which doesn't work
{"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":[[[{"txid":"a89fd1381833a800942812a3981fe9910e364dc4a5fa91332354f8fc8ce02a1f","vout":0},{"txid":"b45394e27c2beb6b53dcefb79646eebcd9fd406bd9810f4b4138e00adadff637","vout":1},{"txid":"cae05651bb8f02cb0848bc984537290c375956d5fdfb3ae7e9d4280196f41765","vout":1},{"txid":"cbf93b0d8bbcadd85b3702653dfe2643d63cdd7ebf5b66f8a3d4c271b2a55492","vout":1}],{"mmhmMNfBiZZ37g1tgg2t8DDbNoEdqKVxAL":1.4,"n2JSa2L2KY4Eq32jEpBmKZrZTdxgCaNVtR":5.8735192599999992}]]}
or in object view:
{
"jsonrpc": "1.0",
"id": "1",
"method": "createrawtransaction",
"params": [
[
[
{
"txid": "a89fd1381833a800942812a3981fe9910e364dc4a5fa91332354f8fc8ce02a1f",
"vout": 0
},
{
"txid": "b45394e27c2beb6b53dcefb79646eebcd9fd406bd9810f4b4138e00adadff637",
"vout": 1
},
{
"txid": "cae05651bb8f02cb0848bc984537290c375956d5fdfb3ae7e9d4280196f41765",
"vout": 1
},
{
"txid": "cbf93b0d8bbcadd85b3702653dfe2643d63cdd7ebf5b66f8a3d4c271b2a55492",
"vout": 1
}
],
{
"mmhmMNfBiZZ37g1tgg2t8DDbNoEdqKVxAL": 1.4,
"n2JSa2L2KY4Eq32jEpBmKZrZTdxgCaNVtR": 5.8735192599999992
}
]
]
}
This does work:
createrawtransaction '[{"txid":"a89fd1381833a800942812a3981fe9910e364dc4a5fa91332354f8fc8ce02a1f","vout":0},{"txid":"b45394e27c2beb6b53dcefb79646eebcd9fd406bd9810f4b4138e00adadff637","vout":1},{"txid":"cae05651bb8f02cb0848bc984537290c375956d5fdfb3ae7e9d4280196f41765","vout":1},{"txid":"cbf93b0d8bbcadd85b3702653dfe2643d63cdd7ebf5b66f8a3d4c271b2a55492","vout":1}]' '{"mmhmMNfBiZZ37g1tgg2t8DDbNoEdqKVxAL":1.4,"n2JSa2L2KY4Eq32jEpBmKZrZTdxgCaNVtR":5.8735192599999992}'
What was needed for it to work is to remove the excess [ and to wrap the tx's in ' ' and add a white space between the tx's and the spending addresses.
** This works in the bitcoin-qt console, though i am unable to get createrawtransaction to work on json-rpc
What error is the RPC API reporting? I've worked a little bit with raw transactions. The RPC engine, though not extremely explicit, does output which portion of the transaction is broken. – RLH – 2013-10-30T16:05:26.580
The RPC-API returns: "The remote server returned an error: <500> Internal server error" And the server log shows: "ThreadRPCServer method=createrawtransaction connection timeout" – Chen – 2013-10-30T17:31:50.393
Try saving the full rpc command to a file and then execute the command directly against bitcoind. – RLH – 2013-10-30T19:36:43.847
Added some more info to the main post, still unable to get this to work :\ – Chen – 2013-10-31T20:01:56.970
@Chen what was the solution to this problem? – George Kimionis – 2014-01-03T19:17:54.517
@George , didn't find any way to get this to work. Just went to process one transaction at a time – Chen – 2014-01-05T18:49:42.303
@Chen this is now solved and works absolutely fine in this c# wrapper: https://github.com/GeorgeKimionis/BitcoinLib
– George Kimionis – 2014-01-05T22:05:14.267