ipld-eth-server/vendor/github.com/libp2p/go-libp2p-pnet/protector.go
Elizabeth Engelman 36533f7c3f Update vendor directory and make necessary code changes
Fixes for new geth version
2019-09-25 16:32:27 -05:00

42 lines
934 B
Go

package pnet
import (
"fmt"
"io"
"net"
ipnet "github.com/libp2p/go-libp2p-core/pnet"
)
var _ ipnet.Protector = (*protector)(nil)
// NewProtector creates ipnet.Protector instance from a io.Reader stream
// that should include Multicodec encoded V1 PSK.
func NewProtector(input io.Reader) (ipnet.Protector, error) {
psk, err := decodeV1PSK(input)
if err != nil {
return nil, fmt.Errorf("malformed private network key: %s", err)
}
return NewV1ProtectorFromBytes(psk)
}
// NewV1ProtectorFromBytes creates ipnet.Protector of the V1 version.
func NewV1ProtectorFromBytes(psk *[32]byte) (ipnet.Protector, error) {
return &protector{
psk: psk,
fingerprint: fingerprint(psk),
}, nil
}
type protector struct {
psk *[32]byte
fingerprint []byte
}
func (p protector) Protect(in net.Conn) (net.Conn, error) {
return newPSKConn(p.psk, in)
}
func (p protector) Fingerprint() []byte {
return p.fingerprint
}