class IcAgent::Principal
Base class for Principal
.
Attributes
Public Class Methods
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
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
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
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
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
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
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
# File lib/ic_agent/principal.rb, line 116 def to_s to_str end
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