Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f11b062c3 | ||
|
|
961f0fe7a8 | ||
|
|
6c7d9e3de2 | ||
|
|
e5a96152bd | ||
|
|
0f69fedd3a | ||
|
|
595cace68a | ||
|
|
167239b405 | ||
|
|
8522370eab |
@@ -1 +1 @@
|
|||||||
0.0.0: QmWzxM6XsNv1GoLs4dpQBay2NdMSWXRTFKmZwSHcsxg8ys
|
0.6.0: QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z
|
||||||
|
|||||||
93
cid.go
93
cid.go
@@ -5,17 +5,17 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
mh "github.com/jbenet/go-multihash"
|
|
||||||
mbase "github.com/multiformats/go-multibase"
|
mbase "github.com/multiformats/go-multibase"
|
||||||
|
mh "github.com/multiformats/go-multihash"
|
||||||
)
|
)
|
||||||
|
|
||||||
const UnsupportedVersionString = "<unsupported cid version>"
|
const UnsupportedVersionString = "<unsupported cid version>"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Protobuf = iota
|
Protobuf = 0x70
|
||||||
Raw
|
CBOR = 0x71
|
||||||
JSON
|
Raw = 0x72
|
||||||
CBOR
|
JSON = 0x73
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCidV0(h mh.Multihash) *Cid {
|
func NewCidV0(h mh.Multihash) *Cid {
|
||||||
@@ -164,3 +164,86 @@ func (c *Cid) UnmarshalJSON(b []byte) error {
|
|||||||
func (c *Cid) MarshalJSON() ([]byte, error) {
|
func (c *Cid) MarshalJSON() ([]byte, error) {
|
||||||
return []byte(fmt.Sprintf("\"%s\"", c.String())), nil
|
return []byte(fmt.Sprintf("\"%s\"", c.String())), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Cid) KeyString() string {
|
||||||
|
return string(c.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cid) Loggable() map[string]interface{} {
|
||||||
|
return map[string]interface{}{
|
||||||
|
"cid": c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cid) Prefix() Prefix {
|
||||||
|
dec, _ := mh.Decode(c.hash) // assuming we got a valid multiaddr, this will not error
|
||||||
|
return Prefix{
|
||||||
|
MhType: dec.Code,
|
||||||
|
MhLength: dec.Length,
|
||||||
|
Version: c.version,
|
||||||
|
Codec: c.codec,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prefix represents all the metadata of a cid, minus any actual content information
|
||||||
|
type Prefix struct {
|
||||||
|
Version uint64
|
||||||
|
Codec uint64
|
||||||
|
MhType int
|
||||||
|
MhLength int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Prefix) Sum(data []byte) (*Cid, error) {
|
||||||
|
hash, err := mh.Sum(data, p.MhType, p.MhLength)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch p.Version {
|
||||||
|
case 0:
|
||||||
|
return NewCidV0(hash), nil
|
||||||
|
case 1:
|
||||||
|
return NewCidV1(p.Codec, hash), nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("invalid cid version")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Prefix) Bytes() []byte {
|
||||||
|
buf := make([]byte, 16)
|
||||||
|
n := binary.PutUvarint(buf, p.Version)
|
||||||
|
n += binary.PutUvarint(buf[n:], p.Codec)
|
||||||
|
n += binary.PutUvarint(buf[n:], uint64(p.MhType))
|
||||||
|
n += binary.PutUvarint(buf[n:], uint64(p.MhLength))
|
||||||
|
return buf[:n]
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrefixFromBytes(buf []byte) (Prefix, error) {
|
||||||
|
r := bytes.NewReader(buf)
|
||||||
|
vers, err := binary.ReadUvarint(r)
|
||||||
|
if err != nil {
|
||||||
|
return Prefix{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
codec, err := binary.ReadUvarint(r)
|
||||||
|
if err != nil {
|
||||||
|
return Prefix{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
mhtype, err := binary.ReadUvarint(r)
|
||||||
|
if err != nil {
|
||||||
|
return Prefix{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
mhlen, err := binary.ReadUvarint(r)
|
||||||
|
if err != nil {
|
||||||
|
return Prefix{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return Prefix{
|
||||||
|
Version: vers,
|
||||||
|
Codec: codec,
|
||||||
|
MhType: int(mhtype),
|
||||||
|
MhLength: int(mhlen),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|||||||
31
cid_test.go
31
cid_test.go
@@ -4,7 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
mh "github.com/jbenet/go-multihash"
|
mh "github.com/multiformats/go-multihash"
|
||||||
)
|
)
|
||||||
|
|
||||||
func assertEqual(t *testing.T, a, b *Cid) {
|
func assertEqual(t *testing.T, a, b *Cid) {
|
||||||
@@ -79,3 +79,32 @@ func TestV0ErrorCases(t *testing.T) {
|
|||||||
t.Fatal("should have failed to decode that ref")
|
t.Fatal("should have failed to decode that ref")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrefixRoundtrip(t *testing.T) {
|
||||||
|
data := []byte("this is some test content")
|
||||||
|
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
||||||
|
c := NewCidV1(CBOR, hash)
|
||||||
|
|
||||||
|
pref := c.Prefix()
|
||||||
|
|
||||||
|
c2, err := pref.Sum(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.Equals(c2) {
|
||||||
|
t.Fatal("output didnt match original")
|
||||||
|
}
|
||||||
|
|
||||||
|
pb := pref.Bytes()
|
||||||
|
|
||||||
|
pref2, err := PrefixFromBytes(pb)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if pref.Version != pref2.Version || pref.Codec != pref2.Codec ||
|
||||||
|
pref.MhType != pref2.MhType || pref.MhLength != pref2.MhLength {
|
||||||
|
t.Fatal("input prefix didnt match output")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
12
package.json
12
package.json
@@ -1,17 +1,17 @@
|
|||||||
{
|
{
|
||||||
"author": "whyrusleeping",
|
"author": "whyrusleeping",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/multiformats/go-cid"
|
"url": "https://github.com/ipfs/go-cid"
|
||||||
},
|
},
|
||||||
"gx": {
|
"gx": {
|
||||||
"dvcsimport": "github.com/multiformats/go-cid"
|
"dvcsimport": "github.com/ipfs/go-cid"
|
||||||
},
|
},
|
||||||
"gxDependencies": [
|
"gxDependencies": [
|
||||||
{
|
{
|
||||||
"author": "whyrusleeping",
|
"author": "whyrusleeping",
|
||||||
"hash": "QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku",
|
"hash": "QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J",
|
||||||
"name": "go-multihash",
|
"name": "go-multihash",
|
||||||
"version": "0.0.0"
|
"version": "1.0.0"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"author": "whyrusleeping",
|
"author": "whyrusleeping",
|
||||||
@@ -24,5 +24,7 @@
|
|||||||
"language": "go",
|
"language": "go",
|
||||||
"license": "",
|
"license": "",
|
||||||
"name": "go-cid",
|
"name": "go-cid",
|
||||||
"version": "0.0.0"
|
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
||||||
|
"version": "0.6.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
set.go
13
set.go
@@ -26,7 +26,7 @@ func (s *Set) Len() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Set) Keys() []*Cid {
|
func (s *Set) Keys() []*Cid {
|
||||||
var out []*Cid
|
out := make([]*Cid, 0, len(s.set))
|
||||||
for k, _ := range s.set {
|
for k, _ := range s.set {
|
||||||
c, _ := Cast([]byte(k))
|
c, _ := Cast([]byte(k))
|
||||||
out = append(out, c)
|
out = append(out, c)
|
||||||
@@ -42,3 +42,14 @@ func (s *Set) Visit(c *Cid) bool {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Set) ForEach(f func(c *Cid) error) error {
|
||||||
|
for cs, _ := range s.set {
|
||||||
|
c, _ := Cast([]byte(cs))
|
||||||
|
err := f(c)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user