1
- ## Python Agent Library for the IC
1
+ ## Python Agent Library for the Internet Computer
2
2
3
3
` ic-py ` provides basic modules to interact with canisters on the DFINITY Internet Computer. Its still under active development.
4
4
5
+ ### Install
6
+
7
+ ```
8
+ pip3 install ic-py
9
+ ```
10
+
5
11
### Modules & Usage
6
12
7
13
#### 1. Principal
8
14
9
15
Create an instance:
10
16
11
- ``` js
17
+ ``` python
18
+ from ic.principal import Principal
12
19
p = Principal() # default is management canister id `aaaaa-aa`
13
20
p1 = Principal(bytes = xxx) # create an instance from bytes
14
21
p2 = Principal.anonymous() # create anonymous principal
@@ -19,7 +26,7 @@ p5 = Principal.from_hex('xxx') # create an instance from hex
19
26
20
27
Class methods:
21
28
22
- ``` js
29
+ ``` python
23
30
p.bytes # principal bytes
24
31
p.len # byte array length
25
32
p.to_str() # convert to string
@@ -29,14 +36,15 @@ p.to_str() # convert to string
29
36
30
37
Create an instance:
31
38
32
- ``` js
39
+ ``` python
40
+ from ic.identity import Identity
33
41
i = Identity() # create an identity instance, key is randomly generated
34
42
i1 = Identity(privkey = " 833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42" ) # create an instance from private key
35
43
```
36
44
37
45
Sign a message:
38
46
39
- ``` js
47
+ ``` python
40
48
msg = b”ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f“
41
49
sig = i.sign(msg) # sig = (der_encoded_pubkey, signature)
42
50
```
@@ -45,23 +53,25 @@ sig = i.sign(msg) # sig = (der_encoded_pubkey, signature)
45
53
46
54
Create an instance:
47
55
48
- ``` js
56
+ ``` python
57
+ from ic.client import Client
49
58
client = Client(url = " https://ic0.app" )
50
59
```
51
60
52
61
#### 4. Candid
53
62
54
63
Encode parameters:
55
64
56
- ``` js
65
+ ``` python
66
+ from ic.candid import encode, decode
57
67
# params is an array, return value is encoded bytes
58
68
params = [{' type' : Types.Nat, ' value' : 10 }]
59
69
data = encode(params)
60
70
```
61
71
62
72
Decode parameters:
63
73
64
- ``` js
74
+ ``` python
65
75
# data is bytes, return value is an parameter array
66
76
params = decode(data)
67
77
```
@@ -70,7 +80,10 @@ params = decode(data)
70
80
71
81
Create an instance:
72
82
73
- ``` js
83
+ ``` python
84
+ from ic.client import Client
85
+ from ic.identity import Identity
86
+ from ic.Agent import Agent
74
87
# Identity and Client are dependencies of Agent
75
88
iden = Identity()
76
89
client = Client()
@@ -79,14 +92,14 @@ agent = Agent(iden, client)
79
92
80
93
Query call:
81
94
82
- ``` js
95
+ ``` python
83
96
# query the name of token canister `gvbup-jyaaa-aaaah-qcdwa-cai`
84
97
name = agent.query_raw(" gvbup-jyaaa-aaaah-qcdwa-cai" , " name" , encode([]))
85
98
```
86
99
87
100
Update call:
88
101
89
- ``` js
102
+ ``` python
90
103
# transfer 100 token to blackhole address `aaaaa-aa`
91
104
params = [
92
105
{' type' : Types.Principal, ' value' : ' aaaaa-aa' },
0 commit comments