class IcAgent::AccountIdentifier

Attributes

bytes[R]

Public Class Methods

generate(principal, sub_account = 0) click to toggle source

Generates a new AccountIdentifier from a Principal.

Parameters:

Returns: The AccountIdentifier instance.

# File lib/ic_agent/principal.rb, line 198
def self.generate(principal, sub_account = 0)
  sha224 = OpenSSL::Digest::SHA224.new
  sha224 << "\naccount-id"
  sha224 << principal.bytes
  format_sub_account = "%08d" % sub_account
  sub_account = format_sub_account.chars.map { |c| c.to_i }.pack('N*')
  sha224 << sub_account
  hash = sha224.digest
  checksum = Zlib.crc32(hash) & 0xFFFFFFFF
  account = [checksum].pack('N') + hash
  AccountIdentifier.new(account)
end
new(hash) click to toggle source

Initializes a new instance of the AccountIdentifier class.

Parameters:

# File lib/ic_agent/principal.rb, line 174
def initialize(hash)
  raise 'Invalid hash length' unless hash.length == 32

  @bytes = hash
end

Public Instance Methods

to_s() click to toggle source
# File lib/ic_agent/principal.rb, line 187
def to_s
  to_str
end
to_str() click to toggle source

Converts the AccountIdentifier to a string representation.

Returns: The string representation of the AccountIdentifier.

# File lib/ic_agent/principal.rb, line 183
def to_str
  '0x' + @bytes.unpack1('H*')
end