Skip to content

Commit

Permalink
Refactoring shared code into shared folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Jul 22, 2024
1 parent 3dbf382 commit 6d01474
Show file tree
Hide file tree
Showing 31 changed files with 389 additions and 151 deletions.
2 changes: 1 addition & 1 deletion rust-toolchain.toml
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" ]
16 changes: 12 additions & 4 deletions sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
],
"license": "GPL-3.0",
"type": "module",
"main": "./dist/node.js",
"browser": "./dist/index.js",
"main": "./dist/testnet/node.js",
"browser": "./dist/testnet/browser.js",
"exports": {
".": {
"node": "./dist/node.js",
"default": "./dist/index.js"
"node": "./dist/testnet/node.js",
"default": "./dist/testnet/browser.js"
},
"./testnet.js": {
"node": "./dist/testnet/node.js",
"default": "./dist/testnet/browser.js"
},
"./mainnet.js": {
"node": "./dist/mainnet/node.js",
"default": "./dist/mainnet/browser.js"
}
},
"files": [
Expand Down
8 changes: 4 additions & 4 deletions sdk/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import $package from "./package.json" assert { type: "json" };

export default {
input: {
index: "./src/index.ts",
worker: "./src/worker.ts",
node: "./src/node.ts",
"node-polyfill": "./src/node-polyfill.ts",
"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`,
Expand Down
2 changes: 0 additions & 2 deletions sdk/src/node.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions sdk/src/index.ts → sdk/src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {VerifyingKey, Metadata} from "@provablehq/wasm";
import {VerifyingKey, Metadata} from "@provablehq/wasm/testnet.js";

const KEY_STORE = Metadata.baseUrl();

Expand Down Expand Up @@ -158,7 +158,7 @@ export {
ViewKey,
initThreadPool,
verifyFunctionExecution,
} from "@provablehq/wasm";
} from "@provablehq/wasm/testnet.js";

export { initializeWasm };

Expand Down
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.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
logAndThrow,
ProgramManagerBase as WasmProgramManager, verifyFunctionExecution, AleoKeyProviderParams, CREDITS_PROGRAM_KEYS,
} from "./index";
import {Execution} from "@provablehq/wasm/dist/crates/aleo_wasm";
import {Execution} from "@provablehq/wasm/testnet.js";

/**
* Represents the options for executing a transaction in the Aleo network.
Expand Down Expand Up @@ -78,7 +78,7 @@ class ProgramManager {
constructor(host?: string | undefined, keyProvider?: FunctionKeyProvider | undefined, recordProvider?: RecordProvider | undefined) {
this.host = host ? host : 'https://api.explorer.aleo.org/v1';
this.networkClient = new AleoNetworkClient(this.host);

this.keyProvider = keyProvider ? keyProvider : new AleoKeyProvider();
this.recordProvider = recordProvider;
}
Expand Down
File renamed without changes.
File renamed without changes.
146 changes: 146 additions & 0 deletions sdk/src/shared/worker.ts
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);
}
}
Loading

0 comments on commit 6d01474

Please sign in to comment.