From 49ee79c04c3c5b6263b2cd0bd825d737aecbb564 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sat, 9 Jul 2022 00:32:13 +0200 Subject: [PATCH] standard channel_id format --- config.yaml.example | 6 +++--- helpers.go | 21 +++++++++++++++++++++ htlcInterceptor.go | 38 +++++++++++++++++--------------------- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/config.yaml.example b/config.yaml.example index 57756f1..9a1b67d 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -27,7 +27,7 @@ forward-mode: "blacklist" # List of channel IDs to whitelist or blacklist forward-whitelist: - - "229797930270721" + - "7143424x65537x0" forward-blacklist: - - "131941395398657" - - "195713069809665" + - "12320768x65536x0" + - "7929856x65537x1" diff --git a/helpers.go b/helpers.go index ec5bb3f..c5a6194 100644 --- a/helpers.go +++ b/helpers.go @@ -1,8 +1,11 @@ package main import ( + "encoding/base64" "encoding/hex" "fmt" + "math/big" + "strconv" log "github.com/sirupsen/logrus" ) @@ -31,3 +34,21 @@ func setLogger(debug bool) { customFormatter.FullTimestamp = true log.SetFormatter(customFormatter) } + +func intTob64(i int64) string { + return base64.RawURLEncoding.EncodeToString(big.NewInt(i).Bytes()) +} + +func intToHex(i int64) string { + return hex.EncodeToString(big.NewInt(i).Bytes()) +} + +func parse_channelID(e uint64) string { + byte_e := big.NewInt(int64(e)).Bytes() + hexstr := hex.EncodeToString(byte_e) + fmt.Println(hexstr) + int_block3, _ := strconv.ParseInt(hexstr[:6], 16, 64) + int_block2, _ := strconv.ParseInt(hexstr[6:12], 16, 64) + int_block1, _ := strconv.ParseInt(hexstr[12:], 16, 64) + return fmt.Sprintf("%dx%dx%d", int_block3, int_block2, int_block1) +} diff --git a/htlcInterceptor.go b/htlcInterceptor.go index 774e280..022b4d6 100644 --- a/htlcInterceptor.go +++ b/htlcInterceptor.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "strconv" "sync" "time" @@ -65,16 +64,16 @@ func (app *app) logHtlcEvents(ctx context.Context, stream routerrpc.Router_Subsc switch event.Event.(type) { case *routerrpc.HtlcEvent_SettleEvent: - log.Debugf("HTLC SettleEvent (chan_id:%d, htlc_id:%d)", event.IncomingChannelId, event.IncomingHtlcId) + log.Debugf("HTLC SettleEvent (chan_id:%s, htlc_id:%d)", parse_channelID(event.IncomingChannelId), event.IncomingHtlcId) case *routerrpc.HtlcEvent_ForwardFailEvent: - log.Debugf("HTLC ForwardFailEvent (chan_id:%d, htlc_id:%d)", event.IncomingChannelId, event.IncomingHtlcId) + log.Debugf("HTLC ForwardFailEvent (chan_id:%s, htlc_id:%d)", parse_channelID(event.IncomingChannelId), event.IncomingHtlcId) case *routerrpc.HtlcEvent_ForwardEvent: - log.Debugf("HTLC ForwardEvent (chan_id:%d, htlc_id:%d)", event.IncomingChannelId, event.IncomingHtlcId) + log.Debugf("HTLC ForwardEvent (chan_id:%s, htlc_id:%d)", parse_channelID(event.IncomingChannelId), event.IncomingHtlcId) case *routerrpc.HtlcEvent_LinkFailEvent: - log.Debugf("HTLC LinkFailEvent (chan_id:%d, htlc_id:%d)", event.IncomingChannelId, event.IncomingHtlcId) + log.Debugf("HTLC LinkFailEvent (chan_id:%s, htlc_id:%d)", parse_channelID(event.IncomingChannelId), event.IncomingHtlcId) } } @@ -93,7 +92,7 @@ func (app *app) interceptHtlcEvents(ctx context.Context, interceptor routerrpc.R channelEdge, err := app.getPubKeyFromChannel(ctx, event.IncomingCircuitKey.ChanId) if err != nil { - log.Error("Error getting pubkey for channel %d", event.IncomingCircuitKey.ChanId) + log.Error("Error getting pubkey for channel %s", parse_channelID(event.IncomingCircuitKey.ChanId)) } var pubkeyFrom, aliasFrom, pubkeyTo, aliasTo string @@ -107,10 +106,9 @@ func (app *app) interceptHtlcEvents(ctx context.Context, interceptor routerrpc.R aliasFrom = trimPubKey([]byte(pubkeyFrom)) log.Error("Error getting alias for node %s", aliasFrom) } - channelEdgeTo, err := app.getPubKeyFromChannel(ctx, event.OutgoingRequestedChanId) if err != nil { - log.Error("Error getting pubkey for channel %d", event.OutgoingRequestedChanId) + log.Error("Error getting pubkey for channel %s", parse_channelID(event.OutgoingRequestedChanId)) } if channelEdgeTo.node1Pub.String() != app.myPubkey { pubkeyTo = channelEdgeTo.node1Pub.String() @@ -123,7 +121,15 @@ func (app *app) interceptHtlcEvents(ctx context.Context, interceptor routerrpc.R log.Error("Error getting alias for node %s", aliasTo) } - forward_info_string := fmt.Sprintf("from %s to %s (%d sat, htlc_id:%d, chan_id:%d->%d)", aliasFrom, aliasTo, event.IncomingAmountMsat/1000, event.IncomingCircuitKey.HtlcId, event.IncomingCircuitKey.ChanId, event.OutgoingRequestedChanId) + forward_info_string := fmt.Sprintf( + "from %s to %s (%d sat, chan_id:%s->%s, htlc_id:%d)", + aliasFrom, + aliasTo, + event.IncomingAmountMsat/1000, + parse_channelID(event.IncomingCircuitKey.ChanId), + parse_channelID(event.OutgoingRequestedChanId), + event.IncomingCircuitKey.HtlcId, + ) response := &routerrpc.ForwardHtlcInterceptResponse{ IncomingCircuitKey: event.IncomingCircuitKey, @@ -149,12 +155,7 @@ func (app *app) htlcInterceptDecision(ctx context.Context, event *routerrpc.Forw if Configuration.ForwardMode == "whitelist" { accept = false for _, channel_id := range Configuration.ForwardWhitelist { - chan_id_int, err := strconv.ParseUint(channel_id, 10, 64) - if err != nil { - log.Error("Error parsing channel id %s", channel_id) - break - } - if event.IncomingCircuitKey.ChanId == chan_id_int { + if parse_channelID(event.IncomingCircuitKey.ChanId) == channel_id { accept = true break } @@ -162,12 +163,7 @@ func (app *app) htlcInterceptDecision(ctx context.Context, event *routerrpc.Forw } else if Configuration.ForwardMode == "blacklist" { accept = true for _, channel_id := range Configuration.ForwardBlacklist { - chan_id_int, err := strconv.ParseUint(channel_id, 10, 64) - if err != nil { - log.Error("Error parsing channel id %s", channel_id) - break - } - if event.IncomingCircuitKey.ChanId == chan_id_int { + if parse_channelID(event.IncomingCircuitKey.ChanId) == channel_id { accept = false break }