Skip to content

Commit

Permalink
Merge pull request #21 from NatLabs/dev
Browse files Browse the repository at this point in the history
[Fix]: fix the redundant addition of the self-describe tag
  • Loading branch information
tomijaga authored Dec 31, 2023
2 parents cf1b95f + 60f62d3 commit 7640a3d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/CBOR/lib.mo
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ module {
type Result<A, B> = Result.Result<A, B>;
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<Blob, Text> {
public func encode(blob : Blob, keys : [Text], options: ?Options) : Result<Blob, Text> {
let decoded_res = Candid.decode(blob, keys, options);
let #ok(candid) = decoded_res else return Utils.send_error(decoded_res);

Expand All @@ -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) };
};
Expand Down Expand Up @@ -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<Blob, Text> {
public func decode(blob: Blob, options: ?Options): Result<Blob, Text> {
let candid_res = toCandid(blob);
let #ok(candid) = candid_res else return Utils.send_error(candid_res);
Candid.encodeOne(candid, options);
Expand All @@ -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") };
Expand Down Expand Up @@ -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");
};
};

Expand Down
22 changes: 22 additions & 0 deletions tests/CBOR.Test.mo
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -45,6 +47,7 @@ suite(
assert opt_text2 == opt_text;

});

test(
"primitives",
func() {
Expand Down Expand Up @@ -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);
Expand All @@ -106,5 +126,7 @@ suite(

},
);


},
);

0 comments on commit 7640a3d

Please sign in to comment.