diff --git a/src/bare-agent.js b/src/bare-agent.js index 0dcdb3d..c3e8019 100644 --- a/src/bare-agent.js +++ b/src/bare-agent.js @@ -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( @@ -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 { @@ -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. @@ -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(); } } @@ -282,5 +286,5 @@ async function try_decode(canister_id, method_name, reply) { } catch (err) { console.log(err); } - return reply; + return msg; } diff --git a/src/index.js b/src/index.js index 0f1db1c..249a6fa 100644 --- a/src/index.js +++ b/src/index.js @@ -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, @@ -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"); @@ -159,17 +159,18 @@ function prepare_send(message) { pre.innerText = "Unsupported message format"; return; } + const canister_id = new Principal(ingress.content.canister_id); + 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) {