class IcAgent::Candid::PrincipalClass

Represents an IDL principal reference

Public Class Methods

new() click to toggle source
Calls superclass method IcAgent::Candid::PrimitiveType::new
# File lib/ic_agent/candid.rb, line 621
def initialize
  super
end

Public Instance Methods

covariant(x) click to toggle source
# File lib/ic_agent/candid.rb, line 625
def covariant(x)
  if x.is_a?(String)
    p = IcAgent::Principal.from_str(x)
  elsif x.is_a?(Array)
    p = IcAgent::Principal.from_hex(x.pack('C*').unpack1('H*'))
  else
    raise ValueError, 'only support string or bytes format'
  end
  p.is_a?(IcAgent::Principal)
end
decode_value(b, t) click to toggle source
# File lib/ic_agent/candid.rb, line 653
def decode_value(b, t)
  check_type(t)
  res = IcAgent::Candid.safe_read_byte(b)
  if res != '01'
    raise ValueError, 'Cannot decode principal'
  end

  length = IcAgent::Candid.leb128u_decode(b)
  IcAgent::Principal.from_hex(IcAgent::Candid.safe_read(b, length)).to_str
end
encode_type(type_table = nil) click to toggle source
# File lib/ic_agent/candid.rb, line 649
def encode_type(type_table = nil)
  LEB128.encode_signed(TypeIds::Principal).string
end
encode_value(val) click to toggle source
# File lib/ic_agent/candid.rb, line 636
def encode_value(val)
  tag = 1.chr(Encoding::ASCII_8BIT)
  if val.is_a?(String)
    buf = IcAgent::Principal.from_str(val).bytes
  elsif val.is_a?(Array)
    buf = val.pack('C*')
  else
    raise ValueError, 'Principal should be string or bytes.'
  end
  l = LEB128.encode_signed(buf.size).string
  tag + l + buf
end
id() click to toggle source
# File lib/ic_agent/candid.rb, line 668
def id
  TypeIds::Principal
end
name() click to toggle source
# File lib/ic_agent/candid.rb, line 664
def name
  'principal'
end