diff --git a/sdk/rollup.config.js b/sdk/rollup.config.js index d761599c2..b430f4a7c 100644 --- a/sdk/rollup.config.js +++ b/sdk/rollup.config.js @@ -2,39 +2,50 @@ import typescript from "rollup-plugin-typescript2"; import replace from "@rollup/plugin-replace"; import $package from "./package.json" assert { type: "json" }; -export default { - input: { - "testnet/browser": "./src/testnet/browser.ts", - "testnet/worker": "./src/shared/worker.ts", - "testnet/node": "./src/testnet/node.ts", - "node-polyfill": "./src/shared/node-polyfill.ts", - }, - output: { - dir: `dist`, - format: "es", - sourcemap: true, - }, - external: [ - "node:worker_threads", - "node:os", - "node:fs", - "node:crypto", - "mime/lite.js", - "sync-request", - "comlink", - "@provablehq/wasm", - ], - plugins: [ - replace({ - preventAssignment: true, - delimiters: ['', ''], - values: { - '%%VERSION%%': $package.version, - }, - }), - typescript({ - tsconfig: "tsconfig.json", - clean: true, - }), - ], -}; +const networks = [ + "testnet", + "mainnet", +]; + +export default networks.map((network) => { + return { + input: { + "node-polyfill": "./src/node-polyfill.ts", + "browser": "./src/browser.ts", + "worker": "./src/worker.ts", + "node": "./src/node.ts", + }, + output: { + dir: `dist/${network}`, + format: "es", + sourcemap: true, + }, + external: [ + // Used by node-polyfill + "node:worker_threads", + "node:os", + "node:fs", + "node:crypto", + "mime/lite.js", + "sync-request", + + // Used by the SDK + "comlink", + `@provablehq/wasm/${network}.js`, + ], + plugins: [ + replace({ + preventAssignment: true, + delimiters: ['', ''], + values: { + '%%VERSION%%': $package.version, + '%%NETWORK%%': network, + }, + }), + typescript({ + tsconfig: "tsconfig.json", + clean: true, + }), + ], + }; +}); diff --git a/sdk/src/shared/account.ts b/sdk/src/account.ts similarity index 99% rename from sdk/src/shared/account.ts rename to sdk/src/account.ts index 13b57099c..62eb019a4 100644 --- a/sdk/src/shared/account.ts +++ b/sdk/src/account.ts @@ -5,7 +5,7 @@ import { ViewKey, PrivateKeyCiphertext, RecordCiphertext, -} from "./index"; +} from "./browser"; interface AccountParam { privateKey?: string; diff --git a/sdk/src/testnet/browser.ts b/sdk/src/browser.ts similarity index 84% rename from sdk/src/testnet/browser.ts rename to sdk/src/browser.ts index 8c743d299..526814fbe 100644 --- a/sdk/src/testnet/browser.ts +++ b/sdk/src/browser.ts @@ -1,4 +1,4 @@ -import {VerifyingKey, Metadata} from "@provablehq/wasm/testnet.js"; +import {VerifyingKey, Metadata} from "@provablehq/wasm/%%NETWORK%%.js"; const KEY_STORE = Metadata.baseUrl(); @@ -102,14 +102,14 @@ function logAndThrow(message: string): Error { throw message; } -import { Account } from "../shared/account"; -import { AleoNetworkClient, ProgramImports } from "../shared/network-client"; -import { Block } from "../shared/models/block"; -import { Execution } from "../shared/models/execution"; -import { Input } from "../shared/models/input"; -import { Output } from "../shared/models/output"; -import { TransactionModel } from "../shared/models/transactionModel"; -import { Transition } from "../shared/models/transition"; +import { Account } from "./account"; +import { AleoNetworkClient, ProgramImports } from "./network-client"; +import { Block } from "./models/block"; +import { Execution } from "./models/execution"; +import { Input } from "./models/input"; +import { Output } from "./models/output"; +import { TransactionModel } from "./models/transactionModel"; +import { Transition } from "./models/transition"; import { AleoKeyProvider, AleoKeyProviderParams, @@ -118,26 +118,26 @@ import { FunctionKeyPair, FunctionKeyProvider, KeySearchParams, -} from "../shared/function-key-provider"; +} from "./function-key-provider"; import { OfflineKeyProvider, OfflineSearchParams -} from "../shared/offline-key-provider"; +} from "./offline-key-provider"; import { BlockHeightSearch, NetworkRecordProvider, RecordProvider, RecordSearchParams, -} from "../shared/record-provider"; +} from "./record-provider"; // @TODO: This function is no longer needed, remove it. async function initializeWasm() { console.warn("initializeWasm is deprecated, you no longer need to use it"); } -export { createAleoWorker } from "../shared/managed-worker"; +export { createAleoWorker } from "./managed-worker"; -export { ProgramManager } from "../shared/program-manager"; +export { ProgramManager } from "./program-manager"; export { Address, @@ -158,7 +158,7 @@ export { ViewKey, initThreadPool, verifyFunctionExecution, -} from "@provablehq/wasm/testnet.js"; +} from "@provablehq/wasm/%%NETWORK%%.js"; export { initializeWasm }; diff --git a/sdk/src/shared/function-key-provider.ts b/sdk/src/function-key-provider.ts similarity index 99% rename from sdk/src/shared/function-key-provider.ts rename to sdk/src/function-key-provider.ts index 9a13d44a9..8128ec378 100644 --- a/sdk/src/shared/function-key-provider.ts +++ b/sdk/src/function-key-provider.ts @@ -8,7 +8,7 @@ import { PUBLIC_TRANSFER, PUBLIC_TO_PRIVATE_TRANSFER, PUBLIC_TRANSFER_AS_SIGNER -} from "./index"; +} from "./browser"; import { get } from "./utils"; type FunctionKeyPair = [ProvingKey, VerifyingKey]; diff --git a/sdk/src/shared/managed-worker.ts b/sdk/src/managed-worker.ts similarity index 100% rename from sdk/src/shared/managed-worker.ts rename to sdk/src/managed-worker.ts diff --git a/sdk/src/shared/models/block.ts b/sdk/src/models/block.ts similarity index 100% rename from sdk/src/shared/models/block.ts rename to sdk/src/models/block.ts diff --git a/sdk/src/shared/models/confirmed_transaction.ts b/sdk/src/models/confirmed_transaction.ts similarity index 100% rename from sdk/src/shared/models/confirmed_transaction.ts rename to sdk/src/models/confirmed_transaction.ts diff --git a/sdk/src/shared/models/execution.ts b/sdk/src/models/execution.ts similarity index 100% rename from sdk/src/shared/models/execution.ts rename to sdk/src/models/execution.ts diff --git a/sdk/src/shared/models/input.ts b/sdk/src/models/input.ts similarity index 100% rename from sdk/src/shared/models/input.ts rename to sdk/src/models/input.ts diff --git a/sdk/src/shared/models/output.ts b/sdk/src/models/output.ts similarity index 100% rename from sdk/src/shared/models/output.ts rename to sdk/src/models/output.ts diff --git a/sdk/src/shared/models/transactionModel.ts b/sdk/src/models/transactionModel.ts similarity index 100% rename from sdk/src/shared/models/transactionModel.ts rename to sdk/src/models/transactionModel.ts diff --git a/sdk/src/shared/models/transition.ts b/sdk/src/models/transition.ts similarity index 100% rename from sdk/src/shared/models/transition.ts rename to sdk/src/models/transition.ts diff --git a/sdk/src/shared/network-client.ts b/sdk/src/network-client.ts similarity index 99% rename from sdk/src/shared/network-client.ts rename to sdk/src/network-client.ts index cba2bd8f5..17e364feb 100644 --- a/sdk/src/shared/network-client.ts +++ b/sdk/src/network-client.ts @@ -9,7 +9,7 @@ import { Transaction, TransactionModel, logAndThrow -} from "./index"; +} from "./browser"; type ProgramImports = { [key: string]: string | Program }; @@ -35,7 +35,7 @@ class AleoNetworkClient { account: Account | undefined; constructor(host: string, options?: AleoNetworkClientOptions) { - this.host = host + "/testnet"; + this.host = host; if (options && options.headers) { this.headers = options.headers; diff --git a/sdk/src/shared/node-polyfill.ts b/sdk/src/node-polyfill.ts similarity index 100% rename from sdk/src/shared/node-polyfill.ts rename to sdk/src/node-polyfill.ts diff --git a/sdk/src/node.ts b/sdk/src/node.ts new file mode 100644 index 000000000..b94eab757 --- /dev/null +++ b/sdk/src/node.ts @@ -0,0 +1,2 @@ +import "./node-polyfill"; +export * from "./browser"; diff --git a/sdk/src/shared/offline-key-provider.ts b/sdk/src/offline-key-provider.ts similarity index 99% rename from sdk/src/shared/offline-key-provider.ts rename to sdk/src/offline-key-provider.ts index b8b404ea4..91fed9dbd 100644 --- a/sdk/src/shared/offline-key-provider.ts +++ b/sdk/src/offline-key-provider.ts @@ -11,7 +11,7 @@ import { PUBLIC_TRANSFER, PUBLIC_TO_PRIVATE_TRANSFER, PUBLIC_TRANSFER_AS_SIGNER -} from "./index"; +} from "./browser"; /** * Search parameters for the offline key provider. This class implements the KeySearchParams interface and includes diff --git a/sdk/src/shared/polyfill/crypto.ts b/sdk/src/polyfill/crypto.ts similarity index 100% rename from sdk/src/shared/polyfill/crypto.ts rename to sdk/src/polyfill/crypto.ts diff --git a/sdk/src/shared/polyfill/fetch.ts b/sdk/src/polyfill/fetch.ts similarity index 100% rename from sdk/src/shared/polyfill/fetch.ts rename to sdk/src/polyfill/fetch.ts diff --git a/sdk/src/shared/polyfill/worker.ts b/sdk/src/polyfill/worker.ts similarity index 100% rename from sdk/src/shared/polyfill/worker.ts rename to sdk/src/polyfill/worker.ts diff --git a/sdk/src/shared/polyfill/xmlhttprequest.ts b/sdk/src/polyfill/xmlhttprequest.ts similarity index 100% rename from sdk/src/shared/polyfill/xmlhttprequest.ts rename to sdk/src/polyfill/xmlhttprequest.ts diff --git a/sdk/src/shared/program-manager.ts b/sdk/src/program-manager.ts similarity index 99% rename from sdk/src/shared/program-manager.ts rename to sdk/src/program-manager.ts index 73c0b7bd8..0519b8d26 100644 --- a/sdk/src/shared/program-manager.ts +++ b/sdk/src/program-manager.ts @@ -3,6 +3,7 @@ import { AleoKeyProvider, AleoNetworkClient, ExecutionResponse, + FunctionExecution, FunctionKeyProvider, FunctionKeyPair, OfflineQuery, @@ -20,8 +21,7 @@ import { VALID_TRANSFER_TYPES, logAndThrow, ProgramManagerBase as WasmProgramManager, verifyFunctionExecution, AleoKeyProviderParams, CREDITS_PROGRAM_KEYS, -} from "./index"; -import {Execution} from "@provablehq/wasm/testnet.js"; +} from "./browser"; /** * Represents the options for executing a transaction in the Aleo network. @@ -1219,7 +1219,7 @@ class ProgramManager { */ verifyExecution(executionResponse: ExecutionResponse): boolean { try { - const execution = executionResponse.getExecution(); + const execution = executionResponse.getExecution(); const function_id = executionResponse.getFunctionId(); const program = executionResponse.getProgram(); const verifyingKey = executionResponse.getVerifyingKey(); diff --git a/sdk/src/shared/record-provider.ts b/sdk/src/record-provider.ts similarity index 99% rename from sdk/src/shared/record-provider.ts rename to sdk/src/record-provider.ts index cc508e851..de44a859a 100644 --- a/sdk/src/shared/record-provider.ts +++ b/sdk/src/record-provider.ts @@ -1,4 +1,4 @@ -import { logAndThrow, RecordPlaintext } from "./index"; +import { logAndThrow, RecordPlaintext } from "./browser"; import { Account } from "./account"; import { AleoNetworkClient } from "./network-client"; diff --git a/sdk/src/shared/index.ts b/sdk/src/shared/index.ts deleted file mode 100644 index 05f119a26..000000000 --- a/sdk/src/shared/index.ts +++ /dev/null @@ -1,198 +0,0 @@ -import {VerifyingKey, Metadata} from "@provablehq/wasm/testnet.js"; - -const KEY_STORE = Metadata.baseUrl(); - -interface Key { - locator: string, - prover: string, - verifier: string, - verifyingKey: () => VerifyingKey, -} - -function convert(metadata: Metadata): Key { - // This looks up the method name in VerifyingKey - const verifyingKey = (VerifyingKey as any)[metadata.verifyingKey]; - - if (!verifyingKey) { - throw new Error("Invalid method name: " + metadata.verifyingKey); - } - - return { - locator: metadata.locator, - prover: metadata.prover, - verifier: metadata.verifier, - verifyingKey, - }; -} - -const CREDITS_PROGRAM_KEYS = { - bond_public: convert(Metadata.bond_public()), - bond_validator: convert(Metadata.bond_validator()), - claim_unbond_public: convert(Metadata.claim_unbond_public()), - fee_private: convert(Metadata.fee_private()), - fee_public: convert(Metadata.fee_public()), - inclusion: convert(Metadata.inclusion()), - join: convert(Metadata.join()), - set_validator_state: convert(Metadata.set_validator_state()), - split: convert(Metadata.split()), - transfer_private: convert(Metadata.transfer_private()), - transfer_private_to_public: convert(Metadata.transfer_private_to_public()), - transfer_public: convert(Metadata.transfer_public()), - transfer_public_as_signer: convert(Metadata.transfer_public_as_signer()), - transfer_public_to_private: convert(Metadata.transfer_public_to_private()), - unbond_public: convert(Metadata.unbond_public()), -}; - -const PRIVATE_TRANSFER_TYPES = new Set([ - "transfer_private", - "private", - "transferPrivate", - "transfer_private_to_public", - "privateToPublic", - "transferPrivateToPublic", -]); -const VALID_TRANSFER_TYPES = new Set([ - "transfer_private", - "private", - "transferPrivate", - "transfer_private_to_public", - "privateToPublic", - "transferPrivateToPublic", - "transfer_public", - "transfer_public_as_signer", - "public", - "public_as_signer", - "transferPublic", - "transferPublicAsSigner", - "transfer_public_to_private", - "publicToPrivate", - "publicAsSigner", - "transferPublicToPrivate", -]); -const PRIVATE_TRANSFER = new Set([ - "private", - "transfer_private", - "transferPrivate", -]); -const PRIVATE_TO_PUBLIC_TRANSFER = new Set([ - "private_to_public", - "privateToPublic", - "transfer_private_to_public", - "transferPrivateToPublic", -]); -const PUBLIC_TRANSFER = new Set([ - "public", - "transfer_public", - "transferPublic", -]); -const PUBLIC_TRANSFER_AS_SIGNER = new Set([ - "public_as_signer", - "transfer_public_as_signer", - "transferPublicAsSigner", -]); -const PUBLIC_TO_PRIVATE_TRANSFER = new Set([ - "public_to_private", - "publicToPrivate", - "transfer_public_to_private", - "transferPublicToPrivate", -]); - -function logAndThrow(message: string): Error { - console.error(message); - throw message; -} - -import { Account } from "./account"; -import { AleoNetworkClient, ProgramImports } from "./network-client"; -import { Block } from "./models/block"; -import { Execution } from "./models/execution"; -import { Input } from "./models/input"; -import { Output } from "./models/output"; -import { TransactionModel } from "./models/transactionModel"; -import { Transition } from "./models/transition"; -import { - AleoKeyProvider, - AleoKeyProviderParams, - AleoKeyProviderInitParams, - CachedKeyPair, - FunctionKeyPair, - FunctionKeyProvider, - KeySearchParams, -} from "./function-key-provider"; -import { - OfflineKeyProvider, - OfflineSearchParams -} from "./offline-key-provider"; -import { - BlockHeightSearch, - NetworkRecordProvider, - RecordProvider, - RecordSearchParams, -} from "./record-provider"; - -// @TODO: This function is no longer needed, remove it. -async function initializeWasm() { - console.warn("initializeWasm is deprecated, you no longer need to use it"); -} - -export { createAleoWorker } from "./managed-worker"; - -export { ProgramManager } from "./program-manager"; - -export { - Address, - Execution as FunctionExecution, - ExecutionResponse, - Field, - OfflineQuery, - PrivateKey, - PrivateKeyCiphertext, - Program, - ProgramManager as ProgramManagerBase, - ProvingKey, - RecordCiphertext, - RecordPlaintext, - Signature, - Transaction, - VerifyingKey, - ViewKey, - initThreadPool, - verifyFunctionExecution, -} from "@provablehq/wasm/testnet.js"; - -export { initializeWasm }; - -export { - Account, - AleoKeyProvider, - AleoKeyProviderParams, - AleoKeyProviderInitParams, - AleoNetworkClient, - Block, - BlockHeightSearch, - CachedKeyPair, - Execution, - FunctionKeyPair, - FunctionKeyProvider, - Input, - KeySearchParams, - NetworkRecordProvider, - ProgramImports, - OfflineKeyProvider, - OfflineSearchParams, - Output, - RecordProvider, - RecordSearchParams, - TransactionModel, - Transition, - CREDITS_PROGRAM_KEYS, - KEY_STORE, - PRIVATE_TRANSFER, - PRIVATE_TO_PUBLIC_TRANSFER, - PRIVATE_TRANSFER_TYPES, - PUBLIC_TRANSFER, - PUBLIC_TRANSFER_AS_SIGNER, - PUBLIC_TO_PRIVATE_TRANSFER, - VALID_TRANSFER_TYPES, - logAndThrow, -}; diff --git a/sdk/src/shared/worker.ts b/sdk/src/shared/worker.ts deleted file mode 100644 index 5846ec6e4..000000000 --- a/sdk/src/shared/worker.ts +++ /dev/null @@ -1,146 +0,0 @@ -import type { FunctionKeyPair } from "./function-key-provider"; -import { ProgramManager } from "./program-manager"; -import { AleoKeyProvider, AleoKeyProviderParams} from "./function-key-provider"; -import { expose } from "comlink"; - - -export interface WorkerAPI { - executeOffline: ( - localProgram: string, - aleoFunction: string, - inputs: string[], - privateKey: string - ) => Promise<{ outputs: any; execution: string } | string>; - - getPrivateKey: () => Promise; -} - - -export abstract class WorkerImpl { - abstract from_string(key: string): any; - - abstract to_string(): string; - - abstract verifyFunctionExecution(execution: any, verifying_key: any, program: any, function_id: string): boolean; - - init() { - const defaultHost = "https://api.explorer.aleo.org/v1"; - const keyProvider = new AleoKeyProvider(); - const programManager = new ProgramManager( - defaultHost, - keyProvider, - undefined - ); - - keyProvider.useCache(true); - - let lastLocalProgram: string = ""; - - const executeOffline = async ( - localProgram: string, - aleoFunction: string, - inputs: string[], - privateKey: string, - proveExecution = false - ) => { - console.log("Web worker: Executing function locally..."); - const startTime = performance.now(); - - try { - // Ensure the program is valid and that it contains the function specified - const program = programManager.createProgramFromSource(localProgram); - if (program instanceof Error) { - throw "Error creating program from source"; - } - const program_id = program.id(); - if (!program.hasFunction(aleoFunction)) { - throw `Program ${program_id} does not contain function ${aleoFunction}`; - } - const cacheKey = `${program_id}:${aleoFunction}`; - - // Get the program imports - const imports = await programManager.networkClient.getProgramImports( - localProgram - ); - - if (imports instanceof Error) { - throw "Error getting program imports"; - } - // Get the proving and verifying keys for the function - if (lastLocalProgram !== localProgram) { - const keys = await programManager.synthesizeKeys( - localProgram, - aleoFunction, - inputs, - this.from_string(privateKey) - ); - programManager.keyProvider.cacheKeys(cacheKey, keys); - lastLocalProgram = localProgram; - } - - // Pass the cache key to the execute function - const keyParams = new AleoKeyProviderParams({ - cacheKey: cacheKey, - }); - - // Execute the function locally - const response = await programManager.run( - localProgram, - aleoFunction, - inputs, - proveExecution, - imports, - keyParams, - undefined, - undefined, - this.from_string(privateKey), - ); - - // Return the outputs to the main thread - console.log( - `Web worker: Local execution completed in ${ - performance.now() - startTime - } ms` - ); - const outputs = response.getOutputs(); - const execution = response.getExecution(); - let executionString = ""; - - const keys = keyProvider.getKeys(cacheKey); - - if (keys instanceof Error) { - throw "Could not get verifying key"; - } - - const verifyingKey = keys[1]; - - if (execution) { - this.verifyFunctionExecution( - execution, - verifyingKey, - program, - "hello" - ); - executionString = execution.toString(); - console.log("Execution verified successfully: " + execution); - } else { - executionString = ""; - } - - console.log(`Function execution response: ${outputs}`); - - return { outputs: outputs, execution: executionString }; - } catch (error) { - console.error(error); - return error ? error.toString() : "Unknown error"; - } - } - - const getPrivateKey = async () => { - return this.to_string(); - }; - - const workerAPI = { executeOffline, getPrivateKey }; - expose(workerAPI); - } -} diff --git a/sdk/src/testnet/node.ts b/sdk/src/testnet/node.ts deleted file mode 100644 index 0886f7cea..000000000 --- a/sdk/src/testnet/node.ts +++ /dev/null @@ -1,2 +0,0 @@ -import "../shared/node-polyfill"; -export * from "./browser"; diff --git a/sdk/src/testnet/worker.ts b/sdk/src/testnet/worker.ts deleted file mode 100644 index e8373a8a8..000000000 --- a/sdk/src/testnet/worker.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { initThreadPool, PrivateKey, verifyFunctionExecution } from "./browser"; -import { WorkerImpl } from "../shared/worker"; - -class TestnetWorker extends WorkerImpl { - from_string(key: string): any { - return PrivateKey.from_string(key); - } - - to_string(): string { - const privateKey = new PrivateKey(); - return privateKey.to_string(); - } - - verifyFunctionExecution(execution: any, verifying_key: any, program: any, function_id: string): boolean { - return verifyFunctionExecution(execution, verifying_key, program, function_id); - } -} - -await initThreadPool(); - -new TestnetWorker().init(); diff --git a/sdk/src/shared/utils.ts b/sdk/src/utils.ts similarity index 100% rename from sdk/src/shared/utils.ts rename to sdk/src/utils.ts diff --git a/sdk/src/worker.ts b/sdk/src/worker.ts new file mode 100644 index 000000000..23d8fce2d --- /dev/null +++ b/sdk/src/worker.ts @@ -0,0 +1,135 @@ +import {initThreadPool, ProgramManager, PrivateKey, verifyFunctionExecution, FunctionKeyPair} from "./browser"; +import { AleoKeyProvider, AleoKeyProviderParams} from "./function-key-provider"; +import { expose } from "comlink"; + +await initThreadPool(); + +const defaultHost = "https://api.explorer.aleo.org/v1"; +const keyProvider = new AleoKeyProvider(); +const programManager = new ProgramManager( + defaultHost, + keyProvider, + undefined +); + +keyProvider.useCache(true); + +let lastLocalProgram: string = ""; + +export interface WorkerAPI { + executeOffline: ( + localProgram: string, + aleoFunction: string, + inputs: string[], + privateKey: string + ) => Promise<{ outputs: any; execution: string } | string>; + + getPrivateKey: () => Promise; +} +async function executeOffline( + localProgram: string, + aleoFunction: string, + inputs: string[], + privateKey: string, + proveExecution = false +) { + console.log("Web worker: Executing function locally..."); + const startTime = performance.now(); + + try { + // Ensure the program is valid and that it contains the function specified + const program = programManager.createProgramFromSource(localProgram); + if (program instanceof Error) { + throw "Error creating program from source"; + } + const program_id = program.id(); + if (!program.hasFunction(aleoFunction)) { + throw `Program ${program_id} does not contain function ${aleoFunction}`; + } + const cacheKey = `${program_id}:${aleoFunction}`; + + // Get the program imports + const imports = await programManager.networkClient.getProgramImports( + localProgram + ); + + if (imports instanceof Error) { + throw "Error getting program imports"; + } + // Get the proving and verifying keys for the function + if (lastLocalProgram !== localProgram) { + const keys = await programManager.synthesizeKeys( + localProgram, + aleoFunction, + inputs, + PrivateKey.from_string(privateKey) + ); + programManager.keyProvider.cacheKeys(cacheKey, keys); + lastLocalProgram = localProgram; + } + + // Pass the cache key to the execute function + const keyParams = new AleoKeyProviderParams({ + cacheKey: cacheKey, + }); + + // Execute the function locally + const response = await programManager.run( + localProgram, + aleoFunction, + inputs, + proveExecution, + imports, + keyParams, + undefined, + undefined, + PrivateKey.from_string(privateKey), + ); + + // Return the outputs to the main thread + console.log( + `Web worker: Local execution completed in ${ + performance.now() - startTime + } ms` + ); + const outputs = response.getOutputs(); + const execution = response.getExecution(); + let executionString = ""; + + const keys = keyProvider.getKeys(cacheKey); + + if (keys instanceof Error) { + throw "Could not get verifying key"; + } + + const verifyingKey = keys[1]; + + if (execution) { + verifyFunctionExecution( + execution, + verifyingKey, + program, + "hello" + ); + executionString = execution.toString(); + console.log("Execution verified successfully: " + execution); + } else { + executionString = ""; + } + + console.log(`Function execution response: ${outputs}`); + + return { outputs: outputs, execution: executionString }; + } catch (error) { + console.error(error); + return error ? error.toString() : "Unknown error"; + } +} + +async function getPrivateKey() { + const privateKey = new PrivateKey(); + return privateKey.to_string(); +} + +const workerAPI = { executeOffline, getPrivateKey }; +expose(workerAPI);