Return early when finding address already exists

This commit is contained in:
Aaron Buchwald 2020-06-12 14:09:45 -04:00
parent e073b4e8ad
commit 1572b1bd97
No known key found for this signature in database
GPG Key ID: C71A37CF7072977F
1 changed files with 5 additions and 7 deletions

View File

@ -670,18 +670,16 @@ func (service *Service) ImportKey(r *http.Request, args *ImportKeyArgs, reply *I
return fmt.Errorf("problem saving key while getting existing addresses: %w", err)
}
newAddress := sk.PublicKey().Address()
exists := false
for _, address := range addresses {
if newAddress.Equals(address) {
exists = true
reply.Address = service.vm.Format(newAddress.Bytes())
return nil
}
}
if !exists {
addresses = append(addresses, newAddress)
if err := user.SetAddresses(db, addresses); err != nil {
return fmt.Errorf("problem saving addresses: %w", err)
}
addresses = append(addresses, newAddress)
if err := user.SetAddresses(db, addresses); err != nil {
return fmt.Errorf("problem saving addresses: %w", err)
}
reply.Address = service.vm.Format(newAddress.Bytes())