Skip to content

Commit

Permalink
noise: add documentation for the Prologue option
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 2, 2022
1 parent 04a0730 commit 40a0d93
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
27 changes: 10 additions & 17 deletions p2p/security/noise/session_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import (

type SessionOption = func(*SessionTransport) error

// Prologue sets a prologue for the Noise session.
// The handshake will only complete successfully if both parties set the same prologue.
// See https://noiseprotocol.org/noise.html#prologue for details.
func Prologue(prologue []byte) SessionOption {
return func(s *SessionTransport) error {
s.prologue = prologue
return nil
}
}

var _ sec.SecureTransport = &SessionTransport{}

// SessionTransport can be used
Expand Down Expand Up @@ -39,20 +49,3 @@ func (i *SessionTransport) SecureInbound(ctx context.Context, insecure net.Conn,
func (i *SessionTransport) SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, error) {
return newSecureSession(i.t, ctx, insecure, p, i.prologue, true)
}

func (t *Transport) WithSessionOptions(opts ...SessionOption) (sec.SecureTransport, error) {
st := &SessionTransport{t: t}
for _, opt := range opts {
if err := opt(st); err != nil {
return nil, err
}
}
return st, nil
}

func Prologue(prologue []byte) SessionOption {
return func(s *SessionTransport) error {
s.prologue = prologue
return nil
}
}
10 changes: 10 additions & 0 deletions p2p/security/noise/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ func (t *Transport) SecureInbound(ctx context.Context, insecure net.Conn, p peer
func (t *Transport) SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, error) {
return newSecureSession(t, ctx, insecure, p, nil, true)
}

func (t *Transport) WithSessionOptions(opts ...SessionOption) (sec.SecureTransport, error) {
st := &SessionTransport{t: t}
for _, opt := range opts {
if err := opt(st); err != nil {
return nil, err
}
}
return st, nil
}

0 comments on commit 40a0d93

Please sign in to comment.