Author

Topic: Trying to make SIGHASH_SINGLE transaction [unsolved] (Read 187 times)

full member
Activity: 297
Merit: 133
...

I can't comment on that since I haven't checked that library.

Examples in this library were bad.

I solved that - just needed to create tx first and set it once again at the end.

I am now trying to sign both inputs (in created output transaction) as SIGHASH_SINGLE:

1. Do I need to sign both inputs with SIGHASH_SINGLE?
2. Do I need to give public key as 0x01000...000 or 0x000...00001 or as hash160 of one of these?
3. How should be the second input signed if it is SIGHASH_SINGLE?
4. I know I have to make signature SIGHASH_SINGLE at the last byte.
5. I get now error "mandatory-script-verify-flag-failed (Non-canonical DER signature)"
legendary
Activity: 2618
Merit: 6452
Self-proclaimed Genius
I suspect that this python-bitcoinlib is failing.
I can't comment on that since I haven't checked that library.
full member
Activity: 297
Merit: 133
...

Use that instead.

Tried that and failing.

I try to send myself part of the balance, but even that does not work.

Script for now is:

Code:
#!/usr/bin/env python3

import hashlib
import subprocess

from bitcoin import *
from bitcoin.core import *
from bitcoin.core.script import *
from bitcoin.core.scripteval import *
from bitcoin.wallet import *
from pprint import pprint

SelectParams('mainnet')
seckey = CBitcoinSecret.from_secret_bytes(lx('my private key in hex'))
txid = lx('f860007e0217aeec309060da55fbb3ea614663dfb93a0cfab1c0377be863383c') # lx! sure
vout = 31
txin = CMutableTxIn(COutPoint(txid, vout))
txin_scriptPubKey = CScript([OP_DUP, OP_HASH160, Hash160(seckey.pub), OP_EQUALVERIFY, OP_CHECKSIG])
txout = CMutableTxOut(77000, CBitcoinAddress('1KkBjUXQ4rrB72PVonzwycJgZe6wXc3k6t').to_scriptPubKey())
tx = CMutableTransaction([txin], [txout])
sighash = SignatureHash(txin_scriptPubKey, tx, 0, SIGHASH_ALL)
sig = seckey.sign(sighash) + bytes([SIGHASH_ALL])
txin.scriptSig = CScript([sig, seckey.pub])
VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
r = subprocess.run(["/mnt/c/Program Files/Bitcoin/daemon/bitcoin-cli.exe", "sendrawtransaction", b2x(tx.serialize())])

The error is:

Code:
error code: -26
error message:
mandatory-script-verify-flag-failed (Script failed an OP_EQUALVERIFY operation)

I suspect that this python-bitcoinlib is failing.

Also, private key shouldn't change here, right?
legendary
Activity: 2618
Merit: 6452
Self-proclaimed Genius
And I would like you to try to change this
Changing those lines will break the script,
and changing the relevant parts of the code to fix it will do nothing but change those variables' names.

Code:
txid0 = lx('2bb8abb322b5087b21b4bb066a919b03b7ac69ce56840d44d7bf7050942f3d9a')
vout0 = 5
This output is already spent on 2023 July 29 by e51364d28584038dbd26a600b56391db85d8e64bf386b3c9c7cb352bf9217713

But it still has another unspent transaction output: f860007e0217aeec309060da55fbb3ea614663dfb93a0cfab1c0377be863383c:31 (0.00079261 BTC)
Use that instead.
full member
Activity: 297
Merit: 133
Can you share the txid and txid2 here I just want to check if you are correct on giving value on vout for both TXIDs (you can share other txid with the same inputs and outputs if you don't want to share your TXIDs).

...
Code:
txid0 = lx('2bb8abb322b5087b21b4bb066a919b03b7ac69ce56840d44d7bf7050942f3d9a')
vout0 = 5
That's 1KkBjUXQ4rrB72PVonzwycJgZe6wXc3k6t address.

txid1 = lx('0009f961969ec48571d3d22ec2d0e1bde9f0714e0edaa7bfb5b4914a21899212')
vout1 = 0
That's 1GKrP8NNcqgi8d7FpkDkjMcU5TKsAowZHf address.
legendary
Activity: 3374
Merit: 3095
Playbet.io - Crypto Casino and Sportsbook
Can you share the txid and txid2 here I just want to check if you are correct on giving value on vout for both TXIDs (you can share other txid with the same inputs and outputs if you don't want to share your TXIDs).

And I would like you to try to change this

Code:
txid = lx('tx number 1')
vout = 0
txid2 = lx('tx number 2')
vout2 = 1

To this

Code:
txid0 = lx('tx number 1')
vout0 = 0
txid1 = lx('tx number 2')
vout1 = 1

Let see what will be the result
full member
Activity: 297
Merit: 133
I've already verified that the library used does not work any way. Used one input address and one output, used for that script from examples and it does return the same error.

Yes, I've changed "tx number 1" and second one to proper tx values. Also changed vouts to proper ones (different than here), and still no luck.

Txout was included.

Vouts are only variables names. They are indexing the outputs from previous transactions (check one and two). I checked few times that they were correct.
Yes, I used proper transaction outputs from previous transactions using indexing from 0.
legendary
Activity: 2618
Merit: 6452
Self-proclaimed Genius
What am I missing here?
Seems like you're using the vout as the input's index number, while it's actually the UTXO's output_index.

For example:
If you want to spend the 3rd output of the transaction (testnet): d7a4d0f41a23af4b07ae75fa003ffa515c687f3da7045baa24d318649a4dbbe9; (0.00013000 tBTC)
The vout would be '2'.
legendary
Activity: 3374
Merit: 3095
Playbet.io - Crypto Casino and Sportsbook
The error bad-txns-inputs-missingorspent it means that you have UTXO missing or you might be trying to spend an already spent uTXO.

Check this part below

Code:
txid = lx('tx number 1')
vout = 0
txid2 = lx('tx number 2')
vout2 = 1

Did you add the hash of your unspent transaction under tx number 1 and 2? Can you add UTXO hash if ever you forgot to add it.

And another thing don't forget to add where you want send it under txout.
full member
Activity: 297
Merit: 133
I am trying to make a SIGHASH_SINGLE transaction.

I have two inputs, one output. I understand that second input can be not signed (really?).

I use Python 3.11.4.

I use these libraries:
Code:
apt install libssl-dev
pip3 install python-bitcoinlib

Script I have is:
Code:
#!/usr/bin/env python3

import hashlib
import subprocess
from bitcoin import SelectParams
from bitcoin.core import b2x, lx, COIN, COutPoint, CMutableTxOut, CMutableTxIn, CMutableTransaction, Hash160
from bitcoin.core.script import CScript, OP_DUP, OP_HASH160, OP_EQUALVERIFY, OP_CHECKSIG, SignatureHash, SIGHASH_ALL, SIGHASH_SINGLE
from bitcoin.core.scripteval import VerifyScript, SCRIPT_VERIFY_P2SH
from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret

SelectParams('mainnet')
h = b'private key bytes in python hex format'
seckey = CBitcoinSecret.from_secret_bytes(h)
txid = lx('tx number 1')
vout = 0
txid2 = lx('tx number 2')
vout2 = 1
txin = CMutableTxIn(COutPoint(txid, vout))
txin2 = CMutableTxIn(COutPoint(txid2, vout2))
txin_scriptPubKey = CScript([OP_DUP, OP_HASH160, Hash160(seckey.pub), OP_EQUALVERIFY, OP_CHECKSIG])
txout = CMutableTxOut(10000, CBitcoinAddress('output address').to_scriptPubKey())
tx = CMutableTransaction([txin]+[txin2], [txout])
sighash = SignatureHash(txin_scriptPubKey, tx, 0, SIGHASH_SINGLE)
sig = seckey.sign(sighash) + bytes([SIGHASH_SINGLE])
txin.scriptSig = CScript([sig, seckey.pub])
VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
print(b2x(tx.serialize()))
r=subprocess.run(["/mnt/c/Program Files/Bitcoin/daemon/bitcoin-cli.exe", "sendrawtransaction", b2x(tx.serialize())])

I am getting error:
Code:
error code: -25
error message:
bad-txns-inputs-missingorspent

Both inputs have balance, I am sure of that. I am running script from WSL1 Bash on Windows.

What am I missing here?
Jump to: