<p>here is an easy hashminer</p>
<pre class="lang-python prettyprint-override"><code>from hashlib import sha256
MAX_NONCE = 100000000000

def SHA256(text):
    return sha256(text.encode(&quot;ascii&quot;)).hexdigest()

def mine(block_number, transactions, previous_hash, prefix_zeros):
    prefix_str = '0'*prefix_zeros
    for nonce in range(MAX_NONCE):
        text = str(block_number) + transactions + previous_hash + str(nonce)
        new_hash = SHA256(text)
        if new_hash.startswith(prefix_str):
            print(f&quot;Yay! Successfully mined bitcoins with nonce value:{nonce}&quot;)
            return new_hash

    raise BaseException(f&quot;Couldn't find correct has after trying {MAX_NONCE} times&quot;)

if __name__=='__main__':
    transactions='''
    Dhaval-&gt;Bhavin-&gt;20,
    Mando-&gt;Cara-&gt;45
    '''
    difficulty=4 # try changing this to higher number and you will see it will take more time for mining as difficulty increases
    import time
    start = time.time()
    print(&quot;start mining&quot;)
    new_hash = mine(5,transactions,'0000000xa036944e29568d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7', difficulty)
    total_time = str((time.time() - start))
    print(f&quot;end mining. Mining took: {total_time} seconds&quot;)
    print(new_hash)
</code></pre>

CC BY-SA 4.0
2023-02-12T15:36:15.733Z
2023-02-18T11:21:02.310Z
2023-02-18T11:21:02.310Z
Vemund
<p>I'm new at BTC mining and I'm still learning. I'm almost medium level in Python.</p>
<p>I'm looking for a Python miner that automatically mines using the last block and stops when a new block is found and if I'm lucky, the algorithm adds the new block to the blockchain and sends the prize to a specific wallet address that I can add.</p>
<p>But now for days I'm still looking and nothing.</p>
<p>I find a simple one, it keeps looking, but even if it finds a new block I don't know how to add that to the blockchain, and I don't know if the last block has been changed or not.</p>
<p>Is there any Python algorithm that do all of that, or a few lines of Python code which I can change?</p>

CC BY-SA 4.0
2021-11-17T22:02:23.133Z
Does this answer your question? [In the ASIC-age, is it worth starting mining Bitcoin at home?](https://bitcoin.stackexchange.com/questions/41276/in-the-asic-age-is-it-worth-starting-mining-bitcoin-at-home)
RedGrittyBrick
CC BY-SA 4.0
2021-11-17T22:17:21.290Z
thank you for the answer it contains a lot of useful information, but no it did not answer my question I'm looking for a python code that I can modify and it run automatically (get the last block, mine, if I'm lucky it add the block to the block chain, and send price to a specific wallet address)
houhou
CC BY-SA 4.0
2021-11-18T21:24:34.653Z
I think the point that RedGrittyBrick was making is that you will never find a block mining with a regular computer. Today, you need ASICs to stand a chance to find a block in a conceivable time.
Murch
CC BY-SA 4.0
2021-11-19T18:50:59.043Z
thank you very much, yes I red the ASIC's article and am looking to learn how to do it with python just for fun if I want to really mine, I will mine another coin, just to learn how to do it and how to make it automatically work, if there is any python ago, or a few hint or an old article because I'm trying to find it for day's now and I find nothing, and thank's again for your help
houhou
<python><mining-software><solo-mining>
Simple Python code miner which automatically mines and stops
houhou
# Simple Python code miner which automatically mines and stops

(Asked by: username_0 on 2021-11-17)

I'm new at BTC mining and I'm still learning. I'm almost medium level in Python.

I'm looking for a Python miner that automatically mines using the last block and stops when a new block is found and if I'm lucky, the algorithm adds the new block to the blockchain and sends the prize to a specific wallet address that I can add.

But now for days I'm still looking and nothing.

I find a simple one, it keeps looking, but even if it finds a new block I don't know how to add that to the blockchain, and I don't know if the last block has been changed or not.

Is there any Python algorithm that do all of that, or a few lines of Python code which I can change?

**Tags:** python, mining-software, solo-mining

> **Comments:**
> **username_1**: Does this answer your question? [In the ASIC-age, is it worth starting mining Bitcoin at home?](https://bitcoin.stackexchange.com/questions/41276/in-the-asic-age-is-it-worth-starting-mining-bitcoin-at-home)  
> **username_0**: thank you for the answer it contains a lot of useful information, but no it did not answer my question I'm looking for a python code that I can modify and it run automatically (get the last block, mine, if I'm lucky it add the block to the block chain, and send price to a specific wallet address)  
> **username_2**: I think the point that username_1 was making is that you will never find a block mining with a regular computer. Today, you need ASICs to stand a chance to find a block in a conceivable time.  
> **username_0**: thank you very much, yes I red the ASIC's article and am looking to learn how to do it with python just for fun if I want to really mine, I will mine another coin, just to learn how to do it and how to make it automatically work, if there is any python ago, or a few hint or an old article because I'm trying to find it for day's now and I find nothing, and thank's again for your help  

---

## 1 answers

### Answer by username_3 on 2023-02-12. Score: 2

here is an easy hashminer

```python
from hashlib import sha256
MAX_NONCE = 100000000000

def SHA256(text):
    return sha256(text.encode("ascii")).hexdigest()

def mine(block_number, transactions, previous_hash, prefix_zeros):
    prefix_str = '0'*prefix_zeros
    for nonce in range(MAX_NONCE):
        text = str(block_number) + transactions + previous_hash + str(nonce)
        new_hash = SHA256(text)
        if new_hash.startswith(prefix_str):
            print(f"Yay! Successfully mined bitcoins with nonce value:{nonce}")
            return new_hash

    raise BaseException(f"Couldn't find correct has after trying {MAX_NONCE} times")

if __name__=='__main__':
    transactions='''
    Dhaval->Bhavin->20,
    Mando->Cara->45
    '''
    difficulty=4 # try changing this to higher number and you will see it will take more time for mining as difficulty increases
    import time
    start = time.time()
    print("start mining")
    new_hash = mine(5,transactions,'0000000xa036944e29568d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7', difficulty)
    total_time = str((time.time() - start))
    print(f"end mining. Mining took: {total_time} seconds")
    print(new_hash)

```


---

