class IcAgent::Principal

Base class for Principal.

Attributes

bytes[R]
hex[R]
is_principal[R]
len[R]

Public Class Methods

anonymous() click to toggle source

Creates a new anonymous Principal.

Returns: The anonymous Principal instance.

# File lib/ic_agent/principal.rb, line 57
def self.anonymous
  Principal.new(bytes: "\x04".b)
end
from_hex(s) click to toggle source

Creates a new Principal from a hexadecimal string representation.

Parameters:

  • s: The hexadecimal string representation of the Principal.

Returns: The Principal instance.

# File lib/ic_agent/principal.rb, line 85
def self.from_hex(s)
  Principal.new(bytes: [s].pack('H*'))
end
from_str(s) click to toggle source

Creates a new Principal from a string representation.

Parameters:

  • s: The string representation of the Principal.

Returns: The Principal instance.

# File lib/ic_agent/principal.rb, line 67
def self.from_str(s)
  s1 = s.delete('-')
  pad_len = ((s1.length / 8.0).ceil * 8) - s1.length
  b = Base32.decode(s1.upcase + ('=' * pad_len))
  raise 'principal length error' if b.length < CRC_LENGTH_IN_BYTES

  p = Principal.new(bytes: b[CRC_LENGTH_IN_BYTES..-1])
  raise 'principal format error' unless p.to_str == s

  p
end
management_canister() click to toggle source

Creates a new Principal instance representing the management canister.

Returns: The Principal instance representing the management canister.

# File lib/ic_agent/principal.rb, line 35
def self.management_canister
  Principal.new
end
new(bytes: ''.b) click to toggle source

Initializes a new instance of the Principal class.

Parameters:

  • bytes: The bytes representing the principal. Defaults to an empty string.

# File lib/ic_agent/principal.rb, line 25
def initialize(bytes: ''.b)
  @len = bytes.length
  @bytes = bytes
  @hex = @bytes.unpack1('H*').upcase
  @is_principal = true
end
self_authenticating(pubkey) click to toggle source

Creates a new self-authenticating Principal.

Parameters:

  • pubkey: The public key associated with the self-authenticating Principal.

Returns: The self-authenticating Principal instance.

# File lib/ic_agent/principal.rb, line 45
def self.self_authenticating(pubkey)
  # check pubkey.size for is ed25519 or secp256k1
  pubkey = [pubkey].pack('H*') if pubkey.size != 44 && pubkey.size != 88

  hash_ = OpenSSL::Digest::SHA224.digest(pubkey)
  hash_ += [PrincipalSort::SELF_AUTHENTICATING].pack('C')
  Principal.new(bytes: hash_)
end

Public Instance Methods

compare_to(other) click to toggle source

Compares the Principal with another Principal.

Parameters:

  • other: The other Principal to compare with.

Returns: The comparison result as a string ('lt', 'eq', or 'gt').

# File lib/ic_agent/principal.rb, line 126
def compare_to(other)
  (0...[self.bytes.length, other.bytes.length].min).each do |i|
    if self.bytes[i] < other.bytes[i]
      return 'lt'
    elsif self.bytes[i] > other.bytes[i]
      return 'gt'
    end
  end

  if self.bytes.length < other.bytes.length
    'lt'
  elsif self.bytes.length > other.bytes.length
    'gt'
  else
    'eq'
  end
end
gt_eq(other) click to toggle source

Utility method checking whether a provided Principal is greater than or equal to the current one using the `compare_to` method.

Parameters:

  • other: The other Principal to compare with.

Returns: `true` if the current Principal is greater than or equal to the provided Principal, otherwise `false`.

# File lib/ic_agent/principal.rb, line 161
def gt_eq(other)
  cmp = compare_to(other)
  %w[gt eq].include?(cmp)
end
lt_eq(other) click to toggle source

Utility method checking whether a provided Principal is less than or equal to the current one using the `compare_to` method.

Parameters:

  • other: The other Principal to compare with.

Returns: `true` if the current Principal is less than or equal to the provided Principal, otherwise `false`.

# File lib/ic_agent/principal.rb, line 150
def lt_eq(other)
  cmp = compare_to(other)
  %w[lt eq].include?(cmp)
end
to_account_id(sub_account = 0) click to toggle source

Converts the Principal to an AccountIdentifier.

Parameters:

  • sub_account: The sub-account identifier. Defaults to 0.

Returns: The AccountIdentifier instance.

# File lib/ic_agent/principal.rb, line 112
def to_account_id(sub_account = 0)
  AccountIdentifier.generate(self, sub_account)
end
to_s() click to toggle source
# File lib/ic_agent/principal.rb, line 116
def to_s
  to_str
end
to_str() click to toggle source

Converts the Principal to a string representation.

Returns: The string representation of the Principal.

# File lib/ic_agent/principal.rb, line 92
def to_str
  checksum = Zlib.crc32(@bytes) & 0xFFFFFFFF
  b = ''
  b += [checksum].pack('N')
  b += @bytes
  s = Base32.encode(b).downcase.delete('=')
  ret = ''
  while s.length > 5
    ret += s[0..4] + '-'
    s = s[5..-1]
  end
  ret + s
end