Newer
Older
Konstantin Tsabolov
committed
import type { CredentialExchangeRecord} from '@aries-framework/core';
Konstantin Tsabolov
committed
EventAnonCredsCredentialOfferGetAll,
EventAnonCredsCredentialOfferGetAllInput,
EventAnonCredsCredentialOfferGetById,
EventAnonCredsCredentialOfferGetByIdInput,
Konstantin Tsabolov
committed
EventAnonCredsCredentialRequestGetAll,
EventAnonCredsCredentialRequestGetAllInput,
Konstantin Tsabolov
committed
EventAnonCredsCredentialRequestGetById,
EventAnonCredsCredentialRequestGetByIdInput,
EventAnonCredsCredentialsDeleteById,
EventAnonCredsCredentialsDeleteByIdInput,
EventAnonCredsCredentialsGetAllInput,
EventAnonCredsCredentialsGetByIdInput,
EventDidcommAnonCredsCredentialsOffer,
EventDidcommAnonCredsCredentialsOfferInput,
EventDidcommAnonCredsCredentialsOfferToSelf,
Konstantin Tsabolov
committed
EventDidcommAnonCredsCredentialsOfferToSelfInput
import { AutoAcceptCredential, CredentialState } from '@aries-framework/core';
import { Injectable } from '@nestjs/common';
import { logger } from '@ocm/shared';
import { MetadataTokens } from '../../common/constants.js';
import { WithTenantService } from '../withTenantService.js';
@Injectable()
export class AnonCredsCredentialsService {
public constructor(private withTenantService: WithTenantService) {}
public async getAll({
tenantId,
}: EventAnonCredsCredentialsGetAllInput): Promise<
Array<CredentialExchangeRecord>
> {
return this.withTenantService.invoke(tenantId, (t) =>
t.credentials.getAll(),
);
}
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public async getAllOffers({
tenantId,
}: EventAnonCredsCredentialOfferGetAllInput): Promise<
EventAnonCredsCredentialOfferGetAll['data']
> {
return this.withTenantService.invoke(tenantId, (t) =>
t.credentials.findAllByQuery({
$or: [
{ state: CredentialState.OfferSent },
{ state: CredentialState.OfferReceived },
],
}),
);
}
public async getAllRequests({
tenantId,
}: EventAnonCredsCredentialRequestGetAllInput): Promise<
EventAnonCredsCredentialRequestGetAll['data']
> {
return this.withTenantService.invoke(tenantId, (t) =>
t.credentials.findAllByQuery({
$or: [
{ state: CredentialState.RequestSent },
{ state: CredentialState.RequestReceived },
],
}),
);
}
public async deleteById({
tenantId,
credentialRecordId,
}: EventAnonCredsCredentialsDeleteByIdInput): Promise<
EventAnonCredsCredentialsDeleteById['data']
> {
return this.withTenantService.invoke(tenantId, async (t) => {
await t.credentials.deleteById(credentialRecordId);
return {};
});
}
public async getById({
tenantId,
credentialRecordId,
}: EventAnonCredsCredentialsGetByIdInput): Promise<CredentialExchangeRecord | null> {
return this.withTenantService.invoke(tenantId, (t) =>
t.credentials.findById(credentialRecordId),
);
}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
public async getOfferById({
tenantId,
credentialOfferId,
}: EventAnonCredsCredentialOfferGetByIdInput): Promise<
EventAnonCredsCredentialOfferGetById['data']
> {
return this.withTenantService.invoke(tenantId, async (t) => {
const credential = await t.credentials.findById(credentialOfferId);
if (
credential &&
credential.state !== CredentialState.OfferSent &&
credential.state !== CredentialState.OfferReceived
) {
logger.warn(
`Credential '${credentialOfferId}' does exist, but is not in offer state. Actual state: ${credential.state}`,
);
}
return credential;
});
}
public async getRequestById({
tenantId,
credentialRequestId,
}: EventAnonCredsCredentialRequestGetByIdInput): Promise<
EventAnonCredsCredentialRequestGetById['data']
> {
return this.withTenantService.invoke(tenantId, async (t) => {
const credential = await t.credentials.findById(credentialRequestId);
if (
credential &&
credential.state !== CredentialState.RequestSent &&
credential.state !== CredentialState.RequestReceived
) {
logger.warn(
`Credential '${credentialRequestId}' does exist, but is not in a request state. Actual state: ${credential.state}`,
);
}
return credential;
});
}
public async offer({
tenantId,
connectionId,
credentialDefinitionId,
attributes,
}: EventDidcommAnonCredsCredentialsOfferInput): Promise<
EventDidcommAnonCredsCredentialsOffer['data']
> {
return this.withTenantService.invoke(tenantId, (t) =>
t.credentials.offerCredential({
protocolVersion: 'v2',
connectionId,
credentialFormats: {
anoncreds: { credentialDefinitionId, attributes },
},
}),
);
}
public async offerToSelf({
tenantId,
credentialDefinitionId,
attributes,
}: EventDidcommAnonCredsCredentialsOfferToSelfInput): Promise<
EventDidcommAnonCredsCredentialsOfferToSelf['data']
> {
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
return this.withTenantService.invoke(tenantId, async (t) => {
const connections = await t.connections.getAll();
const connection = connections.find((c) => {
const metadata = c.metadata.get<{ withSelf: boolean }>(
MetadataTokens.GAIA_X_CONNECTION_METADATA_KEY,
);
return metadata && metadata.withSelf === true;
});
if (!connection) {
throw new Error(
'Cannot offer a credential to yourself as there is no connection',
);
}
if (!connection.isReady) {
throw new Error('Connection with yourself is not ready, yet');
}
return t.credentials.offerCredential({
protocolVersion: 'v2',
autoAcceptCredential: AutoAcceptCredential.Always,
connectionId: connection.id,
credentialFormats: {
anoncreds: { credentialDefinitionId, attributes },
},
});
});
}
}