diff --git a/cln/cln_client.go b/cln/cln_client.go index dc8d7e8..0a9dd3d 100644 --- a/cln/cln_client.go +++ b/cln/cln_client.go @@ -198,7 +198,7 @@ func (c *ClnClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*ligh pubkey := hex.EncodeToString(peerID) channels, err := client.GetPeerChannels(pubkey) if err != nil { - log.Printf("CLN: client.GetPeer(%s) error: %v", pubkey, err) + log.Printf("CLN: client.GetPeerChannels(%s) error: %v", pubkey, err) return nil, err } @@ -206,20 +206,15 @@ func (c *ClnClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*ligh for _, c := range channels { log.Printf("getChannel destination: %s, Short channel id: %v, local alias: %v , FundingTxID:%v, State:%v ", pubkey, c.ShortChannelId, c.Alias.Local, c.FundingTxId, c.State) if slices.Contains(OPEN_STATUSES, c.State) && c.FundingTxId == fundingTxID { - confirmedChanID, err := lightning.NewShortChannelIDFromString(c.ShortChannelId) + + aliasScid, confirmedScid, err := mapScidsFromChannel(c) if err != nil { - fmt.Printf("NewShortChannelIDFromString %v error: %v", c.ShortChannelId, err) - return nil, err - } - initialChanID, err := lightning.NewShortChannelIDFromString(c.Alias.Local) - if err != nil { - fmt.Printf("NewShortChannelIDFromString %v error: %v", c.Alias.Local, err) return nil, err } return &lightning.GetChannelResult{ - InitialChannelID: *initialChanID, - ConfirmedChannelID: *confirmedChanID, - HtlcMinimumMsat: c.MinimumHtlcOutMsat.MSat(), + AliasScid: aliasScid, + ConfirmedScid: confirmedScid, + HtlcMinimumMsat: c.MinimumHtlcOutMsat.MSat(), }, nil } } @@ -324,3 +319,24 @@ func (c *ClnClient) WaitOnline(peerID []byte, deadline time.Time) error { func (c *ClnClient) WaitChannelActive(peerID []byte, deadline time.Time) error { return nil } + +func mapScidsFromChannel(c *glightning.PeerChannel) (*lightning.ShortChannelID, *lightning.ShortChannelID, error) { + var confirmedScid *lightning.ShortChannelID + var aliasScid *lightning.ShortChannelID + var err error + if c.ShortChannelId != "" { + confirmedScid, err = lightning.NewShortChannelIDFromString(c.ShortChannelId) + if err != nil { + return nil, nil, fmt.Errorf("failed to parse scid '%s': %w", c.ShortChannelId, err) + } + } + + if c.Alias != nil && c.Alias.Local != "" { + aliasScid, err = lightning.NewShortChannelIDFromString(c.Alias.Local) + if err != nil { + return nil, nil, fmt.Errorf("failed to parse scid '%s': %w", c.Alias.Local, err) + } + } + + return aliasScid, confirmedScid, nil +} diff --git a/interceptor/intercept_handler.go b/interceptor/intercept_handler.go index a5d2e63..fbb1ca8 100644 --- a/interceptor/intercept_handler.go +++ b/interceptor/intercept_handler.go @@ -224,11 +224,18 @@ func (i *Interceptor) Intercept(req common.InterceptRequest) common.InterceptRes for { chanResult, _ := i.client.GetChannel(destination, *channelPoint) if chanResult != nil { - log.Printf("paymentHash: %s, channel opened successfully alias: %v, confirmed: %v", reqPaymentHashStr, chanResult.InitialChannelID.ToString(), chanResult.ConfirmedChannelID.ToString()) + log.Printf("paymentHash: %s, channel opened successfully alias: '%v', confirmed: '%v'", reqPaymentHashStr, chanResult.AliasScid.ToString(), chanResult.ConfirmedScid.ToString()) - channelID := chanResult.ConfirmedChannelID - if uint64(channelID) == 0 { - channelID = chanResult.InitialChannelID + var scid *lightning.ShortChannelID + if chanResult.ConfirmedScid == nil { + if chanResult.AliasScid == nil { + log.Printf("Error: GetChannel: Both confirmed scid and alias scid are nil: %+v", chanResult) + <-time.After(1 * time.Second) + continue + } + scid = chanResult.AliasScid + } else { + scid = chanResult.ConfirmedScid } useLegacyOnionBlob := slices.Contains(i.config.LegacyOnionTokens, token) @@ -236,7 +243,7 @@ func (i *Interceptor) Intercept(req common.InterceptRequest) common.InterceptRes Action: common.INTERCEPT_RESUME_WITH_ONION, Destination: destination, ChannelPoint: channelPoint, - Scid: channelID, + Scid: *scid, PaymentSecret: paymentSecret, AmountMsat: uint64(amt), TotalAmountMsat: uint64(outgoingAmountMsat), @@ -244,7 +251,7 @@ func (i *Interceptor) Intercept(req common.InterceptRequest) common.InterceptRes }, nil } - log.Printf("paymentHash: %s, waiting for channel to get opened.... %v", reqPaymentHashStr, destination) + log.Printf("paymentHash: %s, waiting for channel to get opened... %x", reqPaymentHashStr, destination) if time.Now().After(deadline) { log.Printf("paymentHash: %s, Stop retrying getChannel(%v, %v)", reqPaymentHashStr, destination, channelPoint.String()) break diff --git a/lightning/client.go b/lightning/client.go index 99acd00..613fd85 100644 --- a/lightning/client.go +++ b/lightning/client.go @@ -12,9 +12,9 @@ type GetInfoResult struct { } type GetChannelResult struct { - InitialChannelID ShortChannelID - ConfirmedChannelID ShortChannelID - HtlcMinimumMsat uint64 + AliasScid *ShortChannelID + ConfirmedScid *ShortChannelID + HtlcMinimumMsat uint64 } type OpenChannelRequest struct { diff --git a/lnd/client.go b/lnd/client.go index fd9799f..6614378 100644 --- a/lnd/client.go +++ b/lnd/client.go @@ -13,7 +13,6 @@ import ( "github.com/breez/lspd/lightning" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/lightningnetwork/lnd/htlcswitch/hop" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/chainrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" @@ -298,17 +297,11 @@ func (c *LndClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*ligh for _, c := range r.Channels { log.Printf("getChannel(%x): %v", peerID, c.ChanId) if c.ChannelPoint == channelPointStr && c.Active { - confirmedChanId := c.ChanId - if c.ZeroConf { - confirmedChanId = c.ZeroConfConfirmedScid - if confirmedChanId == hop.Source.ToUint64() { - confirmedChanId = 0 - } - } + aliasScid, confirmedScid := mapScidsFromChannel(c) return &lightning.GetChannelResult{ - InitialChannelID: lightning.ShortChannelID(c.ChanId), - ConfirmedChannelID: lightning.ShortChannelID(confirmedChanId), - HtlcMinimumMsat: c.LocalConstraints.MinHtlcMsat, + AliasScid: aliasScid, + ConfirmedScid: confirmedScid, + HtlcMinimumMsat: c.LocalConstraints.MinHtlcMsat, }, nil } } @@ -498,3 +491,21 @@ func (c *LndClient) WaitChannelActive(peerID []byte, deadline time.Time) error { return fmt.Errorf("deadline exceeded") } } + +func mapScidsFromChannel(c *lnrpc.Channel) (*lightning.ShortChannelID, *lightning.ShortChannelID) { + var alias *lightning.ShortChannelID + var confirmedScid *lightning.ShortChannelID + if c.ZeroConf { + if c.ZeroConfConfirmedScid != 0 { + confirmedScid = (*lightning.ShortChannelID)(&c.ZeroConfConfirmedScid) + } + alias = (*lightning.ShortChannelID)(&c.ChanId) + } else { + confirmedScid = (*lightning.ShortChannelID)(&c.ChanId) + if len(c.AliasScids) > 0 { + alias = (*lightning.ShortChannelID)(&c.AliasScids[0]) + } + } + + return alias, confirmedScid +} diff --git a/lsps2/intercept_handler.go b/lsps2/intercept_handler.go index 09d54bc..fb87730 100644 --- a/lsps2/intercept_handler.go +++ b/lsps2/intercept_handler.go @@ -516,18 +516,25 @@ func (i *Interceptor) ensureChannelOpen(payment *paymentState) { log.Printf( "Got new channel for forward successfully. scid alias: %v, "+ "confirmed scid: %v", - chanResult.InitialChannelID.ToString(), - chanResult.ConfirmedChannelID.ToString(), + chanResult.AliasScid.ToString(), + chanResult.ConfirmedScid.ToString(), ) - scid := chanResult.ConfirmedChannelID - if uint64(scid) == 0 { - scid = chanResult.InitialChannelID + var scid *lightning.ShortChannelID + if chanResult.ConfirmedScid == nil { + if chanResult.AliasScid == nil { + log.Printf("Error: GetChannel: Both confirmed scid and alias scid are nil: %+v", chanResult) + <-time.After(1 * time.Second) + continue + } + scid = chanResult.AliasScid + } else { + scid = chanResult.ConfirmedScid } i.paymentChanOpened <- &paymentChanOpenedEvent{ paymentId: payment.id, - scid: scid, + scid: *scid, channelPoint: payment.registration.ChannelPoint, htlcMinimumMsat: chanResult.HtlcMinimumMsat, } diff --git a/lsps2/intercept_test.go b/lsps2/intercept_test.go index 85ad694..eff682f 100644 --- a/lsps2/intercept_test.go +++ b/lsps2/intercept_test.go @@ -31,9 +31,9 @@ var defaultChainHash = chainhash.Hash([32]byte{}) var defaultOutPoint = wire.NewOutPoint(&defaultChainHash, 0) var defaultChannelScid uint64 = 456 var defaultChanResult = &lightning.GetChannelResult{ - HtlcMinimumMsat: defaultConfig().HtlcMinimumMsat, - InitialChannelID: lightning.ShortChannelID(defaultChannelScid), - ConfirmedChannelID: lightning.ShortChannelID(defaultChannelScid), + HtlcMinimumMsat: defaultConfig().HtlcMinimumMsat, + AliasScid: (*lightning.ShortChannelID)(&defaultChannelScid), + ConfirmedScid: (*lightning.ShortChannelID)(&defaultChannelScid), } func defaultOpeningFeeParams() common.OpeningFeeParams {