Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: nathnath5 on July 24, 2020, 01:35:12 PM



Title: Making transaction in python using pybitcointool / pycryptotool
Post by: nathnath5 on July 24, 2020, 01:35:12 PM
Hello,

I'm currently trying to understand how to make and sign bitcoin transaction in python using pycryptotool library (https://github.com/primal100/pybitcointools (https://github.com/primal100/pybitcointools)). There's an example explaining how to use this library but I don't quite understand the signing part :

Quote
Code:
> tx = c.mktx(inputs,outs)
> tx
{'locktime': 0, 'version': 1, 'ins': [{'outpoint': {'hash': '3be10a0aaff108766371fd4f4efeabc5b848c61d4aac60db6001464879f07508', 'index': 0}, 'amount': 180000000, 'script': '483045022100fccd16f6....']}
> tx2 = c.sign(tx,0,priv)
> tx2
{'locktime': 0, 'version': 1, 'ins': [{'outpoint': {'hash': '3be10a0aaff108766371fd4f4efeabc5b848c61d4aac60db6001464879f07508', 'index': 0}, 'amount': 180000000, 'script': '483045022100fccd16f619c5f8b8198f5a00f557f6542afaae10b2992733963c5b9c4042544c022041521e7ab2f4b58856e8554c651664af92f6abd58328c41bc652aea460a9a6a30141041f.....']}
> tx3 = c.sign(tx2,1,priv)
> tx3
{'locktime': 0, 'version': 1, 'ins': [{'outpoint': {'hash': '3be10a0aaff108766371fd4f4efeabc5b848c61d4aac60db6001464879f07508', 'index': 0}, 'amount': 180000000, 'script': '483045022100fccd16f619c5f8b8198f5a00f557f6542a...]}
> tx4 = c.serialize(tx)
> tx4
'01000000020875f07948460160db60ac4a1dc648b8c5abfe4e4ffd71637608f1af0a0ae13b000000008b483045022100fccd16f619c5f8b8198f5a00f557f6542afaae10b2992733963c5b9c4042544c022041521e7ab2f4b58856e8554c651664af92f6abd58328c41bc652aea460a9a6a30141041f763d81010db8ba3026fef4ac3dc1ad7ccc....'
> c.pushtx(tx4)

Why is the transaction signed 2 times ? And why is tx serialized and pushed instead of tx2 and tx3 which are signed ?

Thank you in advance for your time and your help,
your fellow beginner,

NathNath


Title: Re: Making transaction in python using pybitcointool / pycryptotool
Post by: ranochigo on July 25, 2020, 02:04:00 PM
In a Bitcoin transaction, each UTXO is signed individually and provides its own signature so that the nodes would know that the UTXO can be spent in that transaction. The transaction has 2 UTXOs so you have to sign both UTXOs, as referenced by index 0 and 1.


Title: Re: Making transaction in python using pybitcointool / pycryptotool
Post by: nathnath5 on July 25, 2020, 10:54:14 PM
Thank you for your answer :)

So when I create a transaction, i need to sign x times for the x UTXOs in the inputs and increment the index. Am I right?


Title: Re: Making transaction in python using pybitcointool / pycryptotool
Post by: TheArchaeologist on July 26, 2020, 07:48:33 AM
So when I create a transaction, i need to sign x times for the x UTXOs in the inputs and increment the index. Am I right?
I am not sure if I'm understanding you question correctly about the inputs and the index. But for the inputs you want to spent you don't have to increment the number. You simply using an unspent output from a previous transaction which has it's own index within the outputs of that transaction. If that didn't make any sense let me try to explain with a (simplified) example:

Code:
Transaction a1
Index: 0
Output : 1 BTC to your address

Transaction a2
Index: 0
Output : 2 BTC to someone elses address

Index: 1
Output: 3 BTC to someone elses address

Index: 2
Output: 4 BTC to your address

---
So nou you can create a transaction spending 5 BTC (1 from a1 and 4 from a2)

So as input for your transaction you will be using the outputs (including the indexes) from a1 and a2:

Inputs:
a1, index 0 > 1 BTC
a2, index 2 -> 4 BTC
As you can see from my simplified example you didn't have to increase indexes, you need to look them up from previous transaction outputs.


Title: Re: Making transaction in python using pybitcointool / pycryptotool
Post by: pooya87 on July 26, 2020, 08:10:13 AM
I am not sure if I'm understanding you question correctly about the inputs and the index. But for the inputs you want to spent you don't have to increment the number.

he probably meant the input index in the array of txins meaning "sign input at index 0" then "sign input at index 1",... in the TxIn[] { txin0, txin1,... }


Title: Re: Making transaction in python using pybitcointool / pycryptotool
Post by: HCP on July 29, 2020, 12:07:41 AM
... And why is tx serialized and pushed instead of tx2 and tx3 which are signed ?
Probably the result of a typo... as, logically, given the pattern the dev was using, it should have been "tx3" being serialized. :P


I believe that the reason this code actually works is the way Python deals with mutable object references and they way they are passed into/out of functions... In essence, tx2 and tx3 are actually just references to the original "tx" object (although I'm not a Python expert, so feel free to correct me if I'm wrong!)...

So, when the code is passing "tx2" and "tx3" to the sign() function... we're actually using the original "tx" object. So when the (accidental?) serialisation of "tx" happens at the end, it all works out. ;)