From 9260f576bdf164bc7490320f4499366c5bcc125a Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Tue, 17 Jan 2023 13:44:32 +0100 Subject: [PATCH] initialize NewOnionErrorEncrypter with shared secret directly Allow for more flexible usage of the error encrypter. This is useful when upgrading an existing legacy error encrypter to fat errors in lnd. --- obfuscation.go | 11 ++--------- obfuscation_test.go | 16 ++++++---------- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/obfuscation.go b/obfuscation.go index b8df7cc..14aabbe 100644 --- a/obfuscation.go +++ b/obfuscation.go @@ -15,17 +15,10 @@ type OnionErrorEncrypter struct { // NewOnionErrorEncrypter creates new instance of the onion encrypter backed by // the passed router, with encryption to be doing using the passed // ephemeralKey. -func NewOnionErrorEncrypter(router *Router, - ephemeralKey *btcec.PublicKey) (*OnionErrorEncrypter, error) { - - sharedSecret, err := router.generateSharedSecret(ephemeralKey) - if err != nil { - return nil, err - } - +func NewOnionErrorEncrypter(sharedSecret Hash256) *OnionErrorEncrypter { return &OnionErrorEncrypter{ sharedSecret: sharedSecret, - }, nil + } } // Encode writes the encrypter's shared secret to the provided io.Writer. diff --git a/obfuscation_test.go b/obfuscation_test.go index 665dfa7..5571956 100644 --- a/obfuscation_test.go +++ b/obfuscation_test.go @@ -34,9 +34,7 @@ func TestOnionFailure(t *testing.T) { } // Emulate creation of the obfuscator on node where error have occurred. - obfuscator := &OnionErrorEncrypter{ - sharedSecret: sharedSecrets[len(errorPath)-1], - } + obfuscator := NewOnionErrorEncrypter(sharedSecrets[len(errorPath)-1]) // Emulate the situation when last hop creates the onion failure // message and send it back. @@ -46,9 +44,7 @@ func TestOnionFailure(t *testing.T) { for i := len(errorPath) - 2; i >= 0; i-- { // Emulate creation of the obfuscator on forwarding node which // propagates the onion failure. - obfuscator = &OnionErrorEncrypter{ - sharedSecret: sharedSecrets[i], - } + obfuscator = NewOnionErrorEncrypter(sharedSecrets[i]) obfuscatedData = obfuscator.EncryptError(false, obfuscatedData) } @@ -207,16 +203,16 @@ func TestOnionFailureSpecVector(t *testing.T) { t.Fatalf("unable to decode spec shared secret: %v", err) } - obfuscator := &OnionErrorEncrypter{ - sharedSecret: sharedSecrets[len(sharedSecrets)-1-i], - } + obfuscator := NewOnionErrorEncrypter( + sharedSecrets[len(sharedSecrets)-1-i], + ) var b bytes.Buffer if err := obfuscator.Encode(&b); err != nil { t.Fatalf("unable to encode obfuscator: %v", err) } - obfuscator2 := &OnionErrorEncrypter{} + obfuscator2 := NewOnionErrorEncrypter(Hash256{}) obfuscatorReader := bytes.NewReader(b.Bytes()) if err := obfuscator2.Decode(obfuscatorReader); err != nil { t.Fatalf("unable to decode obfuscator: %v", err)