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 Oct 30 '13 at 16:05
  • 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 Oct 30 '13 at 17:31
  • Try saving the full rpc command to a file and then execute the command directly against bitcoind. – RLH Oct 30 '13 at 19:36
  • Added some more info to the main post, still unable to get this to work :\ – Chen Oct 31 '13 at 20:01
  • @Chen what was the solution to this problem? – George Kimionis Jan 3 '14 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.