Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: 687_2 on March 03, 2017, 11:30:48 AM



Title: Help with error code -22: Previous output scriptPubKey mismatch
Post by: 687_2 on March 03, 2017, 11:30:48 AM
Hi all,

I'm trying to create a raw transaction using bitcoin-cli. First I look at the transaction:

Code:
687_2$ bitcoin-cli listunspent
[
  {
    "txid": "0d53507afc671e57841e3f72cee5db1324da4fa0c42a50c98872b820d2a2c768",
    "vout": 0,
    "address": "13m5CWgygXuCbwuMo6MFtqNsiZT8uY1o6Q",
    "account": "",
    "scriptPubKey": "76a9141e460f008264743caa92c9d23f944216fd29c7a788ac",
    "amount": 0.00039000,
    "confirmations": 21,
    "spendable": true,
    "solvable": true
  }
]

Then I create the raw transaction:

Code:
bitcoin-cli createrawtransaction '[{"txid":"0d53507afc671e57841e3f72cee5db1324da4fa0c42a50c98872b820d2a2c768","vout":1,"scriptPubKey":"76a9141e460f008264743caa92c9d23f944216fd29c7a788ac"}]' '{"1EhPUwR9BkWWJ1K9rWjxSFbBMGKXKozCim":0.00038,"15sGXSMk99DUzwPMXqCevYB1gmS3c62CQr":0.00007}'


This gives me the hex:

Code:
010000000168c7a2d220b87288c9502ac4a04fda2413dbe5ce723f1e84571e67fc7a50530d0100000000ffffffff0270940000000000001976a914963d0a59d3b0ccfd0df3c95c972bee98e06ba19688ac581b0000000000001976a914356242ba76e9e8df980ac9d2d5399dee1233e45488ac00000000

Ok, now I want to sign the transaction. I format the command as prescribed:
Code:

687_2$ bitcoin-cli signrawtransaction '010000000168c7a2d220b87288c9502ac4a04fda2413dbe5ce723f1e84571e67fc7a50530d0100000000ffffffff0270940000000000001976a914963d0a59d3b0ccfd0df3c95c972bee98e06ba19688ac581b0000000000001976a914356242ba76e9e8df980ac9d2d5399dee1233e45488ac00000000' '[{"txid":"0d53507afc671e57841e3f72cee5db1324da4fa0c42a50c98872b820d2a2c768","vout":1,"scriptPubKey":"76a9141e460f008264743caa92c9d23f944216fd29c7a788ac"}]'

This results in the error:
Code:
error code: -22
error message:
Previous output scriptPubKey mismatch:
OP_DUP OP_HASH160 cbe0394e3f79b2f4f4e87191c638df0e8795bf92 OP_EQUALVERIFY OP_CHECKSIG
vs:
OP_DUP OP_HASH160 1e460f008264743caa92c9d23f944216fd29c7a7 OP_EQUALVERIFY OP_CHECKSIG

I've tried a few different ways of breaking this, looked here: https://www.reddit.com/r/Bitcoin/comments/2ebaow/how_to_create_a_raw_transaction_in_bitcoin_qt/ which doesn't seem to help, and I tried Greg's example: https://people.xiph.org/~greg/signdemo.txt as well; that one worked but I couldn't send the raw transaction (insufficient fee, or something. It made sense).

This seems like a trivial issue, maybe I've just been looking at my monitor too long. Does anyone see what I'm doing wrong? Is there something I'm missing?



Title: Re: Help with error code -22: Previous output scriptPubKey mismatch
Post by: achow101 on March 03, 2017, 02:13:11 PM
Then I create the raw transaction:

Code:
bitcoin-cli createrawtransaction '[{"txid":"0d53507afc671e57841e3f72cee5db1324da4fa0c42a50c98872b820d2a2c768","vout":1,"scriptPubKey":"76a9141e460f008264743caa92c9d23f944216fd29c7a788ac"}]' '{"1EhPUwR9BkWWJ1K9rWjxSFbBMGKXKozCim":0.00038,"15sGXSMk99DUzwPMXqCevYB1gmS3c62CQr":0.00007}'

There is no need to specify the scriptPubKey in this command. It is not used for creating raw unsigned transactions and is not even a parameter of createrawtransaction.

Ok, now I want to sign the transaction. I format the command as prescribed:
Code:

687_2$ bitcoin-cli signrawtransaction '010000000168c7a2d220b87288c9502ac4a04fda2413dbe5ce723f1e84571e67fc7a50530d0100000000ffffffff0270940000000000001976a914963d0a59d3b0ccfd0df3c95c972bee98e06ba19688ac581b0000000000001976a914356242ba76e9e8df980ac9d2d5399dee1233e45488ac00000000' '[{"txid":"0d53507afc671e57841e3f72cee5db1324da4fa0c42a50c98872b820d2a2c768","vout":1,"scriptPubKey":"76a9141e460f008264743caa92c9d23f944216fd29c7a788ac"}]'
Since the transaction you are spending from is in your wallet, there is no need to specify the prevouts. Just run the command with only the raw unsigned transaction from createrawtransaction and the wallet will handle the rest.


Title: Re: Help with error code -22: Previous output scriptPubKey mismatch
Post by: elbandi on March 03, 2017, 02:22:04 PM
Hi all,

I'm trying to create a raw transaction using bitcoin-cli. First I look at the transaction:

Code:
687_2$ bitcoin-cli listunspent
[
  {
    "txid": "0d53507afc671e57841e3f72cee5db1324da4fa0c42a50c98872b820d2a2c768",
    "vout": 0,
    "address": "13m5CWgygXuCbwuMo6MFtqNsiZT8uY1o6Q",
    "account": "",
    "scriptPubKey": "76a9141e460f008264743caa92c9d23f944216fd29c7a788ac",
    "amount": 0.00039000,
    "confirmations": 21,
    "spendable": true,
    "solvable": true
  }
]

Then I create the raw transaction:

Code:
bitcoin-cli createrawtransaction '[{"txid":"0d53507afc671e57841e3f72cee5db1324da4fa0c42a50c98872b820d2a2c768","vout":1,"scriptPubKey":"76a9141e460f008264743caa92c9d23f944216fd29c7a788ac"}]' '{"1EhPUwR9BkWWJ1K9rWjxSFbBMGKXKozCim":0.00038,"15sGXSMk99DUzwPMXqCevYB1gmS3c62CQr":0.00007}'

you used wrong vout index. in listunspent, vout index is 0, in your command line vout is 1.
and remove scriptPubKey param, no need that


Title: Re: Help with error code -22: Previous output scriptPubKey mismatch
Post by: 687_2 on March 04, 2017, 04:51:30 PM
Thanks guys, but now I have this problem:

Code:
error code: -3
error message:
Missing scriptPubKey

Code:
error code: -8
error message:
Invalid parameter, missing vout key

 ???


Title: Re: Help with error code -22: Previous output scriptPubKey mismatch
Post by: achow101 on March 04, 2017, 04:59:34 PM
Thanks guys, but now I have this problem:

Code:
error code: -3
error message:
Missing scriptPubKey

Code:
error code: -8
error message:
Invalid parameter, missing vout key

 ???
For which command is each error being returned?


Title: Re: Help with error code -22: Previous output scriptPubKey mismatch
Post by: 687_2 on March 04, 2017, 05:01:44 PM
The error is always thrown with bitcoin-cli signrawtransaction

createrawtransaction always generates a nice hash:

Code:
010000000168c7a2d220b87288c9502ac4a04fda2413dbe5ce723f1e84571e67fc7a50530d0100000000ffffffff0230750000000000001976a914963d0a59d3b0ccfd0df3c95c972bee98e06ba19688ac401f0000000000001976a9145112f7d2f5818bc8c1921e3181f2d27822feafb388ac00000000


Title: Re: Help with error code -22: Previous output scriptPubKey mismatch
Post by: achow101 on March 04, 2017, 05:17:27 PM
The error is always thrown with bitcoin-cli signrawtransaction
Interesting. Try using your original signrawtransaction command but with "vout":0 instead of "vout":1

createrawtransaction always generates a nice hash:
Createrawtransaction does not generate a hash. It creates a raw transaction. Just because something is displayed in hex does not mean that it s a hash.


Title: Re: Help with error code -22: Previous output scriptPubKey mismatch
Post by: 687_2 on March 05, 2017, 10:33:06 AM
Quote
The error is always thrown with bitcoin-cli signrawtransaction
Interesting. Try using your original signrawtransaction command but with "vout":0 instead of "vout":1

Success! I finally got signrawtransaction to sign the raw transaction, thank you!

Here's my code to create the raw transaction:

Code:
bitcoin-cli createrawtransaction '[{"txid":"0d53507afc671e57841e3f72cee5db1324da4fa0c42a50c98872b820d2a2c768","vout":0,"scriptPubKey":"76a9141e460f008264743caa92c9d23f944216fd29c7a788ac"}]' '{"1EhPUwR9BkWWJ1K9rWjxSFbBMGKXKozCim":0.0003,"18PgTTpQP9snFrJfZk8KCHKEXP9uUX7jLW":0.00008}'

It outputs the hash, and I copy and paste that hash into the signrawtransaction command. Now signing:

Code:
687_2$ bitcoin-cli signrawtransaction '010000000168c7a2d220b87288c9502ac4a04fda2413dbe5ce723f1e84571e67fc7a50530d0000000000ffffffff0230750000000000001976a914963d0a59d3b0ccfd0df3c95c972bee98e06ba19688ac401f0000000000001976a9145112f7d2f5818bc8c1921e3181f2d27822feafb388ac00000000' '[{"txid":"0d53507afc671e57841e3f72cee5db1324da4fa0c42a50c98872b820d2a2c768","vout":0,"scriptPubKey":"76a9141e460f008264743caa92c9d23f944216fd29c7a788ac"}]' '["myprivatekey"]'

Output:
Code:
{
  "hex": "010000000168c7a2d220b87288c9502ac4a04fda2413dbe5ce723f1e84571e67fc7a50530d000000006b483045022100be8112238a16d24ccc1769f98d5df7ec508f99eaf6b97ec42dcece8b9360328402207c07a830266c34aa77d1612e04d90757f8149d4df4785ada8cf957334497878b01210285019768580fb3d7d37928993b17e951527e5c2c2084a78013248e8fe572acecffffffff0230750000000000001976a914963d0a59d3b0ccfd0df3c95c972bee98e06ba19688ac401f0000000000001976a9145112f7d2f5818bc8c1921e3181f2d27822feafb388ac00000000",
  "complete": true
}