Skip to main content

Typed Python SDK for Phantasma RPC, VM scripts, transactions, and Carbon wire formats

Project description

Phantasma Python SDK

Typed Python SDK for Phantasma RPC, classic VM scripts and transactions, Ed25519 keys/signatures, and Carbon wire formats.

The public surface follows the current C#, TypeScript, C++, and Go SDK behavior while using Python naming, dataclasses, exceptions, and type hints.

Requirements

  • Python 3.11 or newer
  • cryptography
  • requests

Development uses uv, ruff, mypy, and pytest.

Install

pip install phantasma-sdk-py

For local development:

uv sync --extra dev
just check

Modules

  • phantasma_py.crypto: addresses, hashes, WIF keys, Ed25519 signatures
  • phantasma_py.vm: classic VM objects, opcodes, and ScriptBuilder
  • phantasma_py.transaction: classic script transaction serialization/signing
  • phantasma_py.rpc: JSON-RPC client and typed response dataclasses
  • phantasma_py.carbon: Carbon primitives, VM schemas, module call args, token builders, and tx messages

RPC

from phantasma_py.rpc import PhantasmaRPC

rpc = PhantasmaRPC.mainnet()
account = rpc.get_account("P...")
balance = account.get_token_balance("SOUL", decimals=8)

print(balance.decimal_amount())

JsonRpcClient validates JSON-RPC response ids, propagates RPC errors as RPCError, and accepts endpoints that echo numeric ids as strings.

Keys And Signatures

from phantasma_py.crypto import PhantasmaKeys

keys = PhantasmaKeys.from_wif("...")
signature = keys.sign(b"message")

assert signature.verify(b"message", [keys.address])

Address parsing rejects malformed Base58/checksum data. Address.from_text("NULL") and Address.null() produce the system null address.

Classic VM Scripts

from phantasma_py.crypto import Address, PhantasmaKeys
from phantasma_py.vm import ScriptBuilder

keys = PhantasmaKeys.from_wif("...")

script = (
    ScriptBuilder.begin()
    .allow_gas(keys.address, Address.null(), gas_price=10_000, gas_limit=210_000)
    .call_contract("stake", "GetStake", keys.address)
    .spend_gas(keys.address)
    .end_script()
)

end_script() raises BuilderError if labels or user input are invalid. end_script_with_error() returns (script, error) for callers that prefer an explicit checked path.

Classic Transactions

from phantasma_py.crypto import PhantasmaKeys
from phantasma_py.transaction import Transaction
from phantasma_py.vm import ScriptBuilder

keys = PhantasmaKeys.from_wif("...")
script = ScriptBuilder.begin().call_interop("Runtime.Time").end_script()

tx = Transaction("mainnet", "main", script, expiration=1_754_000_000)
tx.sign(keys)

raw_hex = tx.to_bytes().hex()

Broadcasting is intentionally separate from signing:

tx_hash = rpc.send_raw_transaction(raw_hex)

Do not run broadcasting examples without explicit credentials, funds, and an endpoint you intend to use.

Carbon

Carbon serialization uses fixed-width little-endian integers, zero-terminated strings, fixed byte types, compact signed Int256 values, and typed transaction payloads. Use serialize() and deserialize() for stable wire round-trips.

from phantasma_py.carbon import (
    Bytes32,
    IntX,
    build_token_info,
    build_token_metadata,
    prepare_standard_token_schemas,
    serialize,
)

owner = Bytes32()
schemas = prepare_standard_token_schemas(shared_metadata=False)
token = build_token_info(
    symbol="ART",
    max_supply=IntX(0),
    is_nft=True,
    decimals=0,
    owner=owner,
    metadata=build_token_metadata(
        {
            "name": "Art Token",
            "icon": "data:image/png;base64,AA==",
            "url": "https://example.invalid/art",
            "description": "Example token metadata",
        }
    ),
    token_schemas=serialize(schemas),
)

payload = serialize(token)

Carbon token and NFT helpers validate required metadata, token symbol casing, standard schema fields, Carbon NFT address packing, and result parsing. Token symbols follow the Carbon token-module rule of uppercase ASCII letters A-Z.

Carbon transaction signing is available without going through RPC:

from phantasma_py.carbon import sign_and_serialize_tx_msg_hex
from phantasma_py.crypto import PhantasmaKeys

keys = PhantasmaKeys.from_wif("...")
raw_hex = sign_and_serialize_tx_msg_hex(tx_msg, keys)

Development

just f          # format and autofix
just f-check    # verify formatting and lint
just typecheck  # strict mypy
just test       # pytest
just build      # package build
just check      # all checks above

The shared Carbon vector fixture in tests/fixtures/carbon_vectors.tsv is copied from the Go SDK and should stay byte-for-byte compatible across SDKs.

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

phantasma_sdk_py-2.0.0.tar.gz (103.2 kB view details)

Uploaded Source

Built Distribution

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

phantasma_sdk_py-2.0.0-py3-none-any.whl (41.1 kB view details)

Uploaded Python 3

File details

Details for the file phantasma_sdk_py-2.0.0.tar.gz.

File metadata

  • Download URL: phantasma_sdk_py-2.0.0.tar.gz
  • Upload date:
  • Size: 103.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for phantasma_sdk_py-2.0.0.tar.gz
Algorithm Hash digest
SHA256 c51623500bb98b585a549c0ef08f1bf9e40a96213cb8cfbde6a1895d02b3b225
MD5 20b221a57588a9fc5f90c8ef242e3e22
BLAKE2b-256 91cca681bf7cd0221752693456b955b3a660218fea37a8f6b7ec28542060e0d3

See more details on using hashes here.

File details

Details for the file phantasma_sdk_py-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for phantasma_sdk_py-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cce1d67fa1d0f78f630469b0d6f84d917bd4b9af8bd9f5f689a7285b7748b9d5
MD5 8fdf424a831d44ed66ad7db1f470afa8
BLAKE2b-256 f863c9a8dcf7a0aea75c4c9374d6d6db6efa1198e9c7a20ea88a502b14ce7e91

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