-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from f321x/splitting_structures
LGTM
- Loading branch information
Showing
16 changed files
with
742 additions
and
450 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,9 @@ members = [ | |
"client", | ||
"coordinator", | ||
"common", | ||
] | ||
] | ||
|
||
[profile.release] | ||
lto = true | ||
opt-level = 3 | ||
strip = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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)] | ||
struct RawCliInput { | ||
buyer_npub: String, | ||
seller_npub: String, | ||
partner_ecash_pubkey: String, | ||
coordinator_npub: String, | ||
nostr_nsec: String, | ||
mode: TradeMode, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ClientCliInput { | ||
pub mode: TradeMode, | ||
pub trader_nostr_keys: NostrKeys, | ||
pub ecash_pubkey_partner: EcashPubkey, | ||
pub coordinator_nostr_pubkey: NostrPubkey, | ||
pub trade_partner_nostr_pubkey: NostrPubkey, | ||
} | ||
|
||
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 partner_ecash_pubkey: String; | ||
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")?; | ||
partner_ecash_pubkey = get_user_input("Enter seller's ecash pubkey: ").await?; | ||
TradeMode::Buyer | ||
} | ||
"2" => { | ||
nostr_nsec = env::var("SELLER_NSEC")?; | ||
partner_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, | ||
partner_ecash_pubkey, | ||
coordinator_npub, | ||
nostr_nsec, | ||
mode, | ||
}) | ||
} | ||
} | ||
|
||
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_partner = EcashPubkey::from_str(&raw_input.partner_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_bech32(&raw_input.seller_npub)?, | ||
TradeMode::Seller => NostrPubkey::from_bech32(&raw_input.buyer_npub)?, | ||
}; | ||
|
||
Ok(Self { | ||
mode: raw_input.mode, | ||
trader_nostr_keys, | ||
ecash_pubkey_partner, | ||
coordinator_nostr_pubkey, | ||
trade_partner_nostr_pubkey, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use super::*; | ||
|
||
pub trait FromClientCliInput { | ||
fn from_client_cli_input( | ||
cli_input: &ClientCliInput, | ||
trade_pubkey: String, | ||
) -> anyhow::Result<TradeContract>; | ||
} | ||
|
||
impl FromClientCliInput for TradeContract { | ||
fn from_client_cli_input( | ||
cli_input: &ClientCliInput, | ||
trade_pubkey: String, | ||
) -> anyhow::Result<Self> { | ||
debug!("Constructing hard coded client trade contract..."); | ||
let npubkey_seller: PublicKey; | ||
let npubkey_buyer: PublicKey; | ||
|
||
match cli_input.mode { | ||
TradeMode::Buyer => { | ||
npubkey_seller = cli_input.trade_partner_nostr_pubkey; | ||
npubkey_buyer = cli_input.trader_nostr_keys.public_key(); | ||
} | ||
TradeMode::Seller => { | ||
npubkey_buyer = cli_input.trade_partner_nostr_pubkey; | ||
npubkey_seller = cli_input.trader_nostr_keys.public_key(); | ||
} | ||
} | ||
|
||
let (ecash_pubkey_seller, ecash_pubkey_buyer) = match cli_input.mode { | ||
TradeMode::Seller => (trade_pubkey, cli_input.ecash_pubkey_partner.to_string()), | ||
TradeMode::Buyer => (cli_input.ecash_pubkey_partner.to_string(), trade_pubkey), | ||
}; | ||
// hardcoded trade contract | ||
Ok(TradeContract { | ||
trade_description: | ||
"Purchase of one Watermelon for 5000 satoshi. 3 days delivery to ...".to_string(), | ||
trade_amount_sat: 5000, | ||
npubkey_seller, | ||
npubkey_buyer, | ||
npubkey_coordinator: cli_input.coordinator_nostr_pubkey, | ||
time_limit: 3 * 24 * 60 * 60, | ||
seller_ecash_public_key: ecash_pubkey_seller, | ||
buyer_ecash_public_key: ecash_pubkey_buyer, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.