Module: MixinBot::Utils::Decoder

Included in:
MixinBot::Utils
Defined in:
lib/mixin_bot/utils/decoder.rb

Instance Method Summary collapse

Instance Method Details

#decode_int(bytes) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/mixin_bot/utils/decoder.rb', line 42

def decode_int(bytes)
  int = 0
  bytes.each do |byte|
    int = (int * (2**8)) + byte
  end

  int
end

#decode_key(key) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mixin_bot/utils/decoder.rb', line 6

def decode_key(key)
  return if key.blank?

  if key.match?(/\A[\h]{64,}\z/i)
    [key].pack('H*')
  elsif key.match?(/^-----BEGIN RSA PRIVATE KEY-----/)
    key.gsub('\\r\\n', "\n").gsub("\r\n", "\n")
  elsif key.match?(/\d{6}/) || (key.size % 32).zero?
    key
  else
    Base64.urlsafe_decode64 key
  end
end

#decode_raw_transaction(hex) ⇒ Object



20
21
22
# File 'lib/mixin_bot/utils/decoder.rb', line 20

def decode_raw_transaction(hex)
  MixinBot::Transaction.new(hex:).decode.to_h
end

#decode_uint16(bytes) ⇒ Object

Raises:



24
25
26
27
28
# File 'lib/mixin_bot/utils/decoder.rb', line 24

def decode_uint16(bytes)
  raise ArgumentError, "only support bytes #{bytes}" unless bytes.is_a?(Array)

  bytes.reverse.pack('C*').unpack1('S*')
end

#decode_uint32(bytes) ⇒ Object

Raises:



30
31
32
33
34
# File 'lib/mixin_bot/utils/decoder.rb', line 30

def decode_uint32(bytes)
  raise ArgumentError, "only support bytes #{bytes}" unless bytes.is_a?(Array)

  bytes.reverse.pack('C*').unpack1('L*')
end

#decode_uint64(bytes) ⇒ Object

Raises:



36
37
38
39
40
# File 'lib/mixin_bot/utils/decoder.rb', line 36

def decode_uint64(bytes)
  raise ArgumentError, "only support bytes #{bytes}" unless bytes.is_a?(Array)

  bytes.reverse.pack('C*').unpack1('Q*')
end

#hex_to_uuid(hex) ⇒ Object



51
52
53
# File 'lib/mixin_bot/utils/decoder.rb', line 51

def hex_to_uuid(hex)
  MixinBot::UUID.new(hex:).unpacked
end