Class: Ciri::Types::Hash32

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/ciri/types/hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ Hash32

Returns a new instance of Hash32.



45
46
47
# File 'lib/ciri/types/hash.rb', line 45

def initialize(h)
  @hash32 = h.to_s
end

Class Method Details

.rlp_decode(data) ⇒ Object



36
37
38
39
40
# File 'lib/ciri/types/hash.rb', line 36

def rlp_decode(data)
  hash32 = self.new(RLP.decode(data))
  hash32.validate
  hash32
end

.rlp_encode(hash32) ⇒ Object



32
33
34
# File 'lib/ciri/types/hash.rb', line 32

def rlp_encode(hash32)
  RLP.encode(hash32.to_s)
end

Instance Method Details

#==(other) ⇒ Object



49
50
51
# File 'lib/ciri/types/hash.rb', line 49

def ==(other)
  self.class == other.class && to_s == other.to_s
end

#empty?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ciri/types/hash.rb', line 63

def empty?
  @hash32.empty?
end

#to_hexObject



59
60
61
# File 'lib/ciri/types/hash.rb', line 59

def to_hex
  Utils.to_hex to_s
end

#to_sObject Also known as: to_str



53
54
55
# File 'lib/ciri/types/hash.rb', line 53

def to_s
  @hash32
end

#validateObject

Raises:



67
68
69
70
71
# File 'lib/ciri/types/hash.rb', line 67

def validate
  # empty address is valid
  return if empty?
  raise InvalidError.new("hash32 must be 32 size, got #{@hash32.size}") unless @hash32.size == 32
end