Monorepo for Tangled
tangled.org
1package keys
2
3import (
4 "context"
5 "fmt"
6
7 comatproto "github.com/bluesky-social/indigo/api/atproto"
8 "github.com/bluesky-social/indigo/atproto/identity"
9 "github.com/bluesky-social/indigo/atproto/syntax"
10 indigoxrpc "github.com/bluesky-social/indigo/xrpc"
11 "tangled.org/core/api/tangled"
12 "tangled.org/core/knotserver/db"
13 "tangled.org/core/log"
14)
15
16func FetchAndStore(ctx context.Context, dir identity.Directory, store *db.DB, did string) error {
17 l := log.FromContext(ctx)
18
19 id, err := dir.LookupDID(ctx, syntax.DID(did))
20 if err != nil {
21 return fmt.Errorf("lookup did to fetch keys: %w", err)
22 }
23
24 serviceEndpoint, ok := id.Services["atproto_pds"]
25 if !ok {
26 l.Warn("did identity did not contain atproto_pds service while adding their keys", "did", did)
27 return nil
28 }
29
30 xrpcc := indigoxrpc.Client{Host: serviceEndpoint.URL}
31 resp, err := comatproto.RepoListRecords(ctx, &xrpcc, tangled.PublicKeyNSID, "", 50, did, false)
32 if err != nil {
33 return fmt.Errorf("fetching public keys for did: %w", err)
34 }
35
36 for _, record := range resp.Records {
37 if record == nil {
38 continue
39 }
40 key, ok := record.Value.Val.(*tangled.PublicKey)
41 if !ok || key == nil {
42 continue
43 }
44 if err := store.AddPublicKey(db.PublicKey{Did: did, PublicKey: *key}); err != nil {
45 return fmt.Errorf("adding public key to db: %w", err)
46 }
47 }
48 return nil
49}