METADATA 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. Metadata-Version: 2.4
  2. Name: eth-keys
  3. Version: 0.7.0
  4. Summary: eth-keys: Common API for Ethereum key operations
  5. Home-page: https://github.com/ethereum/eth-keys
  6. Author: The Ethereum Foundation
  7. Author-email: snakecharmers@ethereum.org
  8. License: MIT
  9. Keywords: ethereum
  10. Classifier: Development Status :: 4 - Beta
  11. Classifier: Intended Audience :: Developers
  12. Classifier: License :: OSI Approved :: MIT License
  13. Classifier: Natural Language :: English
  14. Classifier: Programming Language :: Python :: 3
  15. Classifier: Programming Language :: Python :: 3.8
  16. Classifier: Programming Language :: Python :: 3.9
  17. Classifier: Programming Language :: Python :: 3.10
  18. Classifier: Programming Language :: Python :: 3.11
  19. Classifier: Programming Language :: Python :: 3.12
  20. Classifier: Programming Language :: Python :: 3.13
  21. Requires-Python: >=3.8, <4
  22. Description-Content-Type: text/markdown
  23. License-File: LICENSE
  24. Requires-Dist: eth-utils>=2
  25. Requires-Dist: eth-typing>=3
  26. Provides-Extra: coincurve
  27. Requires-Dist: coincurve>=17.0.0; extra == "coincurve"
  28. Provides-Extra: dev
  29. Requires-Dist: coincurve>=17.0.0; extra == "dev"
  30. Requires-Dist: build>=0.9.0; extra == "dev"
  31. Requires-Dist: bump_my_version>=0.19.0; extra == "dev"
  32. Requires-Dist: ipython; extra == "dev"
  33. Requires-Dist: mypy==1.10.0; extra == "dev"
  34. Requires-Dist: pre-commit>=3.4.0; extra == "dev"
  35. Requires-Dist: tox>=4.0.0; extra == "dev"
  36. Requires-Dist: twine; extra == "dev"
  37. Requires-Dist: wheel; extra == "dev"
  38. Requires-Dist: towncrier<25,>=24; extra == "dev"
  39. Requires-Dist: pytest>=7.0.0; extra == "dev"
  40. Requires-Dist: asn1tools>=0.146.2; extra == "dev"
  41. Requires-Dist: factory-boy>=3.0.1; extra == "dev"
  42. Requires-Dist: pyasn1>=0.4.5; extra == "dev"
  43. Requires-Dist: hypothesis>=5.10.3; extra == "dev"
  44. Requires-Dist: eth-hash[pysha3]; extra == "dev"
  45. Provides-Extra: docs
  46. Requires-Dist: towncrier<25,>=24; extra == "docs"
  47. Provides-Extra: test
  48. Requires-Dist: pytest>=7.0.0; extra == "test"
  49. Requires-Dist: asn1tools>=0.146.2; extra == "test"
  50. Requires-Dist: factory-boy>=3.0.1; extra == "test"
  51. Requires-Dist: pyasn1>=0.4.5; extra == "test"
  52. Requires-Dist: hypothesis>=5.10.3; extra == "test"
  53. Requires-Dist: eth-hash[pysha3]; extra == "test"
  54. Dynamic: author
  55. Dynamic: author-email
  56. Dynamic: classifier
  57. Dynamic: description
  58. Dynamic: description-content-type
  59. Dynamic: home-page
  60. Dynamic: keywords
  61. Dynamic: license
  62. Dynamic: license-file
  63. Dynamic: provides-extra
  64. Dynamic: requires-dist
  65. Dynamic: requires-python
  66. Dynamic: summary
  67. # eth-keys
  68. [![Join the conversation on Discord](https://img.shields.io/discord/809793915578089484?color=blue&label=chat&logo=discord&logoColor=white)](https://discord.gg/GHryRvPB84)
  69. [![Build Status](https://circleci.com/gh/ethereum/eth-keys.svg?style=shield)](https://circleci.com/gh/ethereum/eth-keys)
  70. [![PyPI version](https://badge.fury.io/py/eth-keys.svg)](https://badge.fury.io/py/eth-keys)
  71. [![Python versions](https://img.shields.io/pypi/pyversions/eth-keys.svg)](https://pypi.python.org/pypi/eth-keys)
  72. Common API for Ethereum key operations
  73. > This library and repository was previously located at https://github.com/pipermerriam/ethereum-keys. It was transferred to the Ethereum foundation github in November 2017 and renamed to `eth-keys`. The PyPi package was also renamed from `ethereum-keys` to `eth-keys`.
  74. Read more in the documentation below. [View the change log](https://github.com/ethereum/eth-keys/blob/main/CHANGELOG.rst).
  75. ## Installation
  76. ```sh
  77. python -m pip install eth-keys
  78. ```
  79. ```python
  80. >>> from eth_keys import keys
  81. >>> pk = keys.PrivateKey(b'\x01' * 32)
  82. >>> signature = pk.sign_msg(b'a message')
  83. >>> pk
  84. '0x0101010101010101010101010101010101010101010101010101010101010101'
  85. >>> pk.public_key
  86. '0x1b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f70beaf8f588b541507fed6a642c5ab42dfdf8120a7f639de5122d47a69a8e8d1'
  87. >>> signature
  88. '0xccda990dba7864b79dc49158fea269338a1cf5747bc4c4bf1b96823e31a0997e7d1e65c06c5bf128b7109e1b4b9ba8d1305dc33f32f624695b2fa8e02c12c1e000'
  89. >>> pk.public_key.to_checksum_address()
  90. '0x1a642f0E3c3aF545E7AcBD38b07251B3990914F1'
  91. >>> signature.verify_msg(b'a message', pk.public_key)
  92. True
  93. >>> signature.recover_public_key_from_msg(b'a message') == pk.public_key
  94. True
  95. ```
  96. ## Documentation
  97. ### `KeyAPI(backend=None)`
  98. The `KeyAPI` object is the primary API for interacting with the `eth-keys`
  99. libary. The object takes a single optional argument in its constructor which
  100. designates what backend will be used for eliptical curve cryptography
  101. operations. The built-in backends are:
  102. - `eth_keys.backends.NativeECCBackend`: A pure python implementation of the ECC operations.
  103. - `eth_keys.backends.CoinCurveECCBackend`: Uses the [`coincurve`](https://github.com/ofek/coincurve) library for ECC operations.
  104. By default, `eth-keys` will *try* to use the `CoinCurveECCBackend`,
  105. falling back to the `NativeECCBackend` if the `coincurve` library is not
  106. available.
  107. > Note: The `coincurve` library is not automatically installed with `eth-keys` and must be installed separately.
  108. The `backend` argument can be given in any of the following forms.
  109. - Instance of the backend class
  110. - The backend class
  111. - String with the dot-separated import path for the backend class.
  112. ```python
  113. >>> from eth_keys import KeyAPI
  114. >>> from eth_keys.backends import NativeECCBackend
  115. # These are all the same
  116. >>> keys = KeyAPI(NativeECCBackend)
  117. >>> keys = KeyAPI(NativeECCBackend())
  118. >>> keys = KeyAPI('eth_keys.backends.NativeECCBackend')
  119. # Or for the coincurve base backend
  120. >>> keys = KeyAPI('eth_keys.backends.CoinCurveECCBackend')
  121. ```
  122. The backend can also be configured using the environment variable
  123. `ECC_BACKEND_CLASS` which should be set to the dot-separated python import path
  124. to the desired backend.
  125. ```python
  126. >>> import os
  127. >>> os.environ['ECC_BACKEND_CLASS'] = 'eth_keys.backends.CoinCurveECCBackend'
  128. ```
  129. ### `KeyAPI.ecdsa_sign(message_hash, private_key) -> Signature`
  130. This method returns a signature for the given `message_hash`, signed by the
  131. provided `private_key`.
  132. - `message_hash`: **must** be a byte string of length 32
  133. - `private_key`: **must** be an instance of `PrivateKey`
  134. ### `KeyAPI.ecdsa_verify(message_hash, signature, public_key) -> bool`
  135. Returns `True` or `False` based on whether the provided `signature` is a valid
  136. signature for the provided `message_hash` and `public_key`.
  137. - `message_hash`: **must** be a byte string of length 32
  138. - `signature`: **must** be an instance of `Signature`
  139. - `public_key`: **must** be an instance of `PublicKey`
  140. ### `KeyAPI.ecdsa_recover(message_hash, signature) -> PublicKey`
  141. Returns the `PublicKey` instances recovered from the given `signature` and
  142. `message_hash`.
  143. - `message_hash`: **must** be a byte string of length 32
  144. - `signature`: **must** be an instance of `Signature`
  145. ### `KeyAPI.private_key_to_public_key(private_key) -> PublicKey`
  146. Returns the `PublicKey` instances computed from the given `private_key`
  147. instance.
  148. - `private_key`: **must** be an instance of `PublicKey`
  149. ### Common APIs for `PublicKey`, `PrivateKey` and `Signature`
  150. There is a common API for the following objects.
  151. - `PublicKey`
  152. - `PrivateKey`
  153. - `Signature`
  154. Each of these objects has all of the following APIs.
  155. - `obj.to_bytes()`: Returns the object in it's canonical `bytes` serialization.
  156. - `obj.to_hex()`: Returns a text string of the hex encoded canonical representation.
  157. ### `KeyAPI.PublicKey(public_key_bytes)`
  158. The `PublicKey` class takes a single argument which must be a bytes string with length 64.
  159. > Note that there are two other common formats for public keys: 65 bytes with a leading `\x04` byte
  160. > and 33 bytes starting with either `\x02` or `\x03`. To use the former with the `PublicKey` object,
  161. > remove the first byte. For the latter, refer to `PublicKey.from_compressed_bytes`.
  162. The following methods are available:
  163. #### `PublicKey.from_compressed_bytes(compressed_bytes) -> PublicKey`
  164. This `classmethod` returns a new `PublicKey` instance computed from its compressed representation.
  165. - `compressed_bytes` **must** be a byte string of length 33 starting with `\x02` or `\x03`.
  166. #### `PublicKey.from_private(private_key) -> PublicKey`
  167. This `classmethod` returns a new `PublicKey` instance computed from the
  168. given `private_key`.
  169. - `private_key` may either be a byte string of length 32 or an instance of the `KeyAPI.PrivateKey` class.
  170. #### `PublicKey.recover_from_msg(message, signature) -> PublicKey`
  171. This `classmethod` returns a new `PublicKey` instance computed from the
  172. provided `message` and `signature`.
  173. - `message` **must** be a byte string
  174. - `signature` **must** be an instance of `KeyAPI.Signature`
  175. #### `PublicKey.recover_from_msg_hash(message_hash, signature) -> PublicKey`
  176. Same as `PublicKey.recover_from_msg` except that `message_hash` should be the Keccak
  177. hash of the `message`.
  178. #### `PublicKey.verify_msg(message, signature) -> bool`
  179. This method returns `True` or `False` based on whether the signature is a valid
  180. for the given message.
  181. #### `PublicKey.verify_msg_hash(message_hash, signature) -> bool`
  182. Same as `PublicKey.verify_msg` except that `message_hash` should be the Keccak
  183. hash of the `message`.
  184. #### `PublicKey.to_compressed_bytes() -> bytes`
  185. Returns the compressed representation of this public key.
  186. #### `PublicKey.to_address() -> text`
  187. Returns the hex encoded ethereum address for this public key.
  188. #### `PublicKey.to_checksum_address() -> text`
  189. Returns the ERC55 checksum formatted ethereum address for this public key.
  190. #### `PublicKey.to_canonical_address() -> bytes`
  191. Returns the 20-byte representation of the ethereum address for this public key.
  192. ### `KeyAPI.PrivateKey(private_key_bytes)`
  193. The `PrivateKey` class takes a single argument which must be a bytes string with length 32.
  194. The following methods and properties are available
  195. #### `PrivateKey.public_key`
  196. This *property* holds the `PublicKey` instance coresponding to this private key.
  197. #### `PrivateKey.sign_msg(message) -> Signature`
  198. This method returns a signature for the given `message` in the form of a
  199. `Signature` instance
  200. - `message` **must** be a byte string.
  201. #### `PrivateKey.sign_msg_hash(message_hash) -> Signature`
  202. Same as `PrivateKey.sign` except that `message_hash` should be the Keccak
  203. hash of the `message`.
  204. ### `KeyAPI.Signature(signature_bytes=None, vrs=None)`
  205. The `Signature` class can be instantiated in one of two ways.
  206. - `signature_bytes`: a bytes string with length 65.
  207. - `vrs`: a 3-tuple composed of the integers `v`, `r`, and `s`.
  208. > Note: If using the `signature_bytes` to instantiate, the byte string should be encoded as `r_bytes | s_bytes | v_bytes` where `|` represents concatenation. `r_bytes` and `s_bytes` should be 32 bytes in length. `v_bytes` should be a single byte `\x00` or `\x01`.
  209. Signatures are expected to use `1` or `0` for their `v` value.
  210. The following methods and properties are available
  211. #### `Signature.v`
  212. This property returns the `v` value from the signature as an integer.
  213. #### `Signature.r`
  214. This property returns the `r` value from the signature as an integer.
  215. #### `Signature.s`
  216. This property returns the `s` value from the signature as an integer.
  217. #### `Signature.vrs`
  218. This property returns a 3-tuple of `(v, r, s)`.
  219. #### `Signature.verify_msg(message, public_key) -> bool`
  220. This method returns `True` or `False` based on whether the signature is a valid
  221. for the given public key.
  222. - `message`: **must** be a byte string.
  223. - `public_key`: **must** be an instance of `PublicKey`
  224. #### `Signature.verify_msg_hash(message_hash, public_key) -> bool`
  225. Same as `Signature.verify_msg` except that `message_hash` should be the Keccak
  226. hash of the `message`.
  227. #### `Signature.recover_public_key_from_msg(message) -> PublicKey`
  228. This method returns a `PublicKey` instance recovered from the signature.
  229. - `message`: **must** be a byte string.
  230. #### `Signature.recover_public_key_from_msg_hash(message_hash) -> PublicKey`
  231. Same as `Signature.recover_public_key_from_msg` except that `message_hash`
  232. should be the Keccak hash of the `message`.
  233. ### Exceptions
  234. #### `eth_api.exceptions.ValidationError`
  235. This error is raised during instantaition of any of the `PublicKey`,
  236. `PrivateKey` or `Signature` classes if their constructor parameters are
  237. invalid.
  238. #### `eth_api.exceptions.BadSignature`
  239. This error is raised from any of the `recover` or `verify` methods involving
  240. signatures if the signature is invalid.
  241. ## Developer Setup
  242. If you would like to hack on eth-keys, please check out the [Snake Charmers
  243. Tactical Manual](https://github.com/ethereum/snake-charmers-tactical-manual)
  244. for information on how we do:
  245. - Testing
  246. - Pull Requests
  247. - Documentation
  248. We use [pre-commit](https://pre-commit.com/) to maintain consistent code style. Once
  249. installed, it will run automatically with every commit. You can also run it manually
  250. with `make lint`. If you need to make a commit that skips the `pre-commit` checks, you
  251. can do so with `git commit --no-verify`.
  252. ### Development Environment Setup
  253. You can set up your dev environment with:
  254. ```sh
  255. git clone git@github.com:ethereum/eth-keys.git
  256. cd eth-keys
  257. virtualenv -p python3 venv
  258. . venv/bin/activate
  259. python -m pip install -e ".[dev]"
  260. pre-commit install
  261. ```
  262. ### Release setup
  263. To release a new version:
  264. ```sh
  265. make release bump=$$VERSION_PART_TO_BUMP$$
  266. ```
  267. #### How to bumpversion
  268. The version format for this repo is `{major}.{minor}.{patch}` for stable, and
  269. `{major}.{minor}.{patch}-{stage}.{devnum}` for unstable (`stage` can be alpha or beta).
  270. To issue the next version in line, specify which part to bump,
  271. like `make release bump=minor` or `make release bump=devnum`. This is typically done from the
  272. main branch, except when releasing a beta (in which case the beta is released from main,
  273. and the previous stable branch is released from said branch).
  274. If you are in a beta version, `make release bump=stage` will switch to a stable.
  275. To issue an unstable version when the current version is stable, specify the
  276. new version explicitly, like `make release bump="--new-version 4.0.0-alpha.1 devnum"`