-
Notifications
You must be signed in to change notification settings - Fork 57
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
BYO network transport when minting tokens #605
Draft
vnprc
wants to merge
3
commits into
cashubtc:main
Choose a base branch
from
vnprc:hashpool2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+419
−6
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -25,6 +25,29 @@ impl Wallet { | |
Ok(keys) | ||
} | ||
|
||
/// Add a keyset to the local database and update keyset info | ||
pub async fn add_keyset( | ||
&self, | ||
keys: Keys, | ||
active: bool, | ||
input_fee_ppk: u64, | ||
) -> Result<(), Error> { | ||
self.localstore.add_keys(keys.clone()).await?; | ||
|
||
let keyset_info = KeySetInfo { | ||
id: Id::from(&keys), | ||
active, | ||
unit: self.unit.clone(), | ||
input_fee_ppk, | ||
}; | ||
|
||
self.localstore | ||
.add_mint_keysets(self.mint_url.clone(), vec![keyset_info]) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Get keysets for mint | ||
/// | ||
/// Queries mint for all keysets | ||
|
@@ -97,4 +120,109 @@ impl Wallet { | |
.ok_or(Error::NoActiveKeyset)?; | ||
Ok(keyset_with_lowest_fee) | ||
} | ||
|
||
/// Get active keyset for mint from local without querying the mint | ||
#[instrument(skip(self))] | ||
pub async fn get_active_mint_keyset_local(&self) -> Result<KeySetInfo, Error> { | ||
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. Needs a better function name that indicates this function doesn't make any network calls. |
||
let active_keysets = match self | ||
.localstore | ||
.get_mint_keysets(self.mint_url.clone()) | ||
.await? | ||
{ | ||
Some(keysets) => keysets | ||
.into_iter() | ||
.filter(|k| k.active && k.unit == self.unit) | ||
.collect::<Vec<KeySetInfo>>(), | ||
None => { | ||
vec![] | ||
} | ||
}; | ||
|
||
let keyset_with_lowest_fee = active_keysets | ||
.into_iter() | ||
.min_by_key(|key| key.input_fee_ppk) | ||
.ok_or(Error::NoActiveKeyset)?; | ||
|
||
Ok(keyset_with_lowest_fee) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::cdk_database::WalletMemoryDatabase; | ||
use crate::nuts; | ||
use crate::Wallet; | ||
use bitcoin::bip32::DerivationPath; | ||
use bitcoin::bip32::Xpriv; | ||
use bitcoin::key::Secp256k1; | ||
use cdk_common::KeySet; | ||
use cdk_common::KeySetInfo; | ||
use cdk_common::MintKeySet; | ||
use nuts::CurrencyUnit; | ||
use std::sync::Arc; | ||
|
||
fn create_new_keyset() -> (KeySet, KeySetInfo) { | ||
let secp = Secp256k1::new(); | ||
let seed = [0u8; 32]; // Default seed for testing | ||
let xpriv = Xpriv::new_master(bitcoin::Network::Bitcoin, &seed).expect("RNG busted"); | ||
|
||
let derivation_path = DerivationPath::default(); | ||
let unit = CurrencyUnit::Custom("HASH".to_string()); | ||
let max_order = 64; | ||
|
||
let keyset: KeySet = MintKeySet::generate( | ||
&secp, | ||
xpriv | ||
.derive_priv(&secp, &derivation_path) | ||
.expect("RNG busted"), | ||
unit.clone(), | ||
max_order, | ||
) | ||
.into(); | ||
|
||
let keyset_info = KeySetInfo { | ||
id: keyset.id, | ||
unit: keyset.unit.clone(), | ||
active: true, | ||
input_fee_ppk: 0, | ||
}; | ||
|
||
(keyset, keyset_info) | ||
} | ||
|
||
fn create_wallet() -> Wallet { | ||
use rand::Rng; | ||
|
||
let seed = rand::thread_rng().gen::<[u8; 32]>(); | ||
let mint_url = "https://testnut.cashu.space"; | ||
|
||
let localstore = WalletMemoryDatabase::default(); | ||
Wallet::new( | ||
mint_url, | ||
CurrencyUnit::Custom("HASH".to_string()), | ||
Arc::new(localstore), | ||
&seed, | ||
None, | ||
) | ||
.unwrap() | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_add_and_get_active_mint_keysets_local() { | ||
let (keyset, keyset_info) = create_new_keyset(); | ||
|
||
let wallet = create_wallet(); | ||
|
||
// Add the keyset | ||
wallet.add_keyset(keyset.keys, true, 0).await.unwrap(); | ||
|
||
// Retrieve the keysets locally | ||
let active_keyset = wallet.get_active_mint_keyset_local().await.unwrap(); | ||
|
||
// Validate the retrieved keyset | ||
assert_eq!(active_keyset.id, keyset_info.id); | ||
assert_eq!(active_keyset.active, keyset_info.active); | ||
assert_eq!(active_keyset.unit, keyset_info.unit); | ||
assert_eq!(active_keyset.input_fee_ppk, keyset_info.input_fee_ppk); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why String for the HashMap key here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I don't know if this is necessary. It would be great if I could get rid of the string entirely.
It works this way because I copied some code from somewhere else and it used a string as the index. This code comment seems to indicate using a string helps with sorting these items in a BTreeMap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're planning to remove this memory db so i wouldn't worry about this.
#607
AmountStr is used because the amount in the keyset response is a string not an int. https://github.com/cashubtc/nuts/blob/main/02.md#requesting-public-keys-for-a-specific-keyset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use AmountStr in hashpool in the same way. Is this the recommended way to put pubkeys in a BTreeMap?
https://github.com/vnprc/hashpool/blob/master/protocols/v2/subprotocols/mining/src/cashu.rs#L270
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just use the
Amount
there no need to wrap it in theAmoutnStr
. The reason we have to useAmountStr
is for when the Keys response is json serialized it needs to be as a string and not an int, so that's really the only place we should useAmountStr
everywhere else isAmount
.EDIT: I see you're creating a
Keys
which currently does expect theAmountStr
cdk/crates/cashu/src/nuts/nut01/mod.rs
Line 63 in e1458b0
AmountStr
type?cc @crodas