/
SoloMiner.py
205 lines (167 loc) · 10.1 KB
/
SoloMiner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from colorama import Fore, Style
from threading import Thread
import requests
import binascii
import hashlib
import logging
import socket
import random
import json
import time
import sys
#'''
#
# /\ /\ /\ /\ /\ /\ /\
# /\//\\/\ /\//\\/\ /\//\\/\ /\//\\/\ /\//\\/\ /\//\\/\ /\//\\/\
# /\//\\\///\\/\//\\\///\\/\//\\\///\\/\//\\\///\\/\//\\\///\\/\//\\\///\\/\//\\\///\\/\
# /\\\//\/\\///\\\//\/\\///\\\//\/\\///\\\//\/\\///\\\//\/\\///\\\//\/\\///\\\//\/\\///\\
# \//\/ \/\\//
# \/ ____ ___ _ ___ __ __ ___ _ _ _____ ____ \/
# /\ / ___| / _ \| | / _ \ | \/ |_ _| \ | | ____| _ \ /\
# /\\ \___ \| | | | | | | | | | |\/| || || \| | _| | |_) | //\\
# \// ___) | |_| | |__| |_| | | | | || || |\ | |___| _ < \\//
# \/ |____/ \___/|_____\___/ |_| |_|___|_| \_|_____|_| \_\ \/
# /\ /\
# \/ PROGRAMMER = MMDRZA || Official WebSite : httpS://Mmdrza.Com \/
# /\ X4@mmdrza.Com / Github.Com/PyMmdrza / Dev.TO/Mmdrza /\
# /\\/\ /\//\\
# \///\\/\//\\\///\\/\//\\\///\\/\//\\\///\\/\//\\\///\\/\//\\\///\\/\//\\\///\\/\//\\\//
# \/\\///\\\//\/\\///\\\//\/\\///\\\//\/\\///\\\//\/\\///\\\//\/\\///\\\//\/\\///\\\//\/
# \/\\//\/ \/\\//\/ \/\\//\/ \/\\//\/ \/\\//\/ \/\\//\/ \/\\//\/
# \/ \/ \/ \/ \/ \/ \/
#
#'''
soloxminer = '''
███████╗ ██████╗ ██╗ ██████╗
██╔════╝██╔═══██╗██║ ██╔═══██╗
███████╗██║ ██║██║ ██║ ██║
╚════██║██║ ██║██║ ██║ ██║
███████║╚██████╔╝███████╗╚██████╔╝
╚══════╝ ╚═════╝ ╚══════╝ ╚═════╝
███╗ ███╗██╗███╗ ██╗███████╗██████╗
████╗ ████║██║████╗ ██║██╔════╝██╔══██╗
██╔████╔██║██║██╔██╗ ██║█████╗ ██████╔╝
██║╚██╔╝██║██║██║╚██╗██║██╔══╝ ██╔══██╗
██║ ╚═╝ ██║██║██║ ╚████║███████╗██║ ██║
╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝
'''
mmdrza = '''
|======================================================|
|=========== ╔╦╗╔╦╗╔╦╗╦═╗╔═╗╔═╗ ╔═╗╔═╗╔╦╗ ============|
|=========== ║║║║║║ ║║╠╦╝╔═╝╠═╣ ║ ║ ║║║║ ============|
|=========== ╩ ╩╩ ╩═╩╝╩╚═╚═╝╩ ╩o╚═╝╚═╝╩ ╩ ============|
|------------------------------------------------------|
|- WebSite ------------------------------- Mmdrza.Com -|
|- MAIL ---------------------------- X4@Mmdrza.Com -|
|- DEV ---------------------------- DEV.to/Mmdrza -|
|- GiTHUB ---------------------- Github.Com/PyMmdrza -|
|- MEDIUM -------------- PythonWithMmdrza.Medium.Com -|
|======================================================|
'''
def delay_print(s):
for c in s:
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.1)
print(Fore.RED,soloxminer)
print(Fore.YELLOW,mmdrza)
cHeight = 0
inpAdd = input('[*] INSERT HERE YOUR ADDRESS BITCOIN WALLET For Withdrawal : ')
address = str(inpAdd)
print(Fore.YELLOW,'\nBitcoin Wallet Address ===>> ',Fore.GREEN,str(address))
print(Fore.MAGENTA,'\n------------------------------------------------------------------------------',Style.RESET_ALL)
delay_print(' Your Bitcoin Wallet Address Added For Mining Now ...')
print(Fore.MAGENTA,'\n------------------------------------------------------------------------------',Style.RESET_ALL)
time.sleep(3)
def logg(msg):
logging.basicConfig(level=logging.INFO, filename="miner.log", format='%(asctime)s %(message)s') # include timestamp
logging.info(msg)
def get_current_block_height():
# Returns the current network best height
r = requests.get('https://blockchain.info/latestblock')
return int(r.json()['height'])
def newBlockListener():
global cHeight
while True:
network_height = get_current_block_height()
if network_height > cHeight:
logg('[*] Network has new height %d ' % network_height)
logg('[*] Our local is %d' % cHeight)
cHeight = network_height
logg('[*] Our new local after update is %d' % cHeight)
# respect Api
time.sleep(40)
def BitcoinMiner(restart=False):
if restart:
time.sleep(2)
logg('[*] Bitcoin Miner Restarted')
else:
logg('[*] Bitcoin Miner Started')
print('[*] Bitcoin Miner Started')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('solo.ckpool.org', 3333))
sock.sendall(b'{"id": 1, "method": "mining.subscribe", "params": []}\n')
lines = sock.recv(1024).decode().split('\n')
response = json.loads(lines[0])
sub_details, extranonce1, extranonce2_size = response['result']
sock.sendall(b'{"params": ["' + address.encode() + b'", "password"], "id": 2, "method": "mining.authorize"}\n')
response = b''
while response.count(b'\n') < 4 and not (b'mining.notify' in response): response += sock.recv(1024)
responses = [json.loads(res) for res in response.decode().split('\n') if
len(res.strip()) > 0 and 'mining.notify' in res]
job_id, prevhash, coinb1, coinb2, merkle_branch, version, nbits, ntime, clean_jobs = responses[0]['params']
target = (nbits[2:] + '00' * (int(nbits[:2], 16) - 3)).zfill(64)
extranonce2 = hex(random.randint(0, 2 ** 32 - 1))[2:].zfill(2 * extranonce2_size) # create random
coinbase = coinb1 + extranonce1 + extranonce2 + coinb2
coinbase_hash_bin = hashlib.sha256(hashlib.sha256(binascii.unhexlify(coinbase)).digest()).digest()
merkle_root = coinbase_hash_bin
for h in merkle_branch:
merkle_root = hashlib.sha256(hashlib.sha256(merkle_root + binascii.unhexlify(h)).digest()).digest()
merkle_root = binascii.hexlify(merkle_root).decode()
merkle_root = ''.join([merkle_root[i] + merkle_root[i + 1] for i in range(0, len(merkle_root), 2)][::-1])
work_on = get_current_block_height()
print(Fore.GREEN, 'Working on current Network height',Fore.WHITE, work_on)
print(Fore.YELLOW,'Current TARGET =', Fore.RED, target)
z = 0
while True:
if cHeight > work_on:
logg('[*] Restarting Miner')
BitcoinMiner(restart=True)
break
nonce = hex(random.randint(0, 2 ** 32 - 1))[2:].zfill(8) # nnonve #hex(int(nonce,16)+1)[2:]
blockheader = version + prevhash + merkle_root + nbits + ntime + nonce + \
'000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000'
hash = hashlib.sha256(hashlib.sha256(binascii.unhexlify(blockheader)).digest()).digest()
hash = binascii.hexlify(hash).decode()
if hash.startswith('000000000000000000000'): logg('hash: {}'.format(hash))
print(Fore.GREEN,str(z),' HASH :', Fore.YELLOW,' 000000000000000000000{}'.format(hash), end='\r')
z += 1
if hash.startswith('000000000000000000'): logg('hash: {}'.format(hash))
z += 1
print(Fore.YELLOW,str(z), 'HASH :', Fore.RED,' 000000000000000000{}'.format(hash), end='\r')
z += 1
if hash.startswith('000000000000000'): logg('hash: {}'.format(hash))
print(Fore.BLUE,str(z), 'HASH :', Fore.GREEN,' 000000000000000{}'.format(hash), end='\r')
z += 1
if hash.startswith('000000000000'): logg('hash: {}'.format(hash))
print(Fore.MAGENTA,str(z),'HASH :', Fore.YELLOW,' 000000000000{}'.format(hash), end='\r')
z += 1
if hash.startswith('0000000'): logg('hash: {}'.format(hash))
print(Fore.CYAN,str(z),'HASH :', Fore.YELLOW,'0000000{}'.format(hash), end='\r')
z += 1
if hash < target:
print('[*] New block mined')
logg('[*] success!!')
logg(blockheader)
logg('hash: {}'.format(hash))
payload = bytes('{"params": ["' + address + '", "' + job_id + '", "' + extranonce2 \
+ '", "' + ntime + '", "' + nonce + '"], "id": 1, "method": "mining.submit"}\n', 'utf-8')
sock.sendall(payload)
logg(payload)
ret = sock.recv(1024)
logg(ret)
return True
if __name__ == '__main__':
Thread(target=newBlockListener).start()
time.sleep(2)
Thread(target=BitcoinMiner).start()