diff --git a/.config/flakebox/id b/.config/flakebox/id new file mode 100644 index 00000000..74228ef7 --- /dev/null +++ b/.config/flakebox/id @@ -0,0 +1 @@ +6ed8d7bac0d49950f28394f623607c29d00896bcf1505d366717626babadd81f8f111f93afd1b991b7087d5ce0684b4bcc10124aad93b3876ba1aba600a09cb4 diff --git a/.config/flakebox/shellHook.sh b/.config/flakebox/shellHook.sh new file mode 100644 index 00000000..136ed52c --- /dev/null +++ b/.config/flakebox/shellHook.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +root="$(git rev-parse --show-toplevel)" +dot_git="$(git rev-parse --git-common-dir)" +if [[ ! -d "${dot_git}/hooks" ]]; then mkdir -p "${dot_git}/hooks"; fi +# fix old bug +rm -f "${dot_git}/hooks/comit-msg" +rm -f "${dot_git}/hooks/commit-msg" +ln -sf "${root}/misc/git-hooks/commit-msg" "${dot_git}/hooks/commit-msg" + +root="$(git rev-parse --show-toplevel)" +dot_git="$(git rev-parse --git-common-dir)" +if [[ ! -d "${dot_git}/hooks" ]]; then mkdir -p "${dot_git}/hooks"; fi +# fix old bug +rm -f "${dot_git}/hooks/pre-comit" +rm -f "${dot_git}/hooks/pre-commit" +ln -sf "${root}/misc/git-hooks/pre-commit" "${dot_git}/hooks/pre-commit" + +# set template +git config commit.template misc/git-hooks/commit-template.txt + +if ! flakebox lint --silent; then + >&2 echo "ℹ️ Project recommendations detected. Run 'flakebox lint' for more info." +fi + +if [ -n "${DIRENV_IN_ENVRC:-}" ]; then + # and not set DIRENV_LOG_FORMAT + if [ -n "${DIRENV_LOG_FORMAT:-}" ]; then + >&2 echo "💡 Set 'DIRENV_LOG_FORMAT=\"\"' in your shell environment variables for a cleaner output of direnv" + fi +fi + +>&2 echo "💡 Run 'just' for a list of available 'just ...' helper recipes" diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..3550a30f --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.github/workflows/flakebox-ci.yml b/.github/workflows/flakebox-ci.yml new file mode 100644 index 00000000..f14b1911 --- /dev/null +++ b/.github/workflows/flakebox-ci.yml @@ -0,0 +1,78 @@ +# THIS FILE IS AUTOGENERATED FROM FLAKEBOX CONFIGURATION + +jobs: + build: + name: Build + runs-on: ${{ matrix.runs-on }} + steps: + - uses: actions/checkout@v4 + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@v4 + - name: Magic Nix Cache + uses: DeterminateSystems/magic-nix-cache-action@v2 + - name: Build on ${{ matrix.host }} + run: 'nix flake check -L .# + + ' + strategy: + matrix: + host: + - macos + - linux + include: + - host: linux + runs-on: ubuntu-latest + timeout: 60 + - host: macos + runs-on: macos-12 + timeout: 60 + timeout-minutes: ${{ matrix.timeout }} + flake: + name: Flake self-check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Check Nix flake inputs + uses: DeterminateSystems/flake-checker-action@v5 + with: + fail-mode: true + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@v4 + - name: Magic Nix Cache + uses: DeterminateSystems/magic-nix-cache-action@v2 + - name: Cargo Cache + uses: actions/cache@v3 + with: + key: ${{ runner.os }}-${{ hashFiles('Cargo.lock') }} + path: ~/.cargo + - name: Commit Check + run: '# run the same check that git `pre-commit` hook does + + nix develop --ignore-environment .#lint --command ./misc/git-hooks/pre-commit + + ' +name: CI +'on': + merge_group: + branches: + - master + - main + pull_request: + branches: + - master + - main + push: + branches: + - master + - main + tags: + - v* + workflow_dispatch: {} + + +# THIS FILE IS AUTOGENERATED FROM FLAKEBOX CONFIGURATION diff --git a/.github/workflows/flakebox-flakehub-publish.yml b/.github/workflows/flakebox-flakehub-publish.yml new file mode 100644 index 00000000..5b81f6e6 --- /dev/null +++ b/.github/workflows/flakebox-flakehub-publish.yml @@ -0,0 +1,35 @@ +# THIS FILE IS AUTOGENERATED FROM FLAKEBOX CONFIGURATION + +jobs: + flakehub-publish: + permissions: + contents: read + id-token: write + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ (inputs.tag != null) && format('refs/tags/{0}', inputs.tag) || '' + }} + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@v4 + - name: Flakehub Push + uses: DeterminateSystems/flakehub-push@main + with: + name: ${{ github.repository }} + tag: ${{ inputs.tag }} + visibility: public +name: Publish to Flakehub +'on': + push: + tags: + - v?[0-9]+.[0-9]+.[0-9]+* + workflow_dispatch: + inputs: + tags: + description: The existing tag to publish to FlakeHub + required: true + type: string + + +# THIS FILE IS AUTOGENERATED FROM FLAKEBOX CONFIGURATION diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 00000000..b32175cb --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,4 @@ +group_imports = "StdExternalCrate" +wrap_comments = true +format_code_in_doc_comments = true +imports_granularity = "Module" diff --git a/bindings/cashu-ffi/Cargo.toml b/bindings/cashu-ffi/Cargo.toml index 41ed30f5..e4f39522 100644 --- a/bindings/cashu-ffi/Cargo.toml +++ b/bindings/cashu-ffi/Cargo.toml @@ -17,4 +17,4 @@ url = { workspace = true } uniffi = { workspace = true } [build-dependencies] -uniffi = { workspace = true, features = ["build"] } \ No newline at end of file +uniffi = { workspace = true, features = ["build"] } diff --git a/bindings/cashu-ffi/src/lib.rs b/bindings/cashu-ffi/src/lib.rs index 040f3017..a84bec1c 100644 --- a/bindings/cashu-ffi/src/lib.rs +++ b/bindings/cashu-ffi/src/lib.rs @@ -3,12 +3,15 @@ mod nuts; mod types; mod ffi { + pub use cashu::types::InvoiceStatus; + pub use crate::error::CashuError; pub use crate::nuts::nut00::blinded_message::BlindedMessage; pub use crate::nuts::nut00::blinded_messages::BlindedMessages; pub use crate::nuts::nut00::blinded_signature::BlindedSignature; pub use crate::nuts::nut00::mint_proofs::MintProofs; - pub use crate::nuts::nut00::proof::{mint::Proof as MintProof, Proof}; + pub use crate::nuts::nut00::proof::mint::Proof as MintProof; + pub use crate::nuts::nut00::proof::Proof; pub use crate::nuts::nut00::token::Token; pub use crate::nuts::nut01::key_pair::KeyPair; pub use crate::nuts::nut01::keys::{Keys, KeysResponse}; @@ -26,11 +29,7 @@ mod ffi { pub use crate::nuts::nut08::{MeltRequest, MeltResponse}; pub use crate::nuts::nut09::{MintInfo, MintVersion}; pub use crate::types::amount::Amount; - pub use crate::types::Bolt11Invoice; - pub use crate::types::KeySetInfo; - pub use crate::types::Secret; - - pub use cashu::types::InvoiceStatus; + pub use crate::types::{Bolt11Invoice, KeySetInfo, Secret}; // UDL uniffi::include_scaffolding!("cashu"); diff --git a/bindings/cashu-ffi/src/nuts/nut00/blinded_messages.rs b/bindings/cashu-ffi/src/nuts/nut00/blinded_messages.rs index 66638850..a1a8c59c 100644 --- a/bindings/cashu-ffi/src/nuts/nut00/blinded_messages.rs +++ b/bindings/cashu-ffi/src/nuts/nut00/blinded_messages.rs @@ -1,8 +1,10 @@ -use std::{ops::Deref, sync::Arc}; +use std::ops::Deref; +use std::sync::Arc; use cashu::nuts::nut00::wallet::BlindedMessages as BlindedMessagesSdk; -use crate::{error::Result, Amount, BlindedMessage, Secret, SecretKey}; +use crate::error::Result; +use crate::{Amount, BlindedMessage, Secret, SecretKey}; pub struct BlindedMessages { inner: BlindedMessagesSdk, diff --git a/bindings/cashu-ffi/src/nuts/nut00/blinded_signature.rs b/bindings/cashu-ffi/src/nuts/nut00/blinded_signature.rs index 3775d0eb..c8dd2ab6 100644 --- a/bindings/cashu-ffi/src/nuts/nut00/blinded_signature.rs +++ b/bindings/cashu-ffi/src/nuts/nut00/blinded_signature.rs @@ -3,9 +3,7 @@ use std::sync::Arc; use cashu::nuts::nut00::BlindedSignature as BlindedSignatureSdk; -use crate::Amount; -use crate::Id; -use crate::PublicKey; +use crate::{Amount, Id, PublicKey}; pub struct BlindedSignature { inner: BlindedSignatureSdk, diff --git a/bindings/cashu-ffi/src/nuts/nut00/proof.rs b/bindings/cashu-ffi/src/nuts/nut00/proof.rs index cee5a028..65da1a99 100644 --- a/bindings/cashu-ffi/src/nuts/nut00/proof.rs +++ b/bindings/cashu-ffi/src/nuts/nut00/proof.rs @@ -1,8 +1,10 @@ -use std::{ops::Deref, sync::Arc}; +use std::ops::Deref; +use std::sync::Arc; use cashu::nuts::nut00::Proof as ProofSdk; -use crate::{types::Secret, Amount, Id, PublicKey}; +use crate::types::Secret; +use crate::{Amount, Id, PublicKey}; pub struct Proof { inner: ProofSdk, @@ -73,9 +75,7 @@ pub mod mint { use cashu::nuts::nut00::mint::Proof as ProofSdk; use crate::types::Secret; - use crate::Amount; - use crate::Id; - use crate::PublicKey; + use crate::{Amount, Id, PublicKey}; pub struct Proof { inner: ProofSdk, diff --git a/bindings/cashu-ffi/src/nuts/nut00/token.rs b/bindings/cashu-ffi/src/nuts/nut00/token.rs index fd4538ba..f554489e 100644 --- a/bindings/cashu-ffi/src/nuts/nut00/token.rs +++ b/bindings/cashu-ffi/src/nuts/nut00/token.rs @@ -5,8 +5,7 @@ use cashu::nuts::nut00::wallet::Token as TokenSdk; use cashu::url::UncheckedUrl; use crate::error::Result; -use crate::MintProofs; -use crate::Proof; +use crate::{MintProofs, Proof}; pub struct Token { inner: TokenSdk, diff --git a/bindings/cashu-ffi/src/nuts/nut01/key_pair.rs b/bindings/cashu-ffi/src/nuts/nut01/key_pair.rs index a3dcf859..80330a3b 100644 --- a/bindings/cashu-ffi/src/nuts/nut01/key_pair.rs +++ b/bindings/cashu-ffi/src/nuts/nut01/key_pair.rs @@ -1,7 +1,9 @@ -use std::{ops::Deref, sync::Arc}; +use std::ops::Deref; +use std::sync::Arc; + +use cashu::nuts::nut01::mint::KeyPair as KeyPairSdk; use crate::{PublicKey, SecretKey}; -use cashu::nuts::nut01::mint::KeyPair as KeyPairSdk; pub struct KeyPair { inner: KeyPairSdk, diff --git a/bindings/cashu-ffi/src/nuts/nut01/keys.rs b/bindings/cashu-ffi/src/nuts/nut01/keys.rs index 19a6fd36..f73a94dd 100644 --- a/bindings/cashu-ffi/src/nuts/nut01/keys.rs +++ b/bindings/cashu-ffi/src/nuts/nut01/keys.rs @@ -1,9 +1,12 @@ -use std::{collections::HashMap, ops::Deref, sync::Arc}; +use std::collections::HashMap; +use std::ops::Deref; +use std::sync::Arc; -use crate::{Amount, PublicKey}; use cashu::nuts::nut01::{Keys as KeysSdk, Response as KeysResponseSdk}; use cashu::Amount as AmountSdk; +use crate::{Amount, PublicKey}; + pub struct Keys { inner: KeysSdk, } diff --git a/bindings/cashu-ffi/src/nuts/nut02/key_set.rs b/bindings/cashu-ffi/src/nuts/nut02/key_set.rs index b6ad5760..5a915587 100644 --- a/bindings/cashu-ffi/src/nuts/nut02/key_set.rs +++ b/bindings/cashu-ffi/src/nuts/nut02/key_set.rs @@ -1,9 +1,7 @@ use std::ops::Deref; use std::sync::Arc; -use cashu::nuts::nut02::Id as IdSdk; -use cashu::nuts::nut02::KeySet as KeySetSdk; -use cashu::nuts::nut02::Response; +use cashu::nuts::nut02::{Id as IdSdk, KeySet as KeySetSdk, Response}; use crate::error::Result; use crate::nuts::nut01::keys::Keys; diff --git a/bindings/cashu-ffi/src/nuts/nut03/mod.rs b/bindings/cashu-ffi/src/nuts/nut03/mod.rs index 288598a7..6197f0fa 100644 --- a/bindings/cashu-ffi/src/nuts/nut03/mod.rs +++ b/bindings/cashu-ffi/src/nuts/nut03/mod.rs @@ -1,6 +1,7 @@ use std::str::FromStr; -use cashu::{nuts::nut03::RequestMintResponse as RequestMintResponseSdk, Bolt11Invoice}; +use cashu::nuts::nut03::RequestMintResponse as RequestMintResponseSdk; +use cashu::Bolt11Invoice; use crate::error::Result; diff --git a/bindings/cashu-ffi/src/nuts/nut04/mod.rs b/bindings/cashu-ffi/src/nuts/nut04/mod.rs index 82a7c508..303e68ff 100644 --- a/bindings/cashu-ffi/src/nuts/nut04/mod.rs +++ b/bindings/cashu-ffi/src/nuts/nut04/mod.rs @@ -1,4 +1,5 @@ -use std::{ops::Deref, sync::Arc}; +use std::ops::Deref; +use std::sync::Arc; use cashu::nuts::nut04::{MintRequest as MintRequestSdk, PostMintResponse as PostMintResponseSdk}; diff --git a/bindings/cashu-ffi/src/nuts/nut05/mod.rs b/bindings/cashu-ffi/src/nuts/nut05/mod.rs index 8f1b88d0..98e5ee14 100644 --- a/bindings/cashu-ffi/src/nuts/nut05/mod.rs +++ b/bindings/cashu-ffi/src/nuts/nut05/mod.rs @@ -1,14 +1,15 @@ -use std::{ops::Deref, str::FromStr, sync::Arc}; +use std::ops::Deref; +use std::str::FromStr; +use std::sync::Arc; -use cashu::{ - nuts::nut05::{ - CheckFeesRequest as CheckFeesRequestSdk, CheckFeesResponse as CheckFeesResponseSdk, - MeltRequest as MeltRequestSdk, MeltResponse as MeltResponseSdk, - }, - Bolt11Invoice, +use cashu::nuts::nut05::{ + CheckFeesRequest as CheckFeesRequestSdk, CheckFeesResponse as CheckFeesResponseSdk, + MeltRequest as MeltRequestSdk, MeltResponse as MeltResponseSdk, }; +use cashu::Bolt11Invoice; -use crate::{error::Result, Amount, Proof}; +use crate::error::Result; +use crate::{Amount, Proof}; pub struct CheckFeesRequest { inner: CheckFeesRequestSdk, diff --git a/bindings/cashu-ffi/src/nuts/nut06/mod.rs b/bindings/cashu-ffi/src/nuts/nut06/mod.rs index 4a5e17c9..de5f824e 100644 --- a/bindings/cashu-ffi/src/nuts/nut06/mod.rs +++ b/bindings/cashu-ffi/src/nuts/nut06/mod.rs @@ -1,4 +1,5 @@ -use std::{ops::Deref, sync::Arc}; +use std::ops::Deref; +use std::sync::Arc; use cashu::nuts::nut06::{SplitRequest as SplitRequestSdk, SplitResponse as SplitResponseSdk}; diff --git a/bindings/cashu-ffi/src/nuts/nut07/mod.rs b/bindings/cashu-ffi/src/nuts/nut07/mod.rs index 34642ca6..b4ad68c1 100644 --- a/bindings/cashu-ffi/src/nuts/nut07/mod.rs +++ b/bindings/cashu-ffi/src/nuts/nut07/mod.rs @@ -1,4 +1,5 @@ -use std::{ops::Deref, sync::Arc}; +use std::ops::Deref; +use std::sync::Arc; use cashu::nuts::nut07::{ CheckSpendableRequest as CheckSpendableRequestSdk, diff --git a/bindings/cashu-ffi/src/nuts/nut09/mod.rs b/bindings/cashu-ffi/src/nuts/nut09/mod.rs index ab848326..2cf0fb05 100644 --- a/bindings/cashu-ffi/src/nuts/nut09/mod.rs +++ b/bindings/cashu-ffi/src/nuts/nut09/mod.rs @@ -1,4 +1,5 @@ -use std::{ops::Deref, sync::Arc}; +use std::ops::Deref; +use std::sync::Arc; use cashu::nuts::nut09::{MintInfo as MintInfoSdk, MintVersion as MintVersionSdk}; diff --git a/bindings/cashu-ffi/src/types/amount.rs b/bindings/cashu-ffi/src/types/amount.rs index bfb12e00..d70fb37a 100644 --- a/bindings/cashu-ffi/src/types/amount.rs +++ b/bindings/cashu-ffi/src/types/amount.rs @@ -1,4 +1,5 @@ -use std::{ops::Deref, sync::Arc}; +use std::ops::Deref; +use std::sync::Arc; use cashu::Amount as AmountSdk; diff --git a/bindings/cashu-ffi/src/types/bolt11_invoice.rs b/bindings/cashu-ffi/src/types/bolt11_invoice.rs index 59ebed52..3b6a4457 100644 --- a/bindings/cashu-ffi/src/types/bolt11_invoice.rs +++ b/bindings/cashu-ffi/src/types/bolt11_invoice.rs @@ -1,8 +1,11 @@ -use std::{ops::Deref, str::FromStr, sync::Arc}; +use std::ops::Deref; +use std::str::FromStr; +use std::sync::Arc; use cashu::Bolt11Invoice as Bolt11InvoiceSdk; -use crate::{error::Result, Amount}; +use crate::error::Result; +use crate::Amount; pub struct Bolt11Invoice { inner: Bolt11InvoiceSdk, diff --git a/bindings/cashu-ffi/src/types/keyset_info.rs b/bindings/cashu-ffi/src/types/keyset_info.rs index 677dfd94..d248be35 100644 --- a/bindings/cashu-ffi/src/types/keyset_info.rs +++ b/bindings/cashu-ffi/src/types/keyset_info.rs @@ -1,4 +1,5 @@ -use std::{ops::Deref, sync::Arc}; +use std::ops::Deref; +use std::sync::Arc; use cashu::types::KeysetInfo as KeySetInfoSdk; diff --git a/bindings/cashu-js/examples/amount.js b/bindings/cashu-js/examples/amount.js index 57c1f189..e381dbd4 100644 --- a/bindings/cashu-js/examples/amount.js +++ b/bindings/cashu-js/examples/amount.js @@ -9,4 +9,4 @@ function main() { console.log(amount.toSat()) } -main(); \ No newline at end of file +main(); diff --git a/bindings/cashu-js/justfile b/bindings/cashu-js/justfile index b2741e5a..d1235b12 100644 --- a/bindings/cashu-js/justfile +++ b/bindings/cashu-js/justfile @@ -2,4 +2,4 @@ build: wasm-pack build pack: - npm run package \ No newline at end of file + npm run package diff --git a/bindings/cashu-js/scripts/build.sh b/bindings/cashu-js/scripts/build.sh index 604950fe..2b747eb8 100755 --- a/bindings/cashu-js/scripts/build.sh +++ b/bindings/cashu-js/scripts/build.sh @@ -14,7 +14,7 @@ set -e -cd $(dirname "$0")/.. +cd "$(dirname "$0")"/.. WASM_BINDGEN_WEAKREF=1 wasm-pack build --target nodejs --scope rust-cashu --out-dir pkg "${WASM_PACK_ARGS[@]}" @@ -32,4 +32,4 @@ echo "module.exports = \`$(base64 pkg/cashu_js_bg.wasm)\`;" > pkg/cashu_js_bg.wa mv pkg/cashu_js.js.new pkg/cashu_js.js # also extend the typescript -cat scripts/epilogue.d.ts >> pkg/cashu_js.d.ts \ No newline at end of file +cat scripts/epilogue.d.ts >> pkg/cashu_js.d.ts diff --git a/bindings/cashu-js/scripts/epilogue.d.ts b/bindings/cashu-js/scripts/epilogue.d.ts index db38bcde..2a3f7fe1 100644 --- a/bindings/cashu-js/scripts/epilogue.d.ts +++ b/bindings/cashu-js/scripts/epilogue.d.ts @@ -7,4 +7,4 @@ */ export function loadWasmAsync(): Promise; - export function loadWasmSync(): void; \ No newline at end of file + export function loadWasmSync(): void; diff --git a/bindings/cashu-js/scripts/epilogue.js b/bindings/cashu-js/scripts/epilogue.js index f588ecc6..4028b9cd 100644 --- a/bindings/cashu-js/scripts/epilogue.js +++ b/bindings/cashu-js/scripts/epilogue.js @@ -73,4 +73,4 @@ function unbase64(sBase64) { } return taBytes; -} \ No newline at end of file +} diff --git a/bindings/cashu-js/src/nuts/nut00/blinded_messages.rs b/bindings/cashu-js/src/nuts/nut00/blinded_messages.rs index 1178dbd5..5df4619b 100644 --- a/bindings/cashu-js/src/nuts/nut00/blinded_messages.rs +++ b/bindings/cashu-js/src/nuts/nut00/blinded_messages.rs @@ -1,8 +1,7 @@ use std::ops::Deref; -use wasm_bindgen::prelude::*; - use cashu::nuts::nut00::wallet::BlindedMessages; +use wasm_bindgen::prelude::*; use crate::error::{into_err, Result}; use crate::types::JsAmount; diff --git a/bindings/cashu-js/src/nuts/nut00/blinded_signature.rs b/bindings/cashu-js/src/nuts/nut00/blinded_signature.rs index a6e22f0e..cfaa6975 100644 --- a/bindings/cashu-js/src/nuts/nut00/blinded_signature.rs +++ b/bindings/cashu-js/src/nuts/nut00/blinded_signature.rs @@ -3,7 +3,9 @@ use std::ops::Deref; use cashu::nuts::nut00::BlindedSignature; use wasm_bindgen::prelude::*; -use crate::{nuts::nut01::JsPublicKey, nuts::nut02::JsId, types::JsAmount}; +use crate::nuts::nut01::JsPublicKey; +use crate::nuts::nut02::JsId; +use crate::types::JsAmount; #[wasm_bindgen(js_name = BlindedSignature)] pub struct JsBlindedSignature { diff --git a/bindings/cashu-js/src/nuts/nut00/proof.rs b/bindings/cashu-js/src/nuts/nut00/proof.rs index f65b6cfd..6ed34581 100644 --- a/bindings/cashu-js/src/nuts/nut00/proof.rs +++ b/bindings/cashu-js/src/nuts/nut00/proof.rs @@ -3,7 +3,9 @@ use std::ops::Deref; use cashu::nuts::nut00::Proof; use wasm_bindgen::prelude::*; -use crate::{nuts::nut01::JsPublicKey, nuts::nut02::JsId, types::JsAmount, types::JsSecret}; +use crate::nuts::nut01::JsPublicKey; +use crate::nuts::nut02::JsId; +use crate::types::{JsAmount, JsSecret}; #[wasm_bindgen(js_name = Proof)] pub struct JsProof { diff --git a/bindings/cashu-js/src/nuts/nut00/token.rs b/bindings/cashu-js/src/nuts/nut00/token.rs index 8fccab7e..3207d8d6 100644 --- a/bindings/cashu-js/src/nuts/nut00/token.rs +++ b/bindings/cashu-js/src/nuts/nut00/token.rs @@ -1,6 +1,8 @@ -use std::{ops::Deref, str::FromStr}; +use std::ops::Deref; +use std::str::FromStr; -use cashu::{nuts::nut00::wallet::Token, url::UncheckedUrl}; +use cashu::nuts::nut00::wallet::Token; +use cashu::url::UncheckedUrl; use wasm_bindgen::prelude::*; use crate::error::{into_err, Result}; diff --git a/bindings/cashu-js/src/nuts/nut01/keys.rs b/bindings/cashu-js/src/nuts/nut01/keys.rs index c3d69c94..6533d9ac 100644 --- a/bindings/cashu-js/src/nuts/nut01/keys.rs +++ b/bindings/cashu-js/src/nuts/nut01/keys.rs @@ -3,12 +3,9 @@ use std::ops::Deref; use cashu::nuts::nut01::Keys; use wasm_bindgen::prelude::*; -use crate::{ - error::{into_err, Result}, - types::JsAmount, -}; - use super::JsPublicKey; +use crate::error::{into_err, Result}; +use crate::types::JsAmount; #[wasm_bindgen(js_name = Keys)] pub struct JsKeys { diff --git a/bindings/cashu-js/src/nuts/nut02/keyset.rs b/bindings/cashu-js/src/nuts/nut02/keyset.rs index ceed62c4..c3a3c87e 100644 --- a/bindings/cashu-js/src/nuts/nut02/keyset.rs +++ b/bindings/cashu-js/src/nuts/nut02/keyset.rs @@ -4,10 +4,8 @@ use cashu::nuts::nut01::Response as KeysResponse; use cashu::nuts::nut02::{Id, KeySet, Response as KeySetsResponse}; use wasm_bindgen::prelude::*; -use crate::{ - error::{into_err, Result}, - nuts::nut01::JsKeys, -}; +use crate::error::{into_err, Result}; +use crate::nuts::nut01::JsKeys; #[wasm_bindgen(js_name = Id)] pub struct JsId { diff --git a/bindings/cashu-js/src/nuts/nut02/mod.rs b/bindings/cashu-js/src/nuts/nut02/mod.rs index 8682b32d..f18b73b8 100644 --- a/bindings/cashu-js/src/nuts/nut02/mod.rs +++ b/bindings/cashu-js/src/nuts/nut02/mod.rs @@ -1,8 +1,5 @@ mod keyset; mod mint_keyset; -pub use keyset::JsId; -pub use keyset::JsKeySet; -pub use keyset::JsKeySetsResponse; -pub use keyset::JsKeysResponse; +pub use keyset::{JsId, JsKeySet, JsKeySetsResponse, JsKeysResponse}; pub use mint_keyset::JsMintKeySet; diff --git a/bindings/cashu-js/src/nuts/nut04.rs b/bindings/cashu-js/src/nuts/nut04.rs index 7737603e..3beb0420 100644 --- a/bindings/cashu-js/src/nuts/nut04.rs +++ b/bindings/cashu-js/src/nuts/nut04.rs @@ -3,10 +3,8 @@ use std::ops::Deref; use cashu::nuts::nut04::{MintRequest, PostMintResponse}; use wasm_bindgen::prelude::*; -use crate::{ - error::{into_err, Result}, - types::JsAmount, -}; +use crate::error::{into_err, Result}; +use crate::types::JsAmount; #[wasm_bindgen(js_name = MintRequest)] pub struct JsMintRequest { diff --git a/bindings/cashu-js/src/nuts/nut05.rs b/bindings/cashu-js/src/nuts/nut05.rs index cd63b9c8..33a1fdd0 100644 --- a/bindings/cashu-js/src/nuts/nut05.rs +++ b/bindings/cashu-js/src/nuts/nut05.rs @@ -3,10 +3,8 @@ use std::ops::Deref; use cashu::nuts::nut05::{CheckFeesRequest, CheckFeesResponse}; use wasm_bindgen::prelude::*; -use crate::{ - error::Result, - types::{JsAmount, JsBolt11Invoice}, -}; +use crate::error::Result; +use crate::types::{JsAmount, JsBolt11Invoice}; #[wasm_bindgen(js_name = CheckFeesRequest)] pub struct JsCheckFeesRequest { diff --git a/bindings/cashu-js/src/nuts/nut06.rs b/bindings/cashu-js/src/nuts/nut06.rs index eb6272ce..55dd9901 100644 --- a/bindings/cashu-js/src/nuts/nut06.rs +++ b/bindings/cashu-js/src/nuts/nut06.rs @@ -3,10 +3,8 @@ use std::ops::Deref; use cashu::nuts::nut06::{SplitRequest, SplitResponse}; use wasm_bindgen::prelude::*; -use crate::{ - error::{into_err, Result}, - types::JsAmount, -}; +use crate::error::{into_err, Result}; +use crate::types::JsAmount; #[wasm_bindgen(js_name = SplitRequest)] pub struct JsSplitRequest { diff --git a/bindings/cashu-js/src/nuts/nut08.rs b/bindings/cashu-js/src/nuts/nut08.rs index 56205938..4f7142f1 100644 --- a/bindings/cashu-js/src/nuts/nut08.rs +++ b/bindings/cashu-js/src/nuts/nut08.rs @@ -1,15 +1,11 @@ use std::ops::Deref; -use cashu::nuts::{ - nut00::{BlindedMessage, BlindedSignature, Proof}, - nut08::{MeltRequest, MeltResponse}, -}; +use cashu::nuts::nut00::{BlindedMessage, BlindedSignature, Proof}; +use cashu::nuts::nut08::{MeltRequest, MeltResponse}; use wasm_bindgen::prelude::*; -use crate::{ - error::{into_err, Result}, - types::{JsAmount, JsBolt11Invoice}, -}; +use crate::error::{into_err, Result}; +use crate::types::{JsAmount, JsBolt11Invoice}; #[wasm_bindgen(js_name = MeltRequest)] pub struct JsMeltRequest { diff --git a/bindings/cashu-js/src/nuts/nut09.rs b/bindings/cashu-js/src/nuts/nut09.rs index 52014944..e5cedfef 100644 --- a/bindings/cashu-js/src/nuts/nut09.rs +++ b/bindings/cashu-js/src/nuts/nut09.rs @@ -3,9 +3,8 @@ use std::ops::Deref; use cashu::nuts::nut09::{MintInfo, MintVersion}; use wasm_bindgen::prelude::*; -use crate::error::{into_err, Result}; - use super::nut01::JsPublicKey; +use crate::error::{into_err, Result}; #[wasm_bindgen(js_name = MintVersion)] pub struct JsMintVersion { diff --git a/bindings/cashu-js/src/types/bolt11_invoice.rs b/bindings/cashu-js/src/types/bolt11_invoice.rs index 17156cb6..08603395 100644 --- a/bindings/cashu-js/src/types/bolt11_invoice.rs +++ b/bindings/cashu-js/src/types/bolt11_invoice.rs @@ -1,4 +1,5 @@ -use std::{ops::Deref, str::FromStr}; +use std::ops::Deref; +use std::str::FromStr; use cashu::Bolt11Invoice; use wasm_bindgen::prelude::*; diff --git a/bindings/cashu-sdk-ffi/src/cashu_sdk.udl b/bindings/cashu-sdk-ffi/src/cashu_sdk.udl index 33bf4ad4..dba4caef 100644 --- a/bindings/cashu-sdk-ffi/src/cashu_sdk.udl +++ b/bindings/cashu-sdk-ffi/src/cashu_sdk.udl @@ -352,4 +352,4 @@ interface Mint { void verify_melt_request(MeltRequest melt_request); [Throws=CashuSdkError] MeltResponse process_melt_request(MeltRequest melt_request, string preimage, Amount totoal_spent); -}; \ No newline at end of file +}; diff --git a/bindings/cashu-sdk-ffi/src/mint.rs b/bindings/cashu-sdk-ffi/src/mint.rs index 368d38c5..cbded3e5 100644 --- a/bindings/cashu-sdk-ffi/src/mint.rs +++ b/bindings/cashu-sdk-ffi/src/mint.rs @@ -1,7 +1,5 @@ -use std::{ - ops::Deref, - sync::{Arc, RwLock}, -}; +use std::ops::Deref; +use std::sync::{Arc, RwLock}; use cashu_ffi::{ Amount, CheckSpendableRequest, CheckSpendableResponse, Id, KeySet, KeySetInfo, KeySetResponse, diff --git a/bindings/cashu-sdk-ffi/src/types/melted.rs b/bindings/cashu-sdk-ffi/src/types/melted.rs index 4213ecb9..38949d2e 100644 --- a/bindings/cashu-sdk-ffi/src/types/melted.rs +++ b/bindings/cashu-sdk-ffi/src/types/melted.rs @@ -1,8 +1,8 @@ -use std::{ops::Deref, sync::Arc}; - -use cashu_sdk::types::Melted as MeltedSdk; +use std::ops::Deref; +use std::sync::Arc; use cashu_ffi::Proof; +use cashu_sdk::types::Melted as MeltedSdk; pub struct Melted { inner: MeltedSdk, diff --git a/bindings/cashu-sdk-ffi/src/types/proofs_status.rs b/bindings/cashu-sdk-ffi/src/types/proofs_status.rs index 7f8c1985..992b7ce1 100644 --- a/bindings/cashu-sdk-ffi/src/types/proofs_status.rs +++ b/bindings/cashu-sdk-ffi/src/types/proofs_status.rs @@ -1,4 +1,5 @@ -use std::{ops::Deref, sync::Arc}; +use std::ops::Deref; +use std::sync::Arc; use cashu_sdk::types::ProofsStatus as ProofsStatusSdk; diff --git a/bindings/cashu-sdk-ffi/src/types/send_proofs.rs b/bindings/cashu-sdk-ffi/src/types/send_proofs.rs index cf8d2bac..02e6b24b 100644 --- a/bindings/cashu-sdk-ffi/src/types/send_proofs.rs +++ b/bindings/cashu-sdk-ffi/src/types/send_proofs.rs @@ -1,8 +1,8 @@ -use std::{ops::Deref, sync::Arc}; - -use cashu_sdk::types::SendProofs as SendProofsSdk; +use std::ops::Deref; +use std::sync::Arc; use cashu_ffi::Proof; +use cashu_sdk::types::SendProofs as SendProofsSdk; pub struct SendProofs { inner: SendProofsSdk, diff --git a/bindings/cashu-sdk-ffi/src/wallet.rs b/bindings/cashu-sdk-ffi/src/wallet.rs index 49f63ca2..dedd0772 100644 --- a/bindings/cashu-sdk-ffi/src/wallet.rs +++ b/bindings/cashu-sdk-ffi/src/wallet.rs @@ -7,12 +7,10 @@ use cashu_ffi::{ use cashu_sdk::types::ProofsStatus; use cashu_sdk::wallet::Wallet as WalletSdk; -use crate::{ - client::Client, - error::Result, - types::{Melted, SendProofs}, - Amount, Keys, MintProof, -}; +use crate::client::Client; +use crate::error::Result; +use crate::types::{Melted, SendProofs}; +use crate::{Amount, Keys, MintProof}; pub struct Wallet { inner: WalletSdk, diff --git a/bindings/cashu-sdk-js/examples/amount.js b/bindings/cashu-sdk-js/examples/amount.js index 57c1f189..e381dbd4 100644 --- a/bindings/cashu-sdk-js/examples/amount.js +++ b/bindings/cashu-sdk-js/examples/amount.js @@ -9,4 +9,4 @@ function main() { console.log(amount.toSat()) } -main(); \ No newline at end of file +main(); diff --git a/bindings/cashu-sdk-js/examples/wallet.js b/bindings/cashu-sdk-js/examples/wallet.js index 3d3e9642..95830c79 100644 --- a/bindings/cashu-sdk-js/examples/wallet.js +++ b/bindings/cashu-sdk-js/examples/wallet.js @@ -16,4 +16,4 @@ async function main() { console.log(pr); } -main(); \ No newline at end of file +main(); diff --git a/bindings/cashu-sdk-js/justfile b/bindings/cashu-sdk-js/justfile index b2741e5a..d1235b12 100644 --- a/bindings/cashu-sdk-js/justfile +++ b/bindings/cashu-sdk-js/justfile @@ -2,4 +2,4 @@ build: wasm-pack build pack: - npm run package \ No newline at end of file + npm run package diff --git a/bindings/cashu-sdk-js/scripts/build.sh b/bindings/cashu-sdk-js/scripts/build.sh index c432032c..01ec1540 100755 --- a/bindings/cashu-sdk-js/scripts/build.sh +++ b/bindings/cashu-sdk-js/scripts/build.sh @@ -14,7 +14,7 @@ set -e -cd $(dirname "$0")/.. +cd "$(dirname "$0")"/.. WASM_BINDGEN_WEAKREF=1 wasm-pack build --target nodejs --scope rust-cashu --out-dir pkg "${WASM_PACK_ARGS[@]}" @@ -32,4 +32,4 @@ echo "module.exports = \`$(base64 pkg/cashu_sdk_js_bg.wasm)\`;" > pkg/cashu_sdk_ mv pkg/cashu_sdk_js.js.new pkg/cashu_sdk_js.js # also extend the typescript -cat scripts/epilogue.d.ts >> pkg/cashu_sdk_js.d.ts \ No newline at end of file +cat scripts/epilogue.d.ts >> pkg/cashu_sdk_js.d.ts diff --git a/bindings/cashu-sdk-js/scripts/epilogue.d.ts b/bindings/cashu-sdk-js/scripts/epilogue.d.ts index db38bcde..2a3f7fe1 100644 --- a/bindings/cashu-sdk-js/scripts/epilogue.d.ts +++ b/bindings/cashu-sdk-js/scripts/epilogue.d.ts @@ -7,4 +7,4 @@ */ export function loadWasmAsync(): Promise; - export function loadWasmSync(): void; \ No newline at end of file + export function loadWasmSync(): void; diff --git a/bindings/cashu-sdk-js/scripts/epilogue.js b/bindings/cashu-sdk-js/scripts/epilogue.js index da895f66..967076d5 100644 --- a/bindings/cashu-sdk-js/scripts/epilogue.js +++ b/bindings/cashu-sdk-js/scripts/epilogue.js @@ -73,4 +73,4 @@ function unbase64(sBase64) { } return taBytes; -} \ No newline at end of file +} diff --git a/bindings/cashu-sdk-js/src/mint.rs b/bindings/cashu-sdk-js/src/mint.rs index 0dca7f45..82ab01a8 100644 --- a/bindings/cashu-sdk-js/src/mint.rs +++ b/bindings/cashu-sdk-js/src/mint.rs @@ -1,16 +1,14 @@ use std::ops::Deref; -use cashu_js::{ - nuts::{ - nut02::{JsId, JsKeySet, JsKeySetsResponse, JsKeysResponse, JsMintKeySet}, - nut04::{JsMintRequest, JsPostMintResponse}, - nut06::{JsSplitRequest, JsSplitResponse}, - nut07::{JsCheckSpendableRequest, JsCheckSpendableResponse}, - nut08::{JsMeltRequest, JsMeltResponse}, - }, - JsAmount, -}; -use cashu_sdk::{mint::Mint, nuts::nut01, nuts::nut02::KeySet}; +use cashu_js::nuts::nut02::{JsId, JsKeySet, JsKeySetsResponse, JsKeysResponse, JsMintKeySet}; +use cashu_js::nuts::nut04::{JsMintRequest, JsPostMintResponse}; +use cashu_js::nuts::nut06::{JsSplitRequest, JsSplitResponse}; +use cashu_js::nuts::nut07::{JsCheckSpendableRequest, JsCheckSpendableResponse}; +use cashu_js::nuts::nut08::{JsMeltRequest, JsMeltResponse}; +use cashu_js::JsAmount; +use cashu_sdk::mint::Mint; +use cashu_sdk::nuts::nut01; +use cashu_sdk::nuts::nut02::KeySet; use wasm_bindgen::prelude::*; use crate::error::{into_err, Result}; diff --git a/bindings/cashu-sdk-js/src/wallet.rs b/bindings/cashu-sdk-js/src/wallet.rs index dad0b157..6862a8cd 100644 --- a/bindings/cashu-sdk-js/src/wallet.rs +++ b/bindings/cashu-sdk-js/src/wallet.rs @@ -1,17 +1,15 @@ use std::ops::Deref; use cashu_js::nuts::nut00::{JsBlindedMessages, JsToken}; +use cashu_js::nuts::nut01::JsKeys; use cashu_js::nuts::nut03::JsRequestMintResponse; -use cashu_js::{nuts::nut01::JsKeys, JsAmount}; -use cashu_js::{JsBolt11Invoice, JsProofsStatus}; +use cashu_js::{JsAmount, JsBolt11Invoice, JsProofsStatus}; use cashu_sdk::wallet::Wallet; use wasm_bindgen::prelude::*; +use crate::error::{into_err, Result}; use crate::types::{JsMelted, JsSendProofs}; -use crate::{ - error::{into_err, Result}, - JsClient, -}; +use crate::JsClient; #[wasm_bindgen(js_name = Wallet)] pub struct JsWallet { diff --git a/bindings/uniffi-bindgen/Cargo.toml b/bindings/uniffi-bindgen/Cargo.toml index 99f00d4c..eed466f6 100644 --- a/bindings/uniffi-bindgen/Cargo.toml +++ b/bindings/uniffi-bindgen/Cargo.toml @@ -9,4 +9,4 @@ rust-version.workspace = true uniffi = { workspace = true, features = ["cli"] } [build-dependencies] -uniffi = { workspace = true, features = ["build"] } \ No newline at end of file +uniffi = { workspace = true, features = ["build"] } diff --git a/crates/cashu-sdk/src/client/blocking.rs b/crates/cashu-sdk/src/client/blocking.rs index 072b40bb..63b8deab 100644 --- a/crates/cashu-sdk/src/client/blocking.rs +++ b/crates/cashu-sdk/src/client/blocking.rs @@ -1,22 +1,18 @@ -use crate::RUNTIME; - -use cashu::{ - nuts::{ - nut00::{self, wallet::BlindedMessages, BlindedMessage, Proof}, - nut01::Keys, - nut02, - nut03::RequestMintResponse, - nut04::PostMintResponse, - nut05::CheckFeesResponse, - nut06::{SplitRequest, SplitResponse}, - nut07::CheckSpendableResponse, - nut08::MeltResponse, - nut09::MintInfo, - }, - Amount, Bolt11Invoice, -}; +use cashu::nuts::nut00::wallet::BlindedMessages; +use cashu::nuts::nut00::{self, BlindedMessage, Proof}; +use cashu::nuts::nut01::Keys; +use cashu::nuts::nut02; +use cashu::nuts::nut03::RequestMintResponse; +use cashu::nuts::nut04::PostMintResponse; +use cashu::nuts::nut05::CheckFeesResponse; +use cashu::nuts::nut06::{SplitRequest, SplitResponse}; +use cashu::nuts::nut07::CheckSpendableResponse; +use cashu::nuts::nut08::MeltResponse; +use cashu::nuts::nut09::MintInfo; +use cashu::{Amount, Bolt11Invoice}; use super::Error; +use crate::RUNTIME; #[derive(Debug, Clone)] pub struct Client { diff --git a/crates/cashu-sdk/src/client/mod.rs b/crates/cashu-sdk/src/client/mod.rs index 0a501703..c0567a15 100644 --- a/crates/cashu-sdk/src/client/mod.rs +++ b/crates/cashu-sdk/src/client/mod.rs @@ -2,12 +2,8 @@ use std::fmt; use std::str::FromStr; -use cashu::url::UncheckedUrl; -use serde::{Deserialize, Serialize}; -use serde_json::Value; -use url::Url; - -use cashu::nuts::nut00::{wallet::BlindedMessages, BlindedMessage, Proof}; +use cashu::nuts::nut00::wallet::BlindedMessages; +use cashu::nuts::nut00::{BlindedMessage, Proof}; use cashu::nuts::nut01::Keys; use cashu::nuts::nut03::RequestMintResponse; use cashu::nuts::nut04::{MintRequest, PostMintResponse}; @@ -17,11 +13,13 @@ use cashu::nuts::nut07::{CheckSpendableRequest, CheckSpendableResponse}; use cashu::nuts::nut08::{MeltRequest, MeltResponse}; use cashu::nuts::nut09::MintInfo; use cashu::nuts::*; -use cashu::utils; -use cashu::Amount; - +use cashu::url::UncheckedUrl; +use cashu::{utils, Amount}; #[cfg(target_arch = "wasm32")] use gloo::net::http::Request; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use url::Url; #[cfg(feature = "blocking")] pub mod blocking; diff --git a/crates/cashu-sdk/src/lib.rs b/crates/cashu-sdk/src/lib.rs index fd9ac8f9..3e5f868f 100644 --- a/crates/cashu-sdk/src/lib.rs +++ b/crates/cashu-sdk/src/lib.rs @@ -1,15 +1,13 @@ #[cfg(all(target_arch = "wasm32", feature = "blocking"))] compile_error!("`blocking` feature can't be enabled for WASM targets"); +#[cfg(feature = "blocking")] +use futures_util::Future; #[cfg(feature = "blocking")] use once_cell::sync::Lazy; - #[cfg(feature = "blocking")] use tokio::runtime::Runtime; -#[cfg(feature = "blocking")] -use futures_util::Future; - #[cfg(feature = "wallet")] pub mod client; diff --git a/crates/cashu-sdk/src/mint.rs b/crates/cashu-sdk/src/mint.rs index f1f0380a..f674a5eb 100644 --- a/crates/cashu-sdk/src/mint.rs +++ b/crates/cashu-sdk/src/mint.rs @@ -1,20 +1,13 @@ use std::collections::{HashMap, HashSet}; +use cashu::dhke::{sign_message, verify_message}; pub use cashu::error::mint::Error; - -use cashu::dhke::sign_message; -use cashu::dhke::verify_message; -use cashu::nuts::nut00::BlindedMessage; -use cashu::nuts::nut00::BlindedSignature; -use cashu::nuts::nut00::Proof; +use cashu::nuts::nut00::{BlindedMessage, BlindedSignature, Proof}; use cashu::nuts::nut02::mint::KeySet; use cashu::nuts::nut02::Id; -use cashu::nuts::nut06::SplitRequest; -use cashu::nuts::nut06::SplitResponse; -use cashu::nuts::nut07::CheckSpendableRequest; -use cashu::nuts::nut07::CheckSpendableResponse; -use cashu::nuts::nut08::MeltRequest; -use cashu::nuts::nut08::MeltResponse; +use cashu::nuts::nut06::{SplitRequest, SplitResponse}; +use cashu::nuts::nut07::{CheckSpendableRequest, CheckSpendableResponse}; +use cashu::nuts::nut08::{MeltRequest, MeltResponse}; use cashu::nuts::*; use cashu::secret::Secret; use cashu::types::KeysetInfo; diff --git a/crates/cashu-sdk/src/wallet.rs b/crates/cashu-sdk/src/wallet.rs index 159ffc82..c0f158bb 100644 --- a/crates/cashu-sdk/src/wallet.rs +++ b/crates/cashu-sdk/src/wallet.rs @@ -3,11 +3,9 @@ use std::error::Error as StdError; use std::fmt; use std::str::FromStr; -use cashu::dhke::construct_proofs; -use cashu::dhke::unblind_message; -use cashu::nuts::nut00::{ - mint, wallet::BlindedMessages, wallet::Token, BlindedSignature, Proof, Proofs, -}; +use cashu::dhke::{construct_proofs, unblind_message}; +use cashu::nuts::nut00::wallet::{BlindedMessages, Token}; +use cashu::nuts::nut00::{mint, BlindedSignature, Proof, Proofs}; use cashu::nuts::nut01::Keys; use cashu::nuts::nut03::RequestMintResponse; use cashu::nuts::nut06::{SplitPayload, SplitRequest}; @@ -18,7 +16,6 @@ use tracing::warn; #[cfg(feature = "blocking")] use crate::client::blocking::Client; - #[cfg(not(feature = "blocking"))] use crate::client::Client; diff --git a/crates/cashu/src/dhke.rs b/crates/cashu/src/dhke.rs index 85191962..561bb714 100644 --- a/crates/cashu/src/dhke.rs +++ b/crates/cashu/src/dhke.rs @@ -2,25 +2,19 @@ use std::ops::Mul; -use bitcoin::hashes::sha256; -use bitcoin::hashes::Hash; - +use bitcoin::hashes::{sha256, Hash}; #[cfg(feature = "wallet")] use k256::ProjectivePoint; - use k256::{Scalar, SecretKey}; use crate::error; -use crate::secret::Secret; - #[cfg(feature = "wallet")] use crate::nuts::nut00::{BlindedSignature, Proof, Proofs}; - #[cfg(feature = "wallet")] use crate::nuts::nut01::{Keys, PublicKey}; - #[cfg(feature = "wallet")] use crate::nuts::*; +use crate::secret::Secret; fn hash_to_curve(message: &[u8]) -> k256::PublicKey { let mut msg_to_hash = message.to_vec(); @@ -145,7 +139,6 @@ pub fn verify_message( #[cfg(test)] mod tests { use hex::decode; - use k256::elliptic_curve::scalar::ScalarPrimitive; use super::*; diff --git a/crates/cashu/src/nuts/nut00.rs b/crates/cashu/src/nuts/nut00.rs index ee61f867..2c67a92e 100644 --- a/crates/cashu/src/nuts/nut00.rs +++ b/crates/cashu/src/nuts/nut00.rs @@ -1,11 +1,13 @@ //! Notation and Models // https://github.com/cashubtc/nuts/blob/main/00.md -use crate::{secret::Secret, url::UncheckedUrl, Amount}; use serde::{Deserialize, Serialize}; use super::nut01::PublicKey; use super::nut02::Id; +use crate::secret::Secret; +use crate::url::UncheckedUrl; +use crate::Amount; /// Blinded Message [NUT-00] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -21,21 +23,20 @@ pub struct BlindedMessage { pub mod wallet { use std::str::FromStr; - use base64::{engine::general_purpose, Engine as _}; + use base64::engine::general_purpose; + use base64::Engine as _; use serde::{Deserialize, Serialize}; use url::Url; - use crate::error; + use super::MintProofs; + use crate::dhke::blind_message; use crate::error::wallet; - use crate::nuts::nut00::BlindedMessage; - use crate::nuts::nut00::Proofs; + use crate::nuts::nut00::{BlindedMessage, Proofs}; use crate::nuts::nut01; use crate::secret::Secret; use crate::url::UncheckedUrl; - use crate::Amount; - use crate::{dhke::blind_message, utils::split_amount}; - - use super::MintProofs; + use crate::utils::split_amount; + use crate::{error, Amount}; /// Blinded Messages [NUT-00] #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)] @@ -221,9 +222,10 @@ impl From for mint::Proof { pub mod mint { use serde::{Deserialize, Serialize}; - use crate::{nuts::nut02::Id, secret::Secret, Amount}; - use super::PublicKey; + use crate::nuts::nut02::Id; + use crate::secret::Secret; + use crate::Amount; /// Proofs [NUT-00] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -273,9 +275,7 @@ mod tests { assert_eq!( token.token[0].mint, - UncheckedUrl::from_str("https://8333.space:3338") - .unwrap() - .into() + UncheckedUrl::from_str("https://8333.space:3338").unwrap() ); assert_eq!( token.token[0].proofs[0].clone().id.unwrap(), diff --git a/crates/cashu/src/nuts/nut01.rs b/crates/cashu/src/nuts/nut01.rs index eb2292c2..15ea4211 100644 --- a/crates/cashu/src/nuts/nut01.rs +++ b/crates/cashu/src/nuts/nut01.rs @@ -1,8 +1,7 @@ //! Mint public key exchange // https://github.com/cashubtc/nuts/blob/main/01.md -use std::collections::BTreeMap; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use serde::{Deserialize, Serialize}; @@ -156,7 +155,8 @@ impl<'de> serde::de::Deserialize<'de> for Response { keys.insert(amount, pubkey); } - // TODO: Should return an error if an amount or key is invalid and not continue + // TODO: Should return an error if an amount or key is + // invalid and not continue } Ok(Response { keys: Keys(keys) }) @@ -172,11 +172,9 @@ pub mod mint { use serde::Serialize; + use super::{PublicKey, SecretKey}; use crate::Amount; - use super::PublicKey; - use super::SecretKey; - #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Keys(pub BTreeMap); diff --git a/crates/cashu/src/nuts/nut02.rs b/crates/cashu/src/nuts/nut02.rs index 1cb41bd6..918800e3 100644 --- a/crates/cashu/src/nuts/nut02.rs +++ b/crates/cashu/src/nuts/nut02.rs @@ -4,9 +4,9 @@ use std::collections::HashSet; use std::fmt; -use base64::{engine::general_purpose, Engine as _}; -use bitcoin::hashes::sha256; -use bitcoin::hashes::Hash; +use base64::engine::general_purpose; +use base64::Engine as _; +use bitcoin::hashes::{sha256, Hash}; use itertools::Itertools; use serde::{Deserialize, Serialize}; @@ -41,10 +41,8 @@ impl Id { const STRLEN: usize = 12; pub fn try_from_base64(b64: &str) -> Result { - use base64::{ - engine::general_purpose::{STANDARD, URL_SAFE}, - Engine as _, - }; + use base64::engine::general_purpose::{STANDARD, URL_SAFE}; + use base64::Engine as _; if b64.len() != Self::STRLEN { return Err(Error::Length); @@ -174,14 +172,12 @@ pub mod mint { use std::collections::BTreeMap; use bitcoin::hashes::sha256::Hash as Sha256; - use bitcoin::hashes::Hash; - use bitcoin::hashes::HashEngine; + use bitcoin::hashes::{Hash, HashEngine}; use itertools::Itertools; use k256::SecretKey; use serde::Serialize; use super::Id; - use crate::nuts::nut01::mint::{KeyPair, Keys}; use crate::Amount; @@ -268,9 +264,8 @@ pub mod mint { #[cfg(test)] mod test { - use crate::nuts::nut02::Id; - use super::Keys; + use crate::nuts::nut02::Id; const SHORT_KEYSET_ID: &str = "esom3oyNLLit"; const SHORT_KEYSET: &str = r#" diff --git a/crates/cashu/src/nuts/nut05.rs b/crates/cashu/src/nuts/nut05.rs index 333f9721..6211fec0 100644 --- a/crates/cashu/src/nuts/nut05.rs +++ b/crates/cashu/src/nuts/nut05.rs @@ -5,8 +5,7 @@ use serde::{Deserialize, Serialize}; use super::nut00::Proofs; use crate::error::Error; -use crate::Amount; -use crate::Bolt11Invoice; +use crate::{Amount, Bolt11Invoice}; /// Check Fees Response [NUT-05] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] diff --git a/crates/cashu/src/nuts/nut06.rs b/crates/cashu/src/nuts/nut06.rs index 6e055099..5ebc2e1b 100644 --- a/crates/cashu/src/nuts/nut06.rs +++ b/crates/cashu/src/nuts/nut06.rs @@ -2,13 +2,11 @@ // https://github.com/cashubtc/nuts/blob/main/06.md use serde::{Deserialize, Serialize}; -use crate::nuts::nut00::{BlindedMessage, Proofs}; -use crate::Amount; - +use super::nut00::BlindedSignature; #[cfg(feature = "wallet")] use crate::nuts::nut00::wallet::BlindedMessages; - -use super::nut00::BlindedSignature; +use crate::nuts::nut00::{BlindedMessage, Proofs}; +use crate::Amount; #[cfg(feature = "wallet")] #[derive(Debug, Clone, PartialEq, Eq, Serialize)] diff --git a/crates/cashu/src/nuts/nut08.rs b/crates/cashu/src/nuts/nut08.rs index eb38be36..b221593e 100644 --- a/crates/cashu/src/nuts/nut08.rs +++ b/crates/cashu/src/nuts/nut08.rs @@ -4,9 +4,9 @@ use lightning_invoice::Bolt11Invoice; use serde::{Deserialize, Serialize}; -use crate::{error::Error, Amount}; - use super::nut00::{BlindedMessage, BlindedSignature, Proofs}; +use crate::error::Error; +use crate::Amount; /// Melt Request [NUT-08] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] diff --git a/crates/cashu/src/secret.rs b/crates/cashu/src/secret.rs index 2370fd6f..dc2a3bdb 100644 --- a/crates/cashu/src/secret.rs +++ b/crates/cashu/src/secret.rs @@ -26,7 +26,8 @@ impl Secret { const BIT_LENGTH: usize = 128; /// Create secret value pub fn new() -> Self { - use base64::{engine::general_purpose::URL_SAFE, Engine as _}; + use base64::engine::general_purpose::URL_SAFE; + use base64::Engine as _; use rand::RngCore; let mut rng = rand::thread_rng(); diff --git a/crates/cashu/src/serde_utils.rs b/crates/cashu/src/serde_utils.rs index ae0dcf40..f546f83d 100644 --- a/crates/cashu/src/serde_utils.rs +++ b/crates/cashu/src/serde_utils.rs @@ -21,7 +21,8 @@ pub mod serde_url { } pub mod bytes_base64 { - use base64::{engine::general_purpose, Engine as _}; + use base64::engine::general_purpose; + use base64::Engine as _; use serde::Deserialize; pub fn serialize(my_bytes: &Vec, serializer: S) -> Result diff --git a/crates/cashu/src/types.rs b/crates/cashu/src/types.rs index 7228d195..dfc74f60 100644 --- a/crates/cashu/src/types.rs +++ b/crates/cashu/src/types.rs @@ -2,10 +2,8 @@ use serde::{Deserialize, Serialize}; -use crate::nuts::{ - nut00::{mint, Proofs}, - nut02::Id, -}; +use crate::nuts::nut00::{mint, Proofs}; +use crate::nuts::nut02::Id; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct ProofsStatus { diff --git a/crates/cashu/src/url.rs b/crates/cashu/src/url.rs index d0998c2e..8a01b1ef 100644 --- a/crates/cashu/src/url.rs +++ b/crates/cashu/src/url.rs @@ -5,6 +5,7 @@ use core::fmt; use core::str::FromStr; + use serde::{Deserialize, Serialize}; use url::{ParseError, Url}; @@ -115,6 +116,7 @@ mod tests { assert_eq!(relay, unchecked_relay_url.to_string()); - // assert_eq!(relay, serde_json::to_string(&unchecked_relay_url).unwrap()) + // assert_eq!(relay, + // serde_json::to_string(&unchecked_relay_url).unwrap()) } } diff --git a/crates/cashu/src/utils.rs b/crates/cashu/src/utils.rs index 33717133..e451526c 100644 --- a/crates/cashu/src/utils.rs +++ b/crates/cashu/src/utils.rs @@ -48,9 +48,9 @@ mod tests { split_amount(Amount::from_sat(3)), vec![Amount::from_sat(1), Amount::from_sat(2)] ); - let amounts: Vec = vec![1, 2, 8].iter().map(|a| Amount::from_sat(*a)).collect(); + let amounts: Vec = [1, 2, 8].iter().map(|a| Amount::from_sat(*a)).collect(); assert_eq!(split_amount(Amount::from_sat(11)), amounts); - let amounts: Vec = vec![1, 2, 4, 8, 16, 32, 64, 128] + let amounts: Vec = [1, 2, 4, 8, 16, 32, 64, 128] .iter() .map(|a| Amount::from_sat(*a)) .collect(); diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..2f38a0c6 --- /dev/null +++ b/flake.lock @@ -0,0 +1,375 @@ +{ + "nodes": { + "android-nixpkgs": { + "inputs": { + "devshell": "devshell", + "flake-utils": "flake-utils_2", + "nixpkgs": [ + "flakebox", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1695500413, + "narHash": "sha256-yinrAWIc4XZbWQoXOYkUO0lCNQ5z/vMyl+QCYuIwdPc=", + "owner": "dpc", + "repo": "android-nixpkgs", + "rev": "2e42268a196375ce9b010a10ec5250d2f91a09b4", + "type": "github" + }, + "original": { + "owner": "dpc", + "repo": "android-nixpkgs", + "rev": "2e42268a196375ce9b010a10ec5250d2f91a09b4", + "type": "github" + } + }, + "crane": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils_3", + "nixpkgs": [ + "flakebox", + "nixpkgs" + ], + "rust-overlay": "rust-overlay" + }, + "locked": { + "lastModified": 1697596235, + "narHash": "sha256-4VTrrTdoA1u1wyf15krZCFl3c29YLesSNioYEgfb2FY=", + "owner": "dpc", + "repo": "crane", + "rev": "c97a0c0d83bfdf01c29113c5592a3defc27cb315", + "type": "github" + }, + "original": { + "owner": "dpc", + "repo": "crane", + "rev": "c97a0c0d83bfdf01c29113c5592a3defc27cb315", + "type": "github" + } + }, + "devshell": { + "inputs": { + "nixpkgs": [ + "flakebox", + "android-nixpkgs", + "nixpkgs" + ], + "systems": "systems_2" + }, + "locked": { + "lastModified": 1695195896, + "narHash": "sha256-pq9q7YsGXnQzJFkR5284TmxrLNFc0wo4NQ/a5E93CQU=", + "owner": "numtide", + "repo": "devshell", + "rev": "05d40d17bf3459606316e3e9ec683b784ff28f16", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "devshell", + "type": "github" + } + }, + "fenix": { + "inputs": { + "nixpkgs": [ + "flakebox", + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1696918968, + "narHash": "sha256-18rAHsM9YsGp7aKKemO4gKIeWfrSyDsdJZ/mk4dQ3JI=", + "owner": "nix-community", + "repo": "fenix", + "rev": "638fc95a2a3d01b372c76f71cbb6d73c63909d6e", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1673956053, + "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_3" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_3": { + "inputs": { + "systems": "systems_4" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_4": { + "inputs": { + "systems": [ + "flakebox", + "systems" + ] + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flakebox": { + "inputs": { + "android-nixpkgs": "android-nixpkgs", + "crane": "crane", + "fenix": "fenix", + "flake-utils": "flake-utils_4", + "nixpkgs": [ + "nixpkgs" + ], + "nixpkgs-unstable": "nixpkgs-unstable", + "systems": "systems_5" + }, + "locked": { + "lastModified": 1697876216, + "narHash": "sha256-Sxf43+wj7bW11yNMstEzn6mbI/Pk9fMSYWFF+swQV5s=", + "owner": "rustshop", + "repo": "flakebox", + "rev": "be3482250c7e7427e86aa8c2523df2cbc478ae5f", + "type": "github" + }, + "original": { + "owner": "rustshop", + "repo": "flakebox", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1697777081, + "narHash": "sha256-n2vQARhKevRGyeo+LAa8g+CdUQsdH/caNk8jnylcPhY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "679cadfdfed2b90311a247b2d6ef6dfd3d6cab73", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-unstable": { + "locked": { + "lastModified": 1697456312, + "narHash": "sha256-roiSnrqb5r+ehnKCauPLugoU8S36KgmWraHgRqVYndo=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "ca012a02bf8327be9e488546faecae5e05d7d749", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "flakebox": "flakebox", + "nixpkgs": "nixpkgs" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1696840854, + "narHash": "sha256-wphOvjDSDsUN5DMe3MOhdargANIab7YE3hkh3Qv7qso=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "aaa1e8e1b82d742b876d164a30dda02f318ce809", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "flakebox", + "crane", + "flake-utils" + ], + "nixpkgs": [ + "flakebox", + "crane", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1695003086, + "narHash": "sha256-d1/ZKuBRpxifmUf7FaedCqhy0lyVbqj44Oc2s+P5bdA=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "b87a14abea512d956f0b89d0d8a1e9b41f3e20ff", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_3": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_4": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_5": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..bce12c53 --- /dev/null +++ b/flake.nix @@ -0,0 +1,25 @@ +{ + description = "A very basic flake"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05"; + + flakebox = { + url = "github:rustshop/flakebox"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flakebox, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + flakeboxLib = flakebox.lib.${system} { }; + in + { + devShells = flakeboxLib.mkShells { + packages = [ ]; + }; + }); +} diff --git a/justfile b/justfile index 3b0598bf..fcc00cc5 100644 --- a/justfile +++ b/justfile @@ -1,20 +1,87 @@ -precommit: - rustup default stable - cargo fmt - cargo check -p cashu - cargo check -p cashu-sdk --no-default-features --features mint - cargo check -p cashu-sdk --no-default-features --features wallet - cargo check -p cashu-sdk --no-default-features --features blocking - typos - cargo test -p cashu - cargo test -p cashu-sdk - cargo clippy --target wasm32-unknown-unknown -p cashu - cargo clippy --target wasm32-unknown-unknown -p cashu-sdk - rustup default 1.70.0 - cargo check -p cashu - cargo check -p cashu-sdk --no-default-features --features mint - cargo check -p cashu-sdk --no-default-features --features wallet - cargo check -p cashu-sdk --no-default-features --features blocking - cargo test -p cashu - cargo test -p cashu-sdk - rustup default stable \ No newline at end of file +# THIS FILE IS AUTOGENERATED FROM FLAKEBOX CONFIGURATION + +alias b := build +alias c := check +alias t := test + + +[private] +default: + @just --list + + +# run `cargo build` on everything +build: + cargo build --workspace --all-targets + + +# run `cargo check` on everything +check: + cargo check --workspace --all-targets + + +# run all checks recommended before opening a PR +final-check: lint clippy + cargo test --doc + just test + + +# run code formatters +format: + cargo fmt --all + nixpkgs-fmt $(echo **.nix) + + +# run lints (git pre-commit hook) +lint: + env NO_STASH=true $(git rev-parse --git-common-dir)/hooks/pre-commit + + +# run tests +test: build + cargo test + + +# run and restart on changes +watch: + env RUST_LOG=${RUST_LOG:-debug} cargo watch -x run + + +# run `cargo clippy` on everything +clippy: + cargo clippy --locked --offline --workspace --all-targets -- --deny warnings --allow deprecated + +# run `cargo clippy --fix` on everything +clippy-fix: + cargo clippy --locked --offline --workspace --all-targets --fix + + +# run `semgrep` +semgrep: + env SEMGREP_ENABLE_VERSION_CHECK=0 \ + semgrep --error --no-rewrite-rule-ids --config .config/semgrep.yaml + + +# check typos +[no-exit-message] +typos *PARAMS: + #!/usr/bin/env bash + set -eo pipefail + + export FLAKEBOX_GIT_LS + FLAKEBOX_GIT_LS="$(git ls-files)" + export FLAKEBOX_GIT_LS_TEXT + FLAKEBOX_GIT_LS_TEXT="$(echo "$FLAKEBOX_GIT_LS" | grep -v -E "^db/|\.(png|ods|jpg|jpeg|woff2|keystore|wasm|ttf|jar|ico)\$")" + + + if ! echo "$FLAKEBOX_GIT_LS_TEXT" | typos {{PARAMS}} --file-list - --force-exclude ; then + >&2 echo "Typos found: Valid new words can be added to '.typos.toml'" + return 1 + fi + +# fix all typos +[no-exit-message] +typos-fix-all: + just typos -w + +# THIS FILE IS AUTOGENERATED FROM FLAKEBOX CONFIGURATION diff --git a/misc/git-hooks/commit-msg b/misc/git-hooks/commit-msg new file mode 100755 index 00000000..b4fe09a0 --- /dev/null +++ b/misc/git-hooks/commit-msg @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +# Sanitize file first, by removing leading lines that are empty or start with a hash, +# as `convco` currently does not do it automatically (but git will) +# TODO: next release of convco should be able to do it automatically +MESSAGE="$( + while read -r line ; do + # skip any initial comments (possibly from previous run) + if [ -z "${body_detected:-}" ] && { [[ "$line" =~ ^#.*$ ]] || [ "$line" == "" ]; }; then + continue + fi + body_detected="true" + + echo "$line" + done < "$1" +)" + +# convco fails on fixup!, so remove fixup! prefix +MESSAGE="${MESSAGE#fixup! }" +if ! convco check --from-stdin <<<"$MESSAGE" ; then + >&2 echo "Please follow conventional commits(https://www.conventionalcommits.org)" + >&2 echo "Use git recommit to fix your commit" + exit 1 +fi diff --git a/misc/git-hooks/commit-template.txt b/misc/git-hooks/commit-template.txt new file mode 100644 index 00000000..a9aff55a --- /dev/null +++ b/misc/git-hooks/commit-template.txt @@ -0,0 +1,2 @@ + +# Explain *why* this change is being made width limit ->| diff --git a/misc/git-hooks/pre-commit b/misc/git-hooks/pre-commit new file mode 100755 index 00000000..9d27b61f --- /dev/null +++ b/misc/git-hooks/pre-commit @@ -0,0 +1,165 @@ +#!/usr/bin/env bash + +set -euo pipefail + +set +e +git diff-files --quiet +is_unclean=$? +set -e + +# Revert `git stash` on exit +function revert_git_stash { + >&2 echo "Unstashing uncommitted changes..." + git stash pop -q +} + +# Stash pending changes and revert them when script ends +if [ -z "${NO_STASH:-}" ] && [ $is_unclean -ne 0 ]; then + >&2 echo "Stashing uncommitted changes..." + GIT_LITERAL_PATHSPECS=0 git stash -q --keep-index + trap revert_git_stash EXIT +fi + +export FLAKEBOX_GIT_LS +FLAKEBOX_GIT_LS="$(git ls-files)" +export FLAKEBOX_GIT_LS_TEXT +FLAKEBOX_GIT_LS_TEXT="$(echo "$FLAKEBOX_GIT_LS" | grep -v -E "\.(png|ods|jpg|jpeg|woff2|keystore|wasm|ttf|jar|ico|gif)\$")" + + +function check_nothing() { + true +} +export -f check_nothing + +function check_cargo_fmt() { + set -euo pipefail + + cargo fmt --all --check + +} +export -f check_cargo_fmt + +function check_cargo_lock() { + set -euo pipefail + + # https://users.rust-lang.org/t/check-if-the-cargo-lock-is-up-to-date-without-building-anything/91048/5 + cargo update --workspace --locked + +} +export -f check_cargo_lock + +function check_leftover_dbg() { + set -euo pipefail + + errors="" + for path in $(echo "$FLAKEBOX_GIT_LS_TEXT" | grep '.*\.rs'); do + if grep 'dbg!(' "$path" > /dev/null; then + >&2 echo "$path contains dbg! macro" + errors="true" + fi + done + + if [ -n "$errors" ]; then + >&2 echo "Fix the problems above or use --no-verify" 1>&2 + return 1 + fi + +} +export -f check_leftover_dbg + +function check_semgrep() { + set -euo pipefail + + # semgrep is not available on MacOS + if ! command -v semgrep > /dev/null ; then + >&2 echo "Skipping semgrep check: not available" + return 0 + fi + + if [ ! -f .config/semgrep.yaml ] ; then + >&2 echo "Skipping semgrep check: .config/semgrep.yaml doesn't exist" + return 0 + fi + + if [ ! -s .config/semgrep.yaml ] ; then + >&2 echo "Skipping semgrep check: .config/semgrep.yaml empty" + return 0 + fi + + env SEMGREP_ENABLE_VERSION_CHECK=0 \ + semgrep -q --error --no-rewrite-rule-ids --config .config/semgrep.yaml + +} +export -f check_semgrep + +function check_shellcheck() { + set -euo pipefail + + for path in $(echo "$FLAKEBOX_GIT_LS_TEXT" | grep -E '.*\.sh$'); do + shellcheck --severity=warning "$path" + done + +} +export -f check_shellcheck + +function check_trailing_newline() { + set -euo pipefail + + errors="" + for path in $(echo "$FLAKEBOX_GIT_LS_TEXT"); do + + # extra branches for clarity + if [ ! -s "$path" ]; then + # echo "$path is empty" + true + elif [ -z "$(tail -c 1 < "$path")" ]; then + # echo "$path ends with a newline or with a null byte" + true + else + >&2 echo "$path doesn't end with a newline" 1>&2 + errors="true" + fi + done + + if [ -n "$errors" ]; then + >&2 echo "Fix the problems above or use --no-verify" 1>&2 + return 1 + fi + +} +export -f check_trailing_newline + +function check_trailing_whitespace() { + set -euo pipefail + + if ! git diff --check HEAD ; then + echo "Trailing whitespace detected. Please remove them before committing." + return 1 + fi + +} +export -f check_trailing_whitespace + +function check_typos() { + set -euo pipefail + + if ! echo "$FLAKEBOX_GIT_LS_TEXT" | typos --file-list - --force-exclude ; then + >&2 echo "Typos found: Valid new words can be added to '.typos.toml'" + return 1 + fi + +} +export -f check_typos + +parallel \ + --nonotice \ +::: \ + check_cargo_fmt \ + check_cargo_lock \ + check_leftover_dbg \ + check_semgrep \ + check_shellcheck \ + check_trailing_newline \ + check_trailing_whitespace \ + check_typos \ + check_nothing