Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/Candid/Models/Values/CandidService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ internal override void EncodeValue(CandidType type, Func<CandidId, CandidCompoun
destination.WriteOne<byte>(0);
return;
}
int size = this.principalId!.Raw.Length + 1;
Span<byte> bytes = destination.GetSpan(size);
bytes[0] = 1; // Set first byte to 1
this.principalId.Raw.CopyTo(bytes[1..]); // Set other bytes to raw id
destination.Advance(size);
CandidValue.Principal(this.principalId!).EncodeValue(CandidType.Principal(), getReferencedType, destination);
}

/// <inheritdoc />
Expand Down
15 changes: 9 additions & 6 deletions src/Candid/Parsers/CandidByteParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,11 +582,14 @@ public Principal ReadPrincipal()
// Opaque reference
throw new NotImplementedException();
}
else
{
List<byte> bytes = this.ReadVectorInner(() => this.ReadByte());
return Principal.FromBytes(bytes.ToArray());
}

return this.ReadTransparentPrincipal();
}

private Principal ReadTransparentPrincipal()
{
List<byte> bytes = this.ReadVectorInner(this.ReadByte);
return Principal.FromBytes(bytes.ToArray());
}

public sbyte ReadInt8()
Expand Down Expand Up @@ -647,7 +650,7 @@ public CandidService ReadServiceValue()
}
else
{
Principal? principalId = this.ReadPrincipal();
Principal principalId = this.ReadTransparentPrincipal();
return new CandidService(principalId);
}
}
Expand Down
27 changes: 27 additions & 0 deletions test/Candid.Tests/CandidEncodingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,33 @@ public void Encode_Record_Recursive()
Assert.Equal(expectedArg, actualArg);
}

[Fact]
public void Encode_Service_Example1()
{
const string hex = "4449444C026A000001016901076D6574686F6431000101010A0000000001E0102A0101";
var value1 = new CandidService(Principal.FromText("bpo6s-4qaaa-aaaap-acava-cai"));
var type1 = new CandidServiceType(new Dictionary<CandidId, CandidFuncType>{
{
CandidId.Create("method1"),
new CandidFuncType(new List<FuncMode>{FuncMode.Query}, new List<CandidType>(), new List<CandidType>())
},
});
var expectedArg = CandidArg.FromCandid(new List<CandidTypedValue>
{
CandidTypedValue.FromValueAndType(value1, type1)
});


byte[] actualBytes = ByteUtil.FromHexString(hex);
CandidArg actualArg = CandidArg.FromBytes(actualBytes);

Assert.Equal(expectedArg, actualArg);

string actualHex = ByteUtil.ToHexString(expectedArg.Encode());

Assert.Equal(hex, actualHex);
}


[Fact]
public void EncodeDecode_1()
Expand Down