Class: IcAgent::Candid::PrincipalClass
Overview
Represents an IDL principal reference
Instance Method Summary
collapse
#_build_type_table_impl, #check_type
Methods inherited from BaseType
_build_type_table_impl, #build_type_table, check_type, covariant, decode_value, #display, encode_type, encode_value
Constructor Details
Returns a new instance of PrincipalClass.
621
622
623
|
# File 'lib/ic_agent/candid.rb', line 621
def initialize
super
end
|
Instance Method Details
#covariant(x) ⇒ Object
625
626
627
628
629
630
631
632
633
634
|
# 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) ⇒ Object
653
654
655
656
657
658
659
660
661
662
|
# 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) ⇒ Object
649
650
651
|
# File 'lib/ic_agent/candid.rb', line 649
def encode_type(type_table = nil)
LEB128.encode_signed(TypeIds::Principal).string
end
|
#encode_value(val) ⇒ Object
636
637
638
639
640
641
642
643
644
645
646
647
|
# 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 ⇒ Object
668
669
670
|
# File 'lib/ic_agent/candid.rb', line 668
def id
TypeIds::Principal
end
|
#name ⇒ Object
664
665
666
|
# File 'lib/ic_agent/candid.rb', line 664
def name
'principal'
end
|