Class: IcAgent::Principal
- Inherits:
-
Object
- Object
- IcAgent::Principal
- Defined in:
- lib/ic_agent/principal.rb
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
- .from_hex(s) ⇒ Object
- .from_str(s) ⇒ Object
- .management_canister ⇒ Object
- .self_authenticating(pubkey) ⇒ Object
Instance Method Summary collapse
-
#initialize(bytes: ''.b) ⇒ Principal
constructor
A new instance of Principal.
- #to_account_id(sub_account = 0) ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(bytes: ''.b) ⇒ Principal
Returns a new instance of Principal.
20 21 22 23 24 25 |
# File 'lib/ic_agent/principal.rb', line 20 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.
18 19 20 |
# File 'lib/ic_agent/principal.rb', line 18 def bytes @bytes end |
#hex ⇒ Object (readonly)
Returns the value of attribute hex.
18 19 20 |
# File 'lib/ic_agent/principal.rb', line 18 def hex @hex end |
#is_principal ⇒ Object (readonly)
Returns the value of attribute is_principal.
18 19 20 |
# File 'lib/ic_agent/principal.rb', line 18 def is_principal @is_principal end |
#len ⇒ Object (readonly)
Returns the value of attribute len.
18 19 20 |
# File 'lib/ic_agent/principal.rb', line 18 def len @len end |
Class Method Details
.anonymous ⇒ Object
40 41 42 |
# File 'lib/ic_agent/principal.rb', line 40 def self.anonymous Principal.new(bytes: "\x04".b) end |
.from_hex(s) ⇒ Object
56 57 58 |
# File 'lib/ic_agent/principal.rb', line 56 def self.from_hex(s) Principal.new(bytes: [s].pack('H*')) end |
.from_str(s) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ic_agent/principal.rb', line 44 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
27 28 29 |
# File 'lib/ic_agent/principal.rb', line 27 def self.management_canister Principal.new end |
.self_authenticating(pubkey) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/ic_agent/principal.rb', line 31 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::SelfAuthenticating].pack('C') Principal.new(bytes: hash_) end |
Instance Method Details
#to_account_id(sub_account = 0) ⇒ Object
74 75 76 |
# File 'lib/ic_agent/principal.rb', line 74 def to_account_id(sub_account = 0) AccountIdentifier.new(self, sub_account) end |
#to_s ⇒ Object
78 79 80 |
# File 'lib/ic_agent/principal.rb', line 78 def to_s to_str end |
#to_str ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ic_agent/principal.rb', line 60 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 |