class Universa::HashId

Adapter for Universa HashId class, helps to avoid confusion when using different representations of the ID.

Public Class Methods

create_random() click to toggle source

Create a random HashId @return [HashId] random HashId

# File lib/universa/contract.rb, line 171
def self.create_random
  invoke_static 'create_random'
end
from_digest(digest_bytes) click to toggle source

Construct from binary representation, not to confuse with binary one.

@param [String] digest_bytes binary string of some hash_id.bytes @return [HashId] instance with instance.bytes == digest.bytes

# File lib/universa/contract.rb, line 155
def self.from_digest(digest_bytes)
  digest_bytes.force_encoding 'binary'
  invoke_static 'with_digest', digest_bytes
end
from_string(string_id) click to toggle source

Construct from string representation of the ID, not to confuse with binary one. This method takes both regular base64 representation and RFC3548 url-safe modification, as from {#to_url_safe_string}.

@param [String] string_id id string representation, like from hash_id_instance.to_s. See {#to_s}.

# File lib/universa/contract.rb, line 164
def self.from_string(string_id)
  string_id.force_encoding('utf-8').gsub('-', '+').gsub('_', '/')
  invoke_static 'with_digest', string_id
end

Public Instance Methods

==(other) click to toggle source

Compare hashid with string representation, binary representation or another HashId instance automatically.

Calls superclass method
# File lib/universa/contract.rb, line 216
def == other
  return false if other == nil
  if other.is_a?(Universa::HashId)
    super
  else
    if other.size == 96
      bytes == other
    else
      to_s == other
    end
  end
end
bytes() click to toggle source

Get binary representation. It is shorter than string representation but contain non-printable characters and can cause problems if treated like a string. Use {#to_s} to get string representation instead.

@return [String] binary string

# File lib/universa/contract.rb, line 180
def bytes
  @bytes ||= get_digest
end
eql?(other) click to toggle source

To use it as a hash key_address. Same as this == other.

# File lib/universa/contract.rb, line 210
def eql? other
  self == other
end
hash() click to toggle source

To use it as a hash key_address. @return hash calculated over the digest bytes

# File lib/universa/contract.rb, line 205
def hash
  bytes.hash
end
to_s() click to toggle source

Get string representation. It is, actually, base64 encoded string representation. Longer, but could easily be transferred with text protocols.

@return [String] string representation

# File lib/universa/contract.rb, line 189
def to_s
  @str ||= Base64.encode64(get_digest).gsub(/\s/, '')
end
to_url_safe_string() click to toggle source

Converts to URL-safe varianot of base64, as RFC 3548 suggests:

the 63:nd / character with the underscore _
the 62:nd + character with the minus -

Could be decoded safely back with {HashId.from_string} but not (most likely) with JAVA API itself @return [String] RFC3548 modified base64

# File lib/universa/contract.rb, line 199
def to_url_safe_string
  @urlsafe_str ||= Base64.encode64(get_digest).gsub(/\s/, '').gsub('/', '_').gsub('+', '-')
end