Skip to content

Commit

Permalink
Parse phone number (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-21 authored May 16, 2024
1 parent e9e4b10 commit 8db2c14
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml.breez.sample
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ POCKET_URL_DEV = ""
POCKET_URL_STAGE = ""
POCKET_URL_PROD = ""

LIPA_LIGHTNING_DOMAIN_LOCAL = ""
LIPA_LIGHTNING_DOMAIN_DEV = ""
LIPA_LIGHTNING_DOMAIN_STAGE = ""
LIPA_LIGHTNING_DOMAIN_PROD = ""

BREEZ_SDK_API_KEY = ""
BREEZ_SDK_PARTNER_CERTIFICATE = ""
BREEZ_SDK_PARTNER_KEY = ""
Expand Down
81 changes: 77 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ hex = "0.4.3"
iban_validate = "4.0.1"
log = "0.4.21"
num_enum = "0.7.2"
phonenumber = "0.3.5"
rand = "0.8.5"
regex = { version = "1.10.4" }
reqwest = { version = "0.11", default-features = false, features = ["json", "blocking", "rustls-tls"] }
Expand Down
37 changes: 29 additions & 8 deletions examples/node/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use uniffi_lipalightninglib::{
ExchangeRate, FailedSwapInfo, FiatValue, IncomingPaymentInfo, InvoiceCreationMetadata,
InvoiceDetails, LightningNode, LiquidityLimit, LnUrlPayDetails, LnUrlWithdrawDetails,
MaxRoutingFeeMode, OfferInfo, OfferKind, OutgoingPaymentInfo, PaymentInfo, PaymentMetadata,
TzConfig,
Recipient, TzConfig,
};

pub(crate) fn poll_for_user_input(node: &LightningNode, log_file_path: &str) {
Expand Down Expand Up @@ -104,6 +104,15 @@ pub(crate) fn poll_for_user_input(node: &LightningNode, log_file_path: &str) {
println!("{}", format!("{message:#}").red());
}
}
"parsephonenumber" => {
let number = words.collect::<Vec<_>>().join(" ");
let allowed_countries =
vec!["AT".to_string(), "CH".to_string(), "DE".to_string()];
match node.parse_phone_number(number, allowed_countries) {
Ok(address) => println!("{address}"),
Err(message) => println!("{}", format!("{message:#}").red()),
}
}
"getmaxroutingfeemode" => {
if let Err(message) = get_max_routing_fee_mode(node, &mut words) {
println!("{}", format!("{message:#}").red());
Expand Down Expand Up @@ -194,8 +203,18 @@ pub(crate) fn poll_for_user_input(node: &LightningNode, log_file_path: &str) {
println!("{}", format!("{message:#}").red());
}
}
"listlightningaddresses" => match node.list_lightning_addresses() {
Ok(list) => println!("{}", list.join("\n")),
"listrecipients" => match node.list_recipients() {
Ok(list) => {
let list = list
.into_iter()
.map(|r| match r {
Recipient::LightningAddress { address } => address,
Recipient::PhoneNumber { e164 } => e164,
r => panic!("{r:?}"),
})
.collect::<Vec<_>>();
println!("{}", list.join("\n"));
}
Err(message) => eprintln!("{}", format!("{message:#}").red()),
},
"paymentuuid" => match payment_uuid(node, &mut words) {
Expand Down Expand Up @@ -322,6 +341,10 @@ fn setup_editor(history_path: &Path) -> Editor<CommandHinter, DefaultHistory> {
));
hints.insert(CommandHint::new("d <data>", "d "));
hints.insert(CommandHint::new("decodedata <data>", "decodedata "));
hints.insert(CommandHint::new(
"parsephonenumber <phone number>",
"parsephonenumber ",
));
hints.insert(CommandHint::new(
"getmaxroutingfeemode <payment amount in SAT>",
"getmaxroutingfeemode ",
Expand Down Expand Up @@ -382,10 +405,7 @@ fn setup_editor(history_path: &Path) -> Editor<CommandHinter, DefaultHistory> {
"listactivities [number of activities = 2]",
"listactivities ",
));
hints.insert(CommandHint::new(
"listlightningaddresses",
"listlightningaddresses",
));
hints.insert(CommandHint::new("listrecipients", "listrecipients"));
hints.insert(CommandHint::new(
"registerlightningaddress",
"registerlightningaddress",
Expand Down Expand Up @@ -442,6 +462,7 @@ fn help() {
println!();
println!(" i | invoice <amount in SAT> [description]");
println!(" d | decodedata <data>");
println!(" parsephonenumber <phone number>");
println!(" getmaxroutingfeemode <payment amount in SAT>");
println!(" getinvoiceaffordability <amount in SAT>");
println!(" p | payinvoice <invoice>");
Expand All @@ -463,7 +484,7 @@ fn help() {
println!();
println!(" o | overview [number of activities = 10] [fun mode = false]");
println!(" l | listactivities [number of activities = 2]");
println!(" listlightningaddresses");
println!(" listrecipients");
println!(" registerlightningaddress");
println!(" querylightningaddress");
println!(" paymentuuid <payment hash>");
Expand Down
1 change: 1 addition & 0 deletions examples/node/overview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ fn print_outgoing_payment(payment: OutgoingPaymentInfo) -> Result<()> {
let (icon, title) = match payment.recipient {
Recipient::LightningAddress { address } => (" @".bold(), address),
Recipient::LnUrlPayDomain { domain } => ("🌐".normal(), domain),
Recipient::PhoneNumber { e164 } => ("📞".normal(), e164),
Recipient::Unknown => ("🧾".normal(), "Invoice".to_string()),
};

Expand Down
15 changes: 15 additions & 0 deletions src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(crate) struct Environment {
pub pocket_url: String,
pub notification_webhook_base_url: String,
pub notification_webhook_secret: [u8; 32],
pub lipa_lightning_domain: String,
}

impl Environment {
Expand All @@ -33,6 +34,7 @@ impl Environment {
let notification_webhook_base_url =
get_notification_webhook_base_url(environment).to_string();
let notification_webhook_secret = get_notification_webhook_secret(environment)?;
let lipa_lightning_domain = get_lipa_lightning_domain(environment).to_string();

Ok(match environment {
EnvironmentCode::Local => Self {
Expand All @@ -43,6 +45,7 @@ impl Environment {
pocket_url: env!("POCKET_URL_LOCAL").to_string(),
notification_webhook_base_url,
notification_webhook_secret,
lipa_lightning_domain,
},
EnvironmentCode::Dev => Self {
network: Network::Bitcoin,
Expand All @@ -52,6 +55,7 @@ impl Environment {
pocket_url: env!("POCKET_URL_DEV").to_string(),
notification_webhook_base_url,
notification_webhook_secret,
lipa_lightning_domain,
},
EnvironmentCode::Stage => Self {
network: Network::Bitcoin,
Expand All @@ -61,6 +65,7 @@ impl Environment {
pocket_url: env!("POCKET_URL_STAGE").to_string(),
notification_webhook_base_url,
notification_webhook_secret,
lipa_lightning_domain,
},
EnvironmentCode::Prod => Self {
network: Network::Bitcoin,
Expand All @@ -70,6 +75,7 @@ impl Environment {
pocket_url: env!("POCKET_URL_PROD").to_string(),
notification_webhook_base_url,
notification_webhook_secret,
lipa_lightning_domain,
},
})
}
Expand Down Expand Up @@ -104,3 +110,12 @@ fn get_notification_webhook_secret(environment_code: EnvironmentCode) -> Result<
<[u8; 32]>::from_hex(secret_hex)
.map_to_permanent_failure("Failed to parse embedded notification webhook secret")
}

fn get_lipa_lightning_domain(environment_code: EnvironmentCode) -> &'static str {
match environment_code {
EnvironmentCode::Local => env!("LIPA_LIGHTNING_DOMAIN_LOCAL"),
EnvironmentCode::Dev => env!("LIPA_LIGHTNING_DOMAIN_DEV"),
EnvironmentCode::Stage => env!("LIPA_LIGHTNING_DOMAIN_STAGE"),
EnvironmentCode::Prod => env!("LIPA_LIGHTNING_DOMAIN_PROD"),
}
}
14 changes: 14 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,17 @@ impl NotificationHandlingErrorCode {
Self::NodeUnavailable
}
}

#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum ParsePhoneNumberError {
#[error("ParsingError")]
ParsingError,
#[error("MissingCountryCode")]
MissingCountryCode,
#[error("InvalidCountryCode")]
InvalidCountryCode,
#[error("InvalidPhoneNumber")]
InvalidPhoneNumber,
#[error("UnsupportedCountry")]
UnsupportedCountry,
}
Loading

0 comments on commit 8db2c14

Please sign in to comment.