Compare commits
61 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b9ff3906e | ||
|
|
f7cb4c91eb | ||
|
|
85cd30874e | ||
|
|
12e2623f35 | ||
|
|
d683e9243c | ||
|
|
18af217de6 | ||
|
|
9830f823b5 | ||
|
|
d110f73c43 | ||
|
|
95a7ed4233 | ||
|
|
8dd6fe2778 | ||
|
|
a25d68f3e4 | ||
|
|
7c82f3b81c | ||
|
|
75caa6bdbd | ||
|
|
c1c89c20c1 | ||
|
|
628a0123ed | ||
|
|
266e76d591 | ||
|
|
7d7cb88d78 | ||
|
|
723b4ab105 | ||
|
|
9cb3848e51 | ||
|
|
f458e04107 | ||
|
|
72cd3d39d7 | ||
|
|
c1b740035a | ||
|
|
3da5bbbe45 | ||
|
|
9d598bbdbd | ||
|
|
b420452400 | ||
|
|
ed7dab1755 | ||
|
|
5df89959a0 | ||
|
|
60ab0f84f0 | ||
|
|
08e15f8a6c | ||
|
|
58b483a841 | ||
|
|
79e75dffeb | ||
|
|
d93d4baeab | ||
|
|
cf8cf8856e | ||
|
|
aa8072eeb5 | ||
|
|
51871ccaa9 | ||
|
|
9238d1a533 | ||
|
|
16bbe1cf14 | ||
|
|
c91a795815 | ||
|
|
64acce09e1 | ||
|
|
2a2f67d2c4 | ||
|
|
86ca2a8300 | ||
|
|
e0a778be45 | ||
|
|
9bb7ea6920 | ||
|
|
3f1777738f | ||
|
|
b1cc3e404d | ||
|
|
b16425b966 | ||
|
|
f04f9216e7 | ||
|
|
e7e67e08cf | ||
|
|
29a66d1820 | ||
|
|
08f30d213e | ||
|
|
cf3b4efcaf | ||
|
|
ca991e8eb6 | ||
|
|
8d327b2f4b | ||
|
|
14b828acf5 | ||
|
|
00439572fb | ||
|
|
37bf2f9503 | ||
|
|
e6d04f280e | ||
|
|
033594dcd6 | ||
|
|
c9e99b39db | ||
|
|
3ec3578fe9 | ||
|
|
628ab3426c |
8
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
8
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
blank_issues_enabled: false
|
||||
contact_links:
|
||||
- name: Getting Help on IPFS
|
||||
url: https://ipfs.io/help
|
||||
about: All information about how and where to get help on IPFS.
|
||||
- name: IPFS Official Forum
|
||||
url: https://discuss.ipfs.io
|
||||
about: Please post general questions, support requests, and discussions here.
|
||||
19
.github/ISSUE_TEMPLATE/open_an_issue.md
vendored
Normal file
19
.github/ISSUE_TEMPLATE/open_an_issue.md
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
---
|
||||
name: Open an issue
|
||||
about: Only for actionable issues relevant to this repository.
|
||||
title: ''
|
||||
labels: need/triage
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
<!--
|
||||
Hello! To ensure this issue is correctly addressed as soon as possible by the IPFS team, please try to make sure:
|
||||
|
||||
- This issue is relevant to this repository's topic or codebase.
|
||||
|
||||
- A clear description is provided. It should includes as much relevant information as possible and clear scope for the issue to be actionable.
|
||||
|
||||
FOR GENERAL DISCUSSION, HELP OR QUESTIONS, please see the options at https://ipfs.io/help or head directly to https://discuss.ipfs.io.
|
||||
|
||||
(you can delete this section after reading)
|
||||
-->
|
||||
68
.github/config.yml
vendored
Normal file
68
.github/config.yml
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
# Configuration for welcome - https://github.com/behaviorbot/welcome
|
||||
|
||||
# Configuration for new-issue-welcome - https://github.com/behaviorbot/new-issue-welcome
|
||||
# Comment to be posted to on first time issues
|
||||
newIssueWelcomeComment: >
|
||||
Thank you for submitting your first issue to this repository! A maintainer
|
||||
will be here shortly to triage and review.
|
||||
|
||||
In the meantime, please double-check that you have provided all the
|
||||
necessary information to make this process easy! Any information that can
|
||||
help save additional round trips is useful! We currently aim to give
|
||||
initial feedback within **two business days**. If this does not happen, feel
|
||||
free to leave a comment.
|
||||
|
||||
Please keep an eye on how this issue will be labeled, as labels give an
|
||||
overview of priorities, assignments and additional actions requested by the
|
||||
maintainers:
|
||||
|
||||
- "Priority" labels will show how urgent this is for the team.
|
||||
- "Status" labels will show if this is ready to be worked on, blocked, or in progress.
|
||||
- "Need" labels will indicate if additional input or analysis is required.
|
||||
|
||||
Finally, remember to use https://discuss.ipfs.io if you just need general
|
||||
support.
|
||||
|
||||
# Configuration for new-pr-welcome - https://github.com/behaviorbot/new-pr-welcome
|
||||
# Comment to be posted to on PRs from first time contributors in your repository
|
||||
newPRWelcomeComment: >
|
||||
Thank you for submitting this PR!
|
||||
|
||||
A maintainer will be here shortly to review it.
|
||||
|
||||
We are super grateful, but we are also overloaded! Help us by making sure
|
||||
that:
|
||||
|
||||
* The context for this PR is clear, with relevant discussion, decisions
|
||||
and stakeholders linked/mentioned.
|
||||
|
||||
* Your contribution itself is clear (code comments, self-review for the
|
||||
rest) and in its best form. Follow the [code contribution
|
||||
guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md#code-contribution-guidelines)
|
||||
if they apply.
|
||||
|
||||
Getting other community members to do a review would be great help too on
|
||||
complex PRs (you can ask in the chats/forums). If you are unsure about
|
||||
something, just leave us a comment.
|
||||
|
||||
Next steps:
|
||||
|
||||
* A maintainer will triage and assign priority to this PR, commenting on
|
||||
any missing things and potentially assigning a reviewer for high
|
||||
priority items.
|
||||
|
||||
* The PR gets reviews, discussed and approvals as needed.
|
||||
|
||||
* The PR is merged by maintainers when it has been approved and comments addressed.
|
||||
|
||||
We currently aim to provide initial feedback/triaging within **two business
|
||||
days**. Please keep an eye on any labelling actions, as these will indicate
|
||||
priorities and status of your contribution.
|
||||
|
||||
We are very grateful for your contribution!
|
||||
|
||||
|
||||
# Configuration for first-pr-merge - https://github.com/behaviorbot/first-pr-merge
|
||||
# Comment to be posted to on pull requests merged by a first time user
|
||||
# Currently disabled
|
||||
#firstPRMergeComment: ""
|
||||
@@ -1 +0,0 @@
|
||||
0.9.0: QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7
|
||||
34
.travis.yml
34
.travis.yml
@@ -1,24 +1,30 @@
|
||||
sudo: false
|
||||
|
||||
os:
|
||||
- linux
|
||||
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 'tip'
|
||||
|
||||
- 1.11.x
|
||||
|
||||
env:
|
||||
global:
|
||||
- GOTFLAGS="-race"
|
||||
matrix:
|
||||
- BUILD_DEPTYPE=gomod
|
||||
|
||||
|
||||
# disable travis install
|
||||
install:
|
||||
- go get github.com/whyrusleeping/gx
|
||||
- go get github.com/whyrusleeping/gx-go
|
||||
- gx install --global
|
||||
- true
|
||||
|
||||
script:
|
||||
- gx test -v -race -coverprofile=coverage.txt -covermode=atomic .
|
||||
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
||||
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
|
||||
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $GOPATH/src/gx
|
||||
- $GOPATH/pkg/mod
|
||||
- $HOME/.cache/go-build
|
||||
|
||||
notifications:
|
||||
email: false
|
||||
|
||||
email: false
|
||||
|
||||
13
Makefile
13
Makefile
@@ -1,16 +1,5 @@
|
||||
all: deps
|
||||
|
||||
gx:
|
||||
go get github.com/whyrusleeping/gx
|
||||
go get github.com/whyrusleeping/gx-go
|
||||
|
||||
covertools:
|
||||
deps:
|
||||
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
|
||||
|
||||
29
README.md
29
README.md
@@ -14,6 +14,9 @@ go-cid
|
||||
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.
|
||||
|
||||
## Lead Maintainer
|
||||
|
||||
[Eric Myhre](https://github.com/warpfork)
|
||||
|
||||
## Table of Contents
|
||||
|
||||
@@ -31,43 +34,23 @@ It is used in `go-ipfs` and related packages to refer to a typed hunk of data.
|
||||
go get github.com/ipfs/go-cid
|
||||
```
|
||||
|
||||
Note that `go-cid` is packaged with Gx, so it is recommended to use Gx to install and use it (see Usage section).
|
||||
|
||||
## Usage
|
||||
|
||||
### Using Gx and Gx-go
|
||||
|
||||
This module is packaged with [Gx](https://github.com/whyrusleeping/gx). In order to use it in your own project it is recommended that you:
|
||||
|
||||
```sh
|
||||
go get -u github.com/whyrusleeping/gx
|
||||
go get -u github.com/whyrusleeping/gx-go
|
||||
cd <your-project-repository>
|
||||
gx init
|
||||
gx import github.com/ipfs/go-cid
|
||||
gx install --global
|
||||
gx-go --rewrite
|
||||
```
|
||||
|
||||
Please check [Gx](https://github.com/whyrusleeping/gx) and [Gx-go](https://github.com/whyrusleeping/gx-go) documentation for more information.
|
||||
|
||||
### Running tests
|
||||
|
||||
Before running tests, please run:
|
||||
Run tests with `go test` from the directory root
|
||||
|
||||
```sh
|
||||
make deps
|
||||
go test
|
||||
```
|
||||
|
||||
This will make sure that dependencies are rewritten to known working versions.
|
||||
|
||||
### Examples
|
||||
|
||||
#### Parsing string input from users
|
||||
|
||||
```go
|
||||
// Create a cid from a marshaled string
|
||||
c, err := cid.Decode("zdvgqEMYmNeH5fKciougvQcfzMcNjF3Z1tPouJ8C7pc3pe63k")
|
||||
c, err := cid.Decode("bafzbeigai3eoy2ccc7ybwjfz5r3rdxqrinwi4rwytly24tdbh6yk7zslrm")
|
||||
if err != nil {...}
|
||||
|
||||
fmt.Println("Got CID: ", c)
|
||||
|
||||
@@ -8,6 +8,7 @@ const (
|
||||
|
||||
DagProtobuf = 0x70
|
||||
DagCBOR = 0x71
|
||||
Libp2pKey = 0x72
|
||||
|
||||
GitRaw = 0x78
|
||||
|
||||
@@ -34,6 +35,7 @@ var Codecs = map[string]uint64{
|
||||
"raw": Raw,
|
||||
"protobuf": DagProtobuf,
|
||||
"cbor": DagCBOR,
|
||||
"libp2p-key": Libp2pKey,
|
||||
"git-raw": GitRaw,
|
||||
"eth-block": EthBlock,
|
||||
"eth-block-list": EthBlockList,
|
||||
@@ -57,6 +59,7 @@ var CodecToStr = map[uint64]string{
|
||||
Raw: "raw",
|
||||
DagProtobuf: "protobuf",
|
||||
DagCBOR: "cbor",
|
||||
Libp2pKey: "libp2p-key",
|
||||
GitRaw: "git-raw",
|
||||
EthBlock: "eth-block",
|
||||
EthBlockList: "eth-block-list",
|
||||
|
||||
@@ -38,7 +38,7 @@ func (p V0Builder) Sum(data []byte) (Cid, error) {
|
||||
if err != nil {
|
||||
return Undef, err
|
||||
}
|
||||
return NewCidV0(hash), nil
|
||||
return Cid{string(hash)}, nil
|
||||
}
|
||||
|
||||
func (p V0Builder) GetCodec() uint64 {
|
||||
|
||||
401
cid.go
401
cid.go
@@ -21,29 +21,22 @@ package cid
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
mbase "github.com/multiformats/go-multibase"
|
||||
mh "github.com/multiformats/go-multihash"
|
||||
varint "github.com/multiformats/go-varint"
|
||||
)
|
||||
|
||||
// UnsupportedVersionString just holds an error message
|
||||
const UnsupportedVersionString = "<unsupported cid version>"
|
||||
|
||||
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")
|
||||
@@ -61,98 +54,126 @@ const (
|
||||
|
||||
DagProtobuf = 0x70
|
||||
DagCBOR = 0x71
|
||||
Libp2pKey = 0x72
|
||||
|
||||
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
|
||||
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
|
||||
FilCommitmentUnsealed = 0xf101
|
||||
FilCommitmentSealed = 0xf102
|
||||
)
|
||||
|
||||
// Codecs maps the name of a codec to its type
|
||||
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,
|
||||
"v0": DagProtobuf,
|
||||
"raw": Raw,
|
||||
"protobuf": DagProtobuf,
|
||||
"cbor": DagCBOR,
|
||||
"libp2p-key": Libp2pKey,
|
||||
"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,
|
||||
"fil-commitment-unsealed": FilCommitmentUnsealed,
|
||||
"fil-commitment-sealed": FilCommitmentSealed,
|
||||
}
|
||||
|
||||
// CodecToStr maps the numeric codec to its name
|
||||
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",
|
||||
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",
|
||||
FilCommitmentUnsealed: "fil-commitment-unsealed",
|
||||
FilCommitmentSealed: "fil-commitment-sealed",
|
||||
}
|
||||
|
||||
// tryNewCidV0 tries to convert a multihash into a CIDv0 CID and returns an
|
||||
// error on failure.
|
||||
func tryNewCidV0(mhash mh.Multihash) (Cid, error) {
|
||||
// Need to make sure hash is valid for CidV0 otherwise we will
|
||||
// incorrectly detect it as CidV1 in the Version() method
|
||||
dec, err := mh.Decode(mhash)
|
||||
if err != nil {
|
||||
return Undef, err
|
||||
}
|
||||
if dec.Code != mh.SHA2_256 || dec.Length != 32 {
|
||||
return Undef, fmt.Errorf("invalid hash for cidv0 %d-%d", dec.Code, dec.Length)
|
||||
}
|
||||
return Cid{string(mhash)}, nil
|
||||
}
|
||||
|
||||
// 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.
|
||||
//
|
||||
// Panics if the multihash isn't sha2-256.
|
||||
func NewCidV0(mhash mh.Multihash) Cid {
|
||||
// Need to make sure hash is valid for CidV0 otherwise we will
|
||||
// incorrectly detect it as CidV1 in the Version() method
|
||||
dec, err := mh.Decode(mhash)
|
||||
c, err := tryNewCidV0(mhash)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if dec.Code != mh.SHA2_256 || dec.Length != 32 {
|
||||
panic("invalid hash for cidv0")
|
||||
}
|
||||
return Cid{string(mhash)}
|
||||
return c
|
||||
}
|
||||
|
||||
// NewCidV1 returns a new Cid using the given multicodec-packed
|
||||
// content type.
|
||||
//
|
||||
// Panics if the multihash is invalid.
|
||||
func NewCidV1(codecType uint64, mhash mh.Multihash) Cid {
|
||||
hashlen := len(mhash)
|
||||
// two 8 bytes (max) numbers plus hash
|
||||
buf := make([]byte, 2*binary.MaxVarintLen64+hashlen)
|
||||
n := binary.PutUvarint(buf, 1)
|
||||
n += binary.PutUvarint(buf[n:], codecType)
|
||||
buf := make([]byte, 1+varint.UvarintSize(codecType)+hashlen)
|
||||
n := varint.PutUvarint(buf, 1)
|
||||
n += varint.PutUvarint(buf[n:], codecType)
|
||||
cn := copy(buf[n:], mhash)
|
||||
if cn != hashlen {
|
||||
panic("copy hash length is inconsistent")
|
||||
@@ -161,7 +182,12 @@ func NewCidV1(codecType uint64, mhash mh.Multihash) Cid {
|
||||
return Cid{string(buf[:n+hashlen])}
|
||||
}
|
||||
|
||||
// Cid represents a self-describing content adressed
|
||||
var _ encoding.BinaryMarshaler = Cid{}
|
||||
var _ encoding.BinaryUnmarshaler = (*Cid)(nil)
|
||||
var _ encoding.TextMarshaler = Cid{}
|
||||
var _ encoding.TextUnmarshaler = (*Cid)(nil)
|
||||
|
||||
// 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.
|
||||
type Cid struct{ str string }
|
||||
@@ -189,7 +215,7 @@ func Parse(v interface{}) (Cid, error) {
|
||||
case []byte:
|
||||
return Cast(v2)
|
||||
case mh.Multihash:
|
||||
return NewCidV0(v2), nil
|
||||
return tryNewCidV0(v2)
|
||||
case Cid:
|
||||
return v2, nil
|
||||
default:
|
||||
@@ -220,7 +246,7 @@ func Decode(v string) (Cid, error) {
|
||||
return Undef, err
|
||||
}
|
||||
|
||||
return NewCidV0(hash), nil
|
||||
return tryNewCidV0(hash)
|
||||
}
|
||||
|
||||
_, data, err := mbase.Decode(v)
|
||||
@@ -253,17 +279,6 @@ func ExtractEncoding(v string) (mbase.Encoding, error) {
|
||||
return encoding, nil
|
||||
}
|
||||
|
||||
func uvError(read int) error {
|
||||
switch {
|
||||
case read == 0:
|
||||
return ErrVarintBuffSmall
|
||||
case read < 0:
|
||||
return ErrVarintTooBig
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Cast takes a Cid data slice, parses it and returns a Cid.
|
||||
// For CidV1, the data buffer is in the form:
|
||||
//
|
||||
@@ -276,36 +291,38 @@ func uvError(read int) error {
|
||||
// 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 Cast(data []byte) (Cid, error) {
|
||||
if len(data) == 34 && data[0] == 18 && data[1] == 32 {
|
||||
h, err := mh.Cast(data)
|
||||
if err != nil {
|
||||
return Undef, err
|
||||
}
|
||||
|
||||
return NewCidV0(h), nil
|
||||
}
|
||||
|
||||
vers, n := binary.Uvarint(data)
|
||||
if err := uvError(n); err != nil {
|
||||
return Undef, err
|
||||
}
|
||||
|
||||
if vers != 1 {
|
||||
return Undef, fmt.Errorf("expected 1 as the cid version number, got: %d", vers)
|
||||
}
|
||||
|
||||
_, cn := binary.Uvarint(data[n:])
|
||||
if err := uvError(cn); err != nil {
|
||||
return Undef, err
|
||||
}
|
||||
|
||||
rest := data[n+cn:]
|
||||
h, err := mh.Cast(rest)
|
||||
nr, c, err := CidFromBytes(data)
|
||||
if err != nil {
|
||||
return Undef, err
|
||||
}
|
||||
|
||||
return Cid{string(data[0 : n+cn+len(h)])}, nil
|
||||
if nr != len(data) {
|
||||
return Undef, fmt.Errorf("trailing bytes in data buffer passed to cid Cast")
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// UnmarshalBinary is equivalent to Cast(). It implements the
|
||||
// encoding.BinaryUnmarshaler interface.
|
||||
func (c *Cid) UnmarshalBinary(data []byte) error {
|
||||
casted, err := Cast(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.str = casted.str
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalText is equivalent to Decode(). It implements the
|
||||
// encoding.TextUnmarshaler interface.
|
||||
func (c *Cid) UnmarshalText(text []byte) error {
|
||||
decodedCid, err := Decode(string(text))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.str = decodedCid.str
|
||||
return nil
|
||||
}
|
||||
|
||||
// Version returns the Cid version.
|
||||
@@ -321,20 +338,20 @@ func (c Cid) Type() uint64 {
|
||||
if c.Version() == 0 {
|
||||
return DagProtobuf
|
||||
}
|
||||
_, n := uvarint(c.str)
|
||||
codec, _ := uvarint(c.str[n:])
|
||||
_, n, _ := uvarint(c.str)
|
||||
codec, _, _ := uvarint(c.str[n:])
|
||||
return codec
|
||||
}
|
||||
|
||||
// String returns the default string representation of a
|
||||
// Cid. Currently, Base58 is used as the encoding for the
|
||||
// multibase string.
|
||||
// Cid. Currently, Base32 is used for CIDV1 as the encoding for the
|
||||
// multibase string, Base58 is used for CIDV0.
|
||||
func (c Cid) String() string {
|
||||
switch c.Version() {
|
||||
case 0:
|
||||
return c.Hash().B58String()
|
||||
case 1:
|
||||
mbstr, err := mbase.Encode(mbase.Base58BTC, c.Bytes())
|
||||
mbstr, err := mbase.Encode(mbase.Base32, c.Bytes())
|
||||
if err != nil {
|
||||
panic("should not error with hardcoded mbase: " + err.Error())
|
||||
}
|
||||
@@ -384,9 +401,9 @@ func (c Cid) Hash() mh.Multihash {
|
||||
}
|
||||
|
||||
// skip version length
|
||||
_, n1 := binary.Uvarint(bytes)
|
||||
_, n1, _ := varint.FromUvarint(bytes)
|
||||
// skip codec length
|
||||
_, n2 := binary.Uvarint(bytes[n1:])
|
||||
_, n2, _ := varint.FromUvarint(bytes[n1:])
|
||||
|
||||
return mh.Multihash(bytes[n1+n2:])
|
||||
}
|
||||
@@ -398,6 +415,42 @@ func (c Cid) Bytes() []byte {
|
||||
return []byte(c.str)
|
||||
}
|
||||
|
||||
// ByteLen returns the length of the CID in bytes.
|
||||
// It's equivalent to `len(c.Bytes())`, but works without an allocation,
|
||||
// and should therefore be preferred.
|
||||
//
|
||||
// (See also the WriteTo method for other important operations that work without allocation.)
|
||||
func (c Cid) ByteLen() int {
|
||||
return len(c.str)
|
||||
}
|
||||
|
||||
// WriteBytes writes the CID bytes to the given writer.
|
||||
// This method works without incurring any allocation.
|
||||
//
|
||||
// (See also the ByteLen method for other important operations that work without allocation.)
|
||||
func (c Cid) WriteBytes(w io.Writer) (int, error) {
|
||||
n, err := io.WriteString(w, c.str)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
if n != len(c.str) {
|
||||
return n, fmt.Errorf("failed to write entire cid string")
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// MarshalBinary is equivalent to Bytes(). It implements the
|
||||
// encoding.BinaryMarshaler interface.
|
||||
func (c Cid) MarshalBinary() ([]byte, error) {
|
||||
return c.Bytes(), nil
|
||||
}
|
||||
|
||||
// MarshalText is equivalent to String(). It implements the
|
||||
// encoding.TextMarshaler interface.
|
||||
func (c Cid) MarshalText() ([]byte, error) {
|
||||
return []byte(c.String()), nil
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -465,12 +518,29 @@ func (c Cid) Loggable() map[string]interface{} {
|
||||
|
||||
// Prefix builds and returns a Prefix out of a Cid.
|
||||
func (c Cid) Prefix() Prefix {
|
||||
dec, _ := mh.Decode(c.Hash()) // assuming we got a valid multiaddr, this will not error
|
||||
if c.Version() == 0 {
|
||||
return Prefix{
|
||||
MhType: mh.SHA2_256,
|
||||
MhLength: 32,
|
||||
Version: 0,
|
||||
Codec: DagProtobuf,
|
||||
}
|
||||
}
|
||||
|
||||
offset := 0
|
||||
version, n, _ := uvarint(c.str[offset:])
|
||||
offset += n
|
||||
codec, n, _ := uvarint(c.str[offset:])
|
||||
offset += n
|
||||
mhtype, n, _ := uvarint(c.str[offset:])
|
||||
offset += n
|
||||
mhlen, _, _ := uvarint(c.str[offset:])
|
||||
|
||||
return Prefix{
|
||||
MhType: dec.Code,
|
||||
MhLength: dec.Length,
|
||||
Version: c.Version(),
|
||||
Codec: c.Type(),
|
||||
MhType: mhtype,
|
||||
MhLength: int(mhlen),
|
||||
Version: version,
|
||||
Codec: codec,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -490,7 +560,18 @@ type Prefix struct {
|
||||
// Sum uses the information in a prefix to perform a multihash.Sum()
|
||||
// and return a newly constructed Cid with the resulting multihash.
|
||||
func (p Prefix) Sum(data []byte) (Cid, error) {
|
||||
hash, err := mh.Sum(data, p.MhType, p.MhLength)
|
||||
length := p.MhLength
|
||||
if p.MhType == mh.ID {
|
||||
length = -1
|
||||
}
|
||||
|
||||
if p.Version == 0 && (p.MhType != mh.SHA2_256 ||
|
||||
(p.MhLength != 32 && p.MhLength != -1)) {
|
||||
|
||||
return Undef, fmt.Errorf("invalid v0 prefix")
|
||||
}
|
||||
|
||||
hash, err := mh.Sum(data, p.MhType, length)
|
||||
if err != nil {
|
||||
return Undef, err
|
||||
}
|
||||
@@ -509,34 +590,42 @@ func (p Prefix) Sum(data []byte) (Cid, error) {
|
||||
//
|
||||
// <version><codec><mh-type><mh-length>
|
||||
func (p Prefix) Bytes() []byte {
|
||||
buf := make([]byte, 4*binary.MaxVarintLen64)
|
||||
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]
|
||||
size := varint.UvarintSize(p.Version)
|
||||
size += varint.UvarintSize(p.Codec)
|
||||
size += varint.UvarintSize(p.MhType)
|
||||
size += varint.UvarintSize(uint64(p.MhLength))
|
||||
|
||||
buf := make([]byte, size)
|
||||
n := varint.PutUvarint(buf, p.Version)
|
||||
n += varint.PutUvarint(buf[n:], p.Codec)
|
||||
n += varint.PutUvarint(buf[n:], p.MhType)
|
||||
n += varint.PutUvarint(buf[n:], uint64(p.MhLength))
|
||||
if n != size {
|
||||
panic("size mismatch")
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
// PrefixFromBytes parses a Prefix-byte representation onto a
|
||||
// Prefix.
|
||||
func PrefixFromBytes(buf []byte) (Prefix, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
vers, err := binary.ReadUvarint(r)
|
||||
vers, err := varint.ReadUvarint(r)
|
||||
if err != nil {
|
||||
return Prefix{}, err
|
||||
}
|
||||
|
||||
codec, err := binary.ReadUvarint(r)
|
||||
codec, err := varint.ReadUvarint(r)
|
||||
if err != nil {
|
||||
return Prefix{}, err
|
||||
}
|
||||
|
||||
mhtype, err := binary.ReadUvarint(r)
|
||||
mhtype, err := varint.ReadUvarint(r)
|
||||
if err != nil {
|
||||
return Prefix{}, err
|
||||
}
|
||||
|
||||
mhlen, err := binary.ReadUvarint(r)
|
||||
mhlen, err := varint.ReadUvarint(r)
|
||||
if err != nil {
|
||||
return Prefix{}, err
|
||||
}
|
||||
@@ -548,3 +637,41 @@ func PrefixFromBytes(buf []byte) (Prefix, error) {
|
||||
MhLength: int(mhlen),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func CidFromBytes(data []byte) (int, Cid, error) {
|
||||
if len(data) > 2 && data[0] == mh.SHA2_256 && data[1] == 32 {
|
||||
if len(data) < 34 {
|
||||
return 0, Undef, fmt.Errorf("not enough bytes for cid v0")
|
||||
}
|
||||
|
||||
h, err := mh.Cast(data[:34])
|
||||
if err != nil {
|
||||
return 0, Undef, err
|
||||
}
|
||||
|
||||
return 34, Cid{string(h)}, nil
|
||||
}
|
||||
|
||||
vers, n, err := varint.FromUvarint(data)
|
||||
if err != nil {
|
||||
return 0, Undef, err
|
||||
}
|
||||
|
||||
if vers != 1 {
|
||||
return 0, Undef, fmt.Errorf("expected 1 as the cid version number, got: %d", vers)
|
||||
}
|
||||
|
||||
_, cn, err := varint.FromUvarint(data[n:])
|
||||
if err != nil {
|
||||
return 0, Undef, err
|
||||
}
|
||||
|
||||
mhnr, _, err := mh.MHFromBytes(data[n+cn:])
|
||||
if err != nil {
|
||||
return 0, Undef, err
|
||||
}
|
||||
|
||||
l := n + cn + mhnr
|
||||
|
||||
return l, Cid{string(data[0:l])}, nil
|
||||
}
|
||||
|
||||
200
cid_test.go
200
cid_test.go
@@ -16,25 +16,30 @@ import (
|
||||
// https://github.com/multiformats/go-multihash/blob/7aa9f26a231c6f34f4e9fad52bf580fd36627285/multihash_test.go#L13
|
||||
// Makes it so changing the table accidentally has to happen twice.
|
||||
var tCodecs = 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",
|
||||
Raw: "raw",
|
||||
DagProtobuf: "protobuf",
|
||||
DagCBOR: "cbor",
|
||||
Libp2pKey: "libp2p-key",
|
||||
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",
|
||||
FilCommitmentUnsealed: "fil-commitment-unsealed",
|
||||
FilCommitmentSealed: "fil-commitment-sealed",
|
||||
}
|
||||
|
||||
func assertEqual(t *testing.T, a, b Cid) {
|
||||
@@ -71,6 +76,34 @@ func TestTableForV0(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefixSum(t *testing.T) {
|
||||
// Test creating CIDs both manually and with Prefix.
|
||||
// Tests: https://github.com/ipfs/go-cid/issues/83
|
||||
for _, hashfun := range []uint64{
|
||||
mh.ID, mh.SHA3, mh.SHA2_256,
|
||||
} {
|
||||
h1, err := mh.Sum([]byte("TEST"), hashfun, -1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c1 := NewCidV1(Raw, h1)
|
||||
|
||||
h2, err := mh.Sum([]byte("foobar"), hashfun, -1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c2 := NewCidV1(Raw, h2)
|
||||
|
||||
c3, err := c1.Prefix().Sum([]byte("foobar"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !c2.Equals(c3) {
|
||||
t.Fatal("expected CIDs to be equal")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBasicMarshaling(t *testing.T) {
|
||||
h, err := mh.Sum([]byte("TEST"), mh.SHA3, 4)
|
||||
if err != nil {
|
||||
@@ -156,6 +189,44 @@ func TestBasesMarshaling(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinaryMarshaling(t *testing.T) {
|
||||
data := []byte("this is some test content")
|
||||
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
||||
c := NewCidV1(DagCBOR, hash)
|
||||
var c2 Cid
|
||||
|
||||
data, err := c.MarshalBinary()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = c2.UnmarshalBinary(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !c.Equals(c2) {
|
||||
t.Errorf("cids should be the same: %s %s", c, c2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTextMarshaling(t *testing.T) {
|
||||
data := []byte("this is some test content")
|
||||
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
|
||||
c := NewCidV1(DagCBOR, hash)
|
||||
var c2 Cid
|
||||
|
||||
data, err := c.MarshalText()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = c2.UnmarshalText(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !c.Equals(c2) {
|
||||
t.Errorf("cids should be the same: %s %s", c, c2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyString(t *testing.T) {
|
||||
_, err := Decode("")
|
||||
if err == nil {
|
||||
@@ -264,6 +335,41 @@ func TestNewPrefixV0(t *testing.T) {
|
||||
if c1.Prefix() != c2.Prefix() {
|
||||
t.Fatal("prefixes mismatch")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestInvalidV0Prefix(t *testing.T) {
|
||||
tests := []Prefix{
|
||||
{
|
||||
MhType: mh.SHA2_256,
|
||||
MhLength: 31,
|
||||
},
|
||||
{
|
||||
MhType: mh.SHA2_256,
|
||||
MhLength: 33,
|
||||
},
|
||||
{
|
||||
MhType: mh.SHA2_256,
|
||||
MhLength: -2,
|
||||
},
|
||||
{
|
||||
MhType: mh.SHA2_512,
|
||||
MhLength: 32,
|
||||
},
|
||||
{
|
||||
MhType: mh.SHA2_512,
|
||||
MhLength: -1,
|
||||
},
|
||||
}
|
||||
|
||||
for i, p := range tests {
|
||||
t.Log(i)
|
||||
_, err := p.Sum([]byte("testdata"))
|
||||
if err == nil {
|
||||
t.Fatalf("should error (index %d)", i)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPrefixRoundtrip(t *testing.T) {
|
||||
@@ -370,13 +476,13 @@ func TestHexDecode(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if c.String() != "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG" {
|
||||
if c.String() != "bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm" {
|
||||
t.Fatal("hash value failed to round trip decoding from hex")
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleDecode() {
|
||||
encoded := "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG"
|
||||
encoded := "bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm"
|
||||
c, err := Decode(encoded)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %s", err)
|
||||
@@ -384,11 +490,11 @@ func ExampleDecode() {
|
||||
}
|
||||
|
||||
fmt.Println(c)
|
||||
// Output: zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG
|
||||
// Output: bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm
|
||||
}
|
||||
|
||||
func TestFromJson(t *testing.T) {
|
||||
cval := "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG"
|
||||
cval := "bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm"
|
||||
jsoncid := []byte(`{"/":"` + cval + `"}`)
|
||||
var c Cid
|
||||
err := json.Unmarshal(jsoncid, &c)
|
||||
@@ -402,7 +508,7 @@ func TestFromJson(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestJsonRoundTrip(t *testing.T) {
|
||||
exp, err := Decode("zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG")
|
||||
exp, err := Decode("bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -446,3 +552,49 @@ func BenchmarkStringV1(b *testing.B) {
|
||||
b.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadCidsFromBuffer(t *testing.T) {
|
||||
cidstr := []string{
|
||||
"bafkreie5qrjvaw64n4tjm6hbnm7fnqvcssfed4whsjqxzslbd3jwhsk3mm",
|
||||
"k2cwueckqkibutvhkr4p2ln2pjcaxaakpd9db0e7j7ax1lxhhxy3ekpv",
|
||||
"Qmf5Qzp6nGBku7CEn2UQx4mgN8TW69YUok36DrGa6NN893",
|
||||
"zb2rhZi1JR4eNc2jBGaRYJKYM8JEB4ovenym8L1CmFsRAytkz",
|
||||
}
|
||||
|
||||
var cids []Cid
|
||||
var buf []byte
|
||||
for _, cs := range cidstr {
|
||||
c, err := Decode(cs)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cids = append(cids, c)
|
||||
buf = append(buf, c.Bytes()...)
|
||||
}
|
||||
|
||||
var cur int
|
||||
for _, expc := range cids {
|
||||
n, c, err := CidFromBytes(buf[cur:])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c != expc {
|
||||
t.Fatal("cids mismatched")
|
||||
}
|
||||
cur += n
|
||||
}
|
||||
if cur != len(buf) {
|
||||
t.Fatal("had trailing bytes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadParse(t *testing.T) {
|
||||
hash, err := mh.Sum([]byte("foobar"), mh.SHA3_256, -1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = Parse(hash)
|
||||
if err == nil {
|
||||
t.Fatal("expected to fail to parse an invalid CIDv1 CID")
|
||||
}
|
||||
}
|
||||
|
||||
9
go.mod
Normal file
9
go.mod
Normal file
@@ -0,0 +1,9 @@
|
||||
module github.com/ipfs/go-cid
|
||||
|
||||
require (
|
||||
github.com/multiformats/go-multibase v0.0.3
|
||||
github.com/multiformats/go-multihash v0.0.13
|
||||
github.com/multiformats/go-varint v0.0.5
|
||||
)
|
||||
|
||||
go 1.13
|
||||
28
go.sum
Normal file
28
go.sum
Normal file
@@ -0,0 +1,28 @@
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
|
||||
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 h1:MHkK1uRtFbVqvAgvWxafZe54+5uBxLluGylDiKgdhwo=
|
||||
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
|
||||
github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ=
|
||||
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
||||
github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc=
|
||||
github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
|
||||
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
|
||||
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
|
||||
github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4=
|
||||
github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM=
|
||||
github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk=
|
||||
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
|
||||
github.com/multiformats/go-multihash v0.0.13 h1:06x+mk/zj1FoMsgNejLpy6QTvJqlSt/BhLEy87zidlc=
|
||||
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
|
||||
github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg=
|
||||
github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
|
||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU=
|
||||
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
30
package.json
30
package.json
@@ -1,30 +0,0 @@
|
||||
{
|
||||
"author": "whyrusleeping",
|
||||
"bugs": {
|
||||
"url": "https://github.com/ipfs/go-cid"
|
||||
},
|
||||
"gx": {
|
||||
"dvcsimport": "github.com/ipfs/go-cid"
|
||||
},
|
||||
"gxDependencies": [
|
||||
{
|
||||
"author": "whyrusleeping",
|
||||
"hash": "QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8",
|
||||
"name": "go-multihash",
|
||||
"version": "1.0.8"
|
||||
},
|
||||
{
|
||||
"author": "whyrusleeping",
|
||||
"hash": "QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd",
|
||||
"name": "go-multibase",
|
||||
"version": "0.3.0"
|
||||
}
|
||||
],
|
||||
"gxVersion": "0.8.0",
|
||||
"language": "go",
|
||||
"license": "MIT",
|
||||
"name": "go-cid",
|
||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
||||
"version": "0.9.0"
|
||||
}
|
||||
|
||||
14
varint.go
14
varint.go
@@ -1,5 +1,9 @@
|
||||
package cid
|
||||
|
||||
import (
|
||||
"github.com/multiformats/go-varint"
|
||||
)
|
||||
|
||||
// Version of varint function that work with a string rather than
|
||||
// []byte to avoid unnecessary allocation
|
||||
|
||||
@@ -15,7 +19,7 @@ package cid
|
||||
// n < 0: value larger than 64 bits (overflow)
|
||||
// and -n is the number of bytes read
|
||||
//
|
||||
func uvarint(buf string) (uint64, int) {
|
||||
func uvarint(buf string) (uint64, int, error) {
|
||||
var x uint64
|
||||
var s uint
|
||||
// we have a binary string so we can't use a range loope
|
||||
@@ -23,12 +27,14 @@ func uvarint(buf string) (uint64, int) {
|
||||
b := buf[i]
|
||||
if b < 0x80 {
|
||||
if i > 9 || i == 9 && b > 1 {
|
||||
return 0, -(i + 1) // overflow
|
||||
return 0, 0, varint.ErrOverflow
|
||||
} else if b == 0 && i > 0 {
|
||||
return 0, 0, varint.ErrNotMinimal
|
||||
}
|
||||
return x | uint64(b)<<s, i + 1
|
||||
return x | uint64(b)<<s, i + 1, nil
|
||||
}
|
||||
x |= uint64(b&0x7f) << s
|
||||
s += 7
|
||||
}
|
||||
return 0, 0
|
||||
return 0, 0, varint.ErrUnderflow
|
||||
}
|
||||
|
||||
@@ -1,17 +1,25 @@
|
||||
package cid
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"testing"
|
||||
|
||||
"github.com/multiformats/go-varint"
|
||||
)
|
||||
|
||||
func TestUvarintRoundTrip(t *testing.T) {
|
||||
testCases := []uint64{0, 1, 2, 127, 128, 129, 255, 256, 257, 1<<63 - 1}
|
||||
for _, tc := range testCases {
|
||||
t.Log("testing", tc)
|
||||
buf := make([]byte, 16)
|
||||
binary.PutUvarint(buf, tc)
|
||||
v, l1 := uvarint(string(buf))
|
||||
_, l2 := binary.Uvarint(buf)
|
||||
varint.PutUvarint(buf, tc)
|
||||
v, l1, err := uvarint(string(buf))
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %s", buf, err)
|
||||
}
|
||||
_, l2, err := varint.FromUvarint(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tc != v {
|
||||
t.Errorf("roundtrip failed expected %d but got %d", tc, v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user