Skip to content

Instantly share code, notes, and snippets.

View humbdrag's full-sized avatar

Humbled Drugman humbdrag

View GitHub Profile
@humbdrag
humbdrag / btc_submit_rpc.py
Created January 27, 2022 21:12
Make submission of the mined block pyminer
submission_str = create_submission_str(block_template)
# Define parameters for the RPC.
parameters = {
"host": "127.0.0.1",
"port": "8332",
"rpcuser": "Your Username",
"rpcpass": "Your Password",
"rpcurl": "http://127.0.0.1:8332"
}
@humbdrag
humbdrag / btc_sub_str.py
Created January 27, 2022 21:08
Create submission string
def create_submission_str(block: Dict) -> str:
submission = (
calc_block_header(block).hex()
+ get_le_var_hex(len(block['transactions']))
)
for tx in block['transactions']:
submission += tx['data']
return submission
@humbdrag
humbdrag / btc_cmp_target_hash.py
Created January 27, 2022 21:03
Compare with target hash
block_template['merkleroot'] = calc_merkle_root(
[transaction['hash'] for transaction in block_template['transactions']]
)
block_template['nonce'] = 0
block_header = calc_block_header(block_template)
block_hash = sha256_double_hash(block_header)
block_hash < target_hash
@humbdrag
humbdrag / btc_calc_block_header.py
Created January 27, 2022 20:55
Calculate block header for pyminer
def calc_block_header(block: Dict) -> bytes:
return (
struct.pack("<L", block["version"])
+ bytes.fromhex(block["previousblockhash"])[::-1]
+ bytes.fromhex(block["merkleroot"])[::-1]
+ struct.pack("<L", block["curtime"])
+ bytes.fromhex(block["bits"])[::-1]
+ struct.pack("<L", block["nonce"])
)
@humbdrag
humbdrag / btc_calc_merkle_root.py
Created January 26, 2022 22:38
Calculate merkle root for pyminer
def calc_merkle_root(transactions: List[str]) -> str:
# Convert transactions into big-endian bytes.
be_hashes = [
bytes.fromhex(transaction)[::-1]
for transaction in transactions
]
# We combine the hashes pairwise until there is only 1 left.
while len(be_hashes) > 1:
@humbdrag
humbdrag / btc_coinbase_insert.py
Last active January 27, 2022 20:59
Insert Coinbase transaction pyminer
from essential_generators import DocumentGenerator
import hashlib
def sha256_double_hash(target: str) -> str:
return hashlib.sha256(
hashlib.sha256(target).digest()
).digest()[::-1]
@humbdrag
humbdrag / btc_coinbase_gen.py
Created January 26, 2022 22:03
Create coinbase transaction for pyminer
def get_le_hex(value: int, width: int) -> str:
return value.to_bytes(width, byteorder='little').hex()
def get_le_var_hex(value: int) -> str:
if value < 0xfd:
return get_le_hex(value, 1)
if value <= 0xffff:
return "fd" + get_le_hex(value, 2)
if value <= 0xffffffff:
@humbdrag
humbdrag / btc_calc_target.py
Created January 26, 2022 20:54
Calculate target hash for bitcoin mining
def calc_target(bits: str) -> bytes:
"""
Decompress the target from a compact format.
"""
bits = bytes.fromhex(bits)
# Extract the parts.
byte_length = bits[0] - 3
significand = bits[1:]
@humbdrag
humbdrag / btc_min_rpc.py
Created January 17, 2022 22:40
BTC Minimal RPC
import base64
import json
import random
import urllib.request
import urllib.error
import urllib.parse
# Define parameters for the RPC.
parameters = {
"host": "127.0.0.1",
@humbdrag
humbdrag / btc_gen_rpcauth.sh
Created January 17, 2022 22:18
generate rpcauth
git clone git@github.com:bitcoin/bitcoin.git
cd bitcoin/share/rpcauth
rpcauth.py [Your Username] [Your Password]