Top Level Namespace
Defined Under Namespace
Modules: AddressCodec, BinaryCodec, Core, KeyPairs, Wallet, XRPL
Instance Method Summary collapse
-
#bin_to_hex(bin) ⇒ String
Converts a binary string to a hex string.
-
#bytes_to_hex(bytes) ⇒ String
Converts a byte array to a hex string.
-
#check_byte_length(bytes, expected_length) ⇒ Boolean
Checks if a byte array has the expected length.
-
#concat_args(*args) ⇒ Array
Concatenates multiple arguments into a single array.
-
#hex_to_bin(hex) ⇒ String
Converts a hex string to a binary string.
-
#hex_to_bytes(hex) ⇒ Array<Integer>
Converts a hex string to a byte array.
-
#hex_to_string(hex, encoding = 'utf-8') ⇒ String
Converts a hex string to a string with the given encoding.
-
#int_to_bytes(number, width = 1, byteorder = :big) ⇒ Array<Integer>
Converts an integer to a byte array.
-
#is_scalar?(val) ⇒ Boolean
Checks if a value is a scalar.
-
#random_bytes(size) ⇒ Array<Integer>
Returns a random byte array of the given size.
-
#string_to_hex(string) ⇒ String
Converts a string to a hex string.
-
#valid_hex?(str) ⇒ Boolean
Checks if a string is a valid hex string.
Instance Method Details
#bin_to_hex(bin) ⇒ String
Converts a binary string to a hex string.
29 30 31 |
# File 'lib/core/core.rb', line 29 def bin_to_hex(bin) bin.unpack("H*").first.upcase end |
#bytes_to_hex(bytes) ⇒ String
Converts a byte array to a hex string.
15 16 17 |
# File 'lib/core/core.rb', line 15 def bytes_to_hex(bytes) bytes.pack('C*').unpack1('H*').upcase end |
#check_byte_length(bytes, expected_length) ⇒ Boolean
Checks if a byte array has the expected length.
68 69 70 71 72 73 74 |
# File 'lib/core/core.rb', line 68 def check_byte_length(bytes, expected_length) if bytes.respond_to?(:byte_length) bytes.byte_length == expected_length else bytes.length == expected_length end end |
#concat_args(*args) ⇒ Array
Concatenates multiple arguments into a single array.
79 80 81 82 83 |
# File 'lib/core/core.rb', line 79 def concat_args(*args) args.flat_map do |arg| is_scalar?(arg) ? [arg] : arg.to_a end end |
#hex_to_bin(hex) ⇒ String
Converts a hex string to a binary string.
36 37 38 39 |
# File 'lib/core/core.rb', line 36 def hex_to_bin(hex) raise ArgumentError, 'Invalid hex string' unless valid_hex?(hex) [hex].pack("H*") end |
#hex_to_bytes(hex) ⇒ Array<Integer>
Converts a hex string to a byte array.
21 22 23 24 |
# File 'lib/core/core.rb', line 21 def hex_to_bytes(hex) raise ArgumentError, 'Invalid hex string' unless valid_hex?(hex) [hex].pack('H*').bytes end |
#hex_to_string(hex, encoding = 'utf-8') ⇒ String
Converts a hex string to a string with the given encoding.
45 46 47 48 |
# File 'lib/core/core.rb', line 45 def hex_to_string(hex, encoding = 'utf-8') raise ArgumentError, 'Invalid hex string' unless valid_hex?(hex) hex_to_bin(hex).force_encoding(encoding).encode('utf-8') end |
#int_to_bytes(number, width = 1, byteorder = :big) ⇒ Array<Integer>
Converts an integer to a byte array.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/core/core.rb', line 97 def int_to_bytes(number, width = 1, byteorder = :big) bytes = [] while number > 0 bytes << (number & 0xFF) number >>= 8 end while bytes.size < width bytes << 0 end bytes.reverse! if byteorder == :big bytes end |
#is_scalar?(val) ⇒ Boolean
Checks if a value is a scalar.
88 89 90 |
# File 'lib/core/core.rb', line 88 def is_scalar?(val) val.is_a?(Numeric) end |
#random_bytes(size) ⇒ Array<Integer>
Returns a random byte array of the given size.
8 9 10 |
# File 'lib/core/core.rb', line 8 def random_bytes(size) SecureRandom.random_bytes(size).bytes end |
#string_to_hex(string) ⇒ String
Converts a string to a hex string.
53 54 55 |
# File 'lib/core/core.rb', line 53 def string_to_hex(string) string.unpack1('H*').upcase end |
#valid_hex?(str) ⇒ Boolean
Checks if a string is a valid hex string.
60 61 62 |
# File 'lib/core/core.rb', line 60 def valid_hex?(str) str =~ /\A[0-9a-fA-F]*\z/ && str.length.even? end |