test_sdk1 1.0.0

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.
Files changed (140) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +1 -0
  3. data/LICENSE +201 -0
  4. data/README.md +147 -0
  5. data/SECURITY.md +12 -0
  6. data/lib/crypto/00_asymmetric_key.rb +95 -0
  7. data/lib/crypto/01_ed25519.rb +67 -0
  8. data/lib/crypto/asymmetric_key.rb +87 -0
  9. data/lib/crypto/ed25519_key.rb +44 -0
  10. data/lib/crypto/key_pair.rb +40 -0
  11. data/lib/crypto/keys.rb +7 -0
  12. data/lib/crypto/secp256k1_key.rb +0 -0
  13. data/lib/crypto/test_ed25519_key.rb +44 -0
  14. data/lib/entity/account.rb +45 -0
  15. data/lib/entity/action_thresholds.rb +25 -0
  16. data/lib/entity/associated_key.rb +24 -0
  17. data/lib/entity/auction_state.rb +36 -0
  18. data/lib/entity/bid.rb +24 -0
  19. data/lib/entity/bid_info.rb +51 -0
  20. data/lib/entity/contract.rb +47 -0
  21. data/lib/entity/contract_package.rb +41 -0
  22. data/lib/entity/contract_version.rb +33 -0
  23. data/lib/entity/delegator.rb +37 -0
  24. data/lib/entity/deploy.rb +44 -0
  25. data/lib/entity/deploy_approval.rb +24 -0
  26. data/lib/entity/deploy_executable.rb +162 -0
  27. data/lib/entity/deploy_executable_item_internal.rb +26 -0
  28. data/lib/entity/deploy_executable_transfer.rb +50 -0
  29. data/lib/entity/deploy_hash.rb +16 -0
  30. data/lib/entity/deploy_header.rb +60 -0
  31. data/lib/entity/deploy_info.rb +44 -0
  32. data/lib/entity/deploy_named_argument.rb +19 -0
  33. data/lib/entity/deploy_transfer.rb +10 -0
  34. data/lib/entity/disabled_version.rb +26 -0
  35. data/lib/entity/era_info.rb +18 -0
  36. data/lib/entity/era_summary.rb +44 -0
  37. data/lib/entity/era_validator.rb +23 -0
  38. data/lib/entity/executable_deploy_item.rb +11 -0
  39. data/lib/entity/group.rb +25 -0
  40. data/lib/entity/module_bytes.rb +50 -0
  41. data/lib/entity/peer.rb +25 -0
  42. data/lib/entity/seigniorage_allocation.rb +18 -0
  43. data/lib/entity/stored_contract_by_hash.rb +50 -0
  44. data/lib/entity/stored_contract_by_name.rb +50 -0
  45. data/lib/entity/stored_value.rb +57 -0
  46. data/lib/entity/stored_versioned_contract_by_hash.rb +61 -0
  47. data/lib/entity/stored_versioned_contract_by_name.rb +61 -0
  48. data/lib/entity/transfer.rb +65 -0
  49. data/lib/entity/validator_weight.rb +23 -0
  50. data/lib/entity/vesting_schedule.rb +22 -0
  51. data/lib/include.rb +18 -0
  52. data/lib/rpc/rpc.rb +227 -0
  53. data/lib/rpc/rpc_client.rb +233 -0
  54. data/lib/rpc/rpc_error.rb +79 -0
  55. data/lib/serialization/cl_type_serializer.rb +76 -0
  56. data/lib/serialization/cl_value_bytes_parsers.rb +498 -0
  57. data/lib/serialization/cl_value_serializer.rb +259 -0
  58. data/lib/serialization/deploy_approval_serializer.rb +15 -0
  59. data/lib/serialization/deploy_executable_serializer.rb +27 -0
  60. data/lib/serialization/deploy_header_serializer.rb +49 -0
  61. data/lib/serialization/deploy_named_arg_serializer.rb +19 -0
  62. data/lib/serialization/deploy_serializer.rb +268 -0
  63. data/lib/serialization/test.rb +431 -0
  64. data/lib/test_sdk1.rb +289 -0
  65. data/lib/types/cl_account_hash.rb +24 -0
  66. data/lib/types/cl_account_hash_type.rb +22 -0
  67. data/lib/types/cl_any.rb +25 -0
  68. data/lib/types/cl_any_type.rb +22 -0
  69. data/lib/types/cl_bool.rb +32 -0
  70. data/lib/types/cl_bool_type.rb +35 -0
  71. data/lib/types/cl_byte_array.rb +25 -0
  72. data/lib/types/cl_byte_array_type.rb +27 -0
  73. data/lib/types/cl_i32.rb +26 -0
  74. data/lib/types/cl_i32_type.rb +26 -0
  75. data/lib/types/cl_i64.rb +26 -0
  76. data/lib/types/cl_i64_type.rb +27 -0
  77. data/lib/types/cl_key.rb +39 -0
  78. data/lib/types/cl_key_type.rb +27 -0
  79. data/lib/types/cl_list.rb +25 -0
  80. data/lib/types/cl_list_type.rb +26 -0
  81. data/lib/types/cl_map.rb +25 -0
  82. data/lib/types/cl_map_type.rb +26 -0
  83. data/lib/types/cl_option.rb +33 -0
  84. data/lib/types/cl_option_type.rb +52 -0
  85. data/lib/types/cl_public_key.rb +152 -0
  86. data/lib/types/cl_public_key_type.rb +26 -0
  87. data/lib/types/cl_result.rb +25 -0
  88. data/lib/types/cl_result_type.rb +26 -0
  89. data/lib/types/cl_string.rb +39 -0
  90. data/lib/types/cl_string_type.rb +32 -0
  91. data/lib/types/cl_tuple.rb +151 -0
  92. data/lib/types/cl_tuple_type.rb +108 -0
  93. data/lib/types/cl_type.rb +94 -0
  94. data/lib/types/cl_type_tag.rb +51 -0
  95. data/lib/types/cl_u128.rb +26 -0
  96. data/lib/types/cl_u128_type.rb +26 -0
  97. data/lib/types/cl_u256.rb +26 -0
  98. data/lib/types/cl_u256_type.rb +26 -0
  99. data/lib/types/cl_u32.rb +26 -0
  100. data/lib/types/cl_u32_type.rb +26 -0
  101. data/lib/types/cl_u512.rb +26 -0
  102. data/lib/types/cl_u512_type.rb +26 -0
  103. data/lib/types/cl_u64.rb +26 -0
  104. data/lib/types/cl_u64_type.rb +27 -0
  105. data/lib/types/cl_u8.rb +26 -0
  106. data/lib/types/cl_u8_type.rb +26 -0
  107. data/lib/types/cl_unit.rb +38 -0
  108. data/lib/types/cl_unit_type.rb +22 -0
  109. data/lib/types/cl_uref.rb +120 -0
  110. data/lib/types/cl_uref_type.rb +46 -0
  111. data/lib/types/cl_value.rb +10 -0
  112. data/lib/types/constants.rb +50 -0
  113. data/lib/types/error.rb +7 -0
  114. data/lib/utils/base_16.rb +18 -0
  115. data/lib/utils/byte_utils.rb +107 -0
  116. data/lib/utils/find_byte_parser_by_cl_type.rb +53 -0
  117. data/lib/utils/hash_utils.rb +19 -0
  118. data/lib/utils/hex_utils.rb +12 -0
  119. data/lib/utils/time_utils.rb +85 -0
  120. data/lib/utils/utils.rb +2 -0
  121. data/lib/version.rb +3 -0
  122. data/spec/a_spec.rb +697 -0
  123. data/spec/byte_utils_spec.rb +72 -0
  124. data/spec/cl_public_spec.rb +169 -0
  125. data/spec/cl_types_spec.rb +715 -0
  126. data/spec/cl_value_serializer_spec.rb +140 -0
  127. data/spec/client_spec.rb +25 -0
  128. data/spec/crypto_spec.rb +42 -0
  129. data/spec/deploy_approval_serializer_spec.rb +26 -0
  130. data/spec/deploy_executable_serializer_spec.rb +0 -0
  131. data/spec/deploy_header_serializer_spec.rb +21 -0
  132. data/spec/deploy_named_arg_serializer_spec.rb +49 -0
  133. data/spec/deploy_serializer_spec.rb +77 -0
  134. data/spec/deploy_serializer_test_spec.rb +225 -0
  135. data/spec/mainnet_spec.rb +452 -0
  136. data/spec/spec_helper.rb +100 -0
  137. data/spec/string_spec.rb +68 -0
  138. data/spec/testnet_spec.rb +498 -0
  139. data/spec/time_utils_spec.rb +87 -0
  140. metadata +198 -0
@@ -0,0 +1,715 @@
1
+ require_relative '../lib/types/cl_string.rb'
2
+ require_relative '../lib/types/cl_bool.rb'
3
+ require_relative '../lib/types/cl_i32.rb'
4
+ require_relative '../lib/types/cl_i64.rb'
5
+ require_relative '../lib/types/cl_u8.rb'
6
+ require_relative '../lib/types/cl_u32.rb'
7
+ require_relative '../lib/types/cl_u64.rb'
8
+ require_relative '../lib/types/cl_u128.rb'
9
+ require_relative '../lib/types/cl_u256.rb'
10
+ require_relative '../lib/types/cl_u512.rb'
11
+ require_relative '../lib/types/cl_unit.rb'
12
+ require_relative '../lib/types/cl_tuple.rb'
13
+ require_relative '../lib/types/cl_uref.rb'
14
+ require_relative '../lib/types/cl_public_key.rb'
15
+ require_relative '../lib/types/constants.rb'
16
+ require_relative '../lib/serialization/cl_value_bytes_parsers.rb'
17
+ require 'json'
18
+
19
+ describe CLBool do
20
+ my_bool = CLBool.new(false)
21
+ cl_type = my_bool.get_cl_type
22
+ describe "#get_cl_type" do
23
+ it "Bool should return proper cl_type" do
24
+ expect(cl_type).to eql('Bool')
25
+ end
26
+ end
27
+
28
+ describe "#get_value" do
29
+ it "should return proper value by calling get_value" do
30
+ my_bool1 = CLBool.new(false)
31
+ my_bool2 = CLBool.new(true)
32
+ expect(my_bool1.get_value).to eql(false)
33
+ expect(my_bool2.get_value).to eql(true)
34
+ end
35
+ end
36
+
37
+ it "toBytes() / fromBytes() do proper bytes serialization" do
38
+ bool1 = CLBool.new(false)
39
+ bool2 = CLBool.new(true)
40
+
41
+ bool1_bytes = CLValueBytesParsers::CLBoolBytesParser.to_bytes(bool1)
42
+ expect(bool1_bytes).to eql([0])
43
+
44
+ bool2_bytes = CLValueBytesParsers::CLBoolBytesParser.to_bytes(bool2)
45
+ expect(bool2_bytes).to eql([1])
46
+
47
+ from_bytes1 = CLValueBytesParsers::CLBoolBytesParser.from_bytes(bool1_bytes)
48
+
49
+ expect(from_bytes1.get_value).to eql(bool1.get_value)
50
+ expect(from_bytes1.get_cl_type).to eql(bool1.get_cl_type)
51
+
52
+ from_bytes2 = CLValueBytesParsers::CLBoolBytesParser.from_bytes(bool2_bytes)
53
+
54
+ expect(from_bytes2.get_value).to eql(bool2.get_value)
55
+ expect(from_bytes2.get_cl_type).to eql(bool2.get_cl_type)
56
+ end
57
+ end
58
+
59
+ describe CLString do
60
+ cl_string = CLString.new("ABC")
61
+ describe "#get_value" do
62
+ it "should return proper value" do
63
+ expect(cl_string.get_value).to eq("ABC")
64
+ end
65
+ end
66
+
67
+ describe "#get_cl_type" do
68
+ it "should return proper cl_type" do
69
+ expect(cl_string.get_cl_type).to be_an_instance_of(String)
70
+ end
71
+ end
72
+
73
+ describe "#get_cl_type" do
74
+ it "should return String type" do
75
+ expect(cl_string.get_cl_type).to eq('String')
76
+ end
77
+ end
78
+
79
+ describe "#cl_string.get_size" do
80
+ it "should return proper string length" do
81
+ expect(cl_string.get_size).to eq(3)
82
+ end
83
+ end
84
+
85
+ it "should return \'ABC\'" do
86
+ expect(cl_string.from_bytes("03000000414243")).to eql("ABC")
87
+ end
88
+
89
+ it "should do to_bytes / from_bytes string serialization" do
90
+ string = CLString.new("Hello, World!")
91
+ value = string.get_value
92
+ bytes = string.to_bytes(value)
93
+ result_string = string.from_bytes(bytes)
94
+ expect(value).to eql("Hello, World!")
95
+ expect(bytes).to eql("0d00000048656c6c6f2c20576f726c6421")
96
+ expect(value).to eql(result_string)
97
+ end
98
+ end
99
+
100
+
101
+ describe CLi32 do
102
+ # before :each do
103
+ # cli32 = CLi32.new
104
+ # end
105
+ it "should do proper to_bytes and from_bytes when value is -1" do
106
+ num1 = CLi32.new(-1)
107
+ byte_array = CLValueBytesParsers::CLI32BytesParser.to_bytes(num1)
108
+ expect(CLValueBytesParsers::CLI32BytesParser.from_bytes(byte_array)).to eql(num1.get_value)
109
+ end
110
+
111
+ it "should do proper to_bytes and from_bytes when value is 0" do
112
+ num2 = CLi32.new(0)
113
+ byte_array = CLValueBytesParsers::CLI32BytesParser.to_bytes(num2)
114
+ expect(CLValueBytesParsers::CLI32BytesParser.from_bytes(byte_array)).to eql(num2.get_value)
115
+ end
116
+
117
+ it "should do proper to_bytes and from_bytes when value is 1" do
118
+ num3 = CLi32.new(1)
119
+ byte_array = CLValueBytesParsers::CLI32BytesParser.to_bytes(num3)
120
+ expect(CLValueBytesParsers::CLI32BytesParser.from_bytes(byte_array)).to eql(num3.get_value)
121
+ end
122
+
123
+ it "should do proper to_bytes and from_bytes when value is MIN_I32" do
124
+ num4 = CLi32.new(MIN_I32)
125
+ byte_array = CLValueBytesParsers::CLI32BytesParser.to_bytes(num4)
126
+ expect(CLValueBytesParsers::CLI32BytesParser.from_bytes(byte_array)).to eql(num4.get_value)
127
+ end
128
+
129
+
130
+ it "should do proper to_bytes and from_bytes when value is MAX_I32" do
131
+ num5 = CLi32.new(MAX_I32)
132
+ byte_array = CLValueBytesParsers::CLI32BytesParser.to_bytes(num5)
133
+ expect(CLValueBytesParsers::CLI32BytesParser.from_bytes(byte_array)).to eql(num5.get_value)
134
+ end
135
+ end
136
+
137
+
138
+ describe CLi64 do
139
+ it "should do proper to_bytes and from_bytes when value is -10" do
140
+ first_cli64 = CLi64.new(-10)
141
+ num1 = first_cli64.get_value
142
+ byte_array1 = first_cli64.to_bytes(num1)
143
+ expect(first_cli64.from_bytes(byte_array1)).to eql(num1)
144
+ end
145
+
146
+ it "should do proper to_bytes and from_bytes when value is 0" do
147
+ second_cli64 = CLi64.new(0)
148
+ num2 = second_cli64.get_value
149
+ byte_array2 = second_cli64.to_bytes(num2)
150
+ expect(second_cli64.from_bytes(byte_array2)).to eql(num2)
151
+ end
152
+
153
+ it "should do proper to_bytes and from_bytes when value is 10" do
154
+ third_cli64 = CLi64.new(10)
155
+ num3 = third_cli64.get_value
156
+ byte_array3 = third_cli64.to_bytes(num3)
157
+ expect(third_cli64.from_bytes(byte_array3)).to eql(num3)
158
+ end
159
+
160
+ it "should do proper to_bytes and from_bytes when value is MIN_I64" do
161
+ fourth_cli64 = CLi64.new(MIN_I64)
162
+ min = fourth_cli64.get_value
163
+ byte_array4 = fourth_cli64.to_bytes(min)
164
+ expect(fourth_cli64.from_bytes(byte_array4)).to eq(min)
165
+ end
166
+
167
+
168
+ it "should do proper to_bytes and from_bytes when value is MAX_I64" do
169
+ fifth_cli64 = CLi64.new(MAX_I64)
170
+ max = fifth_cli64.get_value
171
+ byte_array5 = fifth_cli64.to_bytes(max)
172
+ expect(fifth_cli64.from_bytes(byte_array5)).to eq(max)
173
+ end
174
+ end
175
+
176
+ describe CLu8 do
177
+ it "should do proper to_bytes and from_bytes when value is MIN_U8" do
178
+ clu8 = CLu8.new(0)
179
+ num1 = clu8.get_value
180
+ byte_array1 = clu8.to_bytes(num1)
181
+ expect(clu8.from_bytes(byte_array1)).to eql(num1)
182
+ end
183
+
184
+ it "should do proper to_bytes and from_bytes when value is MAX_U8" do
185
+ clu8 = CLu8.new(MAX_U8)
186
+ num2 = clu8.get_value
187
+ byte_array2 = clu8.to_bytes(num2)
188
+ expect(clu8.from_bytes(byte_array2)).to eql(num2)
189
+ end
190
+
191
+ it "should raise error. Parameter value '-1' is not in range [0, 255]" do
192
+ clu8 = CLu8.new(-1)
193
+ num3 = clu8.get_value
194
+ err = clu8.to_bytes(num3)
195
+ expect {raise StandardError, err}.
196
+ to raise_error(err)
197
+ end
198
+ end
199
+
200
+ describe CLu32 do
201
+ it "should do proper to_bytes and from_bytes when value is MIN_U32" do
202
+ clu32 = CLu32.new(0)
203
+ num1 = clu32.get_value
204
+ byte_array1 = clu32.to_bytes(num1)
205
+ expect(clu32.from_bytes(byte_array1)).to eql(num1)
206
+ end
207
+
208
+ it "should do proper to_bytes and from_bytes when value is MAX_U32" do
209
+ clu32 = CLu32.new(MAX_U32)
210
+ num2 = clu32.get_value
211
+ byte_array2 = clu32.to_bytes(num2)
212
+ expect(clu32.from_bytes(byte_array2)).to eql(num2)
213
+ end
214
+
215
+ it "should raise error. Parameter value '-1' is out of range [0, MAX_U32]" do
216
+ clu32 = CLu32.new(-1)
217
+ num3 = clu32.get_value
218
+ err = clu32.to_bytes(num3)
219
+ expect {raise StandardError, err}.
220
+ to raise_error(err)
221
+ end
222
+
223
+ it "should raise error. Parameter value '#{MAX_U32 + 1}' is out of range [0, MAX_U32]" do
224
+ clu32 = CLu32.new(MAX_U32 + 1)
225
+ num3 = clu32.get_value
226
+ err = clu32.to_bytes(num3)
227
+ expect {raise StandardError, err}.
228
+ to raise_error(err)
229
+ end
230
+ end
231
+
232
+ describe CLu64 do
233
+ it "should do proper to_bytes and from_bytes when value is MIN_U64" do
234
+ clu64 = CLu64.new(0)
235
+ num1 = clu64.get_value
236
+ byte_array1 = clu64.to_bytes(num1)
237
+ expect(clu64.from_bytes(byte_array1)).to eql(num1)
238
+ end
239
+
240
+ it "should do proper to_bytes and from_bytes when value is MAX_U64" do
241
+ clu64 = CLu64.new(MAX_U64)
242
+ num2 = clu64.get_value
243
+ byte_array2 = clu64.to_bytes(num2)
244
+ expect(clu64.from_bytes(byte_array2)).to eql(num2)
245
+ end
246
+
247
+ it "should raise error. Parameter value '-1' is out of range [0, MAX_U64]" do
248
+ clu64 = CLu64.new(-1)
249
+ num3 = clu64.get_value
250
+ err = clu64.to_bytes(num3)
251
+ expect {raise StandardError, err}.
252
+ to raise_error(err)
253
+ end
254
+
255
+ it "should raise error. Parameter value '#{MAX_U64 + 1}' is out of range [0, MAX_U64]" do
256
+ clu64 = CLu64
257
+ .new(MAX_U64 + 1)
258
+ num3 = clu64.get_value
259
+ err = clu64.to_bytes(num3)
260
+ expect {raise StandardError, err}.
261
+ to raise_error(err)
262
+ end
263
+ end
264
+
265
+
266
+ describe CLu128 do
267
+ it "should do proper to_bytes and from_bytes when value is MIN_U128" do
268
+ clu128 = CLu128.new(0)
269
+ num1 = clu128.get_value
270
+ byte_array1 = clu128.to_bytes(num1)
271
+ expect(clu128.from_bytes(byte_array1)).to eql(num1)
272
+ end
273
+
274
+ it "should do proper to_bytes and from_bytes when value is MAX_U128" do
275
+ clu128 = CLu128.new(MAX_U128)
276
+ num2 = clu128.get_value
277
+ byte_array2 = clu128.to_bytes(num2)
278
+ expect(clu128.from_bytes(byte_array2)).to eql(num2)
279
+ end
280
+
281
+ it "should raise error. Parameter value '-1' is out of range [0, MAX_U128]" do
282
+ clu128 = CLu128.new(-1)
283
+ num3 = clu128.get_value
284
+ err = clu128.to_bytes(num3)
285
+ expect {raise StandardError, err}.to raise_error(err)
286
+ end
287
+
288
+ it "should raise error. Parameter value '#{MAX_U128 + 1}' is out of range [0, MAX_U128]" do
289
+ clu128 = CLu128.new(MAX_U128 + 1)
290
+ num3 = clu128.get_value
291
+ err = clu128.to_bytes(num3)
292
+ expect {raise StandardError, err}.to raise_error(err)
293
+ end
294
+ end
295
+
296
+
297
+ describe CLu256 do
298
+ it "should do proper to_bytes and from_bytes when value is MIN_U256" do
299
+ clu256 = CLu256.new(0)
300
+ num1 = clu256.get_value
301
+ byte_array1 = clu256.to_bytes(num1)
302
+ expect(clu256.from_bytes(byte_array1)).to eql(num1)
303
+ end
304
+
305
+ it "should do proper to_bytes and from_bytes when value is MAX_U256" do
306
+ clu256 = CLu256.new(MAX_U256)
307
+ num2 = clu256.get_value
308
+ byte_array2 = clu256.to_bytes(num2)
309
+ expect(clu256.from_bytes(byte_array2)).to eql(num2)
310
+ end
311
+
312
+ it "should raise error. Parameter value '-1' is out of range [0, MAX_U256]" do
313
+ clu256 = CLu256.new(-1)
314
+ num3 = clu256.get_value
315
+ err = clu256.to_bytes(num3)
316
+ expect {raise StandardError, err}.to raise_error(err)
317
+ end
318
+
319
+ it "should raise error. Parameter value '#{MAX_U256 + 1}' is out of range [0, MAX_U256]" do
320
+ clu256 = CLu256.new(MAX_U256 + 1)
321
+ num3 = clu256.get_value
322
+ err = clu256.to_bytes(num3)
323
+ expect {raise StandardError, err}.to raise_error(err)
324
+ end
325
+ end
326
+
327
+
328
+ describe CLu512 do
329
+ it "should do proper to_bytes and from_bytes when value is MIN_U512" do
330
+ clu512 = CLu512.new(0)
331
+ num1 = clu512.get_value
332
+ byte_array1 = clu512.to_bytes(num1)
333
+ expect(clu512.from_bytes(byte_array1)).to eql(num1)
334
+ end
335
+
336
+ it "should do proper to_bytes and from_bytes when value is MAX_U512" do
337
+ clu512 = CLu512.new(MAX_U512)
338
+ num2 = clu512.get_value
339
+ byte_array2 = clu512.to_bytes(num2)
340
+ expect(clu512.from_bytes(byte_array2)).to eql(num2)
341
+ end
342
+
343
+ it "should raise error. Parameter value '-1' is out of range [0, MAX_U512]" do
344
+ clu512 = CLu512.new(-1)
345
+ num3 = clu512.get_value
346
+ err = clu512.to_bytes(num3)
347
+ expect {raise StandardError, err}.to raise_error(err)
348
+ end
349
+
350
+ it "should raise error. Parameter value '#{MAX_U512 + 1}' is out of range [0, MAX_U512]" do
351
+ clu512 = CLu512.new(MAX_U512 + 1)
352
+ num3 = clu512.get_value
353
+ err = clu512.to_bytes(num3)
354
+ expect {raise StandardError, err}.to raise_error(err)
355
+ end
356
+ end
357
+
358
+ describe CLUnit do
359
+ it "should return proper type" do
360
+ cl = CLUnit.new()
361
+ expect(cl.get_cl_type).to eql("Unit")
362
+ end
363
+
364
+ it "should return proper value" do
365
+ cl = CLUnit.new()
366
+ expect(cl.get_value).to eql(nil)
367
+ end
368
+
369
+ it "to_json / from_json" do
370
+ cl = CLUnit.new()
371
+ json = cl.to_json
372
+ expected_json = '{"bytes":"","cl_type":"Unit"}'
373
+ expect(json).to eql(expected_json)
374
+ expect(cl.from_json(json)).to eql(cl.get_hash)
375
+ end
376
+ end
377
+
378
+ describe CLTuple do
379
+ describe CLTuple1 do
380
+ it "should return proper CLType" do
381
+ bool = CLBool.new(true)
382
+ t1 = CLTuple1.new([bool])
383
+ expect(t1.get_full_type).to eql("Tuple1 (Bool)")
384
+
385
+ str = CLString.new("ABC")
386
+ t2 = CLTuple1.new([str])
387
+ expect(t2.get_full_type).to eql("Tuple1 (String)")
388
+
389
+ i32 = CLi32.new(99)
390
+ t3 = CLTuple1.new([i32])
391
+ expect(t3.get_full_type).to eql("Tuple1 (I32)")
392
+ end
393
+
394
+ it "should throw an error when tuple elements are not in a correct format" do
395
+ # tuple = CLTuple1.new([CLBool.new(true), CLBool.new(false)])
396
+ tuple = CLTuple1.new(['a'])
397
+ err = tuple.get_full_type
398
+ expect {raise err }.to raise_error(StandardError, "Invalid data type(s) provided.")
399
+ end
400
+ it "should return error when tuple is not correctly built" do
401
+ bool = CLBool.new(false)
402
+ tuple1 = CLTuple1.new([bool, bool])
403
+ err = tuple1.get_full_type
404
+ expect {raise err}.to raise_error(StandardError, "Too many elements!")
405
+ end
406
+
407
+ it "should do proper to_bytes and from_bytes for Tuple1" do
408
+ bool1 = CLBool.new(false)
409
+ tuple = CLTuple1.new([bool1])
410
+
411
+ bool_bytes = CLValueBytesParsers::CLTupleBytesParser.to_bytes(tuple)
412
+ bool2 = CLValueBytesParsers::CLTupleBytesParser.from_bytes([bool1.get_cl_type], bool_bytes)
413
+
414
+ expect(bool2.get_value).to eql(bool1.get_value)
415
+
416
+ i32 = CLi32.new(10)
417
+ tuple = CLTuple1.new([i32])
418
+
419
+ i32_bytes = CLValueBytesParsers::CLTupleBytesParser.to_bytes(tuple)
420
+ i32_value = CLValueBytesParsers::CLTupleBytesParser.from_bytes([i32.get_cl_type], i32_bytes)
421
+ expect(i32_value).to eql(i32.get_value)
422
+ end
423
+ end
424
+
425
+ describe CLTuple2 do
426
+ it "should return proper CLType" do
427
+ bool = CLBool.new(true)
428
+ str = CLString.new("ABC")
429
+ t1 = CLTuple2.new([bool, str])
430
+ expect(t1.get_full_type).to eql("Tuple2 (Bool, String)")
431
+
432
+
433
+ str2 = CLString.new("XYZ")
434
+ u512 = CLu512.new(MAX_U512)
435
+ t2 = CLTuple2.new([str,u512])
436
+ expect(t2.get_full_type).to eql("Tuple2 (String, U512)")
437
+ end
438
+
439
+ it "should throw an error when tuple elements are not in a correct format" do
440
+ tuple = CLTuple2.new(['a', 10])
441
+ err = tuple.get_full_type
442
+ expect {raise err }.to raise_error(StandardError, "Invalid data type(s) provided.")
443
+ end
444
+
445
+ it "should return error when tuple is not correctly built" do
446
+ bool = CLBool.new(false)
447
+ tuple2 = CLTuple2.new([bool, bool, bool])
448
+ err = tuple2.get_full_type
449
+ expect {raise err}.to raise_error(StandardError, "Too many elements!")
450
+ end
451
+
452
+ it "should do proper to_bytes and from_bytes for Tuple2" do
453
+ bool1 = CLBool.new(true)
454
+ bool2 = CLBool.new(false)
455
+ tuple = CLTuple2.new([bool1, bool2])
456
+ tuple_bytes = CLValueBytesParsers::CLTupleBytesParser.to_bytes(tuple)
457
+ end
458
+ end
459
+
460
+ describe CLTuple3 do
461
+ it "should return proper CLType" do
462
+ bool = CLBool.new(true)
463
+ str = CLString.new("ABC")
464
+ u512 = CLu512.new(MAX_U512)
465
+ t1 = CLTuple3.new([bool, str, u512])
466
+ expect(t1.get_full_type).to eql("Tuple3 (Bool, String, U512)")
467
+ end
468
+
469
+ it "should throw an error when tuple elements are not in a correct format" do
470
+ tuple = CLTuple3.new(['a', 10, true])
471
+ err = tuple.get_full_type
472
+ expect {raise err }.to raise_error(StandardError, "Invalid data type(s) provided.")
473
+ end
474
+
475
+ it "should return error when tuple is not correctly built" do
476
+ bool = CLBool.new(false)
477
+ tuple3 = CLTuple3.new([bool, bool, bool, bool])
478
+ err = tuple3.get_full_type
479
+ expect {raise err}.to raise_error(StandardError, "Too many elements!")
480
+ end
481
+ end
482
+ end
483
+
484
+ describe CLURef do
485
+ uref_addr1 = "2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a"
486
+ decoded1 = CLValueBytesParsers::CLURefBytesParser.decode_base_16(uref_addr1)
487
+ encoded1 = CLValueBytesParsers::CLURefBytesParser.encode_base_16(decoded1)
488
+ uref1 = CLURef.new(decoded1, AccessRights[:READ_ADD_WRITE])
489
+ str = "uref-ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff-007"
490
+
491
+ it "should return proper CLType" do
492
+ expect(uref1.get_cl_type).to eql('URef')
493
+ expect(uref1).to be_an_instance_of(CLURef)
494
+ end
495
+
496
+ it "should return proper value" do
497
+ expect(uref1.get_value).to eql(decoded1)
498
+ end
499
+
500
+ it "should return proper AccessRights" do
501
+ expect(uref1.get_access_rights).to eql(AccessRights[:READ_ADD_WRITE])
502
+ end
503
+
504
+ it "should do proper to_bytes and from_bytes" do
505
+ to_bytes = CLValueBytesParsers::CLURefBytesParser.to_bytes(uref1)
506
+ # p to_bytes
507
+ from_bytes = CLValueBytesParsers::CLURefBytesParser.from_bytes(to_bytes)
508
+ # p from_bytes
509
+ expect(uref1.get_value).to eql(from_bytes.get_value)
510
+ expect(uref1.get_access_rights).to eql(from_bytes.get_access_rights)
511
+ end
512
+
513
+ it "should return error when CLUref is not correctly built" do
514
+ uref_addr2 = "4b4b4b"
515
+ decoded2 = CLValueBytesParsers::CLURefBytesParser.decode_base_16(uref_addr2)
516
+ uref2 = CLURef.new(decoded2, 0)
517
+ expect {raise 'The length of URefAddr should be 32'}.to raise_error(RuntimeError, 'The length of URefAddr should be 32')
518
+ # expect {raise 'The length of URefAddr should be 32'}.to raise_error('The length of URefAddr should be 32')
519
+ # expect {raise 'The length of URefAddr should be 32'}.to raise_error(ArgumentError, 'The length of URefAddr should be 32')
520
+ end
521
+
522
+ describe "#parse_uref_address" do
523
+ # str = "uref-ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff-007"
524
+ it "should raise an error when string does not begin with \'uref-\'" do
525
+ bad_formatted_str = "xxx-ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff-007"
526
+ expect {CLURef.parse_uref_address(bad_formatted_str)}.to raise_error(ArgumentError)
527
+ end
528
+
529
+ it "should raise an error when string does not begin with prefix: \'uref-\'" do
530
+ bad_formatted_str = "ureffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff-007"
531
+ expect {CLURef.parse_uref_address(bad_formatted_str)}.to raise_error(ArgumentError)
532
+ end
533
+
534
+ it "should raise an error when uref bytes length is not equal to 32" do
535
+ bad_formatted_str = "uref-fff-007"
536
+ expect {CLURef.parse_uref_address("uref-fff-007")}.to raise_error(ArgumentError)
537
+ end
538
+
539
+ it "should raise an error when given parameter has lack of suffix which represents AccessRights" do
540
+ bad_formatted_str = "uref-ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
541
+ expect {CLURef.parse_uref_address(bad_formatted_str)}.to raise_error(ArgumentError)
542
+ end
543
+
544
+ it "should raise an error when AccessRights is not within a range [0, 7]" do
545
+ bad_formatted_str = "uref-ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff-008"
546
+ expect {CLURef.parse_uref_address(bad_formatted_str)}.to raise_error(ArgumentError)
547
+ end
548
+ it "should convert to_json and from_json properly" do
549
+ uref_addr1 = "2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a"
550
+ decoded1 = CLValueBytesParsers::CLURefBytesParser.decode_base_16(uref_addr1)
551
+ uref1 = CLURef.new(decoded1, 7)
552
+
553
+ expect(CLURef.to_json(uref1)).to eql('{"bytes":"2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a07","cl_type":"URef"}')
554
+
555
+ json = '{"bytes":"2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a07","cl_type":"URef"}'
556
+
557
+ expect(CLURef.from_json(json).get_value).to eql(uref1.get_value)
558
+ expect(CLURef.from_json(json).get_access_rights).to eql(uref1.get_access_rights)
559
+ end
560
+ end
561
+
562
+ end
563
+
564
+
565
+ describe CLPublicKey do
566
+ pub_key_hex_ed25519 = "010af5a943bacd2a8e91792eb4e9a25e32d536ab103372f57f89ebcadfc59820d1"
567
+ pub_key_hex_secp256K1 = "024ae7d5b66b2fd0f66fb0efcceecb673b3762595b30ae1cac48ae8f09d34c952ee4"
568
+ pub_raw_ed25519 = [
569
+ 10, 245, 169, 67, 186, 205, 42, 142,
570
+ 145, 121, 46, 180, 233, 162, 94, 50,
571
+ 213, 54, 171, 16, 51, 114, 245, 127,
572
+ 137, 235, 202, 223, 197, 152, 32, 209
573
+ ]
574
+ pub_raw_secp256K1 = [
575
+ 74, 231, 213, 182, 107, 47, 208, 246,
576
+ 111, 176, 239, 204, 238, 203, 103, 59,
577
+ 55, 98, 89, 91, 48, 174, 28, 172, 72,
578
+ 174, 143, 9, 211, 76, 149, 46, 228
579
+ ]
580
+ it "should return error when CLPublicKey is not correctly built" do
581
+ public_key_ed25519 = CLPublicKey.new(pub_raw_ed25519, CLPublicKeyTag[:ED25519])
582
+ public_key_secp256K1 = CLPublicKey.new(pub_raw_secp256K1, CLPublicKeyTag[:SECP256K1])
583
+
584
+ expect(public_key_ed25519).to be_an_instance_of(CLPublicKey)
585
+ expect(public_key_secp256K1).to be_an_instance_of(CLPublicKey)
586
+ end
587
+
588
+ it "should raise error when CLPublicKey is not properly constructed" do
589
+ expect {CLPublicKey.new(pub_raw_ed25519, 3)}.to raise_error(ArgumentError)
590
+ expect {CLPublicKey.new(pub_raw_secp256K1, 3)}.to raise_error(ArgumentError)
591
+ end
592
+
593
+ it "should return proper CLType" do
594
+ public_key1 = CLPublicKey.new(pub_raw_ed25519, 1)
595
+ public_key2 = CLPublicKey.new(pub_raw_secp256K1, 2)
596
+
597
+ expect(public_key1.get_cl_type).to eql("PublicKey")
598
+ expect(public_key2.get_cl_type).to eql("PublicKey")
599
+ end
600
+
601
+ it "to_hex / from_hex work properly for ed25519" do
602
+ pub_raw_ed25519 = [
603
+ 10, 245, 169, 67, 186, 205, 42, 142,
604
+ 145, 121, 46, 180, 233, 162, 94, 50,
605
+ 213, 54, 171, 16, 51, 114, 245, 127,
606
+ 137, 235, 202, 223, 197, 152, 32, 209
607
+ ]
608
+ public_key = CLPublicKey.new(pub_raw_ed25519, 1)
609
+ expected_hex_result = "010af5a943bacd2a8e91792eb4e9a25e32d536ab103372f57f89ebcadfc59820d1"
610
+ expect(public_key.to_hex).to eql(expected_hex_result)
611
+
612
+ hex_value = public_key.to_hex
613
+ result_public_key = public_key.from_hex(hex_value)
614
+
615
+ expect(result_public_key.get_value).to eql([10, 245, 169, 67, 186, 205, 42, 142, 145, 121, 46,
616
+ 180, 233, 162, 94, 50, 213, 54, 171, 16, 51, 114, 245, 127, 137, 235, 202, 223, 197, 152, 32, 209])
617
+ expect(result_public_key.get_cl_public_key_tag).to eql(1)
618
+ expect(result_public_key.ed25519?).to eql(true)
619
+ expect(result_public_key.get_signature_algorithm).to eql("ed25519")
620
+ end
621
+
622
+ it "to_hex / from_hex work properly for secp256K1" do
623
+ pub_raw_secp256K1 = [74, 231, 213, 182, 107, 47, 208, 246, 111, 176, 239, 204, 238, 203, 103, 59, 55,
624
+ 98, 89, 91, 48, 174, 28, 172, 72, 174, 143, 9, 211, 76, 149, 46, 228]
625
+ public_key = CLPublicKey.new(pub_raw_secp256K1, 2)
626
+ expected_hex_result = "024ae7d5b66b2fd0f66fb0efcceecb673b3762595b30ae1cac48ae8f09d34c952ee4"
627
+ expect(public_key.to_hex).to eql(expected_hex_result)
628
+
629
+ hex_value = public_key.to_hex
630
+ result_public_key = public_key.from_hex(hex_value)
631
+
632
+ expect(result_public_key.get_value).to eql([74, 231, 213, 182, 107, 47, 208, 246, 111, 176, 239, 204,
633
+ 238, 203, 103, 59, 55, 98, 89, 91, 48, 174, 28, 172, 72, 174, 143, 9, 211, 76, 149, 46, 228])
634
+ expect(result_public_key.get_cl_public_key_tag).to eql(2)
635
+ expect(result_public_key.secp256k1?).to eql(true)
636
+ expect(result_public_key.get_signature_algorithm).to eql("secp256k1")
637
+ end
638
+
639
+ it "to_account_hash_byte_array works properly" do
640
+ account_key = "01e23d200eb0f3c8a3dacc8453644e6fcf4462585a68234ebb1c3d6cc8971148c2"
641
+ public_key = CLPublicKey.from_hex(account_key)
642
+ # puts account_key
643
+ # puts public_key.to_hex
644
+ # puts public_key.get_value.inspect
645
+ # puts public_key.get_cl_public_key_tag
646
+ expected_result = [217, 84, 5, 56, 40, 230, 253, 7, 122, 223, 214, 81, 224, 24, 172, 125,
647
+ 213, 28, 170, 162, 61, 8, 23, 104, 77, 183, 110, 48, 67, 209, 9, 3]
648
+ expect(public_key.to_account_hash_byte_array).to eql(expected_result)
649
+ end
650
+
651
+ it "to_account_hash_hex works properly" do
652
+ account_key = "01e23d200eb0f3c8a3dacc8453644e6fcf4462585a68234ebb1c3d6cc8971148c2"
653
+ public_key = CLPublicKey.from_hex(account_key)
654
+ expected_hex_result = "account-hash-d954053828e6fd077adfd651e018ac7dd51caaa23d0817684db76e3043d10903"
655
+ expect(public_key.to_account_hash_hex).to eql(expected_hex_result)
656
+ end
657
+
658
+ it "should do proper to_bytes and from_bytes serialization for CLPublicKey" do
659
+ public_key1 = CLPublicKey.from_ed25519(Array.new(32, 50))
660
+ expected_ed25519_result = [
661
+ 1, 50, 50, 50, 50, 50, 50, 50, 50,
662
+ 50, 50, 50, 50, 50, 50, 50, 50,
663
+ 50, 50, 50, 50, 50, 50, 50, 50,
664
+ 50, 50, 50, 50, 50, 50, 50, 50
665
+ ]
666
+
667
+ bytes = CLValueBytesParsers::CLPublicKeyBytesParser.to_bytes(public_key1)
668
+ expect(bytes).to eql(expected_ed25519_result)
669
+
670
+ expected_raw_public_key1 = Array.new(32, 50)
671
+
672
+ expect(CLValueBytesParsers::CLPublicKeyBytesParser.from_bytes(bytes).get_value).to eql(expected_raw_public_key1)
673
+ expect(CLValueBytesParsers::CLPublicKeyBytesParser.from_bytes(bytes).get_cl_public_key_tag).to eql(1)
674
+
675
+ public_key2 = CLPublicKey.from_secp256k1(Array.new(33, 100))
676
+ expected_secp256k1_result = [
677
+ 2, 100, 100, 100, 100, 100, 100, 100, 100,
678
+ 100, 100, 100, 100, 100, 100, 100, 100, 100,
679
+ 100, 100, 100, 100, 100, 100, 100, 100,
680
+ 100, 100, 100, 100, 100, 100, 100, 100
681
+ ]
682
+
683
+ bytes = CLValueBytesParsers::CLPublicKeyBytesParser.to_bytes(public_key2)
684
+ expect(bytes).to eql(expected_secp256k1_result)
685
+
686
+ expected_raw_public_key2 = Array.new(33, 100)
687
+
688
+ expect(CLValueBytesParsers::CLPublicKeyBytesParser.from_bytes(bytes).get_value).to eql(expected_raw_public_key2)
689
+ expect(CLValueBytesParsers::CLPublicKeyBytesParser.from_bytes(bytes).get_cl_public_key_tag).to eql(2)
690
+ end
691
+
692
+ it "to_json / from_json for CLPublicKey" do
693
+ public_key_hex1 = "01e23d200eb0f3c8a3dacc8453644e6fcf4462585a68234ebb1c3d6cc8971148c2"
694
+ # account_hash_hex = "14b94d33a1be1a2741ddefa7ae68a28cd1956e3801730bea617bf529d50f8aea"
695
+
696
+ public_key1 = CLPublicKey.from_hex(public_key_hex1)
697
+ raw_public_key1 = public_key1.get_value
698
+ tag1 = public_key1.get_cl_public_key_tag
699
+
700
+ expect(CLPublicKey.to_json(public_key1)).to eql('{"bytes":"01e23d200eb0f3c8a3dacc8453644e6fcf4462585a68234ebb1c3d6cc8971148c2","cl_type":"PublicKey"}')
701
+
702
+ json = CLPublicKey.to_json(public_key1)
703
+
704
+ public_key2 = CLPublicKey.from_json(json)
705
+
706
+ raw_public_key2 = public_key2.get_value
707
+ tag2 = public_key2.get_cl_public_key_tag
708
+ public_key_hex2 = public_key2.to_hex
709
+
710
+ expect(raw_public_key2).to eql(raw_public_key1)
711
+ expect(tag2).to eql(tag1)
712
+ expect(public_key_hex2).to eql(public_key_hex1)
713
+ end
714
+ end
715
+