Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split and cleanup client #9

Merged
merged 44 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
42c3806
splitting up client
f321x Jul 16, 2024
2a3b524
merge
f321x Jul 21, 2024
b6fc274
separate client logic, cleanup
f321x Jul 21, 2024
3b9d16c
remaining changes
f321x Jul 21, 2024
71efb91
add comments
f321x Jul 21, 2024
29aabb1
put trade mode in metadata
f321x Jul 21, 2024
e181c89
minor improvements
f321x Jul 21, 2024
9d6ad91
Create ecash wallet at the parse input phase, the trade parner needs …
rodant Jul 25, 2024
c9bb1cf
Fix npubs by usong to_bech32.
rodant Jul 26, 2024
b7288cf
rewrite in a more idiomatic form.
rodant Jul 31, 2024
64923f7
Move EscrowClient struct to its module.
rodant Aug 2, 2024
236346c
Inline init_ttrade in main.
rodant Aug 2, 2024
6b10695
Extract wallet creation from RawCliInput
rodant Aug 5, 2024
7e28154
Start introducing the escrow client as state machine for better testa…
rodant Aug 6, 2024
0cf6574
Remove dependency from cli in escrow client.
rodant Aug 6, 2024
7cef1c7
Some clean up.
rodant Aug 6, 2024
d32dc48
Move trade mode to the escrow client.
rodant Aug 6, 2024
3ffe54f
Split escrow metadata in the contract and escrow registration parts.
rodant Aug 9, 2024
7e1b21a
Use PublicKey type in trade contract.
rodant Aug 10, 2024
71af4c2
Replace tuple though the registration message struct.
rodant Aug 13, 2024
af5121c
Remove utils modules.
rodant Aug 13, 2024
2a88dbc
Remove PubkeyMessage and rearrange models.
rodant Aug 14, 2024
7ee5b40
Fix remaining warnings.
rodant Aug 14, 2024
a27ea9a
bump up nostr_sdk version and revert to nip04 direct messages, nip17 …
rodant Aug 15, 2024
85ce4d0
working version with nip17 private direct messages.
rodant Aug 16, 2024
a9714b7
Make func sync after review comment.
rodant Aug 18, 2024
13082e7
Introduce timeout and improvements in receive_escrow_message
rodant Aug 20, 2024
542a2be
Better having a get public key method than a get npub.
rodant Aug 20, 2024
f001e9c
Remove dependency to cli in ClientNostrClient.
rodant Aug 20, 2024
5014664
Hide nostr client in ClientNostrInstance.
rodant Aug 20, 2024
9ae1ba4
Improve the coordinator loop for running forever.
rodant Aug 21, 2024
0d7a2a2
Improvement for error situations.
rodant Aug 21, 2024
645158c
Bumb up nostr_sdk version.
rodant Aug 21, 2024
5e20036
Handle some error cases.
rodant Aug 22, 2024
6a0e061
Avoid unneeded async func.
rodant Aug 22, 2024
5aaa656
Remove nostr instance and clean up escrow client.
rodant Aug 22, 2024
3bcfde4
Fix race condition in receiving registration and introduce escrow cli…
rodant Aug 26, 2024
d038b12
remove unused imports.
rodant Aug 26, 2024
8d79e1c
Deposit enough funds and proceed to a delivery.
rodant Aug 30, 2024
09396a7
Fix typos.
rodant Sep 1, 2024
f4b75cb
Update coordinator/src/escrow_coordinator/mod.rs
rodant Sep 1, 2024
e2e4382
Initialize the notifications receiver at creation and pull events on …
rodant Sep 2, 2024
e1d295e
Define nostr client as field of the escrow client and pass all its fi…
rodant Sep 2, 2024
94d2424
Handle receice errors from a subscription.
rodant Sep 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ serde = "1.0.203"
serde_json = "1.0.117"
sha2 = "0.10.8"

cashu_escrow_common = { path = "../common" }
cashu_escrow_common = { path = "../common" }
log = "0.4.22"
env_logger = "0.11.3"
106 changes: 106 additions & 0 deletions client/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
pub mod trade_contract;

use super::*;
use cdk::nuts::nut01::PublicKey as EcashPubkey;
use nostr_sdk::Keys as NostrKeys;
use nostr_sdk::PublicKey as NostrPubkey;
use std::str::FromStr;

#[derive(Debug, Clone)]
pub enum TradeMode {
Buyer,
Seller,
}

#[derive(Debug)]
struct RawCliInput {
buyer_npub: String,
seller_npub: String,
seller_ecash_pubkey: String,
buyer_ecash_pubkey: String,
coordinator_npub: String,
nostr_nsec: String,
mode: TradeMode,
mint_url: String,
}

#[derive(Debug)]
pub struct ClientCliInput {
pub mode: TradeMode,
pub trader_nostr_keys: NostrKeys,
pub ecash_pubkey_buyer: EcashPubkey,
pub ecash_pubkey_seller: EcashPubkey,
pub coordinator_nostr_pubkey: NostrPubkey,
pub trade_partner_nostr_pubkey: NostrPubkey,
pub mint_url: String,
}

impl RawCliInput {
async fn parse() -> anyhow::Result<Self> {
// information would be communicated OOB in production
let buyer_npub: String = env::var("BUYER_NPUB")?;
let seller_npub: String = env::var("SELLER_NPUB")?;
let coordinator_npub: String = env::var("ESCROW_NPUB")?;
let mint_url = env::var("MINT_URL")?;

let mut seller_ecash_pubkey: String = String::new();
let mut buyer_ecash_pubkey: String = String::new();
let nostr_nsec: String;

let mode = match get_user_input("Select mode: (1) buyer, (2) seller: ")
.await?
.as_str()
{
"1" => {
nostr_nsec = env::var("BUYER_NSEC")?;
seller_ecash_pubkey = get_user_input("Enter seller's ecash pubkey: ").await?;
TradeMode::Buyer
}
"2" => {
nostr_nsec = env::var("SELLER_NSEC")?;
buyer_ecash_pubkey = get_user_input("Enter buyer's ecash pubkey: ").await?;
TradeMode::Seller
}
_ => {
panic!("Wrong trading mode selected. Select either (1) buyer or (2) seller");
}
};
Ok(Self {
buyer_npub,
seller_npub,
seller_ecash_pubkey,
buyer_ecash_pubkey,
coordinator_npub,
nostr_nsec,
mode,
mint_url,
})
}
}

impl ClientCliInput {
pub async fn parse() -> anyhow::Result<Self> {
let raw_input = RawCliInput::parse().await?;
debug!("Raw parsed CLI input: {:?}", raw_input);

let ecash_pubkey_buyer = EcashPubkey::from_str(&raw_input.buyer_ecash_pubkey)?;
let ecash_pubkey_seller = EcashPubkey::from_str(&raw_input.seller_ecash_pubkey)?;

let trader_nostr_keys = NostrKeys::from_str(&raw_input.nostr_nsec)?;
let coordinator_nostr_pubkey = NostrPubkey::from_str(&raw_input.coordinator_npub)?;
let trade_partner_nostr_pubkey = match raw_input.mode {
TradeMode::Buyer => NostrPubkey::from_str(&raw_input.seller_npub)?,
TradeMode::Seller => NostrPubkey::from_str(&raw_input.buyer_npub)?,
};

Ok(Self {
mode: raw_input.mode,
trader_nostr_keys,
ecash_pubkey_buyer,
ecash_pubkey_seller,
coordinator_nostr_pubkey,
trade_partner_nostr_pubkey,
mint_url: raw_input.mint_url,
})
}
}
37 changes: 37 additions & 0 deletions client/src/cli/trade_contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use super::*;

pub trait FromClientCliInput {
fn from_client_cli_input(cli_input: &ClientCliInput) -> anyhow::Result<TradeContract>;
}

impl FromClientCliInput for TradeContract {
fn from_client_cli_input(cli_input: &ClientCliInput) -> anyhow::Result<Self> {
debug!("Constructing hard coded client trade contract...");
let npub_seller: String;
let npub_buyer: String;

match cli_input.mode {
TradeMode::Buyer => {
npub_seller = cli_input.trade_partner_nostr_pubkey.to_string();
npub_buyer = cli_input.trader_nostr_keys.public_key().to_bech32()?;
}
TradeMode::Seller => {
npub_buyer = cli_input.trade_partner_nostr_pubkey.to_string();
npub_seller = cli_input.trader_nostr_keys.public_key().to_bech32()?;
}
}

// hardcoded trade contract
Ok(TradeContract {
trade_description:
"Purchase of one Watermelon for 5000 satoshi. 3 days delivery to ...".to_string(),
trade_mint_url: cli_input.mint_url.clone(),
trade_amount_sat: 5000,
npub_seller,
npub_buyer,
time_limit: 3 * 24 * 60 * 60,
seller_ecash_public_key: cli_input.ecash_pubkey_seller.to_string(),
buyer_ecash_public_key: cli_input.ecash_pubkey_buyer.to_string(),
})
}
}
Loading