super_pispis
05/19/2023, 2:02 PMError: Script startup exceeded CPU time limit. [code: 10021] when using static import
when deploying. It is resolved when I use dynamic import (import('...
), but dynamic import apparently doesn't work with wasm. Any suggestions?super_pispis
05/19/2023, 2:07 PMjs
import { decodeAddress, encodeAddress } from '@polkadot/keyring';
import { hexToU8a, isHex } from '@polkadot/util';
// Checks whether provided wallet string is a valid address (from https://polkadot.js.org/docs/util-crypto/examples/validate-address/)
export async function validate(wallet: string): Promise<boolean>{
try {
encodeAddress(
isHex(wallet) ? hexToU8a(wallet) : decodeAddress(wallet)
);
} catch (e) {
return false;
}
return true;
};
Isaac McFadyen | YYZ01
05/19/2023, 2:59 PMsuper_pispis
05/19/2023, 2:59 PMIsaac McFadyen | YYZ01
05/19/2023, 3:01 PM@polkadot/keyring
or @polkadot/util
) is doing work in the global scope - outside of a fetch
handler.Isaac McFadyen | YYZ01
05/19/2023, 3:01 PMsuper_pispis
05/19/2023, 3:02 PMsuper_pispis
05/19/2023, 3:02 PMIsaac McFadyen | YYZ01
05/19/2023, 3:04 PMfetch
handler, yeah.
https://developers.cloudflare.com/workers/platform/web-assembly/Isaac McFadyen | YYZ01
05/19/2023, 3:04 PMIsaac McFadyen | YYZ01
05/19/2023, 3:05 PMsuper_pispis
05/19/2023, 3:19 PMjs
// Checks whether provided wallet string is a valid address (from https://polkadot.js.org/docs/util-crypto/examples/validate-address/)
export async function validate(wallet: string): Promise<boolean>{
try {
const polk_keyring = await import('@polkadot/keyring');
const polk_util = await import('@polkadot/util');
polk_keyring.encodeAddress(
polk_util.isHex(wallet) ? polk_util.hexToU8a(wallet) : polk_keyring.decodeAddress(wallet)
);
} catch (e) {
return false;
}
return true;
};
I get this error when using the function:
FATAL: Unable to initialize @polkadot/wasm-crypto:: WebAssembly.instantiate(): Wasm code generation disallowed by embedder
The compiled index.js does use Webassembly.instantiate:
js
try {
if (!wasmBytes2 || !wasmBytes2.length) {
throw new Error("No WebAssembly provided for initialization");
} else if (typeof WebAssembly !== "object" || typeof WebAssembly.instantiate !== "function") {
throw new Error("WebAssembly is not available in your environment");
}
const source = await WebAssembly.instantiate(wasmBytes2, {
wbg
});
...
I found the source that said you can't dynamically load wasm - but maybe I'm misinterpreting it? https://github.com/cloudflare/workers-sdk/issues/1366#issuecomment-1172255671
He says "Specifically, you can't initialise an instance from a string like above, it has to be a separate modules that's imported." - I don't know what he's referring to by 'like above' so maybe I'm wrong.super_pispis
05/19/2023, 3:36 PM.wasm
file. Is that the difference, and why my dynamic loading is failing?Isaac McFadyen | YYZ01
05/19/2023, 4:14 PMIsaac McFadyen | YYZ01
05/19/2023, 4:14 PMsuper_pispis
05/19/2023, 4:33 PMIsaac McFadyen | YYZ01
05/19/2023, 4:35 PMIsaac McFadyen | YYZ01
05/19/2023, 4:35 PMsuper_pispis
05/19/2023, 4:36 PMIsaac McFadyen | YYZ01
05/19/2023, 4:43 PMsuper_pispis
05/19/2023, 5:32 PMFATAL: Unable to initialize @polkadot/wasm-crypto:: WebAssembly.instantiate(): Wasm code generation disallowed by embedder
, which after our discussion makes sense. I'm just not sure why a static/global include (as shown in my second comment in this thread) DOES work (but times out, which I do understand why).Isaac McFadyen | YYZ01
05/19/2023, 5:32 PMsuper_pispis
05/19/2023, 5:33 PMsuper_pispis
05/19/2023, 5:34 PMsuper_pispis
05/19/2023, 9:14 PMnode_compat=true
.