Class: IcAgent::Principal
- Inherits:
-
Object
- Object
- IcAgent::Principal
- Defined in:
- lib/ic_agent/principal.rb
Overview
Base class for Principal.
Instance Attribute Summary collapse
-
#bytes ⇒ Object
readonly
Returns the value of attribute bytes.
-
#hex ⇒ Object
readonly
Returns the value of attribute hex.
-
#is_principal ⇒ Object
readonly
Returns the value of attribute is_principal.
-
#len ⇒ Object
readonly
Returns the value of attribute len.
Class Method Summary collapse
-
.anonymous ⇒ Object
Creates a new anonymous Principal.
-
.from_hex(s) ⇒ Object
Creates a new Principal from a hexadecimal string representation.
-
.from_str(s) ⇒ Object
Creates a new Principal from a string representation.
-
.management_canister ⇒ Object
Creates a new Principal instance representing the management canister.
-
.self_authenticating(pubkey) ⇒ Object
Creates a new self-authenticating Principal.
Instance Method Summary collapse
-
#compare_to(other) ⇒ Object
Compares the Principal with another Principal.
-
#gt_eq(other) ⇒ Object
Utility method checking whether a provided Principal is greater than or equal to the current one using the ‘compare_to` method.
-
#initialize(bytes: ''.b) ⇒ Principal
constructor
Initializes a new instance of the Principal class.
-
#lt_eq(other) ⇒ Object
Utility method checking whether a provided Principal is less than or equal to the current one using the ‘compare_to` method.
-
#to_account_id(sub_account = 0) ⇒ Object
Converts the Principal to an AccountIdentifier.
- #to_s ⇒ Object
-
#to_str ⇒ Object
Converts the Principal to a string representation.
Constructor Details
#initialize(bytes: ''.b) ⇒ Principal
Initializes a new instance of the Principal class.
Parameters:
-
bytes: The bytes representing the principal. Defaults to an empty string.
25 26 27 28 29 30 |
# 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 |
Instance Attribute Details
#bytes ⇒ Object (readonly)
Returns the value of attribute bytes.
19 20 21 |
# File 'lib/ic_agent/principal.rb', line 19 def bytes @bytes end |
#hex ⇒ Object (readonly)
Returns the value of attribute hex.
19 20 21 |
# File 'lib/ic_agent/principal.rb', line 19 def hex @hex end |
#is_principal ⇒ Object (readonly)
Returns the value of attribute is_principal.
19 20 21 |
# File 'lib/ic_agent/principal.rb', line 19 def is_principal @is_principal end |
#len ⇒ Object (readonly)
Returns the value of attribute len.
19 20 21 |
# File 'lib/ic_agent/principal.rb', line 19 def len @len end |
Class Method Details
.anonymous ⇒ Object
Creates a new anonymous Principal.
Returns: The anonymous Principal instance.
57 58 59 |
# File 'lib/ic_agent/principal.rb', line 57 def self.anonymous Principal.new(bytes: "\x04".b) end |
.from_hex(s) ⇒ Object
Creates a new Principal from a hexadecimal string representation.
Parameters:
-
s: The hexadecimal string representation of the Principal.
Returns: The Principal instance.
85 86 87 |
# File 'lib/ic_agent/principal.rb', line 85 def self.from_hex(s) Principal.new(bytes: [s].pack('H*')) end |
.from_str(s) ⇒ Object
Creates a new Principal from a string representation.
Parameters:
-
s: The string representation of the Principal.
Returns: The Principal instance.
67 68 69 70 71 72 73 74 75 76 77 |
# 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 ⇒ Object
Creates a new Principal instance representing the management canister.
Returns: The Principal instance representing the management canister.
35 36 37 |
# File 'lib/ic_agent/principal.rb', line 35 def self.management_canister Principal.new end |
.self_authenticating(pubkey) ⇒ Object
Creates a new self-authenticating Principal.
Parameters:
-
pubkey: The public key associated with the self-authenticating Principal.
Returns: The self-authenticating Principal instance.
45 46 47 48 49 50 51 52 |
# 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 |
Instance Method Details
#compare_to(other) ⇒ Object
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’).
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# 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) ⇒ Object
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`.
161 162 163 164 |
# 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) ⇒ Object
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`.
150 151 152 153 |
# 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) ⇒ Object
Converts the Principal to an AccountIdentifier.
Parameters:
-
sub_account: The sub-account identifier. Defaults to 0.
Returns: The AccountIdentifier instance.
112 113 114 |
# File 'lib/ic_agent/principal.rb', line 112 def to_account_id(sub_account = 0) AccountIdentifier.generate(self, sub_account) end |
#to_s ⇒ Object
116 117 118 |
# File 'lib/ic_agent/principal.rb', line 116 def to_s to_str end |
#to_str ⇒ Object
Converts the Principal to a string representation.
Returns: The string representation of the Principal.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# 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 |