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



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

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
19
20
# 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?(/\A[a-zA-Z0-9\-\_=]{43,}\z/)
    Base64.urlsafe_decode64 key
  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
    raise ArgumentError, "Invalid key #{key}"
  end
end

#decode_raw_transaction(hex) ⇒ Object



22
23
24
# File 'lib/mixin_bot/utils/decoder.rb', line 22

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

#decode_uint_16(bytes) ⇒ Object

Raises:



26
27
28
29
30
# File 'lib/mixin_bot/utils/decoder.rb', line 26

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

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

#decode_uint_32(bytes) ⇒ Object

Raises:



32
33
34
35
36
# File 'lib/mixin_bot/utils/decoder.rb', line 32

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

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

#decode_uint_64(bytes) ⇒ Object

Raises:



38
39
40
41
42
# File 'lib/mixin_bot/utils/decoder.rb', line 38

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

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

#hex_to_uuid(hex) ⇒ Object



53
54
55
# File 'lib/mixin_bot/utils/decoder.rb', line 53

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