-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcandid.py
322 lines (261 loc) · 6.9 KB
/
candid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'''
test candid encode/decode
include: Null, Nat, Int, Text, Variant, Record, ....
'''
from ic.candid import Types, encode, decode
'''
@param: Required
format for example: [{'type': Types.Nat, 'value': 0}, ...]
@rawTypes: Optional
if rawTypes is None, decode return
However, if you specific return types, it will return what you want.
rawType accosiated with your did files. In future, we will auto parse
return types once you provides did file.
'''
def test(params, rawTypes = None):
print('------------------------------------------')
print('input params:', params)
res = encode(params)
print('encode: ', res.hex())
if rawTypes:
print('specific return type:', rawTypes)
print(' decode:', decode(res, rawTypes))
else:
print('There is no specific return type:')
print(' decode:', decode(res))
# Empty Test
types = Types.Empty
val = None
params = [
{'type': types, 'value': val}
]
# TypeError: Invalid empty argument: None
try:
encode(params)
except:
print('Empty encode error: Invalid empty argument: None')
# ValueError: Empty cannot appear as an output
try:
decode(bytes.fromhex('4449444c00016f'))
except:
print('Empty decode error: Empty cannot appear as an output')
# Null Test
types = Types.Null
val = None
params = [
{'type': types, 'value': val}
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
# Bool Test
types = Types.Bool
val = True
params = [
{'type': types, 'value': val}
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
# Text Test
types = Types.Text
val1 = 'Rocklabs!'
val2 = "icpy is a good SDK for ic developers"
params = [
{'type': types, 'value': val1},
{'type': types, 'value': val2},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=[types, types])
# Int Test
types = Types.Int
val1 = 12345
val2 = -12345
params = [
{'type': types, 'value': val1},
{'type': types, 'value': val2},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=[types, types])
# Nat Test
types = Types.Nat
val1 = 12345
val2 = 6789
params = [
{'type': types, 'value': val1},
{'type': types, 'value': val2},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=[types, types])
# Float32 and Float64 Test
type1 = Types.Float32
type2 = Types.Float64
val1 = 12.34
val2 = 56.789
params = [
{'type': type1, 'value': val1},
{'type': type2, 'value': val2},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=[type1, type2])
# Int8,16,32,64 and Nat8,16,32,64 Test
type1 = Types.Int8
type2 = Types.Int16
type3 = Types.Int32
type4 = Types.Int64
type5 = Types.Nat8
type6 = Types.Nat16
type7 = Types.Nat32
type8 = Types.Nat64
val1 = -113
val2 = -12455
val3 = 13454
val4 = 346745456
val5 = 12
val6 = 35654
val7 = 456787656
val8 = 56789876567654567
params = [
{'type': type1, 'value': val1},
{'type': type2, 'value': val2},
{'type': type3, 'value': val3},
{'type': type4, 'value': val4},
{'type': type5, 'value': val5},
{'type': type6, 'value': val6},
{'type': type7, 'value': val7},
{'type': type8, 'value': val8},
]
# There is no specific return type
test(params=params)
# Sepecific return types (part of returns)
test(params=params, rawTypes=[type1, type2, type3])
# Tuple Test
types = Types.Tuple(Types.Nat, Types.Text)
vals = (123456, 'rocklabs')
params = [
{'type': types, 'value': vals},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
# Opt Test
types = Types.Opt(Types.Text)
val = ['rocklabs']
params = [
{'type': types, 'value': val},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
# Vec Test
types = Types.Vec(Types.Nat)
vals = [1, 2, 3, 4]
params = [
{'type': types, 'value': vals},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
# Vec + Tuple Test
types = Types.Vec(Types.Tuple(Types.Nat, Types.Text))
vals = [(123, 'rocklabs')]
params = [
{'type': types, 'value': vals},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
# Record Test
types = Types.Record({'name':Types.Text, 'assets': Types.Int})
vals = {'name': 'rocklabs', 'assets': 888888888}
params = [
{'type': types, 'value': vals},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
# Tuple(Vec, Record) Test
types = Types.Tuple(Types.Vec(Types.Text), Types.Record({'name':Types.Text, 'assets': Types.Int}))
vals = (['rocklabs'], {'name': 'rocklabs', 'assets': 888888888})
params = [
{'type': types, 'value': vals},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
# Variant Test
types = Types.Variant({'ok': Types.Text, 'err': Types.Text})
val = {'ok': 'rocklabs!'}
params = [
{'type': types, 'value': val},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
# Tuple(Variant) Test
types = Types.Tuple(Types.Variant({'ok': Types.Text, 'err': Types.Text}))
val = ({'ok': 'rocklabs!'},)
params = [
{'type': types, 'value': val},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
# Principle Test
types = Types.Principal
val = 'expmt-gtxsw-inftj-ttabj-qhp5s-nozup-n3bbo-k7zvn-dg4he-knac3-lae'
params = [
{'type': types, 'value': val},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
# Opt(Principle Test)
types = Types.Opt(Types.Principal)
val = ['expmt-gtxsw-inftj-ttabj-qhp5s-nozup-n3bbo-k7zvn-dg4he-knac3-lae']
params = [
{'type': types, 'value': val},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
#Func Text
types = Types.Func([Types.Text], [Types.Nat], ['query'])
val = ['expmt-gtxsw-inftj-ttabj-qhp5s-nozup-n3bbo-k7zvn-dg4he-knac3-lae', 'rocklabs']
params = [
{'type': types, 'value': val},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)
#Service Text
types = Types.Service({'rocklabs' : Types.Func([Types.Text], [Types.Nat], ['query'])})
val = 'expmt-gtxsw-inftj-ttabj-qhp5s-nozup-n3bbo-k7zvn-dg4he-knac3-lae'
params = [
{'type': types, 'value': val},
]
# There is no specific return type
test(params=params)
# Sepecific return types
test(params=params, rawTypes=types)