test_sdk1 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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,259 @@
1
+ require_relative '../types/cl_type.rb'
2
+ require_relative '../types/cl_bool.rb'
3
+ require_relative '../types/cl_i32.rb'
4
+ require_relative '../types/cl_i64.rb'
5
+ require_relative '../types/cl_u8.rb'
6
+ require_relative '../types/cl_u32.rb'
7
+ require_relative '../types/cl_u64.rb'
8
+ require_relative '../types/cl_u128.rb'
9
+ require_relative '../types/cl_u256.rb'
10
+ require_relative '../types/cl_u512.rb'
11
+ require_relative '../types/cl_unit.rb'
12
+ require_relative '../types/cl_tuple.rb'
13
+ require_relative '../types/cl_uref.rb'
14
+ require_relative '../types/cl_option.rb'
15
+ require_relative '../types/cl_string.rb'
16
+ # require_relative '../types/cl_key.rb'
17
+ require_relative '../types/cl_uref.rb'
18
+ require_relative '../types/cl_tuple.rb'
19
+ require_relative '../types/cl_public_key.rb'
20
+ require_relative '../types/constants.rb'
21
+ require_relative '../serialization/cl_value_bytes_parsers.rb'
22
+
23
+ class CLValueSerializer
24
+
25
+ def to_bytes(clvalue)
26
+ type = clvalue.get_cl_type
27
+ value = clvalue.get_value
28
+ tag = CLType::TAGS[type.to_sym]
29
+ # puts type
30
+ # puts value
31
+ # puts CLType::TAGS[type.to_sym]
32
+ # puts tag
33
+ [1].pack("L<*").unpack1("H*")
34
+ serialized = ""
35
+ if type == "Bool"
36
+ [1].pack("L<*").unpack1("H*") + [value.to_i].pack("C*").unpack1("H*") + [tag].pack("C*").unpack1("H*")
37
+ elsif type == "I32"
38
+ [4].pack("L<*").unpack1("H*") + [value].pack("l<*").unpack1("H*") + [tag].pack("C*").unpack1("H*")
39
+ elsif type == "I64"
40
+ [8].pack("L<*").unpack1("H*") + [value].pack("q<*").unpack1("H*") + [tag].pack("C*").unpack1("H*")
41
+ elsif type == "U8"
42
+ [1].pack("L<*").unpack1("H*") + [value].pack("C*").unpack1("H*") + [tag].pack("C*").unpack1("H*")
43
+ elsif type == "U32"
44
+ serialized += [4].pack("L<*").unpack1("H*") + [value].pack("L<*").unpack1("H*") + [tag].pack("C*").unpack1("H*")
45
+ elsif type == "U64"
46
+ [8].pack("L<*").unpack1("H*") + [value].pack("Q<*").unpack1("H*") + [tag].pack("C*").unpack1("H*")
47
+ elsif type == "U128"
48
+ [8].pack("L<*").unpack1("H*")
49
+ elsif type == "U256"
50
+ [8].pack("L<*").unpack1("H*")
51
+ elsif type == "U512"
52
+ [8].pack("L<*").unpack1("H*")
53
+ elsif type == "Unit"
54
+ [0].pack("L<*").unpack1("H*") + [tag].pack("C*").unpack1("H*")
55
+ elsif type == "String"
56
+ length = CLValueBytesParsers::CLStringBytesParser.to_bytes(value).length
57
+ [length/2].pack("L<*").unpack1("H*") + CLValueBytesParsers::CLStringBytesParser.to_bytes(value) + [tag].pack("C*").unpack1("H*")
58
+ elsif type == "Key"
59
+ [11].pack("C*").unpack1("H*")
60
+ elsif type == "URef"
61
+ uref = clvalue.get_value
62
+ size = clvalue.to_bytes(uref).length/2
63
+ [size].pack("L<*").unpack1("H*") + clvalue.to_bytes(uref) + [tag].pack("C*").unpack1("H*")
64
+ elsif type == "Option"
65
+ [0].pack("L<*").unpack1("H*")
66
+ elsif type == "List"
67
+ [0].pack("L<*").unpack1("H*")
68
+ elsif type == "ByteArray"
69
+ [0].pack("L<*").unpack1("H*")
70
+ elsif type == "Result"
71
+ [0].pack("L<*").unpack1("H*")
72
+ elsif type == "Map"
73
+ [0].pack("L<*").unpack1("H*")
74
+ elsif type == "Tuple1"
75
+ clvalue1 = clvalue.get_value[0]
76
+ type1 = clvalue1.get_cl_type
77
+ value1 = clvalue1.get_value
78
+ tag1 = CLType::TAGS[type1.to_sym]
79
+ serialized += helper(clvalue.get_value[0]) + [tag].pack("C*").unpack1("H*") + [tag1].pack("C*").unpack1("H*")
80
+ # [18].pack("C*").unpack1("H*") + cl_type.get_data[0].to_bytes
81
+ elsif type == "Tuple2"
82
+ clvalue1 = clvalue.get_value[0]
83
+ type1 = clvalue1.get_cl_type
84
+ value1 = clvalue1.get_value
85
+ tag1 = CLType::TAGS[type1.to_sym]
86
+
87
+ clvalue2 = clvalue.get_value[1]
88
+ type2 = clvalue2.get_cl_type
89
+ value2 = clvalue2.get_value
90
+ tag2 = CLType::TAGS[type2.to_sym]
91
+
92
+ len = only_length(clvalue.get_value[0]) + only_length(clvalue.get_value[1])
93
+ [len].pack("L<*").unpack1("H*") + only_value(clvalue.get_value[0]) + only_value(clvalue.get_value[1]) +
94
+ [tag].pack("C*").unpack1("H*") + [tag1].pack("C*").unpack1("H*") + [tag2].pack("C*").unpack1("H*")
95
+ elsif type == "Tuple3"
96
+ clvalue1 = clvalue.get_value[0]
97
+ type1 = clvalue1.get_cl_type
98
+ value1 = clvalue1.get_value
99
+ tag1 = CLType::TAGS[type1.to_sym]
100
+
101
+ clvalue2 = clvalue.get_value[1]
102
+ type2 = clvalue2.get_cl_type
103
+ value2 = clvalue2.get_value
104
+ tag2 = CLType::TAGS[type2.to_sym]
105
+
106
+ clvalue3 = clvalue.get_value[2]
107
+ type3 = clvalue3.get_cl_type
108
+ value3 = clvalue3.get_value
109
+ tag3 = CLType::TAGS[type3.to_sym]
110
+
111
+ len = only_length(clvalue.get_value[0]) + only_length(clvalue.get_value[1]) + only_length(clvalue.get_value[2])
112
+ [len].pack("L<*").unpack1("H*") + only_value(clvalue.get_value[0]) + only_value(clvalue.get_value[1]) + only_value(clvalue.get_value[2]) +
113
+ [tag].pack("C*").unpack1("H*") + [tag1].pack("C*").unpack1("H*") + [tag2].pack("C*").unpack1("H*") + [tag3].pack("C*").unpack1("H*")
114
+ elsif type == "Any"
115
+ [0].pack("L<*").unpack1("H*")
116
+ elsif type == "PublicKey"
117
+ [clvalue.to_hex.length/2].pack("L<*").unpack1("H*") + clvalue.to_hex + [tag].pack("C*").unpack1("H*")
118
+ else
119
+ "Undefined"
120
+ end
121
+ end
122
+
123
+ def helper(clvalue)
124
+ type = clvalue.get_cl_type
125
+ value = clvalue.get_value
126
+ serialized = ""
127
+ if type == "Bool"
128
+ [1].pack("L<*").unpack1("H*") + [value.to_i].pack("C*").unpack1("H*")
129
+ elsif type == "I32"
130
+ [4].pack("L<*").unpack1("H*") + [value].pack("l<*").unpack1("H*")
131
+ elsif type == "I64"
132
+ [8].pack("L<*").unpack1("H*") + [value].pack("q<*").unpack1("H*")
133
+ elsif type == "U8"
134
+ [1].pack("L<*").unpack1("H*") + [value].pack("C*").unpack1("H*")
135
+ elsif type == "U32"
136
+ serialized += [4].pack("L<*").unpack1("H*") + [value].pack("L<*").unpack1("H*")
137
+ elsif type == "U64"
138
+ [8].pack("L<*").unpack1("H*") + [value].pack("Q<*").unpack1("H*")
139
+ elsif type == "U128"
140
+ [8].pack("L<*").unpack1("H*")
141
+ elsif type == "U256"
142
+ [8].pack("L<*").unpack1("H*")
143
+ elsif type == "U512"
144
+ [8].pack("L<*").unpack1("H*")
145
+ elsif type == "Unit"
146
+ [9].pack("C*").unpack1("H*")
147
+ elsif type == "String"
148
+ length = CLValueBytesParsers::CLStringBytesParser.to_bytes(value).length
149
+ [length/2].pack("L<*").unpack1("H*") + CLValueBytesParsers::CLStringBytesParser.to_bytes(value)
150
+ elsif type == "Key"
151
+ [11].pack("C*").unpack1("H*")
152
+ elsif type == "URef"
153
+ uref = clvalue.get_value
154
+ size = clvalue.to_bytes(uref).length/2
155
+ [size].pack("L<*").unpack1("H*") + clvalue.to_bytes(uref)
156
+ elsif type == "Option"
157
+ [0].pack("L<*").unpack1("H*")
158
+ elsif type == "List"
159
+ [0].pack("L<*").unpack1("H*")
160
+ elsif type == "ByteArray"
161
+ [0].pack("L<*").unpack1("H*")
162
+ elsif type == "Result"
163
+ [0].pack("L<*").unpack1("H*")
164
+ elsif type == "Map"
165
+ [0].pack("L<*").unpack1("H*")
166
+ else
167
+ "Undefined"
168
+ end
169
+ end
170
+
171
+ def only_length(clvalue)
172
+ type = clvalue.get_cl_type
173
+ value = clvalue.get_value
174
+ if type == "Bool"
175
+ 1
176
+ elsif type == "I32"
177
+ 4
178
+ elsif type == "I64"
179
+ 8
180
+ elsif type == "U8"
181
+ 1
182
+ elsif type == "U32"
183
+ 4
184
+ elsif type == "U64"
185
+ 8
186
+ elsif type == "U128"
187
+ 8
188
+ elsif type == "U256"
189
+ 8
190
+ elsif type == "U512"
191
+ 8
192
+ elsif type == "Unit"
193
+ 8
194
+ elsif type == "String"
195
+ 4 + value.length
196
+ elsif type == "Key"
197
+ 32
198
+ elsif type == "URef"
199
+ uref = clvalue.get_value
200
+ size = clvalue.to_bytes(uref).length/2
201
+ elsif type == "Option"
202
+ 8
203
+ elsif type == "List"
204
+ 8
205
+ elsif type == "ByteArray"
206
+ 8
207
+ elsif type == "Result"
208
+ 8
209
+ elsif type == "Map"
210
+ 8
211
+ else
212
+ 0
213
+ end
214
+ end
215
+ def only_value(clvalue)
216
+ type = clvalue.get_cl_type
217
+ value = clvalue.get_value
218
+ if type == "Bool"
219
+ [value.to_i].pack("C*").unpack1("H*")
220
+ elsif type == "I32"
221
+ [value].pack("l<*").unpack1("H*")
222
+ elsif type == "I64"
223
+ [value].pack("q<*").unpack1("H*")
224
+ elsif type == "U8"
225
+ [value].pack("C*").unpack1("H*")
226
+ elsif type == "U32"
227
+ [value].pack("L<*").unpack1("H*")
228
+ elsif type == "U64"
229
+ [value].pack("Q<*").unpack1("H*")
230
+ elsif type == "U128"
231
+ [value].pack("Q<*").unpack1("H*")
232
+ elsif type == "U256"
233
+ [value].pack("Q<*").unpack1("H*")
234
+ elsif type == "U512"
235
+ [value].pack("Q<*").unpack1("H*")
236
+ elsif type == "Unit"
237
+ [value].pack("C*").unpack1("H*")
238
+ elsif type == "String"
239
+ CLValueBytesParsers::CLStringBytesParser.to_bytes(value)
240
+ elsif type == "Key"
241
+ [value].pack("C*").unpack1("H*")
242
+ elsif type == "URef"
243
+ uref = clvalue.get_value
244
+ clvalue.to_bytes(uref)
245
+ elsif type == "Option"
246
+ [0].pack("L<*").unpack1("H*")
247
+ elsif type == "List"
248
+ [0].pack("L<*").unpack1("H*")
249
+ elsif type == "ByteArray"
250
+ [0].pack("L<*").unpack1("H*")
251
+ elsif type == "Result"
252
+ [0].pack("L<*").unpack1("H*")
253
+ elsif type == "Map"
254
+ [0].pack("L<*").unpack1("H*")
255
+ else
256
+ "Undefined"
257
+ end
258
+ end
259
+ end
@@ -0,0 +1,15 @@
1
+ require_relative '../entity/deploy_approval.rb'
2
+ require_relative '../serialization/cl_value_bytes_parsers.rb'
3
+ require_relative '../utils/time_utils.rb'
4
+ require_relative '../utils/byte_utils.rb'
5
+
6
+ class DeployApprovalSerializer
7
+ def to_bytes(deploy_approval)
8
+ @approval_serializer = ""
9
+ @signer = deploy_approval.get_signer
10
+ @approval_serializer << @signer
11
+
12
+ @signature = deploy_approval.get_signature
13
+ @approval_serializer << @signature
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ require_relative '../entity/deploy_executable.rb'
2
+
3
+
4
+ class DeployExecutableSerializer
5
+
6
+ def initialize
7
+ @deploy_executable = Casper::Entity::DeployExecutable.new
8
+ end
9
+
10
+ # @param [DeployExecutable] deploy_executable
11
+ def to_bytes(deploy_executable)
12
+ if deploy_executable.module_bytes?
13
+ deploy_executable.get_module_bytes.to_bytes
14
+ elsif deploy_executable.stored_contract_by_hash?
15
+ deploy_executable.stored_contract_by_hash.to_bytes
16
+ elsif deploy_executable.stored_contract_by_name?
17
+ deploy_executable.stored_contract_by_name.to_bytes
18
+ elsif deploy_executable.stored_versioned_contract_by_hash?
19
+ deploy_executable.stored_versioned_contract_by_hash.to_bytes
20
+ elsif deploy_executable.stored_versioned_contract_by_name?
21
+ deploy_executable.stored_versioned_contract_by_name.to_bytes
22
+ elsif deploy_executable.transfer?
23
+ deploy_executable.transfer.to_bytes
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,49 @@
1
+ require_relative '../entity/deploy_header.rb'
2
+ require_relative '../serialization/cl_value_bytes_parsers.rb'
3
+ require_relative '../utils/time_utils.rb'
4
+ require_relative '../utils/byte_utils.rb'
5
+
6
+
7
+ class DeployHeaderSerializer
8
+
9
+ # @param [DeployerHeader] deploy_header
10
+ # @return [String] serialized_header
11
+ def to_bytes(deploy_header)
12
+ @serialized_header = ""
13
+
14
+ @account = deploy_header.get_account
15
+ @serialized_account = @account
16
+ @serialized_header << @serialized_account
17
+
18
+ @timestamp = deploy_header.get_timestamp
19
+ timestamp_ms = Utils::TimeUtils.to_epoc_ms(@timestamp)
20
+ @serialized_timestamp = Utils::ByteUtils.to_u64(timestamp_ms)
21
+ @serialized_header << @serialized_timestamp
22
+
23
+ @ttl = deploy_header.get_ttl
24
+ ttl_ms = Utils::TimeUtils.ttl_to_milliseconds(@ttl)
25
+ @serialized_ttl = Utils::ByteUtils.to_u64(ttl_ms)
26
+ @serialized_header << @serialized_ttl
27
+
28
+ @gas_price = deploy_header.get_gas_price
29
+ @serialized_gas_price = Utils::ByteUtils.to_u64(@gas_price)
30
+ @serialized_header << @serialized_gas_price
31
+
32
+ @body_hash = deploy_header.get_body_hash
33
+ @serialized_body_hash = @body_hash
34
+ @serialized_header << @serialized_body_hash
35
+
36
+ @dependencies = deploy_header.get_dependencies
37
+ @serialized_dependencies = Utils::ByteUtils.to_u32(@dependencies.size) + @dependencies.join("")
38
+ @serialized_header << @serialized_dependencies
39
+
40
+ # @chain_name = CLString.new(deploy_header.get_chain_name)
41
+ # @serialized_chain_name = @chain_name.to_bytes(deploy_header.get_chain_name)
42
+ @chain_name = deploy_header.get_chain_name
43
+ @serialized_chain_name = CLValueBytesParsers::CLStringBytesParser.to_bytes(@chain_name)
44
+
45
+ @serialized_header << @serialized_chain_name
46
+ @serialized_header
47
+ end
48
+ end
49
+
@@ -0,0 +1,19 @@
1
+ require_relative '../entity/deploy_named_argument.rb'
2
+ require_relative '../utils/find_byte_parser_by_cl_type.rb'
3
+ require_relative '../utils/byte_utils.rb'
4
+ require_relative './cl_type_serializer.rb'
5
+ require_relative './cl_value_serializer.rb'
6
+
7
+ class DeployNamedArgSerializer
8
+
9
+ def to_bytes(arg)
10
+ name = arg.get_name
11
+ serialized_name = CLValueBytesParsers::CLStringBytesParser.to_bytes(name)
12
+ clvalue = arg.get_value
13
+ type = clvalue.get_cl_type
14
+
15
+ serializer = CLValueSerializer.new
16
+ serialized_value = serializer.to_bytes(clvalue)
17
+ serialized_name + serialized_value
18
+ end
19
+ end
@@ -0,0 +1,268 @@
1
+ require_relative './deploy_header_serializer'
2
+ require_relative './deploy_approval_serializer'
3
+ require_relative './deploy_executable_serializer'
4
+ require_relative './deploy_named_arg_serializer'
5
+ require_relative '../utils/byte_utils.rb'
6
+
7
+ class DeploySerializer
8
+
9
+ def to_bytes(deploy)
10
+ result = ""
11
+ deploy_header = Casper::Entity::DeployHeader.new(deploy.get_header)
12
+ result += DeployHeaderSerializer.new().to_bytes(deploy_header)
13
+
14
+ deploy_hash = Casper::Entity::DeployHash.new(deploy.get_hash)
15
+ result += deploy_hash.get_hash
16
+
17
+ payment = deploy.get_payment
18
+ session = deploy.get_session
19
+ if payment.keys[0] == :ModuleBytes
20
+ temp_args = []
21
+ module_bytes = payment[:ModuleBytes][:module_bytes]
22
+ args = payment[:ModuleBytes][:args]
23
+ module_bytes = Casper::Entity::ModuleBytes.new(module_bytes, args)
24
+ args.each do |arg|
25
+ name1 = arg[0]
26
+ clvalue_hash = arg[1]
27
+ clvalue = build_cl_value(arg[1])
28
+ # puts clvalue
29
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
30
+ end
31
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::ModuleBytes.new(module_bytes, temp_args).to_bytes)
32
+ result += temp
33
+ temp = nil
34
+ elsif payment.keys[0] == :StoredContractByHash
35
+ temp_args = []
36
+ hash = payment[:StoredContractByHash][:hash]
37
+ entry_point = payment[:StoredContractByHash][:entry_point]
38
+ args = payment[:StoredContractByHash][:args]
39
+ stored_contract_by_hash = Casper::Entity::StoredContractByHash.new(hash, entry_point, args)
40
+
41
+ args.each do |arg|
42
+ name1 = arg[0] # => "quantity"
43
+ clvalue_hash = arg[1] # => {:cl_type=>"I32", :bytes=>"e8030000", :parsed=>1000}
44
+ clvalue = build_cl_value(arg[1])
45
+ # puts clvalue
46
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
47
+ end
48
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::StoredContractByHash.new(name, entry_point, temp_args).to_bytes)
49
+ result += temp
50
+ temp = nil
51
+ elsif payment.keys[0] == :StoredContractByName
52
+ temp_args = []
53
+ name = payment[:StoredContractByName][:name]
54
+ entry_point = payment[:StoredContractByName][:entry_point]
55
+ args = payment[:StoredContractByName][:args]
56
+
57
+ stored_contract_by_name = Casper::Entity::StoredContractByName.new(name, entry_point, args)
58
+ args.each do |arg|
59
+ name1 = arg[0] # => "quantity"
60
+ clvalue_hash = arg[1] # => {:cl_type=>"I32", :bytes=>"e8030000", :parsed=>1000}
61
+ clvalue = build_cl_value(arg[1])
62
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
63
+ end
64
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::StoredContractByName.new(name, entry_point, temp_args).to_bytes)
65
+ result += temp
66
+ temp = nil
67
+ elsif payment.keys[0] == :StoredVersionedContractByHash
68
+ temp_args = []
69
+ hash = payment[:StoredVersionedContractByHash][:name]
70
+ version = payment[:StoredVersionedContractByHash][:version]
71
+ entry_point = payment[:StoredVersionedContractByHash][:entry_point]
72
+ args = payment[:StoredVersionedContractByHash][:args]
73
+
74
+ stored_versioned_contract_by_hash = Casper::Entity::StoredVersionedContractByHash.new(hash, version, entry_point, args)
75
+ args.each do |arg|
76
+ name1 = arg[0]
77
+ clvalue_hash = arg[1]
78
+ clvalue = build_cl_value(arg[1])
79
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
80
+ end
81
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::StoredVersionedContractByHash.new(name, version,entry_point, temp_args).to_bytes)
82
+ result += temp
83
+ temp = nil
84
+ elsif payment.keys[0] == :StoredVersionedContractByName
85
+ temp_args = []
86
+ name = payment[:StoredVersionedContractByName][:name]
87
+ version = payment[:StoredVersionedContractByName][:version]
88
+ entry_point = payment[:StoredVersionedContractByName][:entry_point]
89
+ args = payment[:StoredVersionedContractByName][:args]
90
+
91
+ stored_versioned_contract_by_name = Casper::Entity::StoredVersionedContractByName.new(name, version, entry_point, args)
92
+ args.each do |arg|
93
+ name1 = arg[0]
94
+ clvalue_hash = arg[1]
95
+ clvalue = build_cl_value(arg[1])
96
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
97
+ end
98
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::StoredVersionedContractByName.new(name, version,entry_point, temp_args).to_bytes)
99
+ result += temp
100
+ temp = nil
101
+ elsif payment.keys[0] == :Transfer
102
+ temp_args = []
103
+ args = payment[:Transfer][:args]
104
+
105
+ transfer = Casper::Entity::DeployExecutableTransfer.new(args)
106
+ args.each do |arg|
107
+ name1 = arg[0] # => "amount"
108
+ clvalue_hash = arg[1] # => {:cl_type=>"I32", :bytes=>"e8030000", :parsed=>1000}
109
+ clvalue = build_cl_value(arg[1])
110
+ # puts clvalue
111
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
112
+ end
113
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::DeployExecutableTransfer.new(temp_args).to_bytes)
114
+ result += temp
115
+ temp = nil
116
+ end
117
+
118
+ if session.keys[0] == :ModuleBytes
119
+ temp_args = []
120
+ module_bytes = session[:ModuleBytes][:module_bytes]
121
+ args = session[:ModuleBytes][:args]
122
+ module_bytes = Casper::Entity::ModuleBytes.new(module_bytes, args)
123
+ args.each do |arg|
124
+ name1 = arg[0]
125
+ clvalue_hash = arg[1]
126
+ clvalue = build_cl_value(arg[1])
127
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
128
+ end
129
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::ModuleBytes.new(module_bytes, temp_args).to_bytes)
130
+ result += temp
131
+ temp = nil
132
+ elsif session.keys[0] == :StoredContractByHash
133
+ temp_args = []
134
+ hash = session[:StoredContractByHash][:hash]
135
+ entry_point = session[:StoredContractByHash][:entry_point]
136
+ args = session[:StoredContractByHash][:args]
137
+ stored_contract_by_hash = Casper::Entity::StoredContractByHash.new(hash, entry_point, args)
138
+
139
+ args.each do |arg|
140
+ name1 = arg[0] # => "quantity"
141
+ clvalue_hash = arg[1] # => {:cl_type=>"I32", :bytes=>"e8030000", :parsed=>1000}
142
+ clvalue = build_cl_value(arg[1])
143
+ # puts clvalue
144
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
145
+ end
146
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::StoredContractByHash.new(name, entry_point, temp_args).to_bytes)
147
+ result += temp
148
+ temp = nil
149
+ elsif session.keys[0] == :StoredContractByName
150
+ temp_args = []
151
+ name = session[:StoredContractByName][:name]
152
+ entry_point = session[:StoredContractByName][:entry_point]
153
+ args = session[:StoredContractByName][:args]
154
+
155
+ stored_contract_by_name = Casper::Entity::StoredContractByName.new(name, entry_point, args)
156
+ args.each do |arg|
157
+ name1 = arg[0] # => "quantity"
158
+ clvalue_hash = arg[1] # => {:cl_type=>"I32", :bytes=>"e8030000", :parsed=>1000}
159
+ clvalue = build_cl_value(arg[1])
160
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
161
+ end
162
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::StoredContractByName.new(name, entry_point, temp_args).to_bytes)
163
+ result += temp
164
+ temp = nil
165
+ elsif session.keys[0] == :StoredVersionedContractByHash
166
+ temp_args = []
167
+ hash = session[:StoredVersionedContractByHash][:name]
168
+ version = session[:StoredVersionedContractByHash][:version]
169
+ entry_point = session[:StoredVersionedContractByHash][:entry_point]
170
+ args = session[:StoredVersionedContractByHash][:args]
171
+
172
+ stored_versioned_contract_by_hash = Casper::Entity::StoredVersionedContractByHash.new(hash, version, entry_point, args)
173
+ args.each do |arg|
174
+ name1 = arg[0]
175
+ clvalue_hash = arg[1]
176
+ clvalue = build_cl_value(arg[1])
177
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
178
+ end
179
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::StoredVersionedContractByHash.new(name, version,entry_point, temp_args).to_bytes)
180
+ result += temp
181
+ temp = nil
182
+ elsif session.keys[0] == :StoredVersionedContractByName
183
+ temp_args = []
184
+ name = session[:StoredVersionedContractByName][:name]
185
+ version = session[:StoredVersionedContractByName][:version]
186
+ entry_point = session[:StoredVersionedContractByName][:entry_point]
187
+ args = session[:StoredVersionedContractByName][:args]
188
+
189
+ stored_versioned_contract_by_name = Casper::Entity::StoredVersionedContractByName.new(name, version, entry_point, args)
190
+ args.each do |arg|
191
+ name1 = arg[0]
192
+ clvalue_hash = arg[1]
193
+ clvalue = build_cl_value(arg[1])
194
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
195
+ end
196
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::StoredVersionedContractByName.new(name, version,entry_point, temp_args).to_bytes)
197
+ result += temp
198
+ temp = nil
199
+
200
+ elsif session.keys[0] == :Transfer
201
+ temp_args = []
202
+ args = session[:Transfer][:args]
203
+
204
+ transfer = Casper::Entity::DeployExecutableTransfer.new(args)
205
+ args.each do |arg|
206
+ name1 = arg[0] # => "amount"
207
+ clvalue_hash = arg[1] # => {:cl_type=>"I32", :bytes=>"e8030000", :parsed=>1000}
208
+ clvalue = build_cl_value(arg[1])
209
+ # puts clvalue
210
+ temp_args << [Casper::Entity::DeployNamedArgument.new(name1, clvalue)]
211
+ end
212
+ temp = Utils::ByteUtils.byte_array_to_hex(Casper::Entity::DeployExecutableTransfer.new(temp_args).to_bytes)
213
+ result += temp
214
+ temp = nil
215
+ end
216
+
217
+ approvals = deploy.get_approvals
218
+ num_of_approvals = approvals.size
219
+ deploy_approval_serializer = DeployApprovalSerializer.new
220
+ result += Utils::ByteUtils.to_u32(num_of_approvals)
221
+ for approval in approvals
222
+ deploy_approval = Casper::Entity::DeployApproval.new(approval)
223
+ result += deploy_approval_serializer.to_bytes(deploy_approval)
224
+ end
225
+ # result
226
+ Utils::ByteUtils.hex_to_byte_array(result)
227
+ end
228
+
229
+ def build_cl_value(h = {})
230
+ cl_type = h[:cl_type]
231
+ bytes = h[:bytes]
232
+ parsed = h[:parsed]
233
+ if cl_type == "Bool"
234
+ CLValueBytesParsers::CLBoolBytesParser.from_bytes([bytes])
235
+ elsif cl_type == "I32"
236
+ value = Utils::ByteUtils.hex_to_integer(bytes)
237
+ CLi32.new(value)
238
+ elsif cl_type == "I64"
239
+ value = Utils::ByteUtils.hex_to_i64_value(bytes)
240
+ CLi64.new(value)
241
+ elsif cl_type == "U8"
242
+ value = Utils::ByteUtils.hex_to_u8_value(bytes)
243
+ CLu8.new(value)
244
+ elsif cl_type == "U32"
245
+ value = Utils::ByteUtils.hex_to_u32_value(bytes)
246
+ CLu32.new(value)
247
+ elsif cl_type == "U64"
248
+ value = Utils::ByteUtils.hex_to_u64_value(bytes)
249
+ CLu32.new(value)
250
+ elsif cl_type == "Unit"
251
+ # value = CLValueBytesParsers::CLUnitBytesParser.from_bytes(bytes)
252
+ if bytes == ""
253
+ value = nil
254
+ CLUnit.new(value)
255
+ end
256
+ elsif cl_type == "String"
257
+ value = CLValueBytesParsers::CLStringBytesParser.from_bytes(bytes)
258
+ CLString.new(value)
259
+ elsif cl_type == "URef"
260
+ value = Utils::ByteUtils.hex_to_byte_array(bytes)
261
+ CLValueBytesParsers::CLURefBytesParser.from_bytes(value)
262
+ elsif cl_type == "PublicKey"
263
+ CLPublicKey.from_hex(bytes)
264
+ end
265
+
266
+
267
+ end
268
+ end