Skip to content

Commit

Permalink
Adding in conditional compilation to the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Jul 25, 2024
1 parent 5f2344a commit 02e4b9b
Show file tree
Hide file tree
Showing 28 changed files with 208 additions and 427 deletions.
83 changes: 47 additions & 36 deletions sdk/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
],
};
});
2 changes: 1 addition & 1 deletion sdk/src/shared/account.ts → sdk/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ViewKey,
PrivateKeyCiphertext,
RecordCiphertext,
} from "./index";
} from "./browser";

interface AccountParam {
privateKey?: string;
Expand Down
30 changes: 15 additions & 15 deletions sdk/src/testnet/browser.ts → sdk/src/browser.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -158,7 +158,7 @@ export {
ViewKey,
initThreadPool,
verifyFunctionExecution,
} from "@provablehq/wasm/testnet.js";
} from "@provablehq/wasm/%%NETWORK%%.js";

export { initializeWasm };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
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.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Transaction,
TransactionModel,
logAndThrow
} from "./index";
} from "./browser";

type ProgramImports = { [key: string]: string | Program };

Expand All @@ -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;
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions sdk/src/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "./node-polyfill";
export * from "./browser";
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 @@ -3,6 +3,7 @@ import {
AleoKeyProvider,
AleoNetworkClient,
ExecutionResponse,
FunctionExecution,
FunctionKeyProvider,
FunctionKeyPair,
OfflineQuery,
Expand All @@ -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.
Expand Down Expand Up @@ -1219,7 +1219,7 @@ class ProgramManager {
*/
verifyExecution(executionResponse: ExecutionResponse): boolean {
try {
const execution = <Execution>executionResponse.getExecution();
const execution = <FunctionExecution>executionResponse.getExecution();
const function_id = executionResponse.getFunctionId();
const program = executionResponse.getProgram();
const verifyingKey = executionResponse.getVerifyingKey();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logAndThrow, RecordPlaintext } from "./index";
import { logAndThrow, RecordPlaintext } from "./browser";
import { Account } from "./account";
import { AleoNetworkClient } from "./network-client";

Expand Down
Loading

0 comments on commit 02e4b9b

Please sign in to comment.