lnsocket

A minimal C library for connecting to the lightning network
git clone git://jb55.com/lnsocket
Log | Files | Refs | Submodules | README | LICENSE

commit c72ee2339079697ad23696f0f1b71acb24b2f00f
parent c0ae302f0b6492bc07c3b58bb22cef43ee96d3da
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 15 Jul 2022 12:07:14 -0700

api: add lnsocket_setkey

Changelog-Added: Added lnsocket_setkey for setting a specific node secret key
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mlnsocket.c | 13+++++++++++++
Mlnsocket.h | 1+
Mtest.c | 13++++++++++++-
3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/lnsocket.c b/lnsocket.c @@ -717,6 +717,19 @@ void lnsocket_genkey(struct lnsocket *ln) ln->key = generate_key(ln->secp); } +int lnsocket_setkey(struct lnsocket *ln, const unsigned char key[32]) +{ + struct keypair k; + memcpy(k.priv.secret.data, key, 32); + + if (!secp256k1_ec_pubkey_create(ln->secp, &k.pub.pubkey, k.priv.secret.data)) + return 0; + + ln->key = k; + + return 1; +} + void lnsocket_print_errors(struct lnsocket *ln) { print_error_backtrace(&ln->errs); diff --git a/lnsocket.h b/lnsocket.h @@ -87,6 +87,7 @@ int lnsocket_recv(struct lnsocket *, unsigned short *msg_type, unsigned char **p void* EXPORT lnsocket_secp(struct lnsocket *); void EXPORT lnsocket_genkey(struct lnsocket *); +int EXPORT lnsocket_setkey(struct lnsocket *, const unsigned char key[32]); void EXPORT lnsocket_destroy(struct lnsocket *); void EXPORT lnsocket_print_errors(struct lnsocket *); int EXPORT lnsocket_make_default_initmsg(unsigned char *msgbuf, int buflen); diff --git a/test.c b/test.c @@ -4,6 +4,7 @@ #include "typedefs.h" #include <stdio.h> #include <assert.h> +#include <sodium/randombytes.h> static void print_data(unsigned char *buf, int len) { @@ -21,13 +22,23 @@ int main(int argc, const char *argv[]) u8 *buf; struct lnsocket *ln; + static unsigned char key[32] = {0}; + u16 len; int ok = 1; ln = lnsocket_create(); assert(ln); - lnsocket_genkey(ln); + if (lnsocket_setkey(ln, key)) { + // key with 0s should be an error + ok = 0; + goto done; + } + + randombytes_buf(key, sizeof(key)); + if (!(ok = lnsocket_setkey(ln, key))) + goto done; const char *nodeid = "03f3c108ccd536b8526841f0a5c58212bb9e6584a1eb493080e7c1cc34f82dad71"; if (!(ok = lnsocket_connect(ln, nodeid, "24.84.152.187")))