Class: IcAgent::Principal

Inherits:
Object
  • Object
show all
Defined in:
lib/ic_agent/principal.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bytesObject (readonly)

Returns the value of attribute bytes.



18
19
20
# File 'lib/ic_agent/principal.rb', line 18

def bytes
  @bytes
end

#hexObject (readonly)

Returns the value of attribute hex.



18
19
20
# File 'lib/ic_agent/principal.rb', line 18

def hex
  @hex
end

#is_principalObject (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

#lenObject (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

.anonymousObject



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_canisterObject



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 ( = 0)
  AccountIdentifier.new(self, )
end

#to_sObject



78
79
80
# File 'lib/ic_agent/principal.rb', line 78

def to_s
  to_str
end

#to_strObject



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