Skip to main content

Offline Key Provider

Overview

A key provider meant for building transactions offline on devices such as hardware wallets. This key provider is not able to contact the internet for key material and instead relies on the user to insert Aleo function proving & verifying keys from local storage prior to usage.

Kind: global class

Class OfflineSearchParams

Search parameters for the offline key provider. This class implements the KeySearchParams interface and includes a convenience method for creating a new instance of this class for each function of the credits.aleo program.

Constructor

Create a new OfflineSearchParams instance for searching keys in the offline key provider cache.

new OfflineSearchParams(cacheKey, verifyCreditsKeys)
ParamTypeDescription
cacheKeystringKey used to store the local function proving & verifying keys. This should be stored under the naming convention "programName/functionName" (i.e. "myprogram.aleo/myfunction")
verifyCreditsKeysbooleanOptional. Whether to verify the keys against the credits.aleo program, defaults to false, but should be set to true if using keys from the credits.aleo program

Example

// If storing a key for a custom program function
const offlineSearchParams = new OfflineSearchParams("myprogram.aleo/myfunction");

// If storing a key for a credits.aleo program function
const bondPublicKeyParams = OfflineSearchParams.bondPublicKeyParams();

Static Methods

bondPublicKeyParams

Creates pre-configured OfflineSearchParams for the bond_public function of the credits.aleo program.

OfflineSearchParams.bondPublicKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the bond_public function

bondValidatorKeyParams

Creates pre-configured OfflineSearchParams for the bond_validator function of the credits.aleo program.

OfflineSearchParams.bondValidatorKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the bond_validator function

claimUnbondPublicKeyParams

Creates pre-configured OfflineSearchParams for the claim_unbond_public function of the credits.aleo program.

OfflineSearchParams.claimUnbondPublicKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the claim_unbond_public function

feePrivateKeyParams

Creates pre-configured OfflineSearchParams for the fee_private function of the credits.aleo program.

OfflineSearchParams.feePrivateKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the fee_private function

feePublicKeyParams

Creates pre-configured OfflineSearchParams for the fee_public function of the credits.aleo program.

OfflineSearchParams.feePublicKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the fee_public function

inclusionKeyParams

Creates pre-configured OfflineSearchParams for the inclusion prover function.

OfflineSearchParams.inclusionKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the inclusion prover function

joinKeyParams

Creates pre-configured OfflineSearchParams for the join function of the credits.aleo program.

OfflineSearchParams.joinKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the join function

setValidatorStateKeyParams

Creates pre-configured OfflineSearchParams for the set_validator_state function of the credits.aleo program.

OfflineSearchParams.setValidatorStateKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the set_validator_state function

splitKeyParams

Creates pre-configured OfflineSearchParams for the split function of the credits.aleo program.

OfflineSearchParams.splitKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the split function

transferPrivateKeyParams

Creates pre-configured OfflineSearchParams for the transfer_private function of the credits.aleo program.

OfflineSearchParams.transferPrivateKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the transfer_private function

transferPrivateToPublicKeyParams

Creates pre-configured OfflineSearchParams for the transfer_private_to_public function of the credits.aleo program.

OfflineSearchParams.transferPrivateToPublicKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the transfer_private_to_public function

transferPublicKeyParams

Creates pre-configured OfflineSearchParams for the transfer_public function of the credits.aleo program.

OfflineSearchParams.transferPublicKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the transfer_public function

transferPublicAsSignerKeyParams

Creates pre-configured OfflineSearchParams for the transfer_public_as_signer function of the credits.aleo program.

OfflineSearchParams.transferPublicAsSignerKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the transfer_public_as_signer function

transferPublicToPrivateKeyParams

Creates pre-configured OfflineSearchParams for the transfer_public_to_private function of the credits.aleo program.

OfflineSearchParams.transferPublicToPrivateKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the transfer_public_to_private function

unbondPublicKeyParams

Creates pre-configured OfflineSearchParams for the unbond_public function of the credits.aleo program.

OfflineSearchParams.unbondPublicKeyParams()
ParametersTypeDescription
returnOfflineSearchParamsSearch params for the unbond_public function

Class OfflineKeyProvider

Constructor

Create a new OfflineKeyProvider instance for managing keys offline on devices such as hardware wallets.

new OfflineKeyProvider()

Example

import { OfflineKeyProvider } from "@provablehq/sdk/mainnet.js";

// Create an offline key provider
const offlineKeyProvider = new OfflineKeyProvider();

OfflineKeyProvider Example

// Create an offline program manager
const programManager = new ProgramManager();

// Create a temporary account for the execution of the program
const account = new Account();
programManager.setAccount(account);

// Create the proving keys from the key bytes on the offline machine
console.log("Creating proving keys from local key files");
const program = "program hello_hello.aleo; function hello: input r0 as u32.public; input r1 as u32.private; add r0 r1 into r2; output r2 as u32.private;";
const myFunctionProver = await getLocalKey("/path/to/my/function/hello_hello.prover");
const myFunctionVerifier = await getLocalKey("/path/to/my/function/hello_hello.verifier");
const feePublicProvingKeyBytes = await getLocalKey("/path/to/credits.aleo/feePublic.prover");

const myFunctionProvingKey = ProvingKey.fromBytes(myFunctionProver);
const myFunctionVerifyingKey = VerifyingKey.fromBytes(myFunctionVerifier);
const feePublicProvingKey = ProvingKey.fromBytes(feePublicProvingKeyBytes);

// Create an offline key provider
console.log("Creating offline key provider");
const offlineKeyProvider = new OfflineKeyProvider();

// Cache the keys
// Cache the proving and verifying keys for the custom hello function
offlineKeyProvider.cacheKeys("hello_hello.aleo/hello", [myFunctionProvingKey, myFunctionVerifyingKey]);

// Cache the proving key for the fee_public function (the verifying key is automatically cached)
offlineKeyProvider.insertFeePublicKeys(feePublicProvingKey);

// Create an offline query using the latest state root in order to create the inclusion proof
const offlineQuery = new OfflineQuery("latestStateRoot");

// Insert the key provider into the program manager
programManager.setKeyProvider(offlineKeyProvider);

// Create the offline search params
const offlineSearchParams = new OfflineSearchParams("hello_hello.aleo/hello");

// Create the offline transaction
const offlineExecuteTx = await programManager.buildExecutionTransaction({
programName: "hello_hello.aleo",
functionName: "hello",
priorityFee: 1,
privateFee: false,
inputs: ["5u32", "5u32"],
keySearchParams: offlineSearchParams,
offlineQuery: offlineQuery,
program: program
});

// Broadcast the transaction later on a machine with internet access
const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
const txId = await networkClient.submitTransaction(offlineExecuteTx);

Methods

bondPublicKeys

Get bond_public function keys from the credits.aleo program. The keys must be cached prior to calling this method for it to work.

bondPublicKeys()
ParametersTypeDescription
returnPromise.<FunctionKeyPair>Proving and verifying keys for the bond_public function

bondValidatorKeys

Get bond_validator function keys from the credits.aleo program. The keys must be cached prior to calling this method for it to work.

bondValidatorKeys()
ParametersTypeDescription
returnPromise.<FunctionKeyPair>Proving and verifying keys for the bond_public function

cacheKeys

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.

cacheKeys(keyId, keys)
ParametersTypeDescription
keyIdstringaccess key for the cache
keysFunctionKeyPairkeys to cache

claimUnbondPublicKeys

Get unbond_public function keys from the credits.aleo program. The keys must be cached prior to calling this method for it to work.

claimUnbondPublicKeys()
ParametersTypeDescription
returnPromise.<FunctionKeyPair>Proving and verifying keys for the unbond_public function

functionKeys

Get arbitrary function key from the offline key provider cache.

functionKeys(params)
ParametersTypeDescription
paramsKeySearchParamsOptional search parameters for the key provider
returnPromise.<FunctionKeyPair>Proving and verifying keys for the specified program

Examples

/// First cache the keys from local offline resources
const offlineKeyProvider = new OfflineKeyProvider();
const myFunctionVerifyingKey = VerifyingKey.fromString("verifier...");
const myFunctionProvingKeyBytes = await readBinaryFile('./resources/myfunction.prover');
const myFunctionProvingKey = ProvingKey.fromBytes(myFunctionProvingKeyBytes);

/// Cache the keys for future use with a memorable locator
offlineKeyProvider.cacheKeys("myprogram.aleo/myfunction", [myFunctionProvingKey, myFunctionVerifyingKey]);

/// When they're needed, retrieve the keys from the cache

/// First create a search parameter object with the same locator used to cache the keys
const keyParams = new OfflineSearchParams("myprogram.aleo/myfunction");

/// Then retrieve the keys
const [myFunctionProver, myFunctionVerifier] = await offlineKeyProvider.functionKeys(keyParams);

verifyCreditsKeys

Determines if the keys for a given credits function match the expected keys.

verifyCreditsKeys(locator, provingKey, verifyingKey) ⇒ boolean
ParametersTypeDescription
locatorstringThe locator of the credits function (e.g. "credits.aleo/transfer_public")
provingKeyProvingKeyThe proving key to verify
verifyingKeyVerifyingKeyThe verifying key to verify
returnbooleanWhether the keys match the expected keys

feePrivateKeys

Get fee_private function keys from the credits.aleo program. The keys must be cached prior to calling this method for it to work.

feePrivateKeys()
ParametersTypeDescription
returnPromise.<FunctionKeyPair>Proving and verifying keys for the join function

feePublicKeys

Get fee_public function keys from the credits.aleo program. The keys must be cached prior to calling this method for it to work.

feePublicKeys()
ParametersTypeDescription
returnPromise.<FunctionKeyPair>Proving and verifying keys for the fee_public function

inclusionKeys

Get the inclusion prover keys. The keys must be cached prior to calling this method for it to work.

inclusionKeys()
ParametersTypeDescription
returnPromise.<FunctionKeyPair>Proving and verifying keys for the inclusion prover

joinKeys

Get join function keys from the credits.aleo program. The keys must be cached prior to calling this method for it to work.

joinKeys()
ParametersTypeDescription
returnPromise.<FunctionKeyPair>Proving and verifying keys for the join function

splitKeys

Get split function keys from the credits.aleo program. The keys must be cached prior to calling this method for it to work.

splitKeys()
ParametersTypeDescription
returnPromise.<FunctionKeyPair>Proving and verifying keys for the join function

transferKeys

Get keys for a variant of the transfer function from the credits.aleo program.

transferKeys(visibility)
ParametersTypeDescription
visibilitystringVisibility of the transfer function (private, public, privateToPublic, publicToPrivate)
returnPromise.<FunctionKeyPair>Proving and verifying keys for the specified transfer function

Examples

// Create a new OfflineKeyProvider
const offlineKeyProvider = new OfflineKeyProvider();

// Cache the keys for future use with the official locator
const transferPublicProvingKeyBytes = await readBinaryFile('./resources/transfer_public.prover.a74565e');
const transferPublicProvingKey = ProvingKey.fromBytes(transferPublicProvingKeyBytes);

// Cache the transfer_public keys for future use with the OfflinKeyProvider's convenience method for
// transfer_public (the verifying key will be cached automatically)
offlineKeyProvider.insertTransferPublicKeys(transferPublicProvingKey);

/// When they're needed, retrieve the keys from the cache
const [transferPublicProvingKey, transferPublicVerifyingKey] = await keyProvider.transferKeys("public");

unBondPublicKeys

Get unbond_public function keys from the credits.aleo program

unBondPublicKeys()
ParametersTypeDescription
returnPromise.<FunctionKeyPair>Proving and verifying keys for the join function

insertBondPublicKeys

Insert the proving and verifying keys for the bond_public function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for bond_public before inserting them into the cache.

insertBondPublicKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the bond_public function

insertClaimUnbondPublicKeys

Insert the proving and verifying keys for the claim_unbond_public function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for claim_unbond_public before inserting them into the cache.

insertClaimUnbondPublicKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the claim_unbond_public function

insertFeePrivateKeys

Insert the proving and verifying keys for the fee_private function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for fee_private before inserting them into the cache.

insertFeePrivateKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the fee_private function

insertFeePublicKeys

Insert the proving and verifying keys for the fee_public function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for fee_public before inserting them into the cache.

insertFeePublicKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the fee_public function

insertInclusionKeys

Insert the proving and verifying keys for the inclusion prover into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for the inclusion prover.

insertInclusionKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the inclusion prover

insertJoinKeys

Insert the proving and verifying keys for the join function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for join before inserting them into the cache.

insertJoinKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the join function

insertSetValidatorStateKeys

Insert the proving and verifying keys for the set_validator_state function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for set_validator_state before inserting them into the cache.

insertSetValidatorStateKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the set_validator_state function

insertSplitKeys

Insert the proving and verifying keys for the split function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for split before inserting them into the cache.

insertSplitKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the split function

insertTransferPrivateKeys

Insert the proving and verifying keys for the transfer_private function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for transfer_private before inserting them into the cache.

insertTransferPrivateKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the transfer_private function

insertTransferPrivateToPublicKeys

Insert the proving and verifying keys for the transfer_private_to_public function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for transfer_private_to_public before inserting them into the cache.

insertTransferPrivateToPublicKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the transfer_private_to_public function

insertTransferPublicKeys

Insert the proving and verifying keys for the transfer_public function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for transfer_public before inserting them into the cache.

insertTransferPublicKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the transfer_public function

insertTransferPublicToPrivateKeys

Insert the proving and verifying keys for the transfer_public_to_private function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for transfer_public_to_private before inserting them into the cache.

insertTransferPublicToPrivateKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the transfer_public_to_private function

insertUnbondPublicKeys

Insert the proving and verifying keys for the unbond_public function into the cache. Only the proving key needs to be inserted, the verifying key is automatically inserted by the SDK. This function will automatically check that the keys match the expected checksum for unbond_public before inserting them into the cache.

insertUnbondPublicKeys(provingKey)
ParametersTypeDescription
provingKeyProvingKeyThe proving key for the unbond_public function