Esempio n. 1
0
 def test_legacy_transaction_eip_2718_from_bytes(self):
     transaction = Transaction.deserialize(
         eth_fixtures.LEGACY_TRANSACTION_EIP_2718)
     re_encoded = rlp.encode(transaction)
     self.assertEqual(
         Transaction.deserialize(eth_fixtures.LEGACY_TRANSACTION),
         transaction)
     self.assertEqual(transaction, rlp.decode(re_encoded, Transaction))
Esempio n. 2
0
def verify_eth_transaction_signature(transaction: Transaction) -> bool:
    """
    checks eth transaction signature
    :param transaction:
    :return: if signature matches public key
    """
    try:
        signature = transaction.signature()
        unsigned_msg = transaction.get_unsigned()
        public_key = crypto_utils.recover_public_key(unsigned_msg, signature, keccak_hash)
        return crypto_utils.verify_signature(public_key, signature, keccak_hash(memoryview(unsigned_msg)))
    # pylint: disable=broad-except
    except Exception:
        return False
Esempio n. 3
0
def tx_to_eth_rpc_json(transaction: Transaction) -> Dict[str, Any]:
    payload = transaction.to_json()
    for field in {"nonce", "gas", "gas_price", "value"}:
        payload[field] = hex(payload[field])

    payload["gasPrice"] = payload["gas_price"]
    del payload["gas_price"]
    return payload
def parse_transaction(tx_bytes: memoryview) -> Optional[Transaction]:
    """
    :param tx_bytes: transaction bytes
    :return: if transaction successfully parsed returns None else transaction
    """

    try:
        payload = rlp.decode(bytearray(tx_bytes), strict=False)
        return Transaction.deserialize(payload)

    # pylint: disable=broad-except
    except Exception:
        return None
Esempio n. 5
0
def get_dummy_transaction(nonce: int,
                          gas_price: Optional[int] = None,
                          v: int = 27,
                          to_address_str: Optional[str] = None) -> Transaction:
    if gas_price is None:
        gas_price = 2 * nonce
    if to_address_str is None:
        to_address = helpers.generate_bytes(eth_common_constants.ADDRESS_LEN)
    else:
        to_address = convert.hex_to_bytes(to_address_str)
    # create transaction object with dummy values multiplied by nonce to be able generate txs with different values
    return Transaction(nonce, gas_price, 3 * nonce, to_address, 4 * nonce,
                       helpers.generate_bytes(15 * nonce), v, 6 * nonce,
                       7 * nonce)
Esempio n. 6
0
    def test_access_list_transaction_from_json(self):
        result = Transaction.from_json(eth_fixtures.ACL_TRANSACTION_JSON)
        self.assertEqual(EthTransactionType.ACCESS_LIST,
                         result.transaction_type)

        result_json = result.to_json()
        for key, val in eth_fixtures.ACL_TRANSACTION_JSON.items():
            # camelcase problems
            if key == "gasPrice":
                key = "gas_price"
            elif key == "chainId":
                key = "chain_id"
            elif key == "accessList":
                continue
            self.assertEqual(val, result_json[key], f"failed on key: {key}")

        for i, access in enumerate(
                eth_fixtures.ACL_TRANSACTION_JSON["accessList"]):
            self.assertEqual(access["address"],
                             result_json["access_list"][i]["address"])
            self.assertEqual(access["storageKeys"],
                             result_json["access_list"][i]["storage_keys"])
    def __init__(self, tx_hash: Sha256Hash, tx_contents: Union[memoryview, Dict[str, Any]]) -> None:
        self.tx_hash = f"0x{str(tx_hash)}"

        try:
            if isinstance(tx_contents, memoryview):
                # parse transaction from memoryview
                transaction = rlp.decode(tx_contents.tobytes(), Transaction)
                self.tx_contents = transaction.to_json()
            else:
                # normalize json from source
                self.tx_contents = Transaction.from_json(tx_contents).to_json()
        except Exception as e:
            tx_contents_str = tx_contents
            if isinstance(tx_contents, memoryview):
                tx_contents_str = tx_contents.tobytes()
                
            logger.error(
                log_messages.COULD_NOT_DESERIALIZE_TRANSACTION,
                tx_hash,
                tx_contents_str,
                e
            )
            raise e
Esempio n. 8
0
    def test_empty_to_serializes(self):
        sample_transactions = dict(
            eth_fixtures.LEGACY_TRANSACTION_JSON_FROM_WS)
        sample_transactions["to"] = None

        Transaction.from_json(sample_transactions).to_json()
Esempio n. 9
0
def get_dummy_transaction(nonce: int, v: int = 27) -> Transaction:
    # create transaction object with dummy values multiplied by nonce to be able generate txs with different values
    return Transaction(
        nonce, 2 * nonce, 3 * nonce,
        helpers.generate_bytes(eth_common_constants.ADDRESS_LEN), 4 * nonce,
        helpers.generate_bytes(15 * nonce), v, 6 * nonce, 7 * nonce)