cid

package module
v0.7.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2017 License: MIT Imports: 7 Imported by: 0

README

go-cid

Coverage Status Travis CI

A package to handle content IDs in go.

This is an implementation in go of the CID spec. It is used in go-ipfs and related packages to refer to a typed hunk of data.

Table of Contents

Install

make deps

Examples

To use CIDs, import it in your code like:

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
// 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
// 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
// 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
// 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 specification.

License

MIT © Jeromy Johnson

Documentation

Index

Constants

View Source
const (
	Raw = 0x55

	DagProtobuf = 0x70
	DagCBOR     = 0x71

	EthereumBlock = 0x90
	EthereumTx    = 0x91
	BitcoinBlock  = 0xb0
	BitcoinTx     = 0xb1
	ZcashBlock    = 0xc0
	ZcashTx       = 0xc1
)
View Source
const UnsupportedVersionString = "<unsupported cid version>"

Variables

View Source
var (
	ErrVarintBuffSmall = errors.New("reading varint: buffer to small")
	ErrVarintTooBig    = errors.New("reading varint: varint bigger than 64bits" +
		" and not supported")
)

Functions

This section is empty.

Types

type Cid

type Cid struct {
	// contains filtered or unexported fields
}

func Cast

func Cast(data []byte) (*Cid, error)

func Decode

func Decode(v string) (*Cid, error)

func NewCidV0

func NewCidV0(h mh.Multihash) *Cid

func NewCidV1

func NewCidV1(c uint64, h mh.Multihash) *Cid

func Parse

func Parse(v interface{}) (*Cid, error)

func (*Cid) Bytes

func (c *Cid) Bytes() []byte

func (*Cid) Equals

func (c *Cid) Equals(o *Cid) bool

func (*Cid) Hash

func (c *Cid) Hash() mh.Multihash

func (*Cid) KeyString

func (c *Cid) KeyString() string

func (*Cid) Loggable

func (c *Cid) Loggable() map[string]interface{}

func (*Cid) MarshalJSON

func (c *Cid) MarshalJSON() ([]byte, error)

func (*Cid) Prefix

func (c *Cid) Prefix() Prefix

func (*Cid) String

func (c *Cid) String() string

func (*Cid) Type

func (c *Cid) Type() uint64

func (*Cid) UnmarshalJSON

func (c *Cid) UnmarshalJSON(b []byte) error

type Prefix

type Prefix struct {
	Version  uint64
	Codec    uint64
	MhType   int
	MhLength int
}

Prefix represents all the metadata of a cid, minus any actual content information

func PrefixFromBytes

func PrefixFromBytes(buf []byte) (Prefix, error)

func (Prefix) Bytes

func (p Prefix) Bytes() []byte

func (Prefix) Sum

func (p Prefix) Sum(data []byte) (*Cid, error)

type Set

type Set struct {
	// contains filtered or unexported fields
}

func NewSet

func NewSet() *Set

func (*Set) Add

func (s *Set) Add(c *Cid)

func (*Set) ForEach

func (s *Set) ForEach(f func(c *Cid) error) error

func (*Set) Has

func (s *Set) Has(c *Cid) bool

func (*Set) Keys

func (s *Set) Keys() []*Cid

func (*Set) Len

func (s *Set) Len() int

func (*Set) Remove

func (s *Set) Remove(c *Cid)

func (*Set) Visit

func (s *Set) Visit(c *Cid) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL