Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rocklabs-io/ic-py
Python Agent Library for the DFINITY Internet Computer
https://github.com/rocklabs-io/ic-py
agent dfinity ic internet-computer python sdk
Last synced: 5 days ago
JSON representation
Python Agent Library for the DFINITY Internet Computer
- Host: GitHub
- URL: https://github.com/rocklabs-io/ic-py
- Owner: rocklabs-io
- License: mit
- Created: 2021-11-16T09:08:00.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-15T09:36:03.000Z (5 months ago)
- Last Synced: 2024-10-30T09:39:24.282Z (8 days ago)
- Topics: agent, dfinity, ic, internet-computer, python, sdk
- Language: Python
- Homepage:
- Size: 4.46 MB
- Stars: 128
- Watchers: 8
- Forks: 27
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-internet-computer - ic-py - Library for interfacing with the IC using Python. (Client Libraries (Agents) / Python)
- awesome-icp - rocklabs-io/ic-py
README
## Python Agent Library for the Internet Computer
![ic-py](./pics/ic-py.png)
`ic-py` provides basic modules to interact with canisters on the DFINITY Internet Computer.
### Install
```
pip3 install ic-py
```### Features
1. candid types encode & decode
2. support secp256k1 & ed25519 identity, pem file import
3. canister DID file parsing
4. canister class, initialized with canister id and DID file
5. common canister interfaces: ledger, management, nns, cycles wallet
6. async support### Modules & Usage
#### 1. Principal
Create an instance:
```python
from ic.principal import Principal
p = Principal() # default is management canister id `aaaaa-aa`
p1 = Principal(bytes=b'') # create an instance from bytes
p2 = Principal.anonymous() # create anonymous principal
p3 = Principal.self_authenticating(pubkey) # create a principal from public key
p4 = Principal.from_str('aaaaa-aa') # create an instance from string
p5 = Principal.from_hex('xxx') # create an instance from hex
```Class methods:
```python
p.bytes # principal bytes
p.len # byte array length
p.to_str() # convert to string
```#### 2. Identity
Create an instance:
```python
from ic.identity import Identity
i = Identity() # create an identity instance, key is randomly generated
i1 = Identity(privkey = "833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42") # create an instance from private key
```Sign a message:
```python
msg = b”ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f“
sig = i.sign(msg) # sig = (der_encoded_pubkey, signature)
```#### 3. Client
Create an instance:
```python
from ic.client import Client
client = Client(url = "https://ic0.app")
```#### 4. Candid
Encode parameters:
```python
from ic.candid import encode, decode, Types
# params is an array, return value is encoded bytes
params = [{'type': Types.Nat, 'value': 10}]
data = encode(params)
```Decode parameters:
```python
# data is bytes, return value is an parameter array
params = decode(data)
```#### 5. Agent
Create an instance:
```python
from ic.client import Client
from ic.identity import Identity
from ic.agent import Agent
# Identity and Client are dependencies of Agent
iden = Identity()
client = Client()
agent = Agent(iden, client)
```Query call:
```python
# query the name of token canister `gvbup-jyaaa-aaaah-qcdwa-cai`
name = agent.query_raw("gvbup-jyaaa-aaaah-qcdwa-cai", "name", encode([]))
```Update call:
```python
# transfer 100 token to blackhole address `aaaaa-aa`
params = [
{'type': Types.Principal, 'value': 'aaaaa-aa'},
{'type': Types.Nat, 'value': 10000000000}
]
result = agent.update_raw("gvbup-jyaaa-aaaah-qcdwa-cai", "transfer", encode(params))
```#### 6. Canister
Create a canister instance with candid interface file and canister id, and call canister method with canister instance:
```python
from ic.canister import Canister
from ic.client import Client
from ic.identity import Identity
from ic.agent import Agent
from ic.candid import Typesiden = Identity()
client = Client()
agent = Agent(iden, client)
# read governance candid from file
governance_did = open("governance.did").read()
# create a governance canister instance
governance = Canister(agent=agent, canister_id="rrkah-fqaaa-aaaaa-aaaaq-cai", candid=governance_did)
# call canister method with instance
res = governance.list_proposals(
{
'include_reward_status': [],
'before_proposal': [],
'limit': 100,
'exclude_topic': [],
'include_status': [1]
}
)
```### 7. Async request
ic-py also supports async requests:
```python
import asyncio
from ic.canister import Canister
from ic.client import Client
from ic.identity import Identity
from ic.agent import Agent
from ic.candid import Typesiden = Identity()
client = Client()
agent = Agent(iden, client)
# read governance candid from file
governance_did = open("governance.did").read()
# create a governance canister instance
governance = Canister(agent=agent, canister_id="rrkah-fqaaa-aaaaa-aaaaq-cai", candid=governance_did)
# async call
async def async_test():
res = await governance.list_proposals_async(
{
'include_reward_status': [],
'before_proposal': [],
'limit': 100,
'exclude_topic': [],
'include_status': [1]
}
)
print(res)
asyncio.run(async_test())
```