-
Notifications
You must be signed in to change notification settings - Fork 6
/
node.js
394 lines (331 loc) · 15.2 KB
/
node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
const fs = require("fs")
const dotenv = require("dotenv").config();
const dotenvexpand = require("dotenv-expand").expand(dotenv);
const repl = require("repl");
const crypto = require("crypto");
const axios = require("axios");
const USER = process.env.BITCOIN_USER;
const PASS = process.env.BITCOIN_PASS;
const HOST = process.env.BITCOIN_CORS_RPC_URL;
const NETWORK_INTERFACE = process.env.NETWORK_INTERFACE;
const PORT = process.env.PORT;
// ✅ Step 1: Initialize the FeeEstimator
// ✅ Step 2: Initialize the Logger
// ✅ Step 3: Initialize the BroadcasterInterface
// ✅ Step 4: Initialize Persist
// ✅ Step 5: Initialize the ChainMonitor
// ✅ Step 6: Initialize the KeysManager
// ❌ Step 7: Read ChannelMonitor state from disk
// ✅ Step 8: Initialize the ChannelManager
// ❌ Step 9: Sync ChannelMonitors and ChannelManager to chain tip
// ✅ Step 10: Give ChannelMonitors to ChainMonitor
// ✅ Step 11: Optional: Initialize the P2PGossipSync
// ✅ Step 12: Initialize the PeerManager
// ✅ Step 13: Initialize networking
// ❌ Step 14: Connect and Disconnect Blocks
// ❌ Step 15: Handle LDK Events
// ❌ Step 16: Initialize routing ProbabilisticScorer
// ❌ Step 17: Create InvoicePayer
// ❌ Step 18: Persist ChannelManager and NetworkGraph
// ✅ Step 19: Background Processing
const rpcclient = async (method, params) => {
const data = { jsonrpc: '1.0', id: Math.random(), method, params };
const res = await axios.post(HOST, data, {
method: 'POST',
auth: {
username: USER,
password: PASS
},
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
}).catch((e) => {
console.log(e)
});
return res.data.result;
};
function toHexString(byteArray) {
return Array.prototype.map.call(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
}
async function start_ldk(ldk, NodeLDKNet) {
var info = await rpcclient("getblockchaininfo",[]);
console.log("Connected to Bitcoin backend: ", info.chain)
console.log('LDK startup successful. To view available commands: "help".');
const ldk_data_dir = "./.ldk/"
if (!fs.existsSync(ldk_data_dir)) {
fs.mkdirSync(ldk_data_dir);
}
console.log("LDK logs are available at <your-supplied-ldk-data-dir-path>/.ldk/logs");
// Step 1: Initialize the FeeEstimator
const fee_est = ldk.FeeEstimator.new_impl({
get_est_sat_per_1000_weight(confirmation_target) {
switch (confirmation_target) {
case ldk.ConfirmationTarget.LDKConfirmationTarget_Background:
// insert code to retireve a background feerate
//return rpc.estimateSmartFee(6, console.log)
//console.log("LDKConfirmationTarget_Background");
return 1;
break;
case ldk.ConfirmationTarget.LDKConfirmationTarget_Normal:
// <insert code to retrieve a normal (i.e. within ~6 blocks) feerate>
//console.log("LDKConfirmationTarget_Normal");
return 5000;
break;
case ldk.ConfirmationTarget.LDKConfirmationTarget_HighPriority:
// <insert code to retrieve a high-priority feerate>
//console.log("LDKConfirmationTarget_HighPriority");
return 10000;
break;
default:
return 253;
}
}
});
// Step 2: Initialize the Logger
const log_file_path = ldk_data_dir + "/debug.log";
const log_file = fs.createWriteStream(log_file_path, {flags : 'w'});
const logger = ldk.Logger.new_impl({
log(record) {
log_file.write(record.get_module_path() + ": " + record.get_args() + "\n");
}
});
// Step 3: Initialize the BroadcasterInterface
var tx_broadcaster;
const tx_broadcasted = new Promise((resolve, reject) => {
tx_broadcaster = ldk.BroadcasterInterface.new_impl({ // Need to call the sendrawtransaction call for the RPC service, loggin this for now to determined when to implement
broadcast_transaction(tx) { console.log("Tx Broadcast: " + tx); resolve(tx); }
});
});
// Step 4: Initialize Persist
const persister = ldk.Persist.new_impl({
persist_new_channel(channel_id, data, update_id) {
console.log("persist_new_channel")
return ldk.Result_NoneChannelMonitorUpdateErrZ.constructor_ok();
},
update_persisted_channel(channel_id, update, data, update_id) {
console.log("persist_new_channel")
return ldk.Result_NoneChannelMonitorUpdateErrZ.constructor_ok();
},
});
// Step 5: Initialize the ChainMonitor
const chain_monitor = ldk.ChainMonitor.constructor_new(ldk.Option_FilterZ.constructor_none(), tx_broadcaster, logger, fee_est, persister);
const chain_watch = chain_monitor.as_Watch();
// Step 6: Initialize the KeysManager
const keys_seed_path = ldk_data_dir + "keys_seed";
var seed = null;
if (!fs.existsSync(keys_seed_path)) {
seed = crypto.randomBytes(32);
fs.writeFileSync(keys_seed_path, seed);
} else {
seed = fs.readFileSync(keys_seed_path);
}
const keys_manager = ldk.KeysManager.constructor_new(seed, BigInt(42), 42);
const keys_interface = keys_manager.as_KeysInterface();
const config = ldk.UserConfig.constructor_default();
const ChannelHandshakeConfig = ldk.ChannelHandshakeConfig.constructor_default();
//ChannelHandshakeConfig.set_announced_channel(true);
//config.set_channel_handshake_config(ChannelHandshakeConfig)
//console.log("Setting up new node at ", info.bestblockhash, info.blocks);
const params = ldk.ChainParameters.constructor_new(ldk.Network.LDKNetwork_Regtest, ldk.BestBlock.constructor_new(Buffer.from(info.bestblockhash,'hex'),info.blocks));
// Step 7: Read ChannelMonitor state from disk
// const channelmonitors = persister.read_channel_monitors(keys_manager);
//persister.read_channel_monitors();
// Step 8: Initialize the ChannelManager
const channel_manager = ldk.ChannelManager.constructor_new(fee_est, chain_watch, tx_broadcaster, logger, keys_interface, config, params);
// ❌ Step 9: Sync ChannelMonitors and ChannelManager to chain tip
//restarting_node = false;
//let getinfo_resp = bitcoind_client.get_blockchain_info().await;
// ❌ Step 10: Give ChannelMonitors to ChainMonitor ASSUMING WE ARE NOT STARTING A NEW NODE
/*
for item in chain_listener_channel_monitors.drain(..) {
let channel_monitor = item.1 .0;
let funding_outpoint = item.2;
chain_monitor.watch_channel(funding_outpoint, channel_monitor).unwrap();
}
*/
// ❌ Step 11: Optional: Initialize the P2PGossipSync
let genesis_hash = await rpcclient("getblockhash",[0]);
let network_graph = ldk.NetworkGraph.constructor_new(Buffer.from(genesis_hash, "hex"), logger);
let gossip_sync = ldk.P2PGossipSync.constructor_new(network_graph, new ldk.Option_AccessZ(), logger)
// ✅ Step 12: Initialize the PeerManager
let lightning_msg_handler = ldk.MessageHandler.constructor_new({
chan_handler: channel_manager,
route_handler: gossip_sync
});
let ephemeral_bytes = crypto.randomBytes(32);
const ignoring_custom_msg_handler = ldk.IgnoringMessageHandler.constructor_new();
let peer_manager = ldk.PeerManager.constructor_new(
channel_manager.as_ChannelMessageHandler(),
gossip_sync.as_RoutingMessageHandler(),
keys_interface.get_node_secret().res,
ephemeral_bytes,
logger,
ignoring_custom_msg_handler.as_CustomMessageHandler()
);
// ## Running LDK
// Step 13: Initialize networking
// Step 14 Connect and Disconnect Blocks
console.log("Sync data from chain", info.bestblockhash, info.blocks);
// inline chain_sync_completed which is not available in TS :/
//channel_manager.write();
//network_graph.write();
let event_handler = ldk.EventHandler.new_impl({
handle_event: function(e) {
console.log(">>>>>>> Handling Event here <<<<<<<", e)
if (e instanceof ldk.Event_FundingGenerationReady) {
//console.log(e)
var final_tx = 0;
console.log(e.temporary_channel_id, e.counterparty_node_id, final_tx)
//channel_manager.funding_transaction_generated(e.temporary_channel_id, e.counterparty_node_id, final_tx);
// <insert code to handle this event>
} else if (e instanceof Event.Event_PaymentReceived) {
// <insert code to handle this event>
} else if (e instanceof Event.Event_PaymentSent) {
// <insert code to handle this event>
} else if (e instanceof Event.Event_PaymentPathFailed) {
// <insert code to handle this event>
} else if (e instanceof Event.Event_PendingHTLCsForwardable) {
// <insert code to handle this event>
} else if (e instanceof Event.Event_SpendableOutputs) {
// <insert code to handle this event>
} else if (e instanceof Event.Event_PaymentForwarded) {
// <insert code to handle this event>
} else if (e instanceof Event.Event_ChannelClosed) {
// <insert code to handle this event>
}
}
});
// Step 15
console.log("Local Node ID is " + Buffer.from(channel_manager.get_our_node_id()).toString('hex'))
channel_manager.timer_tick_occurred();
setInterval(() => {
peer_manager.timer_tick_occurred();
peer_manager.process_events();
channel_manager.as_EventsProvider().process_pending_events(event_handler);
chain_monitor.as_EventsProvider().process_pending_events(event_handler);
},2000)
const net_handler = new NodeLDKNet(peer_manager);
await net_handler.bind_listener(NETWORK_INTERFACE, PORT);
console.log("Listening on " , Buffer.from(channel_manager.get_our_node_id()).toString('hex') + "@" + NETWORK_INTERFACE + ":" + PORT );
var local = repl.start("> ");
local.context.ldk = ldk
local.context.persister = persister;
local.context.km = keys_manager;
local.context.gossip_sync = gossip_sync;
local.context.channel_manager = channel_manager;
local.context.network_graph = network_graph;
local.context.chain_monitor = chain_monitor;
local.context.help = (function() {
var resp = `
openchannel pubkey@host:port <amt_satoshis>
sendpayment <invoice>
keysend <dest_pubkey> <amt_msat>
getinvoice <amt_millisatoshis>
connectpeer pubkey@host:port
listchannels
listpayments
closechannel <channel_id>
forceclosechannel <channel_id>
nodeinfo
listpeers
signmessage <message></message>`;
return resp;
})()
local.context.nodeinfo = function() {
var resp = {
node_pubkey: Buffer.from(channel_manager.get_our_node_id()).toString('hex'),
num_channels: 0,
num_usable_channels: 0,
local_balance_msat: 0,
num_peers: 0
};
return resp;
}
local.context.steps = {
network_graph,
peer_manager,
channel_manager,
chain_monitor
}
local.context.listpeers = function() {
return peer_manager.get_peer_node_ids().map((item) => toHexString(item));
}
// Requires imeplement a signer
local.context.signmessage = function() {
//ldk.util
}
local.context.openchannel = function(peer, channel_value_satoshis, push_msat, user_channel_id, override_config) {
user_channel_id = BigInt(0);
override_config = ldk.UserConfig.constructor_default();
//connect_peer_if_necessary
let peerParts = peer.split("@");
var config = {
pubkey: peerParts[0],
host: peerParts[1].split(":")[0],
port: parseInt(peerParts[1].split(":")[1])
};
var peers = peer_manager.get_peer_node_ids().map((item) => toHexString(item));
console.log("needle", peers, config.pubkey)
if (!peers.includes(config.pubkey)) {
console.log("Connecting to " ,config )
const net_handler = new NodeLDKNet(peer_manager);
net_handler.connect_peer(config.host, config.port, Buffer.from(config.pubkey, 'hex')).then(() => {
return channel_manager.create_channel(Buffer.from(peer,'hex'), BigInt(channel_value_satoshis), BigInt(push_msat),user_channel_id,override_config);
})
} else {
console.log(Buffer.from(peer,'hex'), BigInt(channel_value_satoshis), BigInt(push_msat),BigInt(user_channel_id),override_config)
return channel_manager.create_channel(Buffer.from(peer,'hex'), BigInt(channel_value_satoshis), BigInt(push_msat),user_channel_id,override_config);
}
}
local.context.listchannels = function() {
var channelArr = [];
channel_manager.list_channels().forEach((ChannelDetails) => {
channelArr.push({
channel_id: toHexString(ChannelDetails.get_channel_id()),
funding_txid: toHexString(ChannelDetails.get_funding_txo().get_txid()),
peer_pubkey: toHexString(ChannelDetails.get_counterparty().get_node_id()),
is_channel_ready: Boolean(ChannelDetails.get_is_channel_ready()),
channel_value_satoshis: ChannelDetails.get_channel_value_satoshis(),
local_balance_msat: ChannelDetails.get_balance_msat(),
channel_can_send_payments: Boolean(ChannelDetails.get_is_usable()),
public: Boolean(ChannelDetails.get_is_public())
})
})
return channelArr;
}
local.context.connectpeer = async function(peer) {
let peerParts = peer.split("@");
var config = {
host: peerParts[1].split(":")[0],
port: parseInt(peerParts[1].split(":")[1])
};
const net_handler = new NodeLDKNet(peer_manager);
await net_handler.connect_peer(config.host, config.port, Buffer.from(peerParts[0], 'hex'));
}
if (process.env.LN_REMOTE_HOST) { // Automatically connect for debugging purposes
console.log("Automatically connect to peer")
local.context.connectpeer(process.env.LN_REMOTE_HOST);
}
// Requires implementing rust-lightning-invoice https://github.com/rust-bitcoin/rust-lightning-invoice/
local.context.getinvoice = function(amt_msat,expiry_secs) {
return channel_manager.create_inbound_payment(amt_msat,expiry_secs)
}
}
/*
Using sub lightningdevkit lib due to initializeWasm issues
*/
import("./node_modules/lightningdevkit-node-net/node_modules/lightningdevkit/index.mjs").then(ldk => {
const compileWasm = (pathToWasm) => {
const bytes = fs.readFileSync( pathToWasm)
ldk
.initializeWasmFromBinary(bytes)
.then(async (ff) => {
const { NodeLDKNet } = await import('lightningdevkit-node-net');
start_ldk(ldk, NodeLDKNet);
})
};
compileWasm("./node_modules/lightningdevkit-node-net/node_modules/lightningdevkit/liblightningjs.wasm");
})