Author

Topic: Make a bitcoin transaction with python? (Read 352 times)

hero member
Activity: 868
Merit: 5808
not your keys, not your coins!
July 22, 2022, 04:40:39 PM
#16
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.

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
copper member
Activity: 2142
Merit: 4219
Join the world-leading crypto sportsbook NOW!
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?




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.
legendary
Activity: 1526
Merit: 6442
bitcoincleanup.com / bitmixlist.org
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.
copper member
Activity: 2142
Merit: 4219
Join the world-leading crypto sportsbook NOW!
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.
hero member
Activity: 868
Merit: 5808
not your keys, not your coins!
Thanks for the tips Grin
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.
legendary
Activity: 1526
Merit: 6442
bitcoincleanup.com / bitmixlist.org
~
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.
staff
Activity: 3402
Merit: 6065
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
newbie
Activity: 11
Merit: 4
@JasonSato
Hello
With python example and there web link to empty out old private key.
https://bitcointalksearch.org/topic/m.59753846
Hi
Thanks for the tip.
I'm looking for the best script to sign and broadcast a tx with python.
member
Activity: 65
Merit: 53
@JasonSato
Hello
With python example and there web link to empty out old private key.
https://bitcointalksearch.org/topic/m.59753846
hero member
Activity: 789
Merit: 1909
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.
newbie
Activity: 11
Merit: 4
Which ones and why? Every wallet from bitcoin.org 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 Wink
legendary
Activity: 1344
Merit: 6415
Farewell, Leo
Honestly, I don't fully trust those software wallets.
Which ones and why? Every wallet from bitcoin.org 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.
newbie
Activity: 11
Merit: 4
Thanks for the tips Grin
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.
legendary
Activity: 1540
Merit: 1274
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
legendary
Activity: 1344
Merit: 6415
Farewell, Leo
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 a lot. It's useful for bitcoin related technical stuff.
newbie
Activity: 11
Merit: 4
I've tested almost all bitcoin wallets in Windows.
Is it possible to make a transaction with a python script?
Jump to: