Class: Zold::Id

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/id.rb

Overview

Id of the wallet

Constant Summary collapse

ROOT =

The ID of the root wallet.

Id.new('0000000000000000')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = Id.generate_id) ⇒ Id

Returns a new instance of Id.



42
43
44
45
46
# File 'lib/zold/id.rb', line 42

def initialize(id = Id.generate_id)
  raise "Invalid wallet ID type: #{id.class.name}" unless id.is_a?(String)
  raise "Invalid wallet ID: #{id}" unless PTN.match?(id)
  @id = Integer("0x#{id}", 16)
end

Class Method Details

.generate_idObject



34
35
36
37
38
39
40
# File 'lib/zold/id.rb', line 34

def self.generate_id
  loop do
    id = format('%016x', rand(2**32..2**64 - 1))
    next if IO.read(File.join(__dir__, '../../resources/banned-wallets.csv')).include?("\"#{id}\"")
    return id
  end
end

Instance Method Details

#<=>(other) ⇒ Object



69
70
71
72
# File 'lib/zold/id.rb', line 69

def <=>(other)
  raise 'Can only compare with Id' unless other.is_a?(Id)
  to_s <=> other.to_s
end

#==(other) ⇒ Object



64
65
66
67
# File 'lib/zold/id.rb', line 64

def ==(other)
  raise 'Can only compare with Id' unless other.is_a?(Id)
  to_s == other.to_s
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/zold/id.rb', line 55

def eql?(other)
  raise 'Can only compare with Id' unless other.is_a?(Id)
  to_s == other.to_s
end

#hashObject



60
61
62
# File 'lib/zold/id.rb', line 60

def hash
  to_s.hash
end

#root?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/zold/id.rb', line 51

def root?
  to_s == ROOT.to_s
end

#to_sObject



78
79
80
# File 'lib/zold/id.rb', line 78

def to_s
  format('%016x', @id)
end

#to_strObject



74
75
76
# File 'lib/zold/id.rb', line 74

def to_str
  to_s
end