diff --git a/README.md b/README.md index bfb965e..535c347 100644 --- a/README.md +++ b/README.md @@ -97,3 +97,86 @@ This is a low-level crate which provides wallet (Blockchain) connectivity for fu Users of moat-core do not need to be aware of this crate, yet for maintainers and extenders, the crate provides a convenient low level interface between the higher-level moat-core library and the blockchain. Note that this crate deals only with contract method calling, it does not deal with contract queries. + +## moat-core + +### citadel requests + +Class: RequestCreator +Methods: + create, + create_from_hex_args +Both methods allow for creation of a request, given user's secret spend key and license provider's public spend key. +The request can then be sent to license provider, off-chain or on-chain. + +Class: RequestSender +Methods: send_request +Submits the request into blockchain. +It does so by calling a dummy contract method with request as an argument. + +Class: RequestScanner +Methods: + scan_transactions, + scan_last_blocks, + scan_block_range +Scan requests in a given collection of transactions, +contained in a given range of blocks or in a given number of most recent blocks. + +### citadel queries + +Class: CitadelInquirer +Methods: + get_licenses, + get_merkle_opening, + get_session, + get_info +Execute citadel-specific query methods of the license contract method. + +### blockchain payloads + +Class: PayloadExtractor +Methods: payload_from_tx +Extracts a payload from the given transaction, +errors if payload of a given type is not present or the transaction is not a contract calling transaction. + +Class: PayloadRetriever +Methods: retrieve_payload +Retrieves payload of a given transaction id, +errors if transaction is not found, or it does not contain a payload +(for example, given transaction is not a contract calling transaction) + +Class: PayloadSender +Methods: execute_contract_method +Executes given method of a given contract (identified by a contract id), passing to it the payload as an argument. + +### contract queries + +Class: ContractInquirer +Methods: + query_contract, + query_contract_with_feeder +query_contract - accepts a generic argument, contract id and contract query method name, returns a generic value result +query_contract_with_feeder - accepts a generic argument, contract id and method name, returns result as a Stream of bytes + +### blockchain queries + +Class: BcInquirer +Methods: + gql_query, + block_height +gql_query - executes a GQL query and returns result as a vector of bytes +block_height - returns the current block height as u64 + +Class: TxAwaiter +Methods: + wait_for, + wait_for_tx +Waits for a transaction identified by transaction id to be confirmed on the blockchain. + +Class: TxInquirer +Methods: + txs_from_block, + txs_from_block_range, + txs_from_last_n_blocks, + retrieve_tx +Retrieve transaction identified by transaction id, or transactions contained in a given block, or a collection of blocks. diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index 6a2de53..e84f1a3 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -14,7 +14,7 @@ phoenix-core = { version = "0.20.0-rc.0", features = ["alloc"] } poseidon-merkle = { version = "0.2.1-rc.0", features = ["rkyv-impl"] } dusk-pki = { version = "0.12", default-features = false, features = ["rkyv-impl"] } dusk-plonk = { version = "0.14", default-features = false, features = ["rkyv-impl", "alloc"] } -dusk-jubjub = { version = "0.12", default-features = false } +dusk-bls12_381 = "0.11" wallet-accessor = { path = "../wallet-accessor" } rkyv = { version = "=0.7.39" } tokio = { version = "1.15", features = ["rt-multi-thread", "time", "fs", "macros"] } diff --git a/integration-tests/tests/blockchain/mod.rs b/integration-tests/tests/blockchain/mod.rs new file mode 100644 index 0000000..2b6fa07 --- /dev/null +++ b/integration-tests/tests/blockchain/mod.rs @@ -0,0 +1,8 @@ +// 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. + +mod retrieve_txs; +mod stake_add_owner; diff --git a/integration-tests/tests/retrieve_txs.rs b/integration-tests/tests/blockchain/retrieve_txs.rs similarity index 89% rename from integration-tests/tests/retrieve_txs.rs rename to integration-tests/tests/blockchain/retrieve_txs.rs index 71b13a1..4598c84 100644 --- a/integration-tests/tests/retrieve_txs.rs +++ b/integration-tests/tests/blockchain/retrieve_txs.rs @@ -5,7 +5,7 @@ // Copyright (c) DUSK NETWORK. All rights reserved. use dusk_wallet::RuskHttpClient; -use moat_core::{Error, TxRetriever}; +use moat_core::{Error, TxInquirer}; use toml_base_config::BaseConfig; use tracing::trace; use wallet_accessor::BlockchainAccessConfig; @@ -22,7 +22,7 @@ async fn retrieve_txs_from_block() -> Result<(), Error> { const BLOCK_HEIGHT: u64 = 110; - let txs = TxRetriever::txs_from_block(&client, BLOCK_HEIGHT).await?; + let txs = TxInquirer::txs_from_block(&client, BLOCK_HEIGHT).await?; trace!("transactions retrieved={}", txs.transactions.len()); @@ -42,7 +42,7 @@ async fn retrieve_txs_from_block_range() -> Result<(), Error> { const BLOCK_HEIGHT_BEG: u64 = 1; const BLOCK_HEIGHT_END: u64 = 1000; - let (txs, top_block) = TxRetriever::txs_from_block_range( + let (txs, top_block) = TxInquirer::txs_from_block_range( &client, BLOCK_HEIGHT_BEG, BLOCK_HEIGHT_END, @@ -66,7 +66,7 @@ async fn retrieve_txs_from_last_n_blocks() -> Result<(), Error> { let client = RuskHttpClient::new(cfg.rusk_address); const N: usize = 10000; - let txs = TxRetriever::txs_from_last_n_blocks(&client, N).await?; + let txs = TxInquirer::txs_from_last_n_blocks(&client, N).await?; trace!("transactions={}", txs.transactions.len()); @@ -86,7 +86,7 @@ async fn retrieve_tx_by_id() -> Result<(), Error> { let client = RuskHttpClient::new(config.rusk_address); - let (tx, height) = TxRetriever::retrieve_tx(TXID, &client).await?; + let (tx, height) = TxInquirer::retrieve_tx(TXID, &client).await?; trace!("tx={:?}, block_height={}", tx, height); diff --git a/integration-tests/tests/stake_add_owner.rs b/integration-tests/tests/blockchain/stake_add_owner.rs similarity index 97% rename from integration-tests/tests/stake_add_owner.rs rename to integration-tests/tests/blockchain/stake_add_owner.rs index bf68ec7..f1df0de 100644 --- a/integration-tests/tests/stake_add_owner.rs +++ b/integration-tests/tests/blockchain/stake_add_owner.rs @@ -43,7 +43,7 @@ async fn stake_add_owner() -> Result<(), Error> { PathBuf::from(WALLET_PATH).as_path().join("wallet.dat"), ); - let tx_id = PayloadSender::send_to_contract_method( + let tx_id = PayloadSender::execute_contract_method( request_json.provider_psk, &blockchain_config, &wallet_path, diff --git a/integration-tests/tests/int_test_lp.rs b/integration-tests/tests/citadel/int_test_lp.rs similarity index 89% rename from integration-tests/tests/int_test_lp.rs rename to integration-tests/tests/citadel/int_test_lp.rs index 8a84797..20474b3 100644 --- a/integration-tests/tests/int_test_lp.rs +++ b/integration-tests/tests/citadel/int_test_lp.rs @@ -20,7 +20,7 @@ async fn lp_scan() -> Result<(), Error> { let blockchain_config = BlockchainAccessConfig::load_path(blockchain_config_path)?; - let mut reference_lp = ReferenceLP::init(&lp_config_path)?; + let mut reference_lp = ReferenceLP::create(&lp_config_path)?; reference_lp.scan(&blockchain_config).await?; Ok(()) @@ -37,7 +37,7 @@ async fn lp_scan_last_blocks() -> Result<(), Error> { let blockchain_config = BlockchainAccessConfig::load_path(blockchain_config_path)?; - let mut reference_lp = ReferenceLP::init(&lp_config_path)?; + let mut reference_lp = ReferenceLP::create(&lp_config_path)?; let (_total, _owned) = reference_lp .scan_last_blocks(10000, &blockchain_config) @@ -58,8 +58,8 @@ async fn lp_scan_2_lps() -> Result<(), Error> { let blockchain_config = BlockchainAccessConfig::load_path(blockchain_config_path)?; - let mut reference_lp1 = ReferenceLP::init(&lp1_config_path)?; - let mut reference_lp2 = ReferenceLP::init(&lp2_config_path)?; + let mut reference_lp1 = ReferenceLP::create(&lp1_config_path)?; + let mut reference_lp2 = ReferenceLP::create(&lp2_config_path)?; let (_, _lp1_count) = reference_lp1.scan(&blockchain_config).await?; let (_, _lp2_count) = reference_lp2.scan(&blockchain_config).await?; Ok(()) diff --git a/integration-tests/tests/int_test_user.rs b/integration-tests/tests/citadel/int_test_user.rs similarity index 88% rename from integration-tests/tests/int_test_user.rs rename to integration-tests/tests/citadel/int_test_user.rs index b3a1cda..6d09d0e 100644 --- a/integration-tests/tests/int_test_user.rs +++ b/integration-tests/tests/citadel/int_test_user.rs @@ -21,8 +21,8 @@ use bytecheck::CheckBytes; use bytes::Bytes; +use dusk_bls12_381::BlsScalar; use dusk_bytes::{DeserializableSlice, Serializable}; -use dusk_jubjub::BlsScalar; use dusk_pki::{PublicSpendKey, SecretSpendKey}; use dusk_plonk::prelude::*; use dusk_wallet::{RuskHttpClient, WalletPath}; @@ -30,8 +30,9 @@ use license_provider::{LicenseIssuer, ReferenceLP}; use moat_core::Error::InvalidQueryResponse; use moat_core::{ BcInquirer, CitadelInquirer, Error, JsonLoader, LicenseCircuit, - LicenseSessionId, PayloadSender, RequestCreator, RequestJson, StreamAux, - TxAwaiter, ARITY, DEPTH, LICENSE_CONTRACT_ID, USE_LICENSE_METHOD_NAME, + LicenseSessionId, PayloadRetriever, PayloadSender, RequestCreator, + RequestJson, RequestSender, StreamAux, TxAwaiter, ARITY, DEPTH, + LICENSE_CONTRACT_ID, USE_LICENSE_METHOD_NAME, }; use poseidon_merkle::Opening; use rand::rngs::StdRng; @@ -69,15 +70,14 @@ fn compute_citadel_parameters( psk_lp: PublicSpendKey, lic: &License, merkle_proof: Opening<(), DEPTH, ARITY>, + challenge: &JubJubScalar, ) -> (CitadelProverParameters, SessionCookie) { - const CHALLENGE: u64 = 20221127u64; - let c = JubJubScalar::from(CHALLENGE); let (cpp, sc) = CitadelProverParameters::compute_parameters( &ssk, &lic, &psk_lp, &psk_lp, - &c, + challenge, rng, merkle_proof, ); @@ -120,6 +120,7 @@ async fn prove_and_send_use_license( license: &License, opening: Opening<(), DEPTH, ARITY>, rng: &mut StdRng, + challenge: &JubJubScalar, ) -> Result { let (cpp, sc) = compute_citadel_parameters( rng, @@ -127,6 +128,7 @@ async fn prove_and_send_use_license( reference_lp.psk_lp, license, opening, + &challenge, ); let circuit = LicenseCircuit::new(&cpp, &sc); @@ -146,7 +148,7 @@ async fn prove_and_send_use_license( public_inputs, }; - let tx_id = PayloadSender::send_to_contract_method( + let tx_id = PayloadSender::execute_contract_method( use_license_arg, &blockchain_config, &wallet_path, @@ -258,7 +260,7 @@ async fn user_round_trip() -> Result<(), Error> { let lp_config_path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/config/lp2.json"); - let reference_lp = ReferenceLP::init(&lp_config_path)?; + let reference_lp = ReferenceLP::create(&lp_config_path)?; let blockchain_config = BlockchainAccessConfig::load_path(blockchain_config_path)?; @@ -270,20 +272,36 @@ async fn user_round_trip() -> Result<(), Error> { let client = RuskHttpClient::new(blockchain_config.rusk_address.clone()); // create request - let request_json: RequestJson = RequestJson::from_file(request_path)?; + let ssk_user_bytes = hex::decode(request_json.user_ssk.clone())?; + let ssk_user = SecretSpendKey::from_slice(ssk_user_bytes.as_slice())?; let request = RequestCreator::create_from_hex_args( - request_json.user_ssk.clone(), + request_json.user_ssk, request_json.provider_psk, rng, )?; - let ssk_user_bytes = hex::decode(request_json.user_ssk)?; - let ssk_user = SecretSpendKey::from_slice(ssk_user_bytes.as_slice())?; + // as a User, submit request to blockchain + info!("submitting request to blockchain (as a User)"); + let tx_id = RequestSender::send_request( + request, + &blockchain_config, + &wallet_path, + &PwdHash(PWD_HASH.to_string()), + GAS_LIMIT, + GAS_PRICE, + ) + .await?; + TxAwaiter::wait_for(&client, tx_id).await?; - // as a LP, call issue license, wait for tx to confirm + // as a LP, retrieve request from blockchain + info!("retrieving request from blockchain (as an LP)"); + let tx_id = format!("{:X}", tx_id); + let request: Request = + PayloadRetriever::retrieve_payload(tx_id, &client).await?; + // as a LP, call issue license, wait for tx to confirm show_state(&client, "before issue_license").await?; info!("calling issue_license (as an LP)"); let issue_license_txid = issue_license( @@ -300,7 +318,6 @@ async fn user_round_trip() -> Result<(), Error> { info!("end_height={}", end_height); // as a User, call get_licenses, obtain license and pos - let start_height = if end_height > BLOCK_RANGE { end_height - BLOCK_RANGE } else { @@ -319,14 +336,17 @@ async fn user_round_trip() -> Result<(), Error> { .expect("owned license found"); // as a User, call get_merkle_opening, obtain opening - info!("calling get_merkle_opening (as a user)"); - let opening = CitadelInquirer::get_merkle_opening(&client, pos).await?; + let opening = + CitadelInquirer::get_merkle_opening(&client, pos.clone()).await?; assert!(opening.is_some()); // as a User, compute proof, call use_license, wait for tx to confirm - show_state(&client, "before use_license").await?; + // for test purposes we make challenge dependent on the number of sessions, + // so that it is different every time we run the test + let (_, _, num_sessions) = CitadelInquirer::get_info(&client).await?; + let challenge = JubJubScalar::from(num_sessions as u64 + 1); info!("calling use_license (as a user)"); let session_id = prove_and_send_use_license( &client, @@ -339,13 +359,13 @@ async fn user_round_trip() -> Result<(), Error> { &license, opening.unwrap(), rng, + &challenge, ) .await?; show_state(&client, "after use_license").await?; let session_id = LicenseSessionId { id: session_id }; // as an SP, call get_session - info!("calling get_session (as an SP)"); let session = CitadelInquirer::get_session(&client, session_id).await?; assert!(session.is_some()); diff --git a/integration-tests/tests/issue_license.rs b/integration-tests/tests/citadel/issue_license.rs similarity index 97% rename from integration-tests/tests/issue_license.rs rename to integration-tests/tests/citadel/issue_license.rs index c3e2161..eb4e027 100644 --- a/integration-tests/tests/issue_license.rs +++ b/integration-tests/tests/citadel/issue_license.rs @@ -31,7 +31,7 @@ async fn issue_license() -> Result<(), Error> { let lp_config_path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/config/lp2.json"); - let reference_lp = ReferenceLP::init(&lp_config_path)?; + let reference_lp = ReferenceLP::create(&lp_config_path)?; let request_json: RequestJson = RequestJson::from_file(request_path)?; diff --git a/integration-tests/tests/queries.rs b/integration-tests/tests/citadel/license_queries.rs similarity index 58% rename from integration-tests/tests/queries.rs rename to integration-tests/tests/citadel/license_queries.rs index 9d2df31..ba403d4 100644 --- a/integration-tests/tests/queries.rs +++ b/integration-tests/tests/citadel/license_queries.rs @@ -4,8 +4,9 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. +use dusk_bls12_381::BlsScalar; use dusk_wallet::RuskHttpClient; -use moat_core::{CitadelInquirer, Error, StreamAux}; +use moat_core::{CitadelInquirer, Error, LicenseSessionId, StreamAux}; use toml_base_config::BaseConfig; use tracing::trace; use wallet_accessor::BlockchainAccessConfig; @@ -44,3 +45,37 @@ async fn call_get_merkle_opening() -> Result<(), Error> { trace!("response={:?}", response); Ok(()) } + +#[tokio::test(flavor = "multi_thread")] +#[cfg_attr(not(feature = "int_tests"), ignore)] +async fn call_get_session() -> Result<(), Error> { + let config_path = + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/config/config.toml"); + let config = BlockchainAccessConfig::load_path(config_path)?; + + let client = RuskHttpClient::new(config.rusk_address); + + let response = CitadelInquirer::get_session( + &client, + LicenseSessionId { + id: BlsScalar::one(), + }, + ) + .await?; + trace!("response={:?}", response); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +#[cfg_attr(not(feature = "int_tests"), ignore)] +async fn call_get_info() -> Result<(), Error> { + let config_path = + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/config/config.toml"); + let config = BlockchainAccessConfig::load_path(config_path)?; + + let client = RuskHttpClient::new(config.rusk_address); + + let response = CitadelInquirer::get_info(&client).await?; + trace!("response={:?}", response); + Ok(()) +} diff --git a/integration-tests/tests/citadel/mod.rs b/integration-tests/tests/citadel/mod.rs new file mode 100644 index 0000000..16df4dc --- /dev/null +++ b/integration-tests/tests/citadel/mod.rs @@ -0,0 +1,12 @@ +// 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. + +mod int_test_lp; +mod int_test_user; +mod issue_license; +mod license_queries; +mod retrieve_requests; +mod send_request; diff --git a/integration-tests/tests/retrieve_requests.rs b/integration-tests/tests/citadel/retrieve_requests.rs similarity index 100% rename from integration-tests/tests/retrieve_requests.rs rename to integration-tests/tests/citadel/retrieve_requests.rs diff --git a/integration-tests/tests/send_request.rs b/integration-tests/tests/citadel/send_request.rs similarity index 88% rename from integration-tests/tests/send_request.rs rename to integration-tests/tests/citadel/send_request.rs index a708a40..06d7429 100644 --- a/integration-tests/tests/send_request.rs +++ b/integration-tests/tests/citadel/send_request.rs @@ -4,11 +4,10 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use dusk_jubjub::BlsScalar; use dusk_wallet::{RuskHttpClient, WalletPath}; use moat_core::{ - Error, PayloadRetriever, PayloadSender, RequestCreator, RequestJson, - LICENSE_CONTRACT_ID, MAX_REQUEST_SIZE, NOOP_METHOD_NAME, + Error, PayloadRetriever, RequestCreator, RequestJson, RequestSender, + MAX_REQUEST_SIZE, }; use moat_core::{JsonLoader, TxAwaiter}; use rand::rngs::StdRng; @@ -59,15 +58,13 @@ async fn send_request() -> Result<(), Error> { PathBuf::from(WALLET_PATH).as_path().join("wallet.dat"), ); - let tx_id = PayloadSender::send_to_contract_method( - (request, 0u64, BlsScalar::one()), + let tx_id = RequestSender::send_request( + request, &config, &wallet_path, &PwdHash(PWD_HASH.to_string()), GAS_LIMIT, GAS_PRICE, - LICENSE_CONTRACT_ID, - NOOP_METHOD_NAME, ) .await?; let client = RuskHttpClient::new(config.rusk_address); @@ -75,7 +72,7 @@ async fn send_request() -> Result<(), Error> { let tx_id_hex = format!("{:x}", tx_id); - let (retrieved_request, _, _) = + let retrieved_request = get_request_from_blockchain(tx_id_hex, &client).await?; assert_eq!( request_vec, @@ -91,7 +88,7 @@ async fn send_request() -> Result<(), Error> { async fn get_request_from_blockchain>( tx_id: S, client: &RuskHttpClient, -) -> Result<(Request, u64, BlsScalar), Error> { +) -> Result { const NUM_RETRIES: i32 = 30; for i in 0..NUM_RETRIES { let result = diff --git a/integration-tests/tests/tests.rs b/integration-tests/tests/tests.rs index df6010c..8fc8fab 100644 --- a/integration-tests/tests/tests.rs +++ b/integration-tests/tests/tests.rs @@ -4,4 +4,6 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. +mod blockchain; +mod citadel; mod websocket; diff --git a/integration-tests/tests/websocket/mod.rs b/integration-tests/tests/websocket/mod.rs index 1a99aac..a6fa734 100644 --- a/integration-tests/tests/websocket/mod.rs +++ b/integration-tests/tests/websocket/mod.rs @@ -6,6 +6,7 @@ pub mod ws_client; pub mod ws_common; +pub mod ws_queries; pub mod ws_server; pub mod ws_test; diff --git a/integration-tests/tests/websocket/ws_queries.rs b/integration-tests/tests/websocket/ws_queries.rs index 1663c2e..4106c0a 100644 --- a/integration-tests/tests/websocket/ws_queries.rs +++ b/integration-tests/tests/websocket/ws_queries.rs @@ -4,8 +4,9 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. +use crate::websocket::ws_license_contract_mock_multi_server; +use dusk_bls12_381::BlsScalar; use moat_core::{CitadelInquirerWs, Error, LicenseSession, LicenseSessionId}; -use dusk_jubjub::BlsScalar; const TEST_DURATION_SECONDS: u64 = 4; const PORT: u32 = 9126; diff --git a/integration-tests/tests/websocket/ws_server.rs b/integration-tests/tests/websocket/ws_server.rs index 61f6284..3178299 100644 --- a/integration-tests/tests/websocket/ws_server.rs +++ b/integration-tests/tests/websocket/ws_server.rs @@ -5,7 +5,7 @@ // Copyright (c) DUSK NETWORK. All rights reserved. use crate::websocket::ws_common::*; -use dusk_jubjub::BlsScalar; +use dusk_bls12_381::BlsScalar; use futures_util::{SinkExt, StreamExt}; use moat_core::{Error, LicenseSession, MAX_RESPONSE_SIZE}; use tokio::net::{TcpListener, TcpStream}; diff --git a/license-provider/Cargo.toml b/license-provider/Cargo.toml index b222fbc..56a05bd 100644 --- a/license-provider/Cargo.toml +++ b/license-provider/Cargo.toml @@ -10,6 +10,7 @@ moat-core={ path = "../moat-core" } wallet-accessor = { path = "../wallet-accessor" } dusk-poseidon = { version = "0.30", default-features = false, features = ["rkyv-impl", "alloc"] } dusk-jubjub = { version = "0.12", default-features = false } +dusk-bls12_381 = "0.11" dusk-pki = { version = "0.12", default-features = false, features = ["rkyv-impl"] } rusk-abi = { version = "0.10.0-piecrust.0.6", default-features = false } rkyv = { version = "=0.7.39" } diff --git a/license-provider/src/license_issuer.rs b/license-provider/src/license_issuer.rs index fa07cb2..2b6a01b 100644 --- a/license-provider/src/license_issuer.rs +++ b/license-provider/src/license_issuer.rs @@ -4,7 +4,8 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use dusk_jubjub::{BlsScalar, JubJubAffine, JubJubScalar}; +use dusk_bls12_381::BlsScalar; +use dusk_jubjub::{JubJubAffine, JubJubScalar}; use dusk_pki::SecretSpendKey; use dusk_poseidon::sponge; use dusk_wallet::{RuskHttpClient, WalletPath}; @@ -62,7 +63,7 @@ impl LicenseIssuer { "sending issue license with license blob size={}", tuple.0.len() ); - let tx_id = PayloadSender::send_to_contract_method( + let tx_id = PayloadSender::execute_contract_method( tuple, &self.config, &self.wallet_path, diff --git a/license-provider/src/reference_lp.rs b/license-provider/src/reference_lp.rs index 3aff0c7..edaa722 100644 --- a/license-provider/src/reference_lp.rs +++ b/license-provider/src/reference_lp.rs @@ -45,7 +45,7 @@ impl ReferenceLP { } } - pub fn init>(lp_config_path: P) -> Result { + pub fn create>(lp_config_path: P) -> Result { let lp_config: LPConfig = LPConfig::from_file(lp_config_path)?; let psk_bytes = hex::decode(lp_config.psk_lp)?; let ssk_bytes = hex::decode(lp_config.ssk_lp)?; diff --git a/license-provider/tests/test_lp.rs b/license-provider/tests/test_lp.rs index b09d006..88f5887 100644 --- a/license-provider/tests/test_lp.rs +++ b/license-provider/tests/test_lp.rs @@ -11,7 +11,7 @@ use moat_core::{Error, JsonLoader, RequestScanner, Transactions}; fn lp_filter_requests() -> Result<(), Error> { let lp_config_path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/config/lp.json"); - let reference_lp = ReferenceLP::init(&lp_config_path)?; + let reference_lp = ReferenceLP::create(&lp_config_path)?; let txs_path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/tx/transactions.json"); diff --git a/moat-cli/src/main.rs b/moat-cli/src/main.rs index cddc91a..d2501a9 100644 --- a/moat-cli/src/main.rs +++ b/moat-cli/src/main.rs @@ -60,10 +60,7 @@ use crate::args::Args; use clap::Parser; use dusk_wallet::WalletPath; -use moat_core::{ - JsonLoader, PayloadSender, RequestCreator, RequestJson, - LICENSE_CONTRACT_ID, NOOP_METHOD_NAME, -}; +use moat_core::{JsonLoader, RequestCreator, RequestJson, RequestSender}; use rand::rngs::StdRng; use rand::SeedableRng; use std::error::Error; @@ -107,15 +104,13 @@ async fn main() -> Result<(), Box> { PwdHash(pwd_hash) }; - PayloadSender::send_to_contract_method( + RequestSender::send_request( request, &blockchain_access_config, &wallet_path, &psw, gas_limit, gas_price, - LICENSE_CONTRACT_ID, - NOOP_METHOD_NAME, ) .await?; diff --git a/moat-core/src/blockchain_requests/mod.rs b/moat-core/src/blockchain_payloads/mod.rs similarity index 78% rename from moat-core/src/blockchain_requests/mod.rs rename to moat-core/src/blockchain_payloads/mod.rs index 977c339..21f9d8d 100644 --- a/moat-core/src/blockchain_requests/mod.rs +++ b/moat-core/src/blockchain_payloads/mod.rs @@ -7,11 +7,7 @@ mod payload_extractor; mod payload_retriever; mod payload_sender; -mod request_creator; -mod request_scanner; pub use payload_extractor::PayloadExtractor; pub use payload_retriever::PayloadRetriever; pub use payload_sender::PayloadSender; -pub use request_creator::RequestCreator; -pub use request_scanner::RequestScanner; diff --git a/moat-core/src/blockchain_requests/payload_extractor.rs b/moat-core/src/blockchain_payloads/payload_extractor.rs similarity index 100% rename from moat-core/src/blockchain_requests/payload_extractor.rs rename to moat-core/src/blockchain_payloads/payload_extractor.rs diff --git a/moat-core/src/blockchain_requests/payload_retriever.rs b/moat-core/src/blockchain_payloads/payload_retriever.rs similarity index 85% rename from moat-core/src/blockchain_requests/payload_retriever.rs rename to moat-core/src/blockchain_payloads/payload_retriever.rs index bd816b5..738a712 100644 --- a/moat-core/src/blockchain_requests/payload_retriever.rs +++ b/moat-core/src/blockchain_payloads/payload_retriever.rs @@ -9,9 +9,9 @@ use dusk_wallet::RuskHttpClient; use rkyv::validation::validators::DefaultValidator; use rkyv::{Archive, Deserialize, Infallible}; -use crate::blockchain_requests::PayloadExtractor; +use crate::blockchain_payloads::PayloadExtractor; use crate::error::Error; -use crate::TxRetriever; +use crate::TxInquirer; pub struct PayloadRetriever; @@ -27,7 +27,7 @@ impl PayloadRetriever { + for<'b> CheckBytes>, S: AsRef, { - let (tx, _) = TxRetriever::retrieve_tx(txid.as_ref(), client).await?; + let (tx, _) = TxInquirer::retrieve_tx(txid.as_ref(), client).await?; PayloadExtractor::payload_from_tx(&tx) } } diff --git a/moat-core/src/blockchain_requests/payload_sender.rs b/moat-core/src/blockchain_payloads/payload_sender.rs similarity index 94% rename from moat-core/src/blockchain_requests/payload_sender.rs rename to moat-core/src/blockchain_payloads/payload_sender.rs index 6f6be2b..e151d48 100644 --- a/moat-core/src/blockchain_requests/payload_sender.rs +++ b/moat-core/src/blockchain_payloads/payload_sender.rs @@ -6,7 +6,7 @@ use crate::error::Error; use crate::MAX_CALL_SIZE; -use dusk_jubjub::BlsScalar; +use dusk_bls12_381::BlsScalar; use dusk_wallet::WalletPath; use phoenix_core::transaction::ModuleId; use rkyv::ser::serializers::AllocSerializer; @@ -17,7 +17,7 @@ pub struct PayloadSender; impl PayloadSender { /// Sends payload to a given method #[allow(clippy::too_many_arguments)] - pub async fn send_to_contract_method( + pub async fn execute_contract_method( payload: P, cfg: &BlockchainAccessConfig, wallet_path: &WalletPath, diff --git a/moat-core/src/tx_retrievals/bc_inquirer.rs b/moat-core/src/blockchain_queries/bc_inquirer.rs similarity index 100% rename from moat-core/src/tx_retrievals/bc_inquirer.rs rename to moat-core/src/blockchain_queries/bc_inquirer.rs diff --git a/moat-core/src/tx_retrievals/mod.rs b/moat-core/src/blockchain_queries/mod.rs similarity index 87% rename from moat-core/src/tx_retrievals/mod.rs rename to moat-core/src/blockchain_queries/mod.rs index 3b989d1..a6ff26f 100644 --- a/moat-core/src/tx_retrievals/mod.rs +++ b/moat-core/src/blockchain_queries/mod.rs @@ -6,8 +6,8 @@ mod bc_inquirer; mod tx_awaiter; -mod tx_retriever; +mod tx_inquirer; pub use bc_inquirer::BcInquirer; pub use tx_awaiter::TxAwaiter; -pub use tx_retriever::TxRetriever; +pub use tx_inquirer::TxInquirer; diff --git a/moat-core/src/tx_retrievals/tx_awaiter.rs b/moat-core/src/blockchain_queries/tx_awaiter.rs similarity index 100% rename from moat-core/src/tx_retrievals/tx_awaiter.rs rename to moat-core/src/blockchain_queries/tx_awaiter.rs diff --git a/moat-core/src/tx_retrievals/tx_retriever.rs b/moat-core/src/blockchain_queries/tx_inquirer.rs similarity index 92% rename from moat-core/src/tx_retrievals/tx_retriever.rs rename to moat-core/src/blockchain_queries/tx_inquirer.rs index 1afa3a7..739ec2d 100644 --- a/moat-core/src/tx_retrievals/tx_retriever.rs +++ b/moat-core/src/blockchain_queries/tx_inquirer.rs @@ -10,20 +10,16 @@ use crate::Error::TransactionNotFound; use crate::{BcInquirer, QueryResult}; use dusk_wallet::RuskHttpClient; -pub struct TxRetriever; +pub struct TxInquirer; -impl TxRetriever { +impl TxInquirer { pub async fn txs_from_block( client: &RuskHttpClient, block_height: u64, ) -> Result { - TxRetriever::txs_from_block_range( - client, - block_height, - block_height + 1, - ) - .await - .map(|(txs, _)| txs) + TxInquirer::txs_from_block_range(client, block_height, block_height + 1) + .await + .map(|(txs, _)| txs) } // range retrieval seems to have a limit of 10k diff --git a/moat-core/src/citadel_queries/citadel_types.rs b/moat-core/src/citadel_queries/citadel_types.rs index ec84d55..daa4820 100644 --- a/moat-core/src/citadel_queries/citadel_types.rs +++ b/moat-core/src/citadel_queries/citadel_types.rs @@ -5,7 +5,7 @@ // Copyright (c) DUSK NETWORK. All rights reserved. use bytecheck::CheckBytes; -use dusk_jubjub::BlsScalar; +use dusk_bls12_381::BlsScalar; use rkyv::{Archive, Deserialize, Serialize}; /// License Session Id diff --git a/moat-core/src/citadel_requests/mod.rs b/moat-core/src/citadel_requests/mod.rs new file mode 100644 index 0000000..1c0cb1c --- /dev/null +++ b/moat-core/src/citadel_requests/mod.rs @@ -0,0 +1,13 @@ +// 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. + +mod request_creator; +mod request_scanner; +mod request_sender; + +pub use request_creator::RequestCreator; +pub use request_scanner::RequestScanner; +pub use request_sender::RequestSender; diff --git a/moat-core/src/blockchain_requests/request_creator.rs b/moat-core/src/citadel_requests/request_creator.rs similarity index 86% rename from moat-core/src/blockchain_requests/request_creator.rs rename to moat-core/src/citadel_requests/request_creator.rs index ba0a9d0..1a69449 100644 --- a/moat-core/src/blockchain_requests/request_creator.rs +++ b/moat-core/src/citadel_requests/request_creator.rs @@ -15,6 +15,8 @@ use zk_citadel::license::Request; pub struct RequestCreator; impl RequestCreator { + /// creates request from user's secret spend key + /// and license provider's public spend key pub fn create( ssk_user: &SecretSpendKey, psk_lp: &PublicSpendKey, @@ -31,6 +33,9 @@ impl RequestCreator { Ok(request) } + /// creates request from user's secret spend key + /// and license provider's public spend key + /// parameters are expected to be hexadecimal strings pub fn create_from_hex_args< R: RngCore + CryptoRng, S: AsRef, diff --git a/moat-core/src/blockchain_requests/request_scanner.rs b/moat-core/src/citadel_requests/request_scanner.rs similarity index 87% rename from moat-core/src/blockchain_requests/request_scanner.rs rename to moat-core/src/citadel_requests/request_scanner.rs index 441cf77..67f94df 100644 --- a/moat-core/src/blockchain_requests/request_scanner.rs +++ b/moat-core/src/citadel_requests/request_scanner.rs @@ -4,9 +4,9 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use crate::blockchain_requests::PayloadExtractor; +use crate::blockchain_payloads::PayloadExtractor; use crate::error::Error; -use crate::{Transactions, TxRetriever}; +use crate::{Transactions, TxInquirer}; use dusk_wallet::RuskHttpClient; use wallet_accessor::BlockchainAccessConfig; use zk_citadel::license::Request; @@ -34,7 +34,7 @@ impl RequestScanner { ) -> Result, Error> { let client = RuskHttpClient::new(cfg.rusk_address.clone()); let txs = - TxRetriever::txs_from_last_n_blocks(&client, last_n_blocks).await?; + TxInquirer::txs_from_last_n_blocks(&client, last_n_blocks).await?; let requests = RequestScanner::scan_transactions(txs); Ok(requests) } @@ -47,7 +47,7 @@ impl RequestScanner { ) -> Result<(Vec, u64), Error> { let client = RuskHttpClient::new(cfg.rusk_address.clone()); let (txs, top) = - TxRetriever::txs_from_block_range(&client, height_beg, height_end) + TxInquirer::txs_from_block_range(&client, height_beg, height_end) .await?; let requests = RequestScanner::scan_transactions(txs); Ok((requests, top)) diff --git a/moat-core/src/citadel_requests/request_sender.rs b/moat-core/src/citadel_requests/request_sender.rs new file mode 100644 index 0000000..17834a4 --- /dev/null +++ b/moat-core/src/citadel_requests/request_sender.rs @@ -0,0 +1,37 @@ +// 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 crate::{Error, PayloadSender, LICENSE_CONTRACT_ID, NOOP_METHOD_NAME}; +use dusk_bls12_381::BlsScalar; +use dusk_wallet::WalletPath; +use wallet_accessor::{BlockchainAccessConfig, Password}; +use zk_citadel::license::Request; + +pub struct RequestSender; + +impl RequestSender { + pub async fn send_request( + request: Request, + config: &BlockchainAccessConfig, + wallet_path: &WalletPath, + password: &Password, + gas_limit: u64, + gas_price: u64, + ) -> Result { + let tx_id = PayloadSender::execute_contract_method( + request, + config, + wallet_path, + password, + gas_limit, + gas_price, + LICENSE_CONTRACT_ID, + NOOP_METHOD_NAME, + ) + .await?; + Ok(tx_id) + } +} diff --git a/moat-core/src/lib.rs b/moat-core/src/lib.rs index ce2c189..1d94737 100644 --- a/moat-core/src/lib.rs +++ b/moat-core/src/lib.rs @@ -12,28 +12,29 @@ //! subproject. mod bc_types; -mod blockchain_requests; +mod blockchain_payloads; +mod blockchain_queries; mod circuit; mod citadel_queries; +mod citadel_requests; mod citadel_types; mod contract_queries; mod error; mod json_loader; -mod tx_retrievals; pub use bc_types::*; -pub use blockchain_requests::{ - PayloadExtractor, PayloadRetriever, PayloadSender, RequestCreator, - RequestScanner, +pub use blockchain_payloads::{ + PayloadExtractor, PayloadRetriever, PayloadSender, }; +pub use blockchain_queries::{BcInquirer, TxAwaiter, TxInquirer}; pub use circuit::*; pub use citadel_queries::{ CitadelInquirer, CitadelInquirerWs, LicenseSession, LicenseSessionId, }; +pub use citadel_requests::{RequestCreator, RequestScanner, RequestSender}; pub use citadel_types::*; pub use contract_queries::{ block::*, ContractInquirer, ContractInquirerWs, StreamAux, }; pub use error::Error; pub use json_loader::JsonLoader; -pub use tx_retrievals::{BcInquirer, TxAwaiter, TxRetriever};