-
Notifications
You must be signed in to change notification settings - Fork 140
P2pk receive wallet #1466
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
P2pk receive wallet #1466
Changes from 8 commits
6d840d5
8dfa437
4e2a515
709ab36
d390d56
e054296
95c6baa
cb9f89a
2361f07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| use std::str::FromStr; | ||
|
|
||
| use anyhow::Result; | ||
| use cdk::mint_url::MintUrl; | ||
| use cdk::nuts::CurrencyUnit; | ||
| use cdk::wallet::WalletRepository; | ||
| use clap::Args; | ||
|
|
||
| use crate::utils::get_or_create_wallet; | ||
|
|
||
| #[derive(Args)] | ||
| pub struct GeneratePublicKeySubCommand { | ||
| /// Mint URL to select wallet context | ||
| #[arg(long)] | ||
| mint_url: Option<String>, | ||
| } | ||
|
|
||
| pub async fn generate_public_key( | ||
| wallet_repository: &WalletRepository, | ||
| sub_command_args: &GeneratePublicKeySubCommand, | ||
| unit: &CurrencyUnit, | ||
| ) -> Result<()> { | ||
| let mint_url = match &sub_command_args.mint_url { | ||
| Some(url) => MintUrl::from_str(url)?, | ||
| None => { | ||
| let wallets = wallet_repository.get_wallets().await; | ||
| wallets | ||
| .iter() | ||
| .find(|wallet| &wallet.unit == unit) | ||
| .map(|wallet| wallet.mint_url.clone()) | ||
| .ok_or_else(|| { | ||
| anyhow::anyhow!("No wallet found for unit {}. Use --mint-url.", unit) | ||
| })? | ||
| } | ||
| }; | ||
|
|
||
| let wallet = get_or_create_wallet(wallet_repository, &mint_url, unit).await?; | ||
| let public_key = wallet.generate_public_key().await?; | ||
|
|
||
| println!("\npublic key generated!\n"); | ||
| println!("public key: {}", public_key.to_hex()); | ||
|
|
||
| Ok(()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| use std::str::FromStr; | ||
|
|
||
| use anyhow::Result; | ||
| use cdk::mint_url::MintUrl; | ||
| use cdk::nuts::CurrencyUnit; | ||
| use cdk::wallet::WalletRepository; | ||
| use clap::Args; | ||
|
|
||
| use crate::utils::get_or_create_wallet; | ||
|
|
||
| #[derive(Args)] | ||
| pub struct GetPublicKeysSubCommand { | ||
| /// Show the latest public key | ||
| #[arg(long)] | ||
| pub latest: bool, | ||
| /// Mint URL to select wallet context | ||
| #[arg(long)] | ||
| mint_url: Option<String>, | ||
| } | ||
|
|
||
| pub async fn get_public_keys( | ||
| wallet_repository: &WalletRepository, | ||
| sub_command_args: &GetPublicKeysSubCommand, | ||
| unit: &CurrencyUnit, | ||
| ) -> Result<()> { | ||
| let mint_url = match &sub_command_args.mint_url { | ||
| Some(url) => MintUrl::from_str(url)?, | ||
| None => { | ||
| let wallets = wallet_repository.get_wallets().await; | ||
| wallets | ||
| .iter() | ||
| .find(|wallet| &wallet.unit == unit) | ||
| .map(|wallet| wallet.mint_url.clone()) | ||
| .ok_or_else(|| { | ||
| anyhow::anyhow!("No wallet found for unit {}. Use --mint-url.", unit) | ||
| })? | ||
| } | ||
| }; | ||
|
|
||
| let wallet = get_or_create_wallet(wallet_repository, &mint_url, unit).await?; | ||
|
|
||
| if sub_command_args.latest { | ||
| let latest_public_key = wallet.get_latest_public_key().await?; | ||
|
|
||
| match latest_public_key { | ||
| Some(key) => { | ||
| println!("\npublic key found!\n"); | ||
|
|
||
| println!("public key: {}", key.pubkey.to_hex()); | ||
| println!("derivation path: {}", key.derivation_path); | ||
| } | ||
| None => { | ||
| println!("\npublic key not found!\n"); | ||
| } | ||
| } | ||
|
|
||
| return Ok(()); | ||
| } | ||
|
|
||
| let list_public_keys = wallet.get_public_keys().await?; | ||
| if list_public_keys.is_empty() { | ||
| println!("\npublic not found!\n"); | ||
| } | ||
| println!("\npublic keys found:\n"); | ||
| for public_key in list_public_keys { | ||
|
lescuer97 marked this conversation as resolved.
|
||
| println!("public key: {}", public_key.pubkey.to_hex()); | ||
| println!("derivation path: {}", public_key.derivation_path); | ||
| } | ||
| Ok(()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ use std::fmt; | |
| use std::str::FromStr; | ||
|
|
||
| use async_trait::async_trait; | ||
| use bitcoin::bip32::DerivationPath; | ||
| use bitcoin::hashes::{sha256, Hash, HashEngine}; | ||
| use cashu::amount::SplitTarget; | ||
| use cashu::nuts::nut07::ProofState; | ||
|
|
@@ -950,6 +951,38 @@ pub trait Wallet: Send + Sync { | |
| /// The `Spent` state is typically excluded since spent proofs are removed | ||
| /// from the database. | ||
| async fn get_proofs_by_states(&self, states: Vec<State>) -> Result<Proofs, Self::Error>; | ||
|
|
||
| // P2PK proofs | ||
| /// generates and stores public key in database | ||
| async fn generate_public_key(&self) -> Result<PublicKey, Self::Error>; | ||
|
|
||
| /// gets public key by it's hex value | ||
| async fn get_public_key( | ||
| &self, | ||
| pubkey: &PublicKey, | ||
| ) -> Result<Option<P2PKSigningKey>, Self::Error>; | ||
|
|
||
| /// gets list of stored public keys in database | ||
| async fn get_public_keys(&self) -> Result<Vec<P2PKSigningKey>, Self::Error>; | ||
|
|
||
| /// Gets the latest generated P2PK signing key (most recently created) | ||
| async fn get_latest_public_key(&self) -> Result<Option<P2PKSigningKey>, Self::Error>; | ||
|
|
||
| /// try to get secret key from p2pk signing key in localstore | ||
| async fn get_signing_key(&self, pubkey: &PublicKey) -> Result<Option<SecretKey>, Self::Error>; | ||
|
Comment on lines
+955
to
+972
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On second thought. And thinking about how we want to reduce our public api surface, maybe only generate and get latest should be in the public api and the other fns should be on the wallet as pub(crate) this way they can be removed from the trait and ffi?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the 2 that we could remove from the public are |
||
| } | ||
|
|
||
| /// Public key generated for proof signing | ||
| #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] | ||
| pub struct P2PKSigningKey { | ||
| /// Public key | ||
| pub pubkey: PublicKey, | ||
| /// Derivation path | ||
| pub derivation_path: DerivationPath, | ||
| /// Derivation index | ||
| pub derivation_index: u32, | ||
| /// Created time | ||
| pub created_time: u64, | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.