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

Add Moat Core API #50

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["testing-utils/test-moat-cli", "moat-cli-user", "moat-cli-lp", "moat-cli-sp", "testing-utils/test-moat-request", "moat-core", "wallet-accessor", "integration-tests", "license-provider", "macros/code-hasher"]
members = ["testing-utils/test-moat-cli", "moat-cli-user", "moat-cli-lp", "moat-cli-sp", "testing-utils/test-moat-request", "moat-core", "wallet-accessor", "integration-tests", "macros/code-hasher", "moat-example"]
1 change: 0 additions & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition = "2021"

[dependencies]
moat-core={ path = "../moat-core" }
license-provider = { path = "../license-provider" }

[dev-dependencies]
dusk-wallet = "0.20.1-rc.0"
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/citadel/int_test_lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use license_provider::ReferenceLP;
use moat_core::license_provider::ReferenceLP;
use moat_core::Error;
use toml_base_config::BaseConfig;
use wallet_accessor::BlockchainAccessConfig;
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/tests/citadel/int_test_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use dusk_bytes::DeserializableSlice;
use dusk_pki::SecretSpendKey;
use dusk_plonk::prelude::*;
use dusk_wallet::{RuskHttpClient, WalletPath};
use license_provider::{LicenseIssuer, ReferenceLP};
use moat_core::license_provider::{LicenseIssuer, ReferenceLP};
use moat_core::{
BcInquirer, CitadelInquirer, CrsGetter, Error, JsonLoader, LicenseCircuit,
LicenseSessionId, LicenseUser, PayloadRetriever, RequestCreator,
RequestJson, RequestSender, TxAwaiter,
};
use rand::rngs::StdRng;
use rand::rngs::{OsRng, StdRng};
use rand::SeedableRng;
use std::path::PathBuf;
use toml_base_config::BaseConfig;
Expand Down Expand Up @@ -251,7 +251,7 @@ async fn user_round_trip() -> Result<(), Error> {
&verifier,
&license,
opening.unwrap(),
&mut rng,
&mut OsRng,
&challenge,
GAS_LIMIT,
GAS_PRICE,
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/citadel/issue_license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use dusk_jubjub::JubJubScalar;
use dusk_wallet::WalletPath;
use license_provider::{LicenseIssuer, ReferenceLP};
use moat_core::license_provider::{LicenseIssuer, ReferenceLP};
use moat_core::{Error, JsonLoader, RequestCreator, RequestJson};
use rand::rngs::StdRng;
use rand::SeedableRng;
Expand Down
32 changes: 0 additions & 32 deletions license-provider/Cargo.toml

This file was deleted.

26 changes: 0 additions & 26 deletions license-provider/README.md

This file was deleted.

1 change: 0 additions & 1 deletion moat-cli-lp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ dusk-jubjub = { version = "0.13", default-features = false }
wallet-accessor = { path = "../wallet-accessor" }
moat-core = { path = "../moat-core" }
dusk-pki = "0.13"
license-provider = { path = "../license-provider" }
zk-citadel = "0.6.0-rc.0"
rkyv = { version = "=0.7.39" }
toml-base-config = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion moat-cli-lp/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::SeedableRng;
use dusk_jubjub::JubJubScalar;
use dusk_pki::SecretSpendKey;
use dusk_wallet::{RuskHttpClient, WalletPath};
use license_provider::{LicenseIssuer, ReferenceLP};
use moat_core::license_provider::{LicenseIssuer, ReferenceLP};
use moat_core::{BcInquirer, CitadelInquirer, Error};
use rand::rngs::StdRng;
use wallet_accessor::{BlockchainAccessConfig, Password};
Expand Down
5 changes: 2 additions & 3 deletions moat-cli-user/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use moat_core::{
BcInquirer, CitadelInquirer, CrsGetter, Error, LicenseCircuit, LicenseUser,
RequestCreator, RequestSender, TxAwaiter,
};
use rand::rngs::StdRng;
use rand::rngs::{OsRng, StdRng};
use wallet_accessor::{BlockchainAccessConfig, Password};
use zk_citadel::license::{License, SessionCookie};

Expand Down Expand Up @@ -311,7 +311,6 @@ impl Command {
RuskHttpClient::new(blockchain_access_config.rusk_address.clone());
// let (_, _, num_sessions) = CitadelInquirer::get_info(&client).await?;
// let challenge = JubJubScalar::from(num_sessions as u64 + 1);
let mut rng = StdRng::seed_from_u64(0xbeef);

let setup_holder = match sh_opt {
Some(sh) => sh,
Expand Down Expand Up @@ -381,7 +380,7 @@ impl Command {
&setup_holder.verifier,
license,
opening,
&mut rng,
&mut OsRng,
&challenge,
gas_limit,
gas_price,
Expand Down
2 changes: 2 additions & 0 deletions moat-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ tracing = "0.1"
bytes = "1.4"
reqwest = "0.11"
sha2 = "0.10"
sha3 = "0.10"
bs58 = "0.4"

[dev-dependencies]
tokio = { version = "1.15", features = ["rt-multi-thread", "time", "fs", "macros"] }
224 changes: 224 additions & 0 deletions moat-core/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_jubjub::{JubJubAffine, JubJubScalar};
use dusk_pki::{PublicSpendKey, SecretSpendKey};
use dusk_wallet::{RuskHttpClient, Wallet, WalletPath};

use zk_citadel::license::{Session, SessionCookie};

use crate::license_provider::{LicenseIssuer, ReferenceLP};
use crate::utils::MoatCoreUtils;
use crate::{
CitadelInquirer, LicenseSessionId, RequestCreator, RequestSender, TxAwaiter,
};
use wallet_accessor::Password::{self, Pwd};
use wallet_accessor::{BlockchainAccessConfig, WalletAccessor};

use rand::rngs::OsRng;
use std::path::Path;
use toml_base_config::BaseConfig;

pub struct MoatCore {}

impl MoatCore {
/// Retrieve the keypair (psk, ssk) from the installed wallet, given the
/// Moat Context
pub fn get_wallet_keypair(
moat_context: &MoatContext,
) -> (PublicSpendKey, SecretSpendKey) {
// We access the wallet to get the key pair
let wallet_accessor = WalletAccessor::create(
moat_context.wallet_path.clone(),
moat_context.wallet_password.clone(),
)
.unwrap();
let wallet = Wallet::from_file(wallet_accessor).unwrap();

wallet.spending_keys(wallet.default_address()).unwrap()
}

/// Create and send a transaction containing a license request
pub async fn request_license(
ssk_user: &SecretSpendKey,
psk_lp: &PublicSpendKey,
moat_context: &MoatContext,
rng: &mut OsRng,
) -> String {
let request = RequestCreator::create(ssk_user, psk_lp, rng).unwrap();
let request_hash = MoatCoreUtils::to_hash_hex(&request);

let tx_id = RequestSender::send_request(
request,
&moat_context.blockchain_access_config,
&moat_context.wallet_path,
&moat_context.wallet_password,
moat_context.gas_limit,
moat_context.gas_price,
)
.await
.unwrap();

let client = RuskHttpClient::new(
moat_context.blockchain_access_config.rusk_address.clone(),
);
TxAwaiter::wait_for(&client, tx_id).await.unwrap();

request_hash
}

/// Create and send a transaction containing a license for a given request
pub async fn issue_license(
request_hash: &String,
ssk_lp: &SecretSpendKey,
moat_context: &MoatContext,
attr_data: &JubJubScalar,
rng: &mut OsRng,
) -> String {
let mut reference_lp = ReferenceLP::create_with_ssk(ssk_lp).unwrap();

let (_total_count, _this_lp_count) = reference_lp
.scan(&moat_context.blockchain_access_config)
.await
.unwrap();

let request = reference_lp.get_request(request_hash);

match request {
Some(request) => {
let license_issuer = LicenseIssuer::new(
moat_context.blockchain_access_config.clone(),
moat_context.wallet_path.clone(),
moat_context.wallet_password.clone(),
moat_context.gas_limit,
moat_context.gas_price,
);
let (_tx_id, license_blob) = license_issuer
.issue_license(
rng,
&request,
&reference_lp.ssk_lp,
attr_data,
)
.await
.unwrap();
MoatCoreUtils::blob_to_hash_hex(&license_blob)
}
_ => "nothing to do".to_string(),
}
}

/// Create and send a transaction containing a proof that uses a given
/// license
pub async fn use_license(
moat_context: &MoatContext,
psk_lp: &PublicSpendKey,
psk_sp: &PublicSpendKey,
ssk: &SecretSpendKey,
challenge: &JubJubScalar,
license_hash: &str,
rng: &mut OsRng,
) -> Option<SessionCookie> {
let pos_license = MoatCoreUtils::get_license_to_use(
&moat_context.blockchain_access_config,
ssk,
license_hash.to_owned(),
)
.await
.unwrap();

match pos_license {
Some((pos, license)) => {
println!(
"using license: {}",
MoatCoreUtils::to_hash_hex(&license)
);

let (_tx_id, session_cookie) =
MoatCoreUtils::prove_and_send_use_license(
&moat_context.blockchain_access_config,
&moat_context.wallet_path,
&moat_context.wallet_password,
psk_lp,
psk_sp,
ssk,
challenge,
&license,
pos,
moat_context.gas_limit,
moat_context.gas_price,
&mut None,
rng,
)
.await
.unwrap();

Some(session_cookie)
}
_ => None,
}
}

/// Given a session cookie, verify that it corresponds to an existing
/// session in the Blockchain
pub async fn verify_requested_service(
moat_context: &MoatContext,
psk_lp: &PublicSpendKey,
psk_sp: &PublicSpendKey,
session_cookie: &SessionCookie,
) -> bool {
let client = RuskHttpClient::new(
moat_context.blockchain_access_config.rusk_address.clone(),
);

let pk_lp = JubJubAffine::from(*psk_lp.A());
let pk_sp = JubJubAffine::from(*psk_sp.A());

let session_id = LicenseSessionId {
id: session_cookie.session_id,
};
let session = CitadelInquirer::get_session(&client, session_id)
.await
.unwrap()
.unwrap();

let session = Session::from(&session.public_inputs);
session.verifies_ok(*session_cookie, pk_lp, pk_sp)
}
}

pub struct MoatContext {
blockchain_access_config: BlockchainAccessConfig,
wallet_path: WalletPath,
wallet_password: Password,
gas_limit: u64,
gas_price: u64,
}

impl MoatContext {
/// Create a new Moat Context given the required configurations
pub fn new(
config_path: &String,
wallet_path: &String,
wallet_password: &String,
gas_limit: u64,
gas_price: u64,
) -> Self {
let wallet_path = WalletPath::from(Path::new(&wallet_path));
let wallet_password = Pwd(wallet_password.to_string());

let blockchain_access_config =
BlockchainAccessConfig::load_path(config_path).unwrap();

Self {
blockchain_access_config,
wallet_path,
wallet_password,
gas_limit,
gas_price,
}
}
}
Loading