Skip to content

Commit

Permalink
Allow multihashes with DBL_SHA2_256 code (#197)
Browse files Browse the repository at this point in the history
The double hash multicodec code is not dedicated to encrypted records. Therefore we cannot implicitly assume that it represents one.
  • Loading branch information
gammazero authored Mar 29, 2024
1 parent c72e65b commit 7bf79d7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/gammazero/radixtree v0.3.1
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-log/v2 v2.5.1
github.com/ipni/go-libipni v0.5.15
github.com/ipni/go-libipni v0.5.16
github.com/libp2p/go-libp2p v0.33.2
github.com/mr-tron/base58 v1.2.0
github.com/multiformats/go-multihash v0.2.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ github.com/ipfs/go-log/v2 v2.5.1 h1:1XdUzF7048prq4aBjDQQ4SL5RxftpRGdXhNRwKSAlcY=
github.com/ipfs/go-log/v2 v2.5.1/go.mod h1:prSpmC1Gpllc9UYWxDiZDreBYw7zp4Iqp1kOLU9U5UI=
github.com/ipld/go-ipld-prime v0.21.0 h1:n4JmcpOlPDIxBcY037SVfpd1G+Sj1nKZah0m6QH9C2E=
github.com/ipld/go-ipld-prime v0.21.0/go.mod h1:3RLqy//ERg/y5oShXXdx5YIp50cFGOanyMctpPjsvxQ=
github.com/ipni/go-libipni v0.5.15 h1:fpiCapQTmop5vD61aZlyHwKxDTmi9cHh4cTYFtPYDeg=
github.com/ipni/go-libipni v0.5.15/go.mod h1:SiibaNEFWlEnuNewo/hMVI/517b9a6p7C6qpigG8UT8=
github.com/ipni/go-libipni v0.5.16 h1:icM/MLlXHVnAilyT5N6b0jYveWkimir55OZFIFehDXg=
github.com/ipni/go-libipni v0.5.16/go.mod h1:BMbHRRcCvvIBI311bnE4XifksV2iA8QOCfST4u+8lbs=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI=
Expand Down
29 changes: 9 additions & 20 deletions store/dhstore/dhstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ func (s *dhStore) Put(value indexer.Value, mhs ...multihash.Multihash) error {
start := time.Now()

ctx := context.Background()
err = s.sendDHMetadata(ctx, metaReq)
if err != nil {
if err = s.sendDHMetadata(ctx, metaReq); err != nil {
return err
}

Expand All @@ -130,23 +129,19 @@ func (s *dhStore) Put(value indexer.Value, mhs ...multihash.Multihash) error {
}
merges := make([]client.Index, 0, size)
for _, mh := range mhs {
dm, err := multihash.Decode(mh)
if err != nil {
return err
if _, err = multihash.Decode(mh); err != nil {
log.Errorw("Failed to decode multihash, skipping", "err", err)
continue
}
if dm.Code == multihash.DBL_SHA2_256 {
return errors.New("put double-hashed index not supported")
}

merge, err := makeDHMerge(mh, valueKey)
if err != nil {
return err
log.Errorw("Failed to create dhmerge for multihash, skipping", "err", err)
continue
}

merges = append(merges, merge)
if len(merges) == cap(merges) {
err = s.sendDHMergeIndexRequest(ctx, merges)
if err != nil {
if err = s.sendDHMergeIndexRequest(ctx, merges); err != nil {
return err
}
merges = merges[:0]
Expand All @@ -155,8 +150,7 @@ func (s *dhStore) Put(value indexer.Value, mhs ...multihash.Multihash) error {

// Send remaining merge requests.
if len(merges) != 0 {
err = s.sendDHMergeIndexRequest(ctx, merges)
if err != nil {
if err = s.sendDHMergeIndexRequest(ctx, merges); err != nil {
return err
}
}
Expand Down Expand Up @@ -297,19 +291,14 @@ func (s *dhStore) Stats() (*indexer.Stats, error) {
}

func makeDHMerge(mh multihash.Multihash, valueKey []byte) (client.Index, error) {
mh2, err := dhash.SecondMultihash(mh)
if err != nil {
return client.Index{}, err
}

// Encrypt value key with original multihash.
encValueKey, err := dhash.EncryptValueKey(valueKey, mh)
if err != nil {
return client.Index{}, err
}

return client.Index{
Key: mh2,
Key: dhash.SecondMultihash(mh),
Value: encValueKey,
}, nil
}
Expand Down

0 comments on commit 7bf79d7

Please sign in to comment.