add option for new onion format for lnd lsp

This commit is contained in:
Jesse de Wit
2023-10-17 16:55:55 +02:00
parent 505a02e5d9
commit 4640507c5f
3 changed files with 49 additions and 32 deletions

View File

@@ -15,6 +15,11 @@ type NodeConfig struct {
// configured node, so it's obvious which node an rpc call is meant for.
Tokens []string `json:"tokens"`
// If the used token is in the LegacyOnionTokens array, the forwarded htlc
// will have the legacy onion format. As per the time of writing breezmobile
// requires the legacy onion format.
LegacyOnionTokens []string `json:"legacyOnionTokens"`
// The network location of the lightning node, e.g. `12.34.56.78:9012` or
// `localhost:10011`
Host string `json:"host"`

View File

@@ -15,6 +15,7 @@ import (
"github.com/breez/lspd/lightning"
"github.com/breez/lspd/notifications"
"github.com/btcsuite/btcd/wire"
"golang.org/x/exp/slices"
"golang.org/x/sync/singleflight"
)
@@ -35,14 +36,15 @@ var (
)
type InterceptResult struct {
Action InterceptAction
FailureCode InterceptFailureCode
Destination []byte
AmountMsat uint64
TotalAmountMsat uint64
ChannelPoint *wire.OutPoint
ChannelId uint64
PaymentSecret []byte
Action InterceptAction
FailureCode InterceptFailureCode
Destination []byte
AmountMsat uint64
TotalAmountMsat uint64
ChannelPoint *wire.OutPoint
ChannelId uint64
PaymentSecret []byte
UseLegacyOnionBlob bool
}
type Interceptor struct {
@@ -257,14 +259,16 @@ func (i *Interceptor) Intercept(scid *basetypes.ShortChannelID, reqPaymentHash [
channelID = uint64(chanResult.InitialChannelID)
}
useLegacyOnionBlob := slices.Contains(i.config.LegacyOnionTokens, token)
return InterceptResult{
Action: INTERCEPT_RESUME_WITH_ONION,
Destination: destination,
ChannelPoint: channelPoint,
ChannelId: channelID,
PaymentSecret: paymentSecret,
AmountMsat: uint64(amt),
TotalAmountMsat: uint64(outgoingAmountMsat),
Action: INTERCEPT_RESUME_WITH_ONION,
Destination: destination,
ChannelPoint: channelPoint,
ChannelId: channelID,
PaymentSecret: paymentSecret,
AmountMsat: uint64(amt),
TotalAmountMsat: uint64(outgoingAmountMsat),
UseLegacyOnionBlob: useLegacyOnionBlob,
}, nil
}

View File

@@ -139,23 +139,7 @@ func (i *LndHtlcInterceptor) intercept() error {
interceptResult := i.interceptor.Intercept(&scid, request.PaymentHash, request.OutgoingAmountMsat, request.OutgoingExpiry, request.IncomingExpiry)
switch interceptResult.Action {
case interceptor.INTERCEPT_RESUME_WITH_ONION:
onion, err := i.constructOnion(interceptResult, request.OutgoingExpiry, request.PaymentHash)
if err == nil {
interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{
IncomingCircuitKey: request.IncomingCircuitKey,
Action: routerrpc.ResolveHoldForwardAction_RESUME,
OutgoingAmountMsat: interceptResult.AmountMsat,
OutgoingRequestedChanId: uint64(interceptResult.ChannelId),
OnionBlob: onion,
})
} else {
interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{
IncomingCircuitKey: request.IncomingCircuitKey,
Action: routerrpc.ResolveHoldForwardAction_FAIL,
FailureCode: lnrpc.Failure_TEMPORARY_CHANNEL_FAILURE,
})
}
interceptorClient.Send(i.createOnionResponse(interceptResult, request))
case interceptor.INTERCEPT_FAIL_HTLC_WITH_CODE:
interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{
IncomingCircuitKey: request.IncomingCircuitKey,
@@ -257,3 +241,27 @@ func (i *LndHtlcInterceptor) constructOnion(
return onionBlob.Bytes(), nil
}
func (i *LndHtlcInterceptor) createOnionResponse(interceptResult interceptor.InterceptResult, request *routerrpc.ForwardHtlcInterceptRequest) *routerrpc.ForwardHtlcInterceptResponse {
onionBlob := request.OnionBlob
if interceptResult.UseLegacyOnionBlob {
var err error
onionBlob, err = i.constructOnion(interceptResult, request.OutgoingExpiry, request.PaymentHash)
if err != nil {
return &routerrpc.ForwardHtlcInterceptResponse{
IncomingCircuitKey: request.IncomingCircuitKey,
Action: routerrpc.ResolveHoldForwardAction_FAIL,
FailureCode: lnrpc.Failure_TEMPORARY_CHANNEL_FAILURE,
}
}
}
return &routerrpc.ForwardHtlcInterceptResponse{
IncomingCircuitKey: request.IncomingCircuitKey,
Action: routerrpc.ResolveHoldForwardAction_RESUME,
OutgoingAmountMsat: interceptResult.AmountMsat,
OutgoingRequestedChanId: uint64(interceptResult.ChannelId),
OnionBlob: onionBlob,
}
}