Compare commits
22 Commits
feat/strin
...
feat/cbor-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02253f5120 | ||
|
|
a5acc01164 | ||
|
|
aa319eb8b5 | ||
|
|
9bb7ea6920 | ||
|
|
3f1777738f | ||
|
|
b1cc3e404d | ||
|
|
b16425b966 | ||
|
|
f04f9216e7 | ||
|
|
e7e67e08cf | ||
|
|
29a66d1820 | ||
|
|
08f30d213e | ||
|
|
cf3b4efcaf | ||
|
|
ca991e8eb6 | ||
|
|
8d327b2f4b | ||
|
|
14b828acf5 | ||
|
|
00439572fb | ||
|
|
37bf2f9503 | ||
|
|
e6d04f280e | ||
|
|
033594dcd6 | ||
|
|
c9e99b39db | ||
|
|
3ec3578fe9 | ||
|
|
628ab3426c |
@@ -1 +1 @@
|
|||||||
0.9.0: QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7
|
0.9.3: QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN
|
||||||
|
|||||||
34
.travis.yml
34
.travis.yml
@@ -1,24 +1,30 @@
|
|||||||
sudo: false
|
os:
|
||||||
|
- linux
|
||||||
|
|
||||||
language: go
|
language: go
|
||||||
|
|
||||||
go:
|
go:
|
||||||
- 'tip'
|
- 1.11.x
|
||||||
|
|
||||||
|
env:
|
||||||
|
global:
|
||||||
|
- GOTFLAGS="-race"
|
||||||
|
matrix:
|
||||||
|
- BUILD_DEPTYPE=gomod
|
||||||
|
|
||||||
|
|
||||||
|
# disable travis install
|
||||||
install:
|
install:
|
||||||
- go get github.com/whyrusleeping/gx
|
- true
|
||||||
- go get github.com/whyrusleeping/gx-go
|
|
||||||
- gx install --global
|
|
||||||
script:
|
script:
|
||||||
- gx test -v -race -coverprofile=coverage.txt -covermode=atomic .
|
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
|
||||||
|
|
||||||
after_success:
|
|
||||||
- bash <(curl -s https://codecov.io/bash)
|
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
directories:
|
directories:
|
||||||
- $GOPATH/src/gx
|
- $GOPATH/pkg/mod
|
||||||
|
- $HOME/.cache/go-build
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
email: false
|
email: false
|
||||||
|
|
||||||
|
|||||||
16
Makefile
16
Makefile
@@ -1,16 +0,0 @@
|
|||||||
all: deps
|
|
||||||
|
|
||||||
gx:
|
|
||||||
go get github.com/whyrusleeping/gx
|
|
||||||
go get github.com/whyrusleeping/gx-go
|
|
||||||
|
|
||||||
covertools:
|
|
||||||
go get github.com/mattn/goveralls
|
|
||||||
go get golang.org/x/tools/cmd/cover
|
|
||||||
|
|
||||||
deps: gx covertools
|
|
||||||
gx --verbose install --global
|
|
||||||
gx-go rewrite
|
|
||||||
|
|
||||||
publish:
|
|
||||||
gx-go rewrite --undo
|
|
||||||
18
README.md
18
README.md
@@ -35,22 +35,6 @@ Note that `go-cid` is packaged with Gx, so it is recommended to use Gx to instal
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Using Gx and Gx-go
|
|
||||||
|
|
||||||
This module is packaged with [Gx](https://github.com/whyrusleeping/gx). In order to use it in your own project it is recommended that you:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
go get -u github.com/whyrusleeping/gx
|
|
||||||
go get -u github.com/whyrusleeping/gx-go
|
|
||||||
cd <your-project-repository>
|
|
||||||
gx init
|
|
||||||
gx import github.com/ipfs/go-cid
|
|
||||||
gx install --global
|
|
||||||
gx-go --rewrite
|
|
||||||
```
|
|
||||||
|
|
||||||
Please check [Gx](https://github.com/whyrusleeping/gx) and [Gx-go](https://github.com/whyrusleeping/gx-go) documentation for more information.
|
|
||||||
|
|
||||||
### Running tests
|
### Running tests
|
||||||
|
|
||||||
Before running tests, please run:
|
Before running tests, please run:
|
||||||
@@ -67,7 +51,7 @@ This will make sure that dependencies are rewritten to known working versions.
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
// Create a cid from a marshaled string
|
// Create a cid from a marshaled string
|
||||||
c, err := cid.Decode("zdvgqEMYmNeH5fKciougvQcfzMcNjF3Z1tPouJ8C7pc3pe63k")
|
c, err := cid.Decode("bafzbeigai3eoy2ccc7ybwjfz5r3rdxqrinwi4rwytly24tdbh6yk7zslrm")
|
||||||
if err != nil {...}
|
if err != nil {...}
|
||||||
|
|
||||||
fmt.Println("Got CID: ", c)
|
fmt.Println("Got CID: ", c)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ const (
|
|||||||
|
|
||||||
DagProtobuf = 0x70
|
DagProtobuf = 0x70
|
||||||
DagCBOR = 0x71
|
DagCBOR = 0x71
|
||||||
|
Libp2pKey = 0x72
|
||||||
|
|
||||||
GitRaw = 0x78
|
GitRaw = 0x78
|
||||||
|
|
||||||
@@ -34,6 +35,7 @@ var Codecs = map[string]uint64{
|
|||||||
"raw": Raw,
|
"raw": Raw,
|
||||||
"protobuf": DagProtobuf,
|
"protobuf": DagProtobuf,
|
||||||
"cbor": DagCBOR,
|
"cbor": DagCBOR,
|
||||||
|
"libp2p-key": Libp2pKey,
|
||||||
"git-raw": GitRaw,
|
"git-raw": GitRaw,
|
||||||
"eth-block": EthBlock,
|
"eth-block": EthBlock,
|
||||||
"eth-block-list": EthBlockList,
|
"eth-block-list": EthBlockList,
|
||||||
@@ -57,6 +59,7 @@ var CodecToStr = map[uint64]string{
|
|||||||
Raw: "raw",
|
Raw: "raw",
|
||||||
DagProtobuf: "protobuf",
|
DagProtobuf: "protobuf",
|
||||||
DagCBOR: "cbor",
|
DagCBOR: "cbor",
|
||||||
|
Libp2pKey: "libp2p-key",
|
||||||
GitRaw: "git-raw",
|
GitRaw: "git-raw",
|
||||||
EthBlock: "eth-block",
|
EthBlock: "eth-block",
|
||||||
EthBlockList: "eth-block-list",
|
EthBlockList: "eth-block-list",
|
||||||
|
|||||||
141
cid.go
141
cid.go
@@ -21,19 +21,25 @@ package cid
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
mbase "github.com/multiformats/go-multibase"
|
mbase "github.com/multiformats/go-multibase"
|
||||||
mh "github.com/multiformats/go-multihash"
|
mh "github.com/multiformats/go-multihash"
|
||||||
|
|
||||||
|
cbg "github.com/whyrusleeping/cbor-gen"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnsupportedVersionString just holds an error message
|
// UnsupportedVersionString just holds an error message
|
||||||
const UnsupportedVersionString = "<unsupported cid version>"
|
const UnsupportedVersionString = "<unsupported cid version>"
|
||||||
|
|
||||||
|
const CidMaxLen = 256
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrVarintBuffSmall means that a buffer passed to the cid parser was not
|
// ErrVarintBuffSmall means that a buffer passed to the cid parser was not
|
||||||
// long enough, or did not contain an invalid cid
|
// long enough, or did not contain an invalid cid
|
||||||
@@ -48,6 +54,10 @@ var (
|
|||||||
// enough to be a valid Cid
|
// enough to be a valid Cid
|
||||||
ErrCidTooShort = errors.New("cid too short")
|
ErrCidTooShort = errors.New("cid too short")
|
||||||
|
|
||||||
|
// ErrCidTooLong means that the CIDs payload was greater than CidMaxLen
|
||||||
|
// bytes
|
||||||
|
ErrCidTooLong = errors.New("cid too long")
|
||||||
|
|
||||||
// ErrInvalidEncoding means that selected encoding is not supported
|
// ErrInvalidEncoding means that selected encoding is not supported
|
||||||
// by this Cid version
|
// by this Cid version
|
||||||
ErrInvalidEncoding = errors.New("invalid base encoding")
|
ErrInvalidEncoding = errors.New("invalid base encoding")
|
||||||
@@ -61,6 +71,7 @@ const (
|
|||||||
|
|
||||||
DagProtobuf = 0x70
|
DagProtobuf = 0x70
|
||||||
DagCBOR = 0x71
|
DagCBOR = 0x71
|
||||||
|
Libp2pKey = 0x72
|
||||||
|
|
||||||
GitRaw = 0x78
|
GitRaw = 0x78
|
||||||
|
|
||||||
@@ -79,6 +90,8 @@ const (
|
|||||||
ZcashTx = 0xc1
|
ZcashTx = 0xc1
|
||||||
DecredBlock = 0xe0
|
DecredBlock = 0xe0
|
||||||
DecredTx = 0xe1
|
DecredTx = 0xe1
|
||||||
|
DashBlock = 0xf0
|
||||||
|
DashTx = 0xf1
|
||||||
)
|
)
|
||||||
|
|
||||||
// Codecs maps the name of a codec to its type
|
// Codecs maps the name of a codec to its type
|
||||||
@@ -87,6 +100,7 @@ var Codecs = map[string]uint64{
|
|||||||
"raw": Raw,
|
"raw": Raw,
|
||||||
"protobuf": DagProtobuf,
|
"protobuf": DagProtobuf,
|
||||||
"cbor": DagCBOR,
|
"cbor": DagCBOR,
|
||||||
|
"libp2p-key": Libp2pKey,
|
||||||
"git-raw": GitRaw,
|
"git-raw": GitRaw,
|
||||||
"eth-block": EthBlock,
|
"eth-block": EthBlock,
|
||||||
"eth-block-list": EthBlockList,
|
"eth-block-list": EthBlockList,
|
||||||
@@ -103,6 +117,8 @@ var Codecs = map[string]uint64{
|
|||||||
"zcash-tx": ZcashTx,
|
"zcash-tx": ZcashTx,
|
||||||
"decred-block": DecredBlock,
|
"decred-block": DecredBlock,
|
||||||
"decred-tx": DecredTx,
|
"decred-tx": DecredTx,
|
||||||
|
"dash-block": DashBlock,
|
||||||
|
"dash-tx": DashTx,
|
||||||
}
|
}
|
||||||
|
|
||||||
// CodecToStr maps the numeric codec to its name
|
// CodecToStr maps the numeric codec to its name
|
||||||
@@ -126,6 +142,8 @@ var CodecToStr = map[uint64]string{
|
|||||||
ZcashTx: "zcash-tx",
|
ZcashTx: "zcash-tx",
|
||||||
DecredBlock: "decred-block",
|
DecredBlock: "decred-block",
|
||||||
DecredTx: "decred-tx",
|
DecredTx: "decred-tx",
|
||||||
|
DashBlock: "dash-block",
|
||||||
|
DashTx: "dash-tx",
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCidV0 returns a Cid-wrapped multihash.
|
// NewCidV0 returns a Cid-wrapped multihash.
|
||||||
@@ -161,7 +179,12 @@ func NewCidV1(codecType uint64, mhash mh.Multihash) Cid {
|
|||||||
return Cid{string(buf[:n+hashlen])}
|
return Cid{string(buf[:n+hashlen])}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cid represents a self-describing content adressed
|
var _ encoding.BinaryMarshaler = Cid{}
|
||||||
|
var _ encoding.BinaryUnmarshaler = (*Cid)(nil)
|
||||||
|
var _ encoding.TextMarshaler = Cid{}
|
||||||
|
var _ encoding.TextUnmarshaler = (*Cid)(nil)
|
||||||
|
|
||||||
|
// Cid represents a self-describing content addressed
|
||||||
// identifier. It is formed by a Version, a Codec (which indicates
|
// identifier. It is formed by a Version, a Codec (which indicates
|
||||||
// a multicodec-packed content type) and a Multihash.
|
// a multicodec-packed content type) and a Multihash.
|
||||||
type Cid struct{ str string }
|
type Cid struct{ str string }
|
||||||
@@ -276,6 +299,10 @@ func uvError(read int) error {
|
|||||||
// Please use decode when parsing a regular Cid string, as Cast does not
|
// Please use decode when parsing a regular Cid string, as Cast does not
|
||||||
// expect multibase-encoded data. Cast accepts the output of Cid.Bytes().
|
// expect multibase-encoded data. Cast accepts the output of Cid.Bytes().
|
||||||
func Cast(data []byte) (Cid, error) {
|
func Cast(data []byte) (Cid, error) {
|
||||||
|
if len(data) > CidMaxLen {
|
||||||
|
return Undef, ErrCidTooLong
|
||||||
|
}
|
||||||
|
|
||||||
if len(data) == 34 && data[0] == 18 && data[1] == 32 {
|
if len(data) == 34 && data[0] == 18 && data[1] == 32 {
|
||||||
h, err := mh.Cast(data)
|
h, err := mh.Cast(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -308,6 +335,28 @@ func Cast(data []byte) (Cid, error) {
|
|||||||
return Cid{string(data[0 : n+cn+len(h)])}, nil
|
return Cid{string(data[0 : n+cn+len(h)])}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalBinary is equivalent to Cast(). It implements the
|
||||||
|
// encoding.BinaryUnmarshaler interface.
|
||||||
|
func (c *Cid) UnmarshalBinary(data []byte) error {
|
||||||
|
casted, err := Cast(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.str = casted.str
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText is equivalent to Decode(). It implements the
|
||||||
|
// encoding.TextUnmarshaler interface.
|
||||||
|
func (c *Cid) UnmarshalText(text []byte) error {
|
||||||
|
decodedCid, err := Decode(string(text))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.str = decodedCid.str
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Version returns the Cid version.
|
// Version returns the Cid version.
|
||||||
func (c Cid) Version() uint64 {
|
func (c Cid) Version() uint64 {
|
||||||
if len(c.str) == 34 && c.str[0] == 18 && c.str[1] == 32 {
|
if len(c.str) == 34 && c.str[0] == 18 && c.str[1] == 32 {
|
||||||
@@ -334,7 +383,7 @@ func (c Cid) String() string {
|
|||||||
case 0:
|
case 0:
|
||||||
return c.Hash().B58String()
|
return c.Hash().B58String()
|
||||||
case 1:
|
case 1:
|
||||||
mbstr, err := mbase.Encode(mbase.Base58BTC, c.Bytes())
|
mbstr, err := mbase.Encode(mbase.Base32, c.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("should not error with hardcoded mbase: " + err.Error())
|
panic("should not error with hardcoded mbase: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -398,6 +447,18 @@ func (c Cid) Bytes() []byte {
|
|||||||
return []byte(c.str)
|
return []byte(c.str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalBinary is equivalent to Bytes(). It implements the
|
||||||
|
// encoding.BinaryMarshaler interface.
|
||||||
|
func (c Cid) MarshalBinary() ([]byte, error) {
|
||||||
|
return c.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalText is equivalent to String(). It implements the
|
||||||
|
// encoding.TextMarshaler interface.
|
||||||
|
func (c Cid) MarshalText() ([]byte, error) {
|
||||||
|
return []byte(c.String()), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Equals checks that two Cids are the same.
|
// Equals checks that two Cids are the same.
|
||||||
// In order for two Cids to be considered equal, the
|
// In order for two Cids to be considered equal, the
|
||||||
// Version, the Codec and the Multihash must match.
|
// Version, the Codec and the Multihash must match.
|
||||||
@@ -474,6 +535,75 @@ func (c Cid) Prefix() Prefix {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Cid) MarshalCBOR(w io.Writer) error {
|
||||||
|
tag := cbg.CborEncodeMajorType(cbg.MajTag, 42)
|
||||||
|
if _, err := w.Write(tag); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
typ := cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(c.Bytes())+1))
|
||||||
|
if _, err := w.Write(typ); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// that binary multibase prefix...
|
||||||
|
if _, err := w.Write([]byte{0}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := w.Write(c.Bytes()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cid) UnmarshalCBOR(br cbg.ByteReader) error {
|
||||||
|
maj, extra, err := cbg.CborReadHeader(br)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if maj != cbg.MajTag || extra != 42 {
|
||||||
|
return fmt.Errorf("CBOR serialized CIDs must have the tag 42")
|
||||||
|
}
|
||||||
|
|
||||||
|
maj, extra, err = cbg.CborReadHeader(br)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if maj != 2 {
|
||||||
|
return fmt.Errorf("CBOR serialized CIDs must be tagged byte arrays")
|
||||||
|
}
|
||||||
|
|
||||||
|
if extra < 2 {
|
||||||
|
return fmt.Errorf("encoded CID body must be at least two bytes long")
|
||||||
|
}
|
||||||
|
|
||||||
|
if extra > CidMaxLen+1 {
|
||||||
|
return fmt.Errorf("CIDs cannot be longer than 256 bytes")
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, extra)
|
||||||
|
if _, err := io.ReadFull(br, buf); err != nil {
|
||||||
|
return fmt.Errorf("failed to read CID body: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if buf[0] != 0 {
|
||||||
|
return fmt.Errorf("encoded CID did not have binary multibase")
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := Cast(buf[1:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*c = out
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Prefix represents all the metadata of a Cid,
|
// Prefix represents all the metadata of a Cid,
|
||||||
// that is, the Version, the Codec, the Multihash type
|
// that is, the Version, the Codec, the Multihash type
|
||||||
// and the Multihash length. It does not contains
|
// and the Multihash length. It does not contains
|
||||||
@@ -490,7 +620,12 @@ type Prefix struct {
|
|||||||
// Sum uses the information in a prefix to perform a multihash.Sum()
|
// Sum uses the information in a prefix to perform a multihash.Sum()
|
||||||
// and return a newly constructed Cid with the resulting multihash.
|
// and return a newly constructed Cid with the resulting multihash.
|
||||||
func (p Prefix) Sum(data []byte) (Cid, error) {
|
func (p Prefix) Sum(data []byte) (Cid, error) {
|
||||||
hash, err := mh.Sum(data, p.MhType, p.MhLength)
|
length := p.MhLength
|
||||||
|
if p.MhType == mh.ID {
|
||||||
|
length = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
hash, err := mh.Sum(data, p.MhType, length)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Undef, err
|
return Undef, err
|
||||||
}
|
}
|
||||||
|
|||||||
151
cid_test.go
151
cid_test.go
@@ -2,6 +2,7 @@ package cid
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@@ -19,6 +20,7 @@ var tCodecs = map[uint64]string{
|
|||||||
Raw: "raw",
|
Raw: "raw",
|
||||||
DagProtobuf: "protobuf",
|
DagProtobuf: "protobuf",
|
||||||
DagCBOR: "cbor",
|
DagCBOR: "cbor",
|
||||||
|
Libp2pKey: "libp2p-key",
|
||||||
GitRaw: "git-raw",
|
GitRaw: "git-raw",
|
||||||
EthBlock: "eth-block",
|
EthBlock: "eth-block",
|
||||||
EthBlockList: "eth-block-list",
|
EthBlockList: "eth-block-list",
|
||||||
@@ -35,6 +37,8 @@ var tCodecs = map[uint64]string{
|
|||||||
ZcashTx: "zcash-tx",
|
ZcashTx: "zcash-tx",
|
||||||
DecredBlock: "decred-block",
|
DecredBlock: "decred-block",
|
||||||
DecredTx: "decred-tx",
|
DecredTx: "decred-tx",
|
||||||
|
DashBlock: "dash-block",
|
||||||
|
DashTx: "dash-tx",
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertEqual(t *testing.T, a, b Cid) {
|
func assertEqual(t *testing.T, a, b Cid) {
|
||||||
@@ -71,6 +75,34 @@ func TestTableForV0(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrefixSum(t *testing.T) {
|
||||||
|
// Test creating CIDs both manually and with Prefix.
|
||||||
|
// Tests: https://github.com/ipfs/go-cid/issues/83
|
||||||
|
for _, hashfun := range []uint64{
|
||||||
|
mh.ID, mh.SHA3, mh.SHA2_256,
|
||||||
|
} {
|
||||||
|
h1, err := mh.Sum([]byte("TEST"), hashfun, -1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
c1 := NewCidV1(Raw, h1)
|
||||||
|
|
||||||
|
h2, err := mh.Sum([]byte("foobar"), hashfun, -1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
c2 := NewCidV1(Raw, h2)
|
||||||
|
|
||||||
|
c3, err := c1.Prefix().Sum([]byte("foobar"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !c2.Equals(c3) {
|
||||||
|
t.Fatal("expected CIDs to be equal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBasicMarshaling(t *testing.T) {
|
func TestBasicMarshaling(t *testing.T) {
|
||||||
h, err := mh.Sum([]byte("TEST"), mh.SHA3, 4)
|
h, err := mh.Sum([]byte("TEST"), mh.SHA3, 4)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -156,6 +188,44 @@ func TestBasesMarshaling(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBinaryMarshaling(t *testing.T) {
|
||||||
|
data := []byte("this is some test content")
|
||||||
|
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
||||||
|
c := NewCidV1(DagCBOR, hash)
|
||||||
|
var c2 Cid
|
||||||
|
|
||||||
|
data, err := c.MarshalBinary()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = c2.UnmarshalBinary(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !c.Equals(c2) {
|
||||||
|
t.Errorf("cids should be the same: %s %s", c, c2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTextMarshaling(t *testing.T) {
|
||||||
|
data := []byte("this is some test content")
|
||||||
|
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
||||||
|
c := NewCidV1(DagCBOR, hash)
|
||||||
|
var c2 Cid
|
||||||
|
|
||||||
|
data, err := c.MarshalText()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = c2.UnmarshalText(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !c.Equals(c2) {
|
||||||
|
t.Errorf("cids should be the same: %s %s", c, c2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestEmptyString(t *testing.T) {
|
func TestEmptyString(t *testing.T) {
|
||||||
_, err := Decode("")
|
_, err := Decode("")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -370,13 +440,13 @@ func TestHexDecode(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.String() != "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG" {
|
if c.String() != "bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm" {
|
||||||
t.Fatal("hash value failed to round trip decoding from hex")
|
t.Fatal("hash value failed to round trip decoding from hex")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleDecode() {
|
func ExampleDecode() {
|
||||||
encoded := "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG"
|
encoded := "bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm"
|
||||||
c, err := Decode(encoded)
|
c, err := Decode(encoded)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: %s", err)
|
fmt.Printf("Error: %s", err)
|
||||||
@@ -384,11 +454,11 @@ func ExampleDecode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(c)
|
fmt.Println(c)
|
||||||
// Output: zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG
|
// Output: bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFromJson(t *testing.T) {
|
func TestFromJson(t *testing.T) {
|
||||||
cval := "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG"
|
cval := "bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm"
|
||||||
jsoncid := []byte(`{"/":"` + cval + `"}`)
|
jsoncid := []byte(`{"/":"` + cval + `"}`)
|
||||||
var c Cid
|
var c Cid
|
||||||
err := json.Unmarshal(jsoncid, &c)
|
err := json.Unmarshal(jsoncid, &c)
|
||||||
@@ -402,7 +472,7 @@ func TestFromJson(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestJsonRoundTrip(t *testing.T) {
|
func TestJsonRoundTrip(t *testing.T) {
|
||||||
exp, err := Decode("zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG")
|
exp, err := Decode("bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -430,6 +500,77 @@ func TestJsonRoundTrip(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCBORSerialization(t *testing.T) {
|
||||||
|
cases := map[string]string{
|
||||||
|
"bafybeibpzhw63c3vh7vushzvfzvkc2nbhr6nm3ui2o7kj33cx3xoovnzp4": "d82a582500017012202fc9eded8b753feb491f352e6aa169a13c7cd66e88d3bea4ef62beeee755b97f",
|
||||||
|
"QmRZCTZPygAnfagZKzjy48b4LMmqPNpJnNBEg5LtCCzHCA": "d82a58230012202fc9eded8b753feb491f352e6aa169a13c7cd66e88d3bea4ef62beeee755b97f",
|
||||||
|
}
|
||||||
|
|
||||||
|
for cs, encs := range cases {
|
||||||
|
c, err := Decode(cs)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
if err := c.MarshalCBOR(buf); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
exp, err := hex.DecodeString(encs)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(exp, buf.Bytes()) {
|
||||||
|
t.Fatalf("serialization incorrect: %x != %x", buf.Bytes(), exp)
|
||||||
|
}
|
||||||
|
|
||||||
|
var out Cid
|
||||||
|
if err := out.UnmarshalCBOR(bytes.NewReader(exp)); err != nil {
|
||||||
|
t.Fatalf("unmarshal case %s failed: %s", cs, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if out != c {
|
||||||
|
t.Fatal("unmarshal CBOR failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCBORMarshal(b *testing.B) {
|
||||||
|
c, err := Decode("bafybeibpzhw63c3vh7vushzvfzvkc2nbhr6nm3ui2o7kj33cx3xoovnzp4")
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
buf.Reset()
|
||||||
|
if err := c.MarshalCBOR(buf); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCBORUnmarshal(b *testing.B) {
|
||||||
|
enc, err := hex.DecodeString("d82a582500017012202fc9eded8b753feb491f352e6aa169a13c7cd66e88d3bea4ef62beeee755b97f")
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
var c Cid
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
br := bytes.NewReader(enc)
|
||||||
|
if err := c.UnmarshalCBOR(br); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkStringV1(b *testing.B) {
|
func BenchmarkStringV1(b *testing.B) {
|
||||||
data := []byte("this is some test content")
|
data := []byte("this is some test content")
|
||||||
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
||||||
|
|||||||
7
go.mod
Normal file
7
go.mod
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module github.com/ipfs/go-cid
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/multiformats/go-multibase v0.0.1
|
||||||
|
github.com/multiformats/go-multihash v0.0.1
|
||||||
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190820011425-d5123893e85a
|
||||||
|
)
|
||||||
39
go.sum
Normal file
39
go.sum
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||||
|
github.com/gxed/hashland/keccakpg v0.0.1 h1:wrk3uMNaMxbXiHibbPO4S0ymqJMm41WiudyFSs7UnsU=
|
||||||
|
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
|
||||||
|
github.com/gxed/hashland/murmur3 v0.0.1 h1:SheiaIt0sda5K+8FLz952/1iWS9zrnKsEJaOJu4ZbSc=
|
||||||
|
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
|
||||||
|
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
|
||||||
|
github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
|
||||||
|
github.com/ipfs/go-cid v0.0.2/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
|
||||||
|
github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
|
||||||
|
github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc=
|
||||||
|
github.com/ipfs/go-ipld-cbor v0.0.3/go.mod h1:wTBtrQZA3SoFKMVkp6cn6HMRteIB1VsmHA0AQFOn7Nc=
|
||||||
|
github.com/ipfs/go-ipld-format v0.0.1/go.mod h1:kyJtbkDALmFHv3QR6et67i35QzO3S0dCDnkOJhcZkms=
|
||||||
|
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||||
|
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
|
||||||
|
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
|
||||||
|
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16 h1:5W7KhL8HVF3XCFOweFD3BNESdnO8ewyYTFT2R+/b8FQ=
|
||||||
|
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
|
||||||
|
github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ=
|
||||||
|
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
||||||
|
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
|
||||||
|
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
|
||||||
|
github.com/multiformats/go-multibase v0.0.1 h1:PN9/v21eLywrFWdFNsFKaU04kLJzuYzmrJR+ubhT9qA=
|
||||||
|
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs=
|
||||||
|
github.com/multiformats/go-multihash v0.0.1 h1:HHwN1K12I+XllBCrqKnhX949Orn4oawPkegHMu2vDqQ=
|
||||||
|
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
|
||||||
|
github.com/polydawn/refmt v0.0.0-20190221155625-df39d6c2d992/go.mod h1:uIp+gprXxxrWSjjklXD+mN4wed/tMfjMMmN/9+JsA9o=
|
||||||
|
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||||
|
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa/go.mod h1:2RVY1rIf+2J2o/IM9+vPq9RzmHDSseB7FoXiSNIUsoU=
|
||||||
|
github.com/warpfork/go-wish v0.0.0-20180510122957-5ad1f5abf436/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw=
|
||||||
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190818214716-81392149d1dd h1:Nj8P2Fg3mRVnBfkNnxZK+QhjwCxI3kLng2nfTTLIudk=
|
||||||
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190818214716-81392149d1dd/go.mod h1:ga0xb76iSIPvNNiuR+g3+c3Rnwy0oVuBGvqDwsEV6x4=
|
||||||
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190819235733-17c06ddc16cb h1:49o2mhJ/B2nsmSjV4fUsrFEoKGM9NLWQSnk+YrAKFLg=
|
||||||
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190819235733-17c06ddc16cb/go.mod h1:ga0xb76iSIPvNNiuR+g3+c3Rnwy0oVuBGvqDwsEV6x4=
|
||||||
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190820011425-d5123893e85a h1:MYglUpczqDD6LXsnSXk6NWzwf64fheAuA3eBjyYGD/8=
|
||||||
|
github.com/whyrusleeping/cbor-gen v0.0.0-20190820011425-d5123893e85a/go.mod h1:38LwjsrZy8ga8AbyOueCsLUAtdc1BDj/I929R+LnJfI=
|
||||||
|
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE=
|
||||||
|
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||||
|
golang.org/x/sys v0.0.0-20190219092855-153ac476189d h1:Z0Ahzd7HltpJtjAHHxX8QFP3j1yYgiuvjbjRzDj/KH0=
|
||||||
|
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
30
package.json
30
package.json
@@ -1,30 +0,0 @@
|
|||||||
{
|
|
||||||
"author": "whyrusleeping",
|
|
||||||
"bugs": {
|
|
||||||
"url": "https://github.com/ipfs/go-cid"
|
|
||||||
},
|
|
||||||
"gx": {
|
|
||||||
"dvcsimport": "github.com/ipfs/go-cid"
|
|
||||||
},
|
|
||||||
"gxDependencies": [
|
|
||||||
{
|
|
||||||
"author": "whyrusleeping",
|
|
||||||
"hash": "QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8",
|
|
||||||
"name": "go-multihash",
|
|
||||||
"version": "1.0.8"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"author": "whyrusleeping",
|
|
||||||
"hash": "QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd",
|
|
||||||
"name": "go-multibase",
|
|
||||||
"version": "0.3.0"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"gxVersion": "0.8.0",
|
|
||||||
"language": "go",
|
|
||||||
"license": "MIT",
|
|
||||||
"name": "go-cid",
|
|
||||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
|
||||||
"version": "0.9.0"
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user