Class: Universa::HashId

Inherits:
RemoteAdapter show all
Defined in:
lib/universa/contract.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RemoteAdapter

#__getobj__, #__setobj__, #initialize, #inspect, invoke_static, remote_class, remote_class_name, remote_field, static_method

Constructor Details

This class inherits a constructor from Universa::RemoteAdapter

Class Method Details

.create_randomHashId

Create a random HashId

Returns:



180
181
182
# File 'lib/universa/contract.rb', line 180

def self.create_random
  invoke_static 'create_random'
end

.from_digest(digest_bytes) ⇒ HashId

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

Parameters:

  • digest_bytes (String)

    binary string of some hash_id.bytes

Returns:

  • (HashId)

    instance with instance.bytes == digest.bytes



164
165
166
167
# File 'lib/universa/contract.rb', line 164

def self.from_digest(digest_bytes)
  digest_bytes.force_encoding 'binary'
  invoke_static 'with_digest', digest_bytes
end

.from_string(string_id) ⇒ Object

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.

Parameters:

  • string_id (String)

    id string representation, like from hash_id_instance.to_s. See #to_s.



173
174
175
176
# File 'lib/universa/contract.rb', line 173

def self.from_string(string_id)
  string_id.force_encoding('utf-8').gsub('-', '+').gsub('_', '/')
  invoke_static 'with_digest', string_id
end

Instance Method Details

#==(other) ⇒ Object

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



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/universa/contract.rb', line 225

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

#bytesString

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.

Returns:

  • (String)

    binary string



189
190
191
# File 'lib/universa/contract.rb', line 189

def bytes
  @bytes ||= get_digest
end

#eql?(other) ⇒ Boolean

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

Returns:

  • (Boolean)


219
220
221
# File 'lib/universa/contract.rb', line 219

def eql? other
  self == other
end

#hashObject

To use it as a hash key_address.

Returns:

  • hash calculated over the digest bytes



214
215
216
# File 'lib/universa/contract.rb', line 214

def hash
  bytes.hash
end

#to_sString

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

Returns:

  • (String)

    string representation



198
199
200
# File 'lib/universa/contract.rb', line 198

def to_s
  @str ||= Base64.encode64(get_digest).gsub(/\s/, '')
end

#to_url_safe_stringString

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 from_string but not (most likely) with JAVA API itself

Returns:

  • (String)

    RFC3548 modified base64



208
209
210
# File 'lib/universa/contract.rb', line 208

def to_url_safe_string
  @urlsafe_str ||= Base64.encode64(get_digest).gsub(/\s/, '').gsub('/', '_').gsub('+', '-')
end