From 60f62d3e4baaef13fa277b9439a33611488b532a Mon Sep 17 00:00:00 2001 From: Tomi Jaga Date: Sat, 30 Dec 2023 19:29:42 -0500 Subject: [PATCH] [Fix]: fix the redundant addition of the self-describe tag to each nested value --- src/CBOR/lib.mo | 28 +++++++++++++--------------- tests/CBOR.Test.mo | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/CBOR/lib.mo b/src/CBOR/lib.mo index e036db0..023e7d0 100644 --- a/src/CBOR/lib.mo +++ b/src/CBOR/lib.mo @@ -24,8 +24,10 @@ module { type Result = Result.Result; type CBOR = CBOR_Value.Value; + public type Options = CandidTypes.Options; + /// Converts serialized Candid blob to CBOR blob - public func encode(blob : Blob, keys : [Text], options: ?CandidTypes.Options) : Result { + public func encode(blob : Blob, keys : [Text], options: ?Options) : Result { let decoded_res = Candid.decode(blob, keys, options); let #ok(candid) = decoded_res else return Utils.send_error(decoded_res); @@ -39,7 +41,9 @@ module { let res = transpile_candid_to_cbor(candid); let #ok(transpiled_cbor) = res else return Utils.send_error(res); - switch(CBOR_Encoder.encode(transpiled_cbor)){ + let cbor_with_self_describe_tag = #majorType6({ tag = 55799 : Nat64; value = transpiled_cbor; }); + + switch(CBOR_Encoder.encode(cbor_with_self_describe_tag)){ case(#ok(encoded_cbor)){ #ok (Blob.fromArray(encoded_cbor))}; case(#err(#invalidValue(errMsg))){ #err("Invalid value error while encoding CBOR: " # errMsg) }; }; @@ -109,13 +113,10 @@ module { }; }; - // add self-describe tag - let tagged_cbor = #majorType6({ tag = 55799 : Nat64; value = transpiled_cbor; }); - - #ok(tagged_cbor); + #ok(transpiled_cbor); }; - public func decode(blob: Blob, options: ?CandidTypes.Options): Result { + public func decode(blob: Blob, options: ?Options): Result { let candid_res = toCandid(blob); let #ok(candid) = candid_res else return Utils.send_error(candid_res); Candid.encodeOne(candid, options); @@ -125,7 +126,10 @@ module { let cbor_res = CBOR_Decoder.decode(blob); let candid_res = switch (cbor_res) { - case (#ok(cbor)) transpile_cbor_to_candid(cbor); + case (#ok(cbor)) { + let #majorType6({ tag = 55799; value }) = cbor else return transpile_cbor_to_candid(cbor); + transpile_cbor_to_candid(value); + }; case (#err(cbor_error)) { switch(cbor_error){ case (#unexpectedBreak){ return #err("Error decoding CBOR: Unexpected break") }; @@ -176,13 +180,7 @@ module { return #err("Error decoding CBOR: #_break is not supported"); }; case (#majorType6(tagged_cbor)) { - if (tagged_cbor.tag == 55799){ - let transpiled_candid_res = transpile_cbor_to_candid(tagged_cbor.value); - let #ok(transpiled_candid) = transpiled_candid_res else return Utils.send_error(transpiled_candid_res); - transpiled_candid - } else { - return #err("Error decoding CBOR: Tagged values are not supported"); - }; + return #err("Error decoding CBOR: Tagged values are not supported"); }; }; diff --git a/tests/CBOR.Test.mo b/tests/CBOR.Test.mo index 3fd0305..6bc2fbc 100644 --- a/tests/CBOR.Test.mo +++ b/tests/CBOR.Test.mo @@ -1,4 +1,6 @@ // @testmode wasi +import Array "mo:base/Array"; +import Blob "mo:base/Blob"; import Debug "mo:base/Debug"; import Iter "mo:base/Iter"; import { test; suite } "mo:test"; @@ -45,6 +47,7 @@ suite( assert opt_text2 == opt_text; }); + test( "primitives", func() { @@ -82,6 +85,23 @@ suite( let #ok(list_cbor) = CBOR.encode(list_candid, [], null); let #ok(record_cbor) = CBOR.encode(record_candid, ["a", "b"], null); + let self_describe_tag : Blob = "\D9\D9\F7"; + + func blob_concat(b1: Blob, b2: Blob): Blob = Blob.fromArray(Array.append(Blob.toArray(b1), Blob.toArray(b2))); + func sdt(blob: Blob): Blob = blob_concat(self_describe_tag, blob); + + // cbor encodings from https://cbor.me/ + assert nat_cbor == sdt("\18\7B"); + assert int_cbor == sdt("\38\7A"); + assert float_cbor == sdt("\FB\40\5E\DD\2F\1A\9F\BE\77"); + assert bool_cbor == sdt("\F5"); + assert text_cbor == sdt("\65\68\65\6C\6C\6F"); + assert blob_cbor == sdt("\43\01\02\03"); + assert null_cbor == sdt("\F6"); + // assert empty_cbor == sdt("\F7"); // mapped to undefined + assert list_cbor == sdt("\83\01\02\03"); + assert record_cbor == sdt("\A2\61\61\01\61\62\02"); + let #ok(nat_candid2) = CBOR.decode(nat_cbor, null); let #ok(int_candid2) = CBOR.decode(int_cbor, null); let #ok(float_candid2) = CBOR.decode(float_cbor, null); @@ -106,5 +126,7 @@ suite( }, ); + + }, );