Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into nut-17-ws-subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
crodas committed Nov 5, 2024
2 parents fcae992 + 3db2564 commit 23ca672
Show file tree
Hide file tree
Showing 46 changed files with 542 additions and 242 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ jobs:
-p cdk --no-default-features,
-p cdk --no-default-features --features wallet,
-p cdk --no-default-features --features mint,
-p cdk --no-default-features --features "mint swagger",
-p cdk-axum,
-p cdk-strike,
-p cdk-lnbits,
Expand Down
152 changes: 152 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Development Guide

This guide will help you set up your development environment for working with the CDK repository.

## Prerequisites

Before you begin, ensure you have:
- Git installed on your system
- GitHub account
- Basic familiarity with command line operations

## Initial Setup

### 1. Fork and Clone the Repository

1. Navigate to the CDK repository on GitHub
2. Click the "Fork" button in the top-right corner
3. Clone your forked repository:
```bash
git clone https://github.com/YOUR-USERNAME/cdk.git
cd cdk
```

### 2. Install Nix

<!--
MIT License
Copyright (c) 2021 elsirion
https://github.com/fedimint/fedimint/blob/master/docs/dev-env.md
-->

CDK uses [Nix](https://nixos.org/explore.html) for building, CI, and managing dev environment.
Note: only `Nix` (the language & package manager) and not the NixOS (the Linux distribution) is needed.
Nix can be installed on any Linux distribution and macOS.

While it is technically possible to not use Nix, it is highly recommended as
it ensures consistent and reproducible environment for all developers.

### Install Nix

You have 2 options to install nix:

* **RECOMMENDED:** The [Determinate Nix Installer](https://github.com/DeterminateSystems/nix-installer)
* [The official installer](https://nixos.org/download.html)

Example:

```
> nix --version
nix (Nix) 2.9.1
```

The exact version might be different.

### Enable nix flakes

If you installed Nix using the "determinate installer" you can skip this step. If you used the "official installer", edit either `~/.config/nix/nix.conf` or `/etc/nix/nix.conf` and add:

```
experimental-features = nix-command flakes
```

If the Nix installation is in multi-user mode, don’t forget to restart the nix-daemon.

## Use Nix Shell

```sh
nix develop -c $SHELL
```

## Common Development Tasks

### Building the Project
```sh
just build
```

### Running Unit Tests
```bash
just test
```

### Running Integration Tests
```bash
just itest REDB/SQLITE/MEMEORY
```

### Running Format
```bash
just format
```


### Running Clippy
```bash
just clippy
```

### Running final check before commit
```sh
just final-check
```


## Best Practices

1. **Branch Management**
- Create feature branches from `main`
- Use descriptive branch names: `feature/new-feature` or `fix/bug-description`

2. **Commit Messages**
- Follow conventional commits format
- Begin with type: `feat:`, `fix:`, `docs:`, `chore:`, etc.
- Provide clear, concise descriptions

3. **Testing**
- Write tests for new features
- Ensure all tests pass before submitting PR
- Include integration tests where applicable

## Troubleshooting

### Common Issues

1. **Development Shell Issues**
- Clean Nix store: `nix-collect-garbage -d`
- Remove and recreate development shell

### Getting Help

- Open an issue on GitHub
- Check existing issues for similar problems
- Include relevant error messages and system information
- Reach out in Discord [Invite link](https://discord.gg/tUxMKd5YjN)

## Contributing

1. Create a feature branch
2. Make your changes
3. Run tests and formatting
4. Submit a pull request
5. Wait for review and address feedback

## Additional Resources

- [Nix Documentation](https://nixos.org/manual/nix/stable/)
- [Contributing Guidelines](CONTRIBUTING.md)

## License

Refer to the LICENSE file in the repository for terms of use and distribution.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ The project is split up into several crates in the `crates/` directory:
| [16][16] | Animated QR codes | :x: |
| [17][17] | WebSocket subscriptions | :construction: |

MSRV

## Bindings

Expand All @@ -75,6 +74,9 @@ All contributions welcome.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, shall be licensed as above, without any additional terms or conditions.

Please see the [development guide](DEVELOPMENT.md).


[00]: https://github.com/cashubtc/nuts/blob/main/00.md
[01]: https://github.com/cashubtc/nuts/blob/main/01.md
[02]: https://github.com/cashubtc/nuts/blob/main/02.md
Expand Down
2 changes: 1 addition & 1 deletion bindings/cdk-js/src/types/melt_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl JsMeltQuote {

#[wasm_bindgen(getter)]
pub fn unit(&self) -> JsCurrencyUnit {
self.inner.unit.into()
self.inner.unit.clone().into()
}

#[wasm_bindgen(getter)]
Expand Down
2 changes: 1 addition & 1 deletion bindings/cdk-js/src/types/mint_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl JsMintQuote {

#[wasm_bindgen(getter)]
pub fn unit(&self) -> JsCurrencyUnit {
self.inner.unit.into()
self.inner.unit.clone().into()
}

#[wasm_bindgen(getter)]
Expand Down
1 change: 1 addition & 0 deletions crates/cdk-cli/src/sub_commands/mint_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use cdk::mint_url::MintUrl;
use cdk::wallet::client::HttpClientMethods;
use cdk::HttpClient;
use clap::Args;
use url::Url;
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk-cli/src/sub_commands/pay_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn pay_request(
) -> Result<()> {
let payment_request = &sub_command_args.payment_request;

let unit = payment_request.unit;
let unit = &payment_request.unit;

let amount = match payment_request.amount {
Some(amount) => amount,
Expand Down Expand Up @@ -56,7 +56,7 @@ pub async fn pay_request(
}

if let Some(unit) = unit {
if wallet.unit != unit {
if &wallet.unit != unit {
continue;
}
}
Expand Down Expand Up @@ -97,7 +97,7 @@ pub async fn pay_request(
id: payment_request.payment_id.clone(),
memo: None,
mint: matching_wallet.mint_url.clone(),
unit: matching_wallet.unit,
unit: matching_wallet.unit.clone(),
proofs,
};

Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-cln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ impl MintLightning for Cln {
Settings {
mpp: true,
unit: CurrencyUnit::Msat,
mint_settings: self.mint_settings,
melt_settings: self.melt_settings,
mint_settings: self.mint_settings.clone(),
melt_settings: self.melt_settings.clone(),
invoice_description: true,
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-fake-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ impl MintLightning for FakeWallet {
Settings {
mpp: true,
unit: CurrencyUnit::Msat,
mint_settings: self.mint_settings,
melt_settings: self.melt_settings,
mint_settings: self.mint_settings.clone(),
melt_settings: self.melt_settings.clone(),
invoice_description: true,
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/cdk-integration-tests/src/init_regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ where
Arc::new(database),
ln_backends,
supported_units,
HashMap::new(),
)
.await?;

Expand Down
28 changes: 17 additions & 11 deletions crates/cdk-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use cdk::cdk_lightning::MintLightning;
use cdk::dhke::construct_proofs;
use cdk::mint::FeeReserve;
use cdk::nuts::{
CurrencyUnit, Id, KeySet, MeltMethodSettings, MintInfo, MintMethodSettings, MintQuoteState,
Nuts, PaymentMethod, PreMintSecrets, Proofs, State,
CurrencyUnit, Id, KeySet, MeltMethodSettings, MintBolt11Request, MintInfo, MintMethodSettings,
MintQuoteBolt11Request, MintQuoteState, Nuts, PaymentMethod, PreMintSecrets, Proofs, State,
};
use cdk::types::{LnKey, QuoteTTL};
use cdk::wallet::client::HttpClient;
use cdk::wallet::client::{HttpClient, HttpClientMethods};
use cdk::{Mint, Wallet};
use cdk_fake_wallet::FakeWallet;
use init_regtest::{get_mint_addr, get_mint_port, get_mint_url};
Expand Down Expand Up @@ -82,6 +82,7 @@ pub async fn start_mint(
Arc::new(MintMemoryDatabase::default()),
ln_backends.clone(),
supported_units,
HashMap::new(),
)
.await?;
let cache_time_to_live = 3600;
Expand Down Expand Up @@ -158,8 +159,14 @@ pub async fn mint_proofs(

let wallet_client = HttpClient::new();

let request = MintQuoteBolt11Request {
amount,
unit: CurrencyUnit::Sat,
description,
};

let mint_quote = wallet_client
.post_mint_quote(mint_url.parse()?, 1.into(), CurrencyUnit::Sat, description)
.post_mint_quote(mint_url.parse()?, request)
.await?;

println!("Please pay: {}", mint_quote.request);
Expand All @@ -179,13 +186,12 @@ pub async fn mint_proofs(

let premint_secrets = PreMintSecrets::random(keyset_id, amount, &SplitTarget::default())?;

let mint_response = wallet_client
.post_mint(
mint_url.parse()?,
&mint_quote.quote,
premint_secrets.clone(),
)
.await?;
let request = MintBolt11Request {
quote: mint_quote.quote,
outputs: premint_secrets.blinded_messages(),
};

let mint_response = wallet_client.post_mint(mint_url.parse()?, request).await?;

let pre_swap_proofs = construct_proofs(
mint_response.signatures,
Expand Down
38 changes: 18 additions & 20 deletions crates/cdk-integration-tests/tests/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use bip39::Mnemonic;
use cdk::{
amount::SplitTarget,
cdk_database::WalletMemoryDatabase,
nuts::{CurrencyUnit, MeltQuoteState, PreMintSecrets, State},
wallet::{client::HttpClient, Wallet},
nuts::{CurrencyUnit, MeltBolt11Request, MeltQuoteState, PreMintSecrets, State},
wallet::{
client::{HttpClient, HttpClientMethods},
Wallet,
},
};
use cdk_fake_wallet::{create_fake_invoice, FakeInvoiceDescription};
use cdk_integration_tests::attempt_to_swap_pending;
Expand Down Expand Up @@ -354,28 +357,23 @@ async fn test_fake_melt_change_in_quote() -> Result<()> {

let client = HttpClient::new();

let melt_response = client
.post_melt(
MINT_URL.parse()?,
melt_quote.id.clone(),
proofs.clone(),
Some(premint_secrets.blinded_messages()),
)
.await?;
let melt_request = MeltBolt11Request {
quote: melt_quote.id.clone(),
inputs: proofs.clone(),
outputs: Some(premint_secrets.blinded_messages()),
};

let melt_response = client.post_melt(MINT_URL.parse()?, melt_request).await?;

assert!(melt_response.change.is_some());

let check = wallet.melt_quote_status(&melt_quote.id).await?;
let mut melt_change = melt_response.change.unwrap();
melt_change.sort_by(|a, b| a.amount.cmp(&b.amount));

let mut check = check.change.unwrap();
check.sort_by(|a, b| a.amount.cmp(&b.amount));

assert_eq!(
melt_response
.change
.unwrap()
.sort_by(|a, b| a.amount.cmp(&b.amount)),
check
.change
.unwrap()
.sort_by(|a, b| a.amount.cmp(&b.amount))
);
assert_eq!(melt_change, check);
Ok(())
}
4 changes: 3 additions & 1 deletion crates/cdk-integration-tests/tests/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async fn new_mint(fee: u64) -> Mint {
Arc::new(MintMemoryDatabase::default()),
HashMap::new(),
supported_units,
HashMap::new(),
)
.await
.unwrap()
Expand Down Expand Up @@ -327,7 +328,8 @@ async fn test_swap_unbalanced() -> Result<()> {
async fn test_swap_overpay_underpay_fee() -> Result<()> {
let mint = new_mint(1).await;

mint.rotate_keyset(CurrencyUnit::Sat, 1, 32, 1).await?;
mint.rotate_keyset(CurrencyUnit::Sat, 1, 32, 1, HashMap::new())
.await?;

let keys = mint.pubkeys().await?.keysets.first().unwrap().clone().keys;
let keyset_id = Id::from(&keys);
Expand Down
Loading

0 comments on commit 23ca672

Please sign in to comment.