Compare commits

...

5 Commits

Author SHA1 Message Date
Steven Allen
02253f5120 ci: remove gx support 2019-08-20 08:03:01 -07:00
whyrusleeping
a5acc01164 use constants and enforce max length elsewhere 2019-08-19 18:19:07 -07:00
whyrusleeping
aa319eb8b5 Add fastpath cbor marshalers 2019-08-19 17:13:12 -07:00
Steven Allen
9bb7ea6920 Merge pull request #86 from lidel/feat/libp2p-key
Add support for libp2p-key multicodec to go-cid
2019-05-27 16:46:10 -07:00
Marcin Rataj
3f1777738f Add libp2p-key multicodec
Context: https://github.com/multiformats/multicodec/issues/130

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
2019-05-28 01:40:54 +02:00
9 changed files with 180 additions and 64 deletions

View File

@@ -10,7 +10,6 @@ env:
global:
- GOTFLAGS="-race"
matrix:
- BUILD_DEPTYPE=gx
- BUILD_DEPTYPE=gomod
@@ -24,7 +23,6 @@ script:
cache:
directories:
- $GOPATH/src/gx
- $GOPATH/pkg/mod
- $HOME/.cache/go-build

View File

@@ -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

View File

@@ -35,22 +35,6 @@ Note that `go-cid` is packaged with Gx, so it is recommended to use Gx to instal
## 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
Before running tests, please run:

View File

@@ -8,6 +8,7 @@ const (
DagProtobuf = 0x70
DagCBOR = 0x71
Libp2pKey = 0x72
GitRaw = 0x78
@@ -34,6 +35,7 @@ var Codecs = map[string]uint64{
"raw": Raw,
"protobuf": DagProtobuf,
"cbor": DagCBOR,
"libp2p-key": Libp2pKey,
"git-raw": GitRaw,
"eth-block": EthBlock,
"eth-block-list": EthBlockList,
@@ -57,6 +59,7 @@ var CodecToStr = map[uint64]string{
Raw: "raw",
DagProtobuf: "protobuf",
DagCBOR: "cbor",
Libp2pKey: "libp2p-key",
GitRaw: "git-raw",
EthBlock: "eth-block",
EthBlockList: "eth-block-list",

84
cid.go
View File

@@ -26,15 +26,20 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
mbase "github.com/multiformats/go-multibase"
mh "github.com/multiformats/go-multihash"
cbg "github.com/whyrusleeping/cbor-gen"
)
// UnsupportedVersionString just holds an error message
const UnsupportedVersionString = "<unsupported cid version>"
const CidMaxLen = 256
var (
// ErrVarintBuffSmall means that a buffer passed to the cid parser was not
// long enough, or did not contain an invalid cid
@@ -49,6 +54,10 @@ var (
// enough to be a valid Cid
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
// by this Cid version
ErrInvalidEncoding = errors.New("invalid base encoding")
@@ -62,6 +71,7 @@ const (
DagProtobuf = 0x70
DagCBOR = 0x71
Libp2pKey = 0x72
GitRaw = 0x78
@@ -90,6 +100,7 @@ var Codecs = map[string]uint64{
"raw": Raw,
"protobuf": DagProtobuf,
"cbor": DagCBOR,
"libp2p-key": Libp2pKey,
"git-raw": GitRaw,
"eth-block": EthBlock,
"eth-block-list": EthBlockList,
@@ -288,6 +299,10 @@ func uvError(read int) error {
// Please use decode when parsing a regular Cid string, as Cast does not
// expect multibase-encoded data. Cast accepts the output of Cid.Bytes().
func Cast(data []byte) (Cid, error) {
if len(data) > CidMaxLen {
return Undef, ErrCidTooLong
}
if len(data) == 34 && data[0] == 18 && data[1] == 32 {
h, err := mh.Cast(data)
if err != nil {
@@ -520,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,
// that is, the Version, the Codec, the Multihash type
// and the Multihash length. It does not contains

View File

@@ -2,6 +2,7 @@ package cid
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"math/rand"
@@ -19,6 +20,7 @@ var tCodecs = map[uint64]string{
Raw: "raw",
DagProtobuf: "protobuf",
DagCBOR: "cbor",
Libp2pKey: "libp2p-key",
GitRaw: "git-raw",
EthBlock: "eth-block",
EthBlockList: "eth-block-list",
@@ -498,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) {
data := []byte("this is some test content")
hash, _ := mh.Sum(data, mh.SHA2_256, -1)

1
go.mod
View File

@@ -3,4 +3,5 @@ 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
)

19
go.sum
View File

@@ -1,7 +1,16 @@
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=
@@ -14,6 +23,16 @@ github.com/multiformats/go-multibase v0.0.1 h1:PN9/v21eLywrFWdFNsFKaU04kLJzuYzmr
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=

View File

@@ -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": "QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW",
"name": "go-multihash",
"version": "1.0.9"
},
{
"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.3"
}