Skip to content

Commit

Permalink
Service encoding bug (#133)
Browse files Browse the repository at this point in the history
* Fixing service encoding

* Fixing syntax

* Adding Encode to test
  • Loading branch information
Gekctek authored Oct 23, 2024
1 parent d17263f commit 65417d0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
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

0 comments on commit 65417d0

Please sign in to comment.