class IcAgent::Candid::ServiceClass

Represents an IDL Service reference

Public Class Methods

new(field) click to toggle source
Calls superclass method IcAgent::Candid::ConstructType::new
# File lib/ic_agent/candid.rb, line 1194
def initialize(field)
  super()
  @fields = Hash[field.sort_by { |k, _| IcAgent::Utils.label_hash(k.to_s) }]
end

Public Instance Methods

_build_type_table_impl(type_table) click to toggle source
# File lib/ic_agent/candid.rb, line 1223
def _build_type_table_impl(type_table)
  @fields.each_value { |v| v.build_type_table(type_table) }
  op_code = LEB128.encode_signed(TypeIds::Service).string
  length = LEB128.encode_signed(@fields.length).string
  fields = ''.b
  @fields.each { |k, v|
    fields += LEB128.encode_signed(k.to_s.bytesize).string + k.to_s + v.encode_type(type_table)
  }
  type_table.add(self, op_code + length + fields)
end
covariant(x) click to toggle source
# File lib/ic_agent/candid.rb, line 1199
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 ArgumentError, '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 1234
def decode_value(b, t)
  res = IcAgent::Candid.safe_read_byte(b)
  raise ArgumentError, 'Cannot decode principal' unless res == '01'

  length = IcAgent::Candid.leb128u_decode(b)
  IcAgent::Principal.from_hex(IcAgent::Candid.safe_read(b, length)).to_str
end
encode_value(val) click to toggle source
# File lib/ic_agent/candid.rb, line 1210
def encode_value(val)
  tag = [1].pack('C')
  if val.is_a?(String)
    buf = IcAgent::Principal.from_str(val).bytes
  elsif val.is_a?(Array)
    buf = val
  else
    raise ArgumentError, 'Principal should be string or bytes.'
  end
  l = LEB128.encode_signed(buf.length).string
  tag + l + buf
end
id() click to toggle source
# File lib/ic_agent/candid.rb, line 1247
def id
  TypeIds::Service
end
name() click to toggle source
# File lib/ic_agent/candid.rb, line 1242
def name
  fields = @fields.map { |k, v| "#{k} : #{v.name}" }.join(', ')
  "service #{fields}"
end