Skip to content
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

plugin: Fix parsing of str or int amount_msat #457

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions libs/gl-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,19 @@ fn _parse_gl_config_from_serialized_request(request: String) -> Option<pb::GlCon
}
}


/// Notification handler that receives notifications on incoming
/// payments, then looks up the invoice in the DB, and forwards the
/// full information to the GRPC interface.
async fn on_invoice_payment(plugin: Plugin, v: serde_json::Value) -> Result<serde_json::Value> {
debug!("Got an incoming payment via invoice_payment: {:?}", v);
log::info!("Got an incoming payment via invoice_payment: {:?}", v);
let state = plugin.state();
let call: messages::InvoicePaymentCall = serde_json::from_value(v).unwrap();
let call: messages::InvoicePaymentCall = match serde_json::from_value(v) {
Ok(v) => v,
Err(e) => {
log::error!("Could not decode the invoice_payment_call: {e}");
return Ok(json!({"result": "continue"}));
}
};

let rpc = state.rpc.lock().await.clone();
let req = requests::ListInvoices {
Expand Down
31 changes: 27 additions & 4 deletions libs/gl-plugin/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,24 @@ where
where
E: de::Error,
{
eprintln!("XXX {}", v);
// unfortunately we lose some typed information
// from errors deserializing the json string
serde_json::from_str(v).map_err(E::custom)
let (nums, exts): (Vec<char>, Vec<char>) = v.chars().partition(|c| c.is_digit(10));

let mut num = String::new();
num.extend(nums);
let mut ext = String::new();
ext.extend(exts);

// Conversion table from the unit to `msat`, since msat is
// the unit of account internally for off-chain payments.
let mult = match ext.as_str() {
"msat" => 1,
"sat" => 1000,
"btc" => 100_000_000_000,
_ => return Err(E::custom("unable to parse unit")),
};

let num: u64 = num.parse::<u64>().expect("converting chars to u64");
Ok(num * mult)
}
}

Expand Down Expand Up @@ -412,4 +426,13 @@ mod test {
_ => panic!("This was supposed to be a request"),
}
}

/// We have a bit of trouble parsing some invoice payment hook
/// calls in 2024/06/03.
#[test]
fn test_invoice_payment_payload() {
let s = "{\"payment\": {\"extratlvs\": [], \"label\": \"{\\\"unix_milli\\\":1717422773673,\\\"payer_amount_msat\\\":null}\", \"msat\": \"42btc\", \"preimage\": \"243adf90767a5c3a8f6118e003c89b3e1ab5a2fd318d49cb41f4d42e92d3de41\"}}";
let v = serde_json::from_str(&s).expect("parsing generic value");
let _c: InvoicePaymentCall = serde_json::from_value(v).expect("parsing into struct");
}
}
Loading