Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/management/src/modal/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { UIProvider } from '@3id/ui-provider'
import { aliases as idxAliases } from '@3id/model'
import { DIDDataStore } from '@glazed/did-datastore'
import { CeramicClient } from '@ceramicnetwork/http-client'
import { TileLoader } from '@glazed/tile-loader'

export function create3IDService(uiProvider: UIProvider, didDataStore: DIDDataStore ) {
const connectService = new ThreeIDService()
Expand All @@ -14,5 +15,6 @@ export function create3IDService(uiProvider: UIProvider, didDataStore: DIDDataSt

export function createDIDDataStore() {
const ceramic = new CeramicClient(CERAMIC_URL, { syncInterval: 30 * 60 * 1000 })
return new DIDDataStore({ ceramic, model: idxAliases })
const loader = new TileLoader({ ceramic, cache: true })
return new DIDDataStore({ ceramic, model: idxAliases, loader })
}
1 change: 1 addition & 0 deletions packages/connect-service/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class ThreeIDService {
assert.isDefined(this.authProviderRelay, 'authProviderRelay must be defined')

const manage = new Manager(this.authProviderRelay, { dataStore: this.dataStore })
void manage.preload(accountId)

//TODO if exist in state, return before even looking up links
const existLocally = await manage.cache.getLinkedDid(accountId)
Expand Down
36 changes: 30 additions & 6 deletions packages/did-manager/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CeramicClient } from '@ceramicnetwork/http-client'
import { Caip10Link } from '@ceramicnetwork/stream-caip10-link'
import { DIDDataStore } from '@glazed/did-datastore'
import { hash } from '@stablelib/sha256'
import { TileLoader, getDeterministicQuery, keyToQuery } from '@glazed/tile-loader'
import { Resolver } from 'did-resolver'
import { DID, type DIDProvider } from 'dids'
import { getResolver as getKeyResolver } from 'key-did-resolver'
Expand All @@ -27,13 +28,17 @@ typeof process !== 'undefined' &&
typeof process !== 'undefined' &&
(DID_MIGRATION = process.env.MIGRATION ? process.env.MIGRATION === 'true' : true)

const definitionIDs = Object.values(idxAliases.definitions)
const schemaQueries = Object.values(idxAliases.schemas).map((val) => keyToQuery(val.split('//')[1]))

export class Manager {
authProvider: AuthProvider
store: DIDStore
cache: LinkCache
dataStore: DIDDataStore
ceramic: CeramicApi
threeIdProviders: Record<string, ThreeIdProvider>
loader: TileLoader

// needs work on wording for "account", did, caip10 etc
constructor(
Expand All @@ -43,16 +48,34 @@ export class Manager {
this.authProvider = authprovider
this.store = opts.store || new DIDStore()
this.cache = opts.cache || new LinkCache()
this.ceramic =
opts.ceramic ||
opts.dataStore?.ceramic ||
new CeramicClient(CERAMIC_API, { syncInterval: 30 * 60 * 1000 })
const loader = new TileLoader({ ceramic: this.ceramic, cache: true })
this.dataStore =
opts.dataStore ||
new DIDDataStore({
ceramic: opts.ceramic || new CeramicClient(CERAMIC_API),
model: idxAliases,
})
this.ceramic = opts.ceramic || this.dataStore.ceramic
opts.dataStore || new DIDDataStore({ ceramic: this.ceramic, model: idxAliases, loader })
this.loader = this.dataStore.loader
this.threeIdProviders = {}
}

async preload(accountId: string): Promise<void> {
definitionIDs.forEach((val) => void this.dataStore.getDefinition(val))
const preloadFamilies = ['IDX', 'authLink', ...definitionIDs]
const did = await this.linkInNetwork(accountId)

if (did != null) {
const queries = await Promise.all(
preloadFamilies.map(async (family) => {
return await getDeterministicQuery({ controllers: [did], family })
})
)
await this.loader.loadMany(queries.concat(schemaQueries))
} else {
await this.loader.loadMany(schemaQueries)
}
}

// Create DID
async createAccount(opts?: { legacyDid?: string; skipMigration?: boolean }): Promise<string> {
const migrate = DID_MIGRATION && !opts?.skipMigration
Expand Down Expand Up @@ -136,6 +159,7 @@ export class Manager {
const threeIdConfig = Object.assign(config, {
getPermission,
ceramic: this.ceramic,
loader: this.loader,
})
const threeId = await ThreeIdProvider.create(threeIdConfig)
this.threeIdProviders[threeId.id] = threeId
Expand Down
24 changes: 24 additions & 0 deletions packages/did-manager/test/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ describe('3ID Manager', () => {
expect(links[toLegacyAccountId(accountId.toString())]).toBeTruthy()
})

test('creates/loads new did with preload', async () => {
// auth provider create
const authProvider = await createAuthProvider(7)
const accountId = (await authProvider.accountId()).toString()
const manager = new Manager(authProvider, { ceramic })
await manager.preload(accountId)
const did = await manager.createAccount()
// expect link to be created
const links = await dataStore.get('cryptoAccounts', did)
expect(links[toLegacyAccountId(accountId.toString())]).toBeTruthy()
})

test('creates/loads existing did in network', async () => {
const authProvider = await createAuthProvider(2)
const manager = new Manager(authProvider, { ceramic })
Expand All @@ -44,6 +56,18 @@ describe('3ID Manager', () => {
expect(did1).toEqual(did2)
})

test('creates/loads existing did in network with preload', async () => {
const authProvider = await createAuthProvider(8)
const manager = new Manager(authProvider, { ceramic })
const did1 = await manager.createAccount()
manager.store.store.clearAll()
const accountId = (await authProvider.accountId()).toString()
const manager2 = new Manager(authProvider, { ceramic })
await manager.preload(accountId)
const did2 = await manager2.createAccount()
expect(did1).toEqual(did2)
})

test.todo('creates/loads did from storage')

test.todo('creates/loads did from in memory')
Expand Down