Module: Xpring::Util

Defined in:
lib/xpring/util.rb

Overview

Various utilities

Class Method Summary collapse

Class Method Details

.byte_string_from_hex_bytes(input) ⇒ String

Parameters:

  • input (Array<Integer>)

Returns:

  • (String)


70
71
72
# File 'lib/xpring/util.rb', line 70

def self.byte_string_from_hex_bytes(input)
  input.pack("C*")
end

.byte_string_from_string(input) ⇒ String

Parameters:

  • input (#to_s)

Returns:

  • (String)


76
77
78
79
80
# File 'lib/xpring/util.rb', line 76

def self.byte_string_from_string(input)
  byte_string_from_hex_bytes(
    hex_bytes_from_string(input),
  )
end

.decode(x_address) ⇒ Hash<Symbol, Object>

Parameters:

  • x_address (#to_s)

Returns:

  • (Hash<Symbol, Object>)


52
53
54
55
56
57
58
# File 'lib/xpring/util.rb', line 52

def self.decode(x_address)
  Javascript.run do
    <<~JAVASCRIPT
      #{Javascript::ENTRY_POINT}.Utils.decodeXAddress('#{x_address}');
    JAVASCRIPT
  end
end

.encode(classic_address, tag: nil, test: false) ⇒ String

Parameters:

  • classic_address (#to_s)
  • tag (#to_s, nil) (defaults to: nil)
  • test (true, false) (defaults to: false)

Returns:

  • (String)


42
43
44
45
46
47
48
# File 'lib/xpring/util.rb', line 42

def self.encode(classic_address, tag: nil, test: false)
  Javascript.run do
    <<~JAVASCRIPT
      #{Javascript::ENTRY_POINT}.Utils.encodeXAddress('#{classic_address}', #{tag&.to_s}, #{test});
    JAVASCRIPT
  end
end

.hex_bytes_from_string(input) ⇒ Array<Integer>

Parameters:

  • input (#to_s)

Returns:

  • (Array<Integer>)


62
63
64
65
66
# File 'lib/xpring/util.rb', line 62

def self.hex_bytes_from_string(input)
  input.to_s.chars.each_slice(2).map do |*pair|
    pair.join.hex
  end
end

.valid_address?(address) ⇒ true, false

Parameters:

  • address (#to_s)

Returns:

  • (true, false)


10
11
12
13
14
15
16
# File 'lib/xpring/util.rb', line 10

def self.valid_address?(address)
  Javascript.run do
    <<~JAVASCRIPT
      #{Javascript::ENTRY_POINT}.Utils.isValidAddress('#{address}');
    JAVASCRIPT
  end == true
end

.valid_classic_address?(address) ⇒ true, false

Parameters:

  • address (#to_s)

Returns:

  • (true, false)


30
31
32
33
34
35
36
# File 'lib/xpring/util.rb', line 30

def self.valid_classic_address?(address)
  Javascript.run do
    <<~JAVASCRIPT
      #{Javascript::ENTRY_POINT}.Utils.isValidClassicAddress('#{address}');
    JAVASCRIPT
  end == true
end

.valid_x_address?(address) ⇒ true, false

Parameters:

  • address (#to_s)

Returns:

  • (true, false)


20
21
22
23
24
25
26
# File 'lib/xpring/util.rb', line 20

def self.valid_x_address?(address)
  Javascript.run do
    <<~JAVASCRIPT
      #{Javascript::ENTRY_POINT}.Utils.isValidXAddress('#{address}');
    JAVASCRIPT
  end == true
end