From 6f88df3ab5ba850f98fc563b9f44403816f1b7e0 Mon Sep 17 00:00:00 2001 From: Andrew Cann Date: Mon, 20 Apr 2020 19:22:34 +0800 Subject: [PATCH] Make GetFailureMsgData format non-printable error messages Some lightning implementations will send non-printable data in error messages. For instance outdated versions of lnd will do this: https://github.com/lightningnetwork/lnd/commit/ff37b711c6dea62f6acb9d0aaca36417edcda820 Whereas these are the error codes (sent as single-byte error messages): https://github.com/lightningnetwork/lnd/blob/9b1ecbd3fa3fd93a6d95a5f71921c73777340760/lnwire/error.go#L12 These errors need to be printed as bytes when displayed to the user, rather than interpretted as strings. Error.GetFailureMsgData will now checks whether an error message is printable before returning the message as a string and, if it's not, returns a string containing the hex-encoded binary data in the error. --- src/DotNetLightning.Core/Serialize/Msgs/Msgs.fs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/DotNetLightning.Core/Serialize/Msgs/Msgs.fs b/src/DotNetLightning.Core/Serialize/Msgs/Msgs.fs index 542b4d9c6..020f13ebe 100644 --- a/src/DotNetLightning.Core/Serialize/Msgs/Msgs.fs +++ b/src/DotNetLightning.Core/Serialize/Msgs/Msgs.fs @@ -1288,7 +1288,15 @@ type ErrorMessage = ls.WriteWithLen(this.Data) member this.GetFailureMsgData() = - System.Text.ASCIIEncoding.ASCII.GetString(this.Data) + let isPrintableAscii = + not <| Array.exists (fun asciiByte -> asciiByte < 32uy) this.Data + if isPrintableAscii then + System.Text.ASCIIEncoding.ASCII.GetString(this.Data) + else + Array.fold + (fun msg (asciiByte: byte) -> sprintf "%s %02x" msg asciiByte) + ":" + this.Data and WhichChannel = | SpecificChannel of ChannelId