Skip to content

Commit

Permalink
De-duplicated utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Dec 1, 2023
1 parent c7e3a99 commit fe71717
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 96 deletions.
31 changes: 5 additions & 26 deletions moat-cli/moat-cli-lp/src/run_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use rkyv::ser::serializers::AllocSerializer;
use std::fmt;
use std::ops::Range;
use zk_citadel::license::{License, Request};
// use rkyv::{check_archived_root, Archive, Deserialize, Infallible, Serialize};
use sha3::{Digest, Sha3_256};
use zk_citadel_moat::MoatCoreUtils;

pub struct RequestsLPSummary {
pub found_total: usize,
Expand Down Expand Up @@ -50,7 +48,7 @@ impl fmt::Display for RunResult {
writeln!(
f,
"request to process by LP: {}",
RunResult::to_hash_hex(request)
MoatCoreUtils::to_hash_hex(request)
)?;
}
Ok(())
Expand All @@ -60,7 +58,7 @@ impl fmt::Display for RunResult {
writeln!(
f,
"issuing license for request: {}",
RunResult::to_hash_hex(&summary.request)
MoatCoreUtils::to_hash_hex(&summary.request)
)?;
writeln!(
f,
Expand All @@ -70,7 +68,7 @@ impl fmt::Display for RunResult {
writeln!(
f,
"issued license: {}",
RunResult::blob_to_hash_hex(
MoatCoreUtils::blob_to_hash_hex(
summary.license_blob.as_slice()
)
)?;
Expand All @@ -94,7 +92,7 @@ impl fmt::Display for RunResult {
writeln!(
f,
"license: {}",
RunResult::to_hash_hex(license),
MoatCoreUtils::to_hash_hex(license),
)?;
}
}
Expand All @@ -111,22 +109,3 @@ impl fmt::Display for RunResult {
}
}
}

impl RunResult {
pub fn to_hash_hex<T>(object: &T) -> String
where
T: rkyv::Serialize<AllocSerializer<16386>>,
{
let blob = rkyv::to_bytes::<_, 16386>(object)
.expect("Serializing should be infallible")
.to_vec();
Self::blob_to_hash_hex(blob.as_slice())
}

pub fn blob_to_hash_hex(blob: &[u8]) -> String {
let mut hasher = Sha3_256::new();
hasher.update(blob);
let result = hasher.finalize();
hex::encode(result)
}
}
15 changes: 9 additions & 6 deletions moat-cli/moat-cli-user/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zk_citadel::license::{License, SessionCookie};
use zk_citadel_moat::wallet_accessor::{BlockchainAccessConfig, Password};
use zk_citadel_moat::{
BcInquirer, CitadelInquirer, CrsGetter, LicenseCircuit, LicenseUser,
RequestCreator, RequestSender, TxAwaiter,
MoatCoreUtils, RequestCreator, RequestSender, TxAwaiter,
};

use std::fs::File;
Expand Down Expand Up @@ -126,7 +126,7 @@ impl Command {

let rng = &mut StdRng::from_entropy();
let request = RequestCreator::create(&ssk, &psk_lp, rng)?;
let request_hash = RunResult::to_hash_hex(&request);
let request_hash = MoatCoreUtils::to_hash_hex(&request);
let tx_id = RequestSender::send_request(
request,
blockchain_access_config,
Expand Down Expand Up @@ -197,7 +197,10 @@ impl Command {
.await?;
Ok(match pos_license {
Some((pos, license)) => {
println!("using license: {}", RunResult::to_hash_hex(&license));
println!(
"using license: {}",
MoatCoreUtils::to_hash_hex(&license)
);
let challenge =
JubJubScalar::from(challenge_bytes.parse::<u64>()?);

Expand Down Expand Up @@ -225,13 +228,13 @@ impl Command {
)
.await?;
let summary = UseLicenseSummary {
license_blob: RunResult::to_blob(&license),
license_blob: MoatCoreUtils::to_blob(&license),
tx_id: hex::encode(tx_id.to_bytes()),
user_attr: hex::encode(session_cookie.attr_data.to_bytes()),
session_id: hex::encode(
session_cookie.session_id.to_bytes(),
),
session_cookie: RunResult::to_blob_hex(&session_cookie),
session_cookie: MoatCoreUtils::to_blob_hex(&session_cookie),
};
RunResult::UseLicense(Some(summary))
}
Expand Down Expand Up @@ -273,7 +276,7 @@ impl Command {
None
} else {
for (pos, license) in pairs.iter() {
if license_hash == RunResult::to_hash_hex(license) {
if license_hash == MoatCoreUtils::to_hash_hex(license) {
return Ok(Some((*pos, license.clone())));
}
}
Expand Down
45 changes: 4 additions & 41 deletions moat-cli/moat-cli-user/src/run_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use rkyv::ser::serializers::AllocSerializer;
use sha3::{Digest, Sha3_256};
use std::fmt;
use std::ops::Range;
use zk_citadel::license::License;
use zk_citadel_moat::MoatCoreUtils;

pub struct SubmitRequestSummary {
pub psk_lp: String,
Expand Down Expand Up @@ -70,7 +69,7 @@ impl fmt::Display for RunResult {
writeln!(
f,
"license: {} {}",
RunResult::to_hash_hex(license),
MoatCoreUtils::to_hash_hex(license),
if *is_owned { "owned" } else { "" }
)?;
}
Expand All @@ -83,7 +82,7 @@ impl fmt::Display for RunResult {
writeln!(
f,
"using license: {}",
Self::blob_to_hash_hex(
MoatCoreUtils::blob_to_hash_hex(
summary.license_blob.as_slice()
)
)?;
Expand All @@ -96,7 +95,7 @@ impl fmt::Display for RunResult {
writeln!(
f,
"license {} used",
Self::blob_to_hash_hex(
MoatCoreUtils::blob_to_hash_hex(
summary.license_blob.as_slice()
),
)?;
Expand Down Expand Up @@ -128,39 +127,3 @@ impl fmt::Display for RunResult {
}
}
}

impl RunResult {
pub fn to_hash_hex<T>(object: &T) -> String
where
T: rkyv::Serialize<AllocSerializer<16386>>,
{
let blob = rkyv::to_bytes::<_, 16386>(object)
.expect("Serializing should be infallible")
.to_vec();
Self::blob_to_hash_hex(blob.as_slice())
}

pub fn blob_to_hash_hex(blob: &[u8]) -> String {
let mut hasher = Sha3_256::new();
hasher.update(blob);
let result = hasher.finalize();
hex::encode(result)
}

pub fn to_blob_hex<T>(object: &T) -> String
where
T: rkyv::Serialize<AllocSerializer<16386>>,
{
let blob = Self::to_blob(object);
hex::encode(blob)
}

pub fn to_blob<T>(object: &T) -> Vec<u8>
where
T: rkyv::Serialize<AllocSerializer<16386>>,
{
rkyv::to_bytes::<_, 16386>(object)
.expect("Serializing should be infallible")
.to_vec()
}
}
1 change: 1 addition & 0 deletions moat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ pub use contract_queries::{
};
pub use error::Error;
pub use json_loader::JsonLoader;
pub use utils::MoatCoreUtils;
23 changes: 2 additions & 21 deletions moat/src/license_provider/reference_lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::utils::MoatCoreUtils;
use crate::wallet_accessor::BlockchainAccessConfig;
use crate::{Error, JsonLoader, RequestScanner, MAX_REQUEST_SIZE};
use blake3::OUT_LEN;
use dusk_bytes::DeserializableSlice;
use dusk_pki::{PublicSpendKey, SecretSpendKey, ViewKey};
use rkyv::ser::serializers::AllocSerializer;
use sha3::{Digest, Sha3_256};
use std::collections::BTreeSet;
use std::path::Path;
use zk_citadel::license::Request;
Expand All @@ -23,7 +22,6 @@ pub struct LPConfig {
impl JsonLoader for LPConfig {}

const BLOCKS_RANGE_LEN: u64 = 10000;
const MAX_OBJECT_SIZE: usize = 16384;

pub struct ReferenceLP {
pub psk_lp: PublicSpendKey,
Expand Down Expand Up @@ -148,7 +146,7 @@ impl ReferenceLP {
/// Retrieve request with a given request hash, or None if not found.
pub fn get_request(&mut self, request_hash: &String) -> Option<Request> {
for (index, request) in self.requests_to_process.iter().enumerate() {
if Self::to_hash_hex(request) == *request_hash {
if MoatCoreUtils::to_hash_hex(request) == *request_hash {
self.requests_hashes.remove(&Self::hash_request(request));
return Some(self.requests_to_process.remove(index));
}
Expand All @@ -164,21 +162,4 @@ impl ReferenceLP {
)
.as_bytes()
}

fn to_hash_hex<T>(object: &T) -> String
where
T: rkyv::Serialize<AllocSerializer<MAX_OBJECT_SIZE>>,
{
let blob = rkyv::to_bytes::<_, MAX_OBJECT_SIZE>(object)
.expect("Serializing should be infallible")
.to_vec();
Self::blob_to_hash_hex(blob.as_slice())
}

fn blob_to_hash_hex(blob: &[u8]) -> String {
let mut hasher = Sha3_256::new();
hasher.update(blob);
let result = hasher.finalize();
hex::encode(result)
}
}
23 changes: 21 additions & 2 deletions moat/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ pub struct SetupHolder {

pub struct MoatCoreUtils {}

const MAX_OBJECT_SIZE: usize = 16384;

impl MoatCoreUtils {
pub fn to_hash_hex<T>(object: &T) -> String
where
T: rkyv::Serialize<AllocSerializer<16386>>,
T: rkyv::Serialize<AllocSerializer<MAX_OBJECT_SIZE>>,
{
let blob = rkyv::to_bytes::<_, 16386>(object)
let blob = rkyv::to_bytes::<_, MAX_OBJECT_SIZE>(object)
.expect("Serializing should be infallible")
.to_vec();
Self::blob_to_hash_hex(blob.as_slice())
Expand All @@ -56,6 +58,23 @@ impl MoatCoreUtils {
hex::encode(result)
}

pub fn to_blob_hex<T>(object: &T) -> String
where
T: rkyv::Serialize<AllocSerializer<MAX_OBJECT_SIZE>>,
{
let blob = Self::to_blob(object);
hex::encode(blob)
}

pub fn to_blob<T>(object: &T) -> Vec<u8>
where
T: rkyv::Serialize<AllocSerializer<MAX_OBJECT_SIZE>>,
{
rkyv::to_bytes::<_, MAX_OBJECT_SIZE>(object)
.expect("Serializing should be infallible")
.to_vec()
}

pub async fn get_license_to_use(
blockchain_access_config: &BlockchainAccessConfig,
ssk: &SecretSpendKey,
Expand Down

0 comments on commit fe71717

Please sign in to comment.