this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

identity: remove ParsedPublicKey caching; tweak get/parsing method names and doc-comments (#613)

The main thing here is removing the cached ("pre-parsed") key in the
identity doc. I think this was non-trivial overhead when doing lots of
lookups, and required manual override when serializing (actually
caching!) the objects. I don't think we relied on the key parse caching
for performance anywhere; if actual profiling shows we need it, should
add as a private field, or cache externally.

`PublicKeyFor` read a bit weird and non-idiomatic to my eyes so I
updated to `GetPublicKey(id)`, and added a service endpoint equivalent.
If this is too nit-picky or the method is being used a bunch elsewhere
(branches?) can revert that bit.

authored by

bnewbold and committed by
GitHub
38c8c036 acac567e

+25 -47
-10
atproto/identity/base_directory.go
··· 53 53 } 54 54 ident.Handle = declared 55 55 56 - // optimistic pre-parsing of public key 57 - pk, err := ident.PublicKey() 58 - if nil == err { 59 - ident.ParsedPublicKey = pk 60 - } 61 56 return &ident, nil 62 57 } 63 58 ··· 88 83 } 89 84 } 90 85 91 - // optimistic pre-parsing of public key 92 - pk, err := ident.PublicKey() 93 - if nil == err { 94 - ident.ParsedPublicKey = pk 95 - } 96 86 return &ident, nil 97 87 } 98 88
+22 -22
atproto/identity/identity.go
··· 55 55 // Indicates that DID resolution process failed. A wrapped error may provide more context. 56 56 var ErrDIDResolutionFailed = errors.New("DID resolution failed") 57 57 58 - var ErrKeyNotDeclared = errors.New("identity has no public repo signing key") 58 + // Indicates that DID document did not include a public key with the specified ID 59 + var ErrKeyNotDeclared = errors.New("DID document did not declare a relevant public key") 59 60 60 61 var DefaultPLCURL = "https://plc.directory" 61 62 ··· 91 92 AlsoKnownAs []string 92 93 Services map[string]Service 93 94 Keys map[string]Key 94 - 95 - // If a valid atproto repo signing public key was parsed, it can be cached here. This is a nullable/optional field (crypto.PublicKey is an interface). Calling code should use [Identity.PublicKey] instead of accessing this member. 96 - ParsedPublicKey crypto.PublicKey 97 95 } 98 96 99 97 type Key struct { ··· 155 153 } 156 154 } 157 155 158 - // Identifies and parses the atproto repo signing public key, specifically, out of any keys associated with this identity. 156 + // Identifies and parses the atproto repo signing public key, specifically, out of any keys in this identity's DID document. 159 157 // 160 158 // Returns [ErrKeyNotFound] if there is no such key. 161 159 // 162 160 // Note that [crypto.PublicKey] is an interface, not a concrete type. 163 161 func (i *Identity) PublicKey() (crypto.PublicKey, error) { 164 - if i.ParsedPublicKey != nil { 165 - return i.ParsedPublicKey, nil 166 - } 167 - if i.Keys == nil { 168 - return nil, fmt.Errorf("identity has no atproto public key attached") 169 - } 170 - return i.PublicKeyFor("atproto") 162 + return i.GetPublicKey("atproto") 171 163 } 172 164 173 - // Identifies and parses a specified service signing public key, specifically, out of any keys associated with this identity. 165 + // Identifies and parses a specified service signing public key out of any keys in this identity's DID document. 174 166 // 175 167 // Returns [ErrKeyNotFound] if there is no such key. 176 168 // 177 169 // Note that [crypto.PublicKey] is an interface, not a concrete type. 178 - func (i *Identity) PublicKeyFor(forKey string) (crypto.PublicKey, error) { 179 - if i.ParsedPublicKey != nil { 180 - return i.ParsedPublicKey, nil 181 - } 170 + func (i *Identity) GetPublicKey(id string) (crypto.PublicKey, error) { 182 171 if i.Keys == nil { 183 - return nil, fmt.Errorf("identity has no atproto public key attached") 172 + return nil, ErrKeyNotDeclared 184 173 } 185 - k, ok := i.Keys[forKey] 174 + k, ok := i.Keys[id] 186 175 if !ok { 187 176 return nil, ErrKeyNotDeclared 188 177 } ··· 212 201 } 213 202 } 214 203 215 - // The home PDS endpoint for this account, if one is included in identity metadata (returns empty string if not found). 204 + // The home PDS endpoint for this identity, if one is included in the DID document. 205 + // 206 + // The endpoint should be an HTTP URL with method, hostname, and optional port. It may or may not include path segments. 216 207 // 217 - // The endpoint should be an HTTP URL with method, hostname, and optional port, and (usually) no path segments. 208 + // Returns an empty string if the serivce isn't found, or if the URL fails to parse. 218 209 func (i *Identity) PDSEndpoint() string { 210 + return i.GetServiceEndpoint("atproto_pds") 211 + } 212 + 213 + // Returns the service endpoint URL for specified service ID (the fragment part of identifier, not including the hash symbol). 214 + // 215 + // The endpoint should be an HTTP URL with method, hostname, and optional port. It may or may not include path segments. 216 + // 217 + // Returns an empty string if the serivce isn't found, or if the URL fails to parse. 218 + func (i *Identity) GetServiceEndpoint(id string) string { 219 219 if i.Services == nil { 220 220 return "" 221 221 } 222 - endpoint, ok := i.Services["atproto_pds"] 222 + endpoint, ok := i.Services[id] 223 223 if !ok { 224 224 return "" 225 225 }
-5
atproto/identity/redisdir/redis_directory.go
··· 112 112 return &he, nil 113 113 } 114 114 115 - ident.ParsedPublicKey = nil 116 115 entry := identityEntry{ 117 116 Updated: time.Now(), 118 117 Identity: ident, ··· 195 194 196 195 func (d *RedisDirectory) updateDID(ctx context.Context, did syntax.DID) (*identityEntry, error) { 197 196 ident, err := d.Inner.LookupDID(ctx, did) 198 - // wipe parsed public key; it's a waste of space and can't serialize 199 - if nil == err { 200 - ident.ParsedPublicKey = nil 201 - } 202 197 // persist the identity lookup error, instead of processing it immediately 203 198 entry := identityEntry{ 204 199 Updated: time.Now(),
-2
automod/capture/capture.go
··· 26 26 } 27 27 } 28 28 29 - // clear any pre-parsed key, which would fail to marshal as JSON 30 - ident.ParsedPublicKey = nil 31 29 am, err := eng.GetAccountMeta(ctx, ident) 32 30 if err != nil { 33 31 return nil, err
+1 -2
automod/capture/testdata/capture_atprotocom.json
··· 18 18 "Type": "Multikey", 19 19 "PublicKeyMultibase": "zQ3shunBKsXixLxKtC5qeSG9E4J5RkGN57im31pcTzbNQnm5w" 20 20 } 21 - }, 22 - "ParsedPublicKey": null 21 + } 23 22 }, 24 23 "Profile": { 25 24 "HasAvatar": true,
-3
automod/engine/fetch_account_meta.go
··· 16 16 17 17 logger := e.Logger.With("did", ident.DID.String()) 18 18 19 - // wipe parsed public key; it's a waste of space and can't serialize 20 - ident.ParsedPublicKey = nil 21 - 22 19 // fallback in case client wasn't configured (eg, testing) 23 20 if e.BskyClient == nil { 24 21 logger.Warn("skipping account meta hydration")
+1 -1
automod/rules/quick.go
··· 9 9 "github.com/bluesky-social/indigo/automod" 10 10 ) 11 11 12 - var botLinkStrings = []string{"ainna13762491", "LINK押して", "→ https://tiny.one/"} 12 + var botLinkStrings = []string{"ainna13762491", "LINK押して", "→ https://tiny", "⇒ http://tiny"} 13 13 var botSpamTLDs = []string{".today", ".life"} 14 14 var botSpamStrings = []string{"515-9719"} 15 15
+1 -2
automod/rules/testdata/capture_hackerdarkweb.json
··· 18 18 "Type": "Multikey", 19 19 "PublicKeyMultibase": "zQ3sha9ULLgA6zyJmk1z2uDkpkSs4Ypou33RE8XuhwWtSHF75" 20 20 } 21 - }, 22 - "ParsedPublicKey": null 21 + } 23 22 }, 24 23 "Profile": { 25 24 "HasAvatar": true,