lnsocket

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

commit 507b9017f264cecca8d4d4a1de7e1b24ad6bdb45
parent bfccc580e5e7580f3b33d4a0c7080a29dae2054f
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 15 Jul 2022 12:57:54 -0700

js: add setkey

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mlnsocket_lib.js | 52+++++++++++++++++++++++++++++++++++++++++++++++++++-
Mtest/connection.js | 11+++++++++++
2 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/lnsocket_lib.js b/lnsocket_lib.js @@ -72,7 +72,8 @@ async function lnsocket_init() { const lnsocket_act_one = module.cwrap("lnsocket_act_one", "number", ["number", "string"]) const lnsocket_act_two = module.cwrap("lnsocket_act_two", "number", ["number", "array"]) const lnsocket_print_errors = module.cwrap("lnsocket_print_errors", "int") - const lnsocket_genkey = module.cwrap("lnsocket_genkey", "int") + const lnsocket_genkey = module.cwrap("lnsocket_genkey", null, ["number"]) + const lnsocket_setkey = module.cwrap("lnsocket_setkey", "number", ["number", "array"]) const lnsocket_make_default_initmsg = module.cwrap("lnsocket_make_default_initmsg", "int", ["int", "int"]) const lnsocket_make_ping_msg = module.cwrap("lnsocket_make_ping_msg", "int", ["int", "int", "int", "int"]) const commando_make_rpc_msg = module.cwrap("commando_make_rpc_msg", "int", ["string", "string", "string", "number", "int", "int"]) @@ -108,6 +109,46 @@ async function lnsocket_init() { module._free(buf); } + function char_to_hex(cstr) { + const c = cstr.charCodeAt(0) + // c >= 0 && c <= 9 + if (c >= 48 && c <= 57) { + return c - 48; + } + // c >= a && c <= f + if (c >= 97 && c <= 102) { + return c - 97 + 10; + } + // c >= A && c <= F + if (c >= 65 && c <= 70) { + return c - 65 + 10; + } + return -1; + } + + + function hex_decode(str, buflen) + { + let bufsize = buflen || 33 + let c1, c2 + let i = 0 + let j = 0 + let buf = new Uint8Array(bufsize) + let slen = str.length + while (slen > 1) { + if (-1==(c1 = char_to_hex(str[j])) || -1==(c2 = char_to_hex(str[j+1]))) + return null; + if (!bufsize) + return null; + j += 2 + slen -= 2 + buf[i++] = (c1 << 4) | c2 + bufsize--; + } + + return buf + } + function wasm_alloc(len) { const buf = module._malloc(len); module.HEAPU8.set(Uint8Array, buf); @@ -153,6 +194,15 @@ async function lnsocket_init() { lnsocket_genkey(this.ln) } + LNSocket.prototype.setkeyraw = function lnsocket_setkeyraw(rawkey) { + return lnsocket_setkey(this.ln, rawkey) + } + + LNSocket.prototype.setkey = function _lnsocket_setkey(key) { + const rawkey = hex_decode(key) + return this.setkeyraw(rawkey) + } + LNSocket.prototype.act_one_data = function _lnsocket_act_one(node_id) { const act_one_ptr = lnsocket_act_one(this.ln, node_id) if (act_one_ptr === 0) diff --git a/test/connection.js b/test/connection.js @@ -4,9 +4,20 @@ const t = require('tap') const LNSocket = require('../') t.test('connection works', async (t) => { + t.plan(1) + const ln = await LNSocket() ln.genkey() + + const badkey = "0000000000000000000000000000000000000000000000000000000000000000" + if (ln.setkey(badkey)) + t.ok(false, "expected zero setkey to fail") + + const goodkey = "9a5419eacf204a917cf06d5f830e48bc1fcb3e33bb60932e837697c8b7718f6f" + if (!ln.setkey(goodkey)) + t.ok(false, "expected goodkey to succeed") + await ln.connect_and_init("03f3c108ccd536b8526841f0a5c58212bb9e6584a1eb493080e7c1cc34f82dad71", "24.84.152.187") t.ok(ln.connected, "connection works")