Skip to content

Commit

Permalink
Fix dhash client bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ischasny committed Jan 23, 2023
1 parent d68e510 commit f18392b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
21 changes: 17 additions & 4 deletions api/v0/finder/client/http/dhash_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"

"github.com/ipni/dhstore"
"github.com/ipni/go-indexer-core/store/dhash"
"github.com/ipni/storetheindex/api/v0/finder/model"
"github.com/ipni/storetheindex/api/v0/httpclient"
Expand All @@ -25,6 +26,8 @@ type DHashClient struct {
metadataUrl string
}

// NewDHashClient instantiates a new client that uses Reader Privacy API for querying data.
// It requires more roundtrips to fullfill one query however it also protects the user from a passive observer.
func NewDHashClient(baseURL string, options ...httpclient.Option) (*DHashClient, error) {
c, err := New(baseURL, options...)
if err != nil {
Expand Down Expand Up @@ -72,7 +75,10 @@ func (c *DHashClient) Find(ctx context.Context, mh multihash.Multihash) (*model.
return nil, err
}

c.decryptFindResponse(ctx, findResponse, map[string]multihash.Multihash{smh.B58String(): mh})
err = c.decryptFindResponse(ctx, findResponse, map[string]multihash.Multihash{smh.B58String(): mh})
if err != nil {
return nil, err
}

return findResponse, nil
}
Expand Down Expand Up @@ -109,15 +115,15 @@ func (c *DHashClient) decryptFindResponse(ctx context.Context, resp *model.FindR
if pinfo == nil {
pinfo, err = c.GetProvider(ctx, pid)
if err != nil {
continue
return err
}
pinfoCache[pid] = pinfo
}

// fetch metadata
metadata, err := c.fetchMetadata(ctx, vk)
if err != nil {
continue
return err
}

mhr.ProviderResults = append(mhr.ProviderResults, model.ProviderResult{
Expand Down Expand Up @@ -157,5 +163,12 @@ func (c *DHashClient) fetchMetadata(ctx context.Context, vk []byte) ([]byte, err
return nil, httpclient.ReadError(resp.StatusCode, body)
}

return dhash.DecryptMetadata(body, vk)
findResponse := &dhstore.GetMetadataResponse{}
err = json.Unmarshal(body, findResponse)

if err != nil {
return nil, err
}

return dhash.DecryptMetadata(findResponse.EncryptedMetadata, vk)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/ipld/go-ipld-prime v0.19.0
github.com/ipld/go-ipld-prime/storage/dsadapter v0.0.0-20230102063945-1a409dc236dd
github.com/ipld/go-storethehash v0.3.13
github.com/ipni/go-indexer-core v0.7.1-0.20230120184340-38cba41d97e3
github.com/ipni/go-indexer-core v0.7.1-0.20230122165842-8feed957168a
github.com/libp2p/go-libp2p v0.23.4
github.com/libp2p/go-libp2p-gostream v0.5.0
github.com/libp2p/go-libp2p-pubsub v0.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@ github.com/ipld/go-storethehash v0.3.13 h1:1T6kX5K57lAgxbsGitZEaZMAR3RdWch2kO8ok
github.com/ipld/go-storethehash v0.3.13/go.mod h1:KCYpzmamubnSwm7fvWcCkm0aIwQh4WRNtzrKK4pVhAQ=
github.com/ipni/dhstore v0.0.2-0.20230120184057-c54e9d7c72f7 h1:w4sIScjReSAME4FKuSPaUT0H6rUi4UxEy42lEi5QKJY=
github.com/ipni/dhstore v0.0.2-0.20230120184057-c54e9d7c72f7/go.mod h1:CIz5tpqY9GRSTx2ZML3tmvCZzdDCudDuE3DP57R4ZYo=
github.com/ipni/go-indexer-core v0.7.1-0.20230120184340-38cba41d97e3 h1:XBqu/fzwzmOmH1U5mBm9DrP28EwlTZ8a8yyIAq/k2xc=
github.com/ipni/go-indexer-core v0.7.1-0.20230120184340-38cba41d97e3/go.mod h1:UluJCGNLKAWzxDgHX6zVD+hqpKQxEyuj+wkcAc9j4Po=
github.com/ipni/go-indexer-core v0.7.1-0.20230122165842-8feed957168a h1:Oqx3NW0kEsMYAqPVuFVfQ1Dy8d7l7hvCJ0xVp791pJI=
github.com/ipni/go-indexer-core v0.7.1-0.20230122165842-8feed957168a/go.mod h1:UluJCGNLKAWzxDgHX6zVD+hqpKQxEyuj+wkcAc9j4Po=
github.com/ipsn/go-secp256k1 v0.0.0-20180726113642-9d62b9f0bc52 h1:QG4CGBqCeuBo6aZlGAamSkxWdgWfZGeE49eUOWJPA4c=
github.com/ipsn/go-secp256k1 v0.0.0-20180726113642-9d62b9f0bc52/go.mod h1:fdg+/X9Gg4AsAIzWpEHwnqd+QY3b7lajxyjE1m4hkq4=
github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbkg6SAB4att1aAwTmVIx/5gCVqeyUdI=
Expand Down

0 comments on commit f18392b

Please sign in to comment.