2019-09-05 13:35:39 +02:00
|
|
|
package hop
|
2017-05-01 17:46:53 +03:00
|
|
|
|
|
|
|
import (
|
2019-07-30 21:52:17 -07:00
|
|
|
"bytes"
|
2022-12-14 15:02:01 -05:00
|
|
|
"errors"
|
2019-07-30 21:52:17 -07:00
|
|
|
"fmt"
|
2017-05-02 22:01:46 +03:00
|
|
|
"io"
|
2021-04-27 16:51:27 +02:00
|
|
|
"sync"
|
2017-05-01 17:46:53 +03:00
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
2019-09-05 13:35:39 +02:00
|
|
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
2017-06-16 23:38:42 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2022-12-14 15:02:01 -05:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2024-04-02 08:46:14 -04:00
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
2017-05-01 17:46:53 +03:00
|
|
|
)
|
|
|
|
|
2022-12-14 15:02:01 -05:00
|
|
|
var (
|
|
|
|
// ErrDecodeFailed is returned when we can't decode blinded data.
|
|
|
|
ErrDecodeFailed = errors.New("could not decode blinded data")
|
2024-04-22 14:06:17 -04:00
|
|
|
|
|
|
|
// ErrNoBlindingPoint is returned when we have not provided a blinding
|
|
|
|
// point for a validated payload with encrypted data set.
|
|
|
|
ErrNoBlindingPoint = errors.New("no blinding point set for validated " +
|
|
|
|
"blinded hop")
|
2022-12-14 15:02:01 -05:00
|
|
|
)
|
|
|
|
|
2019-09-05 13:35:39 +02:00
|
|
|
// Iterator is an interface that abstracts away the routing information
|
2017-06-16 23:29:38 +02:00
|
|
|
// included in HTLC's which includes the entirety of the payment path of an
|
|
|
|
// HTLC. This interface provides two basic method which carry out: how to
|
|
|
|
// interpret the forwarding information encoded within the HTLC packet, and hop
|
|
|
|
// to encode the forwarding information for the _next_ hop.
|
2019-09-05 13:35:39 +02:00
|
|
|
type Iterator interface {
|
2019-11-04 15:10:15 -08:00
|
|
|
// HopPayload returns the set of fields that detail exactly _how_ this
|
|
|
|
// hop should forward the HTLC to the next hop. Additionally, the
|
|
|
|
// information encoded within the returned ForwardingInfo is to be used
|
|
|
|
// by each hop to authenticate the information given to it by the prior
|
|
|
|
// hop. The payload will also contain any additional TLV fields provided
|
|
|
|
// by the sender.
|
|
|
|
HopPayload() (*Payload, error)
|
2019-07-30 21:52:17 -07:00
|
|
|
|
2017-06-16 23:29:38 +02:00
|
|
|
// EncodeNextHop encodes the onion packet destined for the next hop
|
|
|
|
// into the passed io.Writer.
|
|
|
|
EncodeNextHop(w io.Writer) error
|
2018-01-16 00:36:14 -08:00
|
|
|
|
|
|
|
// ExtractErrorEncrypter returns the ErrorEncrypter needed for this hop,
|
|
|
|
// along with a failure code to signal if the decoding was successful.
|
|
|
|
ExtractErrorEncrypter(ErrorEncrypterExtracter) (ErrorEncrypter,
|
|
|
|
lnwire.FailCode)
|
2017-05-02 22:01:46 +03:00
|
|
|
}
|
2017-05-02 23:57:13 +03:00
|
|
|
|
|
|
|
// sphinxHopIterator is the Sphinx implementation of hop iterator which uses
|
2017-06-16 23:29:38 +02:00
|
|
|
// onion routing to encode the payment route in such a way so that node might
|
|
|
|
// see only the next hop in the route..
|
2017-05-02 23:57:13 +03:00
|
|
|
type sphinxHopIterator struct {
|
2018-01-16 00:36:14 -08:00
|
|
|
// ogPacket is the original packet from which the processed packet is
|
|
|
|
// derived.
|
|
|
|
ogPacket *sphinx.OnionPacket
|
2017-06-16 23:29:38 +02:00
|
|
|
|
|
|
|
// processedPacket is the outcome of processing an onion packet. It
|
|
|
|
// includes the information required to properly forward the packet to
|
|
|
|
// the next hop.
|
|
|
|
processedPacket *sphinx.ProcessedPacket
|
2024-04-02 10:56:08 -04:00
|
|
|
|
|
|
|
// blindingKit contains the elements required to process hops that are
|
|
|
|
// part of a blinded route.
|
|
|
|
blindingKit BlindingKit
|
2017-05-02 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
2018-01-16 00:36:14 -08:00
|
|
|
// makeSphinxHopIterator converts a processed packet returned from a sphinx
|
2024-04-02 10:56:08 -04:00
|
|
|
// router and converts it into an hop iterator for usage in the link. A
|
|
|
|
// blinding kit is passed through for the link to obtain forwarding information
|
|
|
|
// for blinded routes.
|
2018-01-16 00:36:14 -08:00
|
|
|
func makeSphinxHopIterator(ogPacket *sphinx.OnionPacket,
|
2024-04-02 10:56:08 -04:00
|
|
|
packet *sphinx.ProcessedPacket,
|
|
|
|
blindingKit BlindingKit) *sphinxHopIterator {
|
2018-01-16 00:36:14 -08:00
|
|
|
|
|
|
|
return &sphinxHopIterator{
|
|
|
|
ogPacket: ogPacket,
|
|
|
|
processedPacket: packet,
|
2024-04-02 10:56:08 -04:00
|
|
|
blindingKit: blindingKit,
|
2018-01-16 00:36:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 23:29:38 +02:00
|
|
|
// A compile time check to ensure sphinxHopIterator implements the HopIterator
|
2017-05-02 23:57:13 +03:00
|
|
|
// interface.
|
2019-09-05 13:35:39 +02:00
|
|
|
var _ Iterator = (*sphinxHopIterator)(nil)
|
2017-05-02 23:57:13 +03:00
|
|
|
|
|
|
|
// Encode encodes iterator and writes it to the writer.
|
2017-06-16 23:29:38 +02:00
|
|
|
//
|
2017-05-02 23:57:13 +03:00
|
|
|
// NOTE: Part of the HopIterator interface.
|
2017-06-16 23:29:38 +02:00
|
|
|
func (r *sphinxHopIterator) EncodeNextHop(w io.Writer) error {
|
2018-01-16 00:36:14 -08:00
|
|
|
return r.processedPacket.NextPacket.Encode(w)
|
2017-05-02 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
2019-11-04 15:10:15 -08:00
|
|
|
// HopPayload returns the set of fields that detail exactly _how_ this hop
|
|
|
|
// should forward the HTLC to the next hop. Additionally, the information
|
|
|
|
// encoded within the returned ForwardingInfo is to be used by each hop to
|
|
|
|
// authenticate the information given to it by the prior hop. The payload will
|
|
|
|
// also contain any additional TLV fields provided by the sender.
|
2017-06-16 23:29:38 +02:00
|
|
|
//
|
2017-05-02 23:57:13 +03:00
|
|
|
// NOTE: Part of the HopIterator interface.
|
2019-11-04 15:10:15 -08:00
|
|
|
func (r *sphinxHopIterator) HopPayload() (*Payload, error) {
|
2019-07-30 21:52:17 -07:00
|
|
|
switch r.processedPacket.Payload.Type {
|
2019-11-04 15:10:15 -08:00
|
|
|
|
2019-07-30 21:52:17 -07:00
|
|
|
// If this is the legacy payload, then we'll extract the information
|
|
|
|
// directly from the pre-populated ForwardingInstructions field.
|
|
|
|
case sphinx.PayloadLegacy:
|
|
|
|
fwdInst := r.processedPacket.ForwardingInstructions
|
2019-11-04 15:10:15 -08:00
|
|
|
return NewLegacyPayload(fwdInst), nil
|
2019-07-30 21:52:17 -07:00
|
|
|
|
|
|
|
// Otherwise, if this is the TLV payload, then we'll make a new stream
|
|
|
|
// to decode only what we need to make routing decisions.
|
|
|
|
case sphinx.PayloadTLV:
|
2024-04-02 11:11:35 -04:00
|
|
|
isFinal := r.processedPacket.Action == sphinx.ExitNode
|
|
|
|
payload, parsed, err := NewPayloadFromReader(
|
2023-11-01 10:39:33 -04:00
|
|
|
bytes.NewReader(r.processedPacket.Payload.Payload),
|
2024-04-22 14:06:17 -04:00
|
|
|
isFinal, r.blindingKit.UpdateAddBlinding.IsSome(),
|
2023-11-01 10:39:33 -04:00
|
|
|
)
|
2024-04-02 11:11:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we had an encrypted data payload present, pull out our
|
|
|
|
// forwarding info from the blob.
|
|
|
|
if payload.encryptedData != nil {
|
|
|
|
fwdInfo, err := r.blindingKit.DecryptAndValidateFwdInfo(
|
|
|
|
payload, isFinal, parsed,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.FwdInfo = *fwdInfo
|
|
|
|
}
|
2019-07-30 21:52:17 -07:00
|
|
|
|
2024-04-02 10:59:57 -04:00
|
|
|
return payload, err
|
|
|
|
|
2019-07-30 21:52:17 -07:00
|
|
|
default:
|
2019-11-04 15:10:15 -08:00
|
|
|
return nil, fmt.Errorf("unknown sphinx payload type: %v",
|
2019-08-30 14:11:20 -07:00
|
|
|
r.processedPacket.Payload.Type)
|
2017-05-02 23:57:13 +03:00
|
|
|
}
|
2019-07-30 21:52:17 -07:00
|
|
|
}
|
|
|
|
|
2018-01-16 00:36:14 -08:00
|
|
|
// ExtractErrorEncrypter decodes and returns the ErrorEncrypter for this hop,
|
|
|
|
// along with a failure code to signal if the decoding was successful. The
|
|
|
|
// ErrorEncrypter is used to encrypt errors back to the sender in the event that
|
|
|
|
// a payment fails.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the HopIterator interface.
|
|
|
|
func (r *sphinxHopIterator) ExtractErrorEncrypter(
|
|
|
|
extracter ErrorEncrypterExtracter) (ErrorEncrypter, lnwire.FailCode) {
|
|
|
|
|
2018-03-12 13:37:00 -07:00
|
|
|
return extracter(r.ogPacket.EphemeralKey)
|
2018-01-16 00:36:14 -08:00
|
|
|
}
|
|
|
|
|
2022-12-14 15:02:01 -05:00
|
|
|
// BlindingProcessor is an interface that provides the cryptographic operations
|
|
|
|
// required for processing blinded hops.
|
|
|
|
//
|
|
|
|
// This interface is extracted to allow more granular testing of blinded
|
|
|
|
// forwarding calculations.
|
|
|
|
type BlindingProcessor interface {
|
|
|
|
// DecryptBlindedHopData decrypts a blinded blob of data using the
|
|
|
|
// ephemeral key provided.
|
|
|
|
DecryptBlindedHopData(ephemPub *btcec.PublicKey,
|
|
|
|
encryptedData []byte) ([]byte, error)
|
2024-04-02 09:50:13 -04:00
|
|
|
|
|
|
|
// NextEphemeral returns the next hop's ephemeral key, calculated
|
|
|
|
// from the current ephemeral key provided.
|
|
|
|
NextEphemeral(*btcec.PublicKey) (*btcec.PublicKey, error)
|
2022-12-14 15:02:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlindingKit contains the components required to extract forwarding
|
|
|
|
// information for hops in a blinded route.
|
|
|
|
type BlindingKit struct {
|
|
|
|
// Processor provides the low-level cryptographic operations to
|
|
|
|
// handle an encrypted blob of data in a blinded forward.
|
|
|
|
Processor BlindingProcessor
|
|
|
|
|
|
|
|
// UpdateAddBlinding holds a blinding point that was passed to the
|
|
|
|
// node via update_add_htlc's TLVs.
|
|
|
|
UpdateAddBlinding lnwire.BlindingPointRecord
|
|
|
|
|
|
|
|
// IncomingCltv is the expiry of the incoming HTLC.
|
|
|
|
IncomingCltv uint32
|
|
|
|
|
|
|
|
// IncomingAmount is the amount of the incoming HTLC.
|
|
|
|
IncomingAmount lnwire.MilliSatoshi
|
|
|
|
}
|
|
|
|
|
2024-04-22 14:06:17 -04:00
|
|
|
// getBlindingPoint returns either the payload or updateAddHtlc blinding point,
|
|
|
|
// assuming that validation that these values are appropriately set has already
|
|
|
|
// been handled elsewhere.
|
|
|
|
func (b *BlindingKit) getBlindingPoint(payloadBlinding *btcec.PublicKey) (
|
|
|
|
*btcec.PublicKey, error) {
|
|
|
|
|
2022-12-14 15:02:01 -05:00
|
|
|
payloadBlindingSet := payloadBlinding != nil
|
|
|
|
updateBlindingSet := b.UpdateAddBlinding.IsSome()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case payloadBlindingSet:
|
|
|
|
return payloadBlinding, nil
|
|
|
|
|
|
|
|
case updateBlindingSet:
|
|
|
|
pk, err := b.UpdateAddBlinding.UnwrapOrErr(
|
|
|
|
fmt.Errorf("expected update add blinding"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pk.Val, nil
|
|
|
|
|
2024-04-22 14:06:17 -04:00
|
|
|
default:
|
|
|
|
return nil, ErrNoBlindingPoint
|
|
|
|
}
|
2022-12-14 15:02:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptAndValidateFwdInfo performs all operations required to decrypt and
|
|
|
|
// validate a blinded route.
|
|
|
|
func (b *BlindingKit) DecryptAndValidateFwdInfo(payload *Payload,
|
2024-04-02 15:20:25 -04:00
|
|
|
isFinalHop bool, payloadParsed map[tlv.Type][]byte) (
|
|
|
|
*ForwardingInfo, error) {
|
2022-12-14 15:02:01 -05:00
|
|
|
|
|
|
|
// We expect this function to be called when we have encrypted data
|
2024-04-22 14:06:17 -04:00
|
|
|
// present, and expect validation to already have ensured that a
|
|
|
|
// blinding key is set either in the payload or the
|
2022-12-14 15:02:01 -05:00
|
|
|
// update_add_htlc message.
|
2024-04-22 14:06:17 -04:00
|
|
|
blindingPoint, err := b.getBlindingPoint(payload.blindingPoint)
|
2022-12-14 15:02:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
decrypted, err := b.Processor.DecryptBlindedHopData(
|
|
|
|
blindingPoint, payload.encryptedData,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decrypt blinded "+
|
|
|
|
"data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(decrypted)
|
|
|
|
routeData, err := record.DecodeBlindedRouteData(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %w",
|
|
|
|
ErrDecodeFailed, err)
|
|
|
|
}
|
|
|
|
|
2024-04-02 15:20:25 -04:00
|
|
|
// Validate the contents of the payload against the values we've
|
|
|
|
// just pulled out of the encrypted data blob.
|
|
|
|
err = ValidatePayloadWithBlinded(isFinalHop, payloadParsed)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Validate the data in the blinded route against our incoming htlc's
|
|
|
|
// information.
|
2022-12-14 15:02:01 -05:00
|
|
|
if err := ValidateBlindedRouteData(
|
|
|
|
routeData, b.IncomingAmount, b.IncomingCltv,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fwdAmt, err := calculateForwardingAmount(
|
|
|
|
b.IncomingAmount, routeData.RelayInfo.Val.BaseFee,
|
|
|
|
routeData.RelayInfo.Val.FeeRate,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-02 09:50:13 -04:00
|
|
|
// If we have an override for the blinding point for the next node,
|
|
|
|
// we'll just use it without tweaking (the sender intended to switch
|
|
|
|
// out directly for this blinding point). Otherwise, we'll tweak our
|
|
|
|
// blinding point to get the next ephemeral key.
|
|
|
|
nextEph, err := routeData.NextBlindingOverride.UnwrapOrFuncErr(
|
|
|
|
func() (tlv.RecordT[tlv.TlvType8,
|
|
|
|
*btcec.PublicKey], error) {
|
|
|
|
|
|
|
|
next, err := b.Processor.NextEphemeral(blindingPoint)
|
|
|
|
if err != nil {
|
|
|
|
// Return a zero record because we expect the
|
|
|
|
// error to be checked.
|
|
|
|
return routeData.NextBlindingOverride.Zero(),
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlv.NewPrimitiveRecord[tlv.TlvType8](next), nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:02:01 -05:00
|
|
|
return &ForwardingInfo{
|
|
|
|
NextHop: routeData.ShortChannelID.Val,
|
|
|
|
AmountToForward: fwdAmt,
|
|
|
|
OutgoingCTLV: b.IncomingCltv - uint32(
|
|
|
|
routeData.RelayInfo.Val.CltvExpiryDelta,
|
|
|
|
),
|
2024-04-02 09:50:13 -04:00
|
|
|
// Remap from blinding override type to blinding point type.
|
|
|
|
NextBlinding: tlv.SomeRecordT(
|
|
|
|
tlv.NewPrimitiveRecord[lnwire.BlindingPointTlvType](
|
|
|
|
nextEph.Val),
|
|
|
|
),
|
2022-12-14 15:02:01 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:00:37 -05:00
|
|
|
// calculateForwardingAmount calculates the amount to forward for a blinded
|
|
|
|
// hop based on the incoming amount and forwarding parameters.
|
|
|
|
//
|
|
|
|
// When forwarding a payment, the fee we take is calculated, not on the
|
|
|
|
// incoming amount, but rather on the amount we forward. We charge fees based
|
|
|
|
// on our own liquidity we are forwarding downstream.
|
|
|
|
//
|
|
|
|
// With route blinding, we are NOT given the amount to forward. This
|
|
|
|
// unintuitive looking formula comes from the fact that without the amount to
|
|
|
|
// forward, we cannot compute the fees taken directly.
|
|
|
|
//
|
|
|
|
// The amount to be forwarded can be computed as follows:
|
|
|
|
//
|
|
|
|
// amt_to_forward = incoming_amount - total_fees
|
|
|
|
// total_fees = base_fee + amt_to_forward*(fee_rate/1000000)
|
|
|
|
//
|
|
|
|
// Solving for amount_to_forward:
|
|
|
|
// amt_to_forward = incoming_amount - base_fee - (amount_to_forward * fee_rate)/1e6
|
|
|
|
// amt_to_forward + (amount_to_forward * fee_rate) / 1e6 = incoming_amount - base_fee
|
|
|
|
// amt_to_forward * 1e6 + (amount_to_forward * fee_rate) = (incoming_amount - base_fee) * 1e6
|
|
|
|
// amt_to_forward * (1e6 + fee_rate) = (incoming_amount - base_fee) * 1e6
|
|
|
|
// amt_to_forward = ((incoming_amount - base_fee) * 1e6) / (1e6 + fee_rate)
|
|
|
|
//
|
|
|
|
// From there we use a ceiling formula for integer division so that we always
|
|
|
|
// round up, otherwise the sender may receive slightly less than intended:
|
|
|
|
//
|
|
|
|
// ceil(a/b) = (a + b - 1)/(b).
|
|
|
|
//
|
|
|
|
//nolint:lll,dupword
|
|
|
|
func calculateForwardingAmount(incomingAmount lnwire.MilliSatoshi, baseFee,
|
|
|
|
proportionalFee uint32) (lnwire.MilliSatoshi, error) {
|
|
|
|
|
|
|
|
// Sanity check to prevent overflow.
|
|
|
|
if incomingAmount < lnwire.MilliSatoshi(baseFee) {
|
|
|
|
return 0, fmt.Errorf("incoming amount: %v < base fee: %v",
|
|
|
|
incomingAmount, baseFee)
|
|
|
|
}
|
|
|
|
numerator := (uint64(incomingAmount) - uint64(baseFee)) * 1e6
|
|
|
|
denominator := 1e6 + uint64(proportionalFee)
|
|
|
|
|
|
|
|
ceiling := (numerator + denominator - 1) / denominator
|
|
|
|
|
|
|
|
return lnwire.MilliSatoshi(ceiling), nil
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
// OnionProcessor is responsible for keeping all sphinx dependent parts inside
|
2017-05-02 23:57:13 +03:00
|
|
|
// and expose only decoding function. With such approach we give freedom for
|
2017-06-16 23:29:38 +02:00
|
|
|
// subsystems which wants to decode sphinx path to not be dependable from
|
|
|
|
// sphinx at all.
|
2017-05-02 23:57:13 +03:00
|
|
|
//
|
|
|
|
// NOTE: The reason for keeping decoder separated from hop iterator is too
|
|
|
|
// maintain the hop iterator abstraction. Without it the structures which using
|
2017-06-16 23:29:38 +02:00
|
|
|
// the hop iterator should contain sphinx router which makes their creations in
|
|
|
|
// tests dependent from the sphinx internal parts.
|
2017-06-29 16:40:45 +03:00
|
|
|
type OnionProcessor struct {
|
2017-05-02 23:57:13 +03:00
|
|
|
router *sphinx.Router
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:40:45 +03:00
|
|
|
// NewOnionProcessor creates new instance of decoder.
|
|
|
|
func NewOnionProcessor(router *sphinx.Router) *OnionProcessor {
|
|
|
|
return &OnionProcessor{router}
|
2017-05-02 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
2018-01-16 00:36:14 -08:00
|
|
|
// Start spins up the onion processor's sphinx router.
|
|
|
|
func (p *OnionProcessor) Start() error {
|
2022-01-29 22:47:50 +08:00
|
|
|
log.Info("Onion processor starting")
|
2018-01-16 00:36:14 -08:00
|
|
|
return p.router.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop shutsdown the onion processor's sphinx router.
|
|
|
|
func (p *OnionProcessor) Stop() error {
|
2021-09-02 20:26:00 +08:00
|
|
|
|
2023-09-08 02:16:42 +08:00
|
|
|
log.Info("Onion processor shutting down...")
|
|
|
|
defer log.Debug("Onion processor shutdown complete")
|
2021-09-02 20:26:00 +08:00
|
|
|
|
2018-01-16 00:36:14 -08:00
|
|
|
p.router.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-02 10:56:08 -04:00
|
|
|
// ReconstructBlindingInfo contains the information required to reconstruct a
|
|
|
|
// blinded onion.
|
|
|
|
type ReconstructBlindingInfo struct {
|
|
|
|
// BlindingKey is the blinding point set in UpdateAddHTLC.
|
|
|
|
BlindingKey lnwire.BlindingPointRecord
|
|
|
|
|
|
|
|
// IncomingAmt is the amount for the incoming HTLC.
|
|
|
|
IncomingAmt lnwire.MilliSatoshi
|
|
|
|
|
|
|
|
// IncomingExpiry is the expiry height of the incoming HTLC.
|
|
|
|
IncomingExpiry uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReconstructHopIterator attempts to decode a valid sphinx packet from the
|
|
|
|
// passed io.Reader instance using the rHash as the associated data when
|
|
|
|
// checking the relevant MACs during the decoding process.
|
2023-11-06 15:45:27 -05:00
|
|
|
func (p *OnionProcessor) ReconstructHopIterator(r io.Reader, rHash []byte,
|
2024-04-02 10:56:08 -04:00
|
|
|
blindingInfo ReconstructBlindingInfo) (Iterator, error) {
|
2019-08-26 14:06:57 +02:00
|
|
|
|
|
|
|
onionPkt := &sphinx.OnionPacket{}
|
|
|
|
if err := onionPkt.Decode(r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-06 15:45:27 -05:00
|
|
|
var opts []sphinx.ProcessOnionOpt
|
2024-04-02 10:56:08 -04:00
|
|
|
blindingInfo.BlindingKey.WhenSome(func(
|
|
|
|
r tlv.RecordT[lnwire.BlindingPointTlvType, *btcec.PublicKey]) {
|
|
|
|
|
|
|
|
opts = append(opts, sphinx.WithBlindingPoint(r.Val))
|
|
|
|
})
|
2023-11-06 15:45:27 -05:00
|
|
|
|
2019-08-26 14:06:57 +02:00
|
|
|
// Attempt to process the Sphinx packet. We include the payment hash of
|
|
|
|
// the HTLC as it's authenticated within the Sphinx packet itself as
|
|
|
|
// associated data in order to thwart attempts a replay attacks. In the
|
|
|
|
// case of a replay, an attacker is *forced* to use the same payment
|
|
|
|
// hash twice, thereby losing their money entirely.
|
2023-11-06 15:45:27 -05:00
|
|
|
sphinxPacket, err := p.router.ReconstructOnionPacket(
|
|
|
|
onionPkt, rHash, opts...,
|
|
|
|
)
|
2019-08-26 14:06:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-02 10:56:08 -04:00
|
|
|
return makeSphinxHopIterator(onionPkt, sphinxPacket, BlindingKit{
|
|
|
|
Processor: p.router,
|
|
|
|
UpdateAddBlinding: blindingInfo.BlindingKey,
|
|
|
|
IncomingAmount: blindingInfo.IncomingAmt,
|
|
|
|
IncomingCltv: blindingInfo.IncomingExpiry,
|
|
|
|
}), nil
|
2019-08-26 14:06:57 +02:00
|
|
|
}
|
|
|
|
|
2018-01-16 00:36:14 -08:00
|
|
|
// DecodeHopIteratorRequest encapsulates all date necessary to process an onion
|
|
|
|
// packet, perform sphinx replay detection, and schedule the entry for garbage
|
|
|
|
// collection.
|
|
|
|
type DecodeHopIteratorRequest struct {
|
2023-01-27 14:19:56 -05:00
|
|
|
OnionReader io.Reader
|
|
|
|
RHash []byte
|
|
|
|
IncomingCltv uint32
|
|
|
|
IncomingAmount lnwire.MilliSatoshi
|
2024-04-02 08:46:14 -04:00
|
|
|
BlindingPoint lnwire.BlindingPointRecord
|
2018-01-16 00:36:14 -08:00
|
|
|
}
|
2017-10-10 19:36:52 -07:00
|
|
|
|
2018-01-16 00:36:14 -08:00
|
|
|
// DecodeHopIteratorResponse encapsulates the outcome of a batched sphinx onion
|
|
|
|
// processing.
|
|
|
|
type DecodeHopIteratorResponse struct {
|
2019-09-05 13:35:39 +02:00
|
|
|
HopIterator Iterator
|
2018-01-16 00:36:14 -08:00
|
|
|
FailCode lnwire.FailCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result returns the (HopIterator, lnwire.FailCode) tuple, which should
|
|
|
|
// correspond to the index of a particular DecodeHopIteratorRequest.
|
|
|
|
//
|
|
|
|
// NOTE: The HopIterator should be considered invalid if the fail code is
|
|
|
|
// anything but lnwire.CodeNone.
|
2019-09-05 13:35:39 +02:00
|
|
|
func (r *DecodeHopIteratorResponse) Result() (Iterator, lnwire.FailCode) {
|
2018-01-16 00:36:14 -08:00
|
|
|
return r.HopIterator, r.FailCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeHopIterators performs batched decoding and validation of incoming
|
|
|
|
// sphinx packets. For the same `id`, this method will return the same iterators
|
|
|
|
// and failcodes upon subsequent invocations.
|
|
|
|
//
|
|
|
|
// NOTE: In order for the responses to be valid, the caller must guarantee that
|
|
|
|
// the presented readers and rhashes *NEVER* deviate across invocations for the
|
|
|
|
// same id.
|
|
|
|
func (p *OnionProcessor) DecodeHopIterators(id []byte,
|
|
|
|
reqs []DecodeHopIteratorRequest) ([]DecodeHopIteratorResponse, error) {
|
|
|
|
|
|
|
|
var (
|
|
|
|
batchSize = len(reqs)
|
|
|
|
onionPkts = make([]sphinx.OnionPacket, batchSize)
|
|
|
|
resps = make([]DecodeHopIteratorResponse, batchSize)
|
|
|
|
)
|
|
|
|
|
|
|
|
tx := p.router.BeginTxn(id, batchSize)
|
|
|
|
|
2021-04-27 16:43:06 +02:00
|
|
|
decode := func(seqNum uint16, onionPkt *sphinx.OnionPacket,
|
|
|
|
req DecodeHopIteratorRequest) lnwire.FailCode {
|
2018-01-16 00:36:14 -08:00
|
|
|
|
|
|
|
err := onionPkt.Decode(req.OnionReader)
|
2017-09-12 18:10:30 +02:00
|
|
|
switch err {
|
2018-01-16 00:36:14 -08:00
|
|
|
case nil:
|
|
|
|
// success
|
|
|
|
|
2017-09-12 18:10:30 +02:00
|
|
|
case sphinx.ErrInvalidOnionVersion:
|
2021-04-27 16:43:06 +02:00
|
|
|
return lnwire.CodeInvalidOnionVersion
|
2018-01-16 00:36:14 -08:00
|
|
|
|
2017-09-12 18:10:30 +02:00
|
|
|
case sphinx.ErrInvalidOnionKey:
|
2021-04-27 16:43:06 +02:00
|
|
|
return lnwire.CodeInvalidOnionKey
|
2018-01-16 00:36:14 -08:00
|
|
|
|
2017-09-12 18:10:30 +02:00
|
|
|
default:
|
2017-12-10 15:52:26 -08:00
|
|
|
log.Errorf("unable to decode onion packet: %v", err)
|
2021-04-27 16:43:06 +02:00
|
|
|
return lnwire.CodeInvalidOnionKey
|
2018-01-16 00:36:14 -08:00
|
|
|
}
|
|
|
|
|
2023-11-06 15:40:29 -05:00
|
|
|
var opts []sphinx.ProcessOnionOpt
|
2024-04-02 08:46:14 -04:00
|
|
|
req.BlindingPoint.WhenSome(func(
|
|
|
|
b tlv.RecordT[lnwire.BlindingPointTlvType,
|
|
|
|
*btcec.PublicKey]) {
|
|
|
|
|
2023-11-06 15:40:29 -05:00
|
|
|
opts = append(opts, sphinx.WithBlindingPoint(
|
2024-04-02 08:46:14 -04:00
|
|
|
b.Val,
|
2023-11-06 15:40:29 -05:00
|
|
|
))
|
2024-04-02 08:46:14 -04:00
|
|
|
})
|
2018-01-16 00:36:14 -08:00
|
|
|
err = tx.ProcessOnionPacket(
|
2023-11-06 15:40:29 -05:00
|
|
|
seqNum, onionPkt, req.RHash, req.IncomingCltv, opts...,
|
2018-01-16 00:36:14 -08:00
|
|
|
)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
// success
|
2021-04-27 16:43:06 +02:00
|
|
|
return lnwire.CodeNone
|
2018-01-16 00:36:14 -08:00
|
|
|
|
|
|
|
case sphinx.ErrInvalidOnionVersion:
|
2021-04-27 16:43:06 +02:00
|
|
|
return lnwire.CodeInvalidOnionVersion
|
2018-01-16 00:36:14 -08:00
|
|
|
|
|
|
|
case sphinx.ErrInvalidOnionHMAC:
|
2021-04-27 16:43:06 +02:00
|
|
|
return lnwire.CodeInvalidOnionHmac
|
2018-01-16 00:36:14 -08:00
|
|
|
|
|
|
|
case sphinx.ErrInvalidOnionKey:
|
2021-04-27 16:43:06 +02:00
|
|
|
return lnwire.CodeInvalidOnionKey
|
2018-01-16 00:36:14 -08:00
|
|
|
|
|
|
|
default:
|
|
|
|
log.Errorf("unable to process onion packet: %v", err)
|
2021-04-27 16:43:06 +02:00
|
|
|
return lnwire.CodeInvalidOnionKey
|
2018-01-16 00:36:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 16:51:27 +02:00
|
|
|
// Execute cpu-heavy onion decoding in parallel.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := range reqs {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(seqNum uint16) {
|
|
|
|
defer wg.Done()
|
2021-04-27 16:43:06 +02:00
|
|
|
|
2021-04-27 16:51:27 +02:00
|
|
|
onionPkt := &onionPkts[seqNum]
|
|
|
|
|
|
|
|
resps[seqNum].FailCode = decode(
|
|
|
|
seqNum, onionPkt, reqs[seqNum],
|
|
|
|
)
|
|
|
|
}(uint16(i))
|
2021-04-27 16:43:06 +02:00
|
|
|
}
|
2021-04-27 16:51:27 +02:00
|
|
|
wg.Wait()
|
2021-04-27 16:43:06 +02:00
|
|
|
|
2018-01-16 00:36:14 -08:00
|
|
|
// With that batch created, we will now attempt to write the shared
|
|
|
|
// secrets to disk. This operation will returns the set of indices that
|
|
|
|
// were detected as replays, and the computed sphinx packets for all
|
|
|
|
// indices that did not fail the above loop. Only indices that are not
|
|
|
|
// in the replay set should be considered valid, as they are
|
|
|
|
// opportunistically computed.
|
|
|
|
packets, replays, err := tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to process onion packet batch %x: %v",
|
|
|
|
id, err)
|
|
|
|
|
|
|
|
// If we failed to commit the batch to the secret share log, we
|
|
|
|
// will mark all not-yet-failed channels with a temporary
|
|
|
|
// channel failure and exit since we cannot proceed.
|
|
|
|
for i := range resps {
|
|
|
|
resp := &resps[i]
|
|
|
|
|
|
|
|
// Skip any indexes that already failed onion decoding.
|
|
|
|
if resp.FailCode != lnwire.CodeNone {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Errorf("unable to process onion packet %x-%v",
|
|
|
|
id, i)
|
|
|
|
resp.FailCode = lnwire.CodeTemporaryChannelFailure
|
2017-09-12 18:10:30 +02:00
|
|
|
}
|
2018-01-16 00:36:14 -08:00
|
|
|
|
|
|
|
// TODO(conner): return real errors to caller so link can fail?
|
|
|
|
return resps, err
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2018-01-16 00:36:14 -08:00
|
|
|
// Otherwise, the commit was successful. Now we will post process any
|
|
|
|
// remaining packets, additionally failing any that were included in the
|
|
|
|
// replay set.
|
|
|
|
for i := range resps {
|
|
|
|
resp := &resps[i]
|
|
|
|
|
|
|
|
// Skip any indexes that already failed onion decoding.
|
|
|
|
if resp.FailCode != lnwire.CodeNone {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this index is contained in the replay set, mark it with a
|
|
|
|
// temporary channel failure error code. We infer that the
|
|
|
|
// offending error was due to a replayed packet because this
|
|
|
|
// index was found in the replay set.
|
|
|
|
if replays.Contains(uint16(i)) {
|
|
|
|
log.Errorf("unable to process onion packet: %v",
|
|
|
|
sphinx.ErrReplayedPacket)
|
2023-10-11 01:08:43 -07:00
|
|
|
|
|
|
|
// We set FailCode to CodeInvalidOnionVersion even
|
|
|
|
// though the ephemeral key isn't the problem. We need
|
|
|
|
// to set the BADONION bit since we're sending back a
|
|
|
|
// malformed packet, but as there isn't a specific
|
|
|
|
// failure code for replays, we reuse one of the
|
|
|
|
// failure codes that has BADONION.
|
|
|
|
resp.FailCode = lnwire.CodeInvalidOnionVersion
|
2018-01-16 00:36:14 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, construct a hop iterator from our processed sphinx
|
|
|
|
// packet, simultaneously caching the original onion packet.
|
2024-04-02 10:56:08 -04:00
|
|
|
resp.HopIterator = makeSphinxHopIterator(
|
|
|
|
&onionPkts[i], &packets[i], BlindingKit{
|
|
|
|
Processor: p.router,
|
|
|
|
UpdateAddBlinding: reqs[i].BlindingPoint,
|
|
|
|
IncomingAmount: reqs[i].IncomingAmount,
|
|
|
|
IncomingCltv: reqs[i].IncomingCltv,
|
|
|
|
},
|
|
|
|
)
|
2018-01-16 00:36:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return resps, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExtractErrorEncrypter takes an io.Reader which should contain the onion
|
|
|
|
// packet as original received by a forwarding node and creates an
|
|
|
|
// ErrorEncrypter instance using the derived shared secret. In the case that en
|
|
|
|
// error occurs, a lnwire failure code detailing the parsing failure will be
|
|
|
|
// returned.
|
2018-03-12 13:37:00 -07:00
|
|
|
func (p *OnionProcessor) ExtractErrorEncrypter(ephemeralKey *btcec.PublicKey) (
|
2018-01-16 00:36:14 -08:00
|
|
|
ErrorEncrypter, lnwire.FailCode) {
|
|
|
|
|
2018-03-12 13:37:00 -07:00
|
|
|
onionObfuscator, err := sphinx.NewOnionErrorEncrypter(
|
|
|
|
p.router, ephemeralKey,
|
|
|
|
)
|
2017-06-29 16:40:45 +03:00
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case sphinx.ErrInvalidOnionVersion:
|
|
|
|
return nil, lnwire.CodeInvalidOnionVersion
|
|
|
|
case sphinx.ErrInvalidOnionHMAC:
|
|
|
|
return nil, lnwire.CodeInvalidOnionHmac
|
|
|
|
case sphinx.ErrInvalidOnionKey:
|
|
|
|
return nil, lnwire.CodeInvalidOnionKey
|
|
|
|
default:
|
2017-12-10 15:52:26 -08:00
|
|
|
log.Errorf("unable to process onion packet: %v", err)
|
2018-01-08 17:50:19 -08:00
|
|
|
return nil, lnwire.CodeInvalidOnionKey
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 19:36:52 -07:00
|
|
|
return &SphinxErrorEncrypter{
|
|
|
|
OnionErrorEncrypter: onionObfuscator,
|
2018-03-12 13:37:00 -07:00
|
|
|
EphemeralKey: ephemeralKey,
|
2017-06-29 16:40:45 +03:00
|
|
|
}, lnwire.CodeNone
|
2017-05-02 23:57:13 +03:00
|
|
|
}
|