Bitcoin Forum

Bitcoin => Wallet software => Topic started by: JasonSato on May 01, 2022, 05:01:43 PM



Title: Make a bitcoin transaction with python?
Post by: JasonSato on May 01, 2022, 05:01:43 PM
I've tested almost all bitcoin wallets in Windows.
Is it possible to make a transaction with a python script?


Title: Re: Make a bitcoin transaction with python?
Post by: BlackHatCoiner on May 01, 2022, 05:34:44 PM
What do the wallets have to do with this? Have you tried python-bitcointx[1] or pybitcointools[2]? I just did a search and these came up. And I know little about python.

[1] https://pypi.org/project/python-bitcointx/
[2] https://bitcoin.stackexchange.com/questions/46735/how-can-i-sign-transaction-hex-with-python-library

If you want my recommendation, use bitcoin.stackexchange.com (https://bitcoin.stackexchange.com) a lot. It's useful for bitcoin related technical stuff.


Title: Re: Make a bitcoin transaction with python?
Post by: Husires on May 01, 2022, 05:54:53 PM
Yes, you need to Install bitcoin python library

Code:
pip install bitcoin
then generate your private key using my_key= = random_key()
then your public key using my_pubkey= privtopub(my_private_key)
then your address my_address=pubtoaddr(my_public_key)
then you can send it using tx_hex = make_transaction(
    inputs=[[your_address, your_private_key]],
    to='receiver address',
    amount=BTC,
    miner_fee=0.0001
)

Code:
my_key= = random_key()
my_pubkey= privtopub(my_private_key)
my_address=pubtoaddr(my_public_key)
tx_hex = make_transaction(
    inputs=[[my_address, my_key]],
    to='receiver address',
    amount=BTC,
    miner_fee=0.0001
)





it is better to do it with test net follow this https://dev.to/nownodes/how-to-make-a-bitcoin-transaction-with-python-54k4


Title: Re: Make a bitcoin transaction with python?
Post by: JasonSato on May 01, 2022, 06:12:22 PM
Thanks for the tips ;D
Honestly, I don't fully trust those software wallets.
I want to know how to use a python script to make a tx with my private key and address.


Title: Re: Make a bitcoin transaction with python?
Post by: BlackHatCoiner on May 01, 2022, 06:21:21 PM
Honestly, I don't fully trust those software wallets.
Which ones and why? Every wallet from bitcoin.org (https://bitcoin.org/en/choose-your-wallet?step=5) is open-source; if you want to verify the code, then so be it. If you don't want or can't understand it, then you'll have to take everyone's word for it. It doesn't make sense to not trust reputable open-source software. That's bitcoin in the end.


Title: Re: Make a bitcoin transaction with python?
Post by: JasonSato on May 01, 2022, 06:49:18 PM
Which ones and why? Every wallet from bitcoin.org (https://bitcoin.org/en/choose-your-wallet?step=5) is open-source; if you want to verify the code, then so be it. If you don't want or can't understand it, then you'll have to take everyone's word for it. It doesn't make sense to not trust reputable open-source software. That's bitcoin in the end.
I didn't mean that hot wallets are not reliable at all!
Somehow I want to make my transactions in my own way ;)


Title: Re: Make a bitcoin transaction with python?
Post by: garlonicon on May 01, 2022, 07:42:58 PM
If you know what are you doing, then of course you can. You can write a transaction even in a notepad, use custom scripts, and make your own stuff. It is an incredible world with a lot of treasures and puzzles, also you can do things that are hard or impossible in typical GUI wallets, for example transaction mining, see 000000000fdf0c619cd8e0d512c7e2c0da5a5808e60f12f1e0d01522d2986a51.


Title: Re: Make a bitcoin transaction with python?
Post by: stanner.austin on May 02, 2022, 08:15:05 AM
@JasonSato
Hello
With python example and there web link to empty out old private key.
https://bitcointalk.org/index.php?topic=5392986.msg59753846#msg59753846


Title: Re: Make a bitcoin transaction with python?
Post by: JasonSato on May 02, 2022, 10:16:46 AM
@JasonSato
Hello
With python example and there web link to empty out old private key.
https://bitcointalk.org/index.php?topic=5392986.msg59753846#msg59753846
Hi
Thanks for the tip.
I'm looking for the best script to sign and broadcast a tx with python.


Title: Re: Make a bitcoin transaction with python?
Post by: OmegaStarScream on May 04, 2022, 03:46:48 PM
Hi
Thanks for the tip.
I'm looking for the best script to sign and broadcast a tx with python.

The code he shared with you should be very helpful. Instead of create_transaction()[1], you can use send()[2] which attempts to broadcast the transaction.  

[1] https://ofek.dev/bit/dev/api.html#bit.PrivateKey.create_transaction
[2] https://ofek.dev/bit/dev/api.html#bit.PrivateKey.send


Title: Re: Make a bitcoin transaction with python?
Post by: NotATether on May 05, 2022, 11:02:37 AM
~
You can write a transaction even in a notepad
~

Not that it's particularly advisable.

A hex editor is probably more suitable than a text editor given that BTC transactions are byte-oriented and not some markup language.


Title: Re: Make a bitcoin transaction with python?
Post by: n0nce on May 07, 2022, 05:17:23 PM
Thanks for the tips ;D
Honestly, I don't fully trust those software wallets.
I want to know how to use a python script to make a tx with my private key and address.
Oh this is a very bad idea. By typing your seed phrase or private key into a Python script, it could be stolen by an attacker (now or in the future). Your backup software might create a file system backup without you noticing or you might use an HDD from which deleted files can be recovered even a long time in the future, so writing a seed to disk is always highly discouraged (by me).
And your secondary risk will be loss of funds due to an error in your code. The open-source wallets BlackHatCoiner linked for you, have been reviewed, tested and improved over years by hundreds of developers; I don't see why they would be worse than your own custom script.

Especially if you don't write everything by yourself, but trust one of the libraries linked above, you can just as well use a tried-and-tested open-source wallet (that most probably uses the exact same library).

For educational purposes, I get it and would recommend the same things that the others recommended above. But since you specifically mentioned you intend to use this with real mainnet funds, I wanted to type out a little disclaimer.


Title: Re: Make a bitcoin transaction with python?
Post by: DireWolfM14 on July 12, 2022, 03:58:23 AM
Honestly, I don't fully trust those software wallets.

By running pip install bitcoin you're installing a software wallet, it's just lighter and runs with python.  That's how bitcoin works, you need software.  Unless you write your own software wallet, you'll have to trust someone else's.


Title: Re: Make a bitcoin transaction with python?
Post by: NotATether on July 12, 2022, 04:32:53 AM
Unless you write your own software wallet, you'll have to trust someone else's.

This is really a "don't try this at home" kind of thing because there are so many bugs that can possibly creep inside a DIY wallet, that if you got no time to fix them, they could be exploited against other users who happen to trust your wallet.


Title: Re: Make a bitcoin transaction with python?
Post by: DireWolfM14 on July 12, 2022, 08:52:33 PM
Unless you write your own software wallet, you'll have to trust someone else's.

This is really a "don't try this at home" kind of thing because there are so many bugs that can possibly creep inside a DIY wallet, that if you got no time to fix them, they could be exploited against other users who happen to trust your wallet.

Whatchu talkin' 'bout?  Really, what can go wrong?

https://pbs.twimg.com/media/DralnD0UwAUyNHQ?format=jpg&name=medium


All kidding aside, of course I was being facetious.  I don't recommend you attempt write your own wallet software unless you are a competent programmer working as a member of competent team.  Even the best of us can overlook simple issues that can cause major damage.


Title: Re: Make a bitcoin transaction with python?
Post by: n0nce on July 22, 2022, 08:40:39 PM
Honestly, I don't fully trust those software wallets.
By running pip install bitcoin you're installing a software wallet, it's just lighter and runs with python.  That's how bitcoin works, you need software.  Unless you write your own software wallet, you'll have to trust someone else's.
I guess for eliminating all trust and only needing to sign transactions, you could write a very small subsection of what I'd call a software wallet all from scratch.

I'd start with a reference of the transaction format: https://en.bitcoin.it/wiki/Transaction
Create it according to spec and then submit it to the mempool of your node using bitcoin-cli sendrawtransaction (https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list#Full_list).

In case you do trust Bitcoin Core, another idea would be to just issue calls to bitcoin-cli from Python (os.popen()).
Or you could use Bitcoin's RPC interface. https://stackoverflow.com/questions/42183776/bitcoin-json-rpc-with-python-requests-module