eth-pydantic-types


Nameeth-pydantic-types JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/ApeWorX/eth-pydantic-types
Summaryeth-pydantic-types: Pydantic Types for Ethereum
upload_time2024-04-09 22:28:33
maintainerNone
docs_urlNone
authorApeWorX Ltd.
requires_python<4,>=3.8
licenseApache-2.0
keywords ethereum
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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.8",
    "maintainer_email": null,
    "keywords": "ethereum",
    "author": "ApeWorX Ltd.",
    "author_email": "admin@apeworx.io",
    "download_url": "https://files.pythonhosted.org/packages/73/bf/2eb3bbfb66769677b1b4922aa175776b84008cb3a03a35257b5cf5425260/eth-pydantic-types-0.1.0.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.0",
    "project_urls": {
        "Homepage": "https://github.com/ApeWorX/eth-pydantic-types"
    },
    "split_keywords": [
        "ethereum"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f7e24648a7a5c03e8e3ff722e51ad2de78a7e0fc1d05f33c61e60840d0f8e9db",
                "md5": "8b064609f70e4d1adb6d777bd0aadf22",
                "sha256": "0ddcdb4fb2c51d54e7adc29e6826e7c7229d99f680906986f70beaf9b4cd912d"
            },
            "downloads": -1,
            "filename": "eth_pydantic_types-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8b064609f70e4d1adb6d777bd0aadf22",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.8",
            "size": 14547,
            "upload_time": "2024-04-09T22:28:31",
            "upload_time_iso_8601": "2024-04-09T22:28:31.428280Z",
            "url": "https://files.pythonhosted.org/packages/f7/e2/4648a7a5c03e8e3ff722e51ad2de78a7e0fc1d05f33c61e60840d0f8e9db/eth_pydantic_types-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "73bf2eb3bbfb66769677b1b4922aa175776b84008cb3a03a35257b5cf5425260",
                "md5": "28d14266d46f576ca43559c98f43a20b",
                "sha256": "d95951791931c94fbd869b7fdf50d45cd228520510dba83d6edc04cc04b8dafe"
            },
            "downloads": -1,
            "filename": "eth-pydantic-types-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "28d14266d46f576ca43559c98f43a20b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.8",
            "size": 65640,
            "upload_time": "2024-04-09T22:28:33",
            "upload_time_iso_8601": "2024-04-09T22:28:33.251219Z",
            "url": "https://files.pythonhosted.org/packages/73/bf/2eb3bbfb66769677b1b4922aa175776b84008cb3a03a35257b5cf5425260/eth-pydantic-types-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-09 22:28:33",
    "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"
}
        
Elapsed time: 0.22563s