-
Notifications
You must be signed in to change notification settings - Fork 363
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
add feature for Invoice ser/de #1548
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1548 +/- ##
==========================================
- Coverage 90.96% 90.93% -0.04%
==========================================
Files 80 80
Lines 43533 43610 +77
Branches 43533 43610 +77
==========================================
+ Hits 39599 39655 +56
- Misses 3934 3955 +21
Continue to review full report at Codecov.
|
I'm not sure I fully understand the motivation for this - you're adding methods to do the serialization not implementing serde traits, so it seems like its the same amount of code to just call the existing string-serialization methods? |
|
(accidentally closed) Hi, thank you for the reply, I'm not sure if I understand. You mean to serialize as string I don't need to use |
bdbdc54
to
c7e1c4a
Compare
Ok now someone explained it to me. Does my approach make sense now ? (just force pushed) |
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.
Right, this makes more sense now. I'm still kinda curious what your use-case is - I assume you're storing invoices in some larger struct and want to serde it?
lightning-invoice/Cargo.toml
Outdated
|
||
[dev-dependencies] | ||
lightning = { version = "0.0.108", path = "../lightning", default-features = false, features = ["_test_utils"] } | ||
hex = "0.4" | ||
serde = { version = "1.0.118", features = ["derive"]} | ||
serde_json = "1.0.81" |
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.
Is this used?
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.
Oh my bad I used it in the doc tests in the earlier version and forgot to delete it
I am currently working on a JSON-2.0 RPC for minimint. using it like this: |
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.
LGTM, basically.
lightning-invoice/Cargo.toml
Outdated
@@ -17,6 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"] | |||
default = ["std"] | |||
no-std = ["hashbrown", "lightning/no-std", "core2/alloc"] | |||
std = ["bitcoin_hashes/std", "num-traits/std", "lightning/std", "bech32/std"] | |||
serde_invoice = ["serde"] |
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.
Should we bother specifying the feature here? Even if we don't put it here users can use serde
as a feature to turn on the dependency.
lightning-invoice/src/lib.rs
Outdated
@@ -35,6 +35,8 @@ extern crate secp256k1; | |||
extern crate alloc; | |||
#[cfg(any(test, feature = "std"))] | |||
extern crate core; | |||
#[cfg(feature = "serde_invoice")] |
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.
Lets use the implicit serde
feature instead of serde_invoice
for the cfg tags.
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.
OK (new to rust so I thought there has to be a name)
LGTM, this should get some kinda test, though. |
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.
Generally looks good, mod two nits. 👍
lightning-invoice/src/lib.rs
Outdated
#[cfg(test)] | ||
mod test { | ||
use bitcoin_hashes::hex::FromHex; | ||
use bitcoin_hashes::sha256; | ||
use Invoice; |
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.
nit: No need to import this.
lightning-invoice/src/lib.rs
Outdated
#[test] | ||
fn test_serde() { | ||
let invoice = "lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\ | ||
h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\ |
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.
nit: These lines shouldn't indented as much. :)
lightning-invoice/src/lib.rs
Outdated
let serialized_invoice = serde_json::to_string(&invoice).unwrap(); | ||
let deserialized_invoice: Invoice = serde_json::from_str(serialized_invoice.as_str()).unwrap(); | ||
assert_eq!(invoice, deserialized_invoice); | ||
assert_eq!(invoice.to_string(), deserialized_invoice.to_string()); |
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.
Could you also test here that the serialized invoice actually matches the initial string provided? Otherwise there might be the slight possibility that you make a mistake in the initial parsing and here only ever confirm that the logic is able to deserialize whatever you're serializing from that.
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.
But serde_json::to_string(...)
serializes as JSON so the str is not "lnbc1..." but ""lnbc1..."" so I can't test that right ?
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.
Well, if it's indeed only about the wrapping "
s, then you could just strip them before comparing the strings?
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.
Right, that works I'll force push again thank you
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.
LGTM, mod one nit. Sorry :)
Add feature "serde_invoice" for conditional dependency on serde to serialize/deserialize invoice