#!/usr/bin/env python3
"""Reproduce the serialized Namecoin block-19200 AuxPoW example.

Source: https://en.bitcoin.it/w/index.php?title=Merged_mining_specification&oldid=58250
All hex bytes transcribed from the preserved example. No network calls or
dependencies beyond Python's standard library. This is a check of the
transcription's internal consistency, not independent chain authentication.
"""
import hashlib
import struct
from datetime import datetime, timezone

def H(s):
    return bytes.fromhex(''.join(s.split()))

def sha256d(b):
    return hashlib.sha256(hashlib.sha256(b).digest()).digest()

def display(b):
    return b[::-1].hex()

def compact_target(bits):
    exponent = bits >> 24
    mantissa = bits & 0x007fffff
    return mantissa << (8 * (exponent - 3)) if exponent >= 3 else mantissa >> (8 * (3 - exponent))

nmc_header=H("""
    01 01 01 00
    36 90 9a c0 7a 16 73 da f6 5f a7 d8 28 88 2e 66 c9 e8 9f 85 46 cd d5 0a 9f b1 00 00 00 00 00 00
    0f 5c 65 49 bc d6 08 ab 7c 4e ac 59 3e 5b d5 a7 3b 2d 43 2e b6 35 18 70 8f 77 8f c7 dc df af 88
    8d 1a 90 4e 69 b2 00 1b 00 00 00 00
""")
parent_coinbase=H("""
    01 00 00 00 01
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff ff ff ff
    35
    04 5d ee 09 1a 01 4d 52 2c fa be 6d 6d d8 a7 c3 e0 1e 1e 95 bc ee 01 5e 6f cc 75 83 a2 ca 60 b7
    9e 5a 3a a0 a1 71 ed dd 34 4a da 90 3d 01 00 00 00 00 00 00 00
    ff ff ff ff 01 60 a0 10 2a 01 00 00 00 43
    41 04 f8 bb e9 7e d2 ac bc 5b ba 11 c6 8f 6f 1a 03 13 f9 18 f3 d3 c0 e8 47 50 55 e3 51 e3 bf 44
    2f 8c 8d ce e6 82 d2 45 7b dc 53 51 b7 0d d9 e3 40 26 76 6e ba 18 b0 6e ae e2 e1 02 ef d1 ab 63
    46 67 ac 00 00 00 00
""")
coinbase_branch=[H(h) for h in [
    '05 0a c4 a1 a1 e1 bc e0 c4 8e 55 5b 1a 9f 93 52 81 96 8c 72 d6 37 9b 24 72 9c a0 42 5a 3f c3 cb',
    '43 3c d3 48 b3 5e a2 28 06 cf 21 c7 b1 46 48 9a ef 69 89 55 1e b5 ad 23 73 ab 61 21 06 0f 30 34',
    '1d 64 87 57 c0 21 7d 43 e6 6c 57 ea ed 64 fc 18 20 ec 65 d1 57 f3 3b 74 19 65 18 3a 5e 0c 85 06',
    'ac 26 02 df e2 f5 47 01 2d 1c c7 50 04 d4 8f 97 ab a4 6b d9 93 0f f2 85 c9 f2 76 f5 bd 09 f3 56',
    'df 19 72 45 79 d6 5e c7 cb 62 bf 97 94 6d fc 6f b0 e3 b2 83 9b 7f da b3 7c db 60 e5 51 22 d3 5b',
]]
parent_header=H("""
    01 00 00 00
    08 be 13 29 5c 03 e6 7c b7 0d 00 da e8 1e a0 6e 78 b9 01 4e 5c eb 7d 9b a5 04 00 00 00 00 00 00
    e0 fd 42 db 8e f6 d7 83 f0 79 d1 26 be a1 2e 2d 10 c1 04 c0 92 7c d6 8f 95 4d 85 6f 9e 81 11 e5
    9a 23 90 4e 5d ee 09 1a 1c 65 50 86
""")
nmc_coinbase=H("""
    01 00 00 00 01
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff ff ff ff
    08 04 69 b2 00 1b 01 01 52 ff ff ff ff 01
    00 f2 05 2a 01 00 00 00 43
    41 04 89 fe 91 e6 28 47 57 5c 98 de ea b0 20 f6 5f df f1 7a 3a 87 0e bb 05 82 0b 41 4f 3d 80 97
    21 8e c9 a6 5f 1e 0a e0 ac 35 af 72 47 bd 79 ed 1f 2a 24 67 5f ff b5 aa 6f 96 20 e1 92 0a d4 bf
    5a a6 ac 00 00 00 00
""")

assert len(nmc_header)==len(parent_header)==80, (len(nmc_header),len(parent_header))
assert len(parent_coinbase)>100 and len(nmc_coinbase)>100
assert all(len(x)==32 for x in coinbase_branch)
nmc_hash=display(sha256d(nmc_header))
parent_hash=display(sha256d(parent_header))
assert nmc_hash == 'd8a7c3e01e1e95bcee015e6fcc7583a2ca60b79e5a3aa0a171eddd344ada903d'
assert parent_hash == '0000000000003d47277359fb969c43e3c7e7c0306a17f6444b8e91e19def03a9'

script_length=parent_coinbase[41]
script=parent_coinbase[42:42+script_length]
assert script_length == len(script) == 53
assert script.count(bytes.fromhex('fabe6d6d'))==1
magic_idx=script.index(bytes.fromhex('fabe6d6d'))
assert script[magic_idx+4:magic_idx+36].hex()==nmc_hash
assert struct.unpack('<I',script[magic_idx+36:magic_idx+40])[0]==1
assert struct.unpack('<I',script[magic_idx+40:magic_idx+44])[0]==0

parent_txid=display(sha256d(parent_coinbase))
root=sha256d(parent_coinbase)
for sibling in coinbase_branch:
    root=sha256d(root+sibling)
assert root==parent_header[36:68]
nmc_coinbase_txid=display(sha256d(nmc_coinbase))
assert sha256d(nmc_coinbase)==nmc_header[36:68]

nmc_target=compact_target(struct.unpack_from('<I',nmc_header,72)[0])
parent_target=compact_target(struct.unpack_from('<I',parent_header,72)[0])
parent_work_value=int(parent_hash,16)
assert parent_target < parent_work_value <= nmc_target
assert int(nmc_hash,16)>nmc_target

print('Namecoin block hash:',nmc_hash)
print('Namecoin coinbase txid:',nmc_coinbase_txid)
print('Parent candidate hash:',parent_hash)
print('Parent coinbase txid:',parent_txid)
print('Reconstructed parent transaction Merkle root:',display(root))
print('Parent-header Merkle root:',display(parent_header[36:68]))
print('Aux-chain branch: empty, zero index; commitment found in parent coinbase script')
print('Parent coinbase Merkle branch: 5 hashes, zero index; verified')
print('Namecoin single-transaction Merkle root: verified')
print('Parent hash satisfies Namecoin target and misses Bitcoin target: verified')
print('Namecoin header timestamp:',datetime.fromtimestamp(struct.unpack_from('<I',nmc_header,68)[0],tz=timezone.utc).isoformat())
print('Limit: reproduced a historical example; independent chain retrieval and validation still outstanding.')
