Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Ensure credits.aleo keys are fetched from SnarkVM instead of a remote #907

Merged
merged 9 commits into from
Aug 3, 2024
Prev Previous commit
Next Next commit
Fix credits verifying key convenience functions
iamalwaysuncomfortable committed Aug 2, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 1f920cb57dca1f06bef4746a5d7b01c752d8a5e1
31 changes: 0 additions & 31 deletions index.js

This file was deleted.

6 changes: 3 additions & 3 deletions sdk/tests/key-provider.test.ts
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ describe('KeyProvider', () => {
it('Should use cache when set and not use it when not', async () => {
// Ensure the cache properly downloads and stores keys
keyProvider.useCache(true);
const [provingKey, verifyingKey] = <FunctionKeyPair>await keyProvider.transferKeys("private");
const [provingKey, verifyingKey] = <FunctionKeyPair>await keyProvider.feePublicKeys();
expect(keyProvider.cache.size).toBe(1);
expect(provingKey).toBeInstanceOf(ProvingKey);
expect(verifyingKey).toBeInstanceOf(VerifyingKey);
@@ -35,15 +35,15 @@ describe('KeyProvider', () => {
expect(cachedVerifyingKey).toBeInstanceOf(Uint8Array);

// Ensure the functionKeys method to get the keys and that the cache is used to do so
const [fetchedProvingKey, fetchedVerifyingKey] = <FunctionKeyPair>await keyProvider.fetchCreditsKeys(CREDITS_PROGRAM_KEYS.transfer_private)
const [fetchedProvingKey, fetchedVerifyingKey] = <FunctionKeyPair>await keyProvider.fetchCreditsKeys(CREDITS_PROGRAM_KEYS.fee_public)
expect(keyProvider.cache.size).toBe(1);
expect(fetchedProvingKey).toBeInstanceOf(ProvingKey);
expect(fetchedVerifyingKey).toBeInstanceOf(VerifyingKey);

// Clear the cache and turn it off to ensure the keys are re-downloaded and the cache is not used
keyProvider.clearCache();
keyProvider.useCache(false);
const [redownloadedProvingKey, redownloadedVerifyingKey] = <FunctionKeyPair>await keyProvider.transferKeys("private");
const [redownloadedProvingKey, redownloadedVerifyingKey] = <FunctionKeyPair>await keyProvider.feePublicKeys();
expect(keyProvider.cache.size).toBe(0);
expect(redownloadedProvingKey).toBeInstanceOf(ProvingKey);
expect(redownloadedVerifyingKey).toBeInstanceOf(VerifyingKey);
64 changes: 21 additions & 43 deletions wasm/src/programs/verifying_key/credits.rs
Original file line number Diff line number Diff line change
@@ -18,152 +18,130 @@ use super::*;

#[wasm_bindgen]
impl VerifyingKey {
fn get_credits_verifying_key(name: &str) -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key(name.to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
}

/// Returns the verifying key for the bond_public function
///
/// @returns {VerifyingKey} Verifying key for the bond_public function
#[wasm_bindgen(js_name = "bondPublicVerifier")]
pub fn bond_public_verifier() -> VerifyingKey {
VerifyingKey::from_bytes(&snarkvm_parameters::testnet::BondPublicVerifier::load_bytes().unwrap()).unwrap()
VerifyingKey::get_credits_verifying_key("bond_public")
}

/// Returns the verifying key for the bond_validator function
///
/// @returns {VerifyingKey} Verifying key for the bond_validator function
#[wasm_bindgen(js_name = "bondValidatorVerifier")]
pub fn bond_validator_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("bond_validator")
}

/// Returns the verifying key for the claim_delegator function
///
/// @returns {VerifyingKey} Verifying key for the claim_unbond_public function
#[wasm_bindgen(js_name = "claimUnbondPublicVerifier")]
pub fn claim_unbond_public_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("claim_unbond_public")
}

/// Returns the verifying key for the fee_private function
///
/// @returns {VerifyingKey} Verifying key for the fee_private function
#[wasm_bindgen(js_name = "feePrivateVerifier")]
pub fn fee_private_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("fee_private")
}

/// Returns the verifying key for the fee_public function
///
/// @returns {VerifyingKey} Verifying key for the fee_public function
#[wasm_bindgen(js_name = "feePublicVerifier")]
pub fn fee_public_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("fee_public")
}

/// Returns the verifying key for the inclusion function
///
/// @returns {VerifyingKey} Verifying key for the inclusion function
#[wasm_bindgen(js_name = "inclusionVerifier")]
pub fn inclusion_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("inclusion")
}

/// Returns the verifying key for the join function
///
/// @returns {VerifyingKey} Verifying key for the join function
#[wasm_bindgen(js_name = "joinVerifier")]
pub fn join_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("join")
}

/// Returns the verifying key for the set_validator_state function
///
/// @returns {VerifyingKey} Verifying key for the set_validator_state function
#[wasm_bindgen(js_name = "setValidatorStateVerifier")]
pub fn set_validator_state_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("set_validator_state")
}

/// Returns the verifying key for the split function
///
/// @returns {VerifyingKey} Verifying key for the split function
#[wasm_bindgen(js_name = "splitVerifier")]
pub fn split_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("split")
}

/// Returns the verifying key for the transfer_private function
///
/// @returns {VerifyingKey} Verifying key for the transfer_private function
#[wasm_bindgen(js_name = "transferPrivateVerifier")]
pub fn transfer_private_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("transfer_private")
}

/// Returns the verifying key for the transfer_private_to_public function
///
/// @returns {VerifyingKey} Verifying key for the transfer_private_to_public function
#[wasm_bindgen(js_name = "transferPrivateToPublicVerifier")]
pub fn transfer_private_to_public_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("transfer_private_to_public")
}

/// Returns the verifying key for the transfer_public function
///
/// @returns {VerifyingKey} Verifying key for the transfer_public function
#[wasm_bindgen(js_name = "transferPublicVerifier")]
pub fn transfer_public_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("transfer_public")
}

/// Returns the verifying key for the transfer_public_as_signer function
///
/// @returns {VerifyingKey} Verifying key for the transfer_public_as_signer function
#[wasm_bindgen(js_name = "transferPublicAsSignerVerifier")]
pub fn transfer_public_as_signer_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("transfer_public_as_signer")
}

/// Returns the verifying key for the transfer_public_to_private function
///
/// @returns {VerifyingKey} Verifying key for the transfer_public_to_private function
#[wasm_bindgen(js_name = "transferPublicToPrivateVerifier")]
pub fn transfer_public_to_private_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("transfer_public_to_private")
}

/// Returns the verifying key for the unbond_public function
///
/// @returns {VerifyingKey} Verifying key for the unbond_public function
#[wasm_bindgen(js_name = "unbondPublicVerifier")]
pub fn unbond_public_verifier() -> VerifyingKey {
let vk = CurrentNetwork::get_credits_verifying_key("bond_validator".to_string()).unwrap().clone();
let num_variables = vk.circuit_info.num_public_and_private_variables as u64;
VerifyingKey::from(VerifyingKeyNative::new(vk, num_variables))
VerifyingKey::get_credits_verifying_key("unbond_public")
}

/// Returns the verifying key for the bond_public function
35 changes: 17 additions & 18 deletions wasm/src/programs/verifying_key/metadata.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.

use snarkvm_parameters::canary::BondPublicVerifier;
use super::*;

#[wasm_bindgen]
@@ -37,9 +36,9 @@ pub struct Metadata {
}

impl Metadata {
const BASE_URL: &'static str = "https://s3-us-west-1.amazonaws.com/testnet.parameters/";
const BASE_URL: &'static str = "https://parameters.aleo.org/testnet/";

fn new(name: &str, verifying_key: VerifyingKey, locator: &str, prover: &'static str, verifier: &'static str) -> Self {
fn new(name: &str, verifying_key: &str, locator: &str, prover: &'static str, verifier: &'static str) -> Self {
fn url(function_name: &str, kind: &str, proving_key_metadata: &'static str) -> String {
let metadata: serde_json::Value =
serde_json::from_str(proving_key_metadata).expect("Metadata was not well-formatted");
@@ -68,7 +67,7 @@ impl Metadata {
pub fn bond_public() -> Metadata {
Metadata::new(
"bond_public",
VerifyingKey::bond_public_verifier(),
"bondPublicVerifier",
"credits.aleo/bond_public",
snarkvm_parameters::testnet::BondPublicProver::METADATA,
snarkvm_parameters::testnet::BondPublicVerifier::METADATA,
@@ -79,7 +78,7 @@ impl Metadata {
pub fn bond_validator() -> Metadata {
Metadata::new(
"bond_validator",
VerifyingKey::bond_validator_verifier(),
"bondValidatorVerifier",
"credits.aleo/bond_validator",
snarkvm_parameters::testnet::BondValidatorProver::METADATA,
snarkvm_parameters::testnet::BondValidatorVerifier::METADATA,
@@ -90,7 +89,7 @@ impl Metadata {
pub fn claim_unbond_public() -> Metadata {
Metadata::new(
"claim_unbond_public",
VerifyingKey::claim_unbond_public_verifier(),
"claimUnbondPublicVerifier",
"credits.aleo/claim_unbond_public",
snarkvm_parameters::testnet::ClaimUnbondPublicProver::METADATA,
snarkvm_parameters::testnet::ClaimUnbondPublicVerifier::METADATA,
@@ -101,7 +100,7 @@ impl Metadata {
pub fn fee_private() -> Metadata {
Metadata::new(
"fee_private",
VerifyingKey::fee_private_verifier(),
"feePrivateVerifier",
"credits.aleo/fee_private",
snarkvm_parameters::testnet::FeePrivateProver::METADATA,
snarkvm_parameters::testnet::FeePrivateVerifier::METADATA,
@@ -112,7 +111,7 @@ impl Metadata {
pub fn fee_public() -> Metadata {
Metadata::new(
"fee_public",
VerifyingKey::fee_public_verifier(),
"feePublicVerifier",
"credits.aleo/fee_public",
snarkvm_parameters::testnet::FeePublicProver::METADATA,
snarkvm_parameters::testnet::FeePublicVerifier::METADATA,
@@ -123,7 +122,7 @@ impl Metadata {
pub fn inclusion() -> Metadata {
Metadata::new(
"inclusion",
VerifyingKey::inclusion_verifier(),
"inclusionVerifier",
"inclusion",
snarkvm_parameters::testnet::InclusionProver::METADATA,
snarkvm_parameters::testnet::InclusionVerifier::METADATA,
@@ -134,7 +133,7 @@ impl Metadata {
pub fn join() -> Metadata {
Metadata::new(
"join",
VerifyingKey::join_verifier(),
"joinVerifier",
"credits.aleo/join",
snarkvm_parameters::testnet::JoinProver::METADATA,
snarkvm_parameters::testnet::JoinVerifier::METADATA,
@@ -145,7 +144,7 @@ impl Metadata {
pub fn set_validator_state() -> Metadata {
Metadata::new(
"set_validator_state",
VerifyingKey::set_validator_state_verifier(),
"setValidatorStateVerifier",
"credits.aleo/set_validator_state",
snarkvm_parameters::testnet::SetValidatorStateProver::METADATA,
snarkvm_parameters::testnet::SetValidatorStateVerifier::METADATA,
@@ -156,7 +155,7 @@ impl Metadata {
pub fn split() -> Metadata {
Metadata::new(
"split",
VerifyingKey::split_verifier(),
"splitVerifier",
"credits.aleo/split",
snarkvm_parameters::testnet::SplitProver::METADATA,
snarkvm_parameters::testnet::SplitVerifier::METADATA,
@@ -167,7 +166,7 @@ impl Metadata {
pub fn transfer_private() -> Metadata {
Metadata::new(
"transfer_private",
VerifyingKey::transfer_private_verifier(),
"transferPrivateVerifier",
"credits.aleo/transfer_private",
snarkvm_parameters::testnet::TransferPrivateProver::METADATA,
snarkvm_parameters::testnet::TransferPrivateVerifier::METADATA,
@@ -178,7 +177,7 @@ impl Metadata {
pub fn transfer_private_to_public() -> Metadata {
Metadata::new(
"transfer_private_to_public",
VerifyingKey::transfer_private_to_public_verifier(),
"transferPrivateToPublicVerifier",
"credits.aleo/transfer_private_to_public",
snarkvm_parameters::testnet::TransferPrivateToPublicProver::METADATA,
snarkvm_parameters::testnet::TransferPrivateToPublicVerifier::METADATA,
@@ -189,7 +188,7 @@ impl Metadata {
pub fn transfer_public() -> Metadata {
Metadata::new(
"transfer_public",
VerifyingKey::transfer_public_verifier(),
"transferPublicVerifier",
"credits.aleo/transfer_public",
snarkvm_parameters::testnet::TransferPublicProver::METADATA,
snarkvm_parameters::testnet::TransferPublicVerifier::METADATA,
@@ -200,7 +199,7 @@ impl Metadata {
pub fn transfer_public_as_signer() -> Metadata {
Metadata::new(
"transfer_public_as_signer",
VerifyingKey::transfer_public_as_signer_verifier(),
"transferPublicAsSignerVerifier",
"credits.aleo/transfer_public_as_signer",
snarkvm_parameters::testnet::TransferPublicAsSignerProver::METADATA,
snarkvm_parameters::testnet::TransferPublicAsSignerVerifier::METADATA,
@@ -211,7 +210,7 @@ impl Metadata {
pub fn transfer_public_to_private() -> Metadata {
Metadata::new(
"transfer_public_to_private",
VerifyingKey::transfer_public_to_private_verifier(),
"transferPublicToPrivateVerifier",
"credits.aleo/transfer_public_to_private",
snarkvm_parameters::testnet::TransferPublicToPrivateProver::METADATA,
snarkvm_parameters::testnet::TransferPublicToPrivateVerifier::METADATA,
@@ -222,7 +221,7 @@ impl Metadata {
pub fn unbond_public() -> Metadata {
Metadata::new(
"unbond_public",
VerifyingKey::unbond_public_verifier(),
"unbondPublicVerifier",
"credits.aleo/unbond_public",
snarkvm_parameters::testnet::UnbondPublicProver::METADATA,
snarkvm_parameters::testnet::UnbondPublicVerifier::METADATA,
931 changes: 562 additions & 369 deletions yarn.lock

Large diffs are not rendered by default.