-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a dry run feature #95
Comments
I had a go at 'transpiling' the template barebones program using the jco command line tool and it works great:
This gives us these files:
To use it with node js i also had to make a Then i added an index.js that looks like this: (sorry for my non-idiomatic javascript) import('./template_barebones.js').then(({ evaluate }) => {
function tryProgram(message, auxilaryData, config, oracledata) {
let error
try {
evaluate({ message, auxilaryData }, config, oracledata)
} catch (err) {
error = err
}
console.log(error? error : 'Successful evaluation')
}
// Succeeds (this example will sign any message of at least 10 bytes)
tryProgram(new Uint8Array(10))
// Fails
tryProgram(new Uint8Array(9))
}) I ran it like this:
Which is the desired output. You can see more clearly how to use it by looking at the typescript interface: export interface SignatureRequest {
message: Uint8Array,
auxilaryData?: Uint8Array,
}
export type Error = ErrorInvalidSignatureRequest | ErrorEvaluation;
export interface ErrorInvalidSignatureRequest {
tag: 'invalid-signature-request',
val: string,
}
export interface ErrorEvaluation {
tag: 'evaluation',
val: string,
}
export function evaluate(signatureRequest: SignatureRequest, config: Uint8Array | undefined, oracleData: Uint8Array | undefined): void;
export function customHash(data: Uint8Array): Uint8Array | undefined; What i can't figure out how to do is actually do the transpiling in JS without using the command line tool. I tried doing this: import { transpile } from '@bytecodealliance/jco/component';
import { readFileSync } from 'fs'
let wasmBytes = readFileSync('./template_barebones.wasm');
transpile(new Uint8Array(wasmBytes)).then(files => {
// this should give us the files from the bullet points above
}) I added jco as a dependency in the
I dont really understand whats wrong. If someone who knows JS well (@mixmix ) wants to take a look, the api doc for the |
Update i got transpiling to work in JS by bumping node to latest version (23.2.0) and adding the option import { transpile } from '@bytecodealliance/jco';
import { readFileSync } from 'fs'
let wasmBytes = readFileSync('./template_barebones.wasm');
transpile(new Uint8Array(wasmBytes), { wasiShim: false }).then((wit) => console.log(wit)) This will just dump out the files as Uint8Arrays:
We still need to figure out how to dynamically import them in order to evaluate the program. |
|
@FarafonovBogdan thank you, i tried this and it works great. I am wondering if we can import the code without needing to write it to the filesystem first. Eg: i can import and run javascript like this: const moduleData = "export function hello() { console.log('hello') }"
const url = "data:text/javascript;base64," + btoa(moduleData)
import(url).then(mod => mod.hello()) But the problem is that the generated javascript from transpiling our program internally imports the wasm blob in the same way: const module0 = fetchCompile(new URL('./add.core.wasm', import.meta.url));
const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
let _fs;
async function fetchCompile (url) {
if (isNode) {
_fs = _fs || await import('node:fs/promises');
return WebAssembly.compile(await _fs.readFile(url));
}
return fetch(url).then(WebAssembly.compileStreaming);
} So i guess in a nodejs context the only way to run the transpiled code without writing it to the filesystem would involve modifying the generated code. |
Allowing the runtime to be run in the browser would create a dry run feature that can be shipped and useful for checking that configs and aux data work for programs to be used.
This would mitigate users inputting incorrect fields and having to re set the configs (also would help in the programs side but that doesn't need to be done in the browser so is definitely easier and arguably already exists)
If we aren't able to run the runtime in the browser a third party api solution is possible but less ideal
The text was updated successfully, but these errors were encountered: