Ejemplo n.º 1
0
    def sign_hash(msg_hash, wallet: Wallet):
        """
        This method use `personal_sign`for signing a message. This will always prepend the
        `\x19Ethereum Signed Message:\n32` prefix before signing.

        :param msg_hash:
        :param wallet: Wallet instance
        :return: signature
        """
        s = wallet.sign(msg_hash)
        return s.signature.hex()
Ejemplo n.º 2
0
def test_wallet_arguments():
    """Tests that a wallet's arguments are correctly setup."""
    web3 = Web3Provider.get_web3()

    private_key = os.environ.get("TEST_PRIVATE_KEY1")
    assert private_key, "envvar TEST_PRIVATE_KEY1 is not set."

    # Create wallet with valid private key
    wallet = Wallet(web3, private_key=private_key)
    assert wallet.private_key == private_key
    assert wallet.address
    signed_message = wallet.sign(
        add_ethereum_prefix_and_hash_msg("msg-to-sign"))
    assert signed_message

    # Create wallet with encrypted key and password
    password = "******"
    encrypted_key = web3.eth.account.encrypt(private_key, password)
    w2 = Wallet(web3, encrypted_key=encrypted_key, password=password)
    assert w2.address == wallet.address
    assert w2.private_key == wallet.private_key
    assert w2.sign(
        add_ethereum_prefix_and_hash_msg("msg-to-sign")) == signed_message

    # create wallet with missing arguments
    with pytest.raises(AssertionError):
        Wallet(web3)
    with pytest.raises(AssertionError):
        Wallet(web3, encrypted_key=encrypted_key)
    with pytest.raises(AssertionError):
        Wallet(web3, password=password)

    # Create wallet with invalid private_key
    invalid_key = "332233444332"
    with pytest.raises(ValueError):
        Wallet(web3, private_key=invalid_key)

    with pytest.raises(AssertionError):
        Wallet(web3, private_key=None)
Ejemplo n.º 3
0
def test_wallet_arguments(web3, config):
    """Tests that a wallet's arguments are correctly setup."""
    private_key = os.environ.get("TEST_PRIVATE_KEY1")
    assert private_key, "envvar TEST_PRIVATE_KEY1 is not set."

    # Create wallet with valid private key
    wallet = Wallet(
        web3,
        private_key=private_key,
        block_confirmations=config.block_confirmations,
        transaction_timeout=config.transaction_timeout,
    )
    assert wallet.private_key == private_key, "Private keys are different."
    assert wallet.address, "The wallet does not have a wallet address."
    signed_message = wallet.sign(encode_defunct(text="msg-to-sign"))
    assert signed_message, "Signed message is None."

    # create wallet with missing arguments
    with pytest.raises(TypeError):
        Wallet(web3)

    # Create wallet with invalid private_key
    invalid_key = "332233444332"
    with pytest.raises(ValueError):
        Wallet(
            web3,
            private_key=invalid_key,
            block_confirmations=config.block_confirmations,
            transaction_timeout=config.transaction_timeout,
        )

    with pytest.raises(TypeError):
        Wallet(
            web3,
            private_key=None,
            block_confirmations=config.block_confirmations,
            transaction_timeout=config.transaction_timeout,
        )