From 53b1a5197e37adc5414fe0ec6c85872cc29dc049 Mon Sep 17 00:00:00 2001 From: Pauan Date: Mon, 30 Sep 2024 19:54:55 -0700 Subject: [PATCH] Fixing Rollup warnings --- sdk/rollup.config.js | 1 + sdk/rollup.test.js | 5 ++ sdk/src/account.ts | 2 +- sdk/src/browser.ts | 140 ++++-------------------------- sdk/src/constants.ts | 113 ++++++++++++++++++++++++ sdk/src/function-key-provider.ts | 12 ++- sdk/src/network-client.ts | 11 ++- sdk/src/offline-key-provider.ts | 14 ++- sdk/src/program-manager.ts | 37 +++++--- sdk/src/record-provider.ts | 3 +- sdk/src/utils.ts | 6 ++ sdk/src/wasm.ts | 21 +++++ sdk/tests/record-provider.test.ts | 10 +-- 13 files changed, 215 insertions(+), 160 deletions(-) create mode 100644 sdk/src/constants.ts create mode 100644 sdk/src/wasm.ts diff --git a/sdk/rollup.config.js b/sdk/rollup.config.js index 585e85f93..c905172fc 100644 --- a/sdk/rollup.config.js +++ b/sdk/rollup.config.js @@ -32,6 +32,7 @@ export default networks.map((network) => { // Used by the SDK "comlink", `@provablehq/wasm/${network}.js`, + "core-js/proposals/json-parse-with-source.js", ], plugins: [ replace({ diff --git a/sdk/rollup.test.js b/sdk/rollup.test.js index 5af466a1f..8329d9029 100644 --- a/sdk/rollup.test.js +++ b/sdk/rollup.test.js @@ -39,6 +39,11 @@ export default networks.map((network) => { // Used by the SDK "comlink", `@provablehq/wasm/${network}.js`, + "core-js/proposals/json-parse-with-source.js", + + // Used by tests + "chai", + "sinon", ], plugins: [ replace({ diff --git a/sdk/src/account.ts b/sdk/src/account.ts index 961734e45..0fa64cab5 100644 --- a/sdk/src/account.ts +++ b/sdk/src/account.ts @@ -5,7 +5,7 @@ import { ViewKey, PrivateKeyCiphertext, RecordCiphertext, -} from "./browser"; +} from "./wasm"; interface AccountParam { privateKey?: string; diff --git a/sdk/src/browser.ts b/sdk/src/browser.ts index 3448378ef..6bff23642 100644 --- a/sdk/src/browser.ts +++ b/sdk/src/browser.ts @@ -1,116 +1,4 @@ import "./polyfill/shared"; -import {VerifyingKey, Metadata} from "@provablehq/wasm/%%NETWORK%%.js"; - -const KEY_STORE = Metadata.baseUrl(); - -interface Key { - name: string, - 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 { - name: metadata.name, - 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()), - getKey: function(key: string): Key { - if (this.hasOwnProperty(key)) { - return (this as any)[key] as Key; - } else { - throw new Error(`Key "${key}" not found.`); - } - } -}; - -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): never { - console.error(message); - throw new Error(message); -} import { Account } from "./account"; import { AleoNetworkClient, ProgramImports } from "./network-client"; @@ -149,6 +37,8 @@ export { createAleoWorker } from "./managed-worker"; export { ProgramManager } from "./program-manager"; +export { logAndThrow } from "./utils"; + export { Address, Execution as FunctionExecution, @@ -168,10 +58,23 @@ export { ViewKey, initThreadPool, verifyFunctionExecution, -} from "@provablehq/wasm/%%NETWORK%%.js"; +} from "./wasm"; export { initializeWasm }; +export { + Key, + 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, +} from "./constants"; + export { Account, AleoKeyProvider, @@ -185,7 +88,6 @@ export { FunctionKeyPair, FunctionKeyProvider, Input, - Key, KeySearchParams, NetworkRecordProvider, ProgramImports, @@ -196,14 +98,4 @@ export { 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/constants.ts b/sdk/src/constants.ts new file mode 100644 index 000000000..587107ff0 --- /dev/null +++ b/sdk/src/constants.ts @@ -0,0 +1,113 @@ +import {VerifyingKey, Metadata} from "./wasm"; + +export const KEY_STORE = Metadata.baseUrl(); + +export interface Key { + name: string, + 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 { + name: metadata.name, + locator: metadata.locator, + prover: metadata.prover, + verifier: metadata.verifier, + verifyingKey, + }; +} + +export 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()), + getKey: function(key: string): Key { + if (this.hasOwnProperty(key)) { + return (this as any)[key] as Key; + } else { + throw new Error(`Key "${key}" not found.`); + } + } +}; + +export const PRIVATE_TRANSFER_TYPES = new Set([ + "transfer_private", + "private", + "transferPrivate", + "transfer_private_to_public", + "privateToPublic", + "transferPrivateToPublic", +]); + +export 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", +]); + +export const PRIVATE_TRANSFER = new Set([ + "private", + "transfer_private", + "transferPrivate", +]); + +export const PRIVATE_TO_PUBLIC_TRANSFER = new Set([ + "private_to_public", + "privateToPublic", + "transfer_private_to_public", + "transferPrivateToPublic", +]); + +export const PUBLIC_TRANSFER = new Set([ + "public", + "transfer_public", + "transferPublic", +]); + +export const PUBLIC_TRANSFER_AS_SIGNER = new Set([ + "public_as_signer", + "transfer_public_as_signer", + "transferPublicAsSigner", +]); + +export const PUBLIC_TO_PRIVATE_TRANSFER = new Set([ + "public_to_private", + "publicToPrivate", + "transfer_public_to_private", + "transferPublicToPrivate", +]); diff --git a/sdk/src/function-key-provider.ts b/sdk/src/function-key-provider.ts index 97288ab47..890f8e778 100644 --- a/sdk/src/function-key-provider.ts +++ b/sdk/src/function-key-provider.ts @@ -1,6 +1,4 @@ import { - ProvingKey, - VerifyingKey, CREDITS_PROGRAM_KEYS, KEY_STORE, Key, @@ -8,8 +6,14 @@ import { PRIVATE_TO_PUBLIC_TRANSFER, PUBLIC_TRANSFER, PUBLIC_TO_PRIVATE_TRANSFER, - PUBLIC_TRANSFER_AS_SIGNER -} from "./browser"; + PUBLIC_TRANSFER_AS_SIGNER, +} from "./constants"; + +import { + ProvingKey, + VerifyingKey, +} from "./wasm"; + import { get } from "./utils"; type FunctionKeyPair = [ProvingKey, VerifyingKey]; diff --git a/sdk/src/network-client.ts b/sdk/src/network-client.ts index 5f8349033..9dc27f0b2 100644 --- a/sdk/src/network-client.ts +++ b/sdk/src/network-client.ts @@ -1,15 +1,14 @@ -import { get, post, parseJSON } from "./utils"; +import { get, post, parseJSON, logAndThrow } from "./utils"; +import { Account } from "./account"; +import { Block } from "./models/block"; +import { TransactionModel } from "./models/transactionModel"; import { - Account, - Block, RecordCiphertext, Program, RecordPlaintext, PrivateKey, Transaction, - TransactionModel, - logAndThrow -} from "./browser"; +} from "./wasm"; type ProgramImports = { [key: string]: string | Program }; diff --git a/sdk/src/offline-key-provider.ts b/sdk/src/offline-key-provider.ts index 377f8f917..805e72490 100644 --- a/sdk/src/offline-key-provider.ts +++ b/sdk/src/offline-key-provider.ts @@ -1,17 +1,23 @@ import { + CachedKeyPair, + FunctionKeyPair, FunctionKeyProvider, KeySearchParams, - FunctionKeyPair, - CachedKeyPair, +} from "./function-key-provider"; + +import { ProvingKey, VerifyingKey, +} from "./wasm"; + +import { CREDITS_PROGRAM_KEYS, PRIVATE_TRANSFER, PRIVATE_TO_PUBLIC_TRANSFER, PUBLIC_TRANSFER, PUBLIC_TO_PRIVATE_TRANSFER, - PUBLIC_TRANSFER_AS_SIGNER -} from "./browser"; + PUBLIC_TRANSFER_AS_SIGNER, +} from "./constants"; /** * Search parameters for the offline key provider. This class implements the KeySearchParams interface and includes diff --git a/sdk/src/program-manager.ts b/sdk/src/program-manager.ts index 748ded5f3..242d9cb97 100644 --- a/sdk/src/program-manager.ts +++ b/sdk/src/program-manager.ts @@ -1,27 +1,40 @@ +import { Account } from "./account"; +import { AleoNetworkClient, ProgramImports } from "./network-client"; + +import { + RecordProvider, + RecordSearchParams, +} from "./record-provider"; + import { - Account, AleoKeyProvider, - AleoNetworkClient, - ExecutionResponse, - FunctionExecution, - FunctionKeyProvider, + AleoKeyProviderParams, FunctionKeyPair, - OfflineQuery, + FunctionKeyProvider, KeySearchParams, +} from "./function-key-provider"; + +import { + ExecutionResponse, + Execution as FunctionExecution, + OfflineQuery, RecordPlaintext, - RecordProvider, - RecordSearchParams, PrivateKey, Program, - ProgramImports, ProvingKey, VerifyingKey, Transaction, + ProgramManager as WasmProgramManager, + verifyFunctionExecution, +} from "./wasm"; + +import { + CREDITS_PROGRAM_KEYS, PRIVATE_TRANSFER_TYPES, VALID_TRANSFER_TYPES, - logAndThrow, - ProgramManagerBase as WasmProgramManager, verifyFunctionExecution, AleoKeyProviderParams, CREDITS_PROGRAM_KEYS, -} from "./browser"; +} from "./constants"; + +import { logAndThrow } from "./utils"; /** * Represents the options for executing a transaction in the Aleo network. diff --git a/sdk/src/record-provider.ts b/sdk/src/record-provider.ts index 7acade552..cbb4bdca8 100644 --- a/sdk/src/record-provider.ts +++ b/sdk/src/record-provider.ts @@ -1,4 +1,5 @@ -import { logAndThrow, RecordPlaintext } from "./browser"; +import { RecordPlaintext } from "./wasm"; +import { logAndThrow } from "./utils"; import { Account } from "./account"; import { AleoNetworkClient } from "./network-client"; diff --git a/sdk/src/utils.ts b/sdk/src/utils.ts index 0be9d0506..d92420a45 100644 --- a/sdk/src/utils.ts +++ b/sdk/src/utils.ts @@ -1,3 +1,9 @@ +export function logAndThrow(message: string): never { + console.error(message); + throw new Error(message); +} + + export function parseJSON(json: string): any { function revive(key: string, value: any, context: any) { if (Number.isInteger(value)) { diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.ts new file mode 100644 index 000000000..8258e8a6f --- /dev/null +++ b/sdk/src/wasm.ts @@ -0,0 +1,21 @@ +export { + Address, + Execution, + ExecutionResponse, + Field, + OfflineQuery, + Metadata, + PrivateKey, + PrivateKeyCiphertext, + Program, + ProgramManager, + ProvingKey, + RecordCiphertext, + RecordPlaintext, + Signature, + Transaction, + VerifyingKey, + ViewKey, + initThreadPool, + verifyFunctionExecution, +} from "@provablehq/wasm/%%NETWORK%%.js"; diff --git a/sdk/tests/record-provider.test.ts b/sdk/tests/record-provider.test.ts index ad02555e3..8557ff965 100644 --- a/sdk/tests/record-provider.test.ts +++ b/sdk/tests/record-provider.test.ts @@ -1,7 +1,6 @@ import { expect } from "chai"; import {Account, AleoNetworkClient, BlockHeightSearch, NetworkRecordProvider} from "../src/node"; import {beaconPrivateKeyString} from "./data/account-data"; -import {log} from "console"; describe.skip('RecordProvider', () => { let account: Account; @@ -17,13 +16,8 @@ describe.skip('RecordProvider', () => { describe('Record provider', () => { it('should not find records where there are none', async () => { const params = new BlockHeightSearch(0, 100); - try { - const records = await recordProvider.findCreditsRecords([100, 200], true, [], params); - expect(records).equal([]) - } catch (e) { - log(e) - throw e; - } + const records = await recordProvider.findCreditsRecords([100, 200], true, [], params); + expect(records).equal([]); }); }); });