Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(anoncreds): issue revocable credentials #1427

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
c228f88
feat: initial revocation registry definition implementation
genaris Mar 29, 2023
cafe703
feat: register revocation status list
genaris Mar 31, 2023
1bb11d8
Merge remote-tracking branch 'upstream/main' into feat/issue-revocabl…
genaris Mar 31, 2023
b70ba0a
feat: add RevocationStatusList registration methods in API
genaris Mar 31, 2023
ea88904
feat: add revocation parameters when issuing credential
genaris Apr 1, 2023
1cba24c
test: add anoncreds revocation test
genaris Apr 1, 2023
7cb3f5f
Merge remote-tracking branch 'upstream/main' into feat/issue-revocabl…
genaris Apr 1, 2023
12c7602
Merge remote-tracking branch 'upstream/main' into feat/issue-revocabl…
genaris Apr 1, 2023
429da81
various fixes (WIP)
genaris Apr 3, 2023
e635a49
add test local tails server and some fixes to pass full flow test
genaris Apr 4, 2023
9aeb57d
Merge remote-tracking branch 'upstream/main' into feat/issue-revocabl…
genaris Apr 4, 2023
e155c8d
Merge remote-tracking branch 'upstream/main' into feat/issue-revocabl…
genaris Apr 8, 2023
0cc2cb7
add revokeCredentials method and tests
genaris Apr 10, 2023
f5fba00
Merge remote-tracking branch 'upstream/main' into feat/issue-revocabl…
genaris Apr 10, 2023
8fc9380
add revocation notification and generic tails file manager
genaris Apr 11, 2023
a8d824d
some clean up of tests and naming
genaris Apr 11, 2023
9c41913
add proofs tests with revocation + fixes in timestamp
genaris Apr 11, 2023
970a17a
remove unused interfaces and implementation for indy-vdr
genaris Apr 11, 2023
ea2e8ef
fix: types errors
genaris Apr 12, 2023
70e7651
fix: ensure tails directory is created
genaris Apr 12, 2023
7486550
Merge remote-tracking branch 'upstream/main' into feat/issue-revocabl…
genaris Apr 15, 2023
998ead9
feat: override timestamps and add revoked credential test
genaris Apr 15, 2023
0527a0b
Merge remote-tracking branch 'upstream/main' into feat/issue-revocabl…
genaris Apr 28, 2023
599d9d0
feat: update anoncreds-rs, adjust interfaces
genaris Apr 28, 2023
acf8c71
Merge branch 'main' into feat/issue-revocable-credentials
genaris May 2, 2023
87d88c8
fix: address several PR feedback
genaris Jun 1, 2023
d5c7b6d
feat: simplify TailsFileService interface
genaris Jun 1, 2023
8bf88f3
Merge branch 'main' into feat/issue-revocable-credentials
genaris Jun 1, 2023
64f6bfd
Merge remote-tracking branch 'upstream/main' into feat/issue-revocabl…
genaris Jun 1, 2023
d4685cf
fix: some adjustments after merge
genaris Jun 1, 2023
505bf08
fix: types
genaris Jun 1, 2023
64ce973
Merge branch 'main' into feat/issue-revocable-credentials
genaris Jun 7, 2023
89aca1f
Merge branch 'main' into feat/issue-revocable-credentials
genaris Jul 22, 2023
3ebb665
fix: remove revocStatusList from credential creation interface
genaris Jul 22, 2023
98e5380
fix: unused variable
genaris Jul 22, 2023
da6aa63
test: fix anoncreds module registration
genaris Jul 22, 2023
2c1a3b3
fix: remove unused import
genaris Jul 22, 2023
8f5d6bf
Merge branch 'main' into feat/issue-revocable-credentials
genaris Aug 17, 2023
e2f4964
fix: use getOutboundMessageContext
genaris Aug 17, 2023
c4c5648
Merge branch 'main' into feat/issue-revocable-credentials
genaris Aug 18, 2023
347de98
Merge branch 'main' into feat/issue-revocable-credentials
genaris Oct 30, 2023
430439e
Merge branch 'main' into feat/issue-revocable-credentials
genaris Nov 3, 2023
89d60cd
fix: adapt to latest anoncreds-rs API
genaris Nov 3, 2023
61ac750
fix: optional timestamp in updaterevstatuslist
genaris Nov 6, 2023
0096052
Merge branch 'main' into feat/issue-revocable-credentials
genaris Nov 6, 2023
0b78645
Merge branch 'main' into feat/issue-revocable-credentials
genaris Nov 13, 2023
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
179 changes: 172 additions & 7 deletions packages/anoncreds-rs/src/services/AnonCredsRsIssuerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,35 @@ import type {
AnonCredsCredentialDefinition,
CreateCredentialDefinitionReturn,
AnonCredsCredential,
CreateRevocationRegistryDefinitionOptions,
CreateRevocationRegistryDefinitionReturn,
AnonCredsRevocationRegistryDefinition,
CreateRevocationStatusListOptions,
AnonCredsRevocationStatusList,
UpdateRevocationStatusListOptions,
} from '@aries-framework/anoncreds'
import type { AgentContext } from '@aries-framework/core'
import type { CredentialDefinitionPrivate, JsonObject, KeyCorrectnessProof } from '@hyperledger/anoncreds-shared'

import {
RevocationRegistryState,
AnonCredsRevocationRegistryDefinitionRepository,
AnonCredsRevocationRegistryDefinitionPrivateRepository,
AnonCredsKeyCorrectnessProofRepository,
AnonCredsCredentialDefinitionPrivateRepository,
AnonCredsCredentialDefinitionRepository,
} from '@aries-framework/anoncreds'
import { injectable, AriesFrameworkError } from '@aries-framework/core'
import { Credential, CredentialDefinition, CredentialOffer, Schema } from '@hyperledger/anoncreds-shared'
import {
RevocationStatusList,
RevocationRegistryDefinitionPrivate,
RevocationRegistryDefinition,
CredentialRevocationConfig,
Credential,
CredentialDefinition,
CredentialOffer,
Schema,
} from '@hyperledger/anoncreds-shared'

import { AnonCredsRsError } from '../errors/AnonCredsRsError'

Expand Down Expand Up @@ -79,6 +97,103 @@ export class AnonCredsRsIssuerService implements AnonCredsIssuerService {
}
}

public async createRevocationRegistryDefinition(
agentContext: AgentContext,
options: CreateRevocationRegistryDefinitionOptions
): Promise<CreateRevocationRegistryDefinitionReturn> {
const { tag, issuerId, credentialDefinition, credentialDefinitionId, maximumCredentialNumber, tailsDirectoryPath } =
options

let createReturnObj:
| {
revocationRegistryDefinition: RevocationRegistryDefinition
revocationRegistryDefinitionPrivate: RevocationRegistryDefinitionPrivate
}
| undefined
try {
createReturnObj = RevocationRegistryDefinition.create({
credentialDefinition: credentialDefinition as unknown as JsonObject,
credentialDefinitionId,
issuerId,
maximumCredentialNumber,
revocationRegistryType: 'CL_ACCUM',
tag,
tailsDirectoryPath,
})

return {
revocationRegistryDefinition:
createReturnObj.revocationRegistryDefinition.toJson() as unknown as AnonCredsRevocationRegistryDefinition,
revocationRegistryDefinitionPrivate: createReturnObj.revocationRegistryDefinitionPrivate.toJson(),
}
} finally {
createReturnObj?.revocationRegistryDefinition.handle.clear()
createReturnObj?.revocationRegistryDefinitionPrivate.handle.clear()
}
}

public async createRevocationStatusList(
agentContext: AgentContext,
options: CreateRevocationStatusListOptions
): Promise<AnonCredsRevocationStatusList> {
const { issuerId, revocationRegistryDefinitionId, revocationRegistryDefinition, issuanceByDefault, tailsFilePath } =
options

let revocationStatusList: RevocationStatusList | undefined
try {
revocationStatusList = RevocationStatusList.create({
issuanceByDefault,
revocationRegistryDefinitionId,
revocationRegistryDefinition: {
...revocationRegistryDefinition,
value: { ...revocationRegistryDefinition.value, tailsLocation: tailsFilePath },
genaris marked this conversation as resolved.
Show resolved Hide resolved
} as unknown as JsonObject,
issuerId,
})

return revocationStatusList.toJson() as unknown as AnonCredsRevocationStatusList
} finally {
revocationStatusList?.handle.clear()
}
}

public async updateRevocationStatusList(
agentContext: AgentContext,
options: UpdateRevocationStatusListOptions
): Promise<AnonCredsRevocationStatusList> {
const { revocationStatusList, revocationRegistryDefinition, issued, revoked, timestamp, tailsFilePath } = options

let updatedRevocationStatusList: RevocationStatusList | undefined
let revocationRegistryDefinitionObj: RevocationRegistryDefinition | undefined

try {
updatedRevocationStatusList = RevocationStatusList.fromJson(revocationStatusList as unknown as JsonObject)

if (timestamp && !issued && !revoked) {
updatedRevocationStatusList.updateTimestamp({
timestamp,
})
} else {
revocationRegistryDefinitionObj = RevocationRegistryDefinition.fromJson({
...revocationRegistryDefinition,
value: { ...revocationRegistryDefinition.value, tailsLocation: tailsFilePath },
genaris marked this conversation as resolved.
Show resolved Hide resolved
} as unknown as JsonObject)
updatedRevocationStatusList.update({
// TODO: Fix parameters in anoncreds-rs
revocationRegstryDefinition: revocationRegistryDefinitionObj,
issued: options.issued,
revoked: options.revoked,
timestamp: timestamp ?? -1,
})
}

return updatedRevocationStatusList.toJson() as unknown as AnonCredsRevocationStatusList
} finally {
updatedRevocationStatusList?.handle.clear()
revocationRegistryDefinitionObj?.handle.clear()
}
}

public async createCredentialOffer(
agentContext: AgentContext,
options: CreateCredentialOfferOptions
Expand Down Expand Up @@ -115,14 +230,26 @@ export class AnonCredsRsIssuerService implements AnonCredsIssuerService {
agentContext: AgentContext,
options: CreateCredentialOptions
): Promise<CreateCredentialReturn> {
const { tailsFilePath, credentialOffer, credentialRequest, credentialValues, revocationRegistryId } = options
const {
credentialOffer,
credentialRequest,
credentialValues,
revocationRegistryDefinitionId,
tailsFilePath,
revocationStatusList,
} = options

const definedRevocationOptions = [revocationRegistryDefinitionId, tailsFilePath, revocationStatusList].filter(
(e) => e !== undefined
)
if (definedRevocationOptions.length > 0 && definedRevocationOptions.length < 3) {
throw new AriesFrameworkError(
'Revocation requires all of revocationRegistryDefinitionId, revocationStatusList and tailsFilePath'
)
}

let credential: Credential | undefined
try {
if (revocationRegistryId || tailsFilePath) {
throw new AriesFrameworkError('Revocation not supported yet')
}

const attributeRawValues: Record<string, string> = {}
const attributeEncodedValues: Record<string, string> = {}

Expand All @@ -139,14 +266,52 @@ export class AnonCredsRsIssuerService implements AnonCredsIssuerService {
.resolve(AnonCredsCredentialDefinitionPrivateRepository)
.getByCredentialDefinitionId(agentContext, options.credentialRequest.cred_def_id)

let revocationConfiguration: CredentialRevocationConfig | undefined
if (options.revocationRegistryDefinitionId && options.tailsFilePath) {
const revocationRegistryDefinitionRecord = await agentContext.dependencyManager
.resolve(AnonCredsRevocationRegistryDefinitionRepository)
.getByRevocationRegistryDefinitionId(agentContext, options.revocationRegistryDefinitionId)

const revocationRegistryDefinitionPrivateRecord = await agentContext.dependencyManager
.resolve(AnonCredsRevocationRegistryDefinitionPrivateRepository)
.getByRevocationRegistryDefinitionId(agentContext, options.revocationRegistryDefinitionId)

const registryIndex = revocationRegistryDefinitionPrivateRecord.currentIndex + 1
genaris marked this conversation as resolved.
Show resolved Hide resolved

if (registryIndex >= revocationRegistryDefinitionRecord.revocationRegistryDefinition.value.maxCredNum) {
revocationRegistryDefinitionPrivateRecord.state = RevocationRegistryState.Full
}

// Update current registry index in storage
// Note: if an error is produced or the credential is not effectively sent,
// the previous index will be skipped
revocationRegistryDefinitionPrivateRecord.currentIndex = registryIndex
await agentContext.dependencyManager
.resolve(AnonCredsRevocationRegistryDefinitionPrivateRepository)
.update(agentContext, revocationRegistryDefinitionPrivateRecord)

revocationConfiguration = new CredentialRevocationConfig({
registryDefinition: RevocationRegistryDefinition.fromJson(
revocationRegistryDefinitionRecord.revocationRegistryDefinition as unknown as JsonObject
),
registryDefinitionPrivate: RevocationRegistryDefinitionPrivate.fromJson(
revocationRegistryDefinitionPrivateRecord.value
),
tailsPath: options.tailsFilePath,
registryIndex,
})
}

credential = Credential.create({
credentialDefinition: credentialDefinitionRecord.credentialDefinition as unknown as JsonObject,
credentialOffer: credentialOffer as unknown as JsonObject,
credentialRequest: credentialRequest as unknown as JsonObject,
revocationRegistryId,
revocationRegistryId: revocationRegistryDefinitionId,
attributeEncodedValues,
attributeRawValues,
credentialDefinitionPrivate: credentialDefinitionPrivateRecord.value,
revocationConfiguration,
revocationStatusList: revocationStatusList ? (revocationStatusList as unknown as JsonObject) : undefined,
})

return {
Expand Down
60 changes: 60 additions & 0 deletions packages/anoncreds-rs/tests/InMemoryTailsFileService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { AnonCredsRevocationRegistryDefinition } from '@aries-framework/anoncreds'
import type { AgentContext } from '@aries-framework/core'

import { BasicTailsFileService } from '@aries-framework/anoncreds'

export class InMemoryTailsFileService extends BasicTailsFileService {
private tailsFilePaths: Record<string, string> = {}

public async uploadTailsFile(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we provide the actual tailsFile? Or will that be impactful on performance?

We would like to upload the tails file somewhere. Do we need to load it from the tailsLocation and then upload it?

I thiught the tailsLocation contained the http url, not the path

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tailsLocation thing is a bit confusing, as the output from the library is a RevocationRegistryDefinition whose tailsLocation is actually the local file path, and then we should update it with the actual URL path before storing in the wallet and publishing to the VDR.

Besides this explanation, I think you are right and it will be better to not use the revocation registry definition object but its ID and either the local tails file path or the file contents itself. At first sight I'd prefer to pass the path, as usually libraries like fetch or axios do work well with file streams and that could be more efficient if handled by lower level modules.

agentContext: AgentContext,
options: {
revocationRegistryDefinition: AnonCredsRevocationRegistryDefinition
}
): Promise<string> {
this.tailsFilePaths[options.revocationRegistryDefinition.value.tailsHash] =
options.revocationRegistryDefinition.value.tailsLocation

return options.revocationRegistryDefinition.value.tailsHash
}

public async downloadTailsFile(
agentContext: AgentContext,
options: {
revocationRegistryDefinition: AnonCredsRevocationRegistryDefinition
}
): Promise<{
tailsFilePath: string
}> {
const { revocationRegistryDefinition } = options
const { tailsLocation, tailsHash } = revocationRegistryDefinition.value

try {
agentContext.config.logger.debug(
`Checking to see if tails file for URL ${revocationRegistryDefinition.value.tailsLocation} has been stored in the FileSystem`
)

// hash is used as file identifier
const tailsExists = await this.tailsFileExists(agentContext, tailsHash)
const tailsFilePath = this.getTailsFilePath(agentContext, tailsHash)
agentContext.config.logger.debug(
`Tails file for ${tailsLocation} ${tailsExists ? 'is stored' : 'is not stored'} at ${tailsFilePath}`
)

if (!tailsExists) {
agentContext.config.logger.debug(`Retrieving tails file from URL ${tailsLocation}`)
// TODO
genaris marked this conversation as resolved.
Show resolved Hide resolved
agentContext.config.logger.debug(`Saved tails file to FileSystem at path ${tailsFilePath}`)
}

return {
tailsFilePath,
}
} catch (error) {
agentContext.config.logger.error(`Error while retrieving tails file from URL ${tailsLocation}`, {
error,
})
throw error
}
}
}
30 changes: 30 additions & 0 deletions packages/anoncreds-rs/tests/LocalDidResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { DidResolutionResult, DidResolver, AgentContext } from '@aries-framework/core'

import { DidsApi } from '@aries-framework/core'

export class LocalDidResolver implements DidResolver {
public readonly supportedMethods = ['sov', 'indy']

public async resolve(agentContext: AgentContext, did: string): Promise<DidResolutionResult> {
const didDocumentMetadata = {}

const didsApi = agentContext.dependencyManager.resolve(DidsApi)

const didRecord = (await didsApi.getCreatedDids()).find((record) => record.did === did)
if (!didRecord) {
return {
didDocument: null,
didDocumentMetadata,
didResolutionMetadata: {
error: 'notFound',
message: `resolver_error: Unable to resolve did '${did}'`,
},
}
}
return {
didDocument: didRecord.didDocument ?? null,
didDocumentMetadata,
didResolutionMetadata: { contentType: 'application/did+ld+json' },
}
}
}
Loading