Commit f8a265e7 authored by Kevin Atkinson's avatar Kevin Atkinson

adder: add support for using CidV1

License: MIT
Signed-off-by: default avatarKevin Atkinson <k@kevina.org>
parent 90386a37
...@@ -86,7 +86,7 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) { ...@@ -86,7 +86,7 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
if n.cached == nil { if n.cached == nil {
if n.Prefix.Codec == 0 { // unset if n.Prefix.Codec == 0 { // unset
n.Prefix = defaultCidPrefix n.Prefix = v0CidPrefix
} }
c, err := n.Prefix.Sum(n.encoded) c, err := n.Prefix.Sum(n.encoded)
if err != nil { if err != nil {
......
...@@ -28,13 +28,37 @@ type ProtoNode struct { ...@@ -28,13 +28,37 @@ type ProtoNode struct {
Prefix cid.Prefix Prefix cid.Prefix
} }
var defaultCidPrefix = cid.Prefix{ var v0CidPrefix = cid.Prefix{
Codec: cid.DagProtobuf, Codec: cid.DagProtobuf,
MhLength: -1, MhLength: -1,
MhType: mh.SHA2_256, MhType: mh.SHA2_256,
Version: 0, Version: 0,
} }
var v1CidPrefix = cid.Prefix{
Codec: cid.DagProtobuf,
MhLength: -1,
MhType: mh.SHA2_256,
Version: 1,
}
func PrefixForCidVersion(version int) (cid.Prefix, error) {
switch version {
case 0:
return v0CidPrefix, nil
case 1:
return v1CidPrefix, nil
default:
return cid.Prefix{}, fmt.Errorf("unknown CID version: %d", version)
}
}
func (n *ProtoNode) SetPrefix(prefix cid.Prefix) {
n.Prefix = prefix
n.encoded = nil
n.cached = nil
}
type LinkSlice []*node.Link type LinkSlice []*node.Link
func (ls LinkSlice) Len() int { return len(ls) } func (ls LinkSlice) Len() int { return len(ls) }
...@@ -158,6 +182,9 @@ func (n *ProtoNode) Copy() node.Node { ...@@ -158,6 +182,9 @@ func (n *ProtoNode) Copy() node.Node {
nnode.links = make([]*node.Link, len(n.links)) nnode.links = make([]*node.Link, len(n.links))
copy(nnode.links, n.links) copy(nnode.links, n.links)
} }
nnode.Prefix = n.Prefix
return nnode return nnode
} }
...@@ -260,7 +287,7 @@ func (n *ProtoNode) Cid() *cid.Cid { ...@@ -260,7 +287,7 @@ func (n *ProtoNode) Cid() *cid.Cid {
} }
if n.Prefix.Codec == 0 { if n.Prefix.Codec == 0 {
n.Prefix = defaultCidPrefix n.Prefix = v0CidPrefix
} }
c, err := n.Prefix.Sum(n.RawData()) c, err := n.Prefix.Sum(n.RawData())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment