Compare commits
14 Commits
feat/strin
...
v0.0.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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,32 @@
|
|||||||
sudo: false
|
os:
|
||||||
|
- linux
|
||||||
|
|
||||||
language: go
|
language: go
|
||||||
|
|
||||||
go:
|
go:
|
||||||
- 'tip'
|
- 1.11.x
|
||||||
|
|
||||||
|
env:
|
||||||
|
global:
|
||||||
|
- GOTFLAGS="-race"
|
||||||
|
matrix:
|
||||||
|
- BUILD_DEPTYPE=gx
|
||||||
|
- 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/src/gx
|
||||||
|
- $GOPATH/pkg/mod
|
||||||
|
- $HOME/.cache/go-build
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
email: false
|
email: false
|
||||||
|
|
||||||
|
|||||||
55
cid.go
55
cid.go
@@ -21,6 +21,7 @@ package cid
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -79,6 +80,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
|
||||||
@@ -103,6 +106,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 +131,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 +168,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 }
|
||||||
@@ -308,6 +320,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 {
|
||||||
@@ -398,6 +432,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.
|
||||||
@@ -490,7 +536,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
|
||||||
}
|
}
|
||||||
|
|||||||
68
cid_test.go
68
cid_test.go
@@ -35,6 +35,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 +73,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 +186,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 {
|
||||||
|
|||||||
6
go.mod
Normal file
6
go.mod
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
module github.com/ipfs/go-cid
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/multiformats/go-multibase v0.0.1
|
||||||
|
github.com/multiformats/go-multihash v0.0.1
|
||||||
|
)
|
||||||
20
go.sum
Normal file
20
go.sum
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
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/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=
|
||||||
|
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=
|
||||||
@@ -9,9 +9,9 @@
|
|||||||
"gxDependencies": [
|
"gxDependencies": [
|
||||||
{
|
{
|
||||||
"author": "whyrusleeping",
|
"author": "whyrusleeping",
|
||||||
"hash": "QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8",
|
"hash": "QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW",
|
||||||
"name": "go-multihash",
|
"name": "go-multihash",
|
||||||
"version": "1.0.8"
|
"version": "1.0.9"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"author": "whyrusleeping",
|
"author": "whyrusleeping",
|
||||||
@@ -25,6 +25,6 @@
|
|||||||
"license": "MIT",
|
"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.9.0"
|
"version": "0.9.3"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user