Skip to content

Invoices crate #870

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

Merged
merged 5 commits into from
Apr 10, 2021
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
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ jobs:
- name: Sanity check fuzz targets on Rust ${{ env.TOOLCHAIN }}
run: cd fuzz && RUSTFLAGS="--cfg=fuzzing" cargo test --verbose --color always
- name: Run fuzzers
run: cd fuzz && ./ci-fuzz.sh
run: cd fuzz && ./ci-fuzz.sh && cd ..
- name: Run lightning-invoice fuzzers
run: cd lightning-invoice/fuzz && RUSTFLAGS="--cfg=fuzzing" cargo test --verbose && ./ci-fuzz.sh

linting:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
members = [
"lightning",
"lightning-block-sync",
"lightning-invoice",
"lightning-net-tokio",
"lightning-persister",
"background-processor",
Expand Down
3 changes: 3 additions & 0 deletions lightning-invoice/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
**/*.rs.bk
Cargo.lock
19 changes: 19 additions & 0 deletions lightning-invoice/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "lightning-invoice"
description = "Data structures to parse and serialize BOLT11 lightning invoices"
version = "0.4.0"
authors = ["Sebastian Geisler <[email protected]>"]
documentation = "https://docs.rs/lightning-invoice/"
license = "MIT OR Apache-2.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know much about licenses, but if you need my ok for this you hereby have it.

keywords = [ "lightning", "bitcoin", "invoice", "BOLT11" ]
readme = "README.md"

[dependencies]
bech32 = "0.7"
secp256k1 = { version = "0.20", features = ["recovery"] }
num-traits = "0.2.8"
bitcoin_hashes = "0.9.4"

[lib]
crate-type = ["cdylib", "rlib"]

9 changes: 9 additions & 0 deletions lightning-invoice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# lightning-invoice
[![Docs.rs](https://docs.rs/lightning-invoice/badge.svg)](https://docs.rs/lightning-invoice/)

This repo provides data structures for BOLT 11 lightning invoices and
functions to parse and serialize these from and to bech32.

**Please be sure to run the test suite since we need to check assumptions
regarding `SystemTime`'s bounds on your platform. You can also call `check_platform`
on startup or in your test suite to do so.**
2 changes: 2 additions & 0 deletions lightning-invoice/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
hfuzz_*
26 changes: 26 additions & 0 deletions lightning-invoice/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "lightning-invoice-fuzz"
version = "0.0.1"
authors = ["Automatically generated"]
publish = false

[package.metadata]
cargo-fuzz = true

[features]
afl_fuzz = ["afl"]
honggfuzz_fuzz = ["honggfuzz"]

[dependencies]
honggfuzz = { version = "0.5", optional = true }
afl = { version = "0.4", optional = true }
lightning-invoice = { path = ".."}
bech32 = "0.7"

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[[bin]]
name = "serde_data_part"
path = "fuzz_targets/serde_data_part.rs"
19 changes: 19 additions & 0 deletions lightning-invoice/fuzz/ci-fuzz.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -e
cargo install --force honggfuzz
for TARGET in fuzz_targets/*; do
FILENAME=$(basename $TARGET)
FILE="${FILENAME%.*}"
if [ -d hfuzz_input/$FILE ]; then
HFUZZ_INPUT_ARGS="-f hfuzz_input/$FILE/input"
fi
HFUZZ_BUILD_ARGS="--features honggfuzz_fuzz" HFUZZ_RUN_ARGS="-N1000000 --exit_upon_crash -v $HFUZZ_INPUT_ARGS" cargo hfuzz run $FILE

if [ -f hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT ]; then
cat hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT
for CASE in hfuzz_workspace/$FILE/SIG*; do
cat $CASE | xxd -p
done
exit 1
fi
done
69 changes: 69 additions & 0 deletions lightning-invoice/fuzz/fuzz_targets/serde_data_part.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
extern crate lightning_invoice;
extern crate bech32;

use lightning_invoice::RawDataPart;
use bech32::{FromBase32, ToBase32, u5};

fn do_test(data: &[u8]) {
let bech32 = data.iter().map(|x| u5::try_from_u8(x % 32).unwrap()).collect::<Vec<_>>();
let invoice = match RawDataPart::from_base32(&bech32) {
Ok(invoice) => invoice,
Err(_) => return,
};

// Our encoding is not worse than the input
assert!(invoice.to_base32().len() <= bech32.len());

// Our serialization is loss-less
assert_eq!(
RawDataPart::from_base32(&invoice.to_base32()).expect("faild parsing out own encoding"),
invoice
);
}

#[cfg(feature = "afl")]
#[macro_use] extern crate afl;
#[cfg(feature = "afl")]
fn main() {
fuzz!(|data| {
do_test(&data);
});
}

#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
fuzz!(|data| {
do_test(data);
});
}
}

#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().filter(|&&c| c != b'\n').enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}

#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("000000", &mut a);
super::do_test(&a);
}
}
Loading