Add all the manually-generated bits for the c-bindings crate
authorMatt Corallo <git@bluematt.me>
Tue, 12 May 2020 18:54:12 +0000 (14:54 -0400)
committerMatt Corallo <git@bluematt.me>
Fri, 11 Sep 2020 02:03:32 +0000 (22:03 -0400)
Including:
 * A script to automatically generate all the rest,
 * Cargo.toml and cbindgen.toml,
 * manually-written wrapper types for a few types

genbindings.sh [new file with mode: 0755]
lightning-c-bindings/Cargo.toml [new file with mode: 0644]
lightning-c-bindings/cbindgen.toml [new file with mode: 0644]
lightning-c-bindings/demo.c [new file with mode: 0644]
lightning-c-bindings/demo.cpp [new file with mode: 0644]
lightning-c-bindings/src/bitcoin/mod.rs [new file with mode: 0644]
lightning-c-bindings/src/bitcoin/network.rs [new file with mode: 0644]
lightning-c-bindings/src/c_types/mod.rs [new file with mode: 0644]

diff --git a/genbindings.sh b/genbindings.sh
new file mode 100755 (executable)
index 0000000..c801c44
--- /dev/null
@@ -0,0 +1,189 @@
+#!/usr/bin/env bash
+
+set -e
+set -x
+
+# Generate (and reasonably test) C bindings
+
+# First build the latest c-bindings-gen binary
+cd c-bindings-gen && cargo build && cd ..
+
+# Then wipe all the existing C bindings (because we're being run in the right directory)
+# note that we keep the few manually-generated files first:
+mv lightning-c-bindings/src/c_types/mod.rs ./
+mv lightning-c-bindings/src/bitcoin ./
+
+rm -rf lightning-c-bindings/src
+
+mkdir -p lightning-c-bindings/src/c_types/
+mv ./mod.rs lightning-c-bindings/src/c_types/
+mv ./bitcoin lightning-c-bindings/src/
+
+# Finally, run the c-bindings-gen binary, building fresh bindings.
+SRC="$(pwd)/lightning/src"
+OUT="$(pwd)/lightning-c-bindings/src"
+OUT_TEMPL="$(pwd)/lightning-c-bindings/src/c_types/derived.rs"
+OUT_F="$(pwd)/lightning-c-bindings/include/rust_types.h"
+OUT_CPP="$(pwd)/lightning-c-bindings/include/lightningpp.hpp"
+RUST_BACKTRACE=1 ./c-bindings-gen/target/debug/c-bindings-gen $SRC/ $OUT/ lightning $OUT_TEMPL $OUT_F $OUT_CPP
+
+# Now cd to lightning-c-bindings, build the generated bindings, and call cbindgen to build a C header file
+PATH="$PATH:~/.cargo/bin"
+cd lightning-c-bindings
+cargo build
+cbindgen -v --config cbindgen.toml -o include/lightning.h >/dev/null 2>&1
+
+HOST_PLATFORM="$(rustc --version --verbose | grep "host:")"
+
+# cbindgen is relatively braindead when exporting typedefs -
+# it happily exports all our typedefs for private types, even with the
+# generics we specified in C mode! So we drop all those types manually here.
+if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
+       # OSX sed is for some reason not compatible with GNU sed
+       sed -i '' 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
+else
+       sed -i 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
+fi
+
+# Finally, sanity-check the generated C and C++ bindings with demo apps:
+
+# Naively run the C demo app:
+gcc -Wall -g -pthread demo.c ../target/debug/liblightning.a -ldl
+./a.out
+
+# And run the C++ demo app in valgrind to test memory model correctness and lack of leaks.
+g++ -std=c++11 -Wall -g -pthread demo.cpp -L../target/debug/ -llightning -ldl
+if [ -x "`which valgrind`" ]; then
+       LD_LIBRARY_PATH=../target/debug/ valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out
+       echo
+else
+       echo "WARNING: Please install valgrind for more testing"
+fi
+
+# Test a statically-linked C++ version, tracking the resulting binary size and runtime
+# across debug, LTO, and cross-language LTO builds (using the same compiler each time).
+clang++ -std=c++11 -Wall -pthread demo.cpp ../target/debug/liblightning.a -ldl
+./a.out >/dev/null
+echo " C++ Bin size and runtime w/o optimization:"
+ls -lha a.out
+time ./a.out > /dev/null
+
+# Then, check with memory sanitizer, if we're on Linux and have rustc nightly
+if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
+       if cargo +nightly --version >/dev/null 2>&1; then
+               LLVM_V=$(rustc +nightly --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }')
+               if [ -x "$(which clang-$LLVM_V)" ]; then
+                       cargo +nightly clean
+                       cargo +nightly rustc -Zbuild-std --target x86_64-unknown-linux-gnu -v -- -Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes
+                       mv ../target/x86_64-unknown-linux-gnu/debug/liblightning.* ../target/debug/
+
+                       # Sadly, std doesn't seem to compile into something that is memsan-safe as of Aug 2020,
+                       # so we'll always fail, not to mention we may be linking against git rustc LLVM which
+                       # may differ from clang-llvm, so just allow everything here to fail.
+                       set +e
+
+                       # First the C demo app...
+                       clang-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.c ../target/debug/liblightning.a -ldl
+                       ./a.out
+
+                       # ...then the C++ demo app
+                       clang++-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.cpp ../target/debug/liblightning.a -ldl
+                       ./a.out >/dev/null
+
+                       # restore exit-on-failure
+                       set -e
+               else
+                       echo "WARNING: Can't use memory sanitizer without clang-$LLVM_V"
+               fi
+       else
+               echo "WARNING: Can't use memory sanitizer without rustc nightly"
+       fi
+else
+       echo "WARNING: Can't use memory sanitizer on non-Linux, non-x86 platforms"
+fi
+
+RUSTC_LLVM_V=$(rustc --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }' | tr -d '.')
+
+if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
+       # Apple is special, as always, and decided that they must ensure that there is no way to identify
+       # the LLVM version used. Why? Just to make your life hard.
+       # This list is taken from https://en.wikipedia.org/wiki/Xcode
+       APPLE_CLANG_V=$(clang --version | head -n1 | awk '{ print $4 }')
+       if [ "$APPLE_CLANG_V" = "10.0.0" ]; then
+               CLANG_LLVM_V="6"
+       elif [ "$APPLE_CLANG_V" = "10.0.1" ]; then
+               CLANG_LLVM_V="7"
+       elif [ "$APPLE_CLANG_V" = "11.0.0" ]; then
+               CLANG_LLVM_V="8"
+       elif [ "$APPLE_CLANG_V" = "11.0.3" ]; then
+               CLANG_LLVM_V="9"
+       elif [ "$APPLE_CLANG_V" = "12.0.0" ]; then
+               CLANG_LLVM_V="10"
+       else
+               echo "WARNING: Unable to identify Apple clang LLVM version"
+               CLANG_LLVM_V="0"
+       fi
+else
+       CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($4, 0, 2); }' | tr -d '.')
+fi
+
+if [ "$CLANG_LLVM_V" = "$RUSTC_LLVM_V" ]; then
+       CLANG=clang
+       CLANGPP=clang++
+elif [ "$(which clang-$RUSTC_LLVM_V)" != "" ]; then
+       CLANG="$(which clang-$RUSTC_LLVM_V)"
+       CLANGPP="$(which clang++-$RUSTC_LLVM_V)"
+fi
+
+if [ "$CLANG" != "" -a "$CLANGPP" = "" ]; then
+       echo "WARNING: It appears you have a clang-$RUSTC_LLVM_V but not clang++-$RUSTC_LLVM_V. This is common, but leaves us unable to compile C++ with LLVM $RUSTC_LLVM_V"
+       echo "You should create a symlink called clang++-$RUSTC_LLVM_V pointing to $CLANG in $(dirname $CLANG)"
+fi
+
+# Finally, if we're on OSX or on Linux, build the final debug binary with address sanitizer (and leave it there)
+if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
+       if [ "$CLANGPP" != "" ]; then
+               if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
+                       # OSX sed is for some reason not compatible with GNU sed
+                       sed -i .bk 's/,"cdylib"]/]/g' Cargo.toml
+               else
+                       sed -i.bk 's/,"cdylib"]/]/g' Cargo.toml
+               fi
+               RUSTC_BOOTSTRAP=1 cargo rustc -v -- -Zsanitizer=address -Cforce-frame-pointers=yes || ( mv Cargo.toml.bk Cargo.toml; exit 1)
+               mv Cargo.toml.bk Cargo.toml
+
+               # First the C demo app...
+               $CLANG -fsanitize=address -Wall -g -pthread demo.c ../target/debug/liblightning.a -ldl
+               ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out
+
+               # ...then the C++ demo app
+               $CLANGPP -std=c++11 -fsanitize=address -Wall -g -pthread demo.cpp ../target/debug/liblightning.a -ldl
+               ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
+       else
+               echo "WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer"
+       fi
+else
+       echo "WARNING: Can't use address sanitizer on non-Linux, non-OSX non-x86 platforms"
+fi
+
+# Now build with LTO on on both C++ and rust, but without cross-language LTO:
+cargo rustc -v --release -- -C lto
+clang++ -std=c++11 -Wall -flto -O2 -pthread demo.cpp ../target/release/liblightning.a -ldl
+echo "C++ Bin size and runtime with only RL (LTO) optimized:"
+ls -lha a.out
+time ./a.out > /dev/null
+
+if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
+       # Finally, test cross-language LTO. Note that this will fail if rustc and clang++
+       # build against different versions of LLVM (eg when rustc is installed via rustup
+       # or Ubuntu packages). This should work fine on Distros which do more involved
+       # packaging than simply shipping the rustup binaries (eg Debian should Just Work
+       # here).
+       cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld
+       $CLANGPP -Wall -std=c++11 -flto -fuse-ld=lld -O2 -pthread demo.cpp ../target/release/liblightning.a -ldl
+       echo "C++ Bin size and runtime with cross-language LTO:"
+       ls -lha a.out
+       time ./a.out > /dev/null
+else
+       echo "WARNING: Building with cross-language LTO is not avilable on OSX or without clang-$RUSTC_LLVM_V"
+fi
diff --git a/lightning-c-bindings/Cargo.toml b/lightning-c-bindings/Cargo.toml
new file mode 100644 (file)
index 0000000..8280f35
--- /dev/null
@@ -0,0 +1,19 @@
+[package]
+name = "lightning-c-bindings"
+version = "0.0.1"
+authors = ["Matt Corallo"]
+license = "Apache-2.0"
+edition = "2018"
+description = """
+Utilities to fetch the chain from Bitcoin Core REST/RPC Interfaces and feed them into Rust Lightning.
+"""
+
+[lib]
+name = "lightning"
+crate-type = ["staticlib"
+# Note that the following line is matched exactly by genbindings to turn off dylib creation
+,"cdylib"]
+
+[dependencies]
+bitcoin = "0.24"
+lightning = { version = "0.0.11", path = "../lightning" }
diff --git a/lightning-c-bindings/cbindgen.toml b/lightning-c-bindings/cbindgen.toml
new file mode 100644 (file)
index 0000000..4826778
--- /dev/null
@@ -0,0 +1,552 @@
+# The language to output bindings in
+#
+# possible values: "C", "C++"
+#
+# default: "C++"
+language = "C"
+
+
+
+
+# Options for wrapping the contents of the header:
+
+# An optional string of text to output at the beginning of the generated file
+# default: doesn't emit anything
+header = "/* Text to put at the beginning of the generated file. Probably a license. */"
+
+# An optional string of text to output at the end of the generated file
+# default: doesn't emit anything
+trailer = "/* Text to put at the end of the generated file */"
+
+# An optional name to use as an include guard
+# default: doesn't emit an include guard
+# include_guard = "mozilla_wr_bindings_h"
+
+# An optional string of text to output between major sections of the generated
+# file as a warning against manual editing
+#
+# default: doesn't emit anything
+autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
+
+# Whether to include a comment with the version of cbindgen used to generate the file
+# default: false
+include_version = true
+
+# An optional namespace to output around the generated bindings
+# default: doesn't emit a namespace
+namespace = "LDK"
+
+# An optional list of namespaces to output around the generated bindings
+# default: []
+namespaces = []
+
+# An optional list of namespaces to declare as using with "using namespace"
+# default: []
+using_namespaces = []
+
+# A list of sys headers to #include (with angle brackets)
+# default: []
+# sys_includes = ["stdio", "string"]
+# sys_includes = ["stdint"]
+
+# A list of headers to #include (with quotes)
+# default: []
+# includes = ["my_great_lib.h"]
+
+# Whether cbindgen's default C/C++ standard imports should be suppressed. These
+# imports are included by default because our generated headers tend to require
+# them (e.g. for uint32_t). Currently, the generated imports are:
+#
+# * for C: <stdarg.h>, <stdbool.h>, <stdint.h>, <stdlib.h>, <uchar.h>
+#
+# * for C++: <cstdarg>, <cstdint>, <cstdlib>, <new>, <cassert> (depending on config)
+#
+# default: false
+no_includes = false
+
+
+
+
+
+# Code Style Options
+
+# The style to use for curly braces
+#
+# possible values: "SameLine", "NextLine"
+#
+# default: "SameLine"
+braces = "SameLine"
+
+# The desired length of a line to use when formatting lines
+# default: 100
+line_length = 80
+
+# The amount of spaces to indent by
+# default: 2
+tab_width = 3
+
+# How the generated documentation should be commented.
+#
+# possible values:
+# * "c": /* like this */
+# * "c99": // like this
+# * "c++": /// like this
+# * "doxy": like C, but with leading *'s on each line
+# * "auto": "c++" if that's the language, "doxy" otherwise
+#
+# default: "auto"
+documentation_style = "doxy"
+
+
+
+
+
+# Codegen Options
+
+# When generating a C header, the kind of declaration style to use for structs
+# or enums.
+#
+# possible values:
+# * "type": typedef struct { ... } MyType;
+# * "tag": struct MyType { ... };
+# * "both": typedef struct MyType { ... } MyType;
+#
+# default: "both"
+style = "both"
+
+# A list of substitutions for converting cfg's to ifdefs. cfgs which aren't
+# defined here will just be discarded.
+#
+# e.g.
+# `#[cfg(target = "freebsd")] ...`
+# becomes
+# `#if defined(DEFINE_FREEBSD) ... #endif`
+[defines]
+"target_os = freebsd" = "DEFINE_FREEBSD"
+"feature = serde" = "DEFINE_SERDE"
+
+
+
+
+
+[export]
+# A list of additional items to always include in the generated bindings if they're
+# found but otherwise don't appear to be used by the public API.
+#
+# default: []
+# include = ["MyOrphanStruct", "MyGreatTypeRename"]
+
+# A list of items to not include in the generated bindings
+# default: []
+# exclude = ["Bad"]
+
+# A prefix to add before the name of every item
+# default: no prefix is added
+prefix = "LDK"
+
+# Types of items that we'll generate. If empty, then all types of item are emitted.
+#
+# possible items: (TODO: explain these in detail)
+# * "constants":
+# * "globals":
+# * "enums":
+# * "structs":
+# * "unions":
+# * "typedefs":
+# * "opaque":
+# * "functions":
+#
+# default: []
+item_types = ["constants", "globals", "enums", "structs", "unions", "typedefs", "opaque", "functions"]
+
+# Whether applying rules in export.rename prevents export.prefix from applying.
+#
+# e.g. given this toml:
+#
+# [export]
+# prefix = "capi_"
+# [export.rename]
+# "MyType" = "my_cool_type"
+#
+# You get the following results:
+#
+# renaming_overrides_prefixing = true:
+# "MyType" => "my_cool_type"
+#
+# renaming_overrides_prefixing = false:
+# "MyType => capi_my_cool_type"
+#
+# default: false
+renaming_overrides_prefixing = true
+
+# Table of name conversions to apply to item names (lhs becomes rhs)
+# [export.rename]
+# "MyType" = "my_cool_type"
+# "my_function" = "BetterFunctionName"
+
+# Table of things to prepend to the body of any struct, union, or enum that has the
+# given name. This can be used to add things like methods which don't change ABI,
+# mark fields private, etc
+[export.pre_body]
+"MyType" = """
+  MyType() = delete;
+private:
+"""
+
+# Table of things to append to the body of any struct, union, or enum that has the
+# given name. This can be used to add things like methods which don't change ABI.
+[export.body]
+"MyType" = """
+  void cppMethod() const;
+"""
+
+[layout]
+# A string that should come before the name of any type which has been marked
+# as `#[repr(packed)]`. For instance, "__attribute__((packed))" would be a
+# reasonable value if targeting gcc/clang. A more portable solution would
+# involve emitting the name of a macro which you define in a platform-specific
+# way. e.g. "PACKED"
+#
+# default: `#[repr(packed)]` types will be treated as opaque, since it would
+# be unsafe for C callers to use a incorrectly laid-out union.
+packed = "PACKED"
+
+# A string that should come before the name of any type which has been marked
+# as `#[repr(align(n))]`. This string must be a function-like macro which takes
+# a single argument (the requested alignment, `n`). For instance, a macro
+# `#define`d as `ALIGNED(n)` in `header` which translates to
+# `__attribute__((aligned(n)))` would be a reasonable value if targeting
+# gcc/clang.
+#
+# default: `#[repr(align(n))]` types will be treated as opaque, since it
+# could be unsafe for C callers to use a incorrectly-aligned union.
+aligned_n = "ALIGNED"
+
+
+[fn]
+# An optional prefix to put before every function declaration
+# default: no prefix added
+# prefix = "WR_START_FUNC"
+
+# An optional postfix to put after any function declaration
+# default: no postix added
+# postfix = "WR_END_FUNC"
+
+# How to format function arguments
+#
+# possible values:
+# * "horizontal": place all arguments on the same line
+# * "vertical": place each argument on its own line
+# * "auto": only use vertical if horizontal would exceed line_length
+#
+# default: "auto"
+args = "horizontal"
+
+# An optional string that should prefix function declarations which have been
+# marked as `#[must_use]`. For instance, "__attribute__((warn_unused_result))"
+# would be a reasonable value if targeting gcc/clang. A more portable solution
+# would involve emitting the name of a macro which you define in a
+# platform-specific way. e.g. "MUST_USE_FUNC"
+# default: nothing is emitted for must_use functions
+must_use = "MUST_USE_RES"
+
+# An optional string that, if present, will be used to generate Swift function
+# and method signatures for generated functions, for example "CF_SWIFT_NAME".
+# If no such macro is available in your toolchain, you can define one using the
+# `header` option in cbindgen.toml
+# default: no swift_name function attributes are generated
+# swift_name_macro = "CF_SWIFT_NAME"
+
+# A rule to use to rename function argument names. The renaming assumes the input
+# is the Rust standard snake_case, however it accepts all the different rename_args
+# inputs. This means many options here are no-ops or redundant.
+#
+# possible values (that actually do something):
+# * "CamelCase": my_arg => myArg
+# * "PascalCase": my_arg => MyArg
+# * "GeckoCase": my_arg => aMyArg
+# * "ScreamingSnakeCase": my_arg => MY_ARG
+# * "None": apply no renaming
+#
+# technically possible values (that shouldn't have a purpose here):
+# * "SnakeCase": apply no renaming
+# * "LowerCase": apply no renaming (actually applies to_lowercase, is this bug?)
+# * "UpperCase": same as ScreamingSnakeCase in this context
+# * "QualifiedScreamingSnakeCase" => same as ScreamingSnakeCase in this context
+#
+# default: "None"
+rename_args = "None"
+
+# This rule specifies if the order of functions will be sorted in some way.
+#
+# "Name": sort by the name of the function
+# "None": keep order in which the functions have been parsed
+#
+# default: "Name"
+sort_by = "None"
+
+[struct]
+# A rule to use to rename struct field names. The renaming assumes the input is
+# the Rust standard snake_case, however it acccepts all the different rename_args
+# inputs. This means many options here are no-ops or redundant.
+#
+# possible values (that actually do something):
+# * "CamelCase": my_arg => myArg
+# * "PascalCase": my_arg => MyArg
+# * "GeckoCase": my_arg => mMyArg
+# * "ScreamingSnakeCase": my_arg => MY_ARG
+# * "None": apply no renaming
+#
+# technically possible values (that shouldn't have a purpose here):
+# * "SnakeCase": apply no renaming
+# * "LowerCase": apply no renaming (actually applies to_lowercase, is this bug?)
+# * "UpperCase": same as ScreamingSnakeCase in this context
+# * "QualifiedScreamingSnakeCase" => same as ScreamingSnakeCase in this context
+#
+# default: "None"
+rename_fields = "None"
+
+# An optional string that should come before the name of any struct which has been
+# marked as `#[must_use]`. For instance, "__attribute__((warn_unused))"
+# would be a reasonable value if targeting gcc/clang. A more portable solution
+# would involve emitting the name of a macro which you define in a
+# platform-specific way. e.g. "MUST_USE_STRUCT"
+#
+# default: nothing is emitted for must_use structs
+must_use = "MUST_USE_STRUCT"
+
+# Whether a Rust type with associated consts should emit those consts inside the
+# type's body. Otherwise they will be emitted trailing and with the type's name
+# prefixed. This does nothing if the target is C, or if
+# [const]allow_static_const = false
+#
+# default: false
+# associated_constants_in_body: false
+
+# Whether to derive a simple constructor that takes a value for every field.
+# default: false
+derive_constructor = true
+
+# Whether to derive an operator== for all structs
+# default: false
+derive_eq = false
+
+# Whether to derive an operator!= for all structs
+# default: false
+derive_neq = false
+
+# Whether to derive an operator< for all structs
+# default: false
+derive_lt = false
+
+# Whether to derive an operator<= for all structs
+# default: false
+derive_lte = false
+
+# Whether to derive an operator> for all structs
+# default: false
+derive_gt = false
+
+# Whether to derive an operator>= for all structs
+# default: false
+derive_gte = false
+
+
+
+
+
+[enum]
+# A rule to use to rename enum variants, and the names of any fields those
+# variants have. This should probably be split up into two separate options, but
+# for now, they're the same! See the documentation for `[struct]rename_fields`
+# for how this applies to fields. Renaming of the variant assumes that the input
+# is the Rust standard PascalCase. In the case of QualifiedScreamingSnakeCase,
+# it also assumed that the enum's name is PascalCase.
+#
+# possible values (that actually do something):
+# * "CamelCase": MyVariant => myVariant
+# * "SnakeCase": MyVariant => my_variant
+# * "ScreamingSnakeCase": MyVariant => MY_VARIANT
+# * "QualifiedScreamingSnakeCase": MyVariant => ENUM_NAME_MY_VARIANT
+# * "LowerCase": MyVariant => myvariant
+# * "UpperCase": MyVariant => MYVARIANT
+# * "None": apply no renaming
+#
+# technically possible values (that shouldn't have a purpose for the variants):
+# * "PascalCase": apply no renaming
+# * "GeckoCase": apply no renaming
+#
+# default: "None"
+rename_variants = "None"
+
+# Whether an extra "sentinel" enum variant should be added to all generated enums.
+# Firefox uses this for their IPC serialization library.
+#
+# WARNING: if the sentinel is ever passed into Rust, behaviour will be Undefined.
+# Rust does not know about this value, and will assume it cannot happen.
+#
+# default: false
+add_sentinel = true
+
+# Whether enum variant names should be prefixed with the name of the enum.
+# default: false
+prefix_with_name = true
+
+# Whether to emit enums using "enum class" when targeting C++.
+# default: true
+enum_class = true
+
+# Whether to generate static `::MyVariant(..)` constructors and `bool IsMyVariant()`
+# methods for enums with fields.
+#
+# default: false
+derive_helper_methods = false
+
+# Whether to generate `const MyVariant& AsMyVariant() const` methods for enums with fields.
+# default: false
+derive_const_casts = false
+
+# Whether to generate `MyVariant& AsMyVariant()` methods for enums with fields
+# default: false
+derive_mut_casts = false
+
+# The name of the macro/function to use for asserting `IsMyVariant()` in the body of
+# derived `AsMyVariant()` cast methods.
+#
+# default: "assert" (but also causes `<cassert>` to be included by default)
+cast_assert_name = "MOZ_RELEASE_ASSERT"
+
+# An optional string that should come before the name of any enum which has been
+# marked as `#[must_use]`. For instance, "__attribute__((warn_unused))"
+# would be a reasonable value if targeting gcc/clang. A more portable solution
+# would involve emitting the name of a macro which you define in a
+# platform-specific way. e.g. "MUST_USE_ENUM"
+#
+# Note that this refers to the *output* type. That means this will not apply to an enum
+# with fields, as it will be emitted as a struct. `[struct]must_use` will apply there.
+#
+# default: nothing is emitted for must_use enums
+must_use = "MUST_USE_ENUM"
+
+# Whether enums with fields should generate destructors. This exists so that generic
+# enums can be properly instantiated with payloads that are C++ types with
+# destructors. This isn't necessary for structs because C++ has rules to
+# automatically derive the correct constructors and destructors for those types.
+#
+# Care should be taken with this option, as Rust and C++ cannot
+# properly interoperate with eachother's notions of destructors. Also, this may
+# change the ABI for the type. Either your destructor-full enums must live
+# exclusively within C++, or they must only be passed by-reference between
+# C++ and Rust.
+#
+# default: false
+derive_tagged_enum_destructor = false
+
+# Whether enums with fields should generate copy-constructor. See the discussion on
+# derive_tagged_enum_destructor for why this is both useful and very dangerous.
+#
+# default: false
+derive_tagged_enum_copy_constructor = false
+# Whether enums with fields should generate copy-assignment operators.
+#
+# This depends on also deriving copy-constructors, and it is highly encouraged
+# for this to be set to true.
+#
+# default: false
+derive_tagged_enum_copy_assignment = false
+
+# Whether enums with fields should generate an empty, private destructor.
+# This allows the auto-generated constructor functions to compile, if there are
+# non-trivially constructible members. This falls in the same family of
+# dangerousness as `derive_tagged_enum_copy_constructor` and co.
+#
+# default: false
+private_default_tagged_enum_constructor = false
+
+
+
+
+
+[const]
+# Whether a generated constant can be a static const in C++ mode. I have no
+# idea why you would turn this off.
+#
+# default: true
+allow_static_const = true
+
+# Whether a generated constant can be constexpr in C++ mode.
+#
+# default: false
+
+
+
+
+
+[macro_expansion]
+# Whether bindings should be generated for instances of the bitflags! macro.
+# default: false
+bitflags = true
+
+
+
+
+
+
+# Options for how your Rust library should be parsed
+
+[parse]
+# Whether to parse dependent crates and include their types in the output
+# default: false
+parse_deps = true
+
+# A white list of crate names that are allowed to be parsed. If this is defined,
+# only crates found in this list will ever be parsed.
+#
+# default: there is no whitelist (NOTE: this is the opposite of [])
+include = ["webrender", "webrender_traits"]
+
+# A black list of crate names that are not allowed to be parsed.
+# default: []
+exclude = ["libc"]
+
+# Whether to use a new temporary target directory when running `rustc --pretty=expanded`.
+# This may be required for some build processes.
+#
+# default: false
+clean = false
+
+# Which crates other than the top-level binding crate we should generate
+# bindings for.
+#
+# default: []
+extra_bindings = ["my_awesome_dep"]
+
+[parse.expand]
+# A list of crate names that should be run through `cargo expand` before
+# parsing to expand any macros. Note that if a crate is named here, it
+# will always be parsed, even if the blacklist/whitelist says it shouldn't be.
+#
+# default: []
+crates = ["euclid"]
+
+# If enabled,  use the `--all-features` option when expanding. Ignored when
+# `features` is set. For backwards-compatibility, this is forced on if
+# `expand = ["euclid"]` shorthand is used.
+#
+# default: false
+all_features = false
+
+# When `all_features` is disabled and this is also disabled, use the
+# `--no-default-features` option when expanding.
+#
+# default: true
+default_features = true
+
+# A list of feature names that should be used when running `cargo expand`. This
+# combines with `default_features` like in your `Cargo.toml`. Note that the features
+# listed here are features for the current crate being built, *not* the crates
+# being expanded. The crate's `Cargo.toml` must take care of enabling the
+# appropriate features in its dependencies
+#
+# default: []
+features = ["cbindgen"]
diff --git a/lightning-c-bindings/demo.c b/lightning-c-bindings/demo.c
new file mode 100644 (file)
index 0000000..645c95b
--- /dev/null
@@ -0,0 +1,96 @@
+#include "include/rust_types.h"
+#include "include/lightning.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+void print_log(const void *this_arg, const char *record) {
+       printf("%s", record);
+}
+
+uint32_t get_fee(const void *this_arg, LDKConfirmationTarget target) {
+       if (target == LDKConfirmationTarget_Background) {
+               return 253;
+       } else {
+               return 507;
+       }
+}
+
+void broadcast_tx(const void *this_arg, LDKTransaction tx) {
+       //TODO
+}
+
+LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitor monitor) {
+       return CResult_NoneChannelMonitorUpdateErrZ_ok();
+}
+LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitorUpdate monitor) {
+       return CResult_NoneChannelMonitorUpdateErrZ_ok();
+}
+LDKCVec_MonitorEventZ monitors_pending_monitor_events(const void *this_arg) {
+       LDKCVec_MonitorEventZ empty_htlc_vec = {
+               .data = NULL,
+               .datalen = 0,
+       };
+       return empty_htlc_vec;
+}
+
+int main() {
+       uint8_t node_seed[32];
+       memset(node_seed, 0, 32);
+
+       LDKNetwork net = LDKNetwork_Bitcoin;
+
+       LDKLogger logger = {
+               .this_arg = NULL,
+               .log = print_log,
+               .free = NULL,
+       };
+
+       LDKFeeEstimator fee_est = {
+               .this_arg = NULL,
+               .get_est_sat_per_1000_weight = get_fee,
+               .free = NULL
+       };
+
+       LDKManyChannelMonitor mon = {
+               .this_arg = NULL,
+               .add_monitor = add_channel_monitor,
+               .update_monitor = update_channel_monitor,
+               .get_and_clear_pending_monitor_events = monitors_pending_monitor_events,
+               .free = NULL,
+       };
+
+       LDKBroadcasterInterface broadcast = {
+               broadcast.this_arg = NULL,
+               broadcast.broadcast_transaction = broadcast_tx,
+               .free = NULL,
+       };
+
+       LDKKeysManager keys = KeysManager_new(&node_seed, net, 0, 0);
+       LDKKeysInterface keys_source = KeysManager_as_KeysInterface(&keys);
+
+       LDKUserConfig config = UserConfig_default();
+
+       LDKChannelManager cm = ChannelManager_new(net, fee_est, mon, broadcast, logger, keys_source, config, 0);
+
+       LDKCVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm);
+       assert((unsigned long)channels.data < 4096); // There's an offset, but it should still be an offset against null in the 0 page
+       assert(channels.datalen == 0);
+       CVec_ChannelDetailsZ_free(channels);
+
+       LDKEventsProvider prov = ChannelManager_as_EventsProvider(&cm);
+       LDKCVec_EventZ events = (prov.get_and_clear_pending_events)(prov.this_arg);
+       assert((unsigned long)events.data < 4096); // There's an offset, but it should still be an offset against null in the 0 page
+       assert(events.datalen == 0);
+
+       ChannelManager_free(cm);
+       KeysManager_free(keys);
+
+       // Check that passing empty vecs to rust doesn't blow it up:
+       LDKCVec_MonitorEventZ empty_htlc_vec = {
+               .data = NULL,
+               .datalen = 0,
+       };
+       CVec_MonitorEventZ_free(empty_htlc_vec);
+}
diff --git a/lightning-c-bindings/demo.cpp b/lightning-c-bindings/demo.cpp
new file mode 100644 (file)
index 0000000..7829f02
--- /dev/null
@@ -0,0 +1,556 @@
+extern "C" {
+#include "include/rust_types.h"
+#include "include/lightning.h"
+}
+#include "include/lightningpp.hpp"
+
+#include <assert.h>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include <chrono>
+#include <functional>
+#include <thread>
+#include <mutex>
+#include <vector>
+
+const uint8_t valid_node_announcement[] = {
+       0x94, 0xe4, 0xf5, 0x61, 0x41, 0x24, 0x7d, 0x90, 0x23, 0xa0, 0xc8, 0x34, 0x8c, 0xc4, 0xca, 0x51,
+       0xd8, 0x17, 0x59, 0xff, 0x7d, 0xac, 0x8c, 0x9b, 0x63, 0x29, 0x1c, 0xe6, 0x12, 0x12, 0x93, 0xbd,
+       0x66, 0x4d, 0x6b, 0x9c, 0xfb, 0x35, 0xda, 0x16, 0x06, 0x3d, 0xf0, 0x8f, 0x8a, 0x39, 0x99, 0xa2,
+       0xf2, 0x5d, 0x12, 0x0f, 0x2b, 0x42, 0x1b, 0x8b, 0x9a, 0xfe, 0x33, 0x0c, 0xeb, 0x33, 0x5e, 0x52,
+       0xee, 0x99, 0xa1, 0x07, 0x06, 0xed, 0xf8, 0x48, 0x7a, 0xc6, 0xe5, 0xf5, 0x5e, 0x01, 0x3a, 0x41,
+       0x2f, 0x18, 0x94, 0x8a, 0x3b, 0x0a, 0x52, 0x3f, 0xbf, 0x61, 0xa9, 0xc5, 0x4f, 0x70, 0xee, 0xb8,
+       0x79, 0x23, 0xbb, 0x1a, 0x44, 0x7d, 0x91, 0xe6, 0x2a, 0xbc, 0xa1, 0x07, 0xbc, 0x65, 0x3b, 0x02,
+       0xd9, 0x1d, 0xb2, 0xf2, 0x3a, 0xcb, 0x75, 0x79, 0xc6, 0x66, 0xd8, 0xc1, 0x71, 0x29, 0xdf, 0x04,
+       0x60, 0xf4, 0xbf, 0x07, 0x7b, 0xb9, 0xc2, 0x11, 0x94, 0x6a, 0x28, 0xc2, 0xdd, 0xd8, 0x7b, 0x44,
+       0x8f, 0x08, 0xe3, 0xc8, 0xd8, 0xf4, 0x81, 0xb0, 0x9f, 0x94, 0xcb, 0xc8, 0xc1, 0x3c, 0xc2, 0x6e,
+       0x31, 0x26, 0xfc, 0x33, 0x16, 0x3b, 0xe0, 0xde, 0xa1, 0x16, 0x21, 0x9f, 0x89, 0xdd, 0x97, 0xa4,
+       0x41, 0xf2, 0x9f, 0x19, 0xb1, 0xae, 0x82, 0xf7, 0x85, 0x9a, 0xb7, 0x8f, 0xb7, 0x52, 0x7a, 0x72,
+       0xf1, 0x5e, 0x89, 0xe1, 0x8a, 0xcd, 0x40, 0xb5, 0x8e, 0xc3, 0xca, 0x42, 0x76, 0xa3, 0x6e, 0x1b,
+       0xf4, 0x87, 0x35, 0x30, 0x58, 0x43, 0x04, 0xd9, 0x2c, 0x50, 0x54, 0x55, 0x47, 0x6f, 0x70, 0x9b,
+       0x42, 0x1f, 0x91, 0xfc, 0xa1, 0xdb, 0x72, 0x53, 0x96, 0xc8, 0xe5, 0xcd, 0x0e, 0xcb, 0xa0, 0xfe,
+       0x6b, 0x08, 0x77, 0x48, 0xb7, 0xad, 0x4a, 0x69, 0x7c, 0xdc, 0xd8, 0x04, 0x28, 0x35, 0x9b, 0x73,
+       0x00, 0x00, 0x43, 0x49, 0x7f, 0xd7, 0xf8, 0x26, 0x95, 0x71, 0x08, 0xf4, 0xa3, 0x0f, 0xd9, 0xce,
+       0xc3, 0xae, 0xba, 0x79, 0x97, 0x20, 0x84, 0xe9, 0x0e, 0xad, 0x01, 0xea, 0x33, 0x09, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x5b, 0xe5, 0xe9, 0x47, 0x82,
+       0x09, 0x67, 0x4a, 0x96, 0xe6, 0x0f, 0x1f, 0x03, 0x7f, 0x61, 0x76, 0x54, 0x0f, 0xd0, 0x01, 0xfa,
+       0x1d, 0x64, 0x69, 0x47, 0x70, 0xc5, 0x6a, 0x77, 0x09, 0xc4, 0x2c, 0x03, 0x5c, 0x4e, 0x0d, 0xec,
+       0x72, 0x15, 0xe2, 0x68, 0x33, 0x93, 0x87, 0x30, 0xe5, 0xe5, 0x05, 0xaa, 0x62, 0x50, 0x4d, 0xa8,
+       0x5b, 0xa5, 0x71, 0x06, 0xa4, 0x6b, 0x5a, 0x24, 0x04, 0xfc, 0x9d, 0x8e, 0x02, 0xba, 0x72, 0xa6,
+       0xe8, 0xba, 0x53, 0xe8, 0xb9, 0x71, 0xad, 0x0c, 0x98, 0x23, 0x96, 0x8a, 0xef, 0x4d, 0x78, 0xce,
+       0x8a, 0xf2, 0x55, 0xab, 0x43, 0xdf, 0xf8, 0x30, 0x03, 0xc9, 0x02, 0xfb, 0x8d, 0x02, 0x16, 0x34,
+       0x5b, 0xf8, 0x31, 0x16, 0x4a, 0x03, 0x75, 0x8e, 0xae, 0xa5, 0xe8, 0xb6, 0x6f, 0xee, 0x2b, 0xe7,
+       0x71, 0x0b, 0x8f, 0x19, 0x0e, 0xe8, 0x80, 0x24, 0x90, 0x32, 0xa2, 0x9e, 0xd6, 0x6e
+};
+
+// A simple block containing only one transaction (which is the channel-open transaction for the
+// channel we'll create). This was originally created by printing additional data in a simple
+// rust-lightning unit test.
+const uint8_t channel_open_block[] = {
+       0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0xa2, 0x47, 0xd2, 0xf8, 0xd4, 0xe0, 0x6a, 0x3f, 0xf9, 0x7a, 0x9a, 0x34,
+       0xbb, 0xa9, 0x96, 0xde, 0x63, 0x84, 0x5a, 0xce, 0xcf, 0x98, 0xb8, 0xbb, 0x75, 0x4c, 0x4f, 0x7d,
+       0xee, 0x4c, 0xa9, 0x5f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x01, 0x40, 0x9c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0x20, 0x12, 0x70,
+       0x44, 0x41, 0x40, 0xaf, 0xc5, 0x72, 0x97, 0xc8, 0x69, 0xba, 0x04, 0xdb, 0x28, 0x7b, 0xd7, 0x32,
+       0x07, 0x33, 0x3a, 0x4a, 0xc2, 0xc5, 0x56, 0x06, 0x05, 0x65, 0xd7, 0xa8, 0xcf, 0x01, 0x00, 0x00,
+       0x00, 0x00, 0x00
+};
+
+// The first transaction in the block is header (80 bytes) + transaction count (1 byte) into the block data.
+const uint8_t *channel_open_tx = channel_open_block + 81;
+const uint8_t channel_open_txid[] = {
+       0x5f, 0xa9, 0x4c, 0xee, 0x7d, 0x4f, 0x4c, 0x75, 0xbb, 0xb8, 0x98, 0xcf, 0xce, 0x5a, 0x84, 0x63,
+       0xde, 0x96, 0xa9, 0xbb, 0x34, 0x9a, 0x7a, 0xf9, 0x3f, 0x6a, 0xe0, 0xd4, 0xf8, 0xd2, 0x47, 0xa2
+};
+
+// Two blocks built on top of channel_open_block:
+const uint8_t block_1[] = {
+       0x01, 0x00, 0x00, 0x00, 0x65, 0x8e, 0xf1, 0x90, 0x88, 0xfa, 0x13, 0x9c, 0x6a, 0xea, 0xf7, 0xc1,
+       0x5a, 0xdd, 0x52, 0x4d, 0x3c, 0x48, 0x03, 0xb3, 0x9b, 0x25, 0x4f, 0x02, 0x79, 0x05, 0x90, 0xe0,
+       0xc4, 0x8d, 0xa0, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00
+};
+const uint8_t block_2[] = {
+       0x01, 0x00, 0x00, 0x00, 0xf2, 0x08, 0x87, 0x51, 0xcb, 0xb1, 0x1a, 0x51, 0x76, 0x01, 0x6c, 0x5d,
+       0x76, 0x26, 0x54, 0x6f, 0xd9, 0xbd, 0xa6, 0xa5, 0xe9, 0x4b, 0x21, 0x6e, 0xda, 0xa3, 0x64, 0x23,
+       0xcd, 0xf1, 0xe2, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+       0x00
+};
+
+const LDKThirtyTwoBytes payment_preimage_1 = {
+       .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 }
+};
+const LDKThirtyTwoBytes payment_hash_1 = {
+       .data = {
+               0xdc, 0xb1, 0xac, 0x4a, 0x5d, 0xe3, 0x70, 0xca, 0xd0, 0x91, 0xc1, 0x3f, 0x13, 0xae, 0xe2, 0xf9,
+               0x36, 0xc2, 0x78, 0xfa, 0x05, 0xd2, 0x64, 0x65, 0x3c, 0x0c, 0x13, 0x21, 0x85, 0x2a, 0x35, 0xe8
+       }
+};
+
+void print_log(const void *this_arg, const char *record) {
+       printf("%p - %s\n", this_arg, record);
+}
+
+uint32_t get_fee(const void *this_arg, LDKConfirmationTarget target) {
+       if (target == LDKConfirmationTarget_Background) {
+               return 253;
+       } else {
+               return 507;
+       }
+       // Note that we don't call _free() on target, but that's OK, its unitary
+}
+
+static int num_txs_broadcasted = 0; // Technically a race, but ints are atomic on x86
+void broadcast_tx(const void *this_arg, LDKTransaction tx) {
+       num_txs_broadcasted += 1;
+       //TODO
+}
+
+struct NodeMonitors {
+       std::mutex mut;
+       std::vector<std::pair<LDK::OutPoint, LDK::ChannelMonitor>> mons;
+       LDKChainWatchInterfaceUtil* watch_util;
+       LDKLogger* logger;
+};
+void NodeMonitors_free(void *this_arg) {
+       NodeMonitors* arg = (NodeMonitors*) this_arg;
+       delete arg;
+}
+
+LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitor monitor_arg) {
+       // First bind the args to C++ objects so they auto-free
+       LDK::ChannelMonitor mon(std::move(monitor_arg));
+       LDK::OutPoint funding_txo(std::move(funding_txo_arg));
+
+       NodeMonitors* arg = (NodeMonitors*) this_arg;
+       std::unique_lock<std::mutex> l(arg->mut);
+
+       LDK::ChainWatchInterface watch = ChainWatchInterfaceUtil_as_ChainWatchInterface(arg->watch_util);
+       LDK::C2Tuple_OutPointScriptZ funding_info = ChannelMonitor_get_funding_txo(&mon);
+       LDKThirtyTwoBytes funding_txid;
+       memcpy(funding_txid.data, OutPoint_get_txid(funding_info->a), 32);
+       LDK::C2Tuple_Txidu32Z outpoint(C2Tuple_Txidu32Z_new(funding_txid, OutPoint_get_index(funding_info->a)));
+       watch->install_watch_outpoint(watch->this_arg, outpoint, LDKu8slice {
+               .data = funding_info->b->data,
+               .datalen = funding_info->b->datalen,
+       });
+       watch->install_watch_tx(watch->this_arg, &funding_txid.data, LDKu8slice {
+               .data = funding_info->b->data,
+               .datalen = funding_info->b->datalen,
+       });
+       arg->mons.push_back(std::make_pair(std::move(funding_txo), std::move(mon)));
+       return CResult_NoneChannelMonitorUpdateErrZ_ok();
+}
+static int mons_updated = 0; // Technically a race, but ints are atomic on x86.
+LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo_arg, LDKChannelMonitorUpdate monitor_arg) {
+       // First bind the args to C++ objects so they auto-free
+       LDK::ChannelMonitorUpdate update(std::move(monitor_arg));
+       LDK::OutPoint funding_txo(std::move(funding_txo_arg));
+
+       NodeMonitors* arg = (NodeMonitors*) this_arg;
+       std::unique_lock<std::mutex> l(arg->mut);
+
+       bool updated = false;
+       for (auto& mon : arg->mons) {
+               if (OutPoint_get_index(&mon.first) == OutPoint_get_index(&funding_txo) &&
+                               !memcmp(OutPoint_get_txid(&mon.first), OutPoint_get_txid(&funding_txo), 32)) {
+                       updated = true;
+                       LDKBroadcasterInterface broadcaster = {
+                               .broadcast_transaction = broadcast_tx,
+                       };
+                       LDK::CResult_NoneMonitorUpdateErrorZ res = ChannelMonitor_update_monitor(&mon.second, update, &broadcaster, arg->logger);
+                       assert(res->result_ok);
+               }
+       }
+       assert(updated);
+
+       mons_updated += 1;
+       return CResult_NoneChannelMonitorUpdateErrZ_ok();
+}
+LDKCVec_MonitorEventZ monitors_pending_monitor_events(const void *this_arg) {
+       NodeMonitors* arg = (NodeMonitors*) this_arg;
+       std::unique_lock<std::mutex> l(arg->mut);
+
+       if (arg->mons.size() == 0) {
+               return LDKCVec_MonitorEventZ {
+                       .data = NULL,
+                       .datalen = 0,
+               };
+       } else {
+               // We only ever actually have one channel per node, plus concatenating two
+               // Rust Vecs to each other from C++ will require a bit of effort.
+               assert(arg->mons.size() == 1);
+               return ChannelMonitor_get_and_clear_pending_monitor_events(&arg->mons[0].second);
+       }
+}
+
+uintptr_t sock_send_data(void *this_arg, LDKu8slice data, bool resume_read) {
+       return write((int)((long)this_arg), data.data, data.datalen);
+}
+void sock_disconnect_socket(void *this_arg) {
+       close((int)((long)this_arg));
+}
+bool sock_eq(const void *this_arg, const void *other_arg) {
+       return this_arg == other_arg;
+}
+uint64_t sock_hash(const void *this_arg) {
+       return (uint64_t)this_arg;
+}
+void sock_read_data_thread(int rdfd, LDKSocketDescriptor *peer_descriptor, LDKPeerManager *pm) {
+       unsigned char buf[1024];
+       LDKu8slice data;
+       data.data = buf;
+       ssize_t readlen = 0;
+       while ((readlen = read(rdfd, buf, 1024)) > 0) {
+               data.datalen = readlen;
+               LDK::CResult_boolPeerHandleErrorZ res = PeerManager_read_event(&*pm, peer_descriptor, data);
+               if (!res->result_ok) {
+                       peer_descriptor->disconnect_socket(peer_descriptor->this_arg);
+                       return;
+               }
+               PeerManager_process_events(pm);
+       }
+       PeerManager_socket_disconnected(&*pm, peer_descriptor);
+}
+
+int main() {
+       uint8_t node_seed[32];
+       memset(&node_seed, 0, 32);
+
+       LDKPublicKey null_pk;
+       memset(&null_pk, 0, sizeof(null_pk));
+
+       LDKNetwork network = LDKNetwork_Testnet;
+
+       // Trait implementations:
+       LDKFeeEstimator fee_est {
+               .this_arg = NULL,
+               .get_est_sat_per_1000_weight = get_fee,
+               .free = NULL,
+       };
+
+       LDKBroadcasterInterface broadcast {
+               .this_arg = NULL,
+               .broadcast_transaction = broadcast_tx,
+               .free = NULL,
+       };
+
+       // Instantiate classes for node 1:
+
+       LDKLogger logger1 {
+               .this_arg = (void*)1,
+               .log = print_log,
+               .free = NULL,
+       };
+
+       LDK::ChainWatchInterfaceUtil chain1 = ChainWatchInterfaceUtil_new(network);
+       LDK::BlockNotifier blocks1 = BlockNotifier_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain1));
+
+       LDKManyChannelMonitor mon1 {
+               .this_arg = new NodeMonitors { .watch_util = &chain1, .logger = &logger1 },
+               .add_monitor = add_channel_monitor,
+               .update_monitor = update_channel_monitor,
+               .get_and_clear_pending_monitor_events = monitors_pending_monitor_events,
+               .free = NodeMonitors_free,
+       };
+
+       LDK::KeysManager keys1 = KeysManager_new(&node_seed, network, 0, 0);
+       LDK::KeysInterface keys_source1 = KeysManager_as_KeysInterface(&keys1);
+       LDKSecretKey node_secret1 = keys_source1->get_node_secret(keys_source1->this_arg);
+
+       LDK::UserConfig config1 = UserConfig_default();
+
+       LDK::ChannelManager cm1 = ChannelManager_new(network, fee_est, mon1, broadcast, logger1, KeysManager_as_KeysInterface(&keys1), config1, 0);
+       BlockNotifier_register_listener(&blocks1, ChannelManager_as_ChainListener(&cm1));
+
+       LDK::CVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm1);
+       assert(channels->datalen == 0);
+
+       LDK::NetGraphMsgHandler net_graph1 = NetGraphMsgHandler_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain1), logger1);
+
+       LDK::MessageHandler msg_handler1 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm1), NetGraphMsgHandler_as_RoutingMessageHandler(&net_graph1));
+
+       LDKThirtyTwoBytes random_bytes = keys_source1->get_secure_random_bytes(keys_source1->this_arg);
+       LDK::PeerManager net1 = PeerManager_new(msg_handler1, node_secret1, &random_bytes.data, logger1);
+
+       // Demo getting a channel key and check that its returning real pubkeys:
+       LDK::ChannelKeys chan_keys1 = keys_source1->get_channel_keys(keys_source1->this_arg, false, 42);
+       chan_keys1->set_pubkeys(&chan_keys1); // Make sure pubkeys is defined
+       LDKPublicKey payment_point = ChannelPublicKeys_get_payment_point(&chan_keys1->pubkeys);
+       assert(memcmp(&payment_point, &null_pk, sizeof(null_pk)));
+
+       // Instantiate classes for node 2:
+
+       LDKLogger logger2 {
+               .this_arg = (void*)2,
+               .log = print_log,
+               .free = NULL,
+       };
+
+       LDK::ChainWatchInterfaceUtil chain2 = ChainWatchInterfaceUtil_new(network);
+       LDK::BlockNotifier blocks2 = BlockNotifier_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain2));
+
+       LDKManyChannelMonitor mon2 {
+               .this_arg = new NodeMonitors { .watch_util = &chain2, .logger = &logger2 },
+               .add_monitor = add_channel_monitor,
+               .update_monitor = update_channel_monitor,
+               .get_and_clear_pending_monitor_events = monitors_pending_monitor_events,
+               .free = NodeMonitors_free,
+       };
+
+       memset(&node_seed, 1, 32);
+       LDK::KeysManager keys2 = KeysManager_new(&node_seed, network, 0, 0);
+       LDK::KeysInterface keys_source2 = KeysManager_as_KeysInterface(&keys2);
+       LDKSecretKey node_secret2 = keys_source2->get_node_secret(keys_source2->this_arg);
+
+       LDK::ChannelHandshakeConfig handshake_config2 = ChannelHandshakeConfig_default();
+       ChannelHandshakeConfig_set_minimum_depth(&handshake_config2, 2);
+       LDK::UserConfig config2 = UserConfig_default();
+       UserConfig_set_own_channel_config(&config2, handshake_config2);
+
+       LDK::ChannelManager cm2 = ChannelManager_new(network, fee_est, mon2, broadcast, logger2, KeysManager_as_KeysInterface(&keys2), config2, 0);
+       BlockNotifier_register_listener(&blocks2, ChannelManager_as_ChainListener(&cm2));
+
+       LDK::CVec_ChannelDetailsZ channels2 = ChannelManager_list_channels(&cm2);
+       assert(channels2->datalen == 0);
+
+       LDK::NetGraphMsgHandler net_graph2 = NetGraphMsgHandler_new(ChainWatchInterfaceUtil_as_ChainWatchInterface(&chain2), logger2);
+       LDK::RoutingMessageHandler net_msgs2 = NetGraphMsgHandler_as_RoutingMessageHandler(&net_graph2);
+       LDK::ChannelAnnouncement chan_ann = ChannelAnnouncement_read(LDKu8slice { .data = valid_node_announcement, .datalen = sizeof(valid_node_announcement) });
+       LDK::CResult_boolLightningErrorZ ann_res = net_msgs2->handle_channel_announcement(net_msgs2->this_arg, &chan_ann);
+       assert(ann_res->result_ok);
+
+       LDK::MessageHandler msg_handler2 = MessageHandler_new(ChannelManager_as_ChannelMessageHandler(&cm2), net_msgs2);
+
+       LDKThirtyTwoBytes random_bytes2 = keys_source2->get_secure_random_bytes(keys_source2->this_arg);
+       LDK::PeerManager net2 = PeerManager_new(msg_handler2, node_secret2, &random_bytes2.data, logger2);
+
+       // Open a connection!
+       int pipefds_1_to_2[2];
+       int pipefds_2_to_1[2];
+       assert(!pipe(pipefds_1_to_2));
+       assert(!pipe(pipefds_2_to_1));
+
+       LDKSocketDescriptor sock1 {
+               .this_arg = (void*)(long)pipefds_1_to_2[1],
+               .send_data = sock_send_data,
+               .disconnect_socket = sock_disconnect_socket,
+               .eq = sock_eq,
+               .hash = sock_hash,
+               .clone = NULL,
+               .free = NULL,
+       };
+
+       LDKSocketDescriptor sock2 {
+               .this_arg = (void*)(long)pipefds_2_to_1[1],
+               .send_data = sock_send_data,
+               .disconnect_socket = sock_disconnect_socket,
+               .eq = sock_eq,
+               .hash = sock_hash,
+               .clone = NULL,
+               .free = NULL,
+       };
+
+       std::thread t1(&sock_read_data_thread, pipefds_2_to_1[0], &sock1, &net1);
+       std::thread t2(&sock_read_data_thread, pipefds_1_to_2[0], &sock2, &net2);
+
+       // Note that we have to bind the result to a C++ class to make sure it gets free'd
+       LDK::CResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&net1, ChannelManager_get_our_node_id(&cm2), sock1);
+       assert(con_res->result_ok);
+       LDK::CResult_NonePeerHandleErrorZ con_res2 = PeerManager_new_inbound_connection(&net2, sock2);
+       assert(con_res2->result_ok);
+
+       auto writelen = write(pipefds_1_to_2[1], con_res->contents.result->data, con_res->contents.result->datalen);
+       assert(writelen > 0 && uint64_t(writelen) == con_res->contents.result->datalen);
+
+       while (true) {
+               // Wait for the initial handshakes to complete...
+               LDK::CVec_PublicKeyZ peers_1 = PeerManager_get_peer_node_ids(&net1);
+               LDK::CVec_PublicKeyZ peers_2 = PeerManager_get_peer_node_ids(&net2);
+               if (peers_1->datalen == 1 && peers_2->datalen ==1) { break; }
+               std::this_thread::yield();
+       }
+
+       // Note that we have to bind the result to a C++ class to make sure it gets free'd
+       LDK::CResult_NoneAPIErrorZ res = ChannelManager_create_channel(&cm1, ChannelManager_get_our_node_id(&cm2), 40000, 1000, 42, config1);
+       assert(res->result_ok);
+       PeerManager_process_events(&net1);
+
+       LDK::CVec_ChannelDetailsZ new_channels = ChannelManager_list_channels(&cm1);
+       assert(new_channels->datalen == 1);
+       LDKPublicKey chan_open_pk = ChannelDetails_get_remote_network_id(&new_channels->data[0]);
+       assert(!memcmp(chan_open_pk.compressed_form, ChannelManager_get_our_node_id(&cm2).compressed_form, 33));
+
+       while (true) {
+               LDK::CVec_ChannelDetailsZ new_channels_2 = ChannelManager_list_channels(&cm2);
+               if (new_channels_2->datalen == 1) {
+                       // Sample getting our counterparty's init features (which used to be hard to do without a memory leak):
+                       const LDK::InitFeatures init_feats = ChannelDetails_get_counterparty_features(&new_channels_2->data[0]);
+                       assert(init_feats->inner != NULL);
+                       break;
+               }
+               std::this_thread::yield();
+       }
+
+       LDKEventsProvider ev1 = ChannelManager_as_EventsProvider(&cm1);
+       while (true) {
+               LDK::CVec_EventZ events = ev1.get_and_clear_pending_events(ev1.this_arg);
+               if (events->datalen == 1) {
+                       assert(events->data[0].tag == LDKEvent_FundingGenerationReady);
+                       assert(events->data[0].funding_generation_ready.user_channel_id == 42);
+                       assert(events->data[0].funding_generation_ready.channel_value_satoshis == 40000);
+                       assert(events->data[0].funding_generation_ready.output_script.datalen == 34);
+                       assert(!memcmp(events->data[0].funding_generation_ready.output_script.data, channel_open_tx + 58, 34));
+                       LDKThirtyTwoBytes txid;
+                       for (int i = 0; i < 32; i++) { txid.data[i] = channel_open_txid[31-i]; }
+                       LDK::OutPoint outp = OutPoint_new(txid, 0);
+                       ChannelManager_funding_transaction_generated(&cm1, &events->data[0].funding_generation_ready.temporary_channel_id.data, outp);
+                       break;
+               }
+               std::this_thread::yield();
+       }
+
+       // We observe when the funding signed messages have been exchanged by
+       // waiting for two monitors to be registered.
+       PeerManager_process_events(&net1);
+       while (true) {
+               LDK::CVec_EventZ events = ev1.get_and_clear_pending_events(ev1.this_arg);
+               if (events->datalen == 1) {
+                       assert(events->data[0].tag == LDKEvent_FundingBroadcastSafe);
+                       assert(events->data[0].funding_broadcast_safe.user_channel_id == 42);
+                       break;
+               }
+               std::this_thread::yield();
+       }
+
+       BlockNotifier_block_connected(&blocks1, LDKu8slice { .data = channel_open_block, .datalen = sizeof(channel_open_block) }, 1);
+       BlockNotifier_block_connected(&blocks2, LDKu8slice { .data = channel_open_block, .datalen = sizeof(channel_open_block) }, 1);
+
+       BlockNotifier_block_connected(&blocks1, LDKu8slice { .data = block_1, .datalen = sizeof(block_1) }, 2);
+       BlockNotifier_block_connected(&blocks2, LDKu8slice { .data = block_1, .datalen = sizeof(block_1) }, 2);
+
+       BlockNotifier_block_connected(&blocks1, LDKu8slice { .data = block_2, .datalen = sizeof(block_2) }, 3);
+       BlockNotifier_block_connected(&blocks2, LDKu8slice { .data = block_2, .datalen = sizeof(block_2) }, 3);
+
+       PeerManager_process_events(&net1);
+       PeerManager_process_events(&net2);
+
+       // Now send funds from 1 to 2!
+       while (true) {
+               LDK::CVec_ChannelDetailsZ outbound_channels = ChannelManager_list_usable_channels(&cm1);
+               if (outbound_channels->datalen == 1) {
+                       const LDKChannelDetails *channel = &outbound_channels->data[0];
+                       // Note that the channel ID is the same as the channel txid reversed as the output index is 0
+                       uint8_t expected_chan_id[32];
+                       for (int i = 0; i < 32; i++) { expected_chan_id[i] = channel_open_txid[31-i]; }
+                       assert(!memcmp(ChannelDetails_get_channel_id(channel), expected_chan_id, 32));
+                       assert(!memcmp(ChannelDetails_get_remote_network_id(channel).compressed_form,
+                                       ChannelManager_get_our_node_id(&cm2).compressed_form, 33));
+                       assert(ChannelDetails_get_channel_value_satoshis(channel) == 40000);
+                       // We opened the channel with 1000 push_msat:
+                       assert(ChannelDetails_get_outbound_capacity_msat(channel) == 40000*1000 - 1000);
+                       assert(ChannelDetails_get_inbound_capacity_msat(channel) == 1000);
+                       assert(ChannelDetails_get_is_live(channel));
+                       break;
+               }
+               std::this_thread::yield();
+       }
+
+       LDK::CVec_ChannelDetailsZ outbound_channels = ChannelManager_list_usable_channels(&cm1);
+       LDKThirtyTwoBytes payment_secret;
+       memset(payment_secret.data, 0x42, 32);
+       {
+               LDK::LockedNetworkGraph graph_2_locked = NetGraphMsgHandler_read_locked_graph(&net_graph2);
+               LDK::NetworkGraph graph_2_ref = LockedNetworkGraph_graph(&graph_2_locked);
+               LDK::CResult_RouteLightningErrorZ route = get_route(ChannelManager_get_our_node_id(&cm1), &graph_2_ref, ChannelManager_get_our_node_id(&cm2), &outbound_channels, LDKCVec_RouteHintZ {
+                               .data = NULL, .datalen = 0
+                       }, 5000, 10, logger1);
+               assert(route->result_ok);
+               LDK::CResult_NonePaymentSendFailureZ send_res = ChannelManager_send_payment(&cm1, route->contents.result, payment_hash_1, payment_secret);
+               assert(send_res->result_ok);
+       }
+
+       mons_updated = 0;
+       PeerManager_process_events(&net1);
+       while (mons_updated != 4) {
+               std::this_thread::yield();
+       }
+
+       // Check that we received the payment!
+       LDKEventsProvider ev2 = ChannelManager_as_EventsProvider(&cm2);
+       while (true) {
+               LDK::CVec_EventZ events = ev2.get_and_clear_pending_events(ev2.this_arg);
+               if (events->datalen == 1) {
+                       assert(events->data[0].tag == LDKEvent_PendingHTLCsForwardable);
+                       break;
+               }
+               std::this_thread::yield();
+       }
+       ChannelManager_process_pending_htlc_forwards(&cm2);
+       PeerManager_process_events(&net2);
+
+       mons_updated = 0;
+       {
+               LDK::CVec_EventZ events = ev2.get_and_clear_pending_events(ev2.this_arg);
+               assert(events->datalen == 1);
+               assert(events->data[0].tag == LDKEvent_PaymentReceived);
+               assert(!memcmp(events->data[0].payment_received.payment_hash.data, payment_hash_1.data, 32));
+               assert(!memcmp(events->data[0].payment_received.payment_secret.data, payment_secret.data, 32));
+               assert(events->data[0].payment_received.amt == 5000);
+               assert(ChannelManager_claim_funds(&cm2, payment_preimage_1, payment_secret, 5000));
+       }
+       PeerManager_process_events(&net2);
+       // Wait until we've passed through a full set of monitor updates (ie new preimage + CS/RAA messages)
+       while (mons_updated != 5) {
+               std::this_thread::yield();
+       }
+       {
+               LDK::CVec_EventZ events = ev1.get_and_clear_pending_events(ev1.this_arg);
+               assert(events->datalen == 1);
+               assert(events->data[0].tag == LDKEvent_PaymentSent);
+               assert(!memcmp(events->data[0].payment_sent.payment_preimage.data, payment_preimage_1.data, 32));
+       }
+
+       // Close the channel.
+       uint8_t chan_id[32];
+       for (int i = 0; i < 32; i++) { chan_id[i] = channel_open_txid[31-i]; }
+       LDK::CResult_NoneAPIErrorZ close_res = ChannelManager_close_channel(&cm1, &chan_id);
+       assert(close_res->result_ok);
+       PeerManager_process_events(&net1);
+       num_txs_broadcasted = 0;
+       while (num_txs_broadcasted != 2) {
+               std::this_thread::yield();
+       }
+       LDK::CVec_ChannelDetailsZ chans_after_close1 = ChannelManager_list_channels(&cm1);
+       assert(chans_after_close1->datalen == 0);
+       LDK::CVec_ChannelDetailsZ chans_after_close2 = ChannelManager_list_channels(&cm2);
+       assert(chans_after_close2->datalen == 0);
+
+       close(pipefds_1_to_2[0]);
+       close(pipefds_2_to_1[0]);
+       close(pipefds_1_to_2[1]);
+       close(pipefds_2_to_1[1]);
+       t1.join();
+       t2.join();
+
+       // Few extra random tests:
+       LDKSecretKey sk;
+       memset(&sk, 42, 32);
+       LDKC2Tuple_u64u64Z kdiv_params;
+       kdiv_params.a = (uint64_t*) malloc(8);
+       kdiv_params.b = (uint64_t*) malloc(8);
+       *kdiv_params.a = 42;
+       *kdiv_params.b = 42;
+       LDK::InMemoryChannelKeys keys = InMemoryChannelKeys_new(sk, sk, sk, sk, sk, random_bytes, 42, kdiv_params);
+}
diff --git a/lightning-c-bindings/src/bitcoin/mod.rs b/lightning-c-bindings/src/bitcoin/mod.rs
new file mode 100644 (file)
index 0000000..a61610b
--- /dev/null
@@ -0,0 +1 @@
+pub mod network;
diff --git a/lightning-c-bindings/src/bitcoin/network.rs b/lightning-c-bindings/src/bitcoin/network.rs
new file mode 100644 (file)
index 0000000..13799c0
--- /dev/null
@@ -0,0 +1,18 @@
+use bitcoin::network::constants::Network as BitcoinNetwork;
+
+#[repr(C)]
+pub enum Network {
+       Bitcoin,
+       Testnet,
+       Regtest,
+}
+
+impl Network {
+       pub(crate) fn into_bitcoin(&self) -> BitcoinNetwork {
+               match self {
+                       Network::Bitcoin => BitcoinNetwork::Bitcoin,
+                       Network::Testnet => BitcoinNetwork::Testnet,
+                       Network::Regtest => BitcoinNetwork::Regtest,
+               }
+       }
+}
diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs
new file mode 100644 (file)
index 0000000..be697d4
--- /dev/null
@@ -0,0 +1,419 @@
+pub mod derived;
+
+use bitcoin::Script as BitcoinScript;
+use bitcoin::Transaction as BitcoinTransaction;
+use bitcoin::secp256k1::key::PublicKey as SecpPublicKey;
+use bitcoin::secp256k1::key::SecretKey as SecpSecretKey;
+use bitcoin::secp256k1::Signature as SecpSignature;
+use bitcoin::secp256k1::Error as SecpError;
+
+#[derive(Clone)]
+#[repr(C)]
+pub struct PublicKey {
+       pub compressed_form: [u8; 33],
+}
+impl PublicKey {
+       pub(crate) fn from_rust(pk: &SecpPublicKey) -> Self {
+               Self {
+                       compressed_form: pk.serialize(),
+               }
+       }
+       pub(crate) fn into_rust(&self) -> SecpPublicKey {
+               SecpPublicKey::from_slice(&self.compressed_form).unwrap()
+       }
+       pub(crate) fn is_null(&self) -> bool { self.compressed_form[..] == [0; 33][..] }
+       pub(crate) fn null() -> Self { Self { compressed_form: [0; 33] } }
+}
+
+#[repr(C)]
+pub struct SecretKey {
+       pub bytes: [u8; 32],
+}
+impl SecretKey {
+       // from_rust isn't implemented for a ref since we just return byte array refs directly
+       pub(crate) fn from_rust(sk: SecpSecretKey) -> Self {
+               let mut bytes = [0; 32];
+               bytes.copy_from_slice(&sk[..]);
+               Self { bytes }
+       }
+       pub(crate) fn into_rust(&self) -> SecpSecretKey {
+               SecpSecretKey::from_slice(&self.bytes).unwrap()
+       }
+}
+
+#[repr(C)]
+pub struct Signature {
+       pub compact_form: [u8; 64],
+}
+impl Signature {
+       pub(crate) fn from_rust(pk: &SecpSignature) -> Self {
+               Self {
+                       compact_form: pk.serialize_compact(),
+               }
+       }
+       pub(crate) fn into_rust(&self) -> SecpSignature {
+               SecpSignature::from_compact(&self.compact_form).unwrap()
+       }
+       pub(crate) fn is_null(&self) -> bool { self.compact_form[..] == [0; 64][..] }
+       pub(crate) fn null() -> Self { Self { compact_form: [0; 64] } }
+}
+
+#[repr(C)]
+pub enum Secp256k1Error {
+       IncorrectSignature,
+       InvalidMessage,
+       InvalidPublicKey,
+       InvalidSignature,
+       InvalidSecretKey,
+       InvalidRecoveryId,
+       InvalidTweak,
+       NotEnoughMemory,
+       CallbackPanicked,
+}
+impl Secp256k1Error {
+       pub(crate) fn from_rust(err: SecpError) -> Self {
+               match err {
+                       SecpError::IncorrectSignature => Secp256k1Error::IncorrectSignature,
+                       SecpError::InvalidMessage => Secp256k1Error::InvalidMessage,
+                       SecpError::InvalidPublicKey => Secp256k1Error::InvalidPublicKey,
+                       SecpError::InvalidSignature => Secp256k1Error::InvalidSignature,
+                       SecpError::InvalidSecretKey => Secp256k1Error::InvalidSecretKey,
+                       SecpError::InvalidRecoveryId => Secp256k1Error::InvalidRecoveryId,
+                       SecpError::InvalidTweak => Secp256k1Error::InvalidTweak,
+                       SecpError::NotEnoughMemory => Secp256k1Error::NotEnoughMemory,
+               }
+       }
+}
+
+#[repr(C)]
+/// A reference to a serialized transaction, in (pointer, length) form.
+/// This type does *not* own its own memory, so access to it after, eg, the call in which it was
+/// provided to you are invalid.
+pub struct Transaction {
+       pub data: *const u8,
+       pub datalen: usize,
+}
+impl Transaction {
+       pub(crate) fn into_bitcoin(&self) -> BitcoinTransaction {
+               if self.datalen == 0 { panic!("0-length buffer can never represent a valid Transaction"); }
+               ::bitcoin::consensus::encode::deserialize(unsafe { std::slice::from_raw_parts(self.data, self.datalen) }).unwrap()
+       }
+       pub(crate) fn from_slice(s: &[u8]) -> Self {
+               Self {
+                       data: s.as_ptr(),
+                       datalen: s.len(),
+               }
+       }
+}
+
+#[repr(C)]
+#[derive(Clone)]
+/// A transaction output including a scriptPubKey and value.
+/// This type *does* own its own memory, so must be free'd appropriately.
+pub struct TxOut {
+       pub script_pubkey: derived::CVec_u8Z,
+       pub value: u64,
+}
+
+impl TxOut {
+       pub(crate) fn into_rust(mut self) -> ::bitcoin::blockdata::transaction::TxOut {
+               ::bitcoin::blockdata::transaction::TxOut {
+                       script_pubkey: self.script_pubkey.into_rust().into(),
+                       value: self.value,
+               }
+       }
+       pub(crate) fn from_rust(txout: ::bitcoin::blockdata::transaction::TxOut) -> Self {
+               Self {
+                       script_pubkey: CVecTempl::from(txout.script_pubkey.into_bytes()),
+                       value: txout.value
+               }
+       }
+}
+#[no_mangle]
+pub extern "C" fn TxOut_free(_res: TxOut) { }
+
+#[repr(C)]
+pub struct u8slice {
+       pub data: *const u8,
+       pub datalen: usize
+}
+impl u8slice {
+       pub(crate) fn from_slice(s: &[u8]) -> Self {
+               Self {
+                       data: s.as_ptr(),
+                       datalen: s.len(),
+               }
+       }
+       pub(crate) fn to_slice(&self) -> &[u8] {
+               if self.datalen == 0 { return &[]; }
+               unsafe { std::slice::from_raw_parts(self.data, self.datalen) }
+       }
+}
+
+#[repr(C)]
+pub struct usizeslice {
+       pub data: *const usize,
+       pub datalen: usize
+}
+impl usizeslice {
+       pub(crate) fn from_slice(s: &[usize]) -> Self {
+               Self {
+                       data: s.as_ptr(),
+                       datalen: s.len(),
+               }
+       }
+       pub(crate) fn to_slice(&self) -> &[usize] {
+               if self.datalen == 0 { return &[]; }
+               unsafe { std::slice::from_raw_parts(self.data, self.datalen) }
+       }
+}
+
+#[repr(C)]
+#[derive(Copy, Clone)]
+/// Arbitrary 32 bytes, which could represent one of a few different things. You probably want to
+/// look up the corresponding function in rust-lightning's docs.
+pub struct ThirtyTwoBytes {
+       pub data: [u8; 32],
+}
+impl ThirtyTwoBytes {
+       pub(crate) fn null() -> Self {
+               Self { data: [0; 32] }
+       }
+}
+
+#[repr(C)]
+pub struct ThreeBytes { pub data: [u8; 3], }
+#[derive(Clone)]
+#[repr(C)]
+pub struct FourBytes { pub data: [u8; 4], }
+#[derive(Clone)]
+#[repr(C)]
+pub struct TenBytes { pub data: [u8; 10], }
+#[derive(Clone)]
+#[repr(C)]
+pub struct SixteenBytes { pub data: [u8; 16], }
+
+pub(crate) struct VecWriter(pub Vec<u8>);
+impl lightning::util::ser::Writer for VecWriter {
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               self.0.extend_from_slice(buf);
+               Ok(())
+       }
+       fn size_hint(&mut self, size: usize) {
+               self.0.reserve_exact(size);
+       }
+}
+pub(crate) fn serialize_obj<I: lightning::util::ser::Writeable>(i: &I) -> derived::CVec_u8Z {
+       let mut out = VecWriter(Vec::new());
+       i.write(&mut out).unwrap();
+       CVecTempl::from(out.0)
+}
+pub(crate) fn deserialize_obj<I: lightning::util::ser::Readable>(s: u8slice) -> Result<I, lightning::ln::msgs::DecodeError> {
+       I::read(&mut s.to_slice())
+}
+
+#[repr(C)]
+#[derive(Copy, Clone)]
+/// A Rust str object, ie a reference to a UTF8-valid string.
+/// This is *not* null-terminated so cannot be used directly as a C string!
+pub struct Str {
+       pub chars: *const u8,
+       pub len: usize
+}
+impl Into<Str> for &'static str {
+       fn into(self) -> Str {
+               Str { chars: self.as_ptr(), len: self.len() }
+       }
+}
+impl Into<&'static str> for Str {
+       fn into(self) -> &'static str {
+               if self.len == 0 { return ""; }
+               std::str::from_utf8(unsafe { std::slice::from_raw_parts(self.chars, self.len) }).unwrap()
+       }
+}
+
+// Note that the C++ headers memset(0) all the Templ types to avoid deallocation!
+// Thus, they must gracefully handle being completely null in _free.
+
+// TODO: Integer/bool primitives should avoid the pointer indirection for underlying types
+// everywhere in the containers.
+
+#[repr(C)]
+pub union CResultPtr<O, E> {
+       pub result: *mut O,
+       pub err: *mut E,
+}
+#[repr(C)]
+pub struct CResultTempl<O, E> {
+       pub contents: CResultPtr<O, E>,
+       pub result_ok: bool,
+}
+impl<O, E> CResultTempl<O, E> {
+       pub(crate) extern "C" fn ok(o: O) -> Self {
+               CResultTempl {
+                       contents: CResultPtr {
+                               result: Box::into_raw(Box::new(o)),
+                       },
+                       result_ok: true,
+               }
+       }
+       pub(crate) extern "C" fn err(e: E) -> Self {
+               CResultTempl {
+                       contents: CResultPtr {
+                               err: Box::into_raw(Box::new(e)),
+                       },
+                       result_ok: false,
+               }
+       }
+}
+pub extern "C" fn CResultTempl_free<O, E>(_res: CResultTempl<O, E>) { }
+impl<O, E> Drop for CResultTempl<O, E> {
+       fn drop(&mut self) {
+               if self.result_ok {
+                       if unsafe { !self.contents.result.is_null() } {
+                               unsafe { Box::from_raw(self.contents.result) };
+                       }
+               } else if unsafe { !self.contents.err.is_null() } {
+                       unsafe { Box::from_raw(self.contents.err) };
+               }
+       }
+}
+
+#[repr(C)]
+pub struct CVecTempl<T> {
+       pub data: *mut T,
+       pub datalen: usize
+}
+impl<T> CVecTempl<T> {
+       pub(crate) fn into_rust(&mut self) -> Vec<T> {
+               if self.datalen == 0 { return Vec::new(); }
+               let ret = unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) }.into();
+               self.data = std::ptr::null_mut();
+               self.datalen = 0;
+               ret
+       }
+       pub(crate) fn as_slice(&self) -> &[T] {
+               unsafe { std::slice::from_raw_parts_mut(self.data, self.datalen) }
+       }
+}
+impl<T> From<Vec<T>> for CVecTempl<T> {
+       fn from(v: Vec<T>) -> Self {
+               let datalen = v.len();
+               let data = Box::into_raw(v.into_boxed_slice());
+               CVecTempl { datalen, data: unsafe { (*data).as_mut_ptr() } }
+       }
+}
+pub extern "C" fn CVecTempl_free<T>(_res: CVecTempl<T>) { }
+impl<T> Drop for CVecTempl<T> {
+       fn drop(&mut self) {
+               if self.datalen == 0 { return; }
+               unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) };
+       }
+}
+impl<T: Clone> Clone for CVecTempl<T> {
+       fn clone(&self) -> Self {
+               let mut res = Vec::new();
+               if self.datalen == 0 { return Self::from(res); }
+               res.clone_from_slice(unsafe { std::slice::from_raw_parts_mut(self.data, self.datalen) });
+               Self::from(res)
+       }
+}
+
+#[repr(C)]
+pub struct C2TupleTempl<A, B> {
+       pub a: *mut A,
+       pub b: *mut B,
+}
+impl<A, B> From<(A, B)> for C2TupleTempl<A, B> {
+       fn from(tup: (A, B)) -> Self {
+               Self {
+                       a: Box::into_raw(Box::new(tup.0)),
+                       b: Box::into_raw(Box::new(tup.1)),
+               }
+       }
+}
+impl<A, B> C2TupleTempl<A, B> {
+       pub(crate) fn to_rust(mut self) -> (A, B) {
+               let res = (unsafe { *Box::from_raw(self.a) }, unsafe { *Box::from_raw(self.b) });
+               self.a = std::ptr::null_mut();
+               self.b = std::ptr::null_mut();
+               res
+       }
+}
+pub extern "C" fn C2TupleTempl_free<A, B>(_res: C2TupleTempl<A, B>) { }
+impl<A, B> Drop for C2TupleTempl<A, B> {
+       fn drop(&mut self) {
+               if !self.a.is_null() {
+                       unsafe { Box::from_raw(self.a) };
+               }
+               if !self.b.is_null() {
+                       unsafe { Box::from_raw(self.b) };
+               }
+       }
+}
+impl <A: Clone, B: Clone> Clone for C2TupleTempl<A, B> {
+       fn clone(&self) -> Self {
+               Self {
+                       a: Box::into_raw(Box::new(unsafe { &*self.a }.clone())),
+                       b: Box::into_raw(Box::new(unsafe { &*self.b }.clone()))
+               }
+       }
+}
+
+#[repr(C)]
+pub struct C3TupleTempl<A, B, C> {
+       pub a: *mut A,
+       pub b: *mut B,
+       pub c: *mut C,
+}
+impl<A, B, C> From<(A, B, C)> for C3TupleTempl<A, B, C> {
+       fn from(tup: (A, B, C)) -> Self {
+               Self {
+                       a: Box::into_raw(Box::new(tup.0)),
+                       b: Box::into_raw(Box::new(tup.1)),
+                       c: Box::into_raw(Box::new(tup.2)),
+               }
+       }
+}
+impl<A, B, C> C3TupleTempl<A, B, C> {
+       pub(crate) fn to_rust(mut self) -> (A, B, C) {
+               let res = (unsafe { *Box::from_raw(self.a) }, unsafe { *Box::from_raw(self.b) }, unsafe { *Box::from_raw(self.c) });
+               self.a = std::ptr::null_mut();
+               self.b = std::ptr::null_mut();
+               self.c = std::ptr::null_mut();
+               res
+       }
+}
+pub extern "C" fn C3TupleTempl_free<A, B, C>(_res: C3TupleTempl<A, B, C>) { }
+impl<A, B, C> Drop for C3TupleTempl<A, B, C> {
+       fn drop(&mut self) {
+               if !self.a.is_null() {
+                       unsafe { Box::from_raw(self.a) };
+               }
+               if !self.b.is_null() {
+                       unsafe { Box::from_raw(self.b) };
+               }
+               if !self.c.is_null() {
+                       unsafe { Box::from_raw(self.c) };
+               }
+       }
+}
+
+/// Utility to make it easy to set a pointer to null and get its original value in line.
+pub(crate) trait TakePointer<T> {
+       fn take_ptr(&mut self) -> T;
+}
+impl<T> TakePointer<*const T> for *const T {
+       fn take_ptr(&mut self) -> *const T {
+               let ret = *self;
+               *self = std::ptr::null();
+               ret
+       }
+}
+impl<T> TakePointer<*mut T> for *mut T {
+       fn take_ptr(&mut self) -> *mut T {
+               let ret = *self;
+               *self = std::ptr::null_mut();
+               ret
+       }
+}