Skip to main content

Core Python SDK for the Internet Computer: agent, candid, identity, principal, certificate.

Project description

๐Ÿ ICP-PY-CORE

ICP-PY-CORE Logo

PyPI version License: MIT Internet Computer


๐Ÿ“– About This Project

ICP-PY-CORE is a maintained and extended fork of ic-py.
This version introduces a modular architecture, protocol upgrades, and new APIs while preserving compatibility with the IC ecosystem.

Highlights:

  • โœ… Modular structure under src/ (icp_agent, icp_identity, icp_candid, etc.)
  • โœ… Updated boundary node v3 endpoints (/api/v3/canister/.../call)
  • โœ… Optional certificate verification via blst
  • โœ… Type-safe Candid encoding/decoding
  • โœ… Pythonic high-level Agent.update() and Agent.query() methods

๐Ÿ™ Special thanks to the original ic-py author for their foundational work.

๐Ÿค Community & Contribution


๐Ÿ”ง Installation

pip install icp-py-core

The Candid parser uses a Rust extension with pre-built binary wheels for all platforms.
No Rust compiler is required for installation.
For optional certificate verification, see the blst section below.


๐Ÿš€ Key Improvements

โœณ๏ธ Modular Codebase

Each component is isolated for clarity and extensibility:

src/
โ”œโ”€โ”€ icp_agent/         # Agent & HTTP Client
โ”œโ”€โ”€ icp_identity/      # ed25519 / secp256k1 identities
โ”œโ”€โ”€ icp_candid/        # Candid encoder/decoder
โ”œโ”€โ”€ icp_principal/     # Principal utilities
โ”œโ”€โ”€ icp_certificate/   # Certificate validation
โ”œโ”€โ”€ icp_core/          # Unified facade (one-line import)

๐Ÿ”— Unified Facade (icp_core)

Import everything from a single entrypoint:

from icp_core import (
    Agent, Client,
    Identity, DelegateIdentity,
    Principal, Certificate,
    Canister, Ledger, Governance, Management, CyclesWallet,
    encode, decode, Types,
)

โšก Endpoint Upgrade

All update calls now target Boundary Node v3 endpoints:
/api/v3/canister/<canister_id>/call

๐Ÿ”’ Certificate Verification

Certificate verification is enabled by default for security. Verifies responses via BLS12-381 signatures with blst:

With Agent directly:

# Default: verification enabled
agent.update("canister-id", "method_name", [{'type': Types.Nat, 'value': 2}])

# To disable (for compatibility/testing):
agent.update("canister-id", "method_name", [{'type': Types.Nat, 'value': 2}], verify_certificate=False)

With Canister wrapper:

# Default: verification enabled (matches Agent.update() behavior)
canister.set_value(42)

# Explicitly enable (same as default)
canister.set_value(42, verify_certificate=True)

# Disable verification (when blst is not installed)
canister.set_value(42, verify_certificate=False)

Note: Both Agent.update() and Canister methods default to verify_certificate=True for security. If blst is not installed, you must explicitly pass verify_certificate=False to avoid errors.


๐Ÿงฉ Example Usage

Identity

from icp_core import Identity
iden = Identity(privkey="833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42")
print(iden.sender().to_str())

Client & Agent

from icp_core import Agent, Client, Identity

iden = Identity()
client = Client("https://ic0.app")
agent = Agent(iden, client)

Update (auto-encode)

from icp_core import Types
result = agent.update(
    "wcrzb-2qaaa-aaaap-qhpgq-cai",
    "set",
    [{'type': Types.Nat, 'value': 2}],
    return_type=[Types.Nat],
)

Query (auto-encode empty args)

reply = agent.query("wcrzb-2qaaa-aaaap-qhpgq-cai", "get", [])
print(reply)

Canister Wrapper (Type-Safe Method Calls)

The Canister class provides a high-level, type-safe interface for interacting with canisters. It automatically parses Candid DID files and creates Python methods that match your canister's interface.

Creating a Canister instance:

from icp_core import Agent, Client, Identity, Canister

# Setup agent
client = Client("https://ic0.app")
identity = Identity()
agent = Agent(identity, client)

# Define Candid interface
COUNTER_DID = """
service : {
  get : () -> (nat) query;
  set : (nat) -> (nat)
}
"""

# Create Canister wrapper
counter = Canister(agent, "wcrzb-2qaaa-aaaap-qhpgq-cai", COUNTER_DID)

Calling canister methods:

# Query call (no arguments)
value = counter.get()
print(f"Current value: {value[0]['value']}")

# Update call (with positional argument)
result = counter.set(42)
print(f"Set to: {result[0]['value']}")

# Update call with keyword arguments (for record types)
# If your method takes a single record parameter, you can use kwargs:
# result = counter.update_profile(name="Alice", age=30)

Certificate Verification with Canister: By default, Canister methods enable certificate verification (verify_certificate=True) to match Agent.update() behavior for security. You can control this per method call:

# Default: certificate verification enabled (requires blst)
result = counter.set(42)  # Uses verify_certificate=True by default

# Explicitly enable verification (same as default)
result = counter.set(42, verify_certificate=True)

# Disable verification (useful when blst is not installed)
result = counter.set(42, verify_certificate=False)

Important Notes:

  • verify_certificate is a control parameter, not a method argument. It's extracted from kwargs before processing method arguments.
  • Default value is True to match Agent.update() default behavior for security.
  • If blst is not installed and you don't pass verify_certificate=False, update calls will fail.
  • For query calls, certificate verification is not applicable (queries don't return certificates).

Example: Complete Canister Usage

from icp_core import Agent, Client, Identity, Canister

# Setup
client = Client("https://ic0.app")
identity = Identity(anonymous=True)
agent = Agent(identity, client)

# Define interface
DID = """
service : {
  get : () -> (nat) query;
  set : (nat) -> (nat);
  increment : () -> (nat)
}
"""

# Create canister wrapper
counter = Canister(agent, "wcrzb-2qaaa-aaaap-qhpgq-cai", DID)

# Query (no verification needed)
current = counter.get()
print(f"Current: {current[0]['value']}")

# Update with verification enabled (default, requires blst)
try:
    result = counter.set(100)
    print(f"Set to: {result[0]['value']}")
except Exception as e:
    if "blst" in str(e).lower():
        # Fallback: disable verification if blst not available
        result = counter.set(100, verify_certificate=False)
        print(f"Set to: {result[0]['value']} (verification disabled)")
    else:
        raise

# Update with verification explicitly disabled
result = counter.increment(verify_certificate=False)
print(f"Incremented: {result[0]['value']}")

๐Ÿ”‘ Installing blst (optional)

blst is required for certificate verification (enabled by default). If blst is not installed, you can disable verification with verify_certificate=False.

Prerequisites

macOS:

# Install Xcode Command Line Tools
xcode-select --install

# Install SWIG (required for Python bindings)
brew install swig

Linux (Ubuntu/Debian):

sudo apt-get update
sudo apt-get install build-essential swig python3-dev

Linux (Fedora/RHEL):

sudo dnf install gcc gcc-c++ make swig python3-devel

macOS / Linux Installation

Method 1: Build and add to PYTHONPATH (recommended for development)

git clone https://github.com/supranational/blst
cd blst/bindings/python

# For Apple Silicon (M1/M2/M3) if you encounter ABI issues:
# export BLST_PORTABLE=1

python3 run.me

# Temporary (current session only):
export PYTHONPATH="$PWD:$PYTHONPATH"

# Permanent (add to ~/.bashrc or ~/.zshrc):
echo 'export PYTHONPATH="/path/to/blst/bindings/python:$PYTHONPATH"' >> ~/.bashrc
source ~/.bashrc

Method 2: Install to site-packages (recommended for production)

git clone https://github.com/supranational/blst
cd blst/bindings/python

# For Apple Silicon (M1/M2/M3) if needed:
# export BLST_PORTABLE=1

python3 run.me

# Copy to site-packages
BLST_SRC="$PWD"
PYBIN="python3"

SITE_PURE="$($PYBIN -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')"
SITE_PLAT="$($PYBIN -c 'import sysconfig; print(sysconfig.get_paths()["platlib"])')"

cp "$BLST_SRC/blst.py" "$SITE_PURE"/
cp "$BLST_SRC"/_blst*.so "$SITE_PLAT"/

Method 3: Install in virtual environment

# Activate your virtual environment first
source venv/bin/activate  # or: source .venv/bin/activate

git clone https://github.com/supranational/blst
cd blst/bindings/python

# For Apple Silicon if needed:
# export BLST_PORTABLE=1

python3 run.me

# Copy to virtual environment's site-packages
BLST_SRC="$PWD"
SITE_PURE="$(python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')"
SITE_PLAT="$(python3 -c 'import sysconfig; print(sysconfig.get_paths()["platlib"])')"

cp "$BLST_SRC/blst.py" "$SITE_PURE"/
cp "$BLST_SRC"/_blst*.so "$SITE_PLAT"/

Windows Installation

Option 1: WSL2 (Ubuntu) - Recommended

  1. Install WSL2 and Ubuntu from Microsoft Store
  2. Follow the Linux installation instructions above in WSL2

Option 2: Native Windows (Advanced)

  1. Install Visual Studio Build Tools with C++ support
  2. Install SWIG for Windows from swig.org
  3. Install Python 3.8+ with development headers
  4. Follow the Linux build steps in PowerShell or Command Prompt
  5. Note: Windows support is experimental; WSL2 is recommended

Verify Installation

Test if blst is correctly installed:

try:
    import blst
    assert all(hasattr(blst, n) for n in ("P1_Affine", "P2_Affine", "Pairing", "BLST_SUCCESS"))
    print("โœ“ blst is installed and working correctly")
except (ModuleNotFoundError, AssertionError):
    print("โœ— blst is not available or incomplete")

Or test with icp-py-core:

from icp_certificate.certificate import ensure_blst_available
try:
    ensure_blst_available()
    print("โœ“ blst is available for certificate verification")
except RuntimeError as e:
    print(f"โœ— {e}")

Troubleshooting

Issue: "No module named 'blst'"

  • Ensure blst.py and _blst*.so are in your Python path
  • Check python3 -c "import sys; print(sys.path)" to see search paths
  • If using virtual environment, ensure it's activated

Issue: "ABI mismatch" on Apple Silicon

  • Set export BLST_PORTABLE=1 before running python3 run.me
  • This builds a portable version compatible with all architectures

Issue: "SWIG not found"

  • Install SWIG: brew install swig (macOS) or sudo apt-get install swig (Linux)
  • Ensure SWIG is in your PATH: which swig

Issue: Import succeeds but API is incomplete

  • Ensure you're using the official supranational/blst repository
  • Rebuild: cd blst/bindings/python && python3 run.me
  • Check that all required symbols exist: P1_Affine, P2_Affine, Pairing, BLST_SUCCESS

๐Ÿง  Features

  1. ๐Ÿงฉ Candid encode & decode
  2. ๐Ÿ” ed25519 & secp256k1 identities
  3. ๐Ÿงพ Principal utilities (strict DER mode)
  4. โš™๏ธ High-level canister calls via Agent
  5. ๐Ÿช™ Support for Ledger / Governance / Management / Cycles Wallet
  6. ๐Ÿ” Sync & async APIs

๐Ÿงฐ Example โ€” End-to-End

from icp_core import Agent, Client, Identity, Types

client = Client("https://ic0.app")
iden = Identity()
agent = Agent(iden, client)

# Update (auto-encode [42], certificate verification enabled by default)
agent.update("wcrzb-2qaaa-aaaap-qhpgq-cai", "set_value", [42])

# Query (auto-encode empty args)
res = agent.query("wcrzb-2qaaa-aaaap-qhpgq-cai", "get_value", None, return_type=[Types.Nat])
print(res)

๐Ÿ”„ Migration

Migrating from ic-py? See MIGRATION.md for:

  • New package layout (icp_* subpackages and the icp_core facade)
  • Endpoint changes (v3 call)
  • Argument auto-encoding in Agent.update() / Agent.query()
  • Certificate verification flag

๐Ÿ“ Changelog

We maintain release notes on GitHub Releases:
https://github.com/eliezhao/icp-py-core/releases


๐Ÿ—บ Roadmap

See ROADMAP.md

โœ… Milestone 1: v3 endpoint migration & polling stability
โœ… Milestone 2: Certificate verification with blst
๐Ÿ”œ Milestone 3: ICRC utilities, Candid enhancements, type reflection


๐Ÿ”– Version

  • Current release: v2.1.1

๐Ÿ™Œ Acknowledgments

Special thanks to the IC community and contributors to the original ic-py.
icp-py-core continues this legacy with modern Python standards and long-term maintenance.


๐Ÿ“š Additional Resources

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

icp_py_core-2.1.1.tar.gz (59.8 kB view details)

Uploaded Source

Built Distribution

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

icp_py_core-2.1.1-py3-none-any.whl (56.5 kB view details)

Uploaded Python 3

File details

Details for the file icp_py_core-2.1.1.tar.gz.

File metadata

  • Download URL: icp_py_core-2.1.1.tar.gz
  • Upload date:
  • Size: 59.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for icp_py_core-2.1.1.tar.gz
Algorithm Hash digest
SHA256 8b0c1380167fa063bdb0dd02cbf05effc45e0ee098de3e6495dd9ec070cd5965
MD5 dbd6b8e1c578dd9c37b61c4692c94e8f
BLAKE2b-256 5e4e520defa96cc7d4a766add17e58a634958fa03cd294649d106aad44f0707c

See more details on using hashes here.

Provenance

The following attestation bundles were made for icp_py_core-2.1.1.tar.gz:

Publisher: release.yml on eliezhao/icp-py-core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file icp_py_core-2.1.1-py3-none-any.whl.

File metadata

  • Download URL: icp_py_core-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 56.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for icp_py_core-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 670390903520f2ae944a0a38beecf65c5de2473f4cfc7e587a46f5a6ed33babc
MD5 27b64caa75af8ee9fc52443f0b616e30
BLAKE2b-256 844f19c5ea11792182ed7c2163bad38cdfae53be95e6a0800ec209c28947dce3

See more details on using hashes here.

Provenance

The following attestation bundles were made for icp_py_core-2.1.1-py3-none-any.whl:

Publisher: release.yml on eliezhao/icp-py-core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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