Skip to content

Commit

Permalink
Merge pull request lightningnetwork#17 from cfromknecht/obfuscator-se…
Browse files Browse the repository at this point in the history
…rialization

Obfuscator Serialization
  • Loading branch information
Roasbeef authored Jan 29, 2018
2 parents 18bf661 + cd2eefa commit d8e664f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions obfuscation.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ func (o *OnionErrorEncrypter) EncryptError(initial bool, data []byte) []byte {
return onionEncrypt(o.sharedSecret, data)
}

// Encode writes the encrypter's shared secret to the provided io.Writer.
func (o *OnionErrorEncrypter) Encode(w io.Writer) error {
_, err := w.Write(o.sharedSecret[:])
return err
}

// Decode restores the encrypter's share secret from the provided io.Reader.
func (o *OnionErrorEncrypter) Decode(r io.Reader) error {
_, err := io.ReadFull(r, o.sharedSecret[:])
return err
}

// Circuit is used encapsulate the data which is needed for data deobfuscation.
type Circuit struct {
// SessionKey is the key which have been used during generation of the
Expand Down
16 changes: 16 additions & 0 deletions obfuscation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sphinx
import (
"bytes"
"encoding/hex"
"reflect"
"testing"

"github.com/roasbeef/btcd/btcec"
Expand Down Expand Up @@ -201,6 +202,21 @@ func TestOnionFailureSpecVector(t *testing.T) {
sharedSecret: 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{}
obfuscatorReader := bytes.NewReader(b.Bytes())
if err := obfuscator2.Decode(obfuscatorReader); err != nil {
t.Fatalf("unable to decode obfuscator: %v", err)
}

if !reflect.DeepEqual(obfuscator, obfuscator2) {
t.Fatalf("unable to reconstruct obfuscator: %v", err)
}

if !bytes.Equal(expectedSharedSecret, obfuscator.sharedSecret[:]) {
t.Fatalf("shared secret not match with spec: expected "+
"%x, got %x", expectedSharedSecret,
Expand Down

0 comments on commit d8e664f

Please sign in to comment.