Skip to content

Commit

Permalink
initialize NewOnionErrorEncrypter with shared secret directly
Browse files Browse the repository at this point in the history
Allow for more flexible usage of the error encrypter. This is useful when upgrading an
existing legacy error encrypter to fat errors in lnd.
  • Loading branch information
joostjager committed Jan 17, 2023
1 parent 371dfed commit 9260f57
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
11 changes: 2 additions & 9 deletions obfuscation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 6 additions & 10 deletions obfuscation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9260f57

Please sign in to comment.