Skip to content

Commit

Permalink
feat: smart schemas and creds (no tests)
Browse files Browse the repository at this point in the history
Signed-off-by: Moriarty <[email protected]>
  • Loading branch information
Moriarty committed Jul 4, 2022
1 parent 695e347 commit d15b198
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { scoped, Lifecycle, inject } from 'tsyringe'

import { EventEmitter } from '../../../agent/EventEmitter'
import { InjectionSymbols } from '../../../constants'
import { injectable, inject } from '../../../plugins'
import { Repository } from '../../../storage/Repository'
import { StorageService } from '../../../storage/StorageService'

import { AnonCredsCredentialDefinitionRecord } from './AnonCredsCredentialDefinitionRecord'

@scoped(Lifecycle.ContainerScoped)
@injectable()
export class AnonCredsCredentialDefinitionRepository extends Repository<AnonCredsCredentialDefinitionRecord> {
public constructor(
@inject(InjectionSymbols.StorageService) storageService: StorageService<AnonCredsCredentialDefinitionRecord>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { scoped, Lifecycle, inject } from 'tsyringe'

import { EventEmitter } from '../../../agent/EventEmitter'
import { InjectionSymbols } from '../../../constants'
import { injectable, inject } from '../../../plugins'
import { Repository } from '../../../storage/Repository'
import { StorageService } from '../../../storage/StorageService'

import { AnonCredsSchemaRecord } from './AnonCredsSchemaRecord'

@scoped(Lifecycle.ContainerScoped)
@injectable()
export class AnonCredsSchemaRepository extends Repository<AnonCredsSchemaRecord> {
public constructor(
@inject(InjectionSymbols.StorageService) storageService: StorageService<AnonCredsSchemaRecord>,
Expand Down
78 changes: 42 additions & 36 deletions packages/core/src/modules/ledger/LedgerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { DependencyManager } from '../../plugins'
import type { AnonCredsCredentialDefinitionRecord } from '../indy/repository/AnonCredsCredentialDefinitionRecord'
import type { AnonCredsSchemaRecord } from '../indy/repository/AnonCredsSchemaRecord'
import type { SchemaTemplate, CredentialDefinitionTemplate } from './services'
import type { NymRole, Schema } from 'indy-sdk'
import type { CredDef, NymRole, Schema } from 'indy-sdk'

import { InjectionSymbols } from '../../constants'
import { AriesFrameworkError, RecordNotFoundError } from '../../error'
Expand Down Expand Up @@ -55,7 +55,7 @@ export class LedgerModule {
return this.ledgerService.getPublicDid(did)
}

public async findBySchemaId(schemaId: string): Promise<AnonCredsSchemaRecord | null> {
private async findBySchemaId(schemaId: string): Promise<AnonCredsSchemaRecord | null> {
try {
return await this.anonCredsSchemaRepository.getBySchemaId(schemaId)
} catch (e) {
Expand All @@ -64,6 +64,11 @@ export class LedgerModule {
throw e
}
}

public async getSchema(id: string) {
return this.ledgerService.getSchema(id)
}

public async registerSchema(schema: SchemaTemplate): Promise<Schema> {
const did = this.wallet.publicDid?.did

Expand All @@ -75,30 +80,25 @@ export class LedgerModule {

// Try find the schema in the wallet
const anonSchema = await this.findBySchemaId(schemaId)
// Schema not in wallet
if (!anonSchema) {
try {
// Try look for schema on the ledger and return it
return await this.getSchema(schemaId)
} catch (e) {
// Schema does not exist
if (e instanceof IndySdkError) {
// Register a new schema
return this.ledgerService.registerSchema(did, schema)
} else {
throw e
}
}
}
// Schema exists in wallet so return it
return anonSchema.schema
// Schema in wallet
if (anonSchema) return anonSchema.schema

const schemaFromLedger = await this.findBySchemaIdOnLedger(schemaId)
if (schemaFromLedger) return schemaFromLedger
return this.ledgerService.registerSchema(did, schema)
}

public async getSchema(id: string) {
return this.ledgerService.getSchema(id)
private async findBySchemaIdOnLedger(schemaId: string) {
try {
return await this.ledgerService.getSchema(schemaId)
} catch (e) {
if (e instanceof IndySdkError) return null

throw e
}
}

public async findByCredentialDefinitionId(
private async findByCredentialDefinitionId(
credentialDefinitionId: string
): Promise<AnonCredsCredentialDefinitionRecord | null> {
try {
Expand All @@ -110,6 +110,16 @@ export class LedgerModule {
}
}

private async findByCredentialDefinitionIdOnLedger(credentialDefinitionId: string): Promise<CredDef | null> {
try {
return await this.ledgerService.getCredentialDefinition(credentialDefinitionId)
} catch (e) {
if (e instanceof IndySdkError) return null

throw e
}
}

public async registerCredentialDefinition(
credentialDefinitionTemplate: Omit<CredentialDefinitionTemplate, 'signatureType'>
) {
Expand All @@ -129,20 +139,16 @@ export class LedgerModule {
}

// Check for the credential on the ledger.
try {
const credDefOnLedger = await this.ledgerService.getCredentialDefinition(credentialDefinitionId)
// If the ledger has the credential definition already throw an error
if (credDefOnLedger) {
throw new AriesFrameworkError(`Credential definition ${credentialDefinitionTemplate.tag} already exists.`)
}
} catch (e) {
// Could not retrieve credential from ledger -> credential is not registered yet. Do nothing
// Register the credential
return await this.ledgerService.registerCredentialDefinition(did, {
...credentialDefinitionTemplate,
signatureType: 'CL',
})
}
const credentialDefinitionOnLedger = await this.findByCredentialDefinitionIdOnLedger(credentialDefinitionId)
if (credentialDefinitionOnLedger)
throw new AriesFrameworkError(`Credential definition ${credentialDefinitionTemplate.tag} already exists.`)

// Could not retrieve credential from ledger -> credential is not registered yet. Do nothing
// Register the credential
return await this.ledgerService.registerCredentialDefinition(did, {
...credentialDefinitionTemplate,
signatureType: 'CL',
})
}

public async getCredentialDefinition(id: string) {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/modules/ledger/services/IndyLedgerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ export class IndyLedgerService {
}
}

public async findByCredentialDefinitionId(credentialDefinitionId: string) {
try {
await this.getCredentialDefinition(credentialDefinitionId)
} catch (e) {
if (e instanceof IndySdkError) return null

throw e
}
}

public async getCredentialDefinition(credentialDefinitionId: string) {
const did = didFromCredentialDefinitionId(credentialDefinitionId)
const { pool } = await this.indyPoolService.getPoolForDid(did)
Expand Down

0 comments on commit d15b198

Please sign in to comment.