Bitcoin Forum
November 10, 2024, 05:54:39 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Making transaction in python using pybitcointool / pycryptotool  (Read 221 times)
nathnath5 (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 2


View Profile
July 24, 2020, 01:35:12 PM
Merited by ABCbits (1), ranochigo (1)
 #1

Hello,

I'm currently trying to understand how to make and sign bitcoin transaction in python using pycryptotool library (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
ranochigo
Legendary
*
Offline Offline

Activity: 3038
Merit: 4420


Crypto Swap Exchange


View Profile
July 25, 2020, 02:04:00 PM
Merited by Coding Enthusiast (2), ABCbits (1)
 #2

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.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
nathnath5 (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 2


View Profile
July 25, 2020, 10:54:14 PM
 #3

Thank you for your answer Smiley

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?
TheArchaeologist
Sr. Member
****
Offline Offline

Activity: 310
Merit: 727


---------> 1231006505


View Profile WWW
July 26, 2020, 07:48:33 AM
Merited by ABCbits (1)
 #4

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.

Sooner or later you're going to realize, just as I did, that there's a difference between knowing the path and walking the path
pooya87
Legendary
*
Offline Offline

Activity: 3626
Merit: 11027


Crypto Swap Exchange


View Profile
July 26, 2020, 08:10:13 AM
 #5

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,... }

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4361

<insert witty quote here>


View Profile
July 29, 2020, 12:07:41 AM
 #6

... 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. Tongue


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. Wink

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!