dfinity / agent-js

A collection of libraries and tools for building software around the Internet Computer, in JavaScript.

Home Page:https://agent-js.icp.xyz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error parsing response when returning a Principal from a canister method written in Rust

paulyoung opened this issue · comments

When calling the following canister method:

#[query]
fn whoami() -> ic_cdk::export::Principal {
    ic_cdk::caller()
}

or:

#[query]
fn whoami() -> ic_cdk::export::candid::Principal {
    ic_cdk::caller()
}

I receive the following error:

ÙÙ÷£fstatushrejectedkreject_code�nreject_messagex¥IC0503: Canister rrkah-fqaaa-aaaaa-aaaaq-cai trapped explicitly: Custom(Cannot parse header 4449444c0001

Caused by:
    binary parser error: index at byte offset 6)

dfx canister call <my-project> whoami shows (principal "<my-principal>") so I expected that to work in a browser too. Other methods like the "hello/greet" example work in the browser.

  • @dfinity/agent 0.11.1
  • dfx 0.9.3
  • ic-cdk 0.5.0
  • macOS Monterey, Version 12.2.1 (21D62)
  • Brave, Version 1.36.119 Chromium: 99.0.4844.83
  • Safari, Version 15.3 (17612.4.9.1.8)

Screen Shot 2022-05-16 at 1 43 29 PM

Screen Shot 2022-05-16 at 1 43 59 PM

The error seems to come from the Rust side trying to decode input argument. The client is sending DIDL\00\01, which is invalid. We should be sending DIDL\00\00 instead.

For what it's worth, it appears to work in the Candid UI but I get a different value than I do on the command line with dfx canister call

Can you share the code for calling the canister in JS?

Browser identity and dfx identity are different. And for Candid UI, it only uses the anonymous identity for now.

This appears to have been due to some bindings I wrote that were conceptually typed as Unit -> Principal but called such functions as whoami({}) since {} can represent unit.