Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Sep 9, 2023
1 parent d5b397a commit 64527a3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
34 changes: 23 additions & 11 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,33 @@ function logDiags(diagnostics: Diagnostics[], logger: ILoggingStore) {
});
}

export async function extractCandidFromWasm(
wasm: Uint8Array
): Promise<ArrayBuffer> {
const mod = await WebAssembly.compile(wasm);
let section = WebAssembly.Module.customSections(
mod,
"icp:public candid:args"
);
function get_wasm_metadata(
wasm: WebAssembly.Module,
name: string
): string | undefined {
let section = WebAssembly.Module.customSections(wasm, `icp:public ${name}`);
if (section.length === 0) {
section = WebAssembly.Module.customSections(mod, "icp:private candid:args");
section = WebAssembly.Module.customSections(wasm, `icp:private ${name}`);
}
if (section.length === 0) {
throw new Error("Cannot find candid:args metadata in Wasm module");
return undefined;
}
const decoder = new TextDecoder();
const bytes = new Uint8Array(section[0]);
const str = decoder.decode(bytes);
return str;
}

export async function extractCandidFromWasm(
wasm: Uint8Array
): Promise<[string, undefined | string]> {
const mod = await WebAssembly.compile(wasm);
const serv = get_wasm_metadata(mod, "candid:service");
if (!serv) {
throw new Error("Cannot find candid:service metadata in Wasm module");
}
return section;
const init = get_wasm_metadata(mod, "candid:args");
return [serv, init];
}

export async function compileCandid(
Expand Down
28 changes: 20 additions & 8 deletions src/components/CanisterModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function CanisterModal({ isOpen, close, deploySetter }) {

async function deployWasm() {
if (!candid || !wasm) return;
setError("");
const candidJS = await didToJs(candid);
const init = candidJS.init({ IDL });
await close();
Expand Down Expand Up @@ -157,8 +158,17 @@ export function CanisterModal({ isOpen, close, deploySetter }) {
const wasm = new Uint8Array(reader.result);
setWasm(wasm);
try {
const init = await extractCandidFromWasm(wasm);
console.log(init);
const [serv, init] = await extractCandidFromWasm(wasm);
if (init) {
const candid = (await didjs.merge_init_args(serv, init))[0];
if (!candid) {
setError("Cannot merge candid:args with candid:service from Wasm");
return;
}
setCandid(candid);
} else {
setCandid(serv);
}
} catch (e) {
setError(e.toString());
}
Expand Down Expand Up @@ -238,12 +248,14 @@ export function CanisterModal({ isOpen, close, deploySetter }) {
accept=".wasm"
onChange={handleWasmUpload}
/>
<Field
type="file"
labelText="Upload did file"
accept=".did"
onChange={handleDidUpload}
/>
{error && (
<Field
type="file"
labelText="Upload did file"
accept=".did"
onChange={handleDidUpload}
/>
)}
{error && <Error>{error}</Error>}
<ButtonContainer>
<MyButton variant="primary" onClick={deployWasm}>
Expand Down
5 changes: 5 additions & 0 deletions src/didjs.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export const idlFactory = ({ IDL }) => {
[IDL.Variant({ Ok: IDL.Null, Err: IDL.Text })],
["query"]
),
merge_init_args: IDL.Func(
[IDL.Text, IDL.Text],
[IDL.Opt(IDL.Text)],
["query"]
),
});
};
export const init = ({ IDL }) => {
Expand Down

0 comments on commit 64527a3

Please sign in to comment.