Class: IcAgent::Candid
- Inherits:
-
Object
show all
- Defined in:
- lib/ic_agent/candid.rb
Defined Under Namespace
Classes: BaseType, BaseTypes, BoolClass, ConstructType, EmptyClass, FixedIntClass, FixedNatClass, FloatClass, FuncClass, IntClass, NatClass, NullClass, OptClass, Pipe, PrimitiveType, PrincipalClass, RecClass, RecordClass, ReservedClass, ServiceClass, TextClass, TupleClass, TypeIds, TypeTable, VariantClass, VecClass
Constant Summary
collapse
- PREFIX =
'DIDL'
- SINGLE_TYPES =
%w[null bool nat int nat8 nat16 nat32 nat64 int8 int16 int32 int64 float32 float64 text reserved empty principal]
- MULTI_TYPES =
%w[opt vec record variant func service blob]
- RESULT_TYPE =
%w[Ok Err]
- ALL_TYPES =
SINGLE_TYPES + MULTI_TYPES
Class Method Summary
collapse
Class Method Details
.build_type(raw_table, table, entry) ⇒ Object
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
|
# File 'lib/ic_agent/candid.rb', line 1396
def self.build_type(raw_table, table, entry)
ty = entry[0]
if ty == TypeIds::Vec
if ty >= raw_table.length
raise ValueError, 'type index out of range'
end
t = get_type(raw_table, table, entry[1])
if t.nil?
t = table[t]
end
return BaseTypes::vec(t)
elsif ty == TypeIds::Opt
if ty >= raw_table.length
raise ValueError, 'type index out of range'
end
t = get_type(raw_table, table, entry[1])
if t.nil?
t = table[t]
end
return BaseTypes::opt(t)
elsif ty == TypeIds::Record
fields = {}
entry[1].each do |hash, t|
name = "_#{hash.to_s}_"
if t >= raw_table.length
raise ValueError, 'type index out of range'
end
temp = get_type(raw_table, table, t)
fields[name] = temp
end
record = BaseTypes::record(fields)
tup = record.try_as_tuple()
if tup.is_a?(Array)
return BaseTypes::tuple(*tup)
else
return record
end
elsif ty == TypeIds::Variant
fields = {}
entry[1].each do |hash, t|
name = "_#{hash.to_s}_"
if t >= raw_table.length
raise ValueError, 'type index out of range'
end
temp = get_type(raw_table, table, t)
fields[name] = temp
end
return BaseTypes::variant(fields)
elsif ty == TypeIds::Func
return BaseTypes::func([], [], [])
elsif ty == TypeIds::Service
return BaseTypes::service({})
else
raise ValueError, "Illegal op_code: #{ty}"
end
end
|
.decode(data, ret_types = nil) ⇒ Object
decode a bytes value def decode(retTypes, data):
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
|
# File 'lib/ic_agent/candid.rb', line 1622
def self.decode(data, ret_types = nil)
pipe = Pipe.new(data)
if data.length < PREFIX.length
raise ValueError, 'Message length smaller than prefix number'
end
prefix_buffer = safe_read(pipe, PREFIX.length).hex2str
if prefix_buffer != PREFIX
raise ValueError, "Wrong prefix:#{prefix_buffer}expected prefix: DIDL"
end
raw_table, raw_types = read_type_table(pipe)
if ret_types
if ret_types.class != Array
ret_types = [ret_types]
end
if raw_types.length < ret_types.length
raise ValueError, 'Wrong number of return value'
end
end
table = []
raw_table.length.times do
table.append(BaseTypes.rec)
end
raw_table.each_with_index do |entry, i|
t = build_type(raw_table, table, entry)
table[i].fill(t)
end
types = []
raw_types.each do |t|
types.append(get_type(raw_table, table, t))
end
outputs = []
types.each_with_index do |t, i|
outputs.append({
'type' => t.name,
'value' => t.decode_value(pipe, types[i])
})
end
return outputs
end
|
.encode(params) ⇒ Object
params = [value] data = b’DIDL’ + len(params) + encoded types + encoded values
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
|
# File 'lib/ic_agent/candid.rb', line 1580
def self.encode(params)
arg_types = []
args = []
params.each do |p|
arg_types << p[:type]
args << p[:value]
end
if arg_types.length != args.length
raise ValueError, 'Wrong number of message arguments'
end
typetable = TypeTable.new
arg_types.each do |item|
item.build_type_table(typetable)
end
pre = unicode_to_hex(PREFIX)
table = unicode_to_hex(typetable.encode())
length = leb128_string_hex(args.length)
typs = ''
arg_types.each do |t|
typs += unicode_to_hex(t.encode_type(typetable))
end
vals = ''
args.each_with_index do |arg, i|
t = arg_types[i]
unless t.covariant(args[i])
raise TypeError, "Invalid #{t.display} argument: #{args[i]}"
end
vals += unicode_to_hex(t.encode_value(args[i]))
end
return pre + table + length + typs + vals
end
|
.get_type(raw_table, table, t) ⇒ Object
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
|
# File 'lib/ic_agent/candid.rb', line 1523
def self.get_type(raw_table, table, t)
if t < -24
raise ValueError, 'not supported type'
end
if t < 0
case t
when -1
return BaseTypes.null
when -2
return BaseTypes.bool
when -3
return BaseTypes.nat
when -4
return BaseTypes.int
when -5
return BaseTypes.nat8
when -6
return BaseTypes.nat16
when -7
return BaseTypes.nat32
when -8
return BaseTypes.nat64
when -9
return BaseTypes.int8
when -10
return BaseTypes.int16
when -11
return BaseTypes.int32
when -12
return BaseTypes.int64
when -13
return BaseTypes.float32
when -14
return BaseTypes.float64
when -15
return BaseTypes.text
when -16
return BaseTypes.reserved
when -17
return BaseTypes.empty
when -24
return BaseTypes.principal
else
raise ValueError, "Illegal op_code: #{t}"
end
end
if t >= raw_table.length
raise ValueError, 'type index out of range'
end
return table[t]
end
|
.leb128_string_hex(p_str) ⇒ Object
1388
1389
1390
|
# File 'lib/ic_agent/candid.rb', line 1388
def self.leb128_string_hex(p_str)
LEB128.encode_signed(p_str).string.to_hex
end
|
.leb128i_decode(pipe) ⇒ Object
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
|
# File 'lib/ic_agent/candid.rb', line 1358
def self.leb128i_decode(pipe)
length = pipe.buffer.length
count = 0
(0...length).each do |i|
count = i
if pipe.buffer[i] < '80' if pipe.buffer[i] < '40' return leb128u_decode(pipe)
end
break
end
end
res = StringIO.new
res.putc(safe_read(pipe, count + 1).hex)
LEB128.decode_signed(res)
end
|
.leb128u_decode(pipe) ⇒ Object
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
|
# File 'lib/ic_agent/candid.rb', line 1347
def self.leb128u_decode(pipe)
res = StringIO.new
loop do
byte = safe_read_byte(pipe)
res.putc(byte.hex)
break if byte < '80' || pipe.length.zero?
end
LEB128.decode_signed(res)
end
|
.read_type_table(pipe) ⇒ Object
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
|
# File 'lib/ic_agent/candid.rb', line 1457
def self.read_type_table(pipe)
type_table = []
type_table_len = leb128u_decode(pipe).to_i
type_table_len.times do
ty = leb128i_decode(pipe)
if [TypeIds::Opt, TypeIds::Vec].include?(ty)
t = leb128i_decode(pipe)
type_table << [ty, t]
elsif [TypeIds::Record, TypeIds::Variant].include?(ty)
fields = []
obj_length = leb128u_decode(pipe)
prev_hash = -1
obj_length.times do
hash = leb128u_decode(pipe)
if hash >= 2**32
raise ValueError, 'field id out of 32-bit range'
end
if prev_hash.is_a?(Integer) && prev_hash >= hash
raise ValueError, 'field id collision or not sorted'
end
prev_hash = hash
t = leb128i_decode(pipe)
fields << [hash, t]
end
type_table << [ty, fields]
elsif ty == TypeIds::Func
2.times do
fun_len = leb128u_decode(pipe)
fun_len.times { leb128i_decode(pipe) }
end
ann_len = leb128u_decode(pipe)
safe_read(pipe, ann_len)
type_table << [ty, nil]
elsif ty == TypeIds::Service
serv_len = leb128u_decode(pipe)
serv_len.times do
l = leb128u_decode(pipe)
safe_read(pipe, l)
leb128i_decode(pipe)
end
type_table << [ty, nil]
else
raise ValueError, "Illegal op_code: #{ty}"
end
end
raw_list = []
types_len = leb128u_decode(pipe).to_i
types_len.times do
raw_list << leb128i_decode(pipe)
end
[type_table, raw_list]
end
|
.safe_read(pipe, num) ⇒ Object
1376
1377
1378
1379
1380
|
# File 'lib/ic_agent/candid.rb', line 1376
def self.safe_read(pipe, num)
raise ArgumentError, 'unexpected end of buffer' if pipe.length < num
pipe.read(num)
end
|
.safe_read_byte(pipe) ⇒ Object
1382
1383
1384
1385
1386
|
# File 'lib/ic_agent/candid.rb', line 1382
def self.safe_read_byte(pipe)
raise ArgumentError, 'unexpected end of buffer' if pipe.length < 1
pipe.readbyte
end
|
.unicode_to_hex(u_code) ⇒ Object
1392
1393
1394
|
# File 'lib/ic_agent/candid.rb', line 1392
def self.unicode_to_hex(u_code)
u_code.to_hex
end
|