lnwire: add invalid onion blinding error code

This commit is contained in:
Carla Kirk-Cohen 2023-10-12 09:52:55 -04:00
parent 2fee3f6efa
commit fd8381bf2a
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91
2 changed files with 53 additions and 0 deletions

View File

@ -81,6 +81,7 @@ const (
CodeExpiryTooFar FailCode = 21 CodeExpiryTooFar FailCode = 21
CodeInvalidOnionPayload = FlagPerm | 22 CodeInvalidOnionPayload = FlagPerm | 22
CodeMPPTimeout FailCode = 23 CodeMPPTimeout FailCode = 23
CodeInvalidBlinding = FlagBadOnion | FlagPerm | 24 //nolint:lll
) )
// String returns the string representation of the failure code. // String returns the string representation of the failure code.
@ -158,6 +159,9 @@ func (c FailCode) String() string {
case CodeMPPTimeout: case CodeMPPTimeout:
return "MPPTimeout" return "MPPTimeout"
case CodeInvalidBlinding:
return "InvalidBlinding"
default: default:
return "<unknown>" return "<unknown>"
} }
@ -1233,6 +1237,51 @@ func (f *FailMPPTimeout) Error() string {
return f.Code().String() return f.Code().String()
} }
// FailInvalidBlinding is returned if there has been a route blinding related
// error.
type FailInvalidBlinding struct {
OnionSHA256 [sha256.Size]byte
}
// Code returns the failure unique code.
//
// NOTE: Part of the FailureMessage interface.
func (f *FailInvalidBlinding) Code() FailCode {
return CodeInvalidBlinding
}
// Returns a human readable string describing the target FailureMessage.
//
// NOTE: Implements the error interface.
func (f *FailInvalidBlinding) Error() string {
return f.Code().String()
}
// Decode decodes the failure from bytes stream.
//
// NOTE: Part of the Serializable interface.
func (f *FailInvalidBlinding) Decode(r io.Reader, _ uint32) error {
return ReadElement(r, f.OnionSHA256[:])
}
// Encode writes the failure in bytes stream.
//
// NOTE: Part of the Serializable interface.
func (f *FailInvalidBlinding) Encode(w *bytes.Buffer, _ uint32) error {
return WriteBytes(w, f.OnionSHA256[:])
}
// NewInvalidBlinding creates new instance of FailInvalidBlinding.
func NewInvalidBlinding(onion []byte) *FailInvalidBlinding {
// The spec allows empty onion hashes for invalid blinding, so we only
// include our onion hash if it's provided.
if onion == nil {
return &FailInvalidBlinding{}
}
return &FailInvalidBlinding{OnionSHA256: sha256.Sum256(onion)}
}
// DecodeFailure decodes, validates, and parses the lnwire onion failure, for // DecodeFailure decodes, validates, and parses the lnwire onion failure, for
// the provided protocol version. // the provided protocol version.
func DecodeFailure(r io.Reader, pver uint32) (FailureMessage, error) { func DecodeFailure(r io.Reader, pver uint32) (FailureMessage, error) {
@ -1451,6 +1500,9 @@ func makeEmptyOnionError(code FailCode) (FailureMessage, error) {
case CodeMPPTimeout: case CodeMPPTimeout:
return &FailMPPTimeout{}, nil return &FailMPPTimeout{}, nil
case CodeInvalidBlinding:
return &FailInvalidBlinding{}, nil
default: default:
return nil, errors.Errorf("unknown error code: %v", code) return nil, errors.Errorf("unknown error code: %v", code)
} }

View File

@ -56,6 +56,7 @@ var onionFailures = []FailureMessage{
NewFinalIncorrectCltvExpiry(testCtlvExpiry), NewFinalIncorrectCltvExpiry(testCtlvExpiry),
NewFinalIncorrectHtlcAmount(testAmount), NewFinalIncorrectHtlcAmount(testAmount),
NewInvalidOnionPayload(testType, testOffset), NewInvalidOnionPayload(testType, testOffset),
NewInvalidBlinding(testOnionHash),
} }
// TestEncodeDecodeCode tests the ability of onion errors to be properly encoded // TestEncodeDecodeCode tests the ability of onion errors to be properly encoded