-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from libp2p/feat/no-multicodec
remove dependency on go-multicodec
- Loading branch information
Showing
2 changed files
with
111 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,66 @@ | ||
package pnet | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
|
||
mc "github.com/multiformats/go-multicodec" | ||
bmux "github.com/multiformats/go-multicodec/base/mux" | ||
) | ||
|
||
var ( | ||
pathPSKv1 = []byte("/key/swarm/psk/1.0.0/") | ||
headerPSKv1 = mc.Header(pathPSKv1) | ||
pathPSKv1 = []byte("/key/swarm/psk/1.0.0/") | ||
pathBin = "/bin/" | ||
pathBase16 = "/base16/" | ||
pathBase64 = "/base64/" | ||
) | ||
|
||
func decodeV1PSK(in io.Reader) (*[32]byte, error) { | ||
var err error | ||
in, err = mc.WrapTransformPathToHeader(in) | ||
func readHeader(r *bufio.Reader) ([]byte, error) { | ||
header, err := r.ReadBytes('\n') | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = mc.ConsumeHeader(in, headerPSKv1) | ||
|
||
return bytes.TrimRight(header, "\r\n"), nil | ||
} | ||
|
||
func expectHeader(r *bufio.Reader, expected []byte) error { | ||
header, err := readHeader(r) | ||
if err != nil { | ||
return nil, fmt.Errorf("psk header error: %s", err.Error()) | ||
return err | ||
} | ||
if !bytes.Equal(header, expected) { | ||
return fmt.Errorf("expected file header %s, got: %s", pathPSKv1, header) | ||
} | ||
return nil | ||
} | ||
|
||
in, err = mc.WrapTransformPathToHeader(in) | ||
func decodeV1PSK(in io.Reader) (*[32]byte, error) { | ||
reader := bufio.NewReader(in) | ||
if err := expectHeader(reader, pathPSKv1); err != nil { | ||
return nil, err | ||
} | ||
header, err := readHeader(reader) | ||
if err != nil { | ||
return nil, fmt.Errorf("wrapping error: %s", err.Error()) | ||
return nil, err | ||
} | ||
out := [32]byte{} | ||
|
||
err = bmux.AllBasesMux().Decoder(in).Decode(out[:]) | ||
return &out, err | ||
var decoder io.Reader | ||
switch string(header) { | ||
case pathBase16: | ||
decoder = hex.NewDecoder(reader) | ||
case pathBase64: | ||
decoder = base64.NewDecoder(base64.StdEncoding, reader) | ||
case pathBin: | ||
decoder = reader | ||
default: | ||
return nil, fmt.Errorf("unknown encoding: %s", header) | ||
} | ||
out := new([32]byte) | ||
_, err = io.ReadFull(decoder, out[:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters