Skip to content

Commit

Permalink
Decode input message in Candid (#2)
Browse files Browse the repository at this point in the history
* decode input message in candid

* fix

Co-authored-by: Yan Chen <[email protected]>
  • Loading branch information
chenyan2002 and chenyan-dfinity authored Sep 2, 2021
1 parent bc2ecd0 commit b0536a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
18 changes: 11 additions & 7 deletions src/bare-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export async function send_message(message, update_status, sleep) {
let canister_id = new Principal(ingress.content.canister_id).toString();
let method_name = ingress.content.method_name;
let reply = await query(canister_id, ingress_content);
update_status(await try_decode(canister_id, method_name, reply), true);
update_status(await try_decode(canister_id, method_name, reply.reply.arg, true), true);
} else {
// Update call, handle json format of both nano and dfx
const ingress_content = fromHexString(
Expand Down Expand Up @@ -178,7 +178,7 @@ export async function send_message(message, update_status, sleep) {
} else {
if (reply.status == "replied") {
update_status(
await try_decode(canister_id, method_name, reply),
await try_decode(canister_id, method_name, reply.reply.arg, true),
true
);
} else {
Expand Down Expand Up @@ -222,7 +222,7 @@ const getCandid_interface = ({ IDL }) =>
});

// Try to decode reply using known did files
async function try_decode(canister_id, method_name, reply) {
export async function try_decode(canister_id, method_name, msg, isReply) {
try {
var did;
// Try fetch i using __get_candid_interface_tmp_hack.
Expand Down Expand Up @@ -271,9 +271,13 @@ async function try_decode(canister_id, method_name, reply) {
let mod = await eval('import("' + dataUri + '")');
let services = mod.idlFactory({ IDL });
let func = lookup(services._fields, method_name);
reply = IDL.decode(func.retTypes, Buffer.from(reply.reply.arg));
reply = func.retTypes
.map((t, i) => t.valueToString(reply[i]))
let funcTypes = func.retTypes;
if (!isReply) {
funcTypes = func.argTypes;
}
msg = IDL.decode(funcTypes, Buffer.from(msg));
msg = funcTypes
.map((t, i) => t.valueToString(msg[i]))
.toString();
}
}
Expand All @@ -282,5 +286,5 @@ async function try_decode(canister_id, method_name, reply) {
} catch (err) {
console.log(err);
}
return reply;
return msg;
}
13 changes: 7 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as pako from "pako";
import { HttpAgent, Cbor } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { stringify } from "bigint-json-native";
import { send_message } from "./bare-agent.js";
import { send_message, try_decode } from "./bare-agent.js";

const data = {
version: 1,
Expand Down Expand Up @@ -125,14 +125,14 @@ async function scan() {
const unzipped = pako.inflate(gzipped, { to: "string" });
const message = JSON.parse(unzipped);
render(res, width, height);
prepare_send(message);
await prepare_send(message);
} catch (err) {
console.log(err);
}
}
}

function prepare_send(message) {
async function prepare_send(message) {
scan_paused = true;
if (input_type == "video") {
const scan_button = document.getElementById("scan");
Expand All @@ -159,17 +159,18 @@ function prepare_send(message) {
pre.innerText = "Unsupported message format";
return;
}
const canister_id = new Principal(ingress.content.canister_id);

This comment has been minimized.

Copy link
@chenyan-dfinity

chenyan-dfinity Sep 4, 2021

Author Contributor

@ninegua Need to append .toString() to canister_id, because try_decode expects a string instead of a Principal. The error only happens when __get_candid_interface_tmp_hack is not available.

This comment has been minimized.

Copy link
@ninegua

ninegua Sep 4, 2021

Owner

Ok, got it.

const args = await try_decode(canister_id, ingress.content.method_name, ingress.content.arg, false);
const text =
"Request type : " +
ingress.content.request_type +
"\nSender : " +
new Principal(ingress.content.sender).toString() +
"\nCanister id : " +
new Principal(ingress.content.canister_id).toString() +
"\nCanister id : " + canister_id.toString() +
"\nMethod name : " +
ingress.content.method_name +
"\nArguments : " +
JSON.stringify(ingress.content.arg);
JSON.stringify(args, (_, v) => typeof v === 'bigint' ? `${v}n` : v);
pre.innerText = text;
var button = document.getElementById("send");
if (!button) {
Expand Down

0 comments on commit b0536a3

Please sign in to comment.