Skip to content

Commit

Permalink
Merge branch 'main' into feature/generic-crypto-module
Browse files Browse the repository at this point in the history
  • Loading branch information
koxu1996 authored Mar 27, 2024
2 parents 773094f + dca516f commit 3171590
Show file tree
Hide file tree
Showing 26 changed files with 2,467 additions and 177 deletions.
1,462 changes: 1,390 additions & 72 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ members = [
"kairos-cli",
"kairos-crypto",
"kairos-server",
"kairos-tx",
"kairos-test-utils",
]

[workspace.package]
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,16 @@ To run a single check e.g. the format check, run:
```sh
nix build .#checks.<system>.treefmt
```

## License

Licensed under either of

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
78 changes: 78 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
advisory-db.flake = false;
risc0pkgs.url = "github:cspr-rad/risc0pkgs";
risc0pkgs.inputs.nixpkgs.follows = "nixpkgs";
csprpkgs.url = "github:cspr-rad/csprpkgs/add-cctl";
csprpkgs.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = inputs@{ self, flake-parts, treefmt-nix, ... }:
Expand Down Expand Up @@ -58,6 +60,11 @@
openssl.dev
] ++ lib.optionals stdenv.isDarwin [
libiconv
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.SystemConfiguration
];
checkInputs = [
inputs'.csprpkgs.packages.cctl
];
meta.mainProgram = "kairos-server";
};
Expand Down Expand Up @@ -93,11 +100,22 @@

coverage-report = craneLib.cargoTarpaulin (kairosNodeAttrs // {
cargoArtifacts = self'.packages.kairos-deps;
# Default values from https://crane.dev/API.html?highlight=tarpau#cranelibcargotarpaulin
# --avoid-cfg-tarpaulin fixes nom/bitvec issue https://github.com/xd009642/tarpaulin/issues/756#issuecomment-838769320
cargoTarpaulinExtraArgs = "--skip-clean --out xml --output-dir $out --avoid-cfg-tarpaulin";
# For some reason cargoTarpaulin runs the tests in the buildPhase
buildInputs = kairosNodeAttrs.buildInputs ++ [
inputs'.csprpkgs.packages.cctl
];
});

audit = craneLib.cargoAudit {
inherit (kairosNodeAttrs) src;
advisory-db = inputs.advisory-db;
# Default values from https://crane.dev/API.html?highlight=cargoAudit#cranelibcargoaudit
# FIXME --ignore RUSTSEC-2022-0093 ignores ed25519-dalek 1.0.1 vulnerability caused by introducing casper-client 2.0.0
# FIXME --ignore RUSTSEC-2024-0013 ignores libgit2-sys 0.14.2+1.5.1 vulnerability caused by introducing casper-client 2.0.0
cargoAuditExtraArgs = "--ignore yanked --ignore RUSTSEC-2022-0093 --ignore RUSTSEC-2024-0013";
};
};

Expand Down
3 changes: 2 additions & 1 deletion kairos-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ axum-extra = { version = "0.9", features = [
"typed-header",
"json-deserializer",
] }
thiserror = "1"
anyhow = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1", features = ["full", "tracing", "macros"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["std", "env-filter"] }
hex = "0.4"
kairos-tx = { path = "../kairos-tx" }

[dev-dependencies]
proptest = "1"
Expand Down
2 changes: 2 additions & 0 deletions kairos-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub mod errors;
pub mod routes;
pub mod state;

mod utils;

use axum::Router;
use axum_extra::routing::RouterExt;
use state::LockedBatchState;
Expand Down
31 changes: 21 additions & 10 deletions kairos-server/src/routes/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
use std::ops::Deref;

use anyhow::anyhow;
use anyhow::{anyhow, Context};
use axum::{extract::State, http::StatusCode, Json};
use axum_extra::routing::TypedPath;
use serde::{Deserialize, Serialize};
use tracing::*;

use crate::{state::LockedBatchState, AppErr, PublicKey};
use kairos_tx::asn::{SigningPayload, TransactionBody};

use crate::routes::PayloadBody;
use crate::{state::LockedBatchState, AppErr};

#[derive(TypedPath, Debug, Clone, Copy)]
#[typed_path("/api/v1/deposit")]
pub struct DepositPath;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Deposit {
pub public_key: PublicKey,
pub amount: u64,
}

#[instrument(level = "trace", skip(state), ret)]
pub async fn deposit_handler(
_: DepositPath,
state: State<LockedBatchState>,
Json(Deposit { public_key, amount }): Json<Deposit>,
Json(body): Json<PayloadBody>,
) -> Result<(), AppErr> {
tracing::info!("parsing transaction data");
let signing_payload: SigningPayload =
body.payload.as_slice().try_into().context("payload err")?;
let deposit = match signing_payload.body {
TransactionBody::Deposit(deposit) => deposit,
_ => {
return Err(AppErr::set_status(
anyhow!("invalid transaction type"),
StatusCode::BAD_REQUEST,
))
}
};
let amount = u64::try_from(deposit.amount).context("invalid amount")?;
let public_key = body.public_key;

tracing::info!("TODO: verifying deposit");

tracing::info!("TODO: adding deposit to batch");
Expand Down
14 changes: 14 additions & 0 deletions kairos-server/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@ pub mod withdraw;
pub use deposit::deposit_handler;
pub use transfer::transfer_handler;
pub use withdraw::withdraw_handler;

use crate::utils::{hex_to_vec, vec_to_hex};
use crate::{PublicKey, Signature};

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct PayloadBody {
pub public_key: PublicKey,
#[serde(deserialize_with = "hex_to_vec", serialize_with = "vec_to_hex")]
pub payload: Vec<u8>,
#[serde(deserialize_with = "hex_to_vec", serialize_with = "vec_to_hex")]
pub signature: Signature,
}
Loading

0 comments on commit 3171590

Please sign in to comment.