Module: Beowulf::Utils

Instance Method Summary collapse

Instance Method Details

#debug(obj, prefix = nil) ⇒ Object



72
73
74
75
76
# 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) ⇒ Object



62
63
64
# File 'lib/beowulf/utils.rb', line 62

def error(obj, prefix = nil)
  send_log(:error, obj, prefix)
end

#extract_signatures(options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



78
79
80
81
82
83
84
85
86
# 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) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Object

uint8_t



149
150
151
# File 'lib/beowulf/utils.rb', line 149

def pakC(i)
  [i].pack('C')
end

#pakc(i) ⇒ Object

int8_t



154
155
156
# File 'lib/beowulf/utils.rb', line 154

def pakc(i)
  [i].pack('c')
end

#pakHash(h) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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) ⇒ Object

uint32_t



169
170
171
# File 'lib/beowulf/utils.rb', line 169

def pakI(i)
  [i].pack('I')
end

#pakL!(i) ⇒ Object

int64_t



174
175
176
# File 'lib/beowulf/utils.rb', line 174

def pakL!(i)
  [i].pack('L!')
end

#pakPubKey(s) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# 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) ⇒ Object

uint64_t



179
180
181
# File 'lib/beowulf/utils.rb', line 179

def pakQ(i)
  [i].pack('Q')
end

#pakq(i) ⇒ Object

int64_t



184
185
186
# File 'lib/beowulf/utils.rb', line 184

def pakq(i)
  [i].pack('q')
end

#paks(i) ⇒ Object

int16_t



159
160
161
# File 'lib/beowulf/utils.rb', line 159

def paks(i)
  [i].pack('s')
end

#pakS(i) ⇒ Object

uint16_t



164
165
166
# File 'lib/beowulf/utils.rb', line 164

def pakS(i)
  [i].pack('S')
end

#pakStr(s) ⇒ Object



105
106
107
108
109
110
111
112
# 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

#send_log(level, obj, prefix = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



88
89
90
# File 'lib/beowulf/utils.rb', line 88

def unhexlify(s)
  s.split.pack('H*')
end

#varint(n) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object



66
67
68
69
70
# 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