2017-06-29 15:40:45 +02:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-10-17 03:15:46 +02:00
|
|
|
"fmt"
|
2017-06-29 15:40:45 +02:00
|
|
|
|
2019-06-11 10:24:19 +02:00
|
|
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
2019-09-05 13:35:39 +02:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
2017-06-29 15:40:45 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
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 {
|
2019-06-11 10:24:19 +02:00
|
|
|
// FailureSourceIdx is the index of the node that sent the failure. With
|
|
|
|
// this information, the dispatcher of a payment can modify their set of
|
|
|
|
// candidate routes in response to the type of failure extracted. Index
|
|
|
|
// zero is the self node.
|
|
|
|
FailureSourceIdx int
|
2017-10-11 04:31:44 +02:00
|
|
|
|
2017-10-17 03:15:46 +02:00
|
|
|
// ExtraMsg is an additional error message that callers can provide in
|
|
|
|
// order to provide context specific error details.
|
|
|
|
ExtraMsg string
|
|
|
|
|
2017-10-11 04:31:44 +02:00
|
|
|
lnwire.FailureMessage
|
|
|
|
}
|
2017-10-11 04:36:52 +02:00
|
|
|
|
2017-10-17 03:15:46 +02:00
|
|
|
// Error implements the built-in error interface. We use this method to allow
|
|
|
|
// the switch or any callers to insert additional context to the error message
|
|
|
|
// returned.
|
|
|
|
func (f *ForwardingError) Error() string {
|
|
|
|
if f.ExtraMsg == "" {
|
2019-06-19 15:09:23 +02:00
|
|
|
return fmt.Sprintf("%v", f.FailureMessage)
|
2017-10-17 03:15:46 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 15:09:23 +02:00
|
|
|
return fmt.Sprintf("%v: %v", f.FailureMessage, f.ExtraMsg)
|
2017-10-17 03:15:46 +02:00
|
|
|
}
|
|
|
|
|
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-12-11 00:37:21 +01:00
|
|
|
// UnknownEncrypterType is an error message used to signal that an unexpected
|
|
|
|
// EncrypterType was encountered during decoding.
|
2019-09-05 13:35:39 +02:00
|
|
|
type UnknownEncrypterType hop.EncrypterType
|
2017-12-11 00:37:21 +01:00
|
|
|
|
|
|
|
// Error returns a formatted error indicating the invalid EncrypterType.
|
|
|
|
func (e UnknownEncrypterType) Error() string {
|
|
|
|
return fmt.Sprintf("unknown error encrypter type: %d", e)
|
|
|
|
}
|
|
|
|
|
2019-06-19 15:04:17 +02:00
|
|
|
// OnionErrorDecrypter is the interface that provides onion level error
|
|
|
|
// decryption.
|
|
|
|
type OnionErrorDecrypter interface {
|
|
|
|
// DecryptError attempts to decrypt the passed encrypted error response.
|
|
|
|
// The onion failure is encrypted in backward manner, starting from the
|
|
|
|
// node where error have occurred. As a result, in order to decrypt the
|
|
|
|
// error we need get all shared secret and apply decryption in the
|
|
|
|
// reverse order.
|
|
|
|
DecryptError(encryptedData []byte) (*sphinx.DecryptedError, error)
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-06-19 15:04:17 +02:00
|
|
|
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.
|
2019-06-19 15:04:17 +02:00
|
|
|
func (s *SphinxErrorDecrypter) DecryptError(reason lnwire.OpaqueReason) (
|
|
|
|
*ForwardingError, error) {
|
2017-07-15 05:08:29 +02:00
|
|
|
|
2019-06-11 10:24:19 +02:00
|
|
|
failure, err := s.OnionErrorDecrypter.DecryptError(reason)
|
2017-06-29 15:40:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-19 15:09:23 +02:00
|
|
|
// Decode the failure. If an error occurs, we leave the failure message
|
|
|
|
// field nil.
|
2019-06-11 10:24:19 +02:00
|
|
|
r := bytes.NewReader(failure.Message)
|
2017-10-11 04:36:52 +02:00
|
|
|
failureMsg, err := lnwire.DecodeFailure(r, 0)
|
|
|
|
if err != nil {
|
2019-06-19 15:09:23 +02:00
|
|
|
return &ForwardingError{
|
|
|
|
FailureSourceIdx: failure.SenderIdx,
|
|
|
|
}, nil
|
2017-10-11 04:36:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ForwardingError{
|
2019-06-11 10:24:19 +02:00
|
|
|
FailureSourceIdx: failure.SenderIdx,
|
|
|
|
FailureMessage: failureMsg,
|
2017-10-11 04:36:52 +02:00
|
|
|
}, 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)
|