0% found this document useful (0 votes)
18 views3 pages

Minerador

This document contains a Python script for connecting to the AntPool mining pool using HMAC authentication. It includes functions for generating signatures, connecting to the pool, authenticating with the API, and performing mining operations. The script continuously listens for mining jobs and attempts to find valid nonces to submit results back to the pool.

Uploaded by

Alison Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Minerador

This document contains a Python script for connecting to the AntPool mining pool using HMAC authentication. It includes functions for generating signatures, connecting to the pool, authenticating with the API, and performing mining operations. The script continuously listens for mining jobs and attempts to find valid nonces to submit results back to the pool.

Uploaded by

Alison Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import hashlib

import hmac
import json
import socket
import struct
import time
import requests

# Suas credenciais do AntPool


api_key = "290a8b0f407e498fa8e01aff0ffdf87d"
api_secret = "40bbcc32482b409dbea5c8aec067db22"

# Configurações do minerador
POOL_URL = "ss.antpool.com"
POOL_PORT = 3333
USERNAME = "sincoraalison@gmail.com" # Substitua pelo seu username do AntPool
PASSWORD = "x" # Pode ser "x" ou qualquer coisa simples

# Função para gerar a assinatura HMAC


def generate_signature(api_key, api_secret, nonce):
message = f"{api_key}{nonce}".encode('utf-8')
return hmac.new(api_secret.encode('utf-8'), message,
hashlib.sha256).hexdigest()

# Função para conectar ao pool


def connect_to_pool():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((POOL_URL, POOL_PORT))
return sock

# Função para autenticar com a API


def authenticate_with_api():
nonce = str(int(time.time() * 1000))
signature = generate_signature(api_key, api_secret, nonce)

api_url = "https://antpool.com/api/v1/account/walletBalance.htm"
params = {
'key': api_key,
'nonce': nonce,
'sign': signature
}

response = requests.get(api_url, params=params)

if response.status_code == 200:
return response.json()
else:
print(f"Erro ao acessar a API: {response.status_code}")
return None

# Função para enviar uma requisição de autenticação


def send_auth(sock):
auth_message = {
"id": 1,
"method": "mining.authorize",
"params": [USERNAME, PASSWORD]
}
sock.sendall(json.dumps(auth_message).encode('utf-8') + b'\n')
# Função para minerar
def mine(sock):
while True:
response = sock.recv(1024).decode('utf-8').strip()
for line in response.splitlines():
if line:
message = json.loads(line)
if 'method' in message and message['method'] == 'mining.notify':
params = message['params']
job_id = params[0]
prevhash = params[1]
coinbase1 = params[2]
coinbase2 = params[3]
merkle_branches = params[4]
version = params[5]
nbits = params[6]
ntime = params[7]
clean_jobs = params[8]

# Montar o coinbase
coinbase = coinbase1 + struct.pack("<I", 0) + coinbase2
coinbase_hash =
hashlib.sha256(hashlib.sha256(coinbase).digest()).digest()

# Montar o merkle root


merkle_root = coinbase_hash
for branch in merkle_branches:
merkle_root = hashlib.sha256(hashlib.sha256(merkle_root +
bytes.fromhex(branch)).digest()).digest()

# Montar o cabeçalho do bloco


block_header = (bytes.fromhex(version) +
bytes.fromhex(prevhash) +
merkle_root +
struct.pack("<I", ntime) +
struct.pack("<I", nbits))

# Iniciar a mineração
for nonce in range(0, 0xFFFFFFFF):
block_header_with_nonce = block_header + struct.pack("<I",
nonce)
hash_result =
hashlib.sha256(hashlib.sha256(block_header_with_nonce).digest()).hexdigest()

# Verificar se o hash é válido


if int(hash_result, 16) < int(nbits, 16):
print(f"Nonce encontrado: {nonce}, Hash:
{hash_result}")
submit_result(sock, job_id, nonce, ntime)
break

# Função para enviar o resultado da mineração


def submit_result(sock, job_id, nonce, ntime):
result_message = {
"id": 1,
"method": "mining.submit",
"params": [USERNAME, job_id, hex(nonce)[2:], hex(ntime)[2:]]
}
sock.sendall(json.dumps(result_message).encode('utf-8') + b'\n')
# Função principal
def main():
# Autentica com a API do AntPool
api_response = authenticate_with_api()
if api_response:
print("Autenticação bem-sucedida com a API do AntPool.")

sock = connect_to_pool()
send_auth(sock)
mine(sock)

if _name_ == "_main_":
main()

You might also like