@@ -27,9 +27,9 @@ const (
2727 EncrypterTypeMock = 2
2828)
2929
30- // ErrorEncrypterExtracter defines a function signature that extracts an
31- // ErrorEncrypter from an sphinx OnionPacket.
32- type ErrorEncrypterExtracter func (* btcec.PublicKey ) (ErrorEncrypter ,
30+ // SharedSecretGenerator defines a function signature that extracts a shared
31+ // secret from an sphinx OnionPacket.
32+ type SharedSecretGenerator func (* btcec.PublicKey ) (sphinx. Hash256 ,
3333 lnwire.FailCode )
3434
3535// ErrorEncrypter is an interface that is used to encrypt HTLC related errors
@@ -66,12 +66,13 @@ type ErrorEncrypter interface {
6666 // given io.Reader.
6767 Decode (io.Reader ) error
6868
69- // Reextract rederives the encrypter using the extracter, performing an
70- // ECDH with the sphinx router's key and the ephemeral public key.
69+ // Reextract rederives the encrypter using the shared secret generator,
70+ // performing an ECDH with the sphinx router's key and the ephemeral
71+ // public key.
7172 //
7273 // NOTE: This should be called shortly after Decode to properly
7374 // reinitialize the error encrypter.
74- Reextract (ErrorEncrypterExtracter ) error
75+ Reextract (SharedSecretGenerator ) error
7576}
7677
7778// SphinxErrorEncrypter is a concrete implementation of both the ErrorEncrypter
@@ -84,20 +85,42 @@ type SphinxErrorEncrypter struct {
8485 EphemeralKey * btcec.PublicKey
8586}
8687
87- // NewSphinxErrorEncrypter initializes a blank sphinx error encrypter, that
88- // should be used to deserialize an encoded SphinxErrorEncrypter. Since the
89- // actual encrypter is not stored in plaintext while at rest, reconstructing the
90- // error encrypter requires:
88+ // NewSphinxErrorEncrypterUninitialized initializes a blank sphinx error
89+ // encrypter, that should be used to deserialize an encoded
90+ // SphinxErrorEncrypter. Since the actual encrypter is not stored in plaintext
91+ // while at rest, reconstructing the error encrypter requires:
9192// 1. Decode: to deserialize the ephemeral public key.
9293// 2. Reextract: to "unlock" the actual error encrypter using an active
9394// OnionProcessor.
94- func NewSphinxErrorEncrypter () * SphinxErrorEncrypter {
95+ func NewSphinxErrorEncrypterUninitialized () * SphinxErrorEncrypter {
9596 return & SphinxErrorEncrypter {
9697 OnionErrorEncrypter : nil ,
9798 EphemeralKey : & btcec.PublicKey {},
9899 }
99100}
100101
102+ // NewSphinxErrorEncrypter creates a new instance of a SphinxErrorEncrypter,
103+ // initialized with the provided shared secret. To deserialize an encoded
104+ // SphinxErrorEncrypter, use the NewSphinxErrorEncrypterUninitialized
105+ // constructor.
106+ func NewSphinxErrorEncrypter (ephemeralKey * btcec.PublicKey ,
107+ sharedSecret sphinx.Hash256 ) * SphinxErrorEncrypter {
108+
109+ encrypter := & SphinxErrorEncrypter {
110+ EphemeralKey : ephemeralKey ,
111+ }
112+
113+ encrypter .initialize (sharedSecret )
114+
115+ return encrypter
116+ }
117+
118+ func (s * SphinxErrorEncrypter ) initialize (sharedSecret sphinx.Hash256 ) {
119+ s .OnionErrorEncrypter = sphinx .NewOnionErrorEncrypter (
120+ sharedSecret ,
121+ )
122+ }
123+
101124// EncryptFirstHop transforms a concrete failure message into an encrypted
102125// opaque failure reason. This method will be used at the source that the error
103126// occurs. It differs from BackwardObfuscate slightly, in that it computes a
@@ -177,9 +200,9 @@ func (s *SphinxErrorEncrypter) Decode(r io.Reader) error {
177200// This intended to be used shortly after Decode, to fully initialize a
178201// SphinxErrorEncrypter.
179202func (s * SphinxErrorEncrypter ) Reextract (
180- extract ErrorEncrypterExtracter ) error {
203+ extract SharedSecretGenerator ) error {
181204
182- obfuscator , failcode := extract (s .EphemeralKey )
205+ sharedSecret , failcode := extract (s .EphemeralKey )
183206 if failcode != lnwire .CodeNone {
184207 // This should never happen, since we already validated that
185208 // this obfuscator can be extracted when it was received in the
@@ -188,16 +211,9 @@ func (s *SphinxErrorEncrypter) Reextract(
188211 "obfuscator, got failcode: %d" , failcode )
189212 }
190213
191- sphinxEncrypter , ok := obfuscator .(* SphinxErrorEncrypter )
192- if ! ok {
193- return fmt .Errorf ("incorrect onion error extracter" )
194- }
195-
196- // Copy the freshly extracted encrypter.
197- s .OnionErrorEncrypter = sphinxEncrypter .OnionErrorEncrypter
214+ s .initialize (sharedSecret )
198215
199216 return nil
200-
201217}
202218
203219// A compile time check to ensure SphinxErrorEncrypter implements the
0 commit comments