Compare commits
32 Commits
gx/v0.7.2
...
gx/v0.7.10
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c1db98f9ae | ||
|
|
40c5ff1bfe | ||
|
|
ebda3c73ae | ||
|
|
03f8d08574 | ||
|
|
4ff5ee2b99 | ||
|
|
2b1b4a8339 | ||
|
|
ca0a568b21 | ||
|
|
896454b8a7 | ||
|
|
f623f824db | ||
|
|
15876e12dc | ||
|
|
18f61fe7b9 | ||
|
|
f68586ac44 | ||
|
|
1260e85a2d | ||
|
|
a0eff8c9d1 | ||
|
|
9c8abab049 | ||
|
|
ab63d19c4e | ||
|
|
5ccc98a754 | ||
|
|
ba97b640bd | ||
|
|
9116bf8025 | ||
|
|
c67fe910f2 | ||
|
|
9c3e314588 | ||
|
|
5da6d87c58 | ||
|
|
6ce8a80816 | ||
|
|
5ec5bbcb48 | ||
|
|
8aeb1a44a8 | ||
|
|
832b6a0170 | ||
|
|
eae3431cc9 | ||
|
|
d0e0822854 | ||
|
|
bffa0300df | ||
|
|
691f8625be | ||
|
|
177ab398cc | ||
|
|
02ce4e9b23 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
cid-fuzz.zip
|
||||
@@ -1 +1 @@
|
||||
0.7.2: QmPBa9N9MuZ16fPuKMv9KCq1aAwhkTY4G6LXQYQmWJiCrj
|
||||
0.7.10: QmTau856czj6wc5UyKQX2MfBQZ9iCZPsuUsVW2b2pRtLVp
|
||||
|
||||
24
.travis.yml
Normal file
24
.travis.yml
Normal file
@@ -0,0 +1,24 @@
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.7
|
||||
|
||||
install: true
|
||||
|
||||
before_install:
|
||||
- make deps
|
||||
|
||||
script:
|
||||
- go vet
|
||||
- $GOPATH/bin/goveralls -service="travis-ci"
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $GOPATH/src/gx
|
||||
|
||||
notifications:
|
||||
email: false
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Protocol Labs, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
16
Makefile
Normal file
16
Makefile
Normal file
@@ -0,0 +1,16 @@
|
||||
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
|
||||
94
README.md
Normal file
94
README.md
Normal file
@@ -0,0 +1,94 @@
|
||||
go-cid
|
||||
==================
|
||||
|
||||
[](http://ipn.io)
|
||||
[](http://ipfs.io/)
|
||||
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||
[](https://coveralls.io/github/ipfs/go-cid?branch=master)
|
||||
[](https://travis-ci.org/ipfs/go-cid)
|
||||
|
||||
> A package to handle content IDs in go.
|
||||
|
||||
This is an implementation in go of the [CID spec](https://github.com/ipld/cid).
|
||||
It is used in go-ipfs and related packages to refer to a typed hunk of data.
|
||||
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Install](#install)
|
||||
- [Usage](#usage)
|
||||
- [API](#api)
|
||||
- [Contribute](#contribute)
|
||||
- [License](#license)
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
make deps
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
To use CIDs, import it in your code like:
|
||||
```go
|
||||
import "github.com/ipfs/go-cid"
|
||||
```
|
||||
|
||||
Then, depending on how you want to use it, something like one of the following examples should work:
|
||||
|
||||
### Parsing string input from users
|
||||
```go
|
||||
// Create a cid from a marshaled string
|
||||
c, err := cid.Decode("zdvgqEMYmNeH5fKciougvQcfzMcNjF3Z1tPouJ8C7pc3pe63k")
|
||||
if err != nil {...}
|
||||
|
||||
fmt.Println("Got CID: ", c)
|
||||
```
|
||||
|
||||
### Creating a CID from scratch
|
||||
```go
|
||||
// Create a cid manually by specifying the 'prefix' parameters
|
||||
pref := cid.Prefix{
|
||||
Version: 1,
|
||||
Codec: cid.Raw,
|
||||
MhType: mh.SHA2_256,
|
||||
MhLength: -1, // default length
|
||||
}
|
||||
|
||||
// And then feed it some data
|
||||
c, err := pref.Sum([]byte("Hello World!"))
|
||||
if err != nil {...}
|
||||
|
||||
fmt.Println("Created CID: ", c)
|
||||
```
|
||||
|
||||
### Check if two CIDs match
|
||||
```go
|
||||
// To test if two cid's are equivalent, be sure to use the 'Equals' method:
|
||||
if c1.Equals(c2) {
|
||||
fmt.Println("These two refer to the same exact data!")
|
||||
}
|
||||
```
|
||||
|
||||
### Check if some data matches a given CID
|
||||
```go
|
||||
// To check if some data matches a given cid,
|
||||
// Get your CIDs prefix, and use that to sum the data in question:
|
||||
other, err := c.Prefix().Sum(mydata)
|
||||
if err != nil {...}
|
||||
|
||||
if !c.Equals(other) {
|
||||
fmt.Println("This data is different.")
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Contribute
|
||||
|
||||
PRs are welcome!
|
||||
|
||||
Small note: If editing the Readme, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.
|
||||
|
||||
## License
|
||||
|
||||
MIT © Jeromy Johnson
|
||||
76
cid.go
76
cid.go
@@ -3,7 +3,9 @@ package cid
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
mbase "github.com/multiformats/go-multibase"
|
||||
mh "github.com/multiformats/go-multihash"
|
||||
@@ -12,21 +14,23 @@ import (
|
||||
const UnsupportedVersionString = "<unsupported cid version>"
|
||||
|
||||
const (
|
||||
Protobuf = 0x70
|
||||
CBOR = 0x71
|
||||
Raw = 0x72
|
||||
JSON = 0x73
|
||||
Raw = 0x55
|
||||
|
||||
DagProtobuf = 0x70
|
||||
DagCBOR = 0x71
|
||||
|
||||
EthereumBlock = 0x90
|
||||
EthereumTx = 0x91
|
||||
BitcoinBlock = 0xb0
|
||||
BitcoinTx = 0xb1
|
||||
ZcashBlock = 0xc0
|
||||
ZcashTx = 0xc1
|
||||
)
|
||||
|
||||
func NewCidV0(h mh.Multihash) *Cid {
|
||||
return &Cid{
|
||||
version: 0,
|
||||
codec: Protobuf,
|
||||
codec: DagProtobuf,
|
||||
hash: h,
|
||||
}
|
||||
}
|
||||
@@ -45,7 +49,29 @@ type Cid struct {
|
||||
hash mh.Multihash
|
||||
}
|
||||
|
||||
func Parse(v interface{}) (*Cid, error) {
|
||||
switch v2 := v.(type) {
|
||||
case string:
|
||||
if strings.Contains(v2, "/ipfs/") {
|
||||
return Decode(strings.Split(v2, "/ipfs/")[1])
|
||||
}
|
||||
return Decode(v2)
|
||||
case []byte:
|
||||
return Cast(v2)
|
||||
case mh.Multihash:
|
||||
return NewCidV0(v2), nil
|
||||
case *Cid:
|
||||
return v2, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("can't parse %+v as Cid", v2)
|
||||
}
|
||||
}
|
||||
|
||||
func Decode(v string) (*Cid, error) {
|
||||
if len(v) < 2 {
|
||||
return nil, fmt.Errorf("cid too short")
|
||||
}
|
||||
|
||||
if len(v) == 46 && v[:2] == "Qm" {
|
||||
hash, err := mh.FromB58String(v)
|
||||
if err != nil {
|
||||
@@ -63,6 +89,23 @@ func Decode(v string) (*Cid, error) {
|
||||
return Cast(data)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrVarintBuffSmall = errors.New("reading varint: buffer to small")
|
||||
ErrVarintTooBig = errors.New("reading varint: varint bigger than 64bits" +
|
||||
" and not supported")
|
||||
)
|
||||
|
||||
func uvError(read int) error {
|
||||
switch {
|
||||
case read == 0:
|
||||
return ErrVarintBuffSmall
|
||||
case read < 0:
|
||||
return ErrVarintTooBig
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func Cast(data []byte) (*Cid, error) {
|
||||
if len(data) == 34 && data[0] == 18 && data[1] == 32 {
|
||||
h, err := mh.Cast(data)
|
||||
@@ -71,18 +114,25 @@ func Cast(data []byte) (*Cid, error) {
|
||||
}
|
||||
|
||||
return &Cid{
|
||||
codec: Protobuf,
|
||||
codec: DagProtobuf,
|
||||
version: 0,
|
||||
hash: h,
|
||||
}, nil
|
||||
}
|
||||
|
||||
vers, n := binary.Uvarint(data)
|
||||
if err := uvError(n); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if vers != 0 && vers != 1 {
|
||||
return nil, fmt.Errorf("invalid cid version number: %d", vers)
|
||||
}
|
||||
|
||||
codec, cn := binary.Uvarint(data[n:])
|
||||
if err := uvError(cn); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rest := data[n+cn:]
|
||||
h, err := mh.Cast(rest)
|
||||
@@ -137,10 +187,14 @@ func (c *Cid) bytesV0() []byte {
|
||||
}
|
||||
|
||||
func (c *Cid) bytesV1() []byte {
|
||||
buf := make([]byte, 8+len(c.hash))
|
||||
// two 8 bytes (max) numbers plus hash
|
||||
buf := make([]byte, 2*binary.MaxVarintLen64+len(c.hash))
|
||||
n := binary.PutUvarint(buf, c.version)
|
||||
n += binary.PutUvarint(buf[n:], c.codec)
|
||||
copy(buf[n:], c.hash)
|
||||
cn := copy(buf[n:], c.hash)
|
||||
if cn != len(c.hash) {
|
||||
panic("copy hash length is inconsistent")
|
||||
}
|
||||
|
||||
return buf[:n+len(c.hash)]
|
||||
}
|
||||
@@ -194,7 +248,7 @@ func (c *Cid) Prefix() Prefix {
|
||||
type Prefix struct {
|
||||
Version uint64
|
||||
Codec uint64
|
||||
MhType int
|
||||
MhType uint64
|
||||
MhLength int
|
||||
}
|
||||
|
||||
@@ -215,7 +269,7 @@ func (p Prefix) Sum(data []byte) (*Cid, error) {
|
||||
}
|
||||
|
||||
func (p Prefix) Bytes() []byte {
|
||||
buf := make([]byte, 16)
|
||||
buf := make([]byte, 4*binary.MaxVarintLen64)
|
||||
n := binary.PutUvarint(buf, p.Version)
|
||||
n += binary.PutUvarint(buf[n:], p.Codec)
|
||||
n += binary.PutUvarint(buf[n:], uint64(p.MhType))
|
||||
@@ -248,7 +302,7 @@ func PrefixFromBytes(buf []byte) (Prefix, error) {
|
||||
return Prefix{
|
||||
Version: vers,
|
||||
Codec: codec,
|
||||
MhType: int(mhtype),
|
||||
MhType: mhtype,
|
||||
MhLength: int(mhlen),
|
||||
}, nil
|
||||
}
|
||||
|
||||
37
cid_fuzz.go
Normal file
37
cid_fuzz.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// +build gofuzz
|
||||
|
||||
package cid
|
||||
|
||||
func Fuzz(data []byte) int {
|
||||
cid, err := Cast(data)
|
||||
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
_ = cid.Bytes()
|
||||
_ = cid.String()
|
||||
p := cid.Prefix()
|
||||
_ = p.Bytes()
|
||||
|
||||
if !cid.Equals(cid) {
|
||||
panic("inequality")
|
||||
}
|
||||
|
||||
// json loop
|
||||
json, err := cid.MarshalJSON()
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
cid2 := &Cid{}
|
||||
err = cid2.UnmarshalJSON(json)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
if !cid.Equals(cid2) {
|
||||
panic("json loop not equal")
|
||||
}
|
||||
|
||||
return 1
|
||||
}
|
||||
84
cid_test.go
84
cid_test.go
@@ -2,7 +2,9 @@ package cid
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
mh "github.com/multiformats/go-multihash"
|
||||
@@ -52,6 +54,13 @@ func TestBasicMarshaling(t *testing.T) {
|
||||
assertEqual(t, cid, out2)
|
||||
}
|
||||
|
||||
func TestEmptyString(t *testing.T) {
|
||||
_, err := Decode("")
|
||||
if err == nil {
|
||||
t.Fatal("shouldnt be able to parse an empty cid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestV0Handling(t *testing.T) {
|
||||
old := "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
|
||||
|
||||
@@ -84,7 +93,7 @@ func TestV0ErrorCases(t *testing.T) {
|
||||
func TestPrefixRoundtrip(t *testing.T) {
|
||||
data := []byte("this is some test content")
|
||||
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
||||
c := NewCidV1(CBOR, hash)
|
||||
c := NewCidV1(DagCBOR, hash)
|
||||
|
||||
pref := c.Prefix()
|
||||
|
||||
@@ -110,6 +119,15 @@ func TestPrefixRoundtrip(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test16BytesVarint(t *testing.T) {
|
||||
data := []byte("this is some test content")
|
||||
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
||||
c := NewCidV1(DagCBOR, hash)
|
||||
|
||||
c.codec = 1 << 63
|
||||
_ = c.Bytes()
|
||||
}
|
||||
|
||||
func TestFuzzCid(t *testing.T) {
|
||||
buf := make([]byte, 128)
|
||||
for i := 0; i < 200; i++ {
|
||||
@@ -118,3 +136,67 @@ func TestFuzzCid(t *testing.T) {
|
||||
_, _ = Cast(buf[:s])
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
cid, err := Parse(123)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error from Parse()")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "can't parse 123 as Cid") {
|
||||
t.Fatalf("expected int error, got %s", err.Error())
|
||||
}
|
||||
|
||||
theHash := "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
|
||||
h, err := mh.FromB58String(theHash)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assertions := [][]interface{}{
|
||||
[]interface{}{NewCidV0(h), theHash},
|
||||
[]interface{}{NewCidV0(h).Bytes(), theHash},
|
||||
[]interface{}{h, theHash},
|
||||
[]interface{}{theHash, theHash},
|
||||
[]interface{}{"/ipfs/" + theHash, theHash},
|
||||
[]interface{}{"https://ipfs.io/ipfs/" + theHash, theHash},
|
||||
[]interface{}{"http://localhost:8080/ipfs/" + theHash, theHash},
|
||||
}
|
||||
|
||||
assert := func(arg interface{}, expected string) error {
|
||||
cid, err = Parse(arg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cid.version != 0 {
|
||||
return fmt.Errorf("expected version 0, got %s", string(cid.version))
|
||||
}
|
||||
actual := cid.Hash().B58String()
|
||||
if actual != expected {
|
||||
return fmt.Errorf("expected hash %s, got %s", expected, actual)
|
||||
}
|
||||
actual = cid.String()
|
||||
if actual != expected {
|
||||
return fmt.Errorf("expected string %s, got %s", expected, actual)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, args := range assertions {
|
||||
err := assert(args[0], args[1].(string))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexDecode(t *testing.T) {
|
||||
hexcid := "f015512209d8453505bdc6f269678e16b3e56c2a2948a41f2c792617cc9611ed363c95b63"
|
||||
c, err := Decode(hexcid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if c.String() != "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG" {
|
||||
t.Fatal("hash value failed to round trip decoding from hex")
|
||||
}
|
||||
}
|
||||
|
||||
1
fuzz-data/corpus/cid0
Normal file
1
fuzz-data/corpus/cid0
Normal file
@@ -0,0 +1 @@
|
||||
ëgáD1üüÊe<C38A>-D˜/¹q3ø~å(Ä7`8–<38>‡n
|
||||
1
fuzz-data/corpus/cid1
Normal file
1
fuzz-data/corpus/cid1
Normal file
@@ -0,0 +1 @@
|
||||
q -[<5B>ï<EFBFBD>h<EFBFBD>[<5B><10><>
|
||||
12
package.json
12
package.json
@@ -9,22 +9,22 @@
|
||||
"gxDependencies": [
|
||||
{
|
||||
"author": "whyrusleeping",
|
||||
"hash": "QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J",
|
||||
"hash": "QmbZ6Cee2uHjG7hf19qLHppgKDRtaG4CVtMzdmK9VCVqLu",
|
||||
"name": "go-multihash",
|
||||
"version": "1.0.0"
|
||||
"version": "1.0.2"
|
||||
},
|
||||
{
|
||||
"author": "whyrusleeping",
|
||||
"hash": "QmYiTi9mKBMjfiup7na7PhJK7QEZPdMTJenLdgFYVQ2NUv",
|
||||
"hash": "QmcxkxTVuURV2Ptse8TvkqH5BQDwV62X1x19JqqvbBzwUM",
|
||||
"name": "go-multibase",
|
||||
"version": "0.2.0"
|
||||
"version": "0.2.3"
|
||||
}
|
||||
],
|
||||
"gxVersion": "0.8.0",
|
||||
"language": "go",
|
||||
"license": "",
|
||||
"license": "MIT",
|
||||
"name": "go-cid",
|
||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
||||
"version": "0.7.2"
|
||||
"version": "0.7.10"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user