Commit 82bf38e8 authored by Kevin Atkinson's avatar Kevin Atkinson

Add support for using an alternative hash function with raw nodes.

License: MIT
Signed-off-by: default avatarKevin Atkinson <k@kevina.org>
parent b709fb48
......@@ -116,7 +116,7 @@ func decodeBlock(b blocks.Block) (node.Node, error) {
decnd.Prefix = b.Cid().Prefix()
return decnd, nil
case cid.Raw:
return NewRawNode(b.RawData()), nil
return NewRawNodeWPrefix(b.RawData(), b.Cid().Prefix())
case cid.DagCBOR:
return ipldcbor.Decode(b.RawData())
default:
......
......@@ -12,6 +12,8 @@ type RawNode struct {
blocks.Block
}
// NewRawNode creates a RawNode using the default sha2-256 hash
// funcition.
func NewRawNode(data []byte) *RawNode {
h := u.Hash(data)
c := cid.NewCidV1(cid.Raw, h)
......@@ -20,6 +22,24 @@ func NewRawNode(data []byte) *RawNode {
return &RawNode{blk}
}
// NewRawNodeWPrefix creates a RawNode with the hash function
// specified in prefix.
func NewRawNodeWPrefix(data []byte, prefix cid.Prefix) (*RawNode, error) {
prefix.Codec = cid.Raw
if prefix.Version == 0 {
prefix.Version = 1
}
c, err := prefix.Sum(data)
if err != nil {
return nil, err
}
blk, err := blocks.NewBlockWithCid(data, c)
if err != nil {
return nil, err
}
return &RawNode{blk}, nil
}
func (rn *RawNode) Links() []*node.Link {
return nil
}
......
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