Skip to content

Commit

Permalink
feat: 0.4.0 migration script (#1392)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Mar 29, 2023
1 parent 3bff26d commit bc5455f
Show file tree
Hide file tree
Showing 51 changed files with 3,504 additions and 715 deletions.
6 changes: 3 additions & 3 deletions packages/anoncreds-rs/tests/anoncreds-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,19 @@ describeRunInNodeVersion([18], 'AnonCreds format services using anoncreds-rs', (
})

expect(holderCredentialRecord.metadata.data).toEqual({
'_anonCreds/anonCredsCredential': {
'_anoncreds/credential': {
schemaId: schemaState.schemaId,
credentialDefinitionId: credentialDefinitionState.credentialDefinitionId,
},
'_anonCreds/anonCredsCredentialRequest': {
'_anoncreds/credentialRequest': {
master_secret_blinding_data: expect.any(Object),
master_secret_name: expect.any(String),
nonce: expect.any(String),
},
})

expect(issuerCredentialRecord.metadata.data).toEqual({
'_anonCreds/anonCredsCredential': {
'_anoncreds/credential': {
schemaId: schemaState.schemaId,
credentialDefinitionId: credentialDefinitionState.credentialDefinitionId,
},
Expand Down
6 changes: 3 additions & 3 deletions packages/anoncreds-rs/tests/indy-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,19 @@ describeRunInNodeVersion([18], 'Legacy indy format services using anoncreds-rs',
})

expect(holderCredentialRecord.metadata.data).toEqual({
'_anonCreds/anonCredsCredential': {
'_anoncreds/credential': {
schemaId: schemaState.schemaId,
credentialDefinitionId: credentialDefinitionState.credentialDefinitionId,
},
'_anonCreds/anonCredsCredentialRequest': {
'_anoncreds/credentialRequest': {
master_secret_blinding_data: expect.any(Object),
master_secret_name: expect.any(String),
nonce: expect.any(String),
},
})

expect(issuerCredentialRecord.metadata.data).toEqual({
'_anonCreds/anonCredsCredential': {
'_anoncreds/credential': {
schemaId: schemaState.schemaId,
credentialDefinitionId: credentialDefinitionState.credentialDefinitionId,
},
Expand Down
11 changes: 10 additions & 1 deletion packages/anoncreds/src/AnonCredsModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AnonCredsModuleConfigOptions } from './AnonCredsModuleConfig'
import type { DependencyManager, Module } from '@aries-framework/core'
import type { DependencyManager, Module, Update } from '@aries-framework/core'

import { AnonCredsApi } from './AnonCredsApi'
import { AnonCredsModuleConfig } from './AnonCredsModuleConfig'
Expand All @@ -11,6 +11,7 @@ import {
import { AnonCredsCredentialDefinitionRepository } from './repository/AnonCredsCredentialDefinitionRepository'
import { AnonCredsSchemaRepository } from './repository/AnonCredsSchemaRepository'
import { AnonCredsRegistryService } from './services/registry/AnonCredsRegistryService'
import { updateAnonCredsModuleV0_3_1ToV0_4 } from './updates/0.3.1-0.4'

/**
* @public
Expand All @@ -36,4 +37,12 @@ export class AnonCredsModule implements Module {
dependencyManager.registerSingleton(AnonCredsKeyCorrectnessProofRepository)
dependencyManager.registerSingleton(AnonCredsLinkSecretRepository)
}

public updates = [
{
fromVersion: '0.3.1',
toVersion: '0.4',
doUpdate: updateAnonCredsModuleV0_3_1ToV0_4,
},
] satisfies Update[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,19 @@ describe('Legacy indy format services', () => {
})

expect(holderCredentialRecord.metadata.data).toEqual({
'_anonCreds/anonCredsCredential': {
'_anoncreds/credential': {
schemaId: legacySchemaId,
credentialDefinitionId: legacyCredentialDefinitionId,
},
'_anonCreds/anonCredsCredentialRequest': {
'_anoncreds/credentialRequest': {
master_secret_blinding_data: expect.any(Object),
master_secret_name: expect.any(String),
nonce: expect.any(String),
},
})

expect(issuerCredentialRecord.metadata.data).toEqual({
'_anonCreds/anonCredsCredential': {
'_anoncreds/credential': {
schemaId: legacySchemaId,
credentialDefinitionId: legacyCredentialDefinitionId,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('V1 Connectionless Credentials', () => {
createdAt: expect.any(Date),
metadata: {
data: {
'_anonCreds/anonCredsCredential': {
'_anoncreds/credential': {
schemaId,
credentialDefinitionId,
},
Expand All @@ -136,7 +136,7 @@ describe('V1 Connectionless Credentials', () => {
createdAt: expect.any(Date),
metadata: {
data: {
'_anonCreds/anonCredsCredential': {
'_anoncreds/credential': {
schemaId,
credentialDefinitionId,
},
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('V1 Connectionless Credentials', () => {
createdAt: expect.any(Date),
metadata: {
data: {
'_anonCreds/anonCredsCredential': {
'_anoncreds/credential': {
schemaId,
credentialDefinitionId: credentialDefinitionId,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('V1 Credentials Auto Accept', () => {
createdAt: expect.any(Date),
metadata: {
data: {
'_anonCreds/anonCredsCredential': {
'_anoncreds/credential': {
schemaId: schemaId,
credentialDefinitionId: credentialDefinitionId,
},
Expand Down Expand Up @@ -128,8 +128,8 @@ describe('V1 Credentials Auto Accept', () => {
createdAt: expect.any(Date),
metadata: {
data: {
'_anonCreds/anonCredsCredentialRequest': expect.any(Object),
'_anonCreds/anonCredsCredential': {
'_anoncreds/credentialRequest': expect.any(Object),
'_anoncreds/credential': {
schemaId,
credentialDefinitionId,
},
Expand Down Expand Up @@ -230,8 +230,8 @@ describe('V1 Credentials Auto Accept', () => {
createdAt: expect.any(Date),
metadata: {
data: {
'_anonCreds/anonCredsCredentialRequest': expect.any(Object),
'_anonCreds/anonCredsCredential': {
'_anoncreds/credentialRequest': expect.any(Object),
'_anoncreds/credential': {
schemaId,
credentialDefinitionId: credentialDefinitionId,
},
Expand All @@ -252,7 +252,7 @@ describe('V1 Credentials Auto Accept', () => {
createdAt: expect.any(Date),
metadata: {
data: {
'_anonCreds/anonCredsCredential': {
'_anoncreds/credential': {
schemaId,
credentialDefinitionId: credentialDefinitionId,
},
Expand Down Expand Up @@ -318,8 +318,8 @@ describe('V1 Credentials Auto Accept', () => {
createdAt: expect.any(Date),
metadata: {
data: {
'_anonCreds/anonCredsCredentialRequest': expect.any(Object),
'_anonCreds/anonCredsCredential': {
'_anoncreds/credentialRequest': expect.any(Object),
'_anoncreds/credential': {
schemaId,
credentialDefinitionId: credentialDefinitionId,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class AnonCredsCredentialDefinitionRecord extends BaseRecord<
public static readonly type = 'AnonCredsCredentialDefinitionRecord'
public readonly type = AnonCredsCredentialDefinitionRecord.type

public readonly credentialDefinitionId!: string
public readonly credentialDefinition!: AnonCredsCredentialDefinition
public credentialDefinitionId!: string
public credentialDefinition!: AnonCredsCredentialDefinition

public constructor(props: AnonCredsCredentialDefinitionRecordProps) {
super()
Expand Down
4 changes: 2 additions & 2 deletions packages/anoncreds/src/repository/AnonCredsSchemaRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class AnonCredsSchemaRecord extends BaseRecord<
public static readonly type = 'AnonCredsSchemaRecord'
public readonly type = AnonCredsSchemaRecord.type

public readonly schemaId!: string
public readonly schema!: AnonCredsSchema
public schemaId!: string
public schema!: AnonCredsSchema

public constructor(props: AnonCredsSchemaRecordProps) {
super()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import type { AnonCredsCredentialDefinition } from '../../../models'

import { JsonTransformer } from '../../../../../core/src'
import { Agent } from '../../../../../core/src/agent/Agent'
import { getAgentConfig, getAgentContext, mockFunction } from '../../../../../core/tests'
import { InMemoryAnonCredsRegistry } from '../../../../tests/InMemoryAnonCredsRegistry'
import { AnonCredsCredentialDefinitionRecord } from '../../../repository'
import { AnonCredsCredentialDefinitionRepository } from '../../../repository/AnonCredsCredentialDefinitionRepository'
import * as testModule from '../credentialDefinition'

const agentConfig = getAgentConfig('AnonCreds Migration - Credential Exchange Record - 0.3.1-0.4.0')
const agentContext = getAgentContext()

jest.mock('../../../repository/AnonCredsCredentialDefinitionRepository')
const AnonCredsCredentialDefinitionRepositoryMock =
AnonCredsCredentialDefinitionRepository as jest.Mock<AnonCredsCredentialDefinitionRepository>
const credentialDefinitionRepository = new AnonCredsCredentialDefinitionRepositoryMock()

const inMemoryAnonCredsRegistry = new InMemoryAnonCredsRegistry({
existingCredentialDefinitions: {
'did:indy:bcovrin:test:SDqTzbVuCowusqGBNbNDjH/anoncreds/v0/CLAIM_DEF/104/default': {
schemaId: 'did:indy:bcovrin:test:SDqTzbVuCowusqGBNbNDjH/anoncreds/v0/SCHEMA/credentialDefinition-name/1.0',
tag: 'default',
type: 'CL',
value: {
primary: {
master_secret: '119999 00192381',
},
},
issuerId: 'did:indy:bcovrin:test:SDqTzbVuCowusqGBNbNDjH',
},
},
})

const registryService = {
getRegistryForIdentifier: () => inMemoryAnonCredsRegistry,
}
jest.mock('../../../../../core/src/agent/Agent', () => {
return {
Agent: jest.fn(() => ({
config: agentConfig,
context: agentContext,
dependencyManager: {
resolve: jest.fn((injectionSymbol) =>
injectionSymbol === AnonCredsCredentialDefinitionRepository ? credentialDefinitionRepository : registryService
),
},
})),
}
})

// Mock typed object
const AgentMock = Agent as jest.Mock<Agent>

describe('0.3.1-0.4.0 | AnonCreds Migration | Credential Definition Record', () => {
let agent: Agent

beforeEach(() => {
agent = new AgentMock()
})

afterEach(() => {
jest.clearAllMocks()
})

describe('migrateAnonCredsCredentialDefinitionRecordToV0_4()', () => {
it('should fetch all records and apply the needed updates', async () => {
const records: AnonCredsCredentialDefinitionRecord[] = [
getCredentialDefinitionRecord({
credentialDefinition: {
id: 'did:indy:bcovrin:test:SDqTzbVuCowusqGBNbNDjH/anoncreds/v0/CLAIM_DEF/104/default',
schemaId: '104',
tag: 'default',
type: 'CL',
value: {
primary: {
master_secret: '119999 00192381',
},
},
ver: '1.0',
},
}),
]

mockFunction(credentialDefinitionRepository.getAll).mockResolvedValue(records)

await testModule.migrateAnonCredsCredentialDefinitionRecordToV0_4(agent)

expect(credentialDefinitionRepository.getAll).toHaveBeenCalledTimes(1)
expect(credentialDefinitionRepository.update).toHaveBeenCalledTimes(1)

const [, credentialDefinitionRecord] = mockFunction(credentialDefinitionRepository.update).mock.calls[0]
expect(credentialDefinitionRecord.toJSON()).toMatchObject({
credentialDefinitionId: 'did:indy:bcovrin:test:SDqTzbVuCowusqGBNbNDjH/anoncreds/v0/CLAIM_DEF/104/default',
credentialDefinition: {
schemaId: 'did:indy:bcovrin:test:SDqTzbVuCowusqGBNbNDjH/anoncreds/v0/SCHEMA/credentialDefinition-name/1.0',
tag: 'default',
type: 'CL',
value: {
primary: {
master_secret: '119999 00192381',
},
},
issuerId: 'did:indy:bcovrin:test:SDqTzbVuCowusqGBNbNDjH',
},
})
})

it('should skip records that are already migrated to the 0.4.0 format', async () => {
const records: AnonCredsCredentialDefinitionRecord[] = [
getCredentialDefinitionRecord({
credentialDefinitionId: 'did:indy:bcovrin:test:SDqTzbVuCowusqGBNbNDjH/anoncreds/v0/CLAIM_DEF/104/default',
credentialDefinition: {
schemaId: 'did:indy:bcovrin:test:SDqTzbVuCowusqGBNbNDjH/anoncreds/v0/SCHEMA/credentialDefinition-name/1.0',
tag: 'default',
type: 'CL',
value: {
primary: {
master_secret: '119999 00192381',
},
},
issuerId: 'did:indy:bcovrin:test:SDqTzbVuCowusqGBNbNDjH',
},
}),
]

mockFunction(credentialDefinitionRepository.getAll).mockResolvedValue(records)

await testModule.migrateAnonCredsCredentialDefinitionRecordToV0_4(agent)

expect(credentialDefinitionRepository.getAll).toHaveBeenCalledTimes(1)
expect(credentialDefinitionRepository.update).toHaveBeenCalledTimes(0)
})
})
})

function getCredentialDefinitionRecord({
id,
credentialDefinition,
credentialDefinitionId,
}: {
id?: string
credentialDefinition: testModule.OldCredentialDefinition | AnonCredsCredentialDefinition
credentialDefinitionId?: string
}) {
return JsonTransformer.fromJSON(
{
id: id ?? 'credentialDefinition-record-id',
credentialDefinition,
credentialDefinitionId,
},
AnonCredsCredentialDefinitionRecord
)
}
Loading

0 comments on commit bc5455f

Please sign in to comment.