/
dagpb.go
66 lines (50 loc) · 1.22 KB
/
dagpb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package coredag
import (
"io"
"io/ioutil"
"math"
"github.com/ipfs/go-ipfs/merkledag"
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
node "gx/ipfs/QmPN7cwmpcc4DWXb4KTB9dNAJgjuPY69h3npsMfhRrQL9c/go-ipld-format"
mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
)
func dagpbJSONParser(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
nd := &merkledag.ProtoNode{}
err = nd.UnmarshalJSON(data)
if err != nil {
return nil, err
}
nd.SetPrefix(cidPrefix(mhType, mhLen))
return []node.Node{nd}, nil
}
func dagpbRawParser(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
nd, err := merkledag.DecodeProtobuf(data)
if err != nil {
return nil, err
}
nd.SetPrefix(cidPrefix(mhType, mhLen))
return []node.Node{nd}, nil
}
func cidPrefix(mhType uint64, mhLen int) *cid.Prefix {
if mhType == math.MaxUint64 {
mhType = mh.SHA2_256
}
prefix := &cid.Prefix{
MhType: mhType,
MhLength: mhLen,
Version: 1,
Codec: cid.DagProtobuf,
}
if mhType == mh.SHA2_256 {
prefix.Version = 0
}
return prefix
}