diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..11b04b1 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,140 @@ +name: Tests + +on: + pull_request: + +jobs: + check_formatting: + name: Check Formatting + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v3 + + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: cargo-${{ runner.os }}-formatting-${{ hashFiles('**/Cargo.toml', '**/Cargo.lock') }} + restore-keys: | + cargo-${{ runner.os }}-formatting + cargo-${{ runner.os }}- + + - name: Install nightly toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + components: rustfmt + profile: minimal + + - name: Check formatting + run: | + cargo fmt -- --check + + - name: Check docs + run: cargo doc + + wasm: + name: Build WASM binary + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v3 + - uses: actions-rs/toolchain@v1 + with: + toolchain: nightly-2023-10-24 + target: wasm32-unknown-unknown + override: true + profile: minimal + + - uses: jetli/wasm-pack-action@v0.4.0 + with: + version: 'v0.12.1' + + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: cargo-${{ runner.os }}-wasn-tests-${{ hashFiles('**/Cargo.toml', '**/Cargo.lock') }} + restore-keys: | + cargo-${{ runner.os }}-wasn-tests- + cargo-${{ runner.os }}- + + - name: Build wasm package + env: + RUSTUP_TOOLCHAIN: nightly-2023-10-24 + run: wasm-pack build ./kormir-wasm --release --target web + + wasm_checks: + name: Rust Wasm Checks + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v3 + - uses: actions-rs/toolchain@v1 + with: + toolchain: nightly-2023-10-24 + components: clippy + target: wasm32-unknown-unknown + override: true + profile: minimal + + - name: Setup trunk + uses: jetli/trunk-action@v0.1.0 + with: + version: 'latest' + + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: cargo-${{ runner.os }}-rust-wasm-checks-${{ hashFiles('**/Cargo.toml', '**/Cargo.lock') }} + restore-keys: | + cargo-${{ runner.os }}-rust-wasm-checks- + cargo-${{ runner.os }}- + + - name: Check clippy kormir-wasm + run: cargo clippy --all-features --tests --package kormir-wasm -- -D warnings + + core_tests: + name: Core Tests on Linux + timeout-minutes: 60 + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + profile: minimal + components: clippy + + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: cargo-${{ runner.os }}-core-tests-linux-${{ hashFiles('**/Cargo.toml', '**/Cargo.lock') }} + restore-keys: | + cargo-${{ runner.os }}-core-tests-linux- + cargo-${{ runner.os }}- + + - name: Run cargo clippy + run: cargo clippy --all-features --tests -- -D warnings + + - name: Run cargo test + run: cargo test + + - name: Run cargo build + run: cargo build --all-features diff --git a/kormir-server/src/main.rs b/kormir-server/src/main.rs index 03333d6..19d9541 100644 --- a/kormir-server/src/main.rs +++ b/kormir-server/src/main.rs @@ -1,3 +1,6 @@ +use crate::models::oracle_metadata::OracleMetadata; +use crate::models::{PostgresStorage, MIGRATIONS}; +use crate::routes::*; use axum::http::{StatusCode, Uri}; use axum::routing::{get, post}; use axum::{Extension, Router}; @@ -6,11 +9,8 @@ use diesel::r2d2::{ConnectionManager, Pool}; use diesel::PgConnection; use diesel_migrations::MigrationHarness; use kormir::Oracle; -use nostr_sdk::Client; use nostr::Keys; -use crate::models::oracle_metadata::OracleMetadata; -use crate::models::{PostgresStorage, MIGRATIONS}; -use crate::routes::*; +use nostr_sdk::Client; mod models; mod routes; @@ -83,7 +83,7 @@ async fn main() -> anyhow::Result<()> { .map(|s| s.to_string()) .collect::>(); - let client = Client::new(&oracle.nostr_keys()); + let client = Client::new(oracle.nostr_keys()); client.add_relays(relays).await?; client.connect().await; diff --git a/kormir-server/src/models/mod.rs b/kormir-server/src/models/mod.rs index 4c43407..8ab3214 100644 --- a/kormir-server/src/models/mod.rs +++ b/kormir-server/src/models/mod.rs @@ -8,8 +8,8 @@ use diesel::r2d2::{ConnectionManager, Pool}; use diesel_migrations::{embed_migrations, EmbeddedMigrations}; use dlc_messages::oracle_msgs::{EventDescriptor, OracleAnnouncement}; use kormir::error::Error; -use kormir::storage::{OracleEventData, Storage}; use kormir::lightning::util::ser::Writeable; +use kormir::storage::{OracleEventData, Storage}; use nostr::EventId; use std::collections::HashMap; use std::sync::atomic::{AtomicU32, Ordering}; @@ -65,7 +65,8 @@ impl PostgresStorage { .flat_map(|nonce| nonce.outcome_and_sig()) .collect(); - let announcement_event_id = event.announcement_event_id().map(|ann| ann.to_string()); + let announcement_event_id = + event.announcement_event_id().map(|ann| ann.to_string()); let attestation_event_id = event.attestation_event_id().map(|att| att.to_string()); let data = OracleEventData { diff --git a/kormir-server/src/models/schema.rs b/kormir-server/src/models/schema.rs index 6ba583e..84537d3 100644 --- a/kormir-server/src/models/schema.rs +++ b/kormir-server/src/models/schema.rs @@ -39,8 +39,4 @@ diesel::table! { diesel::joinable!(event_nonces -> events (event_id)); -diesel::allow_tables_to_appear_in_same_query!( - event_nonces, - events, - oracle_metadata, -); +diesel::allow_tables_to_appear_in_same_query!(event_nonces, events, oracle_metadata,); diff --git a/kormir-server/src/routes.rs b/kormir-server/src/routes.rs index 7a88de3..b18bb13 100644 --- a/kormir-server/src/routes.rs +++ b/kormir-server/src/routes.rs @@ -218,7 +218,7 @@ pub fn get_oracle_attestation_impl( let (outcomes, signatures): (Vec, Vec) = event .signatures .iter() - .map(|(outcome, signature)| (outcome.clone(), signature.clone())) + .map(|(outcome, signature)| (outcome.clone(), signature)) .unzip(); Ok(OracleAttestation { diff --git a/kormir-wasm/src/lib.rs b/kormir-wasm/src/lib.rs index 2218ee0..2581d9b 100644 --- a/kormir-wasm/src/lib.rs +++ b/kormir-wasm/src/lib.rs @@ -51,7 +51,7 @@ impl Kormir { let oracle = Oracle::from_signing_key(storage.clone(), nsec)?; - let client = Client::new(&oracle.nostr_keys()); + let client = Client::new(oracle.nostr_keys()); client.add_relays(relays.iter().map(|r| r.as_str())).await?; client.connect().await; @@ -71,9 +71,7 @@ impl Kormir { storage .save_to_indexed_db( NSEC_KEY, - hex::encode(nsec.secret_key() - .expect("just imported") - .secret_bytes()), + hex::encode(nsec.secret_key().expect("just imported").secret_bytes()), ) .await?; diff --git a/kormir-wasm/src/models.rs b/kormir-wasm/src/models.rs index 0fcdfc0..133b95c 100644 --- a/kormir-wasm/src/models.rs +++ b/kormir-wasm/src/models.rs @@ -109,7 +109,11 @@ impl From for Attestation { fn from(value: OracleAttestation) -> Self { Self { oracle_public_key: hex::encode(value.oracle_public_key.serialize()), - signatures: value.signatures.iter().map(|x| hex::encode(x.encode())).collect(), + signatures: value + .signatures + .iter() + .map(|x| hex::encode(x.encode())) + .collect(), outcomes: value.outcomes, } } diff --git a/kormir/src/lib.rs b/kormir/src/lib.rs index 18e9fdd..da19339 100644 --- a/kormir/src/lib.rs +++ b/kormir/src/lib.rs @@ -7,11 +7,11 @@ pub mod storage; use crate::error::Error; use crate::storage::Storage; +use bitcoin::bip32::{ChildNumber, DerivationPath, Xpriv}; use bitcoin::hashes::{sha256, Hash}; +use bitcoin::key::XOnlyPublicKey; use bitcoin::secp256k1::{All, Message, Secp256k1, SecretKey}; -use bitcoin::bip32::{ChildNumber, DerivationPath, Xpriv}; use bitcoin::Network; -use bitcoin::key::XOnlyPublicKey; use secp256k1_zkp::Keypair; use std::collections::HashMap; use std::str::FromStr; @@ -59,8 +59,8 @@ impl Oracle { let secp = Secp256k1::new(); let xpriv_bytes = sha256::Hash::hash(&signing_key.secret_bytes()).to_byte_array(); - let nonce_xpriv = Xpriv::new_master(Network::Bitcoin, &xpriv_bytes) - .map_err(|_| Error::Internal)?; + let nonce_xpriv = + Xpriv::new_master(Network::Bitcoin, &xpriv_bytes).map_err(|_| Error::Internal)?; Ok(Self { storage, @@ -198,10 +198,7 @@ impl Oracle { } } -pub fn derive_signing_key( - secp: &Secp256k1, - xpriv: Xpriv, -) -> Result { +pub fn derive_signing_key(secp: &Secp256k1, xpriv: Xpriv) -> Result { let signing_key = xpriv .derive_priv( secp, @@ -217,7 +214,6 @@ mod test { use super::*; use crate::storage::MemoryStorage; use bitcoin::secp256k1::rand::{thread_rng, Rng}; - use bitcoin::Network; fn create_oracle() -> Oracle { let mut seed: [u8; 64] = [0; 64]; diff --git a/kormir/src/nostr_events.rs b/kormir/src/nostr_events.rs index 2abf9db..93c8db8 100644 --- a/kormir/src/nostr_events.rs +++ b/kormir/src/nostr_events.rs @@ -1,5 +1,3 @@ -#![cfg(feature = "nostr")] - use dlc_messages::oracle_msgs::{OracleAnnouncement, OracleAttestation}; use lightning::util::ser::Writeable; use nostr::event::builder::Error;