@cryptotaxi247 / kubo / commits / b5ad3be06

feat: improve key lookup

* Support peer IDs encoded as CIDs. * Support looking up the identity key by peer ID.

Steven Allen committed Mar 23, 2020 at 11:57 UTC b5ad3be0603fd9c7803453874eab7f4565a8a4f3
1 file changed +26 -4
core/coreapi/name.go
+26 -4
@@ -139,10 +139,16 @@ func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.Nam
139 }
140
141 func keylookup(self ci.PrivKey, kstore keystore.Keystore, k string) (ci.PrivKey, error) {
142 + ////////////////////
143 + // Lookup by name //
144 + ////////////////////
145 +
146 + // First, lookup self.
147 if k == "self" {
148 return self, nil
149 }
150
151 + // Then, look in the keystore.
152 res, err := kstore.Get(k)
153 if res != nil {
154 return res, nil
@@ -157,20 +163,36 @@ func keylookup(self ci.PrivKey, kstore keystore.Keystore, k string) (ci.PrivKey,
163 return nil, err
164 }
165
166 + //////////////////
167 + // Lookup by ID //
168 + //////////////////
169 + targetPid, err := peer.Decode(k)
170 + if err != nil {
171 + return nil, keystore.ErrNoSuchKey
172 + }
173 +
174 + // First, check self.
175 + pid, err := peer.IDFromPrivateKey(self)
176 + if err != nil {
177 + return nil, fmt.Errorf("failed to determine peer ID for private key: %w", err)
178 + }
179 + if pid == targetPid {
180 + return self, nil
181 + }
182 +
183 + // Then, look in the keystore.
184 for _, key := range keys {
185 privKey, err := kstore.Get(key)
186 if err != nil {
187 return nil, err
188 }
189
166 - pubKey := privKey.GetPublic()
167 -
168 - pid, err := peer.IDFromPublicKey(pubKey)
190 + pid, err := peer.IDFromPrivateKey(privKey)
191 if err != nil {
192 return nil, err
193 }
194
173 - if pid.Pretty() == k {
195 + if targetPid == pid {
196 return privKey, nil
197 }
198 }