Exemplo n.º 1
0
def test_eth_account_sign_transaction_from_eth_test(acct, transaction_info):
    expected_raw_txn = transaction_info['signed']
    key = transaction_info['key']

    transaction = dissoc(transaction_info, 'signed', 'key', 'unsigned')

    # validate r, in order to validate the transaction hash
    # There is some ambiguity about whether `r` will always be deterministically
    # generated from the transaction hash and private key, mostly due to code
    # author's ignorance. The example test fixtures and implementations seem to agree, so far.
    # See ecdsa_raw_sign() in /eth_keys/backends/native/ecdsa.py
    signed = acct.sign_transaction(transaction, key)
    assert signed.r == Web3.toInt(hexstr=expected_raw_txn[-130:-66])

    # confirm that signed transaction can be recovered to the sender
    expected_sender = acct.from_key(key).address
    assert acct.recover_transaction(signed.rawTransaction) == expected_sender
Exemplo n.º 2
0
 def mine_blocks(self, num_blocks=1, coinbase=None):
     for _ in range(num_blocks):
         block_to_mine = dissoc(self.block, 'hash')
         block_hash = fake_rlp_hash(block_to_mine)
         mined_block = assoc(block_to_mine, 'hash', block_hash)
         assign_block_info = compose(
             partial(assoc, key='block_number', value=mined_block['number']),
             partial(assoc, key='block_hash', value=mined_block['hash']),
         )
         mined_block['transactions'] = tuple(
             assign_block_info(transaction)
             for transaction
             in mined_block['transactions']
         )
         self.blocks.append(mined_block)
         self.block = make_block_from_parent(mined_block)
         yield block_hash
def test_build_transaction_with_contract_with_arguments(
        web3, skip_if_testrpc, math_contract, transaction_args, method_args,
        method_kwargs, expected, skip_testrpc, buildTransaction):
    if skip_testrpc:
        skip_if_testrpc(web3)

    txn = buildTransaction(contract=math_contract,
                           contract_function='increment',
                           func_args=method_args,
                           func_kwargs=method_kwargs,
                           tx_params=transaction_args)
    expected['to'] = math_contract.address
    assert txn is not None
    if 'gas' in transaction_args:
        assert txn['gas'] == transaction_args['gas']
    else:
        assert 'gas' in txn
    assert dissoc(txn, 'gas') == expected
Exemplo n.º 4
0
def test_build_transaction_with_contract_class_method(
        w3,
        MathContract,
        math_contract,
        build_transaction):
    txn = build_transaction(
        contract=MathContract,
        contract_function='increment',
        tx_params={'to': math_contract.address},
    )
    assert dissoc(txn, 'gas') == {
        'to': math_contract.address,
        'data': '0xd09de08a',
        'value': 0,
        'maxFeePerGas': 2750000000,
        'maxPriorityFeePerGas': 10 ** 9,
        'chainId': 131277322940537,
    }
    def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
        result = make_request(method, params)

        # As of v1.8, Geth returns errors when you request a
        # receipt for a transaction that is not in the chain.
        # It used to return a result of None, so we simulate the old behavior.

        if method == "eth_getTransactionReceipt" and "error" in result:
            is_geth = str(web3.clientVersion).startswith("Geth")
            if is_geth and result["error"]["code"] == -32000:
                return assoc(
                    dissoc(result, "error"),
                    "result",
                    None,
                )
            else:
                return result
        else:
            return result
    def middleware(method, params):
        result = make_request(method, params)

        # As of v1.8, Geth returns errors when you request a
        # receipt for a transaction that is not in the chain.
        # It used to return a result of None, so we simulate the old behavior.

        if method == 'eth_getTransactionReceipt' and 'error' in result:
            is_geth = web3.clientVersion.startswith('Geth')
            if is_geth and result['error']['code'] == -32000:
                return assoc(
                    dissoc(result, 'error'),
                    'result',
                    None,
                )
            else:
                return result
        else:
            return result
Exemplo n.º 7
0
    def build_transaction(self, *args: Tuple) -> SignedTransaction:
        args, tx = _get_tx(args)

        if not tx["from"]:
            raise AttributeError(
                "Final argument must be a dict of transaction parameters that "
                "includes a `from` field specifying the sender of the transaction"
            )

        if "chainId" not in tx:
            # defaults to chainId = 1
            tx.update({"chainId": 1})
        if "gas" not in tx:
            raise AttributeError("we need 'gas' parameter.")
        if "gasPrice" not in tx:
            raise AttributeError("we need 'gasPrice' parameter.")
        if "nonce" not in tx:
            raise AttributeError("we need 'nonce' parameter.")

        tx.update({"to": self._address, "data": self.encode_input(*args)})

        account = tx["from"]
        signed_txn = account.sign_transaction(dissoc(tx, 'from'))
        return signed_txn
Exemplo n.º 8
0
def transaction_normalizer(transaction):
    return dissoc(transaction, 'chainId')
Exemplo n.º 9
0
def _transaction_normalizer(transaction: TxParams) -> TxParams:
    return dissoc(transaction, 'chainId')
Exemplo n.º 10
0
def remove_key_if(key, remove_if, input_dict):
    if key in input_dict and remove_if(input_dict):
        return dissoc(input_dict, key)
    else:
        return input_dict
Exemplo n.º 11
0
 def send_signed_transaction(self, signed_transaction):
     transaction = dissoc(signed_transaction, 'r', 's', 'v')
     return self.send_transaction(transaction)
Exemplo n.º 12
0
def remove_key_if(key: Any, remove_if: Callable[[Dict[Any, Any]], bool],
                  input_dict: Dict[Any, Any]) -> Dict[Any, Any]:
    if key in input_dict and remove_if(input_dict):
        return dissoc(input_dict, key)
    else:
        return input_dict
Exemplo n.º 13
0
def uninstall_from_ethpm_lock(package_name: str, ethpm_lock: Path) -> None:
    old_lock = json.loads(ethpm_lock.read_text())
    new_lock = dissoc(old_lock, package_name)
    with atomic_replace(ethpm_lock) as ethpm_lock_file:
        ethpm_lock_file.write(json.dumps(new_lock, sort_keys=True, indent=4))
        ethpm_lock_file.write("\n")
Exemplo n.º 14
0
    "to": BURN_ADDRESS,
    "gas_price": NON_DEFAULT_GAS_PRICE,
    "value": 0,
    "gas": 21000,
}


TRANSACTION_WTH_NONCE = assoc(SIMPLE_TRANSACTION, 'nonce', 0)

CONTRACT_TRANSACTION_EMPTY_TO = {
    "to": '',
    "gas_price": NON_DEFAULT_GAS_PRICE,
    "value": 0,
    "gas": 100000,
}
CONTRACT_TRANSACTION_MISSING_TO = dissoc(CONTRACT_TRANSACTION_EMPTY_TO, 'to')

BLOCK_KEYS = {
    "number",
    "hash",
    "parent_hash",
    "nonce",
    "sha3_uncles",
    "logs_bloom",
    "transactions_root",
    "receipts_root",
    "state_root",
    "miner",
    "difficulty",
    "total_difficulty",
    "size",