-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring shared code into shared folder
- Loading branch information
Showing
31 changed files
with
389 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[toolchain] | ||
channel = "nightly-2024-05-14" | ||
channel = "nightly-2024-07-21" | ||
components = [ "rust-std", "rust-src" ] | ||
targets = [ "wasm32-unknown-unknown" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
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<any>; | ||
} | ||
|
||
|
||
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 = <FunctionKeyPair>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); | ||
} | ||
} |
Oops, something went wrong.