Class: Zold::Id

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

Overview

Id of the wallet

Constant Summary collapse

BANNED =

Returns a list of banned IDs, as strings

CSV.read(File.join(__dir__, '../../resources/banned-wallets.csv')).map { |r| r[0] }
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.



48
49
50
51
52
# File 'lib/zold/id.rb', line 48

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



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

def self.generate_id
  loop do
    id = SecureRandom.hex(8)
    next if Id::BANNED.include?(id)
    return id
  end
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

def hash
  to_s.hash
end

#root?Boolean

Returns:

  • (Boolean)


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

def root?
  to_s == ROOT.to_s
end

#to_sObject



84
85
86
# File 'lib/zold/id.rb', line 84

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

#to_strObject



80
81
82
# File 'lib/zold/id.rb', line 80

def to_str
  to_s
end