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,120 @@
1
+ require_relative './cl_uref_type.rb'
2
+ require_relative './cl_value.rb'
3
+ require_relative '../serialization/cl_value_bytes_parsers.rb'
4
+ require 'json'
5
+
6
+
7
+ # Access Rights
8
+ # https://docs.casperlabs.io/design/serialization-standard/#clvalue-uref
9
+
10
+
11
+ class CLURef < CLValue
12
+ include CLValueBytesParsers::CLURefBytesParser
13
+ attr_accessor :err
14
+ # @param [Array<Integer>] value
15
+ # @param [AccessRights] access_rights
16
+ # value is an uint8array
17
+ def initialize(value = nil, access_rights = nil)
18
+ super()
19
+ # raise ArgumentError.new('The length of URefAddr should be 32') unless value.length == 32
20
+ # rescue ArgumentError => e
21
+ # p e.message
22
+ begin
23
+ raise ArgumentError.new('The length of URefAddr should be 32') unless value.length == 32
24
+ rescue ArgumentError => e
25
+ e.message
26
+ end
27
+ begin
28
+ raise ArgumentError.new('Unsupported AccessRights') unless AccessRights.has_value?(access_rights)
29
+ rescue ArgumentError => e
30
+ e.message
31
+ end
32
+ # if value.length != 32
33
+ # raise ArgumentError.new('The length of URefAddr should be 32')
34
+ # end
35
+ # raise ArgumentError.new("Unsupported AccessRights") unless AccessRights.has_value?(access_rights)
36
+ # if value.length != 32
37
+ # begin
38
+ # raise Error.new('The length of URefAddr should be 32')
39
+ # rescue StandardError => e
40
+ # puts e
41
+ # end
42
+ # end
43
+ # if !(AccessRights.has_value?(access_rights))
44
+ # raise Error.new('Unsupported AccessRights')
45
+ # end
46
+ @value = value
47
+ @access_rights = access_rights
48
+
49
+ end
50
+
51
+ def get_cl_type
52
+ @cl_type = CLURefType.new
53
+ @cl_type.to_string
54
+ end
55
+
56
+ def get_value
57
+ @value
58
+ end
59
+
60
+ def get_access_rights
61
+ @access_rights
62
+ end
63
+
64
+ def get_size
65
+ @value.size
66
+ end
67
+
68
+ def self.parse_uref_address(str)
69
+ raise ArgumentError.new("Expected a string value of \'uref-\' ") unless str.start_with?("uref-")
70
+ raise ArgumentError.new("Expected a value of 3") unless str[0..str.length-1].split('-', 3).size == 3
71
+ raise ArgumentError.new("Expected a value of 32") unless str[0..str.length-1].split('-', 3)[1].length/2 == 32
72
+
73
+ arr = str[0..str.length-1].split('-', 3)
74
+ prefix = arr[0]
75
+ uref_addr = arr[1]
76
+ suffix = arr[2]
77
+ uref_byte_length = uref_addr.length / 2
78
+ decoded_addr = CLValueBytesParsers::CLURefBytesParser.decode_base_16(uref_addr)
79
+ access_rights = suffix.to_i(8)
80
+ # puts decoded_addr.inspect
81
+ # puts access_rights
82
+ raise ArgumentError.new("The value of \'access_rights\' is out of range. It must be >= 0 and <= 7. Received #{access_rights}") unless suffix.to_i(10).between?(0, 7)
83
+ CLURef.new(decoded_addr, access_rights)
84
+ end
85
+
86
+ # Example: str = "uref-000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f-007";
87
+ def to_bytes(str)
88
+ raise ArgumentError.new("Expected a string value of \'uref-\' ") unless str.start_with?("uref-")
89
+ raise ArgumentError.new("Expected a value of 3") unless str[0..str.length-1].split('-', 3).size == 3
90
+ raise ArgumentError.new("Expected a value of 32") unless str[0..str.length-1].split('-', 3)[1].length/2 == 32
91
+
92
+ arr = str[0..str.length-1].split('-', 3)
93
+ prefix = arr[0]
94
+ uref_addr = arr[1]
95
+ suffix = arr[2]
96
+ uref_byte_length = uref_addr.length / 2
97
+ access_rights = suffix.to_i(8)
98
+ raise ArgumentError.new("The value of \'access_rights\' is out of range. It must be >= 0 and <= 7. Received #{access_rights}") unless suffix.to_i(10).between?(0, 7)
99
+ uref_addr + [access_rights].pack("C*").unpack1("H*")
100
+ end
101
+
102
+ def self.to_json(uref)
103
+ decoded_addr = uref.get_value
104
+ # p decoded_addr
105
+ access_rights = uref.get_access_rights
106
+ decoded_addr_with_access_rights = CLValueBytesParsers::CLURefBytesParser.to_bytes(uref)
107
+ # p decoded_addr_with_access_rights
108
+ encoded_addr_with_access_rights = CLValueBytesParsers::CLURefBytesParser.encode_base_16(decoded_addr_with_access_rights)
109
+ # p encoded_addr_with_access_rights
110
+ json = {"bytes": encoded_addr_with_access_rights, "cl_type": uref.get_cl_type}.to_json
111
+ end
112
+
113
+ def self.from_json(json)
114
+ parsed = JSON.parse(json).deep_symbolize_keys
115
+ bytes = CLValueBytesParsers::CLURefBytesParser.decode_base_16(parsed[:bytes])
116
+ decoded = bytes[0...-1]
117
+ access_rights = bytes.pop
118
+ CLURef.new(decoded, access_rights)
119
+ end
120
+ end
@@ -0,0 +1,46 @@
1
+ require_relative './cl_type.rb'
2
+ require_relative './constants.rb'
3
+
4
+
5
+
6
+ class CLURefType < CLType
7
+ def initialize(value = nil)
8
+ super(value)
9
+ @value = value
10
+ end
11
+
12
+ def to_string
13
+ TAGS.key(12).to_s
14
+ end
15
+
16
+ def get_value
17
+ @value
18
+ end
19
+
20
+ def to_json
21
+ to_string
22
+ end
23
+
24
+ def to_bytes
25
+ [12].pack("C*").unpack1("H*")
26
+ end
27
+ end
28
+
29
+ AccessRights = {
30
+ # No permissions
31
+ None: 0b00000000,
32
+ # Permission to read the value under the associated [[URef]].
33
+ READ: 0b00000001,
34
+ # Permission to write a value under the associated [[URef]].
35
+ WRITE: 0b00000010,
36
+ # Permission to read or write the value under the associated [[URef]].
37
+ READ_WRITE: 0b00000011,
38
+ # Permission to add to the value under the associated [[URef]].
39
+ ADD: 0b00000100,
40
+ # # Permission to read or add to the value under the associated [[URef]].
41
+ READ_ADD: 0b00000101,
42
+ # # Permission to add to, or write the value under the associated [[URef]].
43
+ ADD_WRITE: 0b00000110,
44
+ # # Permission to read, add to, or write the value under the associated [[URef]].
45
+ READ_ADD_WRITE: 0b00000111
46
+ }
@@ -0,0 +1,10 @@
1
+ require_relative './cl_type.rb'
2
+
3
+ class CLValue
4
+
5
+ def initialize(bytes = nil, cl_type = nil, parsed = nil)
6
+ @bytes = bytes
7
+ @cl_type = cl_type
8
+ @parsed = parsed
9
+ end
10
+ end
@@ -0,0 +1,50 @@
1
+ MIN_I32 = -2147483648
2
+ MAX_I32 = 2147483647
3
+
4
+ MIN_I64 = -9223372036854775808
5
+ MAX_I64 = 9223372036854775807
6
+
7
+ MIN_U8 = 0
8
+ MAX_U8 = 255
9
+
10
+ MIN_U32 = 0
11
+ MAX_U32 = 2.pow(32) - 1
12
+
13
+ MIN_U64 = 0
14
+ MAX_U64 = 2.pow(64) - 1
15
+
16
+ MIN_U128 = 0
17
+ MAX_U128 = 2.pow(128) - 1
18
+
19
+ MIN_U256 = 0
20
+ MAX_U256 = 2.pow(256) - 1
21
+
22
+ MIN_U512 = 0
23
+ MAX_U512 = 2.pow(512) - 1
24
+
25
+
26
+ BOOL_ID = 'Bool';
27
+ KEY_ID = 'Key';
28
+ PUBLIC_KEY_ID = 'PublicKey';
29
+ STRING_ID = 'String';
30
+ UREF_ID = 'URef';
31
+ UNIT_ID = 'Unit';
32
+ I32_ID = 'I32';
33
+ I64_ID = 'I64';
34
+ U8_ID = 'U8';
35
+ U32_ID = 'U32';
36
+ U64_ID = 'U64';
37
+ U128_ID = 'U128';
38
+ U256_ID = 'U256';
39
+ U512_ID = 'U512';
40
+
41
+ BYTE_ARRAY_ID = 'ByteArray';
42
+ LIST_ID = 'List';
43
+ MAP_ID = 'Map';
44
+ OPTION_ID = 'Option';
45
+ RESULT_ID = 'Result';
46
+ TUPLE1_ID = 'Tuple1';
47
+ TUPLE2_ID = 'Tuple2';
48
+ TUPLE3_ID = 'Tuple3';
49
+
50
+ ACCOUNT_HASH_ID = 'AccountHash';
@@ -0,0 +1,7 @@
1
+ class Error < StandardError
2
+ attr_reader :err
3
+ def initialize(err)
4
+ @err = err
5
+ super(err)
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module Utils
2
+ module Base16
3
+ module_function
4
+
5
+ # @param [Array<Integer>] byte_array
6
+ # @return [String] encoded
7
+ def encode16(byte_array)
8
+ encoded = byte_array.pack("C*").unpack("H*").first
9
+ end
10
+
11
+ # @param [String] str
12
+ # @return [Array<Integer>] decoded
13
+ def decode16(str)
14
+ decoded = [str].pack('H*').unpack("C*")
15
+ end
16
+ end
17
+
18
+ end
@@ -0,0 +1,107 @@
1
+ module Utils
2
+ module ByteUtils
3
+ extend self
4
+
5
+ # @param [String] str
6
+ # @return [String]
7
+ def string_to_hex(str)
8
+ # str.unpack1("H*")
9
+ str.unpack("H*").first
10
+ end
11
+
12
+ # @param [String] hex_str
13
+ # @return [String]
14
+ def hex_to_string(hex_str)
15
+ [hex_str].pack("H*")
16
+ end
17
+
18
+ # @param [String] hex_str
19
+ # @return [Array]
20
+ def hex_to_byte_array(hex_str)
21
+ [hex_str].pack("H*").unpack("C*")
22
+ end
23
+
24
+ # @param [Array] byte_array
25
+ # @return [String]
26
+ def byte_array_to_hex(byte_array)
27
+ # byte_array.pack("C*").unpack1("H*")
28
+ byte_array.pack("C*").unpack("H*").first
29
+ end
30
+
31
+ # @param [String] str
32
+ # @return [Array]
33
+ def string_to_byte_array(str)
34
+ str.unpack("C*")
35
+ end
36
+
37
+ # @param [Array] byte_array
38
+ # @return [String]
39
+ def byte_array_to_string(byte_array)
40
+ byte_array.pack("C*")
41
+ end
42
+
43
+ # @param [Integer] n
44
+ # @return [String]
45
+ def integer_to_hex(n)
46
+ [n].pack("l<*").unpack("H*").first
47
+ end
48
+
49
+ # @param [String] hex_str
50
+ # @return [Integer]
51
+ def hex_to_integer(hex_str)
52
+ [hex_str].pack("H*").unpack("l").first
53
+ end
54
+
55
+ def to_i32(n)
56
+ # [value].pack("l<*").unpack("C*")
57
+ [n].pack("l<*").unpack("H*").first
58
+ # [n].pack("l<*").unpack1("H*")
59
+ end
60
+
61
+ def to_i64(n)
62
+ # [value].pack("l<*").unpack("C*")
63
+ [n].pack("q<*").unpack("H*").first
64
+ # [n].pack("l<*").unpack1("H*")
65
+ end
66
+
67
+ def to_u8(n)
68
+ [n].pack("C").unpack1("H*")
69
+ end
70
+ def to_u32(n)
71
+ [n].pack("L<*").unpack("H*").first
72
+ # [n].pack("L<*").unpack1("H*")
73
+ end
74
+ # @param [Integer] n
75
+ # @return [String]
76
+ def to_u64(n)
77
+ [n].pack("Q<*").unpack("H*").first
78
+ end
79
+
80
+ # @param [String] hex_str
81
+ # @return [String]
82
+ def hex_from_little_endian_to_big_endian(hex_str)
83
+ # [hex_str].pack("H*").unpack('N*').pack('V*').unpack1('H*')
84
+ [hex_str].pack("H*").unpack('N*').pack('V*').unpack('H*').first
85
+ end
86
+
87
+ def hex_to_i32_value(hex_str)
88
+ [hex_str].pack("H*").unpack("l*").first
89
+ end
90
+
91
+ def hex_to_i64_value(hex_str)
92
+ [hex_str].pack("H*").unpack("q*").first
93
+ end
94
+
95
+ def hex_to_u8_value(hex_str)
96
+ [hex_str].pack("H*").unpack("C*").first
97
+ end
98
+
99
+ def hex_to_u32_value(hex_str)
100
+ [hex_str].pack("H*").unpack("L*").first
101
+ end
102
+
103
+ def hex_to_u64_value(hex_str)
104
+ [hex_str].pack("H*").unpack("Q*").first
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,53 @@
1
+ require_relative '../serialization/cl_value_bytes_parsers.rb'
2
+
3
+ def find_byte_parser_by_cl_type(type, value)
4
+ if type == "Bool"
5
+ CLValueBytesParsers::CLBoolBytesParser.to_bytes(value)
6
+ elsif type == "I32"
7
+ CLValueBytesParsers::CLI32BytesParser.to_bytes(value)
8
+ elsif type == "I64"
9
+ CLValueBytesParsers::CLI64BytesParser.to_bytes(value)
10
+ elsif type == "U8"
11
+ CLValueBytesParsers::CLU8BytesParser.to_bytes(value)
12
+ elsif type == "U32"
13
+ CLValueBytesParsers::CLU32BytesParser.to_bytes(value)
14
+ elsif type == "U64"
15
+ CLValueBytesParsers::CLU64BytesParser.to_bytes(value)
16
+ elsif type == "U128"
17
+ CLValueBytesParsers::CLU128BytesParser.to_bytes(value)
18
+ elsif type == "U256"
19
+ CLValueBytesParsers::CLU256BytesParser.to_bytes(value)
20
+ elsif type == "U512"
21
+ CLValueBytesParsers::CLU512BytesParser.to_bytes(value)
22
+ elsif type == "Unit"
23
+ CLValueBytesParsers::CLUnitBytesParser.to_bytes(value)
24
+ elsif type == "String"
25
+ CLValueBytesParsers::CLStringBytesParser.to_bytes(value)
26
+ elsif type == "Key"
27
+ CLValueBytesParsers::CLKeyBytesParser.to_bytes(value)
28
+ elsif type == "URef"
29
+ CLValueBytesParsers::CLURefBytesParser.to_bytes(value)
30
+ elsif type == "Option"
31
+ CLValueBytesParsers::CLOptionBytesParser.to_bytes(value)
32
+ elsif type == "List"
33
+ CLValueBytesParsers::CLListBytesParser.to_bytes(value)
34
+ elsif type == "ByteArray"
35
+ CLValueBytesParsers::CLByteArrayBytesParser.to_bytes(value)
36
+ elsif type == "Result"
37
+ CLValueBytesParsers::CLResultBytesParser.to_bytes(value)
38
+ elsif type == "Map"
39
+ CLValueBytesParsers::CLMapBytesParser.to_bytes(value)
40
+ elsif type == "Tuple1"
41
+ CLValueBytesParsers::CLTupleBytesParser.to_bytes(value)
42
+ elsif type == "Tuple2"
43
+ CLValueBytesParsers::CLTupleBytesParser.to_bytes(value)
44
+ elsif type == "Tuple3"
45
+ CLValueBytesParsers::CLTupleBytesParser.to_bytes(value)
46
+ elsif type == "Any"
47
+ CLValueBytesParsers::CLAnyBytesParser.to_bytes(value)
48
+ elsif type == "PublicKey"
49
+ CLValueBytesParsers::CLPublicKeyBytesParser.to_bytes(value)
50
+ else
51
+ "Undefined type"
52
+ end
53
+ end
@@ -0,0 +1,19 @@
1
+ require 'blake2'
2
+
3
+ module Utils
4
+ module HashUtils
5
+ module_function
6
+
7
+ def byte_hash(arr)
8
+ str = arr.pack("C*").unpack("H*").first
9
+ key = Blake2::Key.none
10
+ Blake2.bytes(str, key, 32)
11
+ end
12
+
13
+ def account_hash_from_byte_to_hex(account_hash_byte_array)
14
+ prefix = "account-hash-"
15
+ hash_hex = account_hash_byte_array.pack("C*").unpack("H*").first
16
+ prefix + hash_hex
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module Utils
2
+ module HexUtils
3
+ module_function
4
+
5
+ # @param [String] public_key_hex
6
+ # @return [Boolean]
7
+ def valid_public_key_format?(public_key_hex)
8
+ # !!"01f9235ff9c46c990e1e2eee0d531e488101fab48c05b75b8ea9983658e228f06b".match(/^0(1[0-9a-fA-F]{64}|2[0-9a-fA-F]{66})$/)
9
+ !!public_key_hex.match(/^0(1[0-9a-fA-F]{64}|2[0-9a-fA-F]{66})$/)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,85 @@
1
+ require 'date'
2
+ require 'time'
3
+
4
+ module Utils
5
+ module TimeUtils
6
+ extend self
7
+
8
+ # Convert iso datetime to ms from unix epoch
9
+ #
10
+ # @param [String] timestamp
11
+ # @return [Integer]
12
+ def to_epoc_ms(timestamp)
13
+ DateTime.parse( ).strftime("%Q")
14
+ DateTime.parse(timestamp).strftime("%Q").to_i
15
+ end
16
+
17
+ # Converts milliseconds to timestamp
18
+ def to_iso_string(milliseconds)
19
+ milliseconds -= 3 * 60 * 60 * 1000
20
+ Time.at(milliseconds/1000.0).strftime('%Y-%m-%dT%H:%M:%S.%3NZ')
21
+ end
22
+
23
+ # Convert ttl into milliseconds
24
+ # @param [String] ttl
25
+ # @return [Integer]
26
+ def ttl_to_milliseconds(ttl)
27
+ arr = ttl.split(' ')
28
+ total_time = 0
29
+ n = 1
30
+ for item in arr do
31
+ if item.include?("h")
32
+ n = 60 * 60 * 1000
33
+ elsif item.include?("m") && !item.include?("ms")
34
+ n = 60 * 1000
35
+ elsif item.include?("s") && !item.include?("ms")
36
+ n = 1000
37
+ elsif item.include?("ms")
38
+ n = 1
39
+ elsif item.include?("d")
40
+ n = 24 * 60 * 60 * 1000
41
+ elsif item.include?("day")
42
+ n = 24 * 60 * 60 * 1000
43
+ end
44
+ value = item.gsub(/[^0-9,.]/, "").to_i
45
+ value *= n
46
+ total_time += value
47
+ n = 1
48
+ end
49
+ total_time
50
+ end
51
+
52
+ # Milliseconds to String TTL
53
+ #
54
+ # @param [Integer] milliseconds
55
+ # @return [String]
56
+ def milliseconds_to_ttl(milliseconds)
57
+ return '' unless milliseconds
58
+ days, milliseconds = milliseconds.divmod(1000 * 60 * 60 * 24)
59
+ hours, milliseconds = milliseconds.divmod(1000 * 60 * 60)
60
+ minutes, milliseconds = milliseconds.divmod(1000 * 60)
61
+ seconds, milliseconds = milliseconds.divmod(1000)
62
+ str = "#{days}d #{hours}h #{minutes}m #{seconds}s #{milliseconds}ms"
63
+ arr = str.split(" ")
64
+ new_str = ""
65
+ for item in arr do
66
+ if item[0] != '0'
67
+ new_str += item + " "
68
+ end
69
+ end
70
+ new_str.rstrip
71
+ end
72
+
73
+ def get_duration_hrs_mins_secs_ms(milliseconds)
74
+ return '' unless milliseconds
75
+ hours, milliseconds = milliseconds.divmod(1000 * 60 * 60)
76
+ minutes, milliseconds = milliseconds.divmod(1000 * 60)
77
+ seconds, milliseconds = milliseconds.divmod(1000)
78
+ "#{hours}h #{minutes}m #{seconds}s #{milliseconds}ms"
79
+ rescue
80
+ ""
81
+ end
82
+
83
+ end
84
+ end
85
+
@@ -0,0 +1,2 @@
1
+ module Utils
2
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module CasperNetworkSDK
2
+ VERSION = '1.0.0'
3
+ end