~ knotless Search/
↑↓ move↵ open⇧↵ all results

socialweb.computer/socialweb-computer-kcp

socialweb.computer: a compute market on atproto (kcp)

1 collaborator
0
0
51 lines · 1.3 KB Raw History
package requester

import (
	"context"
	"sync"

	"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/market"
	"github.com/johnandersen777/socialweb-computer-kcp/internal/identlink"
	"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/ttlcache"
)

type keyBinding struct {
	records *identlink.Remote

	bound *ttlcache.Cache[string, bool]
}

type keyBindingOnce struct {
	once sync.Once

	binding *keyBinding
}

func (q *Requester) keyBinding() *keyBinding {
	q.binding.once.Do(func() {
		q.binding.binding = &keyBinding{
			records: identlink.NewRemote(q.deps.Directory, q.marketHTTP()),
			bound:   ttlcache.New[string, bool](ttlcache.Options{TTL: identlink.DocTTL, MaxEntries: 4096}),
		}
	})
	return q.binding.binding
}

func (b *keyBinding) check(ctx context.Context, did, key string) (bool, error) {
	id := did + "#" + key
	if ok, hit := b.bound.Get(id); hit {
		return ok, nil
	}
	value, _, err := b.records.GetVerifiedRecord(ctx, did, market.BadgeBlueKeysNSID, market.BadgeBlueKeysRkey(did, key))
	if err != nil {
		return false, err
	}
	keyID, _ := value["keyId"].(string)
	challenge, _ := value["challenge"].(string)
	ok := value != nil && keyID == key && challenge == did
	if !ok {
		return false, nil
	}
	b.bound.Set(id, true)
	return true, nil
}