UNPKG

@provablehq/sdk

Version:

A Software Development Kit (SDK) for Zero-Knowledge Transactions

214 lines (213 loc) 9.95 kB
import { Key } from "../../constants.js"; import { CachedKeyPair, FunctionKeyPair } from "../../models/keyPair.js"; import { FunctionKeyProvider, KeySearchParams } from "./interface.js"; import { ProvingKey, VerifyingKey } from "../../wasm.js"; import { TransportFunction } from "../../utils/utils.js"; import { KeyStore } from "../keystore/interface.js"; type AleoKeyProviderInitParams = { proverUri?: string; verifierUri?: string; cacheKey?: string; }; /** * AleoKeyProviderParams search parameter for the AleoKeyProvider. It allows for the specification of a proverUri and * verifierUri to fetch keys via HTTP from a remote resource as well as a unique cacheKey to store the keys in memory. */ declare class AleoKeyProviderParams implements KeySearchParams { name: string | undefined; proverUri: string | undefined; verifierUri: string | undefined; cacheKey: string | undefined; /** * Create a new AleoKeyProviderParams object which implements the KeySearchParams interface. Users can optionally * specify a url for the proverUri & verifierUri to fetch keys via HTTP from a remote resource as well as a unique * cacheKey to store the keys in memory for future use. If no proverUri or verifierUri is specified, a cachekey must * be provided. * * @param { AleoKeyProviderInitParams } params - Optional search parameters */ constructor(params: { proverUri?: string; verifierUri?: string; cacheKey?: string; name?: string; }); } /** * AleoKeyProvider class. Implements the FunctionKeyProvider interface. Enables the retrieval of Aleo program proving and * verifying keys for the credits.aleo program over HTTP from official Aleo sources and storing and retrieving function * keys from a local memory cache. */ declare class AleoKeyProvider implements FunctionKeyProvider { cache: Map<string, CachedKeyPair>; cacheOption: boolean; keyUris: string; transport: TransportFunction; fetchBytes(url?: string): Promise<Uint8Array>; constructor(options?: { transport?: TransportFunction; }); keyStore(): Promise<KeyStore | undefined>; /** * Use local memory to store keys * * @param {boolean} useCache whether to store keys in local memory */ useCache(useCache: boolean): void; /** * Clear the key cache */ clearCache(): void; /** * Cache a set of keys. This will overwrite any existing keys with the same keyId. The user can check if a keyId * exists in the cache using the containsKeys method prior to calling this method if overwriting is not desired. * * @param {string} keyId access key for the cache * @param {FunctionKeyPair} keys keys to cache */ cacheKeys(keyId: string, keys: FunctionKeyPair): void; /** * Determine if a keyId exists in the cache * * @param {string} keyId keyId of a proving and verifying key pair * @returns {boolean} true if the keyId exists in the cache, false otherwise */ containsKeys(keyId: string): boolean; /** * Delete a set of keys from the cache * * @param {string} keyId keyId of a proving and verifying key pair to delete from memory * @returns {boolean} true if the keyId exists in the cache and was deleted, false if the key did not exist */ deleteKeys(keyId: string): boolean; /** * Get a set of keys from the cache * @param keyId keyId of a proving and verifying key pair * * @returns {FunctionKeyPair} Proving and verifying keys for the specified program */ getKeys(keyId: string): FunctionKeyPair; /** * Get arbitrary function keys from a provider * * @param {KeySearchParams} params parameters for the key search in form of: {proverUri: string, verifierUri: string, cacheKey: string} * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the specified program * * @example * // Create a new object which implements the KeyProvider interface * const networkClient = new AleoNetworkClient("https://api.provable.com/v2"); * const keyProvider = new AleoKeyProvider(); * const recordProvider = new NetworkRecordProvider(account, networkClient); * * // Initialize a program manager with the key provider to automatically fetch keys for value transfers * const programManager = new ProgramManager("https://api.provable.com/v2", keyProvider, recordProvider); * programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5); * * // Keys can also be fetched manually using the key provider * const keySearchParams = { "cacheKey": "myProgram:myFunction" }; * const [transferPrivateProvingKey, transferPrivateVerifyingKey] = await keyProvider.functionKeys(keySearchParams); */ functionKeys(params?: KeySearchParams): Promise<FunctionKeyPair>; /** * Returns the proving and verifying keys for a specified program from a specified url. * * @param {string} verifierUrl Url of the proving key * @param {string} proverUrl Url the verifying key * @param {string} cacheKey Key to store the keys in the cache * * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the specified program * * @example * // Create a new AleoKeyProvider object * const networkClient = new AleoNetworkClient("https://api.provable.com/v2"); * const keyProvider = new AleoKeyProvider(); * const recordProvider = new NetworkRecordProvider(account, networkClient); * * // Initialize a program manager with the key provider to automatically fetch keys for value transfers * const programManager = new ProgramManager("https://api.provable.com/v2", keyProvider, recordProvider); * programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5); * * // Keys can also be fetched manually * const [transferPrivateProvingKey, transferPrivateVerifyingKey] = await keyProvider.fetchKeys( * CREDITS_PROGRAM_KEYS.transfer_private.prover, * CREDITS_PROGRAM_KEYS.transfer_private.verifier, * ); */ fetchRemoteKeys(proverUrl: string, verifierUrl: string, cacheKey?: string): Promise<FunctionKeyPair>; /*** * Fetches the proving key from a remote source. * * @param proverUrl * @param cacheKey * * @returns {Promise<ProvingKey>} Proving key for the specified program */ fetchProvingKey(proverUrl: string, cacheKey?: string): Promise<ProvingKey>; fetchCreditsKeys(key: Key): Promise<FunctionKeyPair>; bondPublicKeys(): Promise<FunctionKeyPair>; bondValidatorKeys(): Promise<FunctionKeyPair>; claimUnbondPublicKeys(): Promise<FunctionKeyPair>; /** * Returns the proving and verifying keys for the transfer functions in the credits.aleo program * @param {string} visibility Visibility of the transfer function * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the transfer functions * * @example * // Create a new AleoKeyProvider * const networkClient = new AleoNetworkClient("https://api.provable.com/v2"); * const keyProvider = new AleoKeyProvider(); * const recordProvider = new NetworkRecordProvider(account, networkClient); * * // Initialize a program manager with the key provider to automatically fetch keys for value transfers * const programManager = new ProgramManager("https://api.provable.com/v2", keyProvider, recordProvider); * programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5); * * // Keys can also be fetched manually * const [transferPublicProvingKey, transferPublicVerifyingKey] = await keyProvider.transferKeys("public"); */ transferKeys(visibility: string): Promise<FunctionKeyPair>; /** * Returns the proving and verifying keys for the transfer_public function. * * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the transfer_public function */ transferPublicKeys(): Promise<FunctionKeyPair>; /** * Returns the proving and verifying keys for the inclusion proof. * * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the inclusion proof. */ inclusionKeys(): Promise<FunctionKeyPair>; /** * Returns the proving and verifying keys for the join function in the credits.aleo program * * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the join function */ joinKeys(): Promise<FunctionKeyPair>; /** * Returns the proving and verifying keys for the split function in the credits.aleo program * * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the split function * */ splitKeys(): Promise<FunctionKeyPair>; /** * Returns the proving and verifying keys for the fee_private function in the credits.aleo program * * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the fee function */ feePrivateKeys(): Promise<FunctionKeyPair>; /** * Returns the proving and verifying keys for the fee_public function in the credits.aleo program * * @returns {Promise<FunctionKeyPair>} Proving and verifying keys for the fee function */ feePublicKeys(): Promise<FunctionKeyPair>; /** * Gets a verifying key. If the verifying key is for a credits.aleo function, get it from the wasm cache otherwise * * @returns {Promise<VerifyingKey>} Verifying key for the function */ getVerifyingKey(verifierUri: string): Promise<VerifyingKey>; unBondPublicKeys(): Promise<FunctionKeyPair>; } export { AleoKeyProvider, AleoKeyProviderInitParams, AleoKeyProviderParams };