# eth-pydantic-types
The types in this package are pydantic types for Ethereum inspired from [eth-typing](https://github.com/ethereum/eth-typing/blob/master/eth_typing/evm.py).
## Hash
`HashBytes{n}` and `HashStr{n}` are good types to use when your hex values are sized.
Both types serialize to `string` in the JSON schema.
Use `HashBytes` types when you want types to serialize to bytes in the Pydantic core schema and `HashStr` types when you want to serialize to `str` in the core Pydantic schema.
```python
from pydantic import BaseModel
from eth_pydantic_types import HashBytes32, HashStr20
# When serializing to JSON, both types are hex strings.
class Transaction(BaseModel):
tx_hash: HashBytes32 # Will be bytes
address: HashStr20 # Will be str
# NOTE: I am able to pass an int-hash as the value and it will
# get validated and type-coerced.
tx = Transaction(
tx_hash=0x1031f0c9ac54dcb64b4f121a27957c14263c5cb49ed316d568e41e19c34d7b28,
address=0x1031f0c9ac54dcb64b4f121a27957c14263c5cb4,
)
```
## HexBytes
A thin-wrapper around an already thin-wrapper `hexbytes.HexBytes`.
The difference here is that this HexBytes properly serializes.
Use HexBytes any place where you would actually use `hexbytes.HexBytes`.
`HexBytes` serializes to bytes in the Pydantic core schema and `string` in the JSON schema with a binary format.
```python
from pydantic import BaseModel
from eth_pydantic_types import HexBytes
class MyStorage(BaseModel):
cid: HexBytes
# NOTE: We are able to pass a hex-str for a HexBytes value.
storage = MyStorage(cid="0x123")
```
## Address
Use the Address class for working with checksummed-addresses.
Addresses get validated and checksummed in model construction.
Addresses serialize to `str` in the Pydantic core schema and `string` in the JSON schema with a binary format.
```python
from pydantic import BaseModel
from eth_pydantic_types import Address
class Account(BaseModel):
address: Address
# NOTE: The address ends up checksummed
# ("0x0837207e343277CBd6c114a45EC0e9Ec56a1AD84")
account = Account(address="0x837207e343277cbd6c114a45ec0e9ec56a1ad84")
```
## HexStr
Use hex str when you only care about un-sized hex strings.
The `HexStr` type serializes to `str` in the Pydantic core schema and a `string` in the JSON schema with a binary format.
```python
from eth_pydantic_types import HexStr
from pydantic import BaseModel
class Tx(BaseModel):
data: HexStr
tx = Tx(data="0x0123")
```
## Bip122Uri
Use BIP-122 URIs in your models by annotating with the `Bip122Uri` type.
This type serializes to a `str` in the Pydantic core schema as well as a `string` in the JSON schema, however the individual hashes are validated.
```python
from eth_pydantic_types import Bip122Uri
from pydantic import BaseModel
class Message(BaseModel):
path: Bip122Uri
message = Message(
path=(
"blockchain://d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
"/block/752820c0ad7abc1200f9ad42c4adc6fbb4bd44b5bed4667990e64565102c1ba6"
)
)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/ApeWorX/eth-pydantic-types",
"name": "eth-pydantic-types",
"maintainer": null,
"docs_url": null,
"requires_python": "<4,>=3.9",
"maintainer_email": null,
"keywords": "ethereum",
"author": "ApeWorX Ltd.",
"author_email": "admin@apeworx.io",
"download_url": "https://files.pythonhosted.org/packages/b1/19/3209c4c20832a95c57611f4fb5be51db18836929f2e6c80affc07b533616/eth-pydantic-types-0.1.3.tar.gz",
"platform": null,
"description": "# eth-pydantic-types\n\nThe types in this package are pydantic types for Ethereum inspired from [eth-typing](https://github.com/ethereum/eth-typing/blob/master/eth_typing/evm.py).\n\n## Hash\n\n`HashBytes{n}` and `HashStr{n}` are good types to use when your hex values are sized.\nBoth types serialize to `string` in the JSON schema.\nUse `HashBytes` types when you want types to serialize to bytes in the Pydantic core schema and `HashStr` types when you want to serialize to `str` in the core Pydantic schema.\n\n```python\nfrom pydantic import BaseModel\n\nfrom eth_pydantic_types import HashBytes32, HashStr20\n\n# When serializing to JSON, both types are hex strings.\nclass Transaction(BaseModel):\n tx_hash: HashBytes32 # Will be bytes\n address: HashStr20 # Will be str\n\n\n# NOTE: I am able to pass an int-hash as the value and it will\n# get validated and type-coerced.\ntx = Transaction(\n tx_hash=0x1031f0c9ac54dcb64b4f121a27957c14263c5cb49ed316d568e41e19c34d7b28,\n address=0x1031f0c9ac54dcb64b4f121a27957c14263c5cb4,\n)\n```\n\n## HexBytes\n\nA thin-wrapper around an already thin-wrapper `hexbytes.HexBytes`.\nThe difference here is that this HexBytes properly serializes.\nUse HexBytes any place where you would actually use `hexbytes.HexBytes`.\n`HexBytes` serializes to bytes in the Pydantic core schema and `string` in the JSON schema with a binary format.\n\n```python\nfrom pydantic import BaseModel\nfrom eth_pydantic_types import HexBytes\n\nclass MyStorage(BaseModel):\n cid: HexBytes\n\n# NOTE: We are able to pass a hex-str for a HexBytes value.\nstorage = MyStorage(cid=\"0x123\")\n```\n\n## Address\n\nUse the Address class for working with checksummed-addresses.\nAddresses get validated and checksummed in model construction.\nAddresses serialize to `str` in the Pydantic core schema and `string` in the JSON schema with a binary format.\n\n```python\nfrom pydantic import BaseModel\nfrom eth_pydantic_types import Address\n\nclass Account(BaseModel):\n address: Address\n\n# NOTE: The address ends up checksummed\n# (\"0x0837207e343277CBd6c114a45EC0e9Ec56a1AD84\")\naccount = Account(address=\"0x837207e343277cbd6c114a45ec0e9ec56a1ad84\")\n```\n\n## HexStr\n\nUse hex str when you only care about un-sized hex strings.\nThe `HexStr` type serializes to `str` in the Pydantic core schema and a `string` in the JSON schema with a binary format.\n\n```python\nfrom eth_pydantic_types import HexStr\nfrom pydantic import BaseModel\n\nclass Tx(BaseModel):\n data: HexStr\n\ntx = Tx(data=\"0x0123\")\n```\n\n## Bip122Uri\n\nUse BIP-122 URIs in your models by annotating with the `Bip122Uri` type.\nThis type serializes to a `str` in the Pydantic core schema as well as a `string` in the JSON schema, however the individual hashes are validated.\n\n```python\nfrom eth_pydantic_types import Bip122Uri\nfrom pydantic import BaseModel\n\nclass Message(BaseModel):\n path: Bip122Uri\n\nmessage = Message(\n path=(\n \"blockchain://d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3\"\n \"/block/752820c0ad7abc1200f9ad42c4adc6fbb4bd44b5bed4667990e64565102c1ba6\"\n )\n)\n```\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "eth-pydantic-types: Pydantic Types for Ethereum",
"version": "0.1.3",
"project_urls": {
"Homepage": "https://github.com/ApeWorX/eth-pydantic-types"
},
"split_keywords": [
"ethereum"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3fb2e44f9e3be45768c33f83080d7c2bfcdf698fdeaec810c98866724eb44846",
"md5": "6a7ef8427fe6fba1bbd2bf7a10400a14",
"sha256": "7a54f253f32bb3c01b21dbbfc7022d476b53ad5b7da96dcc4dae24997ec69469"
},
"downloads": -1,
"filename": "eth_pydantic_types-0.1.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6a7ef8427fe6fba1bbd2bf7a10400a14",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.9",
"size": 14624,
"upload_time": "2024-08-26T16:17:09",
"upload_time_iso_8601": "2024-08-26T16:17:09.728536Z",
"url": "https://files.pythonhosted.org/packages/3f/b2/e44f9e3be45768c33f83080d7c2bfcdf698fdeaec810c98866724eb44846/eth_pydantic_types-0.1.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b1193209c4c20832a95c57611f4fb5be51db18836929f2e6c80affc07b533616",
"md5": "56ebda1a896ac3eaf1a2fe4b92b3e259",
"sha256": "4764ba82434ab6d0d98d3b7aee4ace0cc7b12cf47c7e53424525c33fb9b872af"
},
"downloads": -1,
"filename": "eth-pydantic-types-0.1.3.tar.gz",
"has_sig": false,
"md5_digest": "56ebda1a896ac3eaf1a2fe4b92b3e259",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.9",
"size": 65715,
"upload_time": "2024-08-26T16:17:11",
"upload_time_iso_8601": "2024-08-26T16:17:11.091151Z",
"url": "https://files.pythonhosted.org/packages/b1/19/3209c4c20832a95c57611f4fb5be51db18836929f2e6c80affc07b533616/eth-pydantic-types-0.1.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-26 16:17:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ApeWorX",
"github_project": "eth-pydantic-types",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "eth-pydantic-types"
}