Skip to main content

peaq python sdk

Project description

Peaq SDK (Python)

A robust Python SDK for seamless interaction with the peaq blockchain, supporting both EVM and Substrate-based operations. It provides tools to manage Decentralized Identifiers (DIDs), interact with on-chain storage, and perform blockchain transactions with or without local signing capabilities.

Features

  • DID creation and management
  • On-chain storage utilization
  • RBAC capabilities (coming soon)
  • Universal Machine Time (coming soon)

Installation

Install via pip:

pip install peaq-sdk

Quick Start

Initialize the SDK for interaction:

EVM Connection

from peaq_sdk import Sdk
from peaq_sdk.types import ChainType

# Without signing capabilities (read-only and transaction creation)
sdk = Sdk.create_instance(
    base_url="https://peaq.api.onfinality.io/public",
    chain_type=ChainType.EVM,
)

# With signing capabilities
sdk = Sdk.create_instance(
    base_url="https://peaq.api.onfinality.io/public",
    chain_type=ChainType.EVM,
    seed=my_private_key_or_mnemonic,
)

Substrate Connection

from peaq_sdk import Sdk
from peaq_sdk.types import ChainType

# Without signing capabilities (read-only and transaction creation)
sdk = Sdk.create_instance(
    base_url="wss://peaq.api.onfinality.io/public-ws",
    chain_type=ChainType.SUBSTRATE,
)

# With signing capabilities
sdk = Sdk.create_instance(
    base_url="wss://peaq.api.onfinality.io/public-ws",
    chain_type=ChainType.SUBSTRATE,
    seed=my_private_key_or_mnemonic,
)

SDK Operations

All return objects are either:

Unsent Transaction/Call (seed not in create_instance)

  • Message indicating what tx or call was built.
  • Transaction/Call to send:
    • For EVM instance it will return an EVM Transaction the user can send themselves.
    • The Substrate instance returns a Call object that must be sent using the substrate-interface package.
    • You can see how these calls are sent in the sdk under the _send_evm_tx() and _send_substrate_tx() functions in the peaq_sdk/base.py file.

Sent Transaction (seed in create_instance)

  • Message indicating the operation performed.
  • Receipt of the EVM/Substrate transaction.

Read operations are able to perform without a seed/private key being set. They query the item and return it to the user.

Below are quick examples how to use the sdk assuming the seed is set. For a more detailed tutorial please checkout our Python SDK Reference.

Decentralized Identifiers (DID)

Create a DID

# example that uses all of the custom fields
from peaq_sdk.types import CustomDocumentFields, Verification, Service, Signature

custom_fields = CustomDocumentFields(
    verifications=[
        Verification(type='EcdsaSecp256k1RecoveryMethod2020')
        ],
    signature=Signature(type='EcdsaSecp256k1RecoveryMethod2020', issuer='0x9Eeab1aCcb1A701aEfAB00F3b8a275a39646641E', hash='0xGENERATED_HASH'),
    services=[
        Service(id='#cid', type='peaqStorage', data='CID_VALUE')
        ]
    )

receipt = sdk.did.create(name="myDid", custom_document_fields=custom_fields)

Read a DID

# EVM
result = sdk.did.read(name="myDid", wss_base_url="wss://peaq.api.onfinality.io/public-ws")

# Substrate
result = sdk.did.read(name="myDid")

Update a DID

from peaq_sdk.types import CustomDocumentFields, Verification, Service, Signature

custom_fields = CustomDocumentFields(
    verifications=[
        Verification(type='EcdsaSecp256k1RecoveryMethod2020')
        ],
    signature=Signature(type='EcdsaSecp256k1RecoveryMethod2020', issuer='0x9Eeab1aCcb1A701aEfAB00F3b8a275a39646641C', hash='0xGENERATED_HASH'),
    services=[
        Service(id='#cid', type='peaqStorage', data='CID_VALUE')
        ]
    )

result = sdk.did.update(name="myDid", custom_document_fields=custom_fields)

Delete a DID

result = sdk.did.delete(name="myDid")

On-chain Storage

Add Item

result = sdk.storage.add_item(item_type='key', item='value')

Read Item

result = sdk.storage.get_item(item_type='key')

Update Item

sdk.storage.update_item(item_type='key', item='new_value')

Remove Item

sdk.storage.remove_item(item_type='key')

Security and Private Keys

  • Private keys are never stored, sent, or logged by the SDK.
    • Used solely to generate an Account or Keypair.
  • Signing occurs locally only.
  • Ensure your local environment securely manages and protects private keys. Never share your private keys.

Development

Virtual Environment

python3 -m venv working_env
source working_env/bin/activate

Build Package

pip install -r requirements.txt
python -m build

Exit Virtual Environment

deactivate

Remove virtual env

rm -rf working_env

Testing

Checkout the README.md file in /tests directory for more information.

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

peaq_sdk-0.2.0.tar.gz (42.9 kB view details)

Uploaded Source

Built Distribution

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

peaq_sdk-0.2.0-py3-none-any.whl (48.2 kB view details)

Uploaded Python 3

File details

Details for the file peaq_sdk-0.2.0.tar.gz.

File metadata

  • Download URL: peaq_sdk-0.2.0.tar.gz
  • Upload date:
  • Size: 42.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for peaq_sdk-0.2.0.tar.gz
Algorithm Hash digest
SHA256 609b78b3eda36c07f13a44d68a77460df964066897213efd31b319e5f5a1414e
MD5 4b478e44f63e3ef85354ad88976a22ef
BLAKE2b-256 1faf5cd640b4bff340f24a1501b1ec17bf6328deecfc2e3449ef4363d44e8cd1

See more details on using hashes here.

Provenance

The following attestation bundles were made for peaq_sdk-0.2.0.tar.gz:

Publisher: publish.yml on peaqnetwork/peaq-sdk-py

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

File details

Details for the file peaq_sdk-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: peaq_sdk-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 48.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for peaq_sdk-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cc07a082a66d84b18943183ae7d274554fcc7dad0a12e56e8508656e1c18145c
MD5 abe4e31ef2db611cc3b51c1103a7b9a7
BLAKE2b-256 835790d0e06f08bc4142c50be0ddf6909b174ac313d88fb2202d2119095aebee

See more details on using hashes here.

Provenance

The following attestation bundles were made for peaq_sdk-0.2.0-py3-none-any.whl:

Publisher: publish.yml on peaqnetwork/peaq-sdk-py

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