Skip to content

Commit b77b547

Browse files
authored
Merge pull request #870 from valentinewallace/invoices-crate
Invoices crate
2 parents ba3ef0a + c3d25ed commit b77b547

File tree

14 files changed

+3480
-1
lines changed

14 files changed

+3480
-1
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ jobs:
207207
- name: Sanity check fuzz targets on Rust ${{ env.TOOLCHAIN }}
208208
run: cd fuzz && RUSTFLAGS="--cfg=fuzzing" cargo test --verbose --color always
209209
- name: Run fuzzers
210-
run: cd fuzz && ./ci-fuzz.sh
210+
run: cd fuzz && ./ci-fuzz.sh && cd ..
211+
- name: Run lightning-invoice fuzzers
212+
run: cd lightning-invoice/fuzz && RUSTFLAGS="--cfg=fuzzing" cargo test --verbose && ./ci-fuzz.sh
211213

212214
linting:
213215
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
members = [
44
"lightning",
55
"lightning-block-sync",
6+
"lightning-invoice",
67
"lightning-net-tokio",
78
"lightning-persister",
89
"background-processor",

lightning-invoice/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target
2+
**/*.rs.bk
3+
Cargo.lock

lightning-invoice/Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "lightning-invoice"
3+
description = "Data structures to parse and serialize BOLT11 lightning invoices"
4+
version = "0.4.0"
5+
authors = ["Sebastian Geisler <[email protected]>"]
6+
documentation = "https://docs.rs/lightning-invoice/"
7+
license = "MIT OR Apache-2.0"
8+
keywords = [ "lightning", "bitcoin", "invoice", "BOLT11" ]
9+
readme = "README.md"
10+
11+
[dependencies]
12+
bech32 = "0.7"
13+
secp256k1 = { version = "0.20", features = ["recovery"] }
14+
num-traits = "0.2.8"
15+
bitcoin_hashes = "0.9.4"
16+
17+
[lib]
18+
crate-type = ["cdylib", "rlib"]
19+

lightning-invoice/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# lightning-invoice
2+
[![Docs.rs](https://docs.rs/lightning-invoice/badge.svg)](https://docs.rs/lightning-invoice/)
3+
4+
This repo provides data structures for BOLT 11 lightning invoices and
5+
functions to parse and serialize these from and to bech32.
6+
7+
**Please be sure to run the test suite since we need to check assumptions
8+
regarding `SystemTime`'s bounds on your platform. You can also call `check_platform`
9+
on startup or in your test suite to do so.**

lightning-invoice/fuzz/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
hfuzz_*

lightning-invoice/fuzz/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "lightning-invoice-fuzz"
3+
version = "0.0.1"
4+
authors = ["Automatically generated"]
5+
publish = false
6+
7+
[package.metadata]
8+
cargo-fuzz = true
9+
10+
[features]
11+
afl_fuzz = ["afl"]
12+
honggfuzz_fuzz = ["honggfuzz"]
13+
14+
[dependencies]
15+
honggfuzz = { version = "0.5", optional = true }
16+
afl = { version = "0.4", optional = true }
17+
lightning-invoice = { path = ".."}
18+
bech32 = "0.7"
19+
20+
# Prevent this from interfering with workspaces
21+
[workspace]
22+
members = ["."]
23+
24+
[[bin]]
25+
name = "serde_data_part"
26+
path = "fuzz_targets/serde_data_part.rs"

lightning-invoice/fuzz/ci-fuzz.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
set -e
3+
cargo install --force honggfuzz
4+
for TARGET in fuzz_targets/*; do
5+
FILENAME=$(basename $TARGET)
6+
FILE="${FILENAME%.*}"
7+
if [ -d hfuzz_input/$FILE ]; then
8+
HFUZZ_INPUT_ARGS="-f hfuzz_input/$FILE/input"
9+
fi
10+
HFUZZ_BUILD_ARGS="--features honggfuzz_fuzz" HFUZZ_RUN_ARGS="-N1000000 --exit_upon_crash -v $HFUZZ_INPUT_ARGS" cargo hfuzz run $FILE
11+
12+
if [ -f hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT ]; then
13+
cat hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT
14+
for CASE in hfuzz_workspace/$FILE/SIG*; do
15+
cat $CASE | xxd -p
16+
done
17+
exit 1
18+
fi
19+
done
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
extern crate lightning_invoice;
2+
extern crate bech32;
3+
4+
use lightning_invoice::RawDataPart;
5+
use bech32::{FromBase32, ToBase32, u5};
6+
7+
fn do_test(data: &[u8]) {
8+
let bech32 = data.iter().map(|x| u5::try_from_u8(x % 32).unwrap()).collect::<Vec<_>>();
9+
let invoice = match RawDataPart::from_base32(&bech32) {
10+
Ok(invoice) => invoice,
11+
Err(_) => return,
12+
};
13+
14+
// Our encoding is not worse than the input
15+
assert!(invoice.to_base32().len() <= bech32.len());
16+
17+
// Our serialization is loss-less
18+
assert_eq!(
19+
RawDataPart::from_base32(&invoice.to_base32()).expect("faild parsing out own encoding"),
20+
invoice
21+
);
22+
}
23+
24+
#[cfg(feature = "afl")]
25+
#[macro_use] extern crate afl;
26+
#[cfg(feature = "afl")]
27+
fn main() {
28+
fuzz!(|data| {
29+
do_test(&data);
30+
});
31+
}
32+
33+
#[cfg(feature = "honggfuzz")]
34+
#[macro_use] extern crate honggfuzz;
35+
#[cfg(feature = "honggfuzz")]
36+
fn main() {
37+
loop {
38+
fuzz!(|data| {
39+
do_test(data);
40+
});
41+
}
42+
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
47+
let mut b = 0;
48+
for (idx, c) in hex.as_bytes().iter().filter(|&&c| c != b'\n').enumerate() {
49+
b <<= 4;
50+
match *c {
51+
b'A'...b'F' => b |= c - b'A' + 10,
52+
b'a'...b'f' => b |= c - b'a' + 10,
53+
b'0'...b'9' => b |= c - b'0',
54+
_ => panic!("Bad hex"),
55+
}
56+
if (idx & 1) == 1 {
57+
out.push(b);
58+
b = 0;
59+
}
60+
}
61+
}
62+
63+
#[test]
64+
fn duplicate_crash() {
65+
let mut a = Vec::new();
66+
extend_vec_from_hex("000000", &mut a);
67+
super::do_test(&a);
68+
}
69+
}

0 commit comments

Comments
 (0)