Class: IcAgent::Candid::FuncClass

Inherits:
ConstructType show all
Defined in:
lib/ic_agent/candid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ConstructType

#check_type, #encode_type

Methods inherited from BaseType

_build_type_table_impl, #build_type_table, check_type, covariant, decode_value, encode_type, encode_value

Constructor Details

#initialize(arg_types, ret_types, annotations) ⇒ FuncClass

Returns a new instance of FuncClass.



1079
1080
1081
1082
1083
1084
# File 'lib/ic_agent/candid.rb', line 1079

def initialize(arg_types, ret_types, annotations)
  super()
  @arg_types = arg_types
  @ret_types = ret_types
  @annotations = annotations
end

Instance Attribute Details

#annotationsObject

Returns the value of attribute annotations.



1077
1078
1079
# File 'lib/ic_agent/candid.rb', line 1077

def annotations
  @annotations
end

#arg_typesObject

Returns the value of attribute arg_types.



1077
1078
1079
# File 'lib/ic_agent/candid.rb', line 1077

def arg_types
  @arg_types
end

#ret_typesObject

Returns the value of attribute ret_types.



1077
1078
1079
# File 'lib/ic_agent/candid.rb', line 1077

def ret_types
  @ret_types
end

Instance Method Details

#_build_type_table_impl(type_table) ⇒ Object



1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
# File 'lib/ic_agent/candid.rb', line 1111

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) ⇒ Object



1162
1163
1164
1165
1166
1167
1168
1169
1170
# File 'lib/ic_agent/candid.rb', line 1162

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) ⇒ Object



1086
1087
1088
1089
1090
# File 'lib/ic_agent/candid.rb', line 1086

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) ⇒ Object

Raises:

  • (ArgumentError)


1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
# File 'lib/ic_agent/candid.rb', line 1128

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

#displayObject



1155
1156
1157
1158
1159
1160
# File 'lib/ic_agent/candid.rb', line 1155

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) ⇒ Object



1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
# File 'lib/ic_agent/candid.rb', line 1092

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

#idObject



1151
1152
1153
# File 'lib/ic_agent/candid.rb', line 1151

def id
  TypeIds::Func
end

#nameObject



1144
1145
1146
1147
1148
1149
# File 'lib/ic_agent/candid.rb', line 1144

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