Documentation ¶
Overview ¶
Package cid implements the Content-IDentifiers specification (https://github.com/ipld/cid) in Go. CIDs are self-describing content-addressed identifiers useful for distributed information systems. CIDs are used in the IPFS (https://ipfs.io) project ecosystem.
CIDs have two major versions. A CIDv0 corresponds to a multihash of type DagProtobuf, is deprecated and exists for compatibility reasons. Usually, CIDv1 should be used.
A CIDv1 has four parts:
<cidv1> ::= <multibase-prefix><cid-version><multicodec-packed-content-type><multihash-content-address>
As shown above, the CID implementation relies heavily on Multiformats, particularly Multibase (https://github.com/multiformats/go-multibase), Multicodec (https://github.com/multiformats/multicodec) and Multihash implementations (https://github.com/multiformats/go-multihash).
Index ¶
- Constants
- Variables
- func ExtractEncoding(v string) (mbase.Encoding, error)
- type Builder
- type Cid
- func (c Cid) Bytes() []byte
- func (c Cid) Defined() bool
- func (c Cid) Encode(base mbase.Encoder) string
- func (c Cid) Equals(o Cid) bool
- func (c Cid) Hash() mh.Multihash
- func (c Cid) KeyString() string
- func (c Cid) Loggable() map[string]interface{}
- func (c Cid) MarshalBinary() ([]byte, error)
- func (c Cid) MarshalJSON() ([]byte, error)
- func (c Cid) MarshalText() ([]byte, error)
- func (c Cid) Prefix() Prefix
- func (c Cid) String() string
- func (c Cid) StringOfBase(base mbase.Encoding) (string, error)
- func (c Cid) Type() uint64
- func (c *Cid) UnmarshalBinary(data []byte) error
- func (c *Cid) UnmarshalJSON(b []byte) error
- func (c *Cid) UnmarshalText(text []byte) error
- func (c Cid) Version() uint64
- type Prefix
- type Set
- type V0Builder
- type V1Builder
Examples ¶
Constants ¶
const ( Raw = 0x55 DagProtobuf = 0x70 DagCBOR = 0x71 GitRaw = 0x78 EthBlock = 0x90 EthBlockList = 0x91 EthTxTrie = 0x92 EthTx = 0x93 EthTxReceiptTrie = 0x94 EthTxReceipt = 0x95 EthStateTrie = 0x96 EthAccountSnapshot = 0x97 EthStorageTrie = 0x98 BitcoinBlock = 0xb0 BitcoinTx = 0xb1 ZcashBlock = 0xc0 ZcashTx = 0xc1 DecredBlock = 0xe0 DecredTx = 0xe1 DashBlock = 0xf0 DashTx = 0xf1 )
These are multicodec-packed content types. The should match the codes described in the authoritative document: https://github.com/multiformats/multicodec/blob/master/table.csv
const UnsupportedVersionString = "<unsupported cid version>"
UnsupportedVersionString just holds an error message
Variables ¶
var ( // ErrVarintBuffSmall means that a buffer passed to the cid parser was not // long enough, or did not contain an invalid cid ErrVarintBuffSmall = errors.New("reading varint: buffer too small") // ErrVarintTooBig means that the varint in the given cid was above the // limit of 2^64 ErrVarintTooBig = errors.New("reading varint: varint bigger than 64bits" + " and not supported") // ErrCidTooShort means that the cid passed to decode was not long // enough to be a valid Cid ErrCidTooShort = errors.New("cid too short") // ErrInvalidEncoding means that selected encoding is not supported // by this Cid version ErrInvalidEncoding = errors.New("invalid base encoding") )
var CodecToStr = map[uint64]string{ Raw: "raw", DagProtobuf: "protobuf", DagCBOR: "cbor", GitRaw: "git-raw", EthBlock: "eth-block", EthBlockList: "eth-block-list", EthTxTrie: "eth-tx-trie", EthTx: "eth-tx", EthTxReceiptTrie: "eth-tx-receipt-trie", EthTxReceipt: "eth-tx-receipt", EthStateTrie: "eth-state-trie", EthAccountSnapshot: "eth-account-snapshot", EthStorageTrie: "eth-storage-trie", BitcoinBlock: "bitcoin-block", BitcoinTx: "bitcoin-tx", ZcashBlock: "zcash-block", ZcashTx: "zcash-tx", DecredBlock: "decred-block", DecredTx: "decred-tx", DashBlock: "dash-block", DashTx: "dash-tx", }
CodecToStr maps the numeric codec to its name
var Codecs = map[string]uint64{ "v0": DagProtobuf, "raw": Raw, "protobuf": DagProtobuf, "cbor": DagCBOR, "git-raw": GitRaw, "eth-block": EthBlock, "eth-block-list": EthBlockList, "eth-tx-trie": EthTxTrie, "eth-tx": EthTx, "eth-tx-receipt-trie": EthTxReceiptTrie, "eth-tx-receipt": EthTxReceipt, "eth-state-trie": EthStateTrie, "eth-account-snapshot": EthAccountSnapshot, "eth-storage-trie": EthStorageTrie, "bitcoin-block": BitcoinBlock, "bitcoin-tx": BitcoinTx, "zcash-block": ZcashBlock, "zcash-tx": ZcashTx, "decred-block": DecredBlock, "decred-tx": DecredTx, "dash-block": DashBlock, "dash-tx": DashTx, }
Codecs maps the name of a codec to its type
var Undef = Cid{}
Undef can be used to represent a nil or undefined Cid, using Cid{} directly is also acceptable.
Functions ¶
Types ¶
type Cid ¶
type Cid struct {
// contains filtered or unexported fields
}
Cid represents a self-describing content addressed identifier. It is formed by a Version, a Codec (which indicates a multicodec-packed content type) and a Multihash.
func Cast ¶
Cast takes a Cid data slice, parses it and returns a Cid. For CidV1, the data buffer is in the form:
<version><codec-type><multihash>
CidV0 are also supported. In particular, data buffers starting with length 34 bytes, which starts with bytes [18,32...] are considered binary multihashes.
Please use decode when parsing a regular Cid string, as Cast does not expect multibase-encoded data. Cast accepts the output of Cid.Bytes().
func Decode ¶
Decode parses a Cid-encoded string and returns a Cid object. For CidV1, a Cid-encoded string is primarily a multibase string:
<multibase-type-code><base-encoded-string>
The base-encoded string represents a:
<version><codec-type><multihash>
Decode will also detect and parse CidV0 strings. Strings starting with "Qm" are considered CidV0 and treated directly as B58-encoded multihashes.
Example ¶
encoded := "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG" c, err := Decode(encoded) if err != nil { fmt.Printf("Error: %s", err) return } fmt.Println(c)
Output: zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG
func NewCidV0 ¶
NewCidV0 returns a Cid-wrapped multihash. They exist to allow IPFS to work with Cids while keeping compatibility with the plain-multihash format used used in IPFS. NewCidV1 should be used preferentially.
func Parse ¶
Parse is a short-hand function to perform Decode, Cast etc... on a generic interface{} type.
func (Cid) Bytes ¶
Bytes returns the byte representation of a Cid. The output of bytes can be parsed back into a Cid with Cast().
func (Cid) Defined ¶
Defined returns true if a Cid is defined Calling any other methods on an undefined Cid will result in undefined behavior.
func (Cid) Encode ¶
Encode return the string representation of a Cid in a given base when applicable. Version 0 Cid's are always in Base58 as they do not take a multibase prefix.
func (Cid) Equals ¶
Equals checks that two Cids are the same. In order for two Cids to be considered equal, the Version, the Codec and the Multihash must match.
func (Cid) Loggable ¶
Loggable returns a Loggable (as defined by https://godoc.org/github.com/ipfs/go-log).
func (Cid) MarshalBinary ¶
MarshalBinary is equivalent to Bytes(). It implements the encoding.BinaryMarshaler interface.
func (Cid) MarshalJSON ¶
MarshalJSON procudes a JSON representation of a Cid, which looks as follows:
{ "/": "<cid-string>" }
Note that this formatting comes from the IPLD specification (https://github.com/ipld/specs/tree/master/ipld)
func (Cid) MarshalText ¶
MarshalText is equivalent to String(). It implements the encoding.TextMarshaler interface.
func (Cid) String ¶
String returns the default string representation of a Cid. Currently, Base58 is used as the encoding for the multibase string.
func (Cid) StringOfBase ¶
String returns the string representation of a Cid encoded is selected base
func (*Cid) UnmarshalBinary ¶
UnmarshalBinary is equivalent to Cast(). It implements the encoding.BinaryUnmarshaler interface.
func (*Cid) UnmarshalJSON ¶
UnmarshalJSON parses the JSON representation of a Cid.
func (*Cid) UnmarshalText ¶
UnmarshalText is equivalent to Decode(). It implements the encoding.TextUnmarshaler interface.
type Prefix ¶
Prefix represents all the metadata of a Cid, that is, the Version, the Codec, the Multihash type and the Multihash length. It does not contains any actual content information. NOTE: The use -1 in MhLength to mean default length is deprecated,
use the V0Builder or V1Builder structures instead
func NewPrefixV0 ¶
NewPrefixV0 returns a CIDv0 prefix with the specified multihash type. DEPRECATED: Use V0Builder
func NewPrefixV1 ¶
NewPrefixV1 returns a CIDv1 prefix with the specified codec and multihash type. DEPRECATED: Use V1Builder
func PrefixFromBytes ¶
PrefixFromBytes parses a Prefix-byte representation onto a Prefix.
func (Prefix) Bytes ¶
Bytes returns a byte representation of a Prefix. It looks like:
<version><codec><mh-type><mh-length>
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set is a implementation of a set of Cids, that is, a structure to which holds a single copy of every Cids that is added to it.