Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c8abab049 | ||
|
|
ab63d19c4e | ||
|
|
5ccc98a754 | ||
|
|
ba97b640bd | ||
|
|
9116bf8025 | ||
|
|
c67fe910f2 | ||
|
|
9c3e314588 | ||
|
|
5da6d87c58 | ||
|
|
6ce8a80816 | ||
|
|
5ec5bbcb48 | ||
|
|
8aeb1a44a8 | ||
|
|
832b6a0170 | ||
|
|
eae3431cc9 | ||
|
|
d0e0822854 | ||
|
|
bffa0300df | ||
|
|
691f8625be | ||
|
|
177ab398cc | ||
|
|
02ce4e9b23 | ||
|
|
460554fb6e | ||
|
|
5d17ab8c70 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
cid-fuzz.zip
|
||||||
@@ -1 +1 @@
|
|||||||
0.7.0: QmbTGYCo96Z9hiG37D9zeErFo5GjrEPcqdh7PJX1HTM73E
|
0.7.6: QmX4hxL9LDFVpYtNfBEBgVSynRGsooVf4F8nrvJiCZuxqq
|
||||||
|
|||||||
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.
|
||||||
63
cid.go
63
cid.go
@@ -3,7 +3,9 @@ package cid
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"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"
|
||||||
@@ -19,7 +21,10 @@ const (
|
|||||||
|
|
||||||
EthereumBlock = 0x90
|
EthereumBlock = 0x90
|
||||||
EthereumTx = 0x91
|
EthereumTx = 0x91
|
||||||
Bitcoin = 0xb0
|
BitcoinBlock = 0xb0
|
||||||
|
BitcoinTx = 0xb1
|
||||||
|
ZcashBlock = 0xc0
|
||||||
|
ZcashTx = 0xc1
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCidV0(h mh.Multihash) *Cid {
|
func NewCidV0(h mh.Multihash) *Cid {
|
||||||
@@ -44,7 +49,29 @@ type Cid struct {
|
|||||||
hash mh.Multihash
|
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) {
|
func Decode(v string) (*Cid, error) {
|
||||||
|
if len(v) < 2 {
|
||||||
|
return nil, fmt.Errorf("cid too short")
|
||||||
|
}
|
||||||
|
|
||||||
if len(v) == 46 && v[:2] == "Qm" {
|
if len(v) == 46 && v[:2] == "Qm" {
|
||||||
hash, err := mh.FromB58String(v)
|
hash, err := mh.FromB58String(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -62,6 +89,23 @@ func Decode(v string) (*Cid, error) {
|
|||||||
return Cast(data)
|
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) {
|
func Cast(data []byte) (*Cid, error) {
|
||||||
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)
|
||||||
@@ -77,11 +121,18 @@ func Cast(data []byte) (*Cid, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vers, n := binary.Uvarint(data)
|
vers, n := binary.Uvarint(data)
|
||||||
|
if err := uvError(n); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if vers != 0 && vers != 1 {
|
if vers != 0 && vers != 1 {
|
||||||
return nil, fmt.Errorf("invalid cid version number: %d", vers)
|
return nil, fmt.Errorf("invalid cid version number: %d", vers)
|
||||||
}
|
}
|
||||||
|
|
||||||
codec, cn := binary.Uvarint(data[n:])
|
codec, cn := binary.Uvarint(data[n:])
|
||||||
|
if err := uvError(cn); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
rest := data[n+cn:]
|
rest := data[n+cn:]
|
||||||
h, err := mh.Cast(rest)
|
h, err := mh.Cast(rest)
|
||||||
@@ -136,10 +187,14 @@ func (c *Cid) bytesV0() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cid) bytesV1() []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, c.version)
|
||||||
n += binary.PutUvarint(buf[n:], c.codec)
|
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)]
|
return buf[:n+len(c.hash)]
|
||||||
}
|
}
|
||||||
@@ -214,7 +269,7 @@ func (p Prefix) Sum(data []byte) (*Cid, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p Prefix) Bytes() []byte {
|
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, p.Version)
|
||||||
n += binary.PutUvarint(buf[n:], p.Codec)
|
n += binary.PutUvarint(buf[n:], p.Codec)
|
||||||
n += binary.PutUvarint(buf[n:], uint64(p.MhType))
|
n += binary.PutUvarint(buf[n:], uint64(p.MhType))
|
||||||
|
|||||||
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
|
||||||
|
}
|
||||||
70
cid_test.go
70
cid_test.go
@@ -2,7 +2,9 @@ package cid
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
mh "github.com/multiformats/go-multihash"
|
mh "github.com/multiformats/go-multihash"
|
||||||
@@ -52,6 +54,13 @@ func TestBasicMarshaling(t *testing.T) {
|
|||||||
assertEqual(t, cid, out2)
|
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) {
|
func TestV0Handling(t *testing.T) {
|
||||||
old := "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
|
old := "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
|
||||||
|
|
||||||
@@ -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(CBOR, hash)
|
||||||
|
|
||||||
|
c.codec = 1 << 63
|
||||||
|
_ = c.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
func TestFuzzCid(t *testing.T) {
|
func TestFuzzCid(t *testing.T) {
|
||||||
buf := make([]byte, 128)
|
buf := make([]byte, 128)
|
||||||
for i := 0; i < 200; i++ {
|
for i := 0; i < 200; i++ {
|
||||||
@@ -118,3 +136,55 @@ func TestFuzzCid(t *testing.T) {
|
|||||||
_, _ = Cast(buf[:s])
|
_, _ = 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
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><>
|
||||||
@@ -15,16 +15,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"author": "whyrusleeping",
|
"author": "whyrusleeping",
|
||||||
"hash": "QmYiTi9mKBMjfiup7na7PhJK7QEZPdMTJenLdgFYVQ2NUv",
|
"hash": "QmUq3H9YpcPphbRj6ct6rBgBE377A8wANP8zPMRqe1WYbf",
|
||||||
"name": "go-multibase",
|
"name": "go-multibase",
|
||||||
"version": "0.2.0"
|
"version": "0.2.1"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"gxVersion": "0.8.0",
|
"gxVersion": "0.8.0",
|
||||||
"language": "go",
|
"language": "go",
|
||||||
"license": "",
|
"license": "MIT",
|
||||||
"name": "go-cid",
|
"name": "go-cid",
|
||||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
||||||
"version": "0.7.0"
|
"version": "0.7.6"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user