test_sdk1 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +1 -0
- data/LICENSE +201 -0
- data/README.md +147 -0
- data/SECURITY.md +12 -0
- data/lib/crypto/00_asymmetric_key.rb +95 -0
- data/lib/crypto/01_ed25519.rb +67 -0
- data/lib/crypto/asymmetric_key.rb +87 -0
- data/lib/crypto/ed25519_key.rb +44 -0
- data/lib/crypto/key_pair.rb +40 -0
- data/lib/crypto/keys.rb +7 -0
- data/lib/crypto/secp256k1_key.rb +0 -0
- data/lib/crypto/test_ed25519_key.rb +44 -0
- data/lib/entity/account.rb +45 -0
- data/lib/entity/action_thresholds.rb +25 -0
- data/lib/entity/associated_key.rb +24 -0
- data/lib/entity/auction_state.rb +36 -0
- data/lib/entity/bid.rb +24 -0
- data/lib/entity/bid_info.rb +51 -0
- data/lib/entity/contract.rb +47 -0
- data/lib/entity/contract_package.rb +41 -0
- data/lib/entity/contract_version.rb +33 -0
- data/lib/entity/delegator.rb +37 -0
- data/lib/entity/deploy.rb +44 -0
- data/lib/entity/deploy_approval.rb +24 -0
- data/lib/entity/deploy_executable.rb +162 -0
- data/lib/entity/deploy_executable_item_internal.rb +26 -0
- data/lib/entity/deploy_executable_transfer.rb +50 -0
- data/lib/entity/deploy_hash.rb +16 -0
- data/lib/entity/deploy_header.rb +60 -0
- data/lib/entity/deploy_info.rb +44 -0
- data/lib/entity/deploy_named_argument.rb +19 -0
- data/lib/entity/deploy_transfer.rb +10 -0
- data/lib/entity/disabled_version.rb +26 -0
- data/lib/entity/era_info.rb +18 -0
- data/lib/entity/era_summary.rb +44 -0
- data/lib/entity/era_validator.rb +23 -0
- data/lib/entity/executable_deploy_item.rb +11 -0
- data/lib/entity/group.rb +25 -0
- data/lib/entity/module_bytes.rb +50 -0
- data/lib/entity/peer.rb +25 -0
- data/lib/entity/seigniorage_allocation.rb +18 -0
- data/lib/entity/stored_contract_by_hash.rb +50 -0
- data/lib/entity/stored_contract_by_name.rb +50 -0
- data/lib/entity/stored_value.rb +57 -0
- data/lib/entity/stored_versioned_contract_by_hash.rb +61 -0
- data/lib/entity/stored_versioned_contract_by_name.rb +61 -0
- data/lib/entity/transfer.rb +65 -0
- data/lib/entity/validator_weight.rb +23 -0
- data/lib/entity/vesting_schedule.rb +22 -0
- data/lib/include.rb +18 -0
- data/lib/rpc/rpc.rb +227 -0
- data/lib/rpc/rpc_client.rb +233 -0
- data/lib/rpc/rpc_error.rb +79 -0
- data/lib/serialization/cl_type_serializer.rb +76 -0
- data/lib/serialization/cl_value_bytes_parsers.rb +498 -0
- data/lib/serialization/cl_value_serializer.rb +259 -0
- data/lib/serialization/deploy_approval_serializer.rb +15 -0
- data/lib/serialization/deploy_executable_serializer.rb +27 -0
- data/lib/serialization/deploy_header_serializer.rb +49 -0
- data/lib/serialization/deploy_named_arg_serializer.rb +19 -0
- data/lib/serialization/deploy_serializer.rb +268 -0
- data/lib/serialization/test.rb +431 -0
- data/lib/test_sdk1.rb +289 -0
- data/lib/types/cl_account_hash.rb +24 -0
- data/lib/types/cl_account_hash_type.rb +22 -0
- data/lib/types/cl_any.rb +25 -0
- data/lib/types/cl_any_type.rb +22 -0
- data/lib/types/cl_bool.rb +32 -0
- data/lib/types/cl_bool_type.rb +35 -0
- data/lib/types/cl_byte_array.rb +25 -0
- data/lib/types/cl_byte_array_type.rb +27 -0
- data/lib/types/cl_i32.rb +26 -0
- data/lib/types/cl_i32_type.rb +26 -0
- data/lib/types/cl_i64.rb +26 -0
- data/lib/types/cl_i64_type.rb +27 -0
- data/lib/types/cl_key.rb +39 -0
- data/lib/types/cl_key_type.rb +27 -0
- data/lib/types/cl_list.rb +25 -0
- data/lib/types/cl_list_type.rb +26 -0
- data/lib/types/cl_map.rb +25 -0
- data/lib/types/cl_map_type.rb +26 -0
- data/lib/types/cl_option.rb +33 -0
- data/lib/types/cl_option_type.rb +52 -0
- data/lib/types/cl_public_key.rb +152 -0
- data/lib/types/cl_public_key_type.rb +26 -0
- data/lib/types/cl_result.rb +25 -0
- data/lib/types/cl_result_type.rb +26 -0
- data/lib/types/cl_string.rb +39 -0
- data/lib/types/cl_string_type.rb +32 -0
- data/lib/types/cl_tuple.rb +151 -0
- data/lib/types/cl_tuple_type.rb +108 -0
- data/lib/types/cl_type.rb +94 -0
- data/lib/types/cl_type_tag.rb +51 -0
- data/lib/types/cl_u128.rb +26 -0
- data/lib/types/cl_u128_type.rb +26 -0
- data/lib/types/cl_u256.rb +26 -0
- data/lib/types/cl_u256_type.rb +26 -0
- data/lib/types/cl_u32.rb +26 -0
- data/lib/types/cl_u32_type.rb +26 -0
- data/lib/types/cl_u512.rb +26 -0
- data/lib/types/cl_u512_type.rb +26 -0
- data/lib/types/cl_u64.rb +26 -0
- data/lib/types/cl_u64_type.rb +27 -0
- data/lib/types/cl_u8.rb +26 -0
- data/lib/types/cl_u8_type.rb +26 -0
- data/lib/types/cl_unit.rb +38 -0
- data/lib/types/cl_unit_type.rb +22 -0
- data/lib/types/cl_uref.rb +120 -0
- data/lib/types/cl_uref_type.rb +46 -0
- data/lib/types/cl_value.rb +10 -0
- data/lib/types/constants.rb +50 -0
- data/lib/types/error.rb +7 -0
- data/lib/utils/base_16.rb +18 -0
- data/lib/utils/byte_utils.rb +107 -0
- data/lib/utils/find_byte_parser_by_cl_type.rb +53 -0
- data/lib/utils/hash_utils.rb +19 -0
- data/lib/utils/hex_utils.rb +12 -0
- data/lib/utils/time_utils.rb +85 -0
- data/lib/utils/utils.rb +2 -0
- data/lib/version.rb +3 -0
- data/spec/a_spec.rb +697 -0
- data/spec/byte_utils_spec.rb +72 -0
- data/spec/cl_public_spec.rb +169 -0
- data/spec/cl_types_spec.rb +715 -0
- data/spec/cl_value_serializer_spec.rb +140 -0
- data/spec/client_spec.rb +25 -0
- data/spec/crypto_spec.rb +42 -0
- data/spec/deploy_approval_serializer_spec.rb +26 -0
- data/spec/deploy_executable_serializer_spec.rb +0 -0
- data/spec/deploy_header_serializer_spec.rb +21 -0
- data/spec/deploy_named_arg_serializer_spec.rb +49 -0
- data/spec/deploy_serializer_spec.rb +77 -0
- data/spec/deploy_serializer_test_spec.rb +225 -0
- data/spec/mainnet_spec.rb +452 -0
- data/spec/spec_helper.rb +100 -0
- data/spec/string_spec.rb +68 -0
- data/spec/testnet_spec.rb +498 -0
- data/spec/time_utils_spec.rb +87 -0
- metadata +198 -0
@@ -0,0 +1,79 @@
|
|
1
|
+
|
2
|
+
module Casper
|
3
|
+
module RpcError
|
4
|
+
class ErrorHandle
|
5
|
+
# @param [Integer] code
|
6
|
+
# @param [String] message
|
7
|
+
# @param [String] data
|
8
|
+
def initialize(code = 0, message = "", data = "")
|
9
|
+
@code = code
|
10
|
+
@message = message
|
11
|
+
@data = data
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [String] err
|
15
|
+
def self.invalid_param
|
16
|
+
@code = -32602
|
17
|
+
@message = "Invalid params"
|
18
|
+
@err = "Server error #{@code}: #{@message}"
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param [String] url
|
22
|
+
# @return [String]
|
23
|
+
def error_handling(url)
|
24
|
+
begin
|
25
|
+
response = RestClient.get(url)
|
26
|
+
parsed = JSON.parse(response)
|
27
|
+
# p parsed
|
28
|
+
rescue RestClient::ResourceNotFound => e
|
29
|
+
e.class.inspect
|
30
|
+
# "ResourceNotFound"
|
31
|
+
rescue Errno::ECONNREFUSED => e
|
32
|
+
e.class.inspect
|
33
|
+
# p e.class
|
34
|
+
# "Errno::ECONNREFUSED"
|
35
|
+
rescue SocketError => e
|
36
|
+
e.class.inspect
|
37
|
+
# "Socket Error"
|
38
|
+
rescue
|
39
|
+
"Timed out connecting to server"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [String]
|
44
|
+
def invalid_address
|
45
|
+
"getaddrinfo: Name or service not known (SocketError)"
|
46
|
+
end
|
47
|
+
|
48
|
+
# @return [String]
|
49
|
+
def invalid_parameter
|
50
|
+
"Server error -32602: Invalid params"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class ServerError < StandardError
|
55
|
+
|
56
|
+
# @return [String]
|
57
|
+
def initialize(code, message)
|
58
|
+
super("Server error #{code}: #{message}")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class InvalidParameter
|
63
|
+
|
64
|
+
def initialize
|
65
|
+
end
|
66
|
+
|
67
|
+
# @return [String]
|
68
|
+
def self.error
|
69
|
+
"Server error -32602: Invalid params"
|
70
|
+
end
|
71
|
+
|
72
|
+
# @return [String]
|
73
|
+
def self.argument_error
|
74
|
+
"ArgumentError"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require_relative '../types/cl_bool_type.rb'
|
2
|
+
require_relative '../types/cl_bool.rb'
|
3
|
+
require_relative '../types/cl_i32_type.rb'
|
4
|
+
require_relative '../types/cl_i64_type.rb'
|
5
|
+
require_relative '../types/cl_u8_type.rb'
|
6
|
+
require_relative '../types/cl_u32_type.rb'
|
7
|
+
require_relative '../types/cl_u64_type.rb'
|
8
|
+
require_relative '../types/cl_u128_type.rb'
|
9
|
+
require_relative '../types/cl_u256_type.rb'
|
10
|
+
require_relative '../types/cl_u512_type.rb'
|
11
|
+
require_relative '../types/cl_unit_type.rb'
|
12
|
+
require_relative '../types/cl_string_type.rb'
|
13
|
+
require_relative '../types/cl_key_type.rb'
|
14
|
+
require_relative '../types/cl_option_type.rb'
|
15
|
+
require_relative '../types/cl_list_type.rb'
|
16
|
+
require_relative '../types/cl_byte_array_type.rb'
|
17
|
+
require_relative '../types/cl_result_type.rb'
|
18
|
+
require_relative '../types/cl_map_type.rb'
|
19
|
+
require_relative '../types/cl_tuple_type.rb'
|
20
|
+
require_relative '../types/cl_any_type.rb'
|
21
|
+
require_relative '../types/cl_public_key_type.rb'
|
22
|
+
|
23
|
+
class CLTypeSerizalizer
|
24
|
+
|
25
|
+
def self.serialize_cl_type(cl_type)
|
26
|
+
if cl_type.to_string == "Bool"
|
27
|
+
[0].pack("C*").unpack1("H*")
|
28
|
+
elsif cl_type.to_string == "I32"
|
29
|
+
[1].pack("C*").unpack1("H*")
|
30
|
+
elsif cl_type.to_string == "I64"
|
31
|
+
[2].pack("C*").unpack1("H*")
|
32
|
+
elsif cl_type.to_string == "U8"
|
33
|
+
[3].pack("C*").unpack1("H*")
|
34
|
+
elsif cl_type.to_string == "U32"
|
35
|
+
[4].pack("C*").unpack1("H*")
|
36
|
+
elsif cl_type.to_string == "U64"
|
37
|
+
[5].pack("C*").unpack1("H*")
|
38
|
+
elsif cl_type.to_string == "U128"
|
39
|
+
[6].pack("C*").unpack1("H*")
|
40
|
+
elsif cl_type.to_string == "U256"
|
41
|
+
[7].pack("C*").unpack1("H*")
|
42
|
+
elsif cl_type.to_string == "U512"
|
43
|
+
[8].pack("C*").unpack1("H*")
|
44
|
+
elsif cl_type.to_string == "Unit"
|
45
|
+
[9].pack("C*").unpack1("H*")
|
46
|
+
elsif cl_type.to_string == "String"
|
47
|
+
[10].pack("C*").unpack1("H*")
|
48
|
+
elsif cl_type.to_string == "Key"
|
49
|
+
[11].pack("C*").unpack1("H*")
|
50
|
+
elsif cl_type.to_string == "URef"
|
51
|
+
[12].pack("C*").unpack1("H*")
|
52
|
+
elsif cl_type.to_string == "Option"
|
53
|
+
[13].pack("C*").unpack1("H*") + serialize_cl_type(cl_type.get_inner_type)
|
54
|
+
elsif cl_type.to_string == "List"
|
55
|
+
[14].pack("C*").unpack1("H*")
|
56
|
+
elsif cl_type.to_string == "ByteArray"
|
57
|
+
[15].pack("C*").unpack1("H*")
|
58
|
+
elsif cl_type.to_string == "Result"
|
59
|
+
[16].pack("C*").unpack1("H*")
|
60
|
+
elsif cl_type.to_string == "Map"
|
61
|
+
[17].pack("C*").unpack1("H*")
|
62
|
+
elsif cl_type.to_string == "Tuple1"
|
63
|
+
[18].pack("C*").unpack1("H*") + cl_type.get_data[0].to_bytes
|
64
|
+
elsif cl_type.to_string == "Tuple2"
|
65
|
+
[19].pack("C*").unpack1("H*") + cl_type.get_data[0].to_bytes + cl_type.get_data[1].to_bytes
|
66
|
+
elsif cl_type.to_string == "Tuple3"
|
67
|
+
[20].pack("C*").unpack1("H*") + cl_type.get_data[0].to_bytes + cl_type.get_data[1].to_bytes + cl_type.get_data[2].to_bytes
|
68
|
+
elsif cl_type.to_string == "Any"
|
69
|
+
[21].pack("C*").unpack1("H*")
|
70
|
+
elsif cl_type.to_string == "PublicKey"
|
71
|
+
[22].pack("C*").unpack1("H*")
|
72
|
+
else
|
73
|
+
"Undefined"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,498 @@
|
|
1
|
+
require_relative '../types/constants.rb'
|
2
|
+
require_relative '../types/error.rb'
|
3
|
+
require 'active_support/core_ext/hash/keys'
|
4
|
+
|
5
|
+
class FalseClass; def to_i; 0 end end
|
6
|
+
class TrueClass; def to_i; 1 end end
|
7
|
+
|
8
|
+
module CLValueBytesParsers
|
9
|
+
|
10
|
+
module CLBoolBytesParser
|
11
|
+
extend self
|
12
|
+
def to_bytes(clbool)
|
13
|
+
value = clbool.get_value
|
14
|
+
if value == true || value == false
|
15
|
+
[value.to_i].pack("C").unpack("C")
|
16
|
+
else
|
17
|
+
"CLValue TypeError"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def from_bytes(bytes)
|
22
|
+
if bytes.first == 1
|
23
|
+
CLBool.new(true)
|
24
|
+
else
|
25
|
+
CLBool.new(false)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module CLStringBytesParser
|
31
|
+
extend self
|
32
|
+
# @param [String] raw_bytes
|
33
|
+
def from_bytes(raw_bytes)
|
34
|
+
first_4_bytes = raw_bytes[0..7]
|
35
|
+
string_length = [first_4_bytes].pack("H*").unpack("l").first
|
36
|
+
rest = raw_bytes[8..raw_bytes.length]
|
37
|
+
len2 = (rest.length) / 2
|
38
|
+
if string_length == len2
|
39
|
+
hex_to_string_value = [raw_bytes[8..raw_bytes.length]].pack("H*")
|
40
|
+
else
|
41
|
+
raise "Error"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# @param [String] value
|
46
|
+
def to_bytes(value)
|
47
|
+
len = value.length
|
48
|
+
hex1 = len.to_s(16).rjust(8, '0').scan(/../).reverse.join('')
|
49
|
+
hex2 = value.unpack("H*").first
|
50
|
+
hex1 + hex2
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_json
|
54
|
+
end
|
55
|
+
|
56
|
+
def from_json
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
module CLI32BytesParser
|
62
|
+
extend self
|
63
|
+
@@check = 0
|
64
|
+
def from_bytes(byte_array)
|
65
|
+
if @@check < 0
|
66
|
+
@@check = 0
|
67
|
+
bytes = byte_array.map { |b| b.chr }.join
|
68
|
+
bytes.unpack("B*").first.scan(/[01]{8}/)
|
69
|
+
bytes.unpack("l*").first
|
70
|
+
else
|
71
|
+
byte_array.reverse.inject(0) {|m, b| (m << 8) + b }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_bytes(clvalue)
|
76
|
+
value = clvalue.get_value
|
77
|
+
if value < 0 && value >= MIN_I32
|
78
|
+
@@check = value
|
79
|
+
[value].pack("l<*").unpack("C*")
|
80
|
+
elsif value >= 0 && value <= MAX_I32
|
81
|
+
[value].pack("l<*").unpack("C*")
|
82
|
+
else
|
83
|
+
"Parameter value '#{value}' is out of range."
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
module CLI64BytesParser
|
89
|
+
extend self
|
90
|
+
@@check = 0
|
91
|
+
def from_bytes(byte_array)
|
92
|
+
if @@check < 0
|
93
|
+
@@check = 0
|
94
|
+
bytes = byte_array.map { |b| b.chr }.join
|
95
|
+
bytes.unpack("B*").first.scan(/[01]{8}/)
|
96
|
+
bytes.reverse.unpack("q*").first
|
97
|
+
else
|
98
|
+
byte_array.reverse.inject(0) {|m, b| (m << 8) + b }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def to_bytes(value)
|
103
|
+
if value < 0 && value >= MIN_I64
|
104
|
+
@@check = value
|
105
|
+
[value].pack("q>*").unpack("C*")
|
106
|
+
elsif value >= 0 && value <= MAX_I64
|
107
|
+
[value].pack("q<*").unpack("C*")
|
108
|
+
else
|
109
|
+
"Parameter value '#{value}' is out of range."
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
module CLU8BytesParser
|
115
|
+
extend self
|
116
|
+
def from_bytes(byte_array)
|
117
|
+
byte_array.reverse.inject(0) {|m, b| (m << 8) + b }
|
118
|
+
end
|
119
|
+
|
120
|
+
def to_bytes(value)
|
121
|
+
if value < 0 || value > MAX_U8
|
122
|
+
@@check = value
|
123
|
+
"Parameter value '#{value}' is out of range."
|
124
|
+
else
|
125
|
+
[value].pack("C").unpack("C")
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
module CLU32BytesParser
|
131
|
+
extend self
|
132
|
+
def from_bytes(byte_array)
|
133
|
+
byte_array.reverse.inject(0) {|m, b| (m << 8) + b }
|
134
|
+
end
|
135
|
+
|
136
|
+
def to_bytes(value)
|
137
|
+
if value < 0 || value > MAX_U32
|
138
|
+
"Parameter value '#{value}' is out of range."
|
139
|
+
else
|
140
|
+
[value].pack("L<*").unpack("C*")
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
module CLU64BytesParser
|
146
|
+
extend self
|
147
|
+
def from_bytes(byte_array)
|
148
|
+
byte_array.reverse.inject(0) {|m, b| (m << 8) + b }
|
149
|
+
end
|
150
|
+
|
151
|
+
def to_bytes(value)
|
152
|
+
if value < 0 || value > MAX_U64
|
153
|
+
"Parameter value '#{value}' is out of range."
|
154
|
+
else
|
155
|
+
[value].pack("Q<*").unpack("C*")
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
module CLU128BytesParser
|
161
|
+
extend self
|
162
|
+
def from_bytes(byte_array)
|
163
|
+
byte_array.reverse.inject(0) {|m, b| (m << 8) + b }
|
164
|
+
end
|
165
|
+
|
166
|
+
def to_bytes(value)
|
167
|
+
if value < 0 || value > MAX_U128
|
168
|
+
"Parameter value '#{value}' is out of range."
|
169
|
+
else
|
170
|
+
str = value.to_s(16)
|
171
|
+
arr = str.scan(/[0-9a-f]{4}/).map { |x| x.to_i(16) }
|
172
|
+
packed = arr.pack("n*").unpack("C*").reverse()
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
module CLU256BytesParser
|
178
|
+
extend self
|
179
|
+
def from_bytes(byte_array)
|
180
|
+
byte_array.reverse.inject(0) {|m, b| (m << 8) + b }
|
181
|
+
end
|
182
|
+
|
183
|
+
def to_bytes(value)
|
184
|
+
if value < 0 || value > MAX_U256
|
185
|
+
"Parameter value '#{value}' is out of range."
|
186
|
+
else
|
187
|
+
str = value.to_s(16)
|
188
|
+
arr = str.scan(/[0-9a-f]{4}/).map { |x| x.to_i(16) }
|
189
|
+
packed = arr.pack("n*").unpack("C*").reverse()
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
module CLU512BytesParser
|
195
|
+
extend self
|
196
|
+
def from_bytes(byte_array)
|
197
|
+
byte_array.reverse.inject(0) {|m, b| (m << 8) + b }
|
198
|
+
end
|
199
|
+
|
200
|
+
def to_bytes(value)
|
201
|
+
if value < 0 || value > MAX_U512
|
202
|
+
"Parameter value '#{value}' is out of range."
|
203
|
+
else
|
204
|
+
str = value.to_s(16)
|
205
|
+
arr = str.scan(/[0-9a-f]{4}/).map { |x| x.to_i(16) }
|
206
|
+
packed = arr.pack("n*").unpack("C*").reverse()
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
module CLUnitBytesParser
|
212
|
+
extend self
|
213
|
+
def from_bytes(byte_array)
|
214
|
+
if byte_array.empty?
|
215
|
+
[].pack("")
|
216
|
+
else
|
217
|
+
"byte_array should be empty!"
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def to_bytes
|
222
|
+
"".bytes
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
module CLTupleBytesParser
|
227
|
+
extend self
|
228
|
+
def to_bytes(tuple)
|
229
|
+
# tuple_name can be Tuple1 or Tuple2 or Tuple3
|
230
|
+
tuple_name = tuple.get_tuple_name
|
231
|
+
size = tuple.get_size
|
232
|
+
data = tuple.get_value
|
233
|
+
if size == 1
|
234
|
+
type = data[0].get_cl_type
|
235
|
+
value = data[0]
|
236
|
+
convert_to_bytes(type, value)
|
237
|
+
elsif size == 2
|
238
|
+
# puts data[0]
|
239
|
+
# puts data[1]
|
240
|
+
clvalue1 = data[0]
|
241
|
+
type_of_clvalue1 = clvalue1.get_cl_type
|
242
|
+
clvalue2 = data[1]
|
243
|
+
type_of_clvalue2 = clvalue2.get_cl_type
|
244
|
+
convert_to_bytes(type_of_clvalue1, clvalue1) + convert_to_bytes(type_of_clvalue2, clvalue2)
|
245
|
+
# elsif size == 3
|
246
|
+
else
|
247
|
+
raise "error"
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def from_bytes(types, byte_array)
|
252
|
+
size = types.size
|
253
|
+
if size == 1
|
254
|
+
type = types[0]
|
255
|
+
convert_from_bytes(type, byte_array)
|
256
|
+
elsif size == 2
|
257
|
+
type_of_clvalue1 = types[0]
|
258
|
+
type_of_clvalue2 = types[1]
|
259
|
+
else
|
260
|
+
raise "error"
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
module CLKeyBytesParser
|
266
|
+
extend self
|
267
|
+
|
268
|
+
def to_bytes(cl_list)
|
269
|
+
|
270
|
+
end
|
271
|
+
|
272
|
+
def from_bytes(byte_array)
|
273
|
+
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
module CLURefBytesParser
|
278
|
+
extend self
|
279
|
+
|
280
|
+
# @param [String] str
|
281
|
+
# @return [Array<Integer>] decoded
|
282
|
+
def decode_base_16(str)
|
283
|
+
decoded = [str].pack('H*').unpack("C*")
|
284
|
+
end
|
285
|
+
|
286
|
+
# @param [Array<Integer>] byte_array
|
287
|
+
# @return [String] encoded
|
288
|
+
def encode_base_16(byte_array)
|
289
|
+
encoded = byte_array.pack("C*").unpack("H*").first
|
290
|
+
end
|
291
|
+
|
292
|
+
# @param [CLURef] clvalue
|
293
|
+
# @return [Array<Integer>] value
|
294
|
+
def to_bytes(clvalue)
|
295
|
+
value = clvalue.get_value
|
296
|
+
access_rights = clvalue.get_access_rights
|
297
|
+
value = value.dup
|
298
|
+
value.push(access_rights)
|
299
|
+
end
|
300
|
+
|
301
|
+
# @param [Array<Integer>] byte_array
|
302
|
+
# @return [CLURef] uref
|
303
|
+
def from_bytes(byte_array)
|
304
|
+
get_access_rights = byte_array.last
|
305
|
+
byte_array = byte_array[0..byte_array.size-2]
|
306
|
+
# p byte_array
|
307
|
+
uref = CLURef.new(byte_array, get_access_rights)
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
module CLOptionBytesParser
|
312
|
+
extend self
|
313
|
+
|
314
|
+
# @param [CLOption] cl_option
|
315
|
+
def to_bytes(cl_option)
|
316
|
+
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
module CLListBytesParser
|
321
|
+
extend self
|
322
|
+
|
323
|
+
def to_bytes(cl_list)
|
324
|
+
|
325
|
+
end
|
326
|
+
|
327
|
+
def from_bytes(byte_array)
|
328
|
+
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
module CLByteArrayBytesParser
|
333
|
+
extend self
|
334
|
+
|
335
|
+
def to_bytes(cl_byte_array)
|
336
|
+
|
337
|
+
end
|
338
|
+
|
339
|
+
def from_bytes(byte_array)
|
340
|
+
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
module CLResultBytesParser
|
345
|
+
extend self
|
346
|
+
|
347
|
+
def to_bytes(cl_result)
|
348
|
+
|
349
|
+
end
|
350
|
+
|
351
|
+
def from_bytes(byte_array)
|
352
|
+
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
module CLMapBytesParser
|
357
|
+
extend self
|
358
|
+
|
359
|
+
def to_bytes(cl_map)
|
360
|
+
|
361
|
+
end
|
362
|
+
|
363
|
+
def from_bytes(byte_array)
|
364
|
+
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
module CLAnyBytesParser
|
369
|
+
extend self
|
370
|
+
|
371
|
+
def to_bytes(cl_any)
|
372
|
+
|
373
|
+
end
|
374
|
+
|
375
|
+
def from_bytes(byte_array)
|
376
|
+
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
module CLPublicKeyBytesParser
|
381
|
+
extend self
|
382
|
+
# @param [String] str
|
383
|
+
# @return [Array<Integer>] decoded
|
384
|
+
def decode_base_16(str)
|
385
|
+
decoded = [str].pack('H*').unpack("C*")
|
386
|
+
end
|
387
|
+
|
388
|
+
# @param [Array<Integer>] byte_array
|
389
|
+
# @return [String] encoded
|
390
|
+
def encode_base_16(byte_array)
|
391
|
+
encoded = byte_array.pack("C*").unpack("H*").first
|
392
|
+
end
|
393
|
+
|
394
|
+
def to_bytes(cl_public_key)
|
395
|
+
tag = cl_public_key.get_cl_public_key_tag
|
396
|
+
raw_public_key = cl_public_key.get_value
|
397
|
+
[tag] + raw_public_key
|
398
|
+
end
|
399
|
+
|
400
|
+
def from_bytes(byte_array)
|
401
|
+
byte_array
|
402
|
+
# tag = byte_array[0]
|
403
|
+
tag = 0
|
404
|
+
if byte_array[0] == 1 && byte_array.size == 33
|
405
|
+
tag = 1
|
406
|
+
elsif byte_array[0] == 2 && byte_array.size == 34
|
407
|
+
tag = 2
|
408
|
+
else
|
409
|
+
raise ArgumentError.new("Invalid parameter")
|
410
|
+
end
|
411
|
+
CLPublicKey.new(byte_array[1..], tag)
|
412
|
+
end
|
413
|
+
end
|
414
|
+
end
|
415
|
+
|
416
|
+
|
417
|
+
def convert_to_bytes(type, value)
|
418
|
+
case type
|
419
|
+
when "Bool"
|
420
|
+
CLValueBytesParsers::CLBoolBytesParser.to_bytes(value)
|
421
|
+
when "I32"
|
422
|
+
CLValueBytesParsers::CLI32BytesParser.to_bytes(value)
|
423
|
+
when "I64"
|
424
|
+
CLValueBytesParsers::CLI64BytesParser.to_bytes(value)
|
425
|
+
when "U8"
|
426
|
+
CLValueBytesParsers::CLU8BytesParser.to_bytes(value)
|
427
|
+
when "U32"
|
428
|
+
CLValueBytesParsers::CLU32BytesParser.to_bytes(value)
|
429
|
+
when "U64"
|
430
|
+
CLValueBytesParsers::CLU64BytesParser.to_bytes(value)
|
431
|
+
when "U128"
|
432
|
+
CLValueBytesParsers::CLU128BytesParser.to_bytes(value)
|
433
|
+
when "U256"
|
434
|
+
CLValueBytesParsers::CLU256BytesParser.to_bytes(value)
|
435
|
+
when "U512"
|
436
|
+
CLValueBytesParsers::CLU512BytesParser.to_bytes(value)
|
437
|
+
when "Unit"
|
438
|
+
CLValueBytesParsers::CLUnitBytesParser.to_bytes(value)
|
439
|
+
when "String"
|
440
|
+
CLValueBytesParsers::CLStringBytesParser.to_bytes(value)
|
441
|
+
# when "Key"
|
442
|
+
# when "URef"
|
443
|
+
# when "Option"
|
444
|
+
# when "List"
|
445
|
+
# when "ByteArray"
|
446
|
+
# when "Result"
|
447
|
+
# when "Map"
|
448
|
+
# when "Tuple1"
|
449
|
+
# when "Tuple2"
|
450
|
+
# when "Tuple3"
|
451
|
+
# when "Any"
|
452
|
+
# when "PublicKey"
|
453
|
+
else
|
454
|
+
"Unknown"
|
455
|
+
end
|
456
|
+
end
|
457
|
+
|
458
|
+
def convert_from_bytes(type, byte_array)
|
459
|
+
# p type
|
460
|
+
case type
|
461
|
+
when "Bool"
|
462
|
+
CLValueBytesParsers::CLBoolBytesParser.from_bytes(byte_array)
|
463
|
+
when "I32"
|
464
|
+
CLValueBytesParsers::CLI32BytesParser.from_bytes(byte_array)
|
465
|
+
when "I64"
|
466
|
+
CLValueBytesParsers::CLI64BytesParser.to_bytes(value)
|
467
|
+
when "U8"
|
468
|
+
CLValueBytesParsers::CLU8BytesParser.to_bytes(value)
|
469
|
+
when "U32"
|
470
|
+
CLValueBytesParsers::CLU32BytesParser.to_bytes(value)
|
471
|
+
when "U64"
|
472
|
+
CLValueBytesParsers::CLU64BytesParser.to_bytes(value)
|
473
|
+
when "U128"
|
474
|
+
CLValueBytesParsers::CLU128BytesParser.to_bytes(value)
|
475
|
+
when "U256"
|
476
|
+
CLValueBytesParsers::CLU256BytesParser.to_bytes(value)
|
477
|
+
when "U512"
|
478
|
+
CLValueBytesParsers::CLU512BytesParser.to_bytes(value)
|
479
|
+
when "Unit"
|
480
|
+
CLValueBytesParsers::CLUnitBytesParser.to_bytes(value)
|
481
|
+
when "String"
|
482
|
+
CLValueBytesParsers::CLUnitBytesParser.to_bytes(value)
|
483
|
+
# when "Key"
|
484
|
+
# when "URef"
|
485
|
+
# when "Option"
|
486
|
+
# when "List"
|
487
|
+
# when "ByteArray"
|
488
|
+
# when "Result"
|
489
|
+
# when "Map"
|
490
|
+
# when "Tuple1"
|
491
|
+
# when "Tuple2"
|
492
|
+
# when "Tuple3"
|
493
|
+
# when "Any"
|
494
|
+
# when "PublicKey"
|
495
|
+
else
|
496
|
+
"Unknown"
|
497
|
+
end
|
498
|
+
end
|