base.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from abc import (
  2. ABC,
  3. abstractmethod,
  4. )
  5. from typing import (
  6. Any,
  7. )
  8. from eth_typing import (
  9. ChecksumAddress,
  10. Hash32,
  11. )
  12. from eth_account.datastructures import (
  13. SignedMessage,
  14. SignedTransaction,
  15. )
  16. from eth_account.messages import (
  17. SignableMessage,
  18. )
  19. from eth_account.types import (
  20. TransactionDictType,
  21. )
  22. class BaseAccount(ABC):
  23. """
  24. Specify convenience methods to sign transactions and message hashes.
  25. """
  26. @property
  27. @abstractmethod
  28. def address(self) -> ChecksumAddress:
  29. """
  30. The checksummed public address for this account.
  31. .. code-block:: python
  32. >>> my_account.address # doctest: +SKIP
  33. "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55"
  34. """
  35. @abstractmethod
  36. def sign_message(self, signable_message: SignableMessage) -> SignedMessage:
  37. """
  38. Sign the EIP-191_ message.
  39. This uses the same structure
  40. as in :meth:`~eth_account.account.Account.sign_message`
  41. but without specifying the private key.
  42. :param signable_message: The encoded message, ready for signing
  43. .. _EIP-191: https://eips.ethereum.org/EIPS/eip-191
  44. """
  45. @abstractmethod
  46. def unsafe_sign_hash(self, message_hash: Hash32) -> SignedMessage:
  47. """
  48. Sign the hash of a message.
  49. .. WARNING:: *Never* sign a hash that you didn't generate,
  50. it can be an arbitrary transaction. For example, it might
  51. send all of your account's ether to an attacker.
  52. Instead, prefer :meth:`~eth_account.account.Account.sign_message`,
  53. which cannot accidentally sign a transaction.
  54. This uses the same structure
  55. as in :meth:`~eth_account.account.Account.unsafe_sign_hash`
  56. but without specifying the private key.
  57. :param bytes message_hash: 32 byte hash of the message to sign
  58. """
  59. @abstractmethod
  60. def sign_transaction(
  61. self, transaction_dict: TransactionDictType
  62. ) -> SignedTransaction:
  63. """
  64. Sign a transaction dict.
  65. This uses the same structure as in
  66. :meth:`~eth_account.account.Account.sign_transaction`
  67. but without specifying the private key.
  68. :param dict transaction_dict: transaction with all fields specified
  69. """
  70. def __eq__(self, other: Any) -> bool:
  71. """
  72. Equality test between two accounts.
  73. Two accounts are considered the same if they are exactly the same type,
  74. and can sign for the same address.
  75. """
  76. return type(self) is type(other) and self.address == other.address
  77. def __hash__(self) -> int:
  78. return hash((type(self), self.address))