Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
03f8d08574 | ||
|
|
4ff5ee2b99 | ||
|
|
2b1b4a8339 | ||
|
|
ca0a568b21 | ||
|
|
896454b8a7 | ||
|
|
f623f824db | ||
|
|
15876e12dc | ||
|
|
18f61fe7b9 | ||
|
|
f68586ac44 | ||
|
|
1260e85a2d | ||
|
|
a0eff8c9d1 |
@@ -1 +1 @@
|
|||||||
0.7.6: QmX4hxL9LDFVpYtNfBEBgVSynRGsooVf4F8nrvJiCZuxqq
|
0.7.8: QmXm6Di2T89xAeprno3x6nD6jRgtMm2wRy25DX4zc4gk8V
|
||||||
|
|||||||
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
|
||||||
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
|
||||||
12
cid.go
12
cid.go
@@ -14,10 +14,10 @@ import (
|
|||||||
const UnsupportedVersionString = "<unsupported cid version>"
|
const UnsupportedVersionString = "<unsupported cid version>"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Protobuf = 0x70
|
Raw = 0x55
|
||||||
CBOR = 0x71
|
|
||||||
Raw = 0x72
|
DagProtobuf = 0x70
|
||||||
JSON = 0x73
|
DagCBOR = 0x71
|
||||||
|
|
||||||
EthereumBlock = 0x90
|
EthereumBlock = 0x90
|
||||||
EthereumTx = 0x91
|
EthereumTx = 0x91
|
||||||
@@ -30,7 +30,7 @@ const (
|
|||||||
func NewCidV0(h mh.Multihash) *Cid {
|
func NewCidV0(h mh.Multihash) *Cid {
|
||||||
return &Cid{
|
return &Cid{
|
||||||
version: 0,
|
version: 0,
|
||||||
codec: Protobuf,
|
codec: DagProtobuf,
|
||||||
hash: h,
|
hash: h,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ func Cast(data []byte) (*Cid, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Cid{
|
return &Cid{
|
||||||
codec: Protobuf,
|
codec: DagProtobuf,
|
||||||
version: 0,
|
version: 0,
|
||||||
hash: h,
|
hash: h,
|
||||||
}, nil
|
}, nil
|
||||||
|
|||||||
16
cid_test.go
16
cid_test.go
@@ -93,7 +93,7 @@ func TestV0ErrorCases(t *testing.T) {
|
|||||||
func TestPrefixRoundtrip(t *testing.T) {
|
func TestPrefixRoundtrip(t *testing.T) {
|
||||||
data := []byte("this is some test content")
|
data := []byte("this is some test content")
|
||||||
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
||||||
c := NewCidV1(CBOR, hash)
|
c := NewCidV1(DagCBOR, hash)
|
||||||
|
|
||||||
pref := c.Prefix()
|
pref := c.Prefix()
|
||||||
|
|
||||||
@@ -122,7 +122,7 @@ func TestPrefixRoundtrip(t *testing.T) {
|
|||||||
func Test16BytesVarint(t *testing.T) {
|
func Test16BytesVarint(t *testing.T) {
|
||||||
data := []byte("this is some test content")
|
data := []byte("this is some test content")
|
||||||
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
||||||
c := NewCidV1(CBOR, hash)
|
c := NewCidV1(DagCBOR, hash)
|
||||||
|
|
||||||
c.codec = 1 << 63
|
c.codec = 1 << 63
|
||||||
_ = c.Bytes()
|
_ = c.Bytes()
|
||||||
@@ -188,3 +188,15 @@ func TestParse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"author": "whyrusleeping",
|
"author": "whyrusleeping",
|
||||||
"hash": "QmUq3H9YpcPphbRj6ct6rBgBE377A8wANP8zPMRqe1WYbf",
|
"hash": "QmXzWFN4iLdX1Vq8Sc13mET7aXsHkTyJoMbaJJD3NGRhiJ",
|
||||||
"name": "go-multibase",
|
"name": "go-multibase",
|
||||||
"version": "0.2.1"
|
"version": "0.2.1"
|
||||||
}
|
}
|
||||||
@@ -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.7.6"
|
"version": "0.7.8"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user