Ejemplo n.º 1
0
def serialize_header(inp):
    o = encode(inp['version'], 256, 4)[::-1] + \
        binascii.unhexlify(inp['prev_block_hash'])[::-1] + \
        binascii.unhexlify(inp['merkle_root'])[::-1] + \
        encode(inp['timestamp'], 256, 4)[::-1] + \
        encode(inp['bits'], 256, 4)[::-1] + \
        encode(inp['nonce'], 256, 4)[::-1]
    h = binascii.hexlify(bin_sha256(bin_sha256(o))[::-1]).decode()
    if inp.get('hash'):
        assert h == inp['hash'], (hashlib.sha256(o), inp['hash'])
    return binascii.hexlify(o).decode()
Ejemplo n.º 2
0
def xpub_to_hash(xpub: str):
    xpub_raw = Base58.check_decode(xpub)
    if xpub_raw[0:4] in (b'\x03\xe2\x5d\x7e'):  # remove ppub prefix
        xpub_raw = xpub_raw[4:]
    xpub_hash = bitcoin.bin_sha256(xpub_raw)
    xpub_hash = base64.b64encode(xpub_hash)
    return xpub_hash
def xpub_to_hash(xpub: str):
    xpub_raw = Base58.check_decode(xpub)
    if xpub_raw[0:4] in (b'\x02\xfe\x52\xcc', b'\x04\x88\xb2\x1e'):  # remove xpub prefix
        xpub_raw = xpub_raw[4:]
    xpub_hash = bitcoin.bin_sha256(xpub_raw)
    xpub_hash = base64.b64encode(xpub_hash)
    return xpub_hash
Ejemplo n.º 4
0
def test_right_pad(pad_contract):
    """Ensure we handle trailing zero in the address correctly."""

    address_bytes = get_address_as_bytes(pad_contract.call().rightPad())
    hash = bitcoin.bin_sha256(address_bytes)
    val = pad_contract.call().getHashRightPad()
    val = force_bytes(val)
    assert hash == val
Ejemplo n.º 5
0
def proc_sha256(ext, msg):
    print 'sha256 proc', msg.gas
    OP_GAS = 50 + (utils.ceil32(msg.data.size) / 32) * 50
    gas_cost = OP_GAS
    if msg.gas < gas_cost:
        return 0, 0, []
    d = msg.data.extract_all()
    o = [ord(x) for x in bitcoin.bin_sha256(d)]
    return 1, msg.gas - gas_cost, o
Ejemplo n.º 6
0
def test_left_pad(pad_contract):
    """Ensure we handle leading zero in the address correctly."""

    address_bytes = get_address_as_bytes(
        pad_contract.functions.leftPad().call())
    hash = bitcoin.bin_sha256(address_bytes)
    val = pad_contract.functions.getHashLeftPad().call()
    val = to_bytes(val)
    assert hash == val
Ejemplo n.º 7
0
def proc_sha256(ext, msg):
    print 'sha256 proc', msg.gas
    OP_GAS = 50 + (utils.ceil32(msg.data.size) / 32) * 50
    gas_cost = OP_GAS
    if msg.gas < gas_cost:
        return 0, 0, []
    d = msg.data.extract_all()
    o = [ord(x) for x in bitcoin.bin_sha256(d)]
    return 1, msg.gas - gas_cost, o
Ejemplo n.º 8
0
def proc_sha256(ext, msg):
    # print('sha256 proc', msg.gas)
    OP_GAS = opcodes.GSHA256BASE + \
        (utils.ceil32(msg.data.size) // 32) * opcodes.GSHA256WORD
    gas_cost = OP_GAS
    if msg.gas < gas_cost:
        return 0, 0, []
    d = msg.data.extract_all()
    o = [safe_ord(x) for x in bitcoin.bin_sha256(d)]
    return 1, msg.gas - gas_cost, o
Ejemplo n.º 9
0
def proc_sha256(ext, msg):
    # print('sha256 proc', msg.gas)
    OP_GAS = opcodes.GSHA256BASE + \
        (utils.ceil32(msg.data.size) // 32) * opcodes.GSHA256WORD
    gas_cost = OP_GAS
    if msg.gas < gas_cost:
        return 0, 0, []
    d = msg.data.extract_all()
    o = [safe_ord(x) for x in bitcoin.bin_sha256(d)]
    return 1, msg.gas - gas_cost, o
Ejemplo n.º 10
0
def deserialize_header(header: (str, bytes)):
    if isinstance(header, bytes):
        h, fmt = header, 'bin'
    else:
        h, fmt = binascii.unhexlify(header.encode()), 'hex'
    blockhash = bin_sha256(bin_sha256(h))[::-1]
    data = {
        "version": decode(h[:4][::-1], 256),
        "prev_block_hash": h[4:36][::-1],
        "merkle_root": h[36:68][::-1],
        "timestamp": decode(h[68:72][::-1], 256),
        "bits": decode(h[72:76][::-1], 256),
        "nonce": decode(h[76:80][::-1], 256),
        "hash": blockhash
    }
    if fmt == 'hex':
        data['prev_block_hash'] = binascii.hexlify(
            data['prev_block_hash']).decode()
        data['merkle_root'] = binascii.hexlify(data['merkle_root']).decode()
        data['hash'] = binascii.hexlify(data['hash']).decode()
    verify_pow(h, blockhash)
    return data
Ejemplo n.º 11
0
def print_extended_keys(child_chain_code, child_key, compressed_child_pub_key, depth, index, master,
                        parent_private_key):
    if master:
        parent_key_fingerprint = b'\x00\x00\x00\x00'
    else:
        pubkey = bitcoin.compress(bitcoin.privkey_to_pubkey(parent_private_key))
        sha_ = bitcoin.bin_sha256(pubkey)
        ripemd_ = bitcoin.bin_ripemd160(sha_)
        parent_key_fingerprint = ripemd_[0:4]
    debug_print("Parent Fingerprint: " + print_hex(parent_key_fingerprint))
    child_extended_public_key = serialize_extended_key(extended_public_key_version_bytes, depth, parent_key_fingerprint,
                                                       index,
                                                       extend_key(child_chain_code, compressed_child_pub_key))
    child_extended_private_key = serialize_extended_key(extended_private_key_version_bytes, depth,
                                                        parent_key_fingerprint, index,
                                                        extend_key(child_chain_code, b'\x00' + child_key))
    debug_print("Extended Public Key for (depth,index):   (" + str(depth) + "," + str(index) + ")  "
                + child_extended_public_key)
    debug_print("Extended Private Key for (depth,index): (" + str(depth) + "," + str(index) + ")  "
                + child_extended_private_key)
Ejemplo n.º 12
0
def address_to_hash(address):
    abin = base58.b58decode(address)
    hash = bitcoin.bin_sha256(abin)
    hash = base64.b64encode(hash)
    return hash.decode('ascii')
Ejemplo n.º 13
0
 def hash(cls, data):
     return b.bin_sha256(data)