Skip to content

Commit 7e6537f

Browse files
committed
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: lightningnetwork/lnd@ff37b71 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.
1 parent 0a76946 commit 7e6537f

File tree

1 file changed

+9
-1
lines changed
  • src/DotNetLightning.Core/Serialize/Msgs

1 file changed

+9
-1
lines changed

src/DotNetLightning.Core/Serialize/Msgs/Msgs.fs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,15 @@ type ErrorMessage =
12811281
ls.WriteWithLen(this.Data)
12821282

12831283
member this.GetFailureMsgData() =
1284-
System.Text.ASCIIEncoding.ASCII.GetString(this.Data)
1284+
let isPrintableAscii =
1285+
not <| Array.exists (fun asciiByte -> asciiByte < 32uy) this.Data
1286+
if isPrintableAscii then
1287+
System.Text.ASCIIEncoding.ASCII.GetString(this.Data)
1288+
else
1289+
Array.fold
1290+
(fun msg (asciiByte: byte) -> sprintf "%s %02x" msg asciiByte)
1291+
"<error contains non-printable binary data>:"
1292+
this.Data
12851293

12861294
and WhichChannel =
12871295
| SpecificChannel of ChannelId

0 commit comments

Comments
 (0)