2017-06-29 15:40:45 +02:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lightning-onion"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2017-10-11 04:36:52 +02:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2017-06-29 15:40:45 +02:00
|
|
|
)
|
|
|
|
|
2017-10-11 04:31:44 +02:00
|
|
|
// ForwardingError wraps an lnwire.FailureMessage in a struct that also
|
|
|
|
// includes the source of the error.
|
|
|
|
type ForwardingError struct {
|
|
|
|
// ErrorSource is the public key of the node that sent the error. With
|
|
|
|
// this information, the dispatcher of a payment can modify their set
|
|
|
|
// of candidate routes in response to the type of error extracted.
|
|
|
|
ErrorSource *btcec.PublicKey
|
|
|
|
|
|
|
|
lnwire.FailureMessage
|
|
|
|
}
|
2017-10-11 04:36:52 +02:00
|
|
|
|
|
|
|
// ErrorDecrypter is an interface that is used to decrypt the onion encrypted
|
2017-07-15 05:08:29 +02:00
|
|
|
// failure reason an extra out a well formed error.
|
2017-10-11 04:36:52 +02:00
|
|
|
type ErrorDecrypter interface {
|
|
|
|
// DecryptError peels off each layer of onion encryption from the first
|
2017-07-15 05:08:29 +02:00
|
|
|
// hop, to the source of the error. A fully populated
|
2017-10-11 04:36:52 +02:00
|
|
|
// lnwire.FailureMessage is returned along with the source of the
|
|
|
|
// error.
|
|
|
|
DecryptError(lnwire.OpaqueReason) (*ForwardingError, error)
|
2017-06-29 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 04:36:52 +02:00
|
|
|
// ErrorEncrypter is an interface that is used to encrypt HTLC related errors
|
|
|
|
// at the source of the error, and also at each intermediate hop all the way
|
|
|
|
// back to the source of the payment.
|
|
|
|
type ErrorEncrypter interface {
|
|
|
|
// EncryptFirstHop transforms a concrete failure message into an
|
2017-07-15 05:08:29 +02:00
|
|
|
// encrypted opaque failure reason. This method will be used at the
|
2017-10-11 04:36:52 +02:00
|
|
|
// source that the error occurs. It differs from IntermediateEncrypt
|
2017-07-15 05:08:29 +02:00
|
|
|
// slightly, in that it computes a proper MAC over the error.
|
2017-10-11 04:36:52 +02:00
|
|
|
EncryptFirstHop(lnwire.FailureMessage) (lnwire.OpaqueReason, error)
|
2017-06-29 15:40:45 +02:00
|
|
|
|
2017-10-11 04:36:52 +02:00
|
|
|
// IntermediateEncrypt wraps an already encrypted opaque reason error
|
|
|
|
// in an additional layer of onion encryption. This process repeats
|
|
|
|
// until the error arrives at the source of the payment.
|
|
|
|
IntermediateEncrypt(lnwire.OpaqueReason) lnwire.OpaqueReason
|
2017-06-29 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 04:36:52 +02:00
|
|
|
// SphinxErrorEncrypter is a concrete implementation of both the ErrorEncrypter
|
|
|
|
// interface backed by an implementation of the Sphinx packet format. As a
|
|
|
|
// result, all errors handled are themselves wrapped in layers of onion
|
|
|
|
// encryption and must be treated as such accordingly.
|
|
|
|
type SphinxErrorEncrypter struct {
|
|
|
|
*sphinx.OnionErrorEncrypter
|
2017-06-29 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 04:36:52 +02:00
|
|
|
// EncryptFirstHop transforms a concrete failure message into an encrypted
|
2017-07-15 05:08:29 +02:00
|
|
|
// opaque failure reason. This method will be used at the source that the error
|
|
|
|
// occurs. It differs from BackwardObfuscate slightly, in that it computes a
|
|
|
|
// proper MAC over the error.
|
2017-06-29 15:40:45 +02:00
|
|
|
//
|
2017-10-11 04:36:52 +02:00
|
|
|
// NOTE: Part of the ErrorEncrypter interface.
|
|
|
|
func (o *SphinxErrorEncrypter) EncryptFirstHop(failure lnwire.FailureMessage) (lnwire.OpaqueReason, error) {
|
2017-06-29 15:40:45 +02:00
|
|
|
var b bytes.Buffer
|
|
|
|
if err := lnwire.EncodeFailure(&b, failure, 0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-15 05:08:29 +02:00
|
|
|
// We pass a true as the first parameter to indicate that a MAC should
|
|
|
|
// be added.
|
2017-10-11 04:36:52 +02:00
|
|
|
return o.EncryptError(true, b.Bytes()), nil
|
2017-06-29 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 04:36:52 +02:00
|
|
|
// IntermediateEncrypt wraps an already encrypted opaque reason error in an
|
2017-07-15 05:08:29 +02:00
|
|
|
// additional layer of onion encryption. This process repeats until the error
|
|
|
|
// arrives at the source of the payment. We re-encrypt the message on the
|
|
|
|
// backwards path to ensure that the error is indistinguishable from any other
|
|
|
|
// error seen.
|
2017-06-29 15:40:45 +02:00
|
|
|
//
|
2017-10-11 04:36:52 +02:00
|
|
|
// NOTE: Part of the ErrorEncrypter interface.
|
|
|
|
func (s *SphinxErrorEncrypter) IntermediateEncrypt(reason lnwire.OpaqueReason) lnwire.OpaqueReason {
|
|
|
|
return s.EncryptError(false, reason)
|
2017-06-29 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 04:36:52 +02:00
|
|
|
// A compile time check to ensure SphinxErrorEncrypter implements the
|
|
|
|
// ErrorEncrypter interface.
|
|
|
|
var _ ErrorEncrypter = (*SphinxErrorEncrypter)(nil)
|
2017-06-29 15:40:45 +02:00
|
|
|
|
2017-10-11 04:36:52 +02:00
|
|
|
// SphinxErrorDecrypter wraps the sphinx data SphinxErrorDecrypter and maps the
|
|
|
|
// returned errors to concrete lnwire.FailureMessage instances.
|
|
|
|
type SphinxErrorDecrypter struct {
|
|
|
|
*sphinx.OnionErrorDecrypter
|
2017-06-29 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 04:36:52 +02:00
|
|
|
// DecryptError peels off each layer of onion encryption from the first hop, to
|
|
|
|
// the source of the error. A fully populated lnwire.FailureMessage is returned
|
|
|
|
// along with the source of the error.
|
2017-06-29 15:40:45 +02:00
|
|
|
//
|
2017-10-11 04:36:52 +02:00
|
|
|
// NOTE: Part of the ErrorDecrypter interface.
|
|
|
|
func (s *SphinxErrorDecrypter) DecryptError(reason lnwire.OpaqueReason) (*ForwardingError, error) {
|
2017-07-15 05:08:29 +02:00
|
|
|
|
2017-10-11 04:36:52 +02:00
|
|
|
source, failureData, err := s.OnionErrorDecrypter.DecryptError(reason)
|
2017-06-29 15:40:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := bytes.NewReader(failureData)
|
2017-10-11 04:36:52 +02:00
|
|
|
failureMsg, err := lnwire.DecodeFailure(r, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ForwardingError{
|
|
|
|
ErrorSource: source,
|
|
|
|
FailureMessage: failureMsg,
|
|
|
|
}, nil
|
2017-06-29 15:40:45 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 04:36:52 +02:00
|
|
|
// A compile time check to ensure ErrorDecrypter implements the Deobfuscator
|
|
|
|
// interface.
|
|
|
|
var _ ErrorDecrypter = (*SphinxErrorDecrypter)(nil)
|