mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 19:24:14 +08:00
Add support for using an alternative hash function with raw nodes.
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
This commit is contained in:
@ -165,10 +165,21 @@ func (db *DagBuilderHelper) GetNextDataNode() (*UnixfsNode, error) {
|
||||
}
|
||||
|
||||
if db.rawLeaves {
|
||||
if db.prefix == nil {
|
||||
return &UnixfsNode{
|
||||
rawnode: dag.NewRawNode(data),
|
||||
raw: true,
|
||||
}, nil
|
||||
} else {
|
||||
rawnode, err := dag.NewRawNodeWPrefix(data, *db.prefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &UnixfsNode{
|
||||
rawnode: rawnode,
|
||||
raw: true,
|
||||
}, nil
|
||||
}
|
||||
} else {
|
||||
blk := db.NewUnixfsBlock()
|
||||
blk.SetData(data)
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user