diff --git a/ipld/plugin/nmt.go b/ipld/plugin/nmt.go index 8b65aa4849..c1b2a6fb66 100644 --- a/ipld/plugin/nmt.go +++ b/ipld/plugin/nmt.go @@ -53,9 +53,25 @@ func NewNamespaceHasher(hasher *nmt.Hasher) hash.Hash { } func (n *namespaceHasher) Write(data []byte) (int, error) { - n.tp = data[0] + ln, nln, hln := len(data), int(n.NamespaceLen), n.Hash.Size() + innerNodeSize, leafNodeSize := (nln*2+hln)*2, nln+consts.ShareSize + switch ln { + default: + return 0, fmt.Errorf("wrong data size") + // inner node + case innerNodeSize + 1: // with type byte + fallthrough + case innerNodeSize: + n.tp = nmt.NodePrefix + // leaf data node + case leafNodeSize: + fallthrough + case leafNodeSize + 1: // with type byte + n.tp = nmt.LeafPrefix + } + n.data = data[1:] - return len(data), nil + return ln, nil } func (n *namespaceHasher) Sum([]byte) []byte {