Skip to main content

Python JAM Codec Library

Project description

Python JAM Codec

Build Status Latest Version Supported Python versions License

Description

Python implementation of the JAM codec as specified in the graypaper.

Installation

pip install jamcodec

Code examples

from jamcodec.types import JamBytes, Bool, String, U32, U8, U16, Struct, Vec, VarInt64, Tuple, Enum

# encode a Vec<u16>
obj = Vec(U16).new()
value = [1, 2]
data = obj.encode(value)

# Define and decode a Struct
scale_obj = Struct(test=U8, test2=Tuple(U8, U8)).new()
value = scale_obj.decode(JamBytes("0x020105"))

# Define and encode an Enum
scale_obj = Enum(
    Bool=Bool(),
    Number=U32,
    Complex=Struct(data=String(), version=VarInt64),
    None_=None
).new()
value = {'Bool': True}

data = scale_obj.encode(value)

Examples of different types

Type Description Example SCALE decoding value SCALE encoded value
bool Boolean values are encoded using the least significant bit of a single byte. True 0x01
u16 Basic integers are encoded using a fixed-width little-endian (LE) format. 42 0x2a00
VarInt64 xxxxxxx 0 0x00
1 0x04
42 0xa8
69 0x1501
100000000000000 0x0b00407a10f35a
Vec A collection of same-typed values is encoded, prefixed with a compact encoding of the number of items, followed by each item's encoding concatenated in turn. [4, 8, 15, 16, 23, 42] 0x18040008000f00100017002a00
BitVec A sequence of bools, represented in a more space efficient bit format 0b00000010_01111101 0x287d02
str,Bytes, String Strings are Vectors of bytes (Vec<u8>) containing a valid UTF8 sequence. "Test" 0x1054657374
b"Test" 0x1054657374
[84, 101, 115, 116] 0x1054657374
[u8; 4] Fixed sized array of in this case an u8 b"babe" 0x62616265
"0x62616265" 0x62616265
[98, 97, 98, 101] 0x62616265
Enum Example: enum IntOrBool { Int(u8), Bool(bool),} A fixed number of variants, each mutually exclusive and potentially implying a further value or series of values. Encoded as the first byte identifying the index of the variant that the value is. Any further bytes are used to encode any data that the variant implies. Thus, no more than 256 variants are supported. {'Int': 8} 0x002a
{'Bool': True} 0x0101
Struct Example: struct Motion { pub votes: Vec<AccountId>, pub id: u32 } For structures, the values are named, but that is irrelevant for the encoding (names are ignored - only order matters). All containers store elements consecutively. The order of the elements is not fixed, depends on the container, and cannot be relied on at decoding. This implicitly means that decoding some byte-array into a specified structure that enforces an order and then re-encoding it could result in a different byte array than the original that was decoded. {"votes": ["5GDyPHLVHcQYPTWfygtPYeo gQjyZy7J9fsi4brPhgEFq4pcv"], "id": 4} 0x04b80269ec500e458a630846b99105c397ee57 4125823d6f4388e9c7572e115c0504000000

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jamcodec-0.1.3.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jamcodec-0.1.3-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file jamcodec-0.1.3.tar.gz.

File metadata

  • Download URL: jamcodec-0.1.3.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for jamcodec-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a5b51217959a08a7333ba93b50995b94d8b7a205d2ded55e8f64aa05d7319a6e
MD5 a66e463c78d21deb5c9529a561199fd8
BLAKE2b-256 f13f10834546cd897420aa67ac90132c6cb49f88c6cb3f23b126b7de3bbbec62

See more details on using hashes here.

File details

Details for the file jamcodec-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: jamcodec-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for jamcodec-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2d998a00c7ee65c1d0f3651eb44099ef7bee4adde1d209ecb799aba09cba410d
MD5 78de97e05006ae12d2fe607e23b95e6b
BLAKE2b-256 8e4e58e7b89a053ae1625c1546a3db192ae5f2fca1324e291da1a1baf188c372

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page