Skip to content

Commit 95873f0

Browse files
committed
fix principal encode
1 parent 5a8797d commit 95873f0

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

ic/agent.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def sign_request(req, iden):
1616
'sender_pubkey': sig[0],
1717
'sender_sig': sig[1]
1818
}
19+
# print(req_id, envelop, cbor2.dumps(envelop))
1920
return req_id, cbor2.dumps(envelop)
2021

2122
class Agent:
@@ -54,6 +55,7 @@ def read_state_endpoint(self, canister_id, data):
5455
return result
5556

5657
def query_raw(self, canister_id, method_name, arg):
58+
print(arg)
5759
req = {
5860
'request_type': "query",
5961
'sender': self.identity.sender().bytes,
@@ -71,7 +73,7 @@ def query_raw(self, canister_id, method_name, arg):
7173
elif result['status'] == 'rejected':
7274
return result['reject_message']
7375

74-
def update_raw(self):
76+
def update_raw(self, canister_id, method_name, arg):
7577
req = {
7678
'request_type': "call",
7779
'sender': self.identity.sender().bytes,
@@ -82,9 +84,10 @@ def update_raw(self):
8284
}
8385
req_id, data = sign_request(req, self.identity)
8486
_ = self.call_endpoint(canister_id, req_id, data)
87+
print('hhh')
8588
# poll req_id status to get result
86-
result = self.poll(canister_id, req_id)
87-
print(result)
89+
# result = self.poll(canister_id, req_id)
90+
# print(result)
8891

8992
def read_state_raw(self, canister_id, paths):
9093
req = {

ic/candid.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ def encode_type(t, v):
4545
elif t == Types.Nat64:
4646
return int.to_bytes(v, 8, byteorder='big')
4747
elif t == Types.Principal:
48+
tag = int.to_bytes(1, 1, byteorder='big')
49+
b = v
4850
if isinstance(v, str):
49-
return Principal.from_str(v).bytes
51+
b = Principal.from_str(v).bytes
5052
elif isinstance(v, Principal):
51-
return v.bytes
52-
elif isinstance(v, bytes):
53-
return v
53+
b = v.bytes
54+
l = leb128.u.encode(len(b))
55+
return tag + l + b
5456
elif t == Types.Empty:
5557
return b''
5658
# TODO: int8, int32, int64, float32, float64, text, ...
@@ -68,6 +70,7 @@ def encode(params):
6870
for p in params:
6971
ty += leb128.i.encode(p['type'].value)
7072
value += encode_type(p['type'], p['value'])
73+
print(params, 'types:', ty, 'value:', value)
7174
data += ty
7275
data += value
7376
return data

test_agent.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55

66
client = Client()
77
iden = Identity(privkey="833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42")
8-
print(iden)
9-
msg = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
10-
sig = iden.sign(bytes(bytearray.fromhex(msg)))
11-
print(sig[1].hex())
8+
print(Principal.self_authenticating(iden.der_pubkey))
129
ag = Agent(iden, client)
1310

1411
# ret = ag.query_raw("gvbup-jyaaa-aaaah-qcdwa-cai", "totalSupply", encode([]))
15-
ret = ag.query_raw("gvbup-jyaaa-aaaah-qcdwa-cai", "name", encode([]))
12+
# ret = ag.query_raw("gvbup-jyaaa-aaaah-qcdwa-cai", "name", encode([]))
1613
'''
1714
ret = ag.query_raw(
1815
"gvbup-jyaaa-aaaah-qcdwa-cai",
@@ -21,6 +18,15 @@
2118
{'type': Types.Principal, 'value': iden.sender().bytes}
2219
])
2320
)
24-
'''
2521
print(ret)
22+
'''
2623

24+
ret = ag.update_raw(
25+
"gvbup-jyaaa-aaaah-qcdwa-cai",
26+
"transfer",
27+
encode([
28+
{'type': Types.Principal, 'value': 'aaaaa-aa'},
29+
{'type': Types.Nat, 'value': 10000000000}
30+
])
31+
)
32+
print(ret)

0 commit comments

Comments
 (0)