-
Notifications
You must be signed in to change notification settings - Fork 48
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
Fix None
derivation path index for custom currencies
#464
Draft
vnprc
wants to merge
3
commits into
cashubtc:main
Choose a base branch
from
vnprc:custom_currency
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.
Draft
Changes from 2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -374,45 +374,69 @@ pub enum CurrencyUnit { | |
/// Euro | ||
Eur, | ||
/// Custom currency unit | ||
Custom(String), | ||
Custom(String, u32), | ||
} | ||
|
||
impl CurrencyUnit { | ||
/// Constructor for `CurrencyUnit::Custom` | ||
pub fn new_custom(name: String, index: u32) -> Result<Self, String> { | ||
if (0..=3).contains(&index) { | ||
Err(format!( | ||
"Index {} is reserved and cannot be used for custom currency units.", | ||
index | ||
)) | ||
} else { | ||
Ok(Self::Custom(name, index)) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "mint")] | ||
impl CurrencyUnit { | ||
/// Derivation index mint will use for unit | ||
pub fn derivation_index(&self) -> Option<u32> { | ||
pub fn derivation_index(&self) -> u32 { | ||
match self { | ||
Self::Sat => Some(0), | ||
Self::Msat => Some(1), | ||
Self::Usd => Some(2), | ||
Self::Eur => Some(3), | ||
_ => None, | ||
Self::Sat => 0, | ||
Self::Msat => 1, | ||
Self::Usd => 2, | ||
Self::Eur => 3, | ||
Self::Custom(_, index) => *index, | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for CurrencyUnit { | ||
type Err = Error; | ||
fn from_str(value: &str) -> Result<Self, Self::Err> { | ||
let value = &value.to_uppercase(); | ||
match value.as_str() { | ||
// Split on ':' to check for derivation index | ||
let parts: Vec<&str> = value.split(':').collect(); | ||
let currency = parts[0].to_uppercase(); | ||
|
||
match currency.as_str() { | ||
Comment on lines
+415
to
+419
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. We can't assume that we get the unit in this form it is not in the spec. |
||
"SAT" => Ok(Self::Sat), | ||
"MSAT" => Ok(Self::Msat), | ||
"USD" => Ok(Self::Usd), | ||
"EUR" => Ok(Self::Eur), | ||
c => Ok(Self::Custom(c.to_string())), | ||
c => { | ||
// Require explicit index for custom currencies | ||
if parts.len() != 2 { | ||
return Err(Error::UnsupportedUnit); | ||
} | ||
let index = parts[1].parse().map_err(|_| Error::UnsupportedUnit)?; | ||
Ok(Self::Custom(c.to_string(), index)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for CurrencyUnit { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let s = match self { | ||
CurrencyUnit::Sat => "SAT", | ||
CurrencyUnit::Msat => "MSAT", | ||
CurrencyUnit::Usd => "USD", | ||
CurrencyUnit::Eur => "EUR", | ||
CurrencyUnit::Custom(unit) => unit, | ||
CurrencyUnit::Sat => "SAT".to_string(), | ||
CurrencyUnit::Msat => "MSAT".to_string(), | ||
CurrencyUnit::Usd => "USD".to_string(), | ||
CurrencyUnit::Eur => "EUR".to_string(), | ||
CurrencyUnit::Custom(unit, index) => format!("{}:{}", unit, index), | ||
}; | ||
if let Some(width) = f.width() { | ||
write!(f, "{:width$}", s.to_lowercase(), width = width) | ||
|
@@ -763,4 +787,68 @@ mod tests { | |
.unwrap(); | ||
assert_eq!(b.len(), 1); | ||
} | ||
|
||
#[test] | ||
fn test_currency_unit_from_str() { | ||
// Standard currencies | ||
assert_eq!(CurrencyUnit::from_str("SAT").unwrap(), CurrencyUnit::Sat); | ||
assert_eq!(CurrencyUnit::from_str("sat").unwrap(), CurrencyUnit::Sat); | ||
assert_eq!(CurrencyUnit::from_str("MSAT").unwrap(), CurrencyUnit::Msat); | ||
assert_eq!(CurrencyUnit::from_str("msat").unwrap(), CurrencyUnit::Msat); | ||
assert_eq!(CurrencyUnit::from_str("USD").unwrap(), CurrencyUnit::Usd); | ||
assert_eq!(CurrencyUnit::from_str("usd").unwrap(), CurrencyUnit::Usd); | ||
assert_eq!(CurrencyUnit::from_str("EUR").unwrap(), CurrencyUnit::Eur); | ||
assert_eq!(CurrencyUnit::from_str("eur").unwrap(), CurrencyUnit::Eur); | ||
|
||
// Custom currency | ||
assert_eq!( | ||
CurrencyUnit::from_str("GBP:1001").unwrap(), | ||
CurrencyUnit::Custom("GBP".to_string(), 1001) | ||
); | ||
|
||
assert!(CurrencyUnit::from_str("GBP").is_err()); | ||
assert!(CurrencyUnit::from_str("GBP:").is_err()); | ||
assert!(CurrencyUnit::from_str("GBP:abc").is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_currency_unit_display() { | ||
// Standard currencies | ||
assert_eq!(CurrencyUnit::Sat.to_string(), "sat"); | ||
assert_eq!(CurrencyUnit::Msat.to_string(), "msat"); | ||
assert_eq!(CurrencyUnit::Usd.to_string(), "usd"); | ||
assert_eq!(CurrencyUnit::Eur.to_string(), "eur"); | ||
|
||
// Custom currency | ||
assert_eq!( | ||
CurrencyUnit::Custom("GBP".to_string(), 1001).to_string(), | ||
"gbp:1001" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_custom_currency_valid_index() { | ||
let valid_index = 5; | ||
let result = CurrencyUnit::new_custom("MyCurrency".to_string(), valid_index); | ||
assert!(result.is_ok()); | ||
if let Ok(CurrencyUnit::Custom(name, index)) = result { | ||
assert_eq!(name, "MyCurrency"); | ||
assert_eq!(index, valid_index); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_custom_currency_invalid_indexes() { | ||
let invalid_indexes = [0, 1, 2, 3]; | ||
for &index in &invalid_indexes { | ||
let result = CurrencyUnit::new_custom("InvalidCurrency".to_string(), index); | ||
assert!(result.is_err(), "Index {} should not be allowed", index); | ||
if let Err(err) = result { | ||
assert!( | ||
err.contains(&index.to_string()), | ||
"Error message should mention the invalid index" | ||
); | ||
} | ||
} | ||
} | ||
} |
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.
This would lead to the derivation path being included in the token I believe.
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.
Do you mean in the ecash token? Is this a problem?
edit: guessing it's a problem because it's not in the spec so other mints using other implementations will not know how to handle these tokens
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.
Since the serialize/deserialize implementation just use to/from string internally and this modifies those to include or expect the derivation path when the token (cashuB...) get serialized/desrialized it would include or expect the path.