Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve key lookup #7023

Merged
merged 1 commit into from
Mar 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions core/coreapi/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,16 @@ func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.Nam
}

func keylookup(self ci.PrivKey, kstore keystore.Keystore, k string) (ci.PrivKey, error) {
////////////////////
// Lookup by name //
////////////////////

// First, lookup self.
if k == "self" {
return self, nil
}

// Then, look in the keystore.
res, err := kstore.Get(k)
if res != nil {
return res, nil
Expand All @@ -157,20 +163,36 @@ func keylookup(self ci.PrivKey, kstore keystore.Keystore, k string) (ci.PrivKey,
return nil, err
}

//////////////////
// Lookup by ID //
//////////////////
targetPid, err := peer.Decode(k)
if err != nil {
return nil, keystore.ErrNoSuchKey
}

// First, check self.
pid, err := peer.IDFromPrivateKey(self)
if err != nil {
return nil, fmt.Errorf("failed to determine peer ID for private key: %w", err)
}
if pid == targetPid {
return self, nil
}

// Then, look in the keystore.
for _, key := range keys {
privKey, err := kstore.Get(key)
if err != nil {
return nil, err
}

pubKey := privKey.GetPublic()

pid, err := peer.IDFromPublicKey(pubKey)
pid, err := peer.IDFromPrivateKey(privKey)
if err != nil {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it not doing IDFromPrivateKey(privKey) directly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of lines above, could not add comment directly there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea. It may not have existed? I'll change that.

}

if pid.Pretty() == k {
if targetPid == pid {
return privKey, nil
}
}
Expand Down