Ejemplo n.º 1
0
def test_eth_getBlock_by_hash(web3):
    block_1 = web3.eth.getBlock(1)
    block_1_hash = block_1['hash']

    block_1_by_hash = web3.eth.getBlock(block_1_hash)
    assert block_1_by_hash
    assert block_1_by_hash['number'] == 1
    assert block_1_by_hash['hash'] == block_1_hash
    assert all(is_string(txn) for txn in block_1['transactions'])
Ejemplo n.º 2
0
def extract_ecdsa_signer(msg_hash, signature):
    msg_hash_bytes = decode_hex(msg_hash) if msg_hash.startswith(b'0x') else msg_hash
    signature_bytes = decode_hex(signature) if signature.startswith(b'0x') else signature

    pk = PublicKey(flags=ALL_FLAGS)
    rec_id = signature_bytes[64]
    if is_string(rec_id):
        rec_id = ord(rec_id)
    pk.public_key = pk.ecdsa_recover(
        msg_hash_bytes,
        pk.ecdsa_recoverable_deserialize(
            signature_bytes[:64], rec_id,
        ),
        raw=True,
    )
    pk_serialized = pk.serialize(compressed=False)
    address = add_0x_prefix(sha3(encode_pubkey(pk_serialized, 'bin')[1:])[-40:])
    return address
Ejemplo n.º 3
0
def test_eth_sign(web3):
    if isinstance(web3.currentProvider, TestRPCProvider):
        pytest.skip("testrpc doesn't implement `getBlockTransactionCount`")

    private_key_hex = b'0x5e95384d8050109aab08c1922d3c230739bc16976553c317e5d0b87b59371f2a'
    private_key = decode_hex(private_key_hex)

    # This imports the private key into the running geth instance and unlocks
    # the account so that it can sign things.
    # `0xa5df35f30ba0ce878b1061ae086289adff3ba1e0`
    address = force_bytes(web3.personal.importRawKey(private_key, "password"))
    web3.personal.unlockAccount(address, "password")

    assert add_0x_prefix(encode_hex(privtoaddr(private_key))) == add_0x_prefix(address)
    assert address == b'0xa5df35f30ba0ce878b1061ae086289adff3ba1e0'

    # the data to be signed
    data = b'1234567890abcdefghijklmnopqrstuvwxyz'
    # the hash of the data `0x089c33d56ed10bd8b571a0f493cedb28db1ae0f40c6cd266243001528c06eab3`
    data_hash = web3.sha3(data, encoding=None)
    data_hash_bytes = decode_hex(data_hash)

    assert force_bytes(data_hash) == sha3(data)

    priv_key = PrivateKey(flags=ALL_FLAGS)
    priv_key.set_raw_privkey(private_key)

    # sanit check the extract_ecdsa_signer function works as expected.
    vector_sig = priv_key.ecdsa_sign_recoverable(data_hash_bytes, raw=True, digest=sha3_256)
    vector_sig_bytes, rec_id = priv_key.ecdsa_recoverable_serialize(vector_sig)
    vector_sig_bytes_full = vector_sig_bytes + force_bytes(chr(rec_id))
    vector_address = extract_ecdsa_signer(data_hash_bytes, vector_sig_bytes_full)

    assert vector_address == address

    # Now have geth sign some data.
    signature_hex = web3.eth.sign(address, data)
    signature_bytes = decode_hex(signature_hex)

    actual_signer = extract_ecdsa_signer(data_hash_bytes, signature_bytes)

    with pytest.raises(AssertionError):
        # For some unknown reason, the extracted account from the signature
        # returned from geth is not present in the account list.
        assert actual_signer == address

    # Verify the signature against the public key derived from the
    # original private key.  It fails.
    rec_id = signature_bytes[64]
    if is_string(rec_id):
        rec_id = ord(rec_id)
    recoverable_signature = priv_key.ecdsa_recoverable_deserialize(
        signature_bytes[:64],
        rec_id,
    )
    signature = priv_key.ecdsa_recoverable_convert(recoverable_signature)
    is_valid = priv_key.pubkey.ecdsa_verify(
        msg=data,
        raw_sig=signature,
        digest=sha3_256,
    )
    with pytest.raises(AssertionError):
        # TODO: figure out why this happens and fix it.
        # For some unknown reason, the extracted account from the signature
        # returned from geth is not present in the account list.
        assert is_valid is True
Ejemplo n.º 4
0
def test_eth_getBlock_by_number(web3):
    assert web3.eth.blockNumber >= 1
    block_1 = web3.eth.getBlock(1)
    assert block_1
    assert block_1['number'] == 1
    assert all(is_string(txn) for txn in block_1['transactions'])
Ejemplo n.º 5
0
def test_eth_getBlock_by_number(web3):
    block_1 = web3.eth.getBlock(1)
    assert block_1
    assert block_1['number'] == 1
    assert all(is_string(txn) for txn in block_1['transactions'])