Skip to content

Commit

Permalink
Merge pull request #42 from dusk-network/input
Browse files Browse the repository at this point in the history
  • Loading branch information
xevisalle authored Nov 17, 2023
2 parents 6c9a9a4 + 97afff3 commit 97546c0
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 44 deletions.
1 change: 1 addition & 0 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license-provider = { path = "../license-provider" }

[dev-dependencies]
dusk-wallet = "0.20.1-rc.0"
dusk-jubjub = { version = "0.13", default-features = false }
zk-citadel = "0.5.2-rc.0"
phoenix-core = { version = "0.21", features = ["alloc"] }
poseidon-merkle = { version = "0.3", features = ["rkyv-impl"] }
Expand Down
10 changes: 9 additions & 1 deletion integration-tests/tests/citadel/int_test_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ async fn issue_license(
GAS_PRICE,
);

const ATTRIBUTE_DATA_EXAMPLE: u64 = 1234;

let (tx_id, _) = license_issuer
.issue_license(rng, &request, &reference_lp.ssk_lp)
.issue_license(
rng,
&request,
&reference_lp.ssk_lp,
&JubJubScalar::from(ATTRIBUTE_DATA_EXAMPLE),
)
.await?;
Ok(tx_id)
}
Expand Down Expand Up @@ -239,6 +246,7 @@ async fn user_round_trip() -> Result<(), Error> {
&PwdHash(PWD_HASH.to_string()),
&ssk_user,
&reference_lp.psk_lp,
&reference_lp.psk_lp,
&prover,
&verifier,
&license,
Expand Down
10 changes: 9 additions & 1 deletion integration-tests/tests/citadel/issue_license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_jubjub::JubJubScalar;
use dusk_wallet::WalletPath;
use license_provider::{LicenseIssuer, ReferenceLP};
use moat_core::{Error, JsonLoader, RequestCreator, RequestJson};
Expand Down Expand Up @@ -61,8 +62,15 @@ async fn issue_license() -> Result<(), Error> {
GAS_PRICE,
);

const ATTRIBUTE_DATA_EXAMPLE: u64 = 1234;

license_issuer
.issue_license(rng, &request, &reference_lp.ssk_lp)
.issue_license(
rng,
&request,
&reference_lp.ssk_lp,
&JubJubScalar::from(ATTRIBUTE_DATA_EXAMPLE),
)
.await?;

Ok(())
Expand Down
6 changes: 2 additions & 4 deletions license-provider/src/license_issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub struct LicenseIssuer {
gas_price: u64,
}

const ATTRIBUTE_DATA: u64 = 1 << 17;

impl LicenseIssuer {
pub fn new(
config: BlockchainAccessConfig,
Expand All @@ -50,9 +48,9 @@ impl LicenseIssuer {
rng: &mut R,
request: &Request,
ssk_lp: &SecretSpendKey,
attr_data: &JubJubScalar,
) -> Result<(BlsScalar, Vec<u8>), Error> {
let attr = JubJubScalar::from(ATTRIBUTE_DATA);
let license = License::new(&attr, ssk_lp, request, rng);
let license = License::new(attr_data, ssk_lp, request, rng);
let license_blob = rkyv::to_bytes::<_, MAX_LICENSE_SIZE>(&license)
.expect("Serializing should be infallible")
.to_vec();
Expand Down
1 change: 1 addition & 0 deletions moat-cli-lp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
dusk-wallet = "0.20.1-rc.0"
dusk-jubjub = { version = "0.13", default-features = false }
wallet-accessor = { path = "../wallet-accessor" }
moat-core = { path = "../moat-core" }
dusk-pki = "0.13"
Expand Down
23 changes: 20 additions & 3 deletions moat-cli-lp/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::run_result::{
IssueLicenseSummary, LicenseContractSummary, RequestsLPSummary, RunResult,
};
use crate::SeedableRng;
use dusk_jubjub::JubJubScalar;
use dusk_pki::SecretSpendKey;
use dusk_wallet::{RuskHttpClient, WalletPath};
use license_provider::{LicenseIssuer, ReferenceLP};
Expand All @@ -21,7 +22,10 @@ pub(crate) enum Command {
/// List requests
ListRequestsLP,
/// Issue license
IssueLicenseLP { request_hash: String },
IssueLicenseLP {
request_hash: String,
attr_data_bytes: String,
},
/// List licenses (User)
ListLicenses,
/// Show state
Expand All @@ -43,7 +47,10 @@ impl Command {
Command::ListRequestsLP => {
Self::list_requests_lp(blockchain_access_config, ssk).await?
}
Command::IssueLicenseLP { request_hash } => {
Command::IssueLicenseLP {
request_hash,
attr_data_bytes,
} => {
Self::issue_license_lp(
wallet_path,
psw,
Expand All @@ -52,6 +59,7 @@ impl Command {
gas_limit,
gas_price,
request_hash,
attr_data_bytes,
)
.await?
}
Expand Down Expand Up @@ -93,7 +101,11 @@ impl Command {
gas_limit: u64,
gas_price: u64,
request_hash: String,
attr_data_bytes: String,
) -> Result<RunResult, Error> {
let attr_data =
JubJubScalar::from(attr_data_bytes.parse::<u64>().unwrap());

let mut rng = StdRng::from_entropy();
let mut reference_lp = ReferenceLP::create_with_ssk(ssk)?;
let (_total_count, _this_lp_count) =
Expand All @@ -110,7 +122,12 @@ impl Command {
gas_price,
);
let (tx_id, license_blob) = license_issuer
.issue_license(&mut rng, &request, &reference_lp.ssk_lp)
.issue_license(
&mut rng,
&request,
&reference_lp.ssk_lp,
&attr_data,
)
.await?;
let summary = IssueLicenseSummary {
request,
Expand Down
1 change: 1 addition & 0 deletions moat-cli-lp/src/interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn menu_operation() -> Result<OpSelection, ErrorKind> {
CommandMenuItem::IssueLicenseLP => {
OpSelection::Run(Box::from(Command::IssueLicenseLP {
request_hash: prompt::request_request_hash()?,
attr_data_bytes: prompt::request_attr_data()?,
}))
}
CommandMenuItem::ListLicenses => {
Expand Down
20 changes: 20 additions & 0 deletions moat-cli-lp/src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,23 @@ pub(crate) fn request_request_hash() -> Result<String, ErrorKind> {
let a_str = a.as_string().expect("answer to be a string").to_string();
Ok(a_str)
}

pub(crate) fn request_attr_data() -> Result<String, ErrorKind> {
let q = Question::input("attr_data")
.message("Please enter the attribute data:".to_string())
.validate_on_key(|_, _| {
true // todo: add some validation of the attr_data
})
.validate(|attr_data, _| {
if attr_data.is_empty() {
Err("Please enter valid attribute data".to_string())
} else {
Ok(())
}
})
.build();

let a = requestty::prompt_one(q)?;
let a_str = a.as_string().expect("answer to be a string").to_string();
Ok(a_str)
}
1 change: 1 addition & 0 deletions moat-cli-user/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ futures-core = "0.3"
reqwest = "0.11"
bytecheck = "0.6"
sha3 = "0.10"
bs58 = "0.4"
107 changes: 79 additions & 28 deletions moat-cli-user/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ use moat_core::{
use rand::rngs::StdRng;
use wallet_accessor::{BlockchainAccessConfig, Password};
use zk_citadel::license::{License, SessionCookie};

use std::fs::File;
use std::io::prelude::*;

/// Commands that can be run against the Moat
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub(crate) enum Command {
Expand All @@ -32,6 +36,8 @@ pub(crate) enum Command {
UseLicense {
license_hash: String,
psk_lp_bytes: String,
psk_sp_bytes: String,
challenge_bytes: String,
},
/// Request Service (User)
RequestService { session_cookie: String },
Expand Down Expand Up @@ -72,6 +78,8 @@ impl Command {
Command::UseLicense {
license_hash,
psk_lp_bytes,
psk_sp_bytes,
challenge_bytes,
} => {
Self::use_license(
wallet_path,
Expand All @@ -80,7 +88,9 @@ impl Command {
gas_limit,
gas_price,
psk_lp_bytes,
psk_sp_bytes,
ssk,
challenge_bytes,
setup_holder,
license_hash,
)
Expand Down Expand Up @@ -175,7 +185,9 @@ impl Command {
gas_limit: u64,
gas_price: u64,
psk_lp_bytes: String,
psk_sp_bytes: String,
ssk: SecretSpendKey,
challenge_bytes: String,
setup_holder: &mut Option<SetupHolder>,
license_hash: String,
) -> Result<RunResult, Error> {
Expand All @@ -188,20 +200,33 @@ impl Command {
Ok(match pos_license {
Some((pos, license)) => {
println!("using license: {}", RunResult::to_hash_hex(&license));
let ssk_user = ssk;
let challenge =
JubJubScalar::from(challenge_bytes.parse::<u64>().unwrap());

let psk_lp_bytes: [u8; 64] = hex::decode(psk_lp_bytes.clone())
.expect("Decoded.")
.try_into()
.unwrap();
let psk_lp_bytes: [u8; 64] =
bs58::decode(&psk_lp_bytes.clone())
.into_vec()
.unwrap()
.try_into()
.unwrap();
let psk_lp = PublicSpendKey::from_bytes(&psk_lp_bytes).unwrap();

let psk_sp_bytes: [u8; 64] =
bs58::decode(&psk_sp_bytes.clone())
.into_vec()
.unwrap()
.try_into()
.unwrap();
let psk_sp = PublicSpendKey::from_bytes(&psk_sp_bytes).unwrap();

let (tx_id, session_cookie) = Self::prove_and_send_use_license(
blockchain_access_config,
wallet_path,
psw,
psk_lp,
ssk_user,
psk_sp,
ssk,
challenge,
&license,
pos,
gas_limit,
Expand Down Expand Up @@ -252,12 +277,8 @@ impl Command {
let mut licenses_stream =
CitadelInquirer::get_licenses(&client, block_heights).await?;

let ssk_user = ssk;

let pairs = CitadelInquirer::find_owned_licenses(
ssk_user,
&mut licenses_stream,
)?;
let pairs =
CitadelInquirer::find_owned_licenses(ssk, &mut licenses_stream)?;
Ok(if pairs.is_empty() {
None
} else {
Expand All @@ -276,7 +297,9 @@ impl Command {
wallet_path: &WalletPath,
psw: &Password,
psk_lp: PublicSpendKey,
psk_sp: PublicSpendKey,
ssk_user: SecretSpendKey,
challenge: JubJubScalar,
license: &License,
pos: u64,
gas_limit: u64,
Expand All @@ -287,28 +310,55 @@ 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 challenge = JubJubScalar::from(0xcafebabeu64);
let mut rng = StdRng::seed_from_u64(0xbeef);

let setup_holder = match sh_opt {
Some(sh) => sh,
_ => {
println!("obtaining setup");
let pp_vec = CrsGetter::get_crs(&client).await?;
let pp =
// SAFETY: CRS vector is checked by the hash check when it is received from the node
unsafe { PublicParameters::from_slice_unchecked(pp_vec.as_slice()) };
println!("compiling circuit");
let (prover, verifier) =
Compiler::compile::<LicenseCircuit>(&pp, LABEL)
.expect("Compiling circuit should succeed");
let sh = SetupHolder {
pp,
prover,
verifier,
let wallet_dir_path = match wallet_path.dir() {
Some(path) => path,
None => panic!(),
};
*sh_opt = Some(sh);
sh_opt.as_ref().unwrap()

let prover_path = &wallet_dir_path.join("moat_prover.dat");
let verifier_path = &wallet_dir_path.join("moat_verifier.dat");

if prover_path.exists() && verifier_path.exists() {
let mut file = File::open(prover_path)?;
let mut prover_bytes = vec![];
file.read_to_end(&mut prover_bytes)?;
let prover = Prover::try_from_bytes(prover_bytes).unwrap();

file = File::open(verifier_path)?;
let mut verifier_bytes = vec![];
file.read_to_end(&mut verifier_bytes)?;
let verifier =
Verifier::try_from_bytes(verifier_bytes).unwrap();

let sh = SetupHolder { prover, verifier };
*sh_opt = Some(sh);
sh_opt.as_ref().unwrap()
} else {
println!("obtaining setup");
let pp_vec = CrsGetter::get_crs(&client).await?;
let pp =
// SAFETY: CRS vector is checked by the hash check when it is received from the node
unsafe { PublicParameters::from_slice_unchecked(pp_vec.as_slice()) };
println!("compiling circuit");
let (prover, verifier) =
Compiler::compile::<LicenseCircuit>(&pp, LABEL)
.expect("Compiling circuit should succeed");

let mut file = File::create(prover_path)?;
file.write_all(prover.to_bytes().as_slice())?;

file = File::create(verifier_path)?;
file.write_all(verifier.to_bytes().as_slice())?;

let sh = SetupHolder { prover, verifier };
*sh_opt = Some(sh);
sh_opt.as_ref().unwrap()
}
}
};

Expand All @@ -325,6 +375,7 @@ impl Command {
psw,
&ssk_user,
&psk_lp,
&psk_sp,
&setup_holder.prover,
&setup_holder.verifier,
license,
Expand Down
Loading

0 comments on commit 97546c0

Please sign in to comment.