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 (Role-Based Access Control) capabilities
- Token transfer functionality
- 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-interfacepackage. - You can see how these calls are sent in the sdk under the
_send_evm_tx()and_send_substrate_tx()functions in thepeaq_sdk/base.pyfile.
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
# Works on both EVM and 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')
Role-Based Access Control (RBAC)
Create a Role
# Create a role with auto-generated ID
result = sdk.rbac.create_role(role_name="Admin")
# Create a role with custom ID (must be 32 characters)
custom_role_id = "admin_role_123456789012345678901"
result = sdk.rbac.create_role(role_name="Admin", role_id=custom_role_id)
Fetch a Role
# Read a role by owner address and role ID
owner_address = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" # Substrate address
role_id = "admin_role_123456789012345678901"
result = sdk.rbac.fetch_role(owner=owner_address, role_id=role_id)
Assign Role to User
role_id = "admin_role_123456789012345678901"
user_id = "user_id_12345678901234567890123" # 32-character user ID
result = sdk.rbac.assign_role_to_user(role_id=role_id, user_id=user_id)
Token Transfer
Native Token Transfer
# Transfer native tokens to another address
recipient = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" # Can be Substrate or EVM address
amount = 1.5 # Human-readable amount (e.g., 1.5 PEAQ)
result = sdk.transfer.native(to=recipient, amount=amount)
ERC-20 Token Transfer
# Transfer ERC-20 tokens (EVM only)
erc20_contract = "0x1234567890123456789012345678901234567890"
recipient = "0x9876543210987654321098765432109876543210"
amount = 100.0 # Amount in human-readable format
token_decimals = 18 # Token decimals (default: 18)
result = sdk.transfer.erc20(
erc_20_address=erc20_contract,
recipient_address=recipient,
amount=amount,
token_decimals=token_decimals
)
ERC-721 (NFT) Transfer
# Transfer NFT tokens (EVM only)
nft_contract = "0x1234567890123456789012345678901234567890"
recipient = "0x9876543210987654321098765432109876543210"
token_id = 42 # NFT token ID
result = sdk.transfer.erc721(
erc_721_address=nft_contract,
recipient_address=recipient,
token_id=token_id
)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file peaq_sdk-0.2.1.tar.gz.
File metadata
- Download URL: peaq_sdk-0.2.1.tar.gz
- Upload date:
- Size: 44.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
262b0c3c639cac9b71751bdc638bd0f55dbd497007120aeeb2c97dac13f32f63
|
|
| MD5 |
b4a7467cb040ca8ee77fc6457f60769b
|
|
| BLAKE2b-256 |
53884e0043d219c5cd01fbda97f3e04bcc199869094add6f45390a845fae4a70
|
Provenance
The following attestation bundles were made for peaq_sdk-0.2.1.tar.gz:
Publisher:
publish.yml on peaqnetwork/peaq-sdk-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
peaq_sdk-0.2.1.tar.gz -
Subject digest:
262b0c3c639cac9b71751bdc638bd0f55dbd497007120aeeb2c97dac13f32f63 - Sigstore transparency entry: 258526461
- Sigstore integration time:
-
Permalink:
peaqnetwork/peaq-sdk-py@c2bb08a94e4e0d5bc409d7567e8fdab14ddb4252 -
Branch / Tag:
refs/heads/dev - Owner: https://github.com/peaqnetwork
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c2bb08a94e4e0d5bc409d7567e8fdab14ddb4252 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file peaq_sdk-0.2.1-py3-none-any.whl.
File metadata
- Download URL: peaq_sdk-0.2.1-py3-none-any.whl
- Upload date:
- Size: 48.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f96dc5f29725c134fc5b9d36ccb277a985398fabb85e653311b0976f517031b
|
|
| MD5 |
150aa6f884ad531aff9ae0d226f074ba
|
|
| BLAKE2b-256 |
74532052c9f4ffe2c40ad90cfd92c8f30990b2f36f32cf6965b747732301c575
|
Provenance
The following attestation bundles were made for peaq_sdk-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on peaqnetwork/peaq-sdk-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
peaq_sdk-0.2.1-py3-none-any.whl -
Subject digest:
5f96dc5f29725c134fc5b9d36ccb277a985398fabb85e653311b0976f517031b - Sigstore transparency entry: 258526473
- Sigstore integration time:
-
Permalink:
peaqnetwork/peaq-sdk-py@c2bb08a94e4e0d5bc409d7567e8fdab14ddb4252 -
Branch / Tag:
refs/heads/dev - Owner: https://github.com/peaqnetwork
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c2bb08a94e4e0d5bc409d7567e8fdab14ddb4252 -
Trigger Event:
workflow_dispatch
-
Statement type: