Compare commits

...

13 Commits

Author SHA1 Message Date
Jeromy
460554fb6e gx publish 0.7.2 2016-10-24 09:16:05 -07:00
Jeromy
5d17ab8c70 add bitcoin tx value 2016-10-24 09:10:06 -07:00
Jeromy
ed8d621d9a gx publish 0.7.0 2016-10-19 17:54:06 -07:00
Jeromy
9a43493ca7 add codes for bitcoin and ethereum 2016-10-19 17:53:39 -07:00
Jeromy
8677934d48 add small test to fuzz cid creation routines 2016-10-09 12:19:14 -07:00
Jeromy
5f11b062c3 gx publish 0.6.0 2016-10-08 21:18:24 -07:00
Jeromy
961f0fe7a8 add 'Prefix' object and some more helper routines 2016-10-08 21:17:59 -07:00
Jeromy
6c7d9e3de2 gx publish 0.5.2 2016-10-05 12:08:42 -07:00
Jeromy
e5a96152bd gx publish 0.5.1 2016-10-05 11:37:05 -07:00
Jeromy
0f69fedd3a gx publish 0.5.0 2016-09-27 06:14:57 -07:00
Jeromy
595cace68a make cid's loggable 2016-09-27 06:14:46 -07:00
Jeromy
167239b405 gx publish 0.3.0 2016-09-27 03:43:09 -07:00
Jeromy
8522370eab gx publish 0.1.0 2016-09-04 11:52:09 -07:00
5 changed files with 153 additions and 13 deletions

View File

@@ -1 +1 @@
0.0.0: QmWzxM6XsNv1GoLs4dpQBay2NdMSWXRTFKmZwSHcsxg8ys
0.7.2: QmPBa9N9MuZ16fPuKMv9KCq1aAwhkTY4G6LXQYQmWJiCrj

98
cid.go
View File

@@ -5,17 +5,22 @@ import (
"encoding/binary"
"fmt"
mh "github.com/jbenet/go-multihash"
mbase "github.com/multiformats/go-multibase"
mh "github.com/multiformats/go-multihash"
)
const UnsupportedVersionString = "<unsupported cid version>"
const (
Protobuf = iota
Raw
JSON
CBOR
Protobuf = 0x70
CBOR = 0x71
Raw = 0x72
JSON = 0x73
EthereumBlock = 0x90
EthereumTx = 0x91
BitcoinBlock = 0xb0
BitcoinTx = 0xb1
)
func NewCidV0(h mh.Multihash) *Cid {
@@ -164,3 +169,86 @@ func (c *Cid) UnmarshalJSON(b []byte) error {
func (c *Cid) MarshalJSON() ([]byte, error) {
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
}

View File

@@ -2,9 +2,10 @@ package cid
import (
"bytes"
"math/rand"
"testing"
mh "github.com/jbenet/go-multihash"
mh "github.com/multiformats/go-multihash"
)
func assertEqual(t *testing.T, a, b *Cid) {
@@ -79,3 +80,41 @@ func TestV0ErrorCases(t *testing.T) {
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")
}
}
func TestFuzzCid(t *testing.T) {
buf := make([]byte, 128)
for i := 0; i < 200; i++ {
s := rand.Intn(128)
rand.Read(buf[:s])
_, _ = Cast(buf[:s])
}
}

View File

@@ -1,17 +1,17 @@
{
"author": "whyrusleeping",
"bugs": {
"url": "https://github.com/multiformats/go-cid"
"url": "https://github.com/ipfs/go-cid"
},
"gx": {
"dvcsimport": "github.com/multiformats/go-cid"
"dvcsimport": "github.com/ipfs/go-cid"
},
"gxDependencies": [
{
"author": "whyrusleeping",
"hash": "QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku",
"hash": "QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J",
"name": "go-multihash",
"version": "0.0.0"
"version": "1.0.0"
},
{
"author": "whyrusleeping",
@@ -24,5 +24,7 @@
"language": "go",
"license": "",
"name": "go-cid",
"version": "0.0.0"
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.7.2"
}

13
set.go
View File

@@ -26,7 +26,7 @@ func (s *Set) Len() int {
}
func (s *Set) Keys() []*Cid {
var out []*Cid
out := make([]*Cid, 0, len(s.set))
for k, _ := range s.set {
c, _ := Cast([]byte(k))
out = append(out, c)
@@ -42,3 +42,14 @@ func (s *Set) Visit(c *Cid) bool {
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
}