Skip to content

Commit

Permalink
allow decoding with the service method arg type
Browse files Browse the repository at this point in the history
  • Loading branch information
keplervital committed Oct 23, 2024
1 parent b64aec5 commit 7b49d1d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
7 changes: 6 additions & 1 deletion tools/didc-js/wasm-package/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ pub fn decode(args: DecodeArgs) -> Result<String, LibraryError> {
}
})?;

IDLArgs::from_bytes_with_types(&args_bytes, &idl.env, &method.rets).map_err(|e| {
let method_types = match args.use_service_method_return_type {
None | Some(true) => &method.rets,
Some(false) => &method.args,
};

IDLArgs::from_bytes_with_types(&args_bytes, &idl.env, method_types).map_err(|e| {
LibraryError::IdlArgsFromBytesFailed {
reason: format!("Could not decode args from bytes {}", e),
}
Expand Down
13 changes: 13 additions & 0 deletions tools/didc-js/wasm-package/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export interface DecodeArgs {
* A method to pick from the service. If not provided, the entire idl is used.
*/
serviceMethod?: string;
/**
* Wether to use the service method return type to decode the value. Default is true if serviceMethod is provided.
*/
useServiceMethodReturnType?: boolean;
/**
* The format of the input value. Default is 'hex'.
*/
Expand Down Expand Up @@ -95,6 +99,7 @@ pub struct DecodeArgs {
pub input_format: EncodeFormat,
pub input: String,
pub service_method: Option<String>,
pub use_service_method_return_type: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
Expand Down Expand Up @@ -209,6 +214,13 @@ impl TryFrom<JsDecodeArgs> for DecodeArgs {
})?
.as_string();

let use_service_method_return_type =
js_sys::Reflect::get(&obj, &JsValue::from_str("useServiceMethodReturnType"))
.map_err(|_| LibraryError::MappingError {
reason: "Could not get 'useServiceMethodReturnType' from JsValue".to_string(),
})?
.as_bool();

let target_form = js_sys::Reflect::get(&obj, &JsValue::from_str("targetFormat"))
.map_err(|_| LibraryError::MappingError {
reason: "Could not get 'targetFormat' from JsValue".to_string(),
Expand Down Expand Up @@ -237,6 +249,7 @@ impl TryFrom<JsDecodeArgs> for DecodeArgs {
input_format,
input,
service_method,
use_service_method_return_type,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions tools/didc-js/wasm-package/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mod tests {
input_format: EncodeFormat::Hex,
input: "test".to_string(),
service_method: None,
use_service_method_return_type: None,
};

assert!(args.validate().is_ok());
Expand Down
26 changes: 25 additions & 1 deletion tools/didc-js/wasm-tests/src/decode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { decode } from "@dfinity/didc";
import { IDL } from "./test.utils";

describe("decode", () => {
it("decoding returns the correct candid decoded value from hex", () => {
it("decoding returns the method return type decoded value from hex", () => {
expect(
decode({
idl: IDL,
Expand All @@ -13,4 +13,28 @@ describe("decode", () => {
})
).toBe("(90 : nat64)");
});

it("decoding returns the method arg type decoded value from hex", () => {
expect(
decode({
idl: IDL,
input: "4449444c016c01c98dea8b0a7801006400000000000000",
serviceMethod: "store_number",
inputFormat: "hex",
targetFormat: "candid",
useServiceMethodReturnType: false,
})
).toBe("(record { number = 100 : nat64 })");
});

it("decoding without the service method can still return the decoded value", () => {
expect(
decode({
idl: IDL,
input: "4449444c016c01c98dea8b0a7801006400000000000000",
inputFormat: "hex",
targetFormat: "candid",
})
).toBe("(record { 2_709_161_673 = 100 : nat64 })");
});
});

0 comments on commit 7b49d1d

Please sign in to comment.