Top Level Namespace

Defined Under Namespace

Modules: AddressCodec, BinaryCodec, Core, KeyPairs, Wallet, XRPL

Instance Method Summary collapse

Instance Method Details

#bin_to_hex(bin) ⇒ String

Converts a binary string to a hex string.

Parameters:

  • bin (String)

    The binary string to convert.

Returns:

  • (String)

    The 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.

Parameters:

  • bytes (Array<Integer>)

    The byte array to convert.

Returns:

  • (String)

    The 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.

Parameters:

  • bytes (Array<Integer>, String)

    The byte array or string to check.

  • expected_length (Integer)

    The expected length.

Returns:

  • (Boolean)

    True if the length matches, false otherwise.



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.

Parameters:

  • args (Array)

    The arguments to concatenate.

Returns:

  • (Array)

    The concatenated 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.

Parameters:

  • hex (String)

    The hex string to convert.

Returns:

  • (String)

    The binary string.

Raises:

  • (ArgumentError)


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.

Parameters:

  • hex (String)

    The hex string to convert.

Returns:

  • (Array<Integer>)

    The byte array.

Raises:

  • (ArgumentError)


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.

Parameters:

  • hex (String)

    The hex string to convert.

  • encoding (String) (defaults to: 'utf-8')

    The encoding to use.

Returns:

  • (String)

    The decoded string.

Raises:

  • (ArgumentError)


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.

Parameters:

  • number (Integer)

    The integer to convert.

  • width (Integer) (defaults to: 1)

    The number of bytes in the result.

  • byteorder (Symbol) (defaults to: :big)

    The byte order (:big or :little).

Returns:

  • (Array<Integer>)

    The 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.

Parameters:

  • val (Object)

    The value to check.

Returns:

  • (Boolean)

    True if the value is a numeric scalar, false otherwise.



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.

Parameters:

  • size (Integer)

    The number of bytes to generate.

Returns:

  • (Array<Integer>)

    The generated random byte array.



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.

Parameters:

  • string (String)

    The string to convert.

Returns:

  • (String)

    The 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.

Parameters:

  • str (String)

    The string to check.

Returns:

  • (Boolean)

    True if the string is a valid hex string, false otherwise.



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