Skip to content

Commit

Permalink
Try fetch candid from canister itself, and then from canlista
Browse files Browse the repository at this point in the history
  • Loading branch information
ninegua committed Jun 19, 2021
1 parent f621d18 commit f424d9a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 357 deletions.
9 changes: 2 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
SRC=src/index.js src/bare-agent.js src/index.html
DID_SRC=src/ledger.did src/governance.did
NIX_SRC=default.nix ic-qr-scanner.nix node-env.nix node-packages.nix shell.nix
NPM_SRC=webpack.config.js src/simple.min.css
OTHER_SRC=src/manifest.json src/favicon.ico src/logo.png
ALL_SRC=$(SRC) $(DID_SRC) $(NIX_SRC) $(NPM_SRC) $(OTHER_SRC)
ALL_SRC=$(SRC) $(NIX_SRC) $(NPM_SRC) $(OTHER_SRC)

all: fmt build

build: dist/main.bundle.js

#NNS_IFACES ?= $(shell nix-prefetch-url --print-path https://codeload.github.com/dfinity/nns-ifaces/tar.gz/refs/tags/v0.8.1 | tail -n1)
#$(DID_SRC) &:
# cd src && cat $(NNS_IFACES) |tar zx --wildcards --strip-components=1 */ledger.did */governance.did

dist/index.html dist/main.bundle.js &: $(SRC) $(DID_SRC) $(NPM_SRC)
dist/index.html dist/main.bundle.js &: $(SRC) $(NPM_SRC)
npm run-script build

fmt: $(SRC) webpack.config.js
Expand Down
65 changes: 50 additions & 15 deletions src/bare-agent.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import {
RequestStatusResponseStatus,
HttpAgent,
Actor,
Cbor,
Certificate,
requestIdOf,
toHex,
} from "@dfinity/agent";
import { IDL, blobFromText } from "@dfinity/candid";
import { Principal } from "@dfinity/principal";
import ledger_did from "./ledger.did";
import governance_did from "./governance.did";

/*
import fs from "fs";
const ledger_did = await fs.readFileSync('./src/ledger.did');
const governance_did = await fs.readFileSync("./src/governance.did", "utf-8");
*/

function fromHexString(hexString) {
return new Uint8Array(
Expand Down Expand Up @@ -200,23 +193,63 @@ export async function send_message(message, update_status, sleep) {
}
}

const canister_did_files = {
"ryjl3-tyaaa-aaaaa-aaaba-cai": ledger_did,
"rrkah-fqaaa-aaaaa-aaaaq-cai": governance_did,
};

function lookup(dict, name) {
for (var i = 0; i < dict.length; i++) {
if (dict[i][0] == name) return dict[i][1];
}
}

const CANDID_UI_CANISTER_ID = "a4gq6-oaaaa-aaaab-qaa4q-cai";
const CANLISTA_CANISTER_ID = "kyhgh-oyaaa-aaaae-qaaha-cai";

const getCandidHack_interface = ({ IDL }) =>
IDL.Service({
__get_candid_interface_tmp_hack: IDL.Func([], [IDL.Text], ["query"]),
});

const getCandid_interface = ({ IDL }) =>
IDL.Service({
getCandid: IDL.Func(
[IDL.Principal],
[
IDL.Variant({
ok: IDL.Text,
err: IDL.Variant({ noCandidFound: IDL.null }),
}),
],
["query"]
),
});

// Try to decode reply using known did files
async function try_decode(canister_id, method_name, reply) {
try {
let did = canister_did_files[canister_id];
var did;
// Try fetch i using __get_candid_interface_tmp_hack.
try {
did = await Actor.createActor(getCandidHack_interface, {
agent,
canisterId: canister_id,
}).__get_candid_interface_tmp_hack();
} catch (e) {
console.log(e);
}
if (!did) {
// Try fetch i from canlista kyhgh-oyaaa-aaaae-qaaha-cai
try {
did = await Actor.createActor(getCandid_interface, {
agent,
canisterId: CANLISTA_CANISTER_ID,
}).getCandid(Principal.fromText(canister_id));
} catch (e) {
console.log(e);
}
if (did.ok) {
did = did.ok;
} else {
did = null;
}
}
if (did) {
let result = await agent.query(CANDID_UI_CANISTER_ID, {
methodName: "did_to_js",
Expand Down Expand Up @@ -246,6 +279,8 @@ async function try_decode(canister_id, method_name, reply) {
}
}
}
} catch (err) {}
} catch (err) {
console.log(err);
}
return reply;
}
250 changes: 0 additions & 250 deletions src/governance.did

This file was deleted.

Loading

0 comments on commit f424d9a

Please sign in to comment.