class IcAgent::Candid::FuncClass

Represents an IDL Func reference

Attributes

annotations[RW]
arg_types[RW]
ret_types[RW]

Public Class Methods

new(arg_types, ret_types, annotations) click to toggle source
Calls superclass method IcAgent::Candid::ConstructType::new
# File lib/ic_agent/candid.rb, line 1098
def initialize(arg_types, ret_types, annotations)
  super()
  @arg_types = arg_types
  @ret_types = ret_types
  @annotations = annotations
end

Public Instance Methods

_build_type_table_impl(type_table) click to toggle source
# File lib/ic_agent/candid.rb, line 1130
def _build_type_table_impl(type_table)
  @arg_types.each { |arg| arg.build_type_table(type_table) }
  @ret_types.each { |ret| ret.build_type_table(type_table) }

  op_code = LEB128.encode_signed(TypeIds::Func).string
  arg_len = LEB128.encode_signed(@arg_types.length).string
  args = ''
  @arg_types.each { |arg| args += arg.encode_type(type_table) }
  ret_len = LEB128.encode_signed(@ret_types.length).string
  rets = ''
  @ret_types.each { |ret| rets += ret.encode_type(type_table) }
  ann_len = LEB128.encode_signed(@annotations.length).string
  anns = ''
  @annotations.each { |a| anns += _encode_annotation(a) }
  type_table.add(self, op_code + arg_len + args + ret_len + rets + ann_len + anns)
end
_encode_annotation(ann) click to toggle source
# File lib/ic_agent/candid.rb, line 1181
def _encode_annotation(ann)
  if ann == 'query'
    return [1].pack('C')
  elsif ann == 'oneway'
    return [2].pack('C')
  else
    raise 'Illeagal function annotation'
  end
end
covariant(x) click to toggle source
# File lib/ic_agent/candid.rb, line 1105
def covariant(x)
  x.is_a?(Array) && x.length == 2 && x[0] &&
    (x[0].is_a?(String) ? IcAgent::Principal.from_str(x[0]) : IcAgent::Principal.from_hex(x[0].unpack('H*').first)).is_a?(IcAgent::Principal) &&
    x[1].is_a?(String)
end
decode_value(b, t) click to toggle source
# File lib/ic_agent/candid.rb, line 1147
def decode_value(b, t)
  x = IcAgent::Candid.safe_read_byte(b)
  raise ArgumentError, 'Cannot decode function reference' unless x == '01'

  res = IcAgent::Candid.safe_read_byte(b)
  raise ArgumentError, 'Cannot decode principal' unless res == '01'

  length = IcAgent::Candid.leb128u_decode(b)
  canister = IcAgent::Principal.from_hex(IcAgent::Candid.safe_read(b, length)).to_str
  mLen = IcAgent::Candid.leb128u_decode(b)
  buf =  IcAgent::Candid.safe_read(b, mLen)
  method = buf.hex2str

  [canister, method]
end
display() click to toggle source
# File lib/ic_agent/candid.rb, line 1174
def display
  args = @arg_types.map { |arg| arg.display }.join(', ')
  rets = @ret_types.map { |ret| ret.display }.join(', ')
  anns = @annotations.join(' ')
  "(#{args}) → (#{rets}) #{anns}"
end
encode_value(vals) click to toggle source
# File lib/ic_agent/candid.rb, line 1111
def encode_value(vals)
  principal = vals[0]
  method_name = vals[1]
  tag = [1].pack('C')
  if principal.is_a?(String)
    buf = IcAgent::Principal.from_str(principal).bytes
  elsif principal.is_a?(String)
    buf = principal
  else
    raise ArgumentError, 'Principal should be string or bytes.'
  end
  l = LEB128.encode_signed(buf.length).string
  canister = tag + l + buf

  method = method_name.encode
  method_len = LEB128.encode_signed(method.length).string
  tag + canister + method_len + method
end
id() click to toggle source
# File lib/ic_agent/candid.rb, line 1170
def id
  TypeIds::Func
end
name() click to toggle source
# File lib/ic_agent/candid.rb, line 1163
def name
  args = @arg_types.map { |arg| arg.name }.join(', ')
  rets = @ret_types.map { |ret| ret.name }.join(', ')
  anns = @annotations.join(' ')
  "(#{args}) → (#{rets}) #{anns}"
end