Could not sign Transaction with SignRawTransactionWithKey through the RPC (RegTest)

1

I am able to sign following transaction with Complete=true result, but when trying to decode signed transaction, it returns error "TX decode failed" both from DecodeRawTransaction and SendRawTransaction RPC calls.

signed_Tx = "020000000103d3049e968d8b3e3e967aa90b45d3f64b5e2117ee9610b0c9d5a96081a24e3e000000004847304402201921a1acf601acb0f9e6297a55eeb853c2dd6cd246ea05f060eaa4d327d4124a02206e08e2c90b92113a64d6de1447f81bf19703f88bffeacc6d2cc4bf0e21ed8ac501ffffffff0200e1f5050000000017a9140796253e13d88cda530bf88b07eb1672952a3a7887608a0e24010000001976a914957b73da3bf34440ab2f78937a7a420835255a0988ac00000000"

[TestMethod()]
public async Task CreateRawTxTest()
{
    Dictionary<string, object> prevTx = new Dictionary<string, object>() {
                    { "txid","3e4ea28160a9d5c9b01096ee17215e4bf6d3450ba97a963e3e8b8d969e04d303"} ,
                    { "scriptPubKey", "21033ff1181726af279fb2328e9ae041d71f323a91483f76841fe23aa03f87ecf3d3ac" },
                    { "vout", 0} };

    List<object> txIn = new List<object>()
            {
                new Dictionary<string, object>() {
                    { "txid","3e4ea28160a9d5c9b01096ee17215e4bf6d3450ba97a963e3e8b8d969e04d303"},
                    { "vout", 0} }
            };

    List<object> txOut = new List<object>()
            {
                new Dictionary<string, float>
                {
                    { TestNet_ReceiverAddress, 1.0f}
                },
                new Dictionary<string, float>
                {
                    { "TestNet_SenderAddress", 48.99900f }
                },
            };


    RPCResultPrime<string> rawResult = await disp.CreateRawTransactionAsync(txIn, txOut);

    if (rawResult.Error == null)
    {
        string TxRaw = rawResult.Result;

        Dictionary<string, object> param = new Dictionary<string, object>()
                {
                    {"hexstring", TxRaw },
                    {"privkeys", new string[] { PrivateKey }},
                    {"prevtxs", new List<object>(){ prevTx } }
                    {"sighashtype", "ALL" }
                };

        string signedTx = await disp.SignTransactionAsync(param);

        //at this point transaction signed successfully but could not be Decoded in the next step

        string decode = await disp.DecodeRawTransactionAsync(signedTx);

        string jsonSend = await disp.SendRawTransactionAsync(signedTx);

        Debugger.Break();
    }

}

Ali Harsini

Posted 2019-02-04T18:17:52.590

Reputation: 13

Answers

1

You are passing the parameter with signedTx incorrectly. A JSON-RPC param key must have a value that is either an array or an object. You are passing in a string. Instead you should pass in the parameter similarly to how you did for the signrawtransactionwithkey call.

You should have something like this:

     Dictionary<string, object> param = new Dictionary<string, object>()
            {
                {"hexstring", signedTx }
            };

    string decode = await disp.DecodeRawTransactionAsync(param);

The same should be done for sendrawtransaction.

Andrew Chow

Posted 2019-02-04T18:17:52.590

Reputation: 40 910

With passing command as array or collection then it says it wants a string type parameter { "result":null, "error":{ "code":-3,"message":"Expected type string, got object"}, "id":null }Ali Harsini 2019-02-05T00:52:28.850

What does the resulting command string that is sent to bitcoind look like?Andrew Chow 2019-02-05T01:31:35.517

"method":"signrawtransactionwithkey",
"params":
{ "hexstring":"0200000001810211089221fb6f6e0d0a4d5805f938f", "privkeys": ["{\"result\":\"cR4zAfhBqYb5zopnzbSPapHGMHzA\",\"error\":null,\"id\":null}\n"], "prevtxs": [{ "txid":"20715ec617522f2530530828c8ab09fd38f905584d0a0d6e6ffb219208110281", "scriptPubKey":"210346335c729cda01ae00f29ebf9e3b31431ef4f6eb89a50fb4846ce3f4dd2a714dac", "vout":0, "amount":50.0 }], "sighashtype":"ALL" }
Ali Harsini 2019-02-05T07:52:02.843

sorry, I can't formated comment to display correctly, and to fit into space I discard some text form "hexstring" and "privkeys"Ali Harsini 2019-02-05T07:52:26.853

The problem is how you are passing privkeys here. You are giving an array of objects, but it is expecting an array of strings.Andrew Chow 2019-02-05T15:32:21.887

0

Seems to be able to be decoded:

$ bitcoin-cli -regtest decoderawtransaction 020000000103d3049e968d8b3e3e967aa90b45d3f64b5e2117ee9610b0c9d5a96081a24e3e000000004847304402201921a1acf601acb0f9e6297a55eeb853c2dd6cd246ea05f060eaa4d327d4124a02206e08e2c90b92113a64d6de1447f81bf19703f88bffeacc6d2cc4bf0e21ed8ac501ffffffff0200e1f5050000000017a9140796253e13d88cda530bf88b07eb1672952a3a7887608a0e24010000001976a914957b73da3bf34440ab2f78937a7a420835255a0988ac00000000
{
  "txid": "9cbedd5f9674aff9fff890cec60aa38dba074fcc3984c5a2b760ff69d2c9ea3f",
  "hash": "9cbedd5f9674aff9fff890cec60aa38dba074fcc3984c5a2b760ff69d2c9ea3f",
  "version": 2,
  "size": 189,
  "vsize": 189,
  "weight": 756,
  "locktime": 0,
  "vin": [
    {
      "txid": "3e4ea28160a9d5c9b01096ee17215e4bf6d3450ba97a963e3e8b8d969e04d303",
      "vout": 0,
      "scriptSig": {
        "asm": "304402201921a1acf601acb0f9e6297a55eeb853c2dd6cd246ea05f060eaa4d327d4124a02206e08e2c90b92113a64d6de1447f81bf19703f88bffeacc6d2cc4bf0e21ed8ac5[ALL]",
        "hex": "47304402201921a1acf601acb0f9e6297a55eeb853c2dd6cd246ea05f060eaa4d327d4124a02206e08e2c90b92113a64d6de1447f81bf19703f88bffeacc6d2cc4bf0e21ed8ac501"
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 1.00000000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 0796253e13d88cda530bf88b07eb1672952a3a78 OP_EQUAL",
        "hex": "a9140796253e13d88cda530bf88b07eb1672952a3a7887",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2MswLavAixrNJY2eN58LLT6xjwLqouwvASn"
        ]
      }
    },
    {
      "value": 48.99900000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 957b73da3bf34440ab2f78937a7a420835255a09 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914957b73da3bf34440ab2f78937a7a420835255a0988ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "mu9LsGdG3KFnpgB3Vx8w1sg8uCLb4gn3Uk"
        ]
      }
    }
  ]
}

JBaczuk

Posted 2019-02-04T18:17:52.590

Reputation: 6 172

yes you'r right, it's works fine with bitcoin-cli, but return error when called through the RPC in code. i am a little bit confused to where to looking for cause of errorAli Harsini 2019-02-04T18:53:39.467

Are you using a library, where is SignTransactionAsync defined?JBaczuk 2019-02-04T19:07:23.930

It's a helper method, just cover parameter passing and Http methodsAli Harsini 2019-02-04T19:21:16.493

I am testing it with both depricated "signrawtransaction" and new one "signrawtransactionwithkey" but got the same errorAli Harsini 2019-02-04T19:44:09.503

would need to see the function definitionsJBaczuk 2019-02-04T20:08:51.580

public async Task<string> SignTransactionWithKeyAsync(Dictionary<string, object> param) { RPCCommand cmd = new RPCCommand() { Method = "signrawtransactionwithkey", Params = param, Id = 0 };

string cmdRequest = server.SerializeCommand(cmd, true);

string jsonStr = await server.PostCommand(cmdRequest, server.serverAddress);

return jsonStr;
}
Ali Harsini 2019-02-04T20:11:58.473

I don't know what RPCCommand() doesJBaczuk 2019-02-04T20:34:52.033