module Beowulf::Utils

Public Instance Methods

debug(obj, prefix = nil) click to toggle source
# File lib/beowulf/utils.rb, line 72
def debug(obj, prefix = nil)
  if %w(DEBUG TRACE).include? ENV['LOG']
    send_log(:debug, obj, prefix)
  end
end
error(obj, prefix = nil) click to toggle source
# File lib/beowulf/utils.rb, line 62
def error(obj, prefix = nil)
  send_log(:error, obj, prefix)
end
extract_signatures(options) click to toggle source
# File lib/beowulf/utils.rb, line 5
def extract_signatures(options)
  return [] unless defined? options[:params].map

  params = options[:params]

  signatures = params.map do |param|
    next unless defined? param.map

    param.map do |tx|
      tx[:signatures] rescue nil
    end
  end.flatten.compact

  expirations = params.map do |param|
    next unless defined? param.map

    param.map do |tx|
      Time.parse(tx[:expiration] + 'Z') rescue nil
    end
  end.flatten.compact

  [signatures, expirations.min]
end
hexlify(s) click to toggle source
# File lib/beowulf/utils.rb, line 78
def hexlify(s)
  a = []
  if s.respond_to? :each_byte
    s.each_byte { |b| a << sprintf('%02X', b) }
  else
    s.each { |b| a << sprintf('%02X', b) }
  end
  a.join.downcase
end
pakArr(a) click to toggle source
# File lib/beowulf/utils.rb, line 114
def pakArr(a)
  varint(a.size) + a.map do |v|
    case v
    when Symbol then pakStr(v.to_s)
    when String then pakStr(v)
    when Integer then paks(v)
    when TrueClass then pakC(1)
    when FalseClass then pakC(0)
    when ::Array then pakArr(v)
    when ::Hash then pakHash(v)
    when NilClass then next
    else
      raise OperationError, "Unsupported type: #{v.class}"
    end
  end.join
end
pakC(i) click to toggle source

uint8_t

# File lib/beowulf/utils.rb, line 149
def pakC(i)
  [i].pack('C')
end
pakHash(h) click to toggle source
# File lib/beowulf/utils.rb, line 131
def pakHash(h)
  varint(h.size) + h.map do |k, v|
    pakStr(k.to_s) + case v
                     when Symbol then pakStr(v.to_s)
                     when String then pakStr(v)
                     when Integer then paks(v)
                     when TrueClass then pakC(1)
                     when FalseClass then pakC(0)
                     when ::Array then pakArr(v)
                     when ::Hash then pakHash(v)
                     when NilClass then next
                     else
                       raise OperationError, "Unsupported type: #{v.class}"
                     end
  end.join
end
pakI(i) click to toggle source

uint32_t

# File lib/beowulf/utils.rb, line 169
def pakI(i)
  [i].pack('I')
end
pakL!(i) click to toggle source

int64_t

# File lib/beowulf/utils.rb, line 174
def pakL!(i)
  [i].pack('L!')
end
pakPubKey(s) click to toggle source
# File lib/beowulf/utils.rb, line 188
def pakPubKey(s)
  # Get substring past index three through end of string.
  pkn1 = s[3..-1]
  # puts 'pkn1', pkn1

  #b58.length=37
  b58 = Base58.base58_to_binary(pkn1, :bitcoin)
  # puts 'b58.length', b58.length
  # puts 'b58', hexlify(b58)

  # Get checksum 4-bytes end.
  lb58 = b58.length-4
  chs = b58[lb58..-1]

  # Get raw PublicKey = b58 cut 4-bytes checksum.
  # pkn2.length=33
  hb58 = lb58-1
  pkn2 = b58[0..hb58]

  # Validate PublicKey again.
  checksum = OpenSSL::Digest::RIPEMD160.digest(pkn2)
  # take 4 bytes.
  nchs = checksum[0..3]
  achs = hexlify(chs)
  bnchs = hexlify(nchs)
  if !(achs.eql? bnchs)
    puts achs, bnchs
    puts 'Public key is incorrect'
  end
  pkn2
end
pakQ(i) click to toggle source

uint64_t

# File lib/beowulf/utils.rb, line 179
def pakQ(i)
  [i].pack('Q')
end
pakS(i) click to toggle source

uint16_t

# File lib/beowulf/utils.rb, line 164
def pakS(i)
  [i].pack('S')
end
pakStr(s) click to toggle source
# File lib/beowulf/utils.rb, line 105
def pakStr(s)
  s = s.dup.force_encoding('BINARY')
  bytes = []
  bytes << varint(s.size)
  bytes << s

  bytes.join
end
pakc(i) click to toggle source

int8_t

# File lib/beowulf/utils.rb, line 154
def pakc(i)
  [i].pack('c')
end
pakq(i) click to toggle source

int64_t

# File lib/beowulf/utils.rb, line 184
def pakq(i)
  [i].pack('q')
end
paks(i) click to toggle source

int16_t

# File lib/beowulf/utils.rb, line 159
def paks(i)
  [i].pack('s')
end
send_log(level, obj, prefix = nil) click to toggle source
# File lib/beowulf/utils.rb, line 29
def send_log(level, obj, prefix = nil)
  log_message = case obj
                when String
                  log_message = if !!prefix
                                  "#{prefix} :: #{obj}"
                                else
                                  obj
                                end

                  if !!@logger
                    @logger.send level, log_message
                  else
                    puts "#{level}: #{log_message}"
                  end
                else
                  if defined? @logger.ap
                    if !!prefix
                      @logger.ap log_level: level, prefix => obj
                    else
                      @logger.ap obj, level
                    end
                  else
                    if !!prefix
                      @logger.send level, ({prefix => obj}).inspect
                    else
                      @logger.send level, obj.inspect
                    end
                  end
                end

  nil
end
unhexlify(s) click to toggle source
# File lib/beowulf/utils.rb, line 88
def unhexlify(s)
  s.split.pack('H*')
end
varint(n) click to toggle source
# File lib/beowulf/utils.rb, line 92
def varint(n)
  data = []
  while n >= 0x80
    data += [(n & 0x7f) | 0x80]

    n >>= 7
  end

  data += [n]

  data.pack('C*')
end
warning(obj, prefix = nil, log_debug_node = false) click to toggle source
# File lib/beowulf/utils.rb, line 66
def warning(obj, prefix = nil, log_debug_node = false)
  debug("Current node: #{@url}", prefix) if !!log_debug_node && @url

  send_log(:warn, obj, prefix)
end