Module: DEVp2p::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/devp2p/utils.rb

Constant Summary collapse

BYTE_ZERO =
"\x00".freeze
DOUBLE_COLON =
'::'.freeze

Instance Method Summary collapse

Instance Method Details

#big_endian_to_int(s) ⇒ Object



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

def big_endian_to_int(s)
  RLP::Sedes.big_endian_int.deserialize s.sub(/\A(\x00)+/, '')
end

#bpad(x, l) ⇒ Object



44
45
46
# File 'lib/devp2p/utils.rb', line 44

def bpad(x, l)
  lpad x.to_s(2), '0', l
end

#ceil16(x) ⇒ Object



31
32
33
# File 'lib/devp2p/utils.rb', line 31

def ceil16(x)
  x % 16 == 0 ? x : (x + 16 - (x%16))
end

#class_to_cmd_name(klass) ⇒ Object



105
106
107
# File 'lib/devp2p/utils.rb', line 105

def class_to_cmd_name(klass)
  klass.name.split(DOUBLE_COLON).last.downcase
end

#decode_hex(s) ⇒ Object



14
15
16
# File 'lib/devp2p/utils.rb', line 14

def decode_hex(s)
  RLP::Utils.decode_hex s
end

#encode_hex(b) ⇒ Object



10
11
12
# File 'lib/devp2p/utils.rb', line 10

def encode_hex(b)
  RLP::Utils.encode_hex b
end

#host_port_pubkey_from_uri(uri) ⇒ Object

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
# File 'lib/devp2p/utils.rb', line 79

def host_port_pubkey_from_uri(uri)
  raise ArgumentError, 'invalid uri' unless uri =~ /\A#{NODE_URI_SCHEME}.+@.+:.+$/

  pubkey_hex, ip_port = uri[NODE_URI_SCHEME.size..-1].split('@')
  raise ArgumentError, 'invalid pubkey length' unless pubkey_hex.size == 2 * Kademlia::PUBKEY_SIZE / 8

  ip, port = ip_port.split(':')
  return ip, port, Utils.decode_hex(pubkey_hex)
end

#host_port_pubkey_to_uri(host, port, pubkey) ⇒ Object

Raises:

  • (ArgumentError)


89
90
91
92
93
# File 'lib/devp2p/utils.rb', line 89

def host_port_pubkey_to_uri(host, port, pubkey)
  raise ArgumentError, 'invalid pubkey length' unless pubkey.size == Kademlia::PUBKEY_SIZE / 8

  "#{NODE_URI_SCHEME}#{encode_hex pubkey}@#{host}:#{port}"
end

#int_to_big_endian(i) ⇒ Object



18
19
20
# File 'lib/devp2p/utils.rb', line 18

def int_to_big_endian(i)
  RLP::Sedes.big_endian_int.serialize(i)
end

#int_to_big_endian4(i) ⇒ Object

4 bytes big endian integer



27
28
29
# File 'lib/devp2p/utils.rb', line 27

def int_to_big_endian4(i)
  [i].pack('I>')
end

#lpad(x, symbol, l) ⇒ Object



35
36
37
38
# File 'lib/devp2p/utils.rb', line 35

def lpad(x, symbol, l)
  return x if x.size >= l
  symbol * (l - x.size) + x
end

#rzpad16(data) ⇒ Object



48
49
50
51
52
# File 'lib/devp2p/utils.rb', line 48

def rzpad16(data)
  extra = data.size % 16
  data += "\x00" * (16 - extra) if extra != 0
  data
end

#sxor(s1, s2) ⇒ Object

String xor.

Raises:

  • (ArgumentError)


61
62
63
64
65
# File 'lib/devp2p/utils.rb', line 61

def sxor(s1, s2)
  raise ArgumentError, "strings must have equal size" unless s1.size == s2.size

  s1.bytes.zip(s2.bytes).map {|a, b| (a ^ b).chr }.join
end

#underscore(s) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/devp2p/utils.rb', line 96

def underscore(s)
  word = s.split(DOUBLE_COLON).last
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end

#update_config_with_defaults(config, default_config) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/devp2p/utils.rb', line 67

def update_config_with_defaults(config, default_config)
  default_config.each do |k, v|
    if v.is_a?(Hash)
      config[k] = update_config_with_defaults(config.fetch(k, {}), v)
    elsif !config.has_key?(k)
      config[k] = default_config[k]
    end
  end

  config
end

#zpad(x, l) ⇒ Object



40
41
42
# File 'lib/devp2p/utils.rb', line 40

def zpad(x, l)
  lpad x, BYTE_ZERO, l
end

#zpad_int(i, l = 32) ⇒ Object



54
55
56
# File 'lib/devp2p/utils.rb', line 54

def zpad_int(i, l=32)
  Utils.zpad Utils.int_to_big_endian(i), l
end