| | | 1 | | using MessagePack; |
| | | 2 | | using NLightning.Client.Handlers; |
| | | 3 | | using NLightning.Client.Ipc; |
| | | 4 | | using NLightning.Client.Printers; |
| | | 5 | | using NLightning.Client.Utils; |
| | | 6 | | using NLightning.Daemon.Contracts.Helpers; |
| | | 7 | | using NLightning.Daemon.Contracts.Utilities; |
| | | 8 | | using NLightning.Transport.Ipc.MessagePack; |
| | | 9 | | |
| | | 10 | | // Register the default formatter for MessagePackSerializer |
| | 0 | 11 | | MessagePackSerializer.DefaultOptions = NLightningMessagePackOptions.Options; |
| | | 12 | | |
| | 0 | 13 | | var cts = new CancellationTokenSource(); |
| | 0 | 14 | | Console.CancelKeyPress += (_, e) => |
| | 0 | 15 | | { |
| | 0 | 16 | | e.Cancel = true; |
| | 0 | 17 | | cts.Cancel(); |
| | 0 | 18 | | }; |
| | | 19 | | |
| | | 20 | | // Get network for the NamedPipe file path |
| | 0 | 21 | | var cookiePath = CommandLineHelper.GetCookiePath(args); |
| | 0 | 22 | | var namedPipeFilePath = NodeUtils.GetNamedPipeFilePath(cookiePath); |
| | 0 | 23 | | var cookieFilePath = NodeUtils.GetCookieFilePath(cookiePath); |
| | | 24 | | |
| | 0 | 25 | | var cmd = CommandLineHelper.GetCommand(args) ?? "node-info"; |
| | | 26 | | |
| | | 27 | | try |
| | 0 | 28 | | { |
| | 0 | 29 | | if (CommandLineHelper.IsHelpRequested(args)) |
| | 0 | 30 | | { |
| | 0 | 31 | | ClientUtils.ShowUsage(); |
| | 0 | 32 | | return 0; |
| | | 33 | | } |
| | | 34 | | |
| | 0 | 35 | | await using var client = new NamedPipeIpcClient(namedPipeFilePath, cookieFilePath); |
| | | 36 | | |
| | 0 | 37 | | var commandArgs = CommandLineHelper.GetCommandArguments(cmd, args); |
| | | 38 | | |
| | 0 | 39 | | switch (cmd) |
| | | 40 | | { |
| | | 41 | | case "info": |
| | | 42 | | case "node-info": |
| | 0 | 43 | | var info = await client.GetNodeInfoAsync(cts.Token); |
| | 0 | 44 | | new NodeInfoPrinter().Print(info); |
| | 0 | 45 | | break; |
| | | 46 | | case "connect": |
| | | 47 | | case "connect-peer": |
| | 0 | 48 | | if (commandArgs.Length == 0) |
| | 0 | 49 | | Console.Error.WriteLine("No arguments specified."); |
| | 0 | 50 | | var connect = await client.ConnectPeerAsync(commandArgs[0], cts.Token); |
| | 0 | 51 | | new ConnectPeerPrinter().Print(connect); |
| | 0 | 52 | | break; |
| | | 53 | | case "listpeers": |
| | | 54 | | case "list-peers": |
| | 0 | 55 | | var listPeers = await client.ListPeersAsync(cts.Token); |
| | 0 | 56 | | new ListPeersPrinter().Print(listPeers); |
| | 0 | 57 | | break; |
| | | 58 | | case "getaddress": |
| | | 59 | | case "get-address": |
| | 0 | 60 | | var addresses = await client.GetAddressAsync(commandArgs[0], cts.Token); |
| | 0 | 61 | | new GetAddressPrinter().Print(addresses); |
| | 0 | 62 | | break; |
| | | 63 | | case "walletbalance": |
| | | 64 | | case "wallet-balance": |
| | 0 | 65 | | var balance = await client.GetWalletBalance(cts.Token); |
| | 0 | 66 | | new WalletBalancePrinter().Print(balance); |
| | 0 | 67 | | break; |
| | | 68 | | case "openchannel": |
| | | 69 | | case "open-channel": |
| | 0 | 70 | | OpenChannelMessageHandler.HandleAsync(commandArgs, client, cts.Token).GetAwaiter().GetResult(); |
| | 0 | 71 | | break; |
| | | 72 | | default: |
| | 0 | 73 | | Console.Error.WriteLine($"Unknown command: {cmd}"); |
| | 0 | 74 | | ClientUtils.ShowUsage(); |
| | 0 | 75 | | Environment.ExitCode = 2; |
| | 0 | 76 | | break; |
| | | 77 | | } |
| | 0 | 78 | | } |
| | 0 | 79 | | catch (Exception ex) |
| | 0 | 80 | | { |
| | 0 | 81 | | Console.Error.WriteLine($"Error: {ex.Message}"); |
| | 0 | 82 | | return 1; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | return 0; |