ic_agent 0.1.1 → 0.1.2

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: 1f7c8e2c2b92f692a20ba38898f78dd76a854367369a905e8553c162c3a0238c
4
- data.tar.gz: 51885b2b8966bae6a23b4dc5e775c7fd3ee864b4452cafa2213e54aa1fdddeae
3
+ metadata.gz: afda8300e3e7767519eb7322d27268d5f486b0201131ef5de7ae786ae9385239
4
+ data.tar.gz: fc2baeee2c47d76345dfc64d668dd5bd6d68f81a4113aae7b07865f85ba8062d
5
5
  SHA512:
6
- metadata.gz: 3369b673094b4b7416f1a5125f440d5b5ad4fd9b8878d58564165b50bbd33b503e0653220fb92ac617fdc25890cb20efe2429c3f7f64340f8305134162f58a95
7
- data.tar.gz: 0e8e47383437dfd4a1de1d05ac8975faed0fb1cff4e56ea9c2006450fd7036dbff578cb4bbbbea2f635e4a41dbf05e0f25ce178a8978e5f6bdd2e79a7d644d22
6
+ metadata.gz: c1fa506bb6e618bfe3f0995b1127c14030a4000fb4b07ee40e0af976d968b5d441c4564ee92bd2c877c9b47430f772a671c245467dc15959ff986ec94fb30723
7
+ data.tar.gz: e8cf412aeac35f7b37c8fb2a396e0a6d0aa98424e32bc9aadf7f3d84e18a691c8c5cb41f898b458612012bcb6413d82c573e260606b63c5d5b79e7c98b0654ec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ic_agent (0.1.1)
4
+ ic_agent (0.1.2)
5
5
  base32 (~> 0.3.4)
6
6
  bitcoin-ruby (~> 0.0.20)
7
7
  cbor (~> 0.5.9.6)
data/README.md CHANGED
@@ -14,11 +14,10 @@ gem install ic_agent
14
14
  ### Features
15
15
 
16
16
  1. candid types encode & decode
17
- 2. support secp256k1 & ed25519 identity
17
+ 2. support secp256k1 & ed25519 identity, pem file import
18
18
  3. canister DID file parsing
19
19
  4. canister class, initialized with canister id and DID file
20
20
  5. common canister interfaces: ledger, management, nns, cycles wallet
21
- 6. BLS Verify
22
21
 
23
22
  ### Modules & Usage
24
23
 
@@ -156,14 +156,16 @@ module IcAgent
156
156
  pure_child_code, replaced_hash = replace_multi_type(type_str)
157
157
  key_index = 0
158
158
  value_arr = []
159
- pure_child_code.split(';').each do |item|
160
- _, item_value = get_record_key_value(item, ' : ', key_index)
159
+ pure_child_arr = pure_child_code.strip.split(';').map(&:strip).collect { |item| item.gsub('{', '').gsub('}', '') }.reject(&:empty?)
160
+ pure_child_arr.each do |item|
161
+ _, item_value = get_record_key_value(item, ':', key_index)
161
162
  value_arr << item_value.split(' ')
162
163
  key_index += 1
163
164
  end
164
165
 
165
166
  replaced_hash.each_key do |key|
166
- item_arr = replaced_hash[key].strip.split(';')
167
+ item_type = key.index('record') ? 'record' : 'variant'
168
+ item_arr = replaced_hash[key].sub('record', '').sub('variant', '').strip.split(';').map(&:strip).collect { |item| item.gsub('{', '').gsub('}', '') }.reject(&:empty?)
167
169
  item_arr.each do |item|
168
170
  multi_item_arr = item.strip.split(':')
169
171
  if multi_item_arr.size > 1
@@ -173,7 +175,7 @@ module IcAgent
173
175
  item_value_arr.delete('{}')
174
176
  value_arr << item_value_arr
175
177
  else
176
- value_arr << multi_item_arr[1].strip
178
+ value_arr << multi_item_arr[0].strip if item_type == 'record'
177
179
  end
178
180
  end
179
181
  value_arr.delete(key)
@@ -6,7 +6,7 @@ grammar DIDGrammar
6
6
 
7
7
  rule base_type
8
8
  'bool' / 'text' / 'null' / 'reserved' / 'empty' / 'principal' / 'nat8' / 'nat16' / 'nat32' / 'nat64' / 'int8' / 'int16' / 'int32' / 'int64' / 'float32' / 'float64' /
9
- 'opt' / 'vec' / 'record' / 'variant' / 'service' / 'func' / 'nat' / 'int' / blob
9
+ 'opt' / 'vec' / 'record' / 'variant' / 'service' / 'func' / 'nat' / 'int' / 'blob'
10
10
  end
11
11
 
12
12
  rule base_type_single
@@ -94,11 +94,15 @@ grammar DIDGrammar
94
94
  end
95
95
 
96
96
  rule service_method_params
97
- param_element (", " param_element)* <IcAgent::Ast::Nodes::IcServiceMethodParams>
97
+ source_coding (", " source_coding)* <IcAgent::Ast::Nodes::IcServiceMethodParams>
98
+ end
99
+
100
+ rule source_coding
101
+ [a-zA-Z0-9_{}:;\n ]*
98
102
  end
99
103
 
100
104
  rule service_method_return_type
101
- [a-zA-Z0-9_{}: ]* <IcAgent::Ast::Nodes::IcServiceMethodReturn>
105
+ [a-zA-Z0-9_{}:;\n ]* <IcAgent::Ast::Nodes::IcServiceMethodReturn>
102
106
  end
103
107
 
104
108
  rule method_query
@@ -0,0 +1,159 @@
1
+ grammar DIDGrammar
2
+
3
+ rule body
4
+ ( type_declaration / service_declaration / comment / end_of_line )* <IcAgent::Ast::Nodes::DIDFile>
5
+ end
6
+
7
+ rule base_type
8
+ 'bool' / 'text' / 'null' / 'reserved' / 'empty' / 'principal' / 'nat8' / 'nat16' / 'nat32' / 'nat64' / 'int8' / 'int16' / 'int32' / 'int64' / 'float32' / 'float64' /
9
+ 'opt' / 'vec' / 'record' / 'variant' / 'service' / 'func' / 'nat' / 'int' / blob
10
+ end
11
+
12
+ rule base_type_single
13
+ 'bool' / 'text' / 'null' / 'reserved' / 'empty' / 'principal' / 'nat8' / 'nat16' / 'nat32' / 'nat64' / 'int8' / 'int16' / 'int32' / 'int64' / 'float32' / 'float64' / 'nat' / 'int' / 'blob'
14
+ end
15
+
16
+ rule base_type_base
17
+ base_type_single+ <IcAgent::Ast::Nodes::BaseTypeSingle>
18
+ end
19
+
20
+ rule base_type_code
21
+ [a-zA-Z0-9_]+ <IcAgent::Ast::Nodes::BaseTypeOther>
22
+ end
23
+
24
+ rule base_type_key
25
+ [a-zA-Z0-9_]+ <IcAgent::Ast::Nodes::BaseTypeKey>
26
+ end
27
+
28
+ rule base_type_vec
29
+ 'vec' space ic_all_type <IcAgent::Ast::Nodes::BaseTypeVec>
30
+ end
31
+
32
+ rule base_type_opt
33
+ 'opt' space ic_all_type <IcAgent::Ast::Nodes::BaseTypeOpt>
34
+ end
35
+
36
+ rule base_type_record
37
+ 'record' space '{' space_and_line ic_base_content space_and_line '}' <IcAgent::Ast::Nodes::BaseTypeRecord>
38
+ end
39
+
40
+ rule base_type_record_null
41
+ 'record {}' <IcAgent::Ast::Nodes::BaseTypeRecord>
42
+ end
43
+
44
+ rule base_type_variant
45
+ 'variant' space '{' space_and_line ic_base_content space_and_line '}' <IcAgent::Ast::Nodes::BaseTypeVariant>
46
+ end
47
+
48
+ rule base_type_func
49
+ 'func' space? "(" service_method_params? ")" space? "->" space? "(" service_method_return_type ")" space? method_query? <IcAgent::Ast::Nodes::BaseTypeFunc>
50
+ end
51
+
52
+ rule ic_all_type
53
+ base_type_base / base_type_vec / base_type_record / base_type_record_null / base_type_variant / base_type_opt / base_type_func / base_type_code
54
+ end
55
+
56
+ rule ic_base_content
57
+ (ic_base_type)+ <IcAgent::Ast::Nodes::BaseTypeContent>
58
+ end
59
+
60
+ rule ic_base_type
61
+ space? base_type_key space? (':' space ic_all_type)* optional_semicolon <IcAgent::Ast::Nodes::BaseTypeChild>
62
+ end
63
+
64
+ rule type_declaration
65
+ 'type' space_and_line type_name space_and_line '=' space_and_line ic_all_type ';' end_of_line <IcAgent::Ast::Nodes::TypeDeclaration>
66
+ end
67
+
68
+ rule type_name
69
+ [a-zA-Z0-9_]+ <IcAgent::Ast::Nodes::TypeName>
70
+ end
71
+
72
+ rule comment
73
+ space? '//' space? rest_of_line <IcAgent::Ast::Nodes::Comment>
74
+ end
75
+
76
+ rule service_declaration
77
+ "service" space ":" space service_name* "{" end_of_line service_methods "}" <IcAgent::Ast::Nodes::Service>
78
+ end
79
+
80
+ rule service_name
81
+ "(" ic_service_name ")" space? "->" space?
82
+ end
83
+
84
+ rule ic_service_name
85
+ [a-zA-Z0-9_]* <IcAgent::Ast::Nodes::IcServiceName>
86
+ end
87
+
88
+ rule service_methods
89
+ (service_item)+ <IcAgent::Ast::Nodes::IcServiceMethods>
90
+ end
91
+
92
+ rule service_item
93
+ space? service_method_name space? ":" space? "(" service_method_params? ")" space? "->" space? "(" service_method_return_type ")" space? method_query? ';' end_of_line? <IcAgent::Ast::Nodes::IcServiceItem>
94
+ end
95
+
96
+ rule service_method_params
97
+ param_element (", " param_element)* <IcAgent::Ast::Nodes::IcServiceMethodParams>
98
+ end
99
+
100
+ rule service_method_return_type
101
+ [a-zA-Z0-9_{}: ]* <IcAgent::Ast::Nodes::IcServiceMethodReturn>
102
+ end
103
+
104
+ rule method_query
105
+ [a-zA-Z0-9_]* <IcAgent::Ast::Nodes::IcServiceMethodQuery>
106
+ end
107
+
108
+ rule words
109
+ [a-zA-Z_] [a-zA-Z0-9_]*
110
+ end
111
+
112
+ rule param_element
113
+ [a-zA-Z0-9_ ]+
114
+ end
115
+
116
+ rule service_method_name
117
+ [a-zA-Z0-9_]* <IcAgent::Ast::Nodes::IcServiceMethodName>
118
+ end
119
+
120
+ rule string_without_spaces
121
+ [\S]* <IcAgent::Ast::Nodes::StringLiteral>
122
+ end
123
+
124
+ rule rest_of_line
125
+ [^\n]* <IcAgent::Ast::Nodes::StringLiteral>
126
+ end
127
+
128
+ rule end_of_line
129
+ [\n]+
130
+ end
131
+
132
+ rule block_record
133
+ '{}'
134
+ end
135
+
136
+ rule has_end_of_line
137
+ [\n]*
138
+ end
139
+
140
+ rule splite_code
141
+ ' : '
142
+ end
143
+
144
+ rule space_and_line
145
+ [\s\n]*
146
+ end
147
+
148
+ rule space
149
+ [\s]+
150
+ end
151
+
152
+ rule start_space
153
+ [^\s]+
154
+ end
155
+
156
+ rule optional_semicolon
157
+ [;]*
158
+ end
159
+ end
@@ -0,0 +1,106 @@
1
+ grammar TypeGrammar
2
+ rule statement_block
3
+ ic_all_type optional_semicolon <IcAgent::Ast::Nodes::StatementBlock>
4
+ end
5
+
6
+ rule ic_all_type
7
+ (ic_type_def)+ <IcAgent::Ast::Nodes::IcTypeDef>
8
+ end
9
+
10
+ rule ic_type_def
11
+ base_type_base / base_type_vec / base_type_record / base_type_record_null / base_type_variant / base_type_opt / base_type_func / base_type_code
12
+ end
13
+
14
+ rule statement_content
15
+ (ic_base_type)+ <IcAgent::Ast::Nodes::StatementContent>
16
+ end
17
+
18
+ rule base_type
19
+ 'bool' / 'text' / 'null' / 'reserved' / 'empty' / 'principal' / 'nat8' / 'nat16' / 'nat32' / 'nat64' / 'int8' / 'int16' / 'int32' / 'int64' / 'float32' / 'float64' /
20
+ 'opt' / 'vec' / 'record' / 'variant' / 'service' / 'func' / 'nat' / 'int' / 'blob'
21
+ end
22
+
23
+ rule base_type_single
24
+ 'bool' / 'text' / 'null' / 'reserved' / 'empty' / 'principal' / 'nat8' / 'nat16' / 'nat32' / 'nat64' / 'int8' / 'int16' / 'int32' / 'int64' / 'float32' / 'float64' / 'nat' / 'int' / 'blob'
25
+ end
26
+
27
+ rule ic_base_type
28
+ space? base_type_key space? base_type_value? optional_semicolon <IcAgent::Ast::Nodes::IcBaseTypeChild>
29
+ end
30
+
31
+ rule base_type_base
32
+ base_type_single+ <IcAgent::Ast::Nodes::IcBaseTypeSingle>
33
+ end
34
+
35
+ rule base_type_code
36
+ [a-zA-Z0-9_]+ <IcAgent::Ast::Nodes::IcBaseTypeOther>
37
+ end
38
+
39
+ rule base_type_key
40
+ [a-zA-Z0-9_]+ <IcAgent::Ast::Nodes::IcBaseTypeKey>
41
+ end
42
+
43
+ rule base_type_value
44
+ (':' space ic_all_type)* <IcAgent::Ast::Nodes::IcBaseTypeValue>
45
+ end
46
+
47
+ rule base_type_vec
48
+ 'vec' space ic_all_type <IcAgent::Ast::Nodes::IcBaseTypeVec>
49
+ end
50
+
51
+ rule base_type_opt
52
+ 'opt' space ic_all_type <IcAgent::Ast::Nodes::IcBaseTypeOpt>
53
+ end
54
+
55
+ rule base_type_record
56
+ 'record' space '{' space_and_line statement_content space_and_line '}' <IcAgent::Ast::Nodes::IcBaseTypeRecord>
57
+ end
58
+
59
+ rule base_type_record_null
60
+ 'record {}' <IcAgent::Ast::Nodes::IcBaseTypeRecord>
61
+ end
62
+
63
+ rule base_type_variant
64
+ 'variant' space '{' space_and_line statement_content space_and_line '}' <IcAgent::Ast::Nodes::IcBaseTypeVariant>
65
+ end
66
+
67
+ rule base_type_func
68
+ 'func' space? "(" service_method_params? ")" space? "->" space? "(" service_method_return_type ")" space? method_query? <IcAgent::Ast::Nodes::IcBaseTypeFunc>
69
+ end
70
+
71
+ rule service_method_params
72
+ source_coding / refer_coding
73
+ end
74
+
75
+ rule service_method_return_type
76
+ [a-zA-Z0-9_{}:;\n ]* <IcAgent::Ast::Nodes::IcServiceMethodReturn>
77
+ end
78
+
79
+ rule refer_coding
80
+ param_element (", " param_element)* <IcAgent::Ast::Nodes::IcServiceMethodParams>
81
+ end
82
+
83
+ rule source_coding
84
+ [a-zA-Z0-9_{}:;\n ]* <IcAgent::Ast::Nodes::IcServiceMethodParams>
85
+ end
86
+
87
+ rule param_element
88
+ [a-zA-Z0-9_ ]+
89
+ end
90
+
91
+ rule method_query
92
+ [a-zA-Z0-9]* <IcAgent::Ast::Nodes::IcServiceMethodQuery>
93
+ end
94
+
95
+ rule space_and_line
96
+ [\s\n]*
97
+ end
98
+
99
+ rule space
100
+ [\s]+
101
+ end
102
+
103
+ rule optional_semicolon
104
+ [;]*
105
+ end
106
+ end
@@ -84,59 +84,12 @@ module IcAgent
84
84
  names
85
85
  end
86
86
 
87
- def type_child_item_values
88
- values = []
89
- type_child_items.each do |ele|
90
-
91
- # get multi type value
92
- replaced_hash = {}
93
- modified_str = ele.text_value.gsub("\n", '').gsub(/record\s*{[^{}]*}/) do |match|
94
- rad_id = rand(100000..999999)
95
- type_name = "record_#{rad_id}"
96
- replaced_hash[type_name] = match
97
- type_name
98
- end
99
- modified_str = modified_str.gsub(/variant\s*{[^{}]*}/) do |match|
100
- rad_id = rand(100000..999999)
101
- type_name = "variant_#{rad_id}"
102
- replaced_hash[type_name] = match
103
- type_name
104
- end
105
- replaced_hash.each_key do |key|
106
- item_arr = replaced_hash[key].strip.split(';')
107
- item_arr.each do |item|
108
- multi_item_arr = item.strip.split(':')
109
- if multi_item_arr.size > 1
110
- item_value_arr = multi_item_arr[1].strip.split(' ').collect { |v| v.strip.gsub(';', '') }
111
- item_value_arr.delete('{')
112
- item_value_arr.delete('}')
113
- item_value_arr.delete('{}')
114
- values << item_value_arr
115
- else
116
- values << []
117
- end
118
- end
119
- end
120
-
121
- # get root type value
122
- item_arr = modified_str.strip.split(':')
123
- if item_arr.size > 1
124
- item_value_arr = item_arr[1].strip.split(' ').collect { |v| v.strip.gsub(';', '') }
125
- item_value_arr.delete('{')
126
- item_value_arr.delete('}')
127
- item_value_arr.delete('{}')
128
- item_value_arr -= replaced_hash.keys
129
- values << item_value_arr
130
- else
131
- values << []
132
- end
133
- end
134
- values
135
- end
136
-
137
- def type_child_refer_items
138
- child_args = type_child_item_values.flatten - IcAgent::Candid::ALL_TYPES
139
- child_args.uniq
87
+ def type_refer_items
88
+ source_string = self.type_param_content
89
+ parser = IcAgent::Ast::StatementParser.new
90
+ parser.parse(source_string)
91
+ refer_type = parser.source_tree.content[:refer_type]
92
+ refer_type
140
93
  end
141
94
 
142
95
  def to_s
@@ -148,7 +101,7 @@ module IcAgent
148
101
  'type_param_name' => type_param_name,
149
102
  'type_root_opt_code' => type_root_opt_code,
150
103
  'type_child_item_keys' => type_child_item_keys,
151
- 'type_child_item_values' => type_child_item_values
104
+ 'type_child_item_values' => type_refer_items
152
105
  }
153
106
  end
154
107
  end
@@ -349,7 +302,7 @@ module IcAgent
349
302
  def to_obj
350
303
  obj = {}
351
304
  elements.each do |element|
352
- obj[element.title.to_s] = element.text_value
305
+ obj[element.title.to_s] = element.text_value.gsub("\n", '')
353
306
  end
354
307
  obj
355
308
  end
@@ -404,6 +357,16 @@ module IcAgent
404
357
  elements_to_s
405
358
  end
406
359
  end
360
+
361
+ class BaseTypeContent < NamedNode
362
+ def title
363
+ :ic_service_method_query
364
+ end
365
+
366
+ def to_s
367
+ elements_to_s
368
+ end
369
+ end
407
370
  end
408
371
  end
409
372
  end
@@ -0,0 +1,224 @@
1
+ require 'treetop'
2
+
3
+ module IcAgent
4
+ module Ast
5
+ module Nodes
6
+ class StatementNode < Treetop::Runtime::SyntaxNode
7
+ attr_accessor :child_count, :depth
8
+
9
+ def title
10
+ :named_node
11
+ end
12
+
13
+ def to_array
14
+ [title] + elements.map(&:to_array)
15
+ end
16
+
17
+ def to_s
18
+ "#{title.to_s.upcase} #{elements_to_s}"
19
+ end
20
+
21
+ def elements_to_s
22
+ elements.map(&:to_s).join("\n")
23
+ end
24
+
25
+ def add_child
26
+ @child_count ||= 0 + 1
27
+ end
28
+
29
+ def source_content
30
+ self.text_value.strip
31
+ end
32
+ end
33
+
34
+ class IcBaseType < StatementNode
35
+ def title
36
+ :base_type
37
+ end
38
+
39
+ def to_s
40
+ elements_to_s
41
+ end
42
+ end
43
+
44
+ class IcBaseTypeSingle < StatementNode
45
+ def title
46
+ :base_type_single
47
+ end
48
+
49
+ def to_s
50
+ elements_to_s
51
+ end
52
+
53
+ def opt_code
54
+ 'single'
55
+ end
56
+ end
57
+
58
+ class IcBaseTypeRecord < StatementNode
59
+ def title
60
+ :base_type_record
61
+ end
62
+
63
+ def to_s
64
+ elements_to_s
65
+ end
66
+
67
+ def opt_code
68
+ 'record'
69
+ end
70
+ end
71
+
72
+ class IcBaseTypeKey < StatementNode
73
+ def title
74
+ :base_type_key
75
+ end
76
+
77
+ def to_s
78
+ elements_to_s
79
+ end
80
+ end
81
+
82
+ class IcBaseTypeValue < StatementNode
83
+ def title
84
+ :base_type_value
85
+ end
86
+
87
+ def to_s
88
+ elements_to_s
89
+ end
90
+ end
91
+
92
+ class IcTypeDef < StatementNode
93
+ def title
94
+ :base_type_def
95
+ end
96
+
97
+ def to_s
98
+ elements_to_s
99
+ end
100
+ end
101
+
102
+ class IcBaseTypeVariant < StatementNode
103
+ def title
104
+ :base_type_variant
105
+ end
106
+
107
+ def to_s
108
+ elements_to_s
109
+ end
110
+
111
+ def opt_code
112
+ 'variant'
113
+ end
114
+ end
115
+
116
+ class IcBaseTypeFunc < StatementNode
117
+ def title
118
+ :base_type_func
119
+ end
120
+
121
+ def to_s
122
+ elements_to_s
123
+ end
124
+
125
+ def opt_code
126
+ 'func'
127
+ end
128
+ end
129
+
130
+ class IcBaseTypeOpt < StatementNode
131
+ def title
132
+ :base_type_opt
133
+ end
134
+
135
+ def to_s
136
+ elements_to_s
137
+ end
138
+
139
+ def opt_code
140
+ 'opt'
141
+ end
142
+ end
143
+
144
+ class IcBaseTypeVec < StatementNode
145
+ def title
146
+ :base_type_vec
147
+ end
148
+
149
+ def to_s
150
+ elements_to_s
151
+ end
152
+
153
+ def opt_code
154
+ 'vec'
155
+ end
156
+ end
157
+
158
+ class IcBaseTypeOther < StatementNode
159
+ def title
160
+ :base_type_other
161
+ end
162
+
163
+ def to_s
164
+ elements_to_s
165
+ end
166
+
167
+ def opt_code
168
+ text_value
169
+ end
170
+ end
171
+
172
+ class IcBaseTypeContent < StatementNode
173
+ def title
174
+ :base_type_content
175
+ end
176
+
177
+ def to_s
178
+ elements_to_s
179
+ end
180
+ end
181
+
182
+ class IcBaseTypeChild < StatementNode
183
+ def title
184
+ :base_type_child
185
+ end
186
+
187
+ def to_s
188
+ elements_to_s
189
+ end
190
+ end
191
+
192
+ class IcTypeName < StatementNode
193
+ def title
194
+ :type_name
195
+ end
196
+
197
+ def to_s
198
+ elements_to_s
199
+ end
200
+ end
201
+
202
+ class StatementBlock < StatementNode
203
+ def title
204
+ :statement_block
205
+ end
206
+
207
+ def to_s
208
+ elements_to_s
209
+ end
210
+ end
211
+
212
+ class StatementContent < StatementNode
213
+ def title
214
+ :statement_content
215
+ end
216
+
217
+ def to_s
218
+ elements_to_s
219
+ end
220
+ end
221
+ end
222
+ end
223
+ end
224
+
@@ -0,0 +1,93 @@
1
+ require 'treetop'
2
+
3
+ module IcAgent
4
+ module Ast
5
+ class StatementParser
6
+ attr_accessor :parser, :tree, :source_tree
7
+
8
+ REFER_TYPE_CLASS = ['IcAgent::Ast::Nodes::IcBaseTypeRecord',
9
+ 'IcAgent::Ast::Nodes::IcBaseTypeVariant',
10
+ 'IcAgent::Ast::Nodes::IcBaseTypeVec',
11
+ 'IcAgent::Ast::Nodes::IcBaseTypeOpt']
12
+
13
+ TREE_TYPE_CLASS = ['IcAgent::Ast::Nodes::IcBaseTypeRecord',
14
+ 'IcAgent::Ast::Nodes::IcBaseTypeVariant',
15
+ 'IcAgent::Ast::Nodes::IcBaseTypeVec',
16
+ 'IcAgent::Ast::Nodes::IcBaseTypeFunc',
17
+ 'IcAgent::Ast::Nodes::IcBaseTypeOpt',
18
+ 'IcAgent::Ast::Nodes::IcBaseTypeSingle',
19
+ 'IcAgent::Ast::Nodes::IcBaseTypeOther']
20
+
21
+ REFER_TYPE_KEYS = ['record', 'variant']
22
+
23
+ def initialize
24
+ Treetop.load(File.expand_path(File.join(File.dirname(__FILE__), 'nested_type_grammar.treetop')))
25
+ @parser = TypeGrammarParser.new
26
+ end
27
+
28
+ def parse(data, return_type = :string)
29
+ tree = @parser.parse(data)
30
+ raise Exception, "Parse error at offset: #{@parser.index} #{@parser.failure_reason}" if tree.nil?
31
+
32
+
33
+ # this edits the tree in place
34
+ clean_tree(tree)
35
+ # generate soure tree
36
+ gen_source_tree(tree)
37
+ @tree = tree
38
+ tree
39
+ end
40
+
41
+ def clean_tree(root_node)
42
+ return if root_node.elements.nil?
43
+
44
+ root_node.elements.delete_if { |node| node.class.name == 'Treetop::Runtime::SyntaxNode' and node.parent.class.name != 'IcAgent::Ast::Nodes::IcBaseTypeValue' and node.parent.class.name != "IcAgent::Ast::Nodes::IcTypeDef" }
45
+ root_node.elements.each { |node| self.clean_tree(node) }
46
+ end
47
+
48
+ # @param [Object] root_node
49
+ # @param [nil] tree_root_node
50
+ # @param [nil] tree_current_node
51
+ def gen_source_tree(root_node, tree_root_node = nil, tree_current_node = nil)
52
+ return if root_node.elements.nil?
53
+
54
+ tree_root_node = tree_root_node.nil? ? Tree::TreeNode.new('root', { 'total_child': 0, 'ic_type': nil, 'refer_type': [], 'prototype': root_node.source_content, 'content': root_node.source_content }) : tree_root_node
55
+ tree_current_node = tree_current_node.nil? ? tree_root_node : tree_current_node
56
+
57
+ root_node.elements.each do |node|
58
+ if TREE_TYPE_CLASS.include?(node.class.name) && node.source_content != tree_root_node.content[:prototype]
59
+
60
+ id = tree_root_node.content[:total_child] + 1
61
+ new_tree_node = Tree::TreeNode.new("node_#{id}", { 'total_child': 0, 'ic_type': nil, 'prototype': root_node.source_content, 'content': root_node.source_content })
62
+ tree_current_node << new_tree_node
63
+ tree_root_node.content[:total_child] = id
64
+
65
+ # set refer_type
66
+ unless Regexp.union(REFER_TYPE_KEYS) === root_node.source_content
67
+ param_arr = root_node.source_content.strip.split(' ').collect { |v| v.strip.gsub(';', '') }
68
+ param_arr = param_arr - IcAgent::Candid::ALL_TYPES
69
+ tree_root_node.content[:refer_type] = (tree_root_node.content[:refer_type] + param_arr).uniq
70
+ end
71
+
72
+ self.source_tree = tree_root_node
73
+ self.gen_source_tree(node, tree_root_node, new_tree_node)
74
+ else
75
+ self.gen_source_tree(node, tree_root_node, tree_current_node)
76
+ end
77
+ end
78
+
79
+ self.source_tree = tree_root_node
80
+ end
81
+
82
+ def ic_statement_root
83
+ tree.elements[0]
84
+ end
85
+
86
+ def ic_statement_childs
87
+ if tree.elements[0] && tree.elements[0].elements[0].elements[0]
88
+ tree.elements[0].elements[0].elements[0].elements
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -69,7 +69,7 @@ module IcAgent
69
69
  def build_param_tree(parser, type_name, current_node = nil, tree_root_node = nil)
70
70
  if current_node.nil?
71
71
  root_type = parser.ic_type_by_name(type_name)
72
- refer_nodes = root_type.type_child_refer_items.nil? ? [] : root_type.type_child_refer_items
72
+ refer_nodes = root_type.type_refer_items.nil? ? [] : root_type.type_refer_items
73
73
  root_node = Tree::TreeNode.new(type_name,
74
74
  { 'total_child': refer_nodes.size,
75
75
  'all_child': refer_nodes,
@@ -119,7 +119,7 @@ module IcAgent
119
119
  else
120
120
  # non self refer
121
121
  child_type = parser.ic_type_by_name(refer_node)
122
- child_refer_nodes = child_type.type_child_refer_items
122
+ child_refer_nodes = child_type.type_refer_items
123
123
  if child_refer_nodes.size == 0
124
124
  # set ic type
125
125
  ic_type = IcAgent::Ast::Assembler.build_type(child_type.type_param_content)
@@ -4,7 +4,6 @@ module IcAgent
4
4
  CANISTER_ID = 'aaaaa-aa'
5
5
  DID_FILE = <<~DIDL_DOC
6
6
  type canister_id = principal;
7
- type user_id = principal;
8
7
  type wasm_module = blob;
9
8
 
10
9
  type canister_settings = record {
@@ -21,21 +20,117 @@ module IcAgent
21
20
  freezing_threshold : nat;
22
21
  };
23
22
 
24
- service ic : {
23
+ type change_origin = variant {
24
+ from_user : record {
25
+ user_id : principal;
26
+ };
27
+ from_canister : record {
28
+ canister_id : principal;
29
+ canister_version : opt nat64;
30
+ };
31
+ };
32
+
33
+ type change_details = variant {
34
+ creation : record {
35
+ controllers : vec principal;
36
+ };
37
+ code_uninstall;
38
+ code_deployment : record {
39
+ mode : variant {install; reinstall; upgrade};
40
+ module_hash : blob;
41
+ };
42
+ controllers_change : record {
43
+ controllers : vec principal;
44
+ };
45
+ };
46
+
47
+ type change = record {
48
+ timestamp_nanos : nat64;
49
+ canister_version : nat64;
50
+ origin : change_origin;
51
+ details : change_details;
52
+ };
53
+
54
+ type http_header = record { name: text; value: text };
55
+
56
+ type ecdsa_curve = variant { secp256k1; };
57
+
58
+ type satoshi = nat64;
59
+
60
+ type bitcoin_network = variant {
61
+ mainnet;
62
+ testnet;
63
+ };
64
+
65
+ type bitcoin_address = text;
66
+
67
+ type block_hash = blob;
68
+
69
+ type outpoint = record {
70
+ txid : blob;
71
+ vout : nat32
72
+ };
73
+
74
+ type utxo = record {
75
+ outpoint: outpoint;
76
+ value: satoshi;
77
+ height: nat32;
78
+ };
79
+
80
+ type get_utxos_request = record {
81
+ address : bitcoin_address;
82
+ network: bitcoin_network;
83
+ filter: opt variant {
84
+ min_confirmations: nat32;
85
+ page: blob;
86
+ };
87
+ };
88
+
89
+ type get_current_fee_percentiles_request = record {
90
+ network: bitcoin_network;
91
+ };
92
+
93
+ type get_utxos_response = record {
94
+ utxos: vec utxo;
95
+ tip_block_hash: block_hash;
96
+ tip_height: nat32;
97
+ next_page: opt blob;
98
+ };
99
+
100
+ type get_balance_request = record {
101
+ address : bitcoin_address;
102
+ network: bitcoin_network;
103
+ min_confirmations: opt nat32;
104
+ };
105
+
106
+ type send_transaction_request = record {
107
+ transaction: blob;
108
+ network: bitcoin_network;
109
+ };
110
+
111
+ type millisatoshi_per_byte = nat64;
112
+
113
+ service : {
25
114
  create_canister : (record {
26
- settings : opt canister_settings
115
+ settings : opt canister_settings;
116
+ sender_canister_version : opt nat64;
27
117
  }) -> (record {canister_id : canister_id});
28
118
  update_settings : (record {
29
119
  canister_id : principal;
30
- settings : canister_settings
120
+ settings : canister_settings;
121
+ sender_canister_version : opt nat64;
31
122
  }) -> ();
32
123
  install_code : (record {
33
124
  mode : variant {install; reinstall; upgrade};
34
125
  canister_id : canister_id;
35
126
  wasm_module : wasm_module;
36
127
  arg : blob;
128
+ sender_canister_version : opt nat64;
129
+ }) -> ();
130
+ uninstall_code : (record {
131
+ canister_id : canister_id;
132
+ sender_canister_version : opt nat64;
37
133
  }) -> ();
38
- uninstall_code : (record {canister_id : canister_id}) -> ();
39
134
  start_canister : (record {canister_id : canister_id}) -> ();
40
135
  stop_canister : (record {canister_id : canister_id}) -> ();
41
136
  canister_status : (record {canister_id : canister_id}) -> (record {
@@ -44,12 +139,41 @@ module IcAgent
44
139
  module_hash: opt blob;
45
140
  memory_size: nat;
46
141
  cycles: nat;
142
+ idle_cycles_burned_per_day: nat;
143
+ });
144
+ canister_info : (record {
145
+ canister_id : canister_id;
146
+ num_requested_changes : opt nat64;
147
+ }) -> (record {
148
+ total_num_changes : nat64;
149
+ recent_changes : vec change;
150
+ module_hash : opt blob;
151
+ controllers : vec principal;
47
152
  });
48
153
  delete_canister : (record {canister_id : canister_id}) -> ();
49
154
  deposit_cycles : (record {canister_id : canister_id}) -> ();
155
+ raw_rand : () -> (blob);
156
+
157
+ ecdsa_public_key : (record {
158
+ canister_id : opt canister_id;
159
+ derivation_path : vec blob;
160
+ key_id : record { curve: ecdsa_curve; name: text };
161
+ }) -> (record { public_key : blob; chain_code : blob; });
162
+ sign_with_ecdsa : (record {
163
+ message_hash : blob;
164
+ derivation_path : vec blob;
165
+ key_id : record { curve: ecdsa_curve; name: text };
166
+ }) -> (record { signature : blob });
167
+
168
+ bitcoin_get_balance: (get_balance_request) -> (satoshi);
169
+ bitcoin_get_utxos: (get_utxos_request) -> (get_utxos_response);
170
+ bitcoin_send_transaction: (send_transaction_request) -> ();
171
+ bitcoin_get_current_fee_percentiles: (get_current_fee_percentiles_request) -> (vec millisatoshi_per_byte);
172
+
50
173
  provisional_create_canister_with_cycles : (record {
51
174
  amount: opt nat;
52
- settings : opt canister_settings
175
+ settings : opt canister_settings;
176
+ specified_id: opt canister_id;
53
177
  }) -> (record {canister_id : canister_id});
54
178
  provisional_top_up_canister :
55
179
  (record { canister_id: canister_id; amount: nat }) -> ();
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IcAgent
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
data/lib/ic_agent.rb CHANGED
@@ -14,8 +14,10 @@ require_relative 'ic_agent/system_state'
14
14
  require_relative 'ic_agent/canister'
15
15
 
16
16
  require_relative 'ic_agent/ast/nodes/named_nodes'
17
+ require_relative 'ic_agent/ast/nodes/statement_nodes'
17
18
  require_relative 'ic_agent/ast/nodes/string_literal'
18
19
  require_relative 'ic_agent/ast/parser'
20
+ require_relative 'ic_agent/ast/statement_parser'
19
21
  require_relative 'ic_agent/ast/writer'
20
22
  require_relative 'ic_agent/ast/assembler'
21
23
 
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.1.1
4
+ version: 0.1.2
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-06-08 00:00:00.000000000 Z
11
+ date: 2023-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base32
@@ -241,9 +241,13 @@ files:
241
241
  - lib/ic_agent/ast/assembler.rb
242
242
  - lib/ic_agent/ast/did_grammar.treetop
243
243
  - lib/ic_agent/ast/did_grammar_v1.treetop
244
+ - lib/ic_agent/ast/did_grammar_v2.treetop
245
+ - lib/ic_agent/ast/nested_type_grammar.treetop
244
246
  - lib/ic_agent/ast/nodes/named_nodes.rb
247
+ - lib/ic_agent/ast/nodes/statement_nodes.rb
245
248
  - lib/ic_agent/ast/nodes/string_literal.rb
246
249
  - lib/ic_agent/ast/parser.rb
250
+ - lib/ic_agent/ast/statement_parser.rb
247
251
  - lib/ic_agent/ast/writer.rb
248
252
  - lib/ic_agent/candid.rb
249
253
  - lib/ic_agent/canister.rb