ic_agent 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c87eb2ad8a0305a051b52137d44398b743540432609f7468eb61318b183caf4
4
- data.tar.gz: c51a300fd8596bce7e064f3f47cd4d025bff47af4dfe84113f55bf70a8d4c5bb
3
+ metadata.gz: b1bb3c395959ef0b33094ab3e34a6087b061a76287ce34b312286703c568fade
4
+ data.tar.gz: f71667c7d06d470c89afd7d9e8734bfded8abc2f7d85a89a77d84fa3ce7e210c
5
5
  SHA512:
6
- metadata.gz: 432a2d181f6bd503f87c0cca4b60086aa059032c88da35382b78b2f587116fea18c7c9692e533cdffa39889dc44325e603320c245ecb5c8038ecd971068b12a0
7
- data.tar.gz: 7ce801835791d320a52d9f0f35e494d8c2e7c5482cc9cddf71742bda0b69ed0fa6d10d05afbc95c90eb30f690f914851baf3487ad94bc8c6ac81d680b1156502
6
+ metadata.gz: 437bab4be804cc57aa1098aa91275d88623e79c98f9190129956eb2ceb0447dd5dbd305ec7425b3a3fd7fc945035a79329b6a73052c876bc31118a94387cb61a
7
+ data.tar.gz: 05adc3829656980d0a833ac495cbfbf67e84fee5474dda75e39d6f94fb538ee22120de034ba7b81c36680ef4444a586409ecb2c6f0f43d53f9cd7aa28b51e394
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ic_agent (0.2.0)
4
+ ic_agent (0.2.1)
5
5
  base32 (~> 0.3.4)
6
6
  bitcoin-ruby (~> 0.0.20)
7
7
  bls12-381 (~> 0.3.0)
data/README.md CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  `ic_agent` provides basic modules to interact with canisters on the DFINITY Internet Computer.
6
6
 
7
+ [![Gem Version](https://badge.fury.io/rb/ic_agent.svg)](https://badge.fury.io/rb/ic_agent)[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
8
+
7
9
 
8
10
  ## Installation
9
11
 
@@ -248,6 +248,9 @@ module IcAgent
248
248
  end
249
249
  end
250
250
 
251
+ # Verify a BLS signature
252
+ # The signature must be exactly 48 bytes (compressed G1 element)
253
+ # The key must be exactly 96 bytes (compressed G2 element)
251
254
  def verify(cert, canister_id)
252
255
  signature_hex = IcAgent::Certificate.signature(cert).str2hex
253
256
  tree = IcAgent::Certificate.tree(cert)
@@ -262,6 +265,7 @@ module IcAgent
262
265
  BLS.verify(signature, msg, public_key)
263
266
  end
264
267
 
268
+ # Check the delegation and return the corresponding root key.
265
269
  def check_delegation(delegation, effective_canister_id, disable_range_check)
266
270
  return @root_key unless delegation
267
271
 
@@ -303,6 +307,7 @@ module IcAgent
303
307
  false
304
308
  end
305
309
 
310
+ # Extract the BLS public key from the DER buffer.
306
311
  def extract_der(der_buf)
307
312
  bls_der_prefix = OpenSSL::BN.from_hex(IcAgent::BLS_DER_PREFIX).to_s(2)
308
313
  expected_length = bls_der_prefix.bytesize + IcAgent::BLS_KEY_LENGTH
@@ -5,24 +5,30 @@ module IcAgent
5
5
  class Assembler
6
6
  TYPE_MAPPING = {}
7
7
 
8
+ # Builds a single Candid type from a given child type.
8
9
  def self.build_single_type(child_type)
9
10
  IcAgent::Candid::BaseTypes.send(child_type)
10
11
  end
11
12
 
13
+
14
+ # Builds a Candid blob type.
12
15
  def self.build_blob
13
16
  IcAgent::Candid::BaseTypes.vec(IcAgent::Candid::BaseTypes.nat8)
14
17
  end
15
18
 
19
+ # Builds a Candid optional type from a given child type.
16
20
  def self.build_opt(child_type, key_types = {})
17
21
  child_type = key_types[child_type].nil? ? build_type(child_type, key_types) : key_types[child_type]
18
22
  IcAgent::Candid::BaseTypes.opt(child_type)
19
23
  end
20
24
 
25
+ # Builds a Candid vector type from a given child type.
21
26
  def self.build_vec(child_type, key_types = {})
22
27
  child_type = key_types[child_type].nil? ? build_type(child_type, key_types) : key_types[child_type]
23
28
  IcAgent::Candid::BaseTypes.vec(child_type)
24
29
  end
25
30
 
31
+ # Builds a Candid record type from a given hash of field names and types.
26
32
  def self.build_record(child_hash, multi_types = {}, key_types = {})
27
33
  child_types = {}
28
34
  child_hash.each_key do |key|
@@ -38,6 +44,7 @@ module IcAgent
38
44
  IcAgent::Candid::BaseTypes.record(child_types)
39
45
  end
40
46
 
47
+ # Builds a Candid variant type from a given hash of field names and types.
41
48
  def self.build_variant(child_hash, multi_types = {}, key_types = {})
42
49
  child_types = {}
43
50
  child_hash.each_key do |key|
@@ -53,6 +60,7 @@ module IcAgent
53
60
  IcAgent::Candid::BaseTypes.variant(child_types)
54
61
  end
55
62
 
63
+ # Builds a Candid type based on the given type string.
56
64
  def self.build_type(type_str, key_types = {}, multi_types = {})
57
65
  opt_code = get_opt_code(type_str)
58
66
 
@@ -91,6 +99,7 @@ module IcAgent
91
99
  end
92
100
  end
93
101
 
102
+ # Replaces the last occurrence of a pattern in a string with the given replacement.
94
103
  def self.replace_last_occurrence(string, pattern, replacement)
95
104
  last_index = string.rindex(pattern)
96
105
  return string unless last_index
@@ -99,18 +108,21 @@ module IcAgent
99
108
  string
100
109
  end
101
110
 
111
+ # Extracts the content of a Candid record type from the type string.
102
112
  def self.get_record_content(record_str)
103
113
  record_str = record_str.sub('record', '').sub('{', '')
104
114
  record_str = replace_last_occurrence(record_str, '}', '')
105
115
  record_str.strip
106
116
  end
107
117
 
118
+ # Extracts the content of a Candid variant type from the type string.
108
119
  def self.get_variant_content(variant_str)
109
120
  variant_str = variant_str.sub('variant', '').sub('{', '')
110
121
  variant_str = replace_last_occurrence(variant_str, '}', '')
111
122
  variant_str.strip
112
123
  end
113
124
 
125
+ # Extracts the key-value pairs from a Candid record item string.
114
126
  def self.get_record_key_value(item_str, index_str, key_index = 0)
115
127
  first_index = item_str.index(index_str)
116
128
  if first_index
@@ -123,16 +135,19 @@ module IcAgent
123
135
  return key, value
124
136
  end
125
137
 
138
+ # Extracts the Candid code (e.g., "record", "variant", "opt", etc.) from the type string.
126
139
  def self.get_opt_code(item_str)
127
140
  opt_code = item_str.strip
128
141
  opt_code.split(' ')[0]
129
142
  end
130
143
 
144
+ # Extracts the child Candid code from the type string.
131
145
  def self.get_child_code(item_str, index_str)
132
146
  first_index = item_str.index(index_str)
133
147
  item_str[(first_index + index_str.size)..].strip
134
148
  end
135
149
 
150
+ # Replaces occurrences of Candid record and variant types with unique type names.
136
151
  def self.replace_multi_type(type_str)
137
152
  replaced_hash = {}
138
153
  modified_str = type_str.gsub(/record\s*{[^{}]*}/) do |match|
@@ -152,6 +167,7 @@ module IcAgent
152
167
  return modified_str, replaced_hash
153
168
  end
154
169
 
170
+ # Gets the refer types used in the type string.
155
171
  def self.get_params_refer_values(type_str)
156
172
  parser = IcAgent::Ast::StatementParser.new
157
173
  parser.parse(type_str)
@@ -159,6 +175,7 @@ module IcAgent
159
175
  refer_type
160
176
  end
161
177
 
178
+ # Recovers the original type string from the multi_types hash.
162
179
  def self.recover_type(type_str, multi_types)
163
180
  multi_types.each_key do |key|
164
181
  type_str = type_str.gsub(key, multi_types[key])
@@ -3,15 +3,19 @@ require 'treetop'
3
3
  module IcAgent
4
4
  module Ast
5
5
  module Nodes
6
+ # Represents a named node in the abstract syntax tree.
6
7
  class NamedNode < Treetop::Runtime::SyntaxNode
8
+ # The title of the named node.
7
9
  def title
8
10
  :named_node
9
11
  end
10
12
 
13
+ # Converts the node and its children to an array representation.
11
14
  def to_array
12
15
  [title] + elements.map(&:to_array)
13
16
  end
14
17
 
18
+ # Converts the node and its children to a string representation.
15
19
  def to_s
16
20
  "#{title.to_s.upcase} #{elements_to_s}"
17
21
  end
@@ -25,53 +29,68 @@ module IcAgent
25
29
  end
26
30
  end
27
31
 
32
+ # Represents an instruction node in the abstract syntax tree, a subclass of NamedNode.
28
33
  class Instruction < NamedNode
34
+ # The title of the instruction node.
29
35
  def title
30
36
  :instruction
31
37
  end
32
38
 
39
+ # Converts the instruction node to a string representation.
33
40
  def to_s
34
41
  elements_to_s
35
42
  end
36
43
  end
37
44
 
45
+ # Represents a comment node in the abstract syntax tree, a subclass of NamedNode.
38
46
  class Comment < NamedNode
47
+ # The title of the comment node.
39
48
  def title
40
49
  :comment
41
50
  end
42
51
 
52
+ # Converts the comment node to a string representation with '#' prefix.
43
53
  def to_s
44
54
  "# #{elements[0].to_s}"
45
55
  end
46
56
  end
47
57
 
58
+ # Represents a DID file node in the abstract syntax tree, a subclass of NamedNode.
48
59
  class DIDFile < NamedNode
60
+ # The title of the DID file node.
49
61
  def title
50
62
  :did_file
51
63
  end
52
64
 
65
+ # Converts the DID file node to a string representation.
53
66
  def to_s
54
67
  elements_to_s
55
68
  end
56
69
  end
57
70
 
71
+ # Represents a type declaration node in the abstract syntax tree, a subclass of NamedNode.
58
72
  class TypeDeclaration < NamedNode
73
+ # The title of the type declaration node.
59
74
  def title
60
75
  :type_declaration
61
76
  end
62
77
 
78
+ # Returns the name of the type parameter.
63
79
  def type_param_name
64
80
  elements[0].source_content
65
81
  end
66
82
 
83
+ # Returns the content of the type parameter without newlines and trailing ';}' replaced with '}'.
67
84
  def type_param_content
68
85
  elements[1].source_content.gsub("\n", '').gsub(';}', '}')
69
86
  end
70
87
 
88
+ # Returns the opt code of the root type element.
71
89
  def type_root_opt_code
72
90
  elements[1].opt_code
73
91
  end
74
92
 
93
+ # Returns an array of type child items.
75
94
  def type_child_items
76
95
  if elements && elements[1] && elements[1].elements && elements[1].elements[0]
77
96
  elements[1].elements[0].elements
@@ -80,6 +99,7 @@ module IcAgent
80
99
  end
81
100
  end
82
101
 
102
+ # Returns an array of keys of type child items.
83
103
  def type_child_item_keys
84
104
  names = []
85
105
  type_child_items.each do |ele|
@@ -88,6 +108,7 @@ module IcAgent
88
108
  names
89
109
  end
90
110
 
111
+ # Returns an array of referenced types in the type parameter content.
91
112
  def type_refer_items
92
113
  source_string = self.type_param_content
93
114
  parser = IcAgent::Ast::StatementParser.new
@@ -96,10 +117,12 @@ module IcAgent
96
117
  refer_type
97
118
  end
98
119
 
120
+ # Converts the type declaration node to a string representation.
99
121
  def to_s
100
122
  text_value
101
123
  end
102
124
 
125
+ # Converts the type declaration node to a hash representation.
103
126
  def to_obj
104
127
  {
105
128
  'type_param_name' => type_param_name,
@@ -110,199 +133,257 @@ module IcAgent
110
133
  end
111
134
  end
112
135
 
136
+ # Represents a base type node in the abstract syntax tree, a subclass of NamedNode.
113
137
  class BaseType < NamedNode
138
+ # The title of the base type node.
114
139
  def title
115
140
  :base_type
116
141
  end
117
142
 
143
+ # Converts the base type node to a string representation.
118
144
  def to_s
119
145
  elements_to_s
120
146
  end
121
147
  end
122
148
 
149
+ # Represents a single base type node in the abstract syntax tree, a subclass of BaseType.
123
150
  class BaseTypeSingle < NamedNode
151
+ # The title of the single base type node.
124
152
  def title
125
153
  :base_type_single
126
154
  end
127
155
 
156
+ # Converts the single base type node to a string representation.
128
157
  def to_s
129
158
  elements_to_s
130
159
  end
131
160
 
161
+ # Returns the opt code for the single base type.
132
162
  def opt_code
133
163
  'single'
134
164
  end
135
165
  end
136
166
 
167
+ # Represents a record base type node in the abstract syntax tree, a subclass of BaseType.
137
168
  class BaseTypeRecord < NamedNode
169
+ # The title of the record base type node.
138
170
  def title
139
171
  :base_type_record
140
172
  end
141
173
 
174
+ # Converts the record base type node to a string representation.
142
175
  def to_s
143
176
  elements_to_s
144
177
  end
145
178
 
179
+ # Returns the opt code for the record base type.
146
180
  def opt_code
147
181
  'record'
148
182
  end
149
183
  end
150
184
 
185
+ # Represents a key base type node in the abstract syntax tree, a subclass of BaseType.
151
186
  class BaseTypeKey < NamedNode
187
+ # The title of the key base type node.
152
188
  def title
153
189
  :base_type_key
154
190
  end
155
191
 
192
+ # Converts the key base type node to a string representation.
156
193
  def to_s
157
194
  elements_to_s
158
195
  end
159
196
  end
160
197
 
198
+ # Represents a variant base type node in the abstract syntax tree, a subclass of BaseType.
161
199
  class BaseTypeVariant < NamedNode
200
+ # The title of the variant base type node.
162
201
  def title
163
202
  :base_type_variant
164
203
  end
165
204
 
205
+ # Converts the variant base type node to a string representation.
166
206
  def to_s
167
207
  elements_to_s
168
208
  end
169
209
 
210
+ # Returns the opt code for the variant base type.
170
211
  def opt_code
171
212
  'variant'
172
213
  end
173
214
  end
174
215
 
216
+ # Represents a function base type node in the abstract syntax tree, a subclass of BaseType.
175
217
  class BaseTypeFunc < NamedNode
218
+ # The title of the function base type node.
176
219
  def title
177
220
  :base_type_func
178
221
  end
179
222
 
223
+ # Converts the function base type node to a string representation.
180
224
  def to_s
181
225
  elements_to_s
182
226
  end
183
227
 
228
+ # Returns the opt code for the function base type.
184
229
  def opt_code
185
230
  'func'
186
231
  end
187
232
  end
188
233
 
234
+ # Represents an optional base type node in the abstract syntax tree, a subclass of BaseType.
189
235
  class BaseTypeOpt < NamedNode
236
+ # The title of the optional base type node.
190
237
  def title
191
238
  :base_type_opt
192
239
  end
193
240
 
241
+ # Converts the optional base type node to a string representation.
194
242
  def to_s
195
243
  elements_to_s
196
244
  end
197
245
 
246
+ # Returns the opt code for the optional base type.
198
247
  def opt_code
199
248
  'opt'
200
249
  end
201
250
  end
202
251
 
252
+ # Represents a vector base type node in the abstract syntax tree, a subclass of BaseType.
203
253
  class BaseTypeVec < NamedNode
254
+ # The title of the vector base type node.
204
255
  def title
205
256
  :base_type_vec
206
257
  end
207
258
 
259
+ # Converts the vector base type node to a string representation.
208
260
  def to_s
209
261
  elements_to_s
210
262
  end
211
263
 
264
+ # Returns the opt code for the vector base type.
212
265
  def opt_code
213
266
  'vec'
214
267
  end
215
268
  end
216
269
 
270
+ # Represents an other base type node in the abstract syntax tree, a subclass of BaseType.
217
271
  class BaseTypeOther < NamedNode
272
+ # The title of the other base type node.
218
273
  def title
219
274
  :base_type_other
220
275
  end
221
276
 
277
+ # Converts the other base type node to a string representation.
222
278
  def to_s
223
279
  elements_to_s
224
280
  end
225
281
 
282
+ # Returns the opt code for the other base type.
226
283
  def opt_code
227
284
  text_value
228
285
  end
229
286
  end
230
287
 
288
+ # Represents the content of a base type node in the abstract syntax tree, a subclass of NamedNode.
231
289
  class BaseTypeContent < NamedNode
290
+ # The title of the base type content node.
232
291
  def title
233
292
  :base_type_content
234
293
  end
235
294
 
295
+ # Converts the base type content node to a string representation.
236
296
  def to_s
237
297
  elements_to_s
238
298
  end
239
299
  end
240
300
 
301
+ # Represents a child of a base type node in the abstract syntax tree, a subclass of NamedNode.
241
302
  class BaseTypeChild < NamedNode
303
+ # The title of the base type child node.
242
304
  def title
243
305
  :base_type_child
244
306
  end
245
307
 
308
+ # Converts the base type child node to a string representation.
246
309
  def to_s
247
310
  elements_to_s
248
311
  end
249
312
  end
250
313
 
314
+ # Represents a type name node in the abstract syntax tree, a subclass of NamedNode.
251
315
  class TypeName < NamedNode
316
+ # The title of the type name node.
252
317
  def title
253
318
  :type_name
254
319
  end
255
320
 
321
+ # Converts the type name node to a string representation.
256
322
  def to_s
257
323
  elements_to_s
258
324
  end
259
325
  end
260
326
 
327
+ # Represents an IC service node in the abstract syntax tree, a subclass of NamedNode.
261
328
  class Service < NamedNode
329
+ # The title of the IC service node.
262
330
  def title
263
331
  :ic_service
264
332
  end
265
333
 
334
+ # Converts the IC service node to a string representation.
266
335
  def to_s
267
336
  elements_to_s
268
337
  end
269
338
  end
270
339
 
340
+ # Represents an IC service name node in the abstract syntax tree, a subclass of NamedNode.
271
341
  class IcServiceName < NamedNode
342
+ # The title of the IC service name node.
272
343
  def title
273
344
  :ic_service_name
274
345
  end
275
346
 
347
+ # Converts the IC service name node to a string representation with '#' prefix.
276
348
  def to_s
277
349
  "# #{elements[0].to_s}"
278
350
  end
279
351
  end
280
352
 
353
+ # Represents IC service methods node in the abstract syntax tree, a subclass of NamedNode.
281
354
  class IcServiceMethods < NamedNode
355
+ # The title of the IC service methods node.
282
356
  def title
283
357
  :ic_service_methods
284
358
  end
285
359
 
360
+ # Returns an array of IC service method nodes.
286
361
  def value
287
362
  elements.map { |update| update.value }
288
363
  end
289
364
  end
290
365
 
366
+ # Represents an IC service method name node in the abstract syntax tree, a subclass of NamedNode.
291
367
  class IcServiceMethodName < NamedNode
368
+ # The title of the IC service method name node.
292
369
  def title
293
370
  :ic_service_method_name
294
371
  end
295
372
  end
296
373
 
374
+ # Represents an IC service item node in the abstract syntax tree, a subclass of NamedNode.
297
375
  class IcServiceItem < NamedNode
376
+ # The title of the IC service item node.
298
377
  def title
299
378
  :ic_service_item
300
379
  end
301
380
 
381
+ # Converts the IC service item node to a string representation.
302
382
  def to_s
303
383
  elements_to_s
304
384
  end
305
385
 
386
+ # Converts the IC service item node to a hash representation.
306
387
  def to_obj
307
388
  obj = {}
308
389
  elements.each do |element|
@@ -312,61 +393,79 @@ module IcAgent
312
393
  end
313
394
  end
314
395
 
396
+ # Represents an IC service param node in the abstract syntax tree, a subclass of NamedNode.
315
397
  class IcServiceParam < NamedNode
398
+ # The title of the IC service param node.
316
399
  def title
317
400
  :ic_service_param
318
401
  end
319
402
 
403
+ # Converts the IC service param node to a string representation with '#' prefix.
320
404
  def to_s
321
405
  "# #{elements[0].to_s}"
322
406
  end
323
407
  end
324
408
 
409
+ # Represents an IC service name node in the abstract syntax tree, a subclass of NamedNode.
325
410
  class IcServiceName < NamedNode
411
+ # The title of the IC service name node.
326
412
  def title
327
413
  :ic_service_name
328
414
  end
329
415
 
416
+ # Converts the IC service name node to a string representation with '#' prefix.
330
417
  def to_s
331
418
  "# #{elements[0].to_s}"
332
419
  end
333
420
  end
334
421
 
422
+ # Represents an IC service method params node in the abstract syntax tree, a subclass of NamedNode.
335
423
  class IcServiceMethodParams < NamedNode
424
+ # The title of the IC service method params node.
336
425
  def title
337
426
  :ic_service_method_params
338
427
  end
339
428
 
429
+ # Converts the IC service method params node to a string representation.
340
430
  def to_s
341
431
  elements_to_s
342
432
  end
343
433
  end
344
434
 
435
+ # Represents an IC service method return node in the abstract syntax tree, a subclass of NamedNode.
345
436
  class IcServiceMethodReturn < NamedNode
437
+ # The title of the IC service method return node.
346
438
  def title
347
439
  :ic_service_method_return
348
440
  end
349
441
 
442
+ # Converts the IC service method return node to a string representation.
350
443
  def to_s
351
444
  elements_to_s
352
445
  end
353
446
  end
354
447
 
448
+ # Represents an IC service method query node in the abstract syntax tree, a subclass of NamedNode.
355
449
  class IcServiceMethodQuery < NamedNode
450
+ # The title of the IC service method query node.
356
451
  def title
357
452
  :ic_service_method_query
358
453
  end
359
454
 
455
+ # Converts the IC service method query node to a string representation.
360
456
  def to_s
361
457
  elements_to_s
362
458
  end
363
459
  end
364
460
 
461
+ # Represents the content of a base type node in the abstract syntax tree, a subclass of NamedNode.
365
462
  class BaseTypeContent < NamedNode
463
+ # The title of the IC service method query node.
366
464
  def title
367
465
  :ic_service_method_query
368
466
  end
369
467
 
468
+ # Converts the IC service method query node to a string representation.
370
469
  def to_s
371
470
  elements_to_s
372
471
  end
@@ -374,4 +473,3 @@ module IcAgent
374
473
  end
375
474
  end
376
475
  end
377
-
@@ -3,217 +3,280 @@ require 'treetop'
3
3
  module IcAgent
4
4
  module Ast
5
5
  module Nodes
6
+ # Represents a statement node in the abstract syntax tree with additional attributes.
6
7
  class StatementNode < Treetop::Runtime::SyntaxNode
8
+ # Additional attributes to store child count and depth.
7
9
  attr_accessor :child_count, :depth
8
10
 
11
+ # The title of the statement node, by default :named_node.
9
12
  def title
10
13
  :named_node
11
14
  end
12
15
 
16
+ # Converts the statement node and its children to an array representation.
13
17
  def to_array
14
18
  [title] + elements.map(&:to_array)
15
19
  end
16
20
 
21
+ # Converts the statement node and its children to a string representation.
17
22
  def to_s
18
23
  "#{title.to_s.upcase} #{elements_to_s}"
19
24
  end
20
25
 
26
+ # Converts the children of the statement node to a string.
21
27
  def elements_to_s
22
28
  elements.map(&:to_s).join("\n")
23
29
  end
24
30
 
31
+ # Adds a child to the statement node.
25
32
  def add_child
26
33
  @child_count ||= 0 + 1
27
34
  end
28
35
 
36
+ # Returns the source content of the statement node by removing leading and trailing whitespaces.
29
37
  def source_content
30
38
  self.text_value.strip
31
39
  end
32
40
  end
33
41
 
42
+ # Represents an IC base type node in the abstract syntax tree, a subclass of StatementNode.
34
43
  class IcBaseType < StatementNode
44
+ # The title of the IC base type node.
35
45
  def title
36
46
  :base_type
37
47
  end
38
48
 
49
+ # Converts the IC base type node to a string representation.
39
50
  def to_s
40
51
  elements_to_s
41
52
  end
42
53
  end
43
54
 
55
+ # Represents an IC base type single node in the abstract syntax tree, a subclass of StatementNode.
44
56
  class IcBaseTypeSingle < StatementNode
57
+ # The title of the IC base type single node.
45
58
  def title
46
59
  :base_type_single
47
60
  end
48
61
 
62
+ # Converts the IC base type single node to a string representation.
49
63
  def to_s
50
64
  elements_to_s
51
65
  end
52
66
 
67
+ # Returns the opt code for the IC base type single node, which is 'single'.
53
68
  def opt_code
54
69
  'single'
55
70
  end
56
71
  end
57
72
 
73
+ # Represents an IC base type record node in the abstract syntax tree, a subclass of StatementNode.
58
74
  class IcBaseTypeRecord < StatementNode
75
+ # The title of the IC base type record node.
59
76
  def title
60
77
  :base_type_record
61
78
  end
62
79
 
80
+ # Converts the IC base type record node to a string representation.
63
81
  def to_s
64
82
  elements_to_s
65
83
  end
66
84
 
85
+ # Returns the opt code for the IC base type record node, which is 'record'.
67
86
  def opt_code
68
87
  'record'
69
88
  end
70
89
  end
71
90
 
91
+ # Represents an IC base type key node in the abstract syntax tree, a subclass of StatementNode.
72
92
  class IcBaseTypeKey < StatementNode
93
+ # The title of the IC base type key node.
73
94
  def title
74
95
  :base_type_key
75
96
  end
76
97
 
98
+ # Converts the IC base type key node to a string representation.
77
99
  def to_s
78
100
  elements_to_s
79
101
  end
80
102
  end
81
103
 
104
+ # Represents an IC base type value node in the abstract syntax tree, a subclass of StatementNode.
82
105
  class IcBaseTypeValue < StatementNode
106
+ # The title of the IC base type value node.
83
107
  def title
84
108
  :base_type_value
85
109
  end
86
110
 
111
+ # Converts the IC base type value node to a string representation.
87
112
  def to_s
88
113
  elements_to_s
89
114
  end
90
115
  end
91
116
 
117
+ # Represents an IC type definition node in the abstract syntax tree, a subclass of StatementNode.
92
118
  class IcTypeDef < StatementNode
119
+ # The title of the IC type definition node.
93
120
  def title
94
121
  :base_type_def
95
122
  end
96
123
 
124
+ # Converts the IC type definition node to a string representation.
97
125
  def to_s
98
126
  elements_to_s
99
127
  end
100
128
  end
101
129
 
130
+ # Represents an IC base type variant node in the abstract syntax tree, a subclass of StatementNode.
102
131
  class IcBaseTypeVariant < StatementNode
132
+ # The title of the IC base type variant node.
103
133
  def title
104
134
  :base_type_variant
105
135
  end
106
136
 
137
+ # Converts the IC base type variant node to a string representation.
107
138
  def to_s
108
139
  elements_to_s
109
140
  end
110
141
 
142
+ # Returns the opt code for the IC base type variant node, which is 'variant'.
111
143
  def opt_code
112
144
  'variant'
113
145
  end
114
146
  end
115
147
 
148
+ # Represents an IC base type function node in the abstract syntax tree, a subclass of StatementNode.
116
149
  class IcBaseTypeFunc < StatementNode
150
+ # The title of the IC base type function node.
117
151
  def title
118
152
  :base_type_func
119
153
  end
120
154
 
155
+ # Converts the IC base type function node to a string representation.
121
156
  def to_s
122
157
  elements_to_s
123
158
  end
124
159
 
160
+ # Returns the opt code for the IC base type function node, which is 'func'.
125
161
  def opt_code
126
162
  'func'
127
163
  end
128
164
  end
129
165
 
166
+ # Represents an IC base type optional node in the abstract syntax tree, a subclass of StatementNode.
130
167
  class IcBaseTypeOpt < StatementNode
168
+ # The title of the IC base type optional node.
131
169
  def title
132
170
  :base_type_opt
133
171
  end
134
172
 
173
+ # Converts the IC base type optional node to a string representation.
135
174
  def to_s
136
175
  elements_to_s
137
176
  end
138
177
 
178
+ # Returns the opt code for the IC base type optional node, which is 'opt'.
139
179
  def opt_code
140
180
  'opt'
141
181
  end
142
182
  end
143
183
 
184
+ # Represents an IC base type vector node in the abstract syntax tree, a subclass of StatementNode.
144
185
  class IcBaseTypeVec < StatementNode
186
+ # The title of the IC base type vector node.
145
187
  def title
146
188
  :base_type_vec
147
189
  end
148
190
 
191
+ # Converts the IC base type vector node to a string representation.
149
192
  def to_s
150
193
  elements_to_s
151
194
  end
152
195
 
196
+ # Returns the opt code for the IC base type vector node, which is 'vec'.
153
197
  def opt_code
154
198
  'vec'
155
199
  end
156
200
  end
157
201
 
202
+ # Represents an IC base type other node in the abstract syntax tree, a subclass of StatementNode.
158
203
  class IcBaseTypeOther < StatementNode
204
+ # The title of the IC base type other node.
159
205
  def title
160
206
  :base_type_other
161
207
  end
162
208
 
209
+ # Converts the IC base type other node to a string representation.
163
210
  def to_s
164
211
  elements_to_s
165
212
  end
166
213
 
214
+ # Returns the opt code for the IC base type other node.
167
215
  def opt_code
168
216
  text_value
169
217
  end
170
218
  end
171
219
 
220
+ # Represents the content of an IC base type node in the abstract syntax tree, a subclass of StatementNode.
172
221
  class IcBaseTypeContent < StatementNode
222
+ # The title of the IC base type content node.
173
223
  def title
174
224
  :base_type_content
175
225
  end
176
226
 
227
+ # Converts the IC base type content node to a string representation.
177
228
  def to_s
178
229
  elements_to_s
179
230
  end
180
231
  end
181
232
 
233
+ # Represents a child of an IC base type node in the abstract syntax tree, a subclass of StatementNode.
182
234
  class IcBaseTypeChild < StatementNode
235
+ # The title of the IC base type child node.
183
236
  def title
184
237
  :base_type_child
185
238
  end
186
239
 
240
+ # Converts the IC base type child node to a string representation.
187
241
  def to_s
188
242
  elements_to_s
189
243
  end
190
244
  end
191
245
 
246
+ # Represents an IC type name node in the abstract syntax tree, a subclass of StatementNode.
192
247
  class IcTypeName < StatementNode
248
+ # The title of the IC type name node.
193
249
  def title
194
250
  :type_name
195
251
  end
196
252
 
253
+ # Converts the IC type name node to a string representation.
197
254
  def to_s
198
255
  elements_to_s
199
256
  end
200
257
  end
201
258
 
259
+ # Represents a statement block node in the abstract syntax tree, a subclass of StatementNode.
202
260
  class StatementBlock < StatementNode
261
+ # The title of the statement block node.
203
262
  def title
204
263
  :statement_block
205
264
  end
206
265
 
266
+ # Converts the statement block node to a string representation.
207
267
  def to_s
208
268
  elements_to_s
209
269
  end
210
270
  end
211
271
 
272
+ # Represents the content of a statement node in the abstract syntax tree, a subclass of StatementNode.
212
273
  class StatementContent < StatementNode
274
+ # The title of the statement content node.
213
275
  def title
214
276
  :statement_content
215
277
  end
216
278
 
279
+ # Converts the statement content node to a string representation.
217
280
  def to_s
218
281
  elements_to_s
219
282
  end
@@ -221,4 +284,3 @@ module IcAgent
221
284
  end
222
285
  end
223
286
  end
224
-
@@ -3,11 +3,16 @@ require 'treetop'
3
3
  module IcAgent
4
4
  module Ast
5
5
  module Nodes
6
+ # Represents a string literal node in the abstract syntax tree.
6
7
  class StringLiteral < Treetop::Runtime::SyntaxNode
8
+ # Converts the string literal node to an array representation.
9
+ # In this case, the array contains the text value of the string literal.
7
10
  def to_array
8
11
  self.text_value
9
12
  end
10
13
 
14
+ # Converts the string literal node to a string representation.
15
+ # In this case, it returns the text value of the string literal.
11
16
  def to_s
12
17
  self.text_value
13
18
  end
@@ -2,26 +2,41 @@ require 'treetop'
2
2
 
3
3
  module IcAgent
4
4
  module Ast
5
+ # The Parser class provides methods to parse data using a Treetop grammar and work with the resulting Abstract Syntax Tree (AST).
5
6
  class Parser
6
7
  attr_accessor :parser, :tree
7
8
 
9
+ # Initializes the Parser by loading the Treetop grammar file.
8
10
  def initialize
9
11
  Treetop.load(File.expand_path(File.join(File.dirname(__FILE__), 'did_grammar.treetop')))
10
12
  @parser = DIDGrammarParser.new
11
13
  end
12
14
 
15
+ # Parses the given data using the Treetop parser and returns the AST.
16
+ #
17
+ # Parameters:
18
+ # - data: The data to be parsed.
19
+ # - return_type: The desired return type for the parse result (:string by default).
20
+ #
21
+ # Returns:
22
+ # - The root node of the Abstract Syntax Tree (AST).
23
+ #
24
+ # Raises:
25
+ # - Exception if there is a parse error.
13
26
  def parse(data, return_type = :string)
14
27
  tree = @parser.parse(data)
15
28
 
16
29
  raise Exception, "Parse error at offset: #{@parser.index} #{@parser.failure_reason}" if tree.nil?
17
30
 
18
- # this edits the tree in place
31
+ # This method edits the tree in place to remove unnecessary syntax nodes.
19
32
  clean_tree(tree)
20
33
 
21
34
  @tree = tree
22
35
  tree
23
36
  end
24
37
 
38
+ # Recursively cleans the syntax tree by removing nodes of class 'Treetop::Runtime::SyntaxNode'.
39
+ # This method is used to remove unnecessary nodes generated during parsing.
25
40
  def clean_tree(root_node)
26
41
  return if root_node.elements.nil?
27
42
 
@@ -29,6 +44,10 @@ module IcAgent
29
44
  root_node.elements.each { |node| self.clean_tree(node) }
30
45
  end
31
46
 
47
+ # Retrieves the root node of the IC service from the AST.
48
+ #
49
+ # Returns:
50
+ # - The root node representing the IC service, or nil if not found.
32
51
  def ic_service
33
52
  tree.elements.each do |ele|
34
53
  return ele if ele.title == :ic_service
@@ -36,6 +55,7 @@ module IcAgent
36
55
  nil
37
56
  end
38
57
 
58
+ # Retrieves an array of AST nodes representing IC type declarations.
39
59
  def ic_types
40
60
  type_arr = []
41
61
  tree.elements.each do |ele|
@@ -44,6 +64,7 @@ module IcAgent
44
64
  type_arr
45
65
  end
46
66
 
67
+ # Converts the IC type declarations to an array of corresponding objects.
47
68
  def ic_types_obj
48
69
  obj_arr = []
49
70
  ic_types.each do |ic_type|
@@ -52,6 +73,10 @@ module IcAgent
52
73
  obj_arr
53
74
  end
54
75
 
76
+ # Retrieves the root node of the IC service methods from the AST.
77
+ #
78
+ # Returns:
79
+ # - The root node representing the IC service methods, or nil if not found.
55
80
  def ic_service_methods
56
81
  ic_service_tree = ic_service
57
82
  unless ic_service_tree.empty?
@@ -62,10 +87,12 @@ module IcAgent
62
87
  nil
63
88
  end
64
89
 
90
+ # Retrieves the name of an IC type from its AST node.
65
91
  def ic_type_name(ic_type)
66
92
  ic_type.type_param_name
67
93
  end
68
94
 
95
+ # Retrieves an array of names of all IC types from the AST.
69
96
  def ic_type_names
70
97
  names_arr = []
71
98
  ic_types.each do |ic_type|
@@ -74,6 +101,13 @@ module IcAgent
74
101
  names_arr
75
102
  end
76
103
 
104
+ # Retrieves the AST node of an IC type by its name.
105
+ #
106
+ # Parameters:
107
+ # - type_name: The name of the IC type to retrieve.
108
+ #
109
+ # Returns:
110
+ # - The AST node representing the IC type, or nil if not found.
77
111
  def ic_type_by_name(type_name)
78
112
  ic_types.each do |ic_type|
79
113
  return ic_type if type_name == ic_type_name(ic_type)
@@ -2,6 +2,8 @@ require 'treetop'
2
2
 
3
3
  module IcAgent
4
4
  module Ast
5
+ # The StatementParser class provides methods to parse data and generate an Abstract Syntax Tree (AST)
6
+ # for nested types based on the specified grammar.
5
7
  class StatementParser
6
8
  attr_accessor :parser, :tree, :source_tree
7
9
 
@@ -21,10 +23,19 @@ module IcAgent
21
23
  REFER_TYPE_KEYS = ['record', 'variant']
22
24
 
23
25
  def initialize
26
+ # Loads the Treetop grammar from the specified file.
24
27
  Treetop.load(File.expand_path(File.join(File.dirname(__FILE__), 'nested_type_grammar.treetop')))
25
28
  @parser = TypeGrammarParser.new
26
29
  end
27
30
 
31
+ # Parses the input data and generates the Abstract Syntax Tree (AST) for nested types based on the grammar.
32
+ #
33
+ # Parameters:
34
+ # - data: The input data to be parsed.
35
+ # - return_type: The desired format for the parsed AST (:string by default).
36
+ #
37
+ # Returns:
38
+ # - The generated AST in the specified format.
28
39
  def parse(data, return_type = :string)
29
40
  tree = @parser.parse(data)
30
41
  raise Exception, "Parse error at offset: #{@parser.index} #{@parser.failure_reason}" if tree.nil?
@@ -38,6 +49,7 @@ module IcAgent
38
49
  tree
39
50
  end
40
51
 
52
+ # Cleans up the AST tree by removing unnecessary nodes.
41
53
  def clean_tree(root_node)
42
54
  return if root_node.elements.nil?
43
55
 
@@ -45,6 +57,7 @@ module IcAgent
45
57
  root_node.elements.each { |node| self.clean_tree(node) }
46
58
  end
47
59
 
60
+ # Generates the source tree from the AST tree.
48
61
  # @param [Object] root_node
49
62
  # @param [nil] tree_root_node
50
63
  # @param [nil] tree_current_node
@@ -87,10 +100,12 @@ module IcAgent
87
100
  self.source_tree = tree_root_node
88
101
  end
89
102
 
103
+ # Returns the root node of the AST statement.
90
104
  def ic_statement_root
91
105
  tree.elements[0]
92
106
  end
93
107
 
108
+ # Returns the child nodes of the AST statement.
94
109
  def ic_statement_childs
95
110
  if tree.elements[0] && tree.elements[0].elements[0].elements[0]
96
111
  tree.elements[0].elements[0].elements[0].elements
@@ -1,10 +1,25 @@
1
1
  module IcAgent
2
2
  module Ast
3
+ # The Writer class provides methods to represent an Abstract Syntax Tree (AST) in different formats.
3
4
  class Writer
5
+ # Initializes the Writer with an Abstract Syntax Tree (AST).
6
+ #
7
+ # Parameters:
8
+ # - tree: The Abstract Syntax Tree (AST) to be represented.
4
9
  def initialize(tree)
5
10
  @tree = tree
6
11
  end
7
12
 
13
+ # Writes the AST in the desired format.
14
+ #
15
+ # Parameters:
16
+ # - return_type: The desired format to represent the AST (:string by default).
17
+ #
18
+ # Returns:
19
+ # - The AST in the specified format:
20
+ # - :tree: Returns the original AST.
21
+ # - :array: Returns the AST as an array.
22
+ # - :string: Returns the AST as a string.
8
23
  def write(return_type = :string)
9
24
  if return_type == :tree
10
25
  @tree
@@ -2,6 +2,12 @@ require 'rubytree'
2
2
 
3
3
  module IcAgent
4
4
  class Canister
5
+ # Constructor for the Canister class.
6
+ #
7
+ # Parameters:
8
+ # - agent: An instance of the agent.
9
+ # - canister_id: ID of the canister.
10
+ # - candid: (Optional) The Candid description of the canister. If not provided, it will be queried.
5
11
  def initialize(agent, canister_id, candid=nil)
6
12
  @agent = agent
7
13
  @canister_id = canister_id
@@ -17,6 +23,7 @@ module IcAgent
17
23
  raise BaseException, "canister #{@canister_id} has no __get_candid_interface_tmp_hack method."
18
24
  end
19
25
 
26
+ # Parse the Candid description to extract information about service methods.
20
27
  parser = IcAgent::Ast::Parser.new
21
28
  parser.parse(@candid)
22
29
 
@@ -261,6 +261,11 @@ module IcAgent
261
261
 
262
262
  attr_accessor :identity, :client, :agent, :canister
263
263
 
264
+ # Constructor for the CyclesWallet class.
265
+ #
266
+ # Parameters:
267
+ # - iden: (Optional) An instance of the Identity class.
268
+ # - wallet_id: The ID of the CyclesWallet.
264
269
  def initialize(iden = nil, wallet_id)
265
270
  @identity = iden.nil? ? IcAgent::Identity.new : iden
266
271
  @client = IcAgent::Client.new
@@ -355,6 +355,10 @@ module IcAgent
355
355
 
356
356
  attr_accessor :identity, :client, :agent, :canister
357
357
 
358
+ # Constructor for the Governance class.
359
+ #
360
+ # Parameters:
361
+ # - iden: (Optional) An instance of the Identity class.
358
362
  def initialize(iden = nil)
359
363
  @identity = iden.nil? ? IcAgent::Identity.new : iden
360
364
  @client = IcAgent::Client.new
@@ -166,6 +166,10 @@ module IcAgent
166
166
 
167
167
  attr_accessor :identity, :client, :agent, :canister
168
168
 
169
+ # Constructor for the Ledger class.
170
+ #
171
+ # Parameters:
172
+ # - iden: (Optional) An instance of the Identity class.
169
173
  def initialize(iden = nil)
170
174
  @identity = iden.nil? ? IcAgent::Identity.new : iden
171
175
  @client = IcAgent::Client.new
@@ -182,6 +182,10 @@ module IcAgent
182
182
 
183
183
  attr_accessor :identity, :client, :agent, :canister
184
184
 
185
+ # Constructor for the Management class.
186
+ #
187
+ # Parameters:
188
+ # - iden: (Optional) An instance of the Identity class.
185
189
  def initialize(iden = nil)
186
190
  @identity = iden.nil? ? IcAgent::Identity.new : iden
187
191
  @client = IcAgent::Client.new
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IcAgent
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ic_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Terry.Tu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-21 00:00:00.000000000 Z
11
+ date: 2023-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base32