From c260352fd0df53dfbf329ec66693735fd0e96dc8 Mon Sep 17 00:00:00 2001 From: RajKadir Date: Fri, 1 Apr 2022 17:30:25 +0100 Subject: [PATCH 1/2] chore: add S3 project and testing --- s3/Cargo.toml | 13 +++++ s3/src/main.rs | 156 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 s3/Cargo.toml create mode 100644 s3/src/main.rs diff --git a/s3/Cargo.toml b/s3/Cargo.toml new file mode 100644 index 00000000..249dc8b0 --- /dev/null +++ b/s3/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "rust-practise" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rusty-s3 = "0.2.1" +reqwest = "0.11" +tokio = { version = "1.0.1", features = ["macros", "fs", "rt-multi-thread"] } +getrandom = "0.2" +hex = "0.4" \ No newline at end of file diff --git a/s3/src/main.rs b/s3/src/main.rs new file mode 100644 index 00000000..daf54940 --- /dev/null +++ b/s3/src/main.rs @@ -0,0 +1,156 @@ +use std::error::Error as StdError; +use std::time::Duration; + +use reqwest::Client; +use rusty_s3::actions::{CreateBucket, S3Action, ListObjectsV2, ListObjectsV2Response, ObjectIdentifier}; +use rusty_s3::{Bucket, Credentials, UrlStyle}; + + +const ONE_HOUR: Duration = Duration::from_secs(3600); + +// NOTE: make sure url, key, secret match your local minio server +// TO RUN: cargo test + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(); + + // connection details to a local minio server + let url = "".parse().unwrap(); + let key = ""; + let secret = ""; + let region = "minio"; + + let mut buf = [0; 8]; + getrandom::getrandom(&mut buf).expect("getrandom"); + let hex = hex::encode(&buf); + let name = format!("test-{}", hex); + + // create a bucket + let bucket = Bucket::new(url, UrlStyle::Path, name, region).unwrap(); + + // setup action to send to server + let credential = Credentials::new(key, secret); + let action = CreateBucket::new(&bucket, &credential); + let signed_url = action.sign(ONE_HOUR); + + client.put(signed_url).send().await?.error_for_status()?; + + Ok(()) +} + + +//////////////////////////// +// TESTS /////////////////// +//////////////////////////// + +pub async fn bucket() -> (Bucket, Credentials, Client) { + let mut buf = [0; 8]; + getrandom::getrandom(&mut buf).expect("getrandom"); + + let hex = hex::encode(&buf); + let name = format!("test-{}", hex); + + // connection details to a local minio server + let url = "".parse().unwrap(); + let key = ""; + let secret = ""; + let region = "minio"; + + let bucket = Bucket::new(url, UrlStyle::Path, name, region).unwrap(); + let credentials = Credentials::new(key, secret); + + let client = Client::new(); + let action = CreateBucket::new(&bucket, &credentials); + let url = action.sign(Duration::from_secs(60)); + client + .put(url) + .send() + .await + .expect("send CreateBucket request") + .error_for_status() + .expect("CreateBucket request unexpected status code"); + + (bucket, credentials, client) +} + +struct UserSession { + id: String, + statechain_id: String, + authentication: String, + proofkey: String, + s2: String, // EC_Scalar ? + sig_hash: String, + withdraw_sc_sig: String, + tx_withdraw: String +} + +#[tokio::test] +async fn test_create_bucket_user_table() { + // Schema: statechainentity + /* + Table: User + id String (UUID) true Primary Key + statechain_id String (UUID) false Foreign Key for StateChain table + authentication undetermined true Can be string token for now + proofkey String false + s2 EC Scalar false Required at transfer to create new User + sig_hash String (Hash) false Required for any tx signing + withdraw_sc_sig StateChainSig false Required for withdraw + tx_withdraw Transaction false Withdraw tx data + */ + + println!("Get bucket, credentials and client"); + let (bucket, credentials, client) = bucket().await; + + println!("User session table test"); + + // mock data - // TODO - convert data to json string + let usersession1 = UserSession { + id: String::from("1"), + statechain_id: String::from("someusername123"), + authentication: String::from(""), + proofkey: String::from(""), + s2: String::from(""), + sig_hash: String::from(""), + withdraw_sc_sig: String::from(""), + tx_withdraw: String::from("") + }; + + + + // put request takes a body (the data) and a filename to place the data in + let body = "[{id: \"\", statechain_id: \"\", authentication: \"empty\", proofkey: \" empty \", s2: \" empty \", sig_hash: \" empty \", withdraw_sc_sig:\" empty \", tx_withdraw: \" empty \"}]"; + + let action = bucket.put_object(Some(&credentials), "user.json"); + let url = action.sign(Duration::from_secs(60)); + client + .put(url) + .body(body.clone()) + .send() + .await + .expect("send PutObject") + .error_for_status() + .expect("PutObject unexpected status code"); + + + let action = bucket.get_object(Some(&credentials), "user.json"); + let url = action.sign(Duration::from_secs(60)); + + let resp = client + .get(url) + .send() + .await + .expect("send GetObject") + .error_for_status() + .expect("GetObject unexpected status code"); + let bytes = resp.bytes().await.expect("GetObject read response body"); + + // check data retreived is the same as the json string form above + assert_eq!(body, bytes); +} + +#[tokio::test] +async fn getcoins() { + println!("Test was ran3"); +} \ No newline at end of file From ca173ff9ee667c7be69b1a70283c6727bb17c710 Mon Sep 17 00:00:00 2001 From: RajKadir Date: Fri, 1 Apr 2022 17:34:16 +0100 Subject: [PATCH 2/2] chore: change project name --- s3/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s3/Cargo.toml b/s3/Cargo.toml index 249dc8b0..5785570b 100644 --- a/s3/Cargo.toml +++ b/s3/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "rust-practise" +name = "s3" version = "0.1.0" edition = "2021"