1

Hello everyone when I start this code on PYCHARM looks like no problem. But when I turned this code from python to exe and exe don't start and it says: import error: cannot import name "privtopub" from 'bitcoin'

import random

import requests

import base58

import hashlib

from bitcoin import privtopub, pubtoaddr

# Some code here

while True:

    i += 1

    first_character = "89ABCDEF"
    first_character.encode('utf-8')
    last_15_character = "0123456789ABCDEF"
    last_15_character.encode('utf-8')
    privateKeyByte = ("000000000000000000000000000000000000000000000000" + "".join(
        random.choice(first_character) for _ in range(1)) + "".join(
        random.choice(last_15_character) for _ in range(15))).encode('utf-8')
    privateKey = str(privateKeyByte)[2:-1]
    extendedPrivateKey = ("80" + privateKey)
    extendedPrivateKeyByte = bytearray.fromhex(extendedPrivateKey)
    firstHash = hashlib.sha256()
    firstHash.update(extendedPrivateKeyByte)
    secondHash = hashlib.sha256()
    firstHashFromHex = bytearray.fromhex(firstHash.hexdigest())
    secondHash.update(firstHashFromHex)
    checksum = secondHash.hexdigest()[0:8]
    finalPrivateKey = extendedPrivateKey + checksum
    encodedKey = bytes.fromhex(finalPrivateKey)
    wifKey = base58.b58encode(encodedKey)
    wifKeyString = str(wifKey)[2:-1]
    publicKey = privtopub(privateKey)
    btcAddress = pubtoaddr(publicKey)
  • Last 2 code i mean publicKey = privtopub(privateKey) and btcAddress = pubtoaddr(publicKey) cannot import from bitcoin ? why ?

  • Also i had another problem but i solved it turned my privateKeyByte to byte add this code " .encode('utf-8')." end of the privateKeyByte

  • When i delete this all code and to get to public adress with this way :

    import random
    import bitcoin
    import requests
    
    while True:
        i += 1
        liste_ilk_basamak = ("89ABCDEF")
        liste_son15_basamak = ("0123456789ABCDEF")
        private_key_random = "".join(random.choice(liste_ilk_basamak) for _ in range(1)) + "".join(
            random.choice(liste_son15_basamak) for _ in range(15))
        decoded_private_key = bitcoin.decode_privkey(private_key_random, 'hex')
        valid_private_key = 0 < decoded_private_key < bitcoin.N
        wif_encoded_private_key = bitcoin.encode_privkey(decoded_private_key, 'wif')
        public_key = bitcoin.fast_multiply(bitcoin.G, decoded_private_key)
        adress = bitcoin.pubkey_to_address(public_key)
  • İ Have This Error : AttributeError: Module "bitcoin" has no attribute 'decode_privkey'

I am trying to solve this problem 4 days and I am going to be crazy lost my mind :S help me pls :)

5
  • I think a link to the library that you are using can be helpful, not all the programmers here use python and know all the details about the library. A link can be helpful. Commented May 15, 2021 at 23:14
  • I have the same problem with the missing attribute 'decode_privkey'. This attribute existed, but now it's not existing. Strange. This happened with my script "doubleslow": github.com/vstoykovbg/doubleslow . Maybe "bitcoin" was installed with "pip3 install --user bitcoin". Commented Nov 22, 2021 at 4:59
  • $ python3 -m pip show bitcoin Name: bitcoin Version: 1.1.42 Summary: Python Bitcoin Tools Home-page: http://github.com/vbuterin/pybitcointools Author: Vitalik Buterin Author-email: [email protected] License: UNKNOWN Location: /home/valentin/.local/lib/python3.6/site-packages Requires: Commented Nov 22, 2021 at 5:02
  • Hmm... Strange. The package bitcoin 1.1.42 was released on Jan 11, 2016. Therefore, it was not changed. The attribute 'decode_privkey' should not be missing. Commented Nov 22, 2021 at 5:06
  • You can try to run your script with this distro: github.com/vstoykovbg/cryptopup With this distro my script was working without the error about the missing attribute 'decode_privkey'. Now I am with the latest Ubuntu 18 and this error occurs. Commented Nov 22, 2021 at 5:07

2 Answers 2

1

I solved the problem by installing the bitcoin package again.

I was getting this error message with my script doubleslow-base.py:

Traceback (most recent call last):
  File "./doubleslow-base.py", line 136, in <module>
    print_the_secrets(digest256_final)
  File "/home/valentin/python/doubleslow/doubleslow_module.py", line 301, in print_the_secrets
    my_256_bit_secret_WIF = bitcoin.encode_privkey(my_256_bit_secret_HEX,"wif_compressed")
AttributeError: module 'bitcoin' has no attribute 'encode_privkey'

After installing it again it works:

$ pip3 install --user bitcoin
Collecting bitcoin
Installing collected packages: bitcoin
Successfully installed bitcoin-1.1.42

Just in case I installed again all requirements:

$ pip3 install -r requirements.txt

I am not sure why this solved the problem. The script was working last year (when it was released) on the same system.

I was able to run your script with python2 and python3 by installing bitcoin with pip install --user bitcoin (to work with python2) and by defining i:

#!/usr/bin/python3

import random
import bitcoin
import requests

i=0

while True:
    i += 1
    liste_ilk_basamak = ("89ABCDEF")
    liste_son15_basamak = ("0123456789ABCDEF")
    private_key_random = "".join(random.choice(liste_ilk_basamak) for _ in range(1)) + "".join(
        random.choice(liste_son15_basamak) for _ in range(15))
    decoded_private_key = bitcoin.decode_privkey(private_key_random, 'hex')
    valid_private_key = 0 < decoded_private_key < bitcoin.N
    wif_encoded_private_key = bitcoin.encode_privkey(decoded_private_key, 'wif')
    public_key = bitcoin.fast_multiply(bitcoin.G, decoded_private_key)
    adress = bitcoin.pubkey_to_address(public_key)

    print(adress)

The module requests is not being used, so the line import requests can be removed. I added a print to see what happens and to confirm that the script is actually working.

First, I got this error:

$ python3 ./test0.py 
Traceback (most recent call last):
  File "./test0.py", line 9, in <module>
    i += 1
NameError: name 'i' is not defined

Obviously, you forgot to define i.

0

For this error:

AttributeError: module 'bitcoin' has no attribute 'privtoaddr'

I uninstalled bitcoin lib:

pip3 uninstall bitcoin

Then removed folder:

/home/user/.local/lib/python3.11/site-packages/bitcoin

with all files. Install the lib once again:

pip3 install bitcoin

Then the problem vanished.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.