module DEVp2p::Utils

Constants

BYTE_ZERO
DOUBLE_COLON

Public Instance Methods

big_endian_to_int(s) click to toggle source
# File lib/devp2p/utils.rb, line 21
def big_endian_to_int(s)
  RLP::Sedes.big_endian_int.deserialize s.sub(/\A(\x00)+/, '')
end
bpad(x, l) click to toggle source
# File lib/devp2p/utils.rb, line 43
def bpad(x, l)
  lpad x.to_s(2), '0', l
end
ceil16(x) click to toggle source
# File lib/devp2p/utils.rb, line 30
def ceil16(x)
  x % 16 == 0 ? x : (x + 16 - (x%16))
end
class_to_cmd_name(klass) click to toggle source
# File lib/devp2p/utils.rb, line 104
def class_to_cmd_name(klass)
  klass.name.split(DOUBLE_COLON).last.downcase
end
decode_hex(s) click to toggle source
# File lib/devp2p/utils.rb, line 13
def decode_hex(s)
  RLP::Utils.decode_hex s
end
encode_hex(b) click to toggle source
# File lib/devp2p/utils.rb, line 9
def encode_hex(b)
  RLP::Utils.encode_hex b
end
host_port_pubkey_from_uri(uri) click to toggle source
# File lib/devp2p/utils.rb, line 78
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) click to toggle source
# File lib/devp2p/utils.rb, line 88
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) click to toggle source
# File lib/devp2p/utils.rb, line 17
def int_to_big_endian(i)
  RLP::Sedes.big_endian_int.serialize(i)
end
int_to_big_endian4(i) click to toggle source

4 bytes big endian integer

# File lib/devp2p/utils.rb, line 26
def int_to_big_endian4(i)
  [i].pack('I>')
end
lpad(x, symbol, l) click to toggle source
# File lib/devp2p/utils.rb, line 34
def lpad(x, symbol, l)
  return x if x.size >= l
  symbol * (l - x.size) + x
end
rzpad16(data) click to toggle source
# File lib/devp2p/utils.rb, line 47
def rzpad16(data)
  extra = data.size % 16
  data += "\x00" * (16 - extra) if extra != 0
  data
end
sxor(s1, s2) click to toggle source

String xor.

# File lib/devp2p/utils.rb, line 60
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) click to toggle source
# File lib/devp2p/utils.rb, line 95
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) click to toggle source
# File lib/devp2p/utils.rb, line 66
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) click to toggle source
# File lib/devp2p/utils.rb, line 39
def zpad(x, l)
  lpad x, BYTE_ZERO, l
end
zpad_int(i, l=32) click to toggle source
# File lib/devp2p/utils.rb, line 53
def zpad_int(i, l=32)
  Utils.zpad Utils.int_to_big_endian(i), l
end