From 55c52db27d7aba7cbac2e6bc01941dd1f7ebadec Mon Sep 17 00:00:00 2001 From: Gekctek Date: Wed, 23 Oct 2024 12:20:23 -0700 Subject: [PATCH 1/3] Fixing service encoding --- src/Candid/Models/Values/CandidService.cs | 6 +----- src/Candid/Parsers/CandidByteParser.cs | 15 ++++++++------ test/Candid.Tests/CandidEncodingTests.cs | 25 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/Candid/Models/Values/CandidService.cs b/src/Candid/Models/Values/CandidService.cs index c339b200..9f1f924c 100644 --- a/src/Candid/Models/Values/CandidService.cs +++ b/src/Candid/Models/Values/CandidService.cs @@ -58,11 +58,7 @@ internal override void EncodeValue(CandidType type, Func(0); return; } - int size = this.principalId!.Raw.Length + 1; - Span 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); } /// diff --git a/src/Candid/Parsers/CandidByteParser.cs b/src/Candid/Parsers/CandidByteParser.cs index 167ed881..90c295e1 100644 --- a/src/Candid/Parsers/CandidByteParser.cs +++ b/src/Candid/Parsers/CandidByteParser.cs @@ -582,11 +582,14 @@ public Principal ReadPrincipal() // Opaque reference throw new NotImplementedException(); } - else - { - List bytes = this.ReadVectorInner(() => this.ReadByte()); - return Principal.FromBytes(bytes.ToArray()); - } + + return this.ReadTransparentPrincipal(); + } + + private Principal ReadTransparentPrincipal() + { + List bytes = this.ReadVectorInner(this.ReadByte); + return Principal.FromBytes(bytes.ToArray()); } public sbyte ReadInt8() @@ -647,7 +650,7 @@ public CandidService ReadServiceValue() } else { - Principal? principalId = this.ReadPrincipal(); + Principal principalId = this.ReadTransparentPrincipal(); return new CandidService(principalId); } } diff --git a/test/Candid.Tests/CandidEncodingTests.cs b/test/Candid.Tests/CandidEncodingTests.cs index 8ac1c6bd..63328134 100644 --- a/test/Candid.Tests/CandidEncodingTests.cs +++ b/test/Candid.Tests/CandidEncodingTests.cs @@ -432,6 +432,31 @@ 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.Create("method1"), + new CandidFuncType([FuncMode.Query], new List(), new List()) + }, + }); + var expectedArg = CandidArg.FromCandid(new List + { + CandidTypedValue.FromValueAndType(value1, type1) + }); + + + byte[] actualBytes = ByteUtil.FromHexString(hex); + CandidArg actualArg = CandidArg.FromBytes(actualBytes); + + string a = ByteUtil.ToHexString(expectedArg.Encode()); + + Assert.Equal(expectedArg, actualArg); + } + [Fact] public void EncodeDecode_1() From 7305ef07fc6c02387fd1364580c7fbf08f801dfa Mon Sep 17 00:00:00 2001 From: Gekctek Date: Wed, 23 Oct 2024 12:23:25 -0700 Subject: [PATCH 2/3] Fixing syntax --- src/Agent/API.xml | 78 ++++++++++++------------ test/Candid.Tests/CandidEncodingTests.cs | 2 +- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/Agent/API.xml b/src/Agent/API.xml index 52c70ba1..8de8b54a 100644 --- a/src/Agent/API.xml +++ b/src/Agent/API.xml @@ -4,45 +4,6 @@ EdjCase.ICP.Agent - - - An `IAgent` implementation using HTTP to make requests to the IC - - - - - The identity that will be used on each request unless overriden - This identity can be anonymous - - - - Optional. Identity to use for each request. If unspecified, will use anonymous identity - Optional. Bls crypto implementation to validate signatures. If unspecified, will use default implementation - Optional. Sets the http client to use, otherwise will use the default http client - - - Optional. Identity to use for each request. If unspecified, will use anonymous identity - Optional. Bls crypto implementation to validate signatures. If unspecified, will use default implementation - Url to the boundry node to connect to. Defaults to `https://ic0.app/` - - - - - - - - - - - - - - - - - - - The default http client to use with the built in `HttpClient` @@ -111,6 +72,45 @@ + + + An `IAgent` implementation using HTTP to make requests to the IC + + + + + The identity that will be used on each request unless overriden + This identity can be anonymous + + + + Optional. Identity to use for each request. If unspecified, will use anonymous identity + Optional. Bls crypto implementation to validate signatures. If unspecified, will use default implementation + Optional. Sets the http client to use, otherwise will use the default http client + + + Optional. Identity to use for each request. If unspecified, will use anonymous identity + Optional. Bls crypto implementation to validate signatures. If unspecified, will use default implementation + Url to the boundry node to connect to. Defaults to `https://ic0.app/` + + + + + + + + + + + + + + + + + + + An agent is used to communicate with the Internet Computer with certain protocols that diff --git a/test/Candid.Tests/CandidEncodingTests.cs b/test/Candid.Tests/CandidEncodingTests.cs index 63328134..0a1d63d2 100644 --- a/test/Candid.Tests/CandidEncodingTests.cs +++ b/test/Candid.Tests/CandidEncodingTests.cs @@ -440,7 +440,7 @@ public void Encode_Service_Example1() var type1 = new CandidServiceType(new Dictionary{ { CandidId.Create("method1"), - new CandidFuncType([FuncMode.Query], new List(), new List()) + new CandidFuncType(new List{FuncMode.Query}, new List(), new List()) }, }); var expectedArg = CandidArg.FromCandid(new List From c0031bc3c8683fdd6e0ee0a76d5fa97407448b04 Mon Sep 17 00:00:00 2001 From: Gekctek Date: Wed, 23 Oct 2024 12:30:13 -0700 Subject: [PATCH 3/3] Adding Encode to test --- test/Candid.Tests/CandidEncodingTests.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/Candid.Tests/CandidEncodingTests.cs b/test/Candid.Tests/CandidEncodingTests.cs index 0a1d63d2..86f8d603 100644 --- a/test/Candid.Tests/CandidEncodingTests.cs +++ b/test/Candid.Tests/CandidEncodingTests.cs @@ -452,9 +452,11 @@ public void Encode_Service_Example1() byte[] actualBytes = ByteUtil.FromHexString(hex); CandidArg actualArg = CandidArg.FromBytes(actualBytes); - string a = ByteUtil.ToHexString(expectedArg.Encode()); - Assert.Equal(expectedArg, actualArg); + + string actualHex = ByteUtil.ToHexString(expectedArg.Encode()); + + Assert.Equal(hex, actualHex); }