@verana-labs/vs-agent-sdk 2.0.0-dev.12 → 2.0.0-dev.120

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/README.md +0 -2
  2. package/build/adminApi/AdminApiError.d.ts +28 -0
  3. package/build/adminApi/AdminApiError.js +67 -0
  4. package/build/adminApi/AdminApiError.js.map +1 -0
  5. package/build/adminApi/didcommModules.d.ts +2 -0
  6. package/build/adminApi/didcommModules.js +8 -0
  7. package/build/adminApi/didcommModules.js.map +1 -0
  8. package/build/adminApi/index.d.ts +4 -0
  9. package/build/adminApi/index.js +21 -0
  10. package/build/adminApi/index.js.map +1 -0
  11. package/build/adminApi/moduleEndpoint.d.ts +7 -0
  12. package/build/adminApi/moduleEndpoint.js +21 -0
  13. package/build/adminApi/moduleEndpoint.js.map +1 -0
  14. package/build/adminApi/pagination/PageDto.d.ts +7 -0
  15. package/build/adminApi/pagination/PageDto.js +50 -0
  16. package/build/adminApi/pagination/PageDto.js.map +1 -0
  17. package/build/adminApi/pagination/PaginationQueryDto.d.ts +11 -0
  18. package/build/adminApi/pagination/PaginationQueryDto.js +46 -0
  19. package/build/adminApi/pagination/PaginationQueryDto.js.map +1 -0
  20. package/build/adminApi/pagination/cursor.d.ts +28 -0
  21. package/build/adminApi/pagination/cursor.js +64 -0
  22. package/build/adminApi/pagination/cursor.js.map +1 -0
  23. package/build/adminApi/pagination/index.d.ts +4 -0
  24. package/build/adminApi/pagination/index.js +21 -0
  25. package/build/adminApi/pagination/index.js.map +1 -0
  26. package/build/adminApi/pagination/paginate.d.ts +27 -0
  27. package/build/adminApi/pagination/paginate.js +77 -0
  28. package/build/adminApi/pagination/paginate.js.map +1 -0
  29. package/build/agent/VsAgent.d.ts +32 -36
  30. package/build/agent/VsAgent.js +179 -221
  31. package/build/agent/VsAgent.js.map +1 -1
  32. package/build/agent/createVsAgent.d.ts +9 -6
  33. package/build/agent/createVsAgent.js +5 -5
  34. package/build/agent/createVsAgent.js.map +1 -1
  35. package/build/blockchain/AnonCredsTrustService.d.ts +115 -0
  36. package/build/blockchain/AnonCredsTrustService.js +287 -0
  37. package/build/blockchain/AnonCredsTrustService.js.map +1 -0
  38. package/build/blockchain/AuthorizationService.d.ts +8 -6
  39. package/build/blockchain/AuthorizationService.js +30 -38
  40. package/build/blockchain/AuthorizationService.js.map +1 -1
  41. package/build/blockchain/IndexerWebSocketService.d.ts +5 -1
  42. package/build/blockchain/IndexerWebSocketService.js +42 -21
  43. package/build/blockchain/IndexerWebSocketService.js.map +1 -1
  44. package/build/blockchain/VeranaChainService.d.ts +38 -27
  45. package/build/blockchain/VeranaChainService.js +186 -177
  46. package/build/blockchain/VeranaChainService.js.map +1 -1
  47. package/build/blockchain/VeranaIndexerService.d.ts +14 -1
  48. package/build/blockchain/VeranaIndexerService.js +122 -8
  49. package/build/blockchain/VeranaIndexerService.js.map +1 -1
  50. package/build/blockchain/feePreflight.d.ts +34 -0
  51. package/build/blockchain/feePreflight.js +96 -0
  52. package/build/blockchain/feePreflight.js.map +1 -0
  53. package/build/blockchain/handlers/IndexerHandlerRegistry.d.ts +0 -1
  54. package/build/blockchain/handlers/IndexerHandlerRegistry.js.map +1 -1
  55. package/build/blockchain/handlers/authorizationHandlers.js +2 -2
  56. package/build/blockchain/handlers/authorizationHandlers.js.map +1 -1
  57. package/build/blockchain/handlers/defaultHandlers.js +7 -2
  58. package/build/blockchain/handlers/defaultHandlers.js.map +1 -1
  59. package/build/blockchain/handlers/index.d.ts +1 -0
  60. package/build/blockchain/handlers/index.js +1 -0
  61. package/build/blockchain/handlers/index.js.map +1 -1
  62. package/build/blockchain/handlers/selfIssuanceHandlers.d.ts +13 -0
  63. package/build/blockchain/handlers/selfIssuanceHandlers.js +76 -0
  64. package/build/blockchain/handlers/selfIssuanceHandlers.js.map +1 -0
  65. package/build/blockchain/handlers/stateMutations.d.ts +28 -4
  66. package/build/blockchain/handlers/stateMutations.js +356 -48
  67. package/build/blockchain/handlers/stateMutations.js.map +1 -1
  68. package/build/blockchain/index.d.ts +4 -0
  69. package/build/blockchain/index.js +4 -0
  70. package/build/blockchain/index.js.map +1 -1
  71. package/build/blockchain/indexerMappers.d.ts +3 -0
  72. package/build/blockchain/indexerMappers.js +57 -0
  73. package/build/blockchain/indexerMappers.js.map +1 -0
  74. package/build/blockchain/rpcRetry.d.ts +2 -0
  75. package/build/blockchain/rpcRetry.js +43 -0
  76. package/build/blockchain/rpcRetry.js.map +1 -0
  77. package/build/blockchain/triggerResolver.d.ts +26 -0
  78. package/build/blockchain/triggerResolver.js +110 -0
  79. package/build/blockchain/triggerResolver.js.map +1 -0
  80. package/build/blockchain/types.d.ts +14 -70
  81. package/build/blockchain/types.js +6 -1
  82. package/build/blockchain/types.js.map +1 -1
  83. package/build/bootstrap/EcsBootstrapService.d.ts +13 -4
  84. package/build/bootstrap/EcsBootstrapService.js +176 -124
  85. package/build/bootstrap/EcsBootstrapService.js.map +1 -1
  86. package/build/connections/InvitationRecord.d.ts +50 -0
  87. package/build/connections/InvitationRecord.js +60 -0
  88. package/build/connections/InvitationRecord.js.map +1 -0
  89. package/build/connections/ParentConnectionModule.d.ts +22 -0
  90. package/build/connections/ParentConnectionModule.js +91 -0
  91. package/build/connections/ParentConnectionModule.js.map +1 -0
  92. package/build/credentials/FullTailsFileService.d.ts +0 -1
  93. package/build/credentials/FullTailsFileService.js +0 -49
  94. package/build/credentials/FullTailsFileService.js.map +1 -1
  95. package/build/did/CachedDocumentLoader.d.ts +3 -0
  96. package/build/did/CachedDocumentLoader.js +9 -3
  97. package/build/did/CachedDocumentLoader.js.map +1 -1
  98. package/build/did/CachedWebDidResolver.d.ts +0 -4
  99. package/build/did/CachedWebDidResolver.js +1 -5
  100. package/build/did/CachedWebDidResolver.js.map +1 -1
  101. package/build/did/artifactServices.d.ts +12 -0
  102. package/build/did/artifactServices.js +48 -0
  103. package/build/did/artifactServices.js.map +1 -0
  104. package/build/did/legacyDidWeb.d.ts +6 -4
  105. package/build/did/legacyDidWeb.js +17 -14
  106. package/build/did/legacyDidWeb.js.map +1 -1
  107. package/build/did/publicDid.d.ts +4 -0
  108. package/build/did/publicDid.js +19 -0
  109. package/build/did/publicDid.js.map +1 -0
  110. package/build/did/reusedDidDocument.d.ts +37 -0
  111. package/build/did/reusedDidDocument.js +93 -0
  112. package/build/did/reusedDidDocument.js.map +1 -0
  113. package/build/events/BaseMessageEvents.d.ts +24 -0
  114. package/build/events/BaseMessageEvents.js +368 -14
  115. package/build/events/BaseMessageEvents.js.map +1 -1
  116. package/build/events/ConnectionEvents.js +149 -87
  117. package/build/events/ConnectionEvents.js.map +1 -1
  118. package/build/events/InvitationEvents.d.ts +6 -0
  119. package/build/events/InvitationEvents.js +57 -0
  120. package/build/events/InvitationEvents.js.map +1 -0
  121. package/build/events/VsAgentEvents.d.ts +40 -9
  122. package/build/events/VsAgentEvents.js +10 -1
  123. package/build/events/VsAgentEvents.js.map +1 -1
  124. package/build/events/VtFlowEvents.d.ts +1 -1
  125. package/build/events/VtFlowEvents.js +29 -17
  126. package/build/events/VtFlowEvents.js.map +1 -1
  127. package/build/events/index.d.ts +1 -0
  128. package/build/events/index.js +1 -0
  129. package/build/events/index.js.map +1 -1
  130. package/build/index.d.ts +9 -1
  131. package/build/index.js +20 -2
  132. package/build/index.js.map +1 -1
  133. package/build/plugins/setupBaseDidComm.js +24 -22
  134. package/build/plugins/setupBaseDidComm.js.map +1 -1
  135. package/build/transports/HttpInboundTransport.js +6 -1
  136. package/build/transports/HttpInboundTransport.js.map +1 -1
  137. package/build/transports/VsAgentWsInboundTransport.d.ts +1 -1
  138. package/build/transports/VsAgentWsInboundTransport.js +20 -14
  139. package/build/transports/VsAgentWsInboundTransport.js.map +1 -1
  140. package/build/types.d.ts +14 -13
  141. package/build/types.js +1 -2
  142. package/build/types.js.map +1 -1
  143. package/build/utils/agent.d.ts +42 -4
  144. package/build/utils/agent.js +126 -22
  145. package/build/utils/agent.js.map +1 -1
  146. package/build/utils/boundedFetch.d.ts +13 -0
  147. package/build/utils/boundedFetch.js +47 -0
  148. package/build/utils/boundedFetch.js.map +1 -0
  149. package/build/utils/didReadiness.js +5 -2
  150. package/build/utils/didReadiness.js.map +1 -1
  151. package/build/utils/ecsClaims.d.ts +46 -0
  152. package/build/utils/ecsClaims.js +115 -0
  153. package/build/utils/ecsClaims.js.map +1 -0
  154. package/build/utils/ecsService.d.ts +8 -0
  155. package/build/utils/ecsService.js +23 -0
  156. package/build/utils/ecsService.js.map +1 -0
  157. package/build/utils/index.d.ts +7 -0
  158. package/build/utils/index.js +7 -0
  159. package/build/utils/index.js.map +1 -1
  160. package/build/utils/publishedDidRecord.d.ts +15 -0
  161. package/build/utils/publishedDidRecord.js +83 -0
  162. package/build/utils/publishedDidRecord.js.map +1 -0
  163. package/build/utils/selfIssuedEcsCredential.d.ts +8 -0
  164. package/build/utils/selfIssuedEcsCredential.js +158 -0
  165. package/build/utils/selfIssuedEcsCredential.js.map +1 -0
  166. package/build/utils/setupSelfTr.d.ts +34 -74
  167. package/build/utils/setupSelfTr.js +79 -321
  168. package/build/utils/setupSelfTr.js.map +1 -1
  169. package/build/utils/trustCredentialStore.d.ts +86 -9
  170. package/build/utils/trustCredentialStore.js +342 -137
  171. package/build/utils/trustCredentialStore.js.map +1 -1
  172. package/build/utils/typeMetadata.d.ts +44 -0
  173. package/build/utils/typeMetadata.js +89 -0
  174. package/build/utils/typeMetadata.js.map +1 -0
  175. package/build/utils/util.d.ts +20 -0
  176. package/build/utils/util.js +32 -1
  177. package/build/utils/util.js.map +1 -1
  178. package/build/utils/vcdm2.d.ts +34 -0
  179. package/build/utils/vcdm2.js +60 -0
  180. package/build/utils/vcdm2.js.map +1 -0
  181. package/build/utils/vtjscResolver.d.ts +23 -0
  182. package/build/utils/vtjscResolver.js +53 -0
  183. package/build/utils/vtjscResolver.js.map +1 -0
  184. package/build/vtFlow/VtFlowOrchestrator.d.ts +134 -16
  185. package/build/vtFlow/VtFlowOrchestrator.js +965 -138
  186. package/build/vtFlow/VtFlowOrchestrator.js.map +1 -1
  187. package/build/vtFlow/assertVerifiableService.d.ts +31 -2
  188. package/build/vtFlow/assertVerifiableService.js +68 -10
  189. package/build/vtFlow/assertVerifiableService.js.map +1 -1
  190. package/build/vtFlow/ecsIssuanceExemption.d.ts +12 -0
  191. package/build/vtFlow/ecsIssuanceExemption.js +63 -0
  192. package/build/vtFlow/ecsIssuanceExemption.js.map +1 -0
  193. package/build/vtFlow/index.d.ts +2 -0
  194. package/build/vtFlow/index.js +2 -0
  195. package/build/vtFlow/index.js.map +1 -1
  196. package/build/vtFlow/issuanceRepair.d.ts +4 -0
  197. package/build/vtFlow/issuanceRepair.js +181 -0
  198. package/build/vtFlow/issuanceRepair.js.map +1 -0
  199. package/package.json +27 -19
  200. package/build/did/migrateWebVhLog.d.ts +0 -34
  201. package/build/did/migrateWebVhLog.js +0 -230
  202. package/build/did/migrateWebVhLog.js.map +0 -1
  203. package/build/did/migrateWebVhVersionTime.d.ts +0 -9
  204. package/build/did/migrateWebVhVersionTime.js +0 -191
  205. package/build/did/migrateWebVhVersionTime.js.map +0 -1
@@ -1,29 +1,101 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.signLinkedDataIntegrityPresentation = signLinkedDataIntegrityPresentation;
3
4
  exports.findMetadataEntry = findMetadataEntry;
4
5
  exports.saveMetadataEntry = saveMetadataEntry;
5
6
  exports.deleteMetadataEntry = deleteMetadataEntry;
6
7
  exports.createVtc = createVtc;
7
8
  exports.createJsc = createJsc;
8
9
  exports.removeTrustCredential = removeTrustCredential;
10
+ exports.findExchangeCredentialId = findExchangeCredentialId;
9
11
  exports.removeStoredTrustCredential = removeStoredTrustCredential;
10
12
  exports.migrateVtjscServiceIds = migrateVtjscServiceIds;
13
+ exports.detachVtjscPublications = detachVtjscPublications;
14
+ exports.reattachVtjscPublication = reattachVtjscPublication;
11
15
  exports.getTrustMetadata = getTrustMetadata;
16
+ exports.saveVtjscTypeMetadata = saveVtjscTypeMetadata;
17
+ exports.findVtjscTypeMetadata = findVtjscTypeMetadata;
12
18
  exports.rebindEcsCredentialSchema = rebindEcsCredentialSchema;
19
+ exports.withdrawSelfIssuedEcsCredentials = withdrawSelfIssuedEcsCredentials;
13
20
  const core_1 = require("@credo-ts/core");
21
+ // No type definitions available for this library
22
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
23
+ //@ts-expect-error
24
+ const jsonld_signatures_1 = require("jsonld-signatures");
14
25
  const verre_1 = require("@verana-labs/verre");
15
- const adminApiService_1 = require("../did/adminApiService");
26
+ const feePreflight_1 = require("../blockchain/feePreflight");
16
27
  const setupSelfTr_1 = require("./setupSelfTr");
28
+ const selfIssuedEcsCredential_1 = require("./selfIssuedEcsCredential");
29
+ const publishedDidRecord_1 = require("./publishedDidRecord");
17
30
  const data_1 = require("./data");
18
31
  async function getDidRecord(agent) {
19
32
  const [didRecord] = await agent.dids.getCreatedDids({ did: agent.did });
20
33
  return didRecord;
21
34
  }
22
- async function updateDidRecord(agent, didRecord) {
23
- const repo = agent.context.dependencyManager.resolve(core_1.DidRepository);
24
- (0, adminApiService_1.applyAdminApiServiceEntry)(didRecord.didDocument, agent.adminApiServiceEndpoint);
25
- await repo.update(agent.context, didRecord);
26
- await agent.dids.update({ did: didRecord.did, didDocument: didRecord.didDocument });
35
+ /** The credential fields shared by both data models: id, credentialSchema and credentialSubject */
36
+ function trustCredentialFields(credential) {
37
+ return credential instanceof core_1.W3cV2DataIntegrityVerifiableCredential
38
+ ? credential.resolvedCredential
39
+ : credential;
40
+ }
41
+ /**
42
+ * What the metadata entry stores and the DID Document service endpoint serves. A data model 1.1
43
+ * presentation is kept as its class instance, which serializes to JSON-LD when the DID record is
44
+ * saved; a Data Integrity one wraps the secured JSON, so that is what gets stored.
45
+ */
46
+ function trustCredentialEntry(credential) {
47
+ return credential instanceof core_1.W3cV2DataIntegrityVerifiableCredential
48
+ ? credential.securedCredential
49
+ : credential.jsonCredential;
50
+ }
51
+ function trustPresentationEntry(presentation) {
52
+ return presentation instanceof core_1.W3cV2DataIntegrityVerifiablePresentation
53
+ ? presentation.securedPresentation
54
+ : presentation;
55
+ }
56
+ function trustPresentationId(presentation) {
57
+ return presentation instanceof core_1.W3cV2DataIntegrityVerifiablePresentation
58
+ ? presentation.securedPresentation.id
59
+ : presentation.id;
60
+ }
61
+ /**
62
+ * Wraps a VC Data Model 2.0 credential in a data model 2.0 presentation secured with a
63
+ * DataIntegrityProof under the cryptosuite the agent is configured with, the counterpart of the
64
+ * linked data proof a data model 1.1 linked VP carries. The presentation is published from the DID
65
+ * Document rather than presented to a verifier, so there is no challenge to bind; its proof purpose
66
+ * is `authentication`, as Data Integrity defines for presentations.
67
+ */
68
+ async function signLinkedDataIntegrityPresentation(agent, options) {
69
+ const presentation = new core_1.W3cV2Presentation({
70
+ id: options.id,
71
+ holder: options.holder,
72
+ verifiableCredential: [options.credential],
73
+ });
74
+ // The options type requires a challenge for every presentation format, while a Data Integrity
75
+ // proof only carries one when it is given
76
+ return await agent.w3cV2Credentials.signPresentation({
77
+ format: core_1.ClaimFormat.DiVp,
78
+ presentation,
79
+ cryptosuite: (0, setupSelfTr_1.getDataIntegrityCryptosuite)(agent),
80
+ verificationMethod: options.verificationMethodId,
81
+ });
82
+ }
83
+ /**
84
+ * Wraps a VC Data Model 1.1 credential in a data model 1.1 presentation carrying an
85
+ * Ed25519Signature2020 linked data proof, for credentials received from issuers that still use it.
86
+ */
87
+ async function signLinkedDataProofPresentation(agent, options) {
88
+ return await agent.w3cCredentials.signPresentation({
89
+ format: core_1.ClaimFormat.LdpVp,
90
+ presentation: new core_1.W3cPresentation({
91
+ id: options.id,
92
+ holder: options.holder,
93
+ verifiableCredential: [options.credential],
94
+ }),
95
+ proofType: 'Ed25519Signature2020',
96
+ verificationMethod: options.verificationMethodId,
97
+ proofPurpose: new jsonld_signatures_1.purposes.AssertionProofPurpose(),
98
+ });
27
99
  }
28
100
  function findMetadataEntry(didRecord, key, id, jsonSchemaRef) {
29
101
  var _a, _b;
@@ -32,59 +104,53 @@ function findMetadataEntry(didRecord, key, id, jsonSchemaRef) {
32
104
  return null;
33
105
  if (!id && !jsonSchemaRef)
34
106
  return { schemaId: '', data: metadata, didDocumentServiceId: '' };
35
- for (const [schemaId, entry] of Object.entries(metadata)) {
36
- if (schemaId === jsonSchemaRef) {
37
- return Object.assign(Object.assign({ schemaId }, entry), { data: entry.verifiablePresentation });
38
- }
39
- const credId = (_a = entry.credential) === null || _a === void 0 ? void 0 : _a.id;
40
- const presId = (_b = entry.verifiablePresentation) === null || _b === void 0 ? void 0 : _b.id;
41
- if (credId === id) {
107
+ const entries = Object.entries(metadata);
108
+ // the metadata key identifies one entry, so it answers before any preference between entries
109
+ const exact = jsonSchemaRef ? entries.find(([schemaId]) => schemaId === jsonSchemaRef) : undefined;
110
+ if (exact)
111
+ return Object.assign(Object.assign({ schemaId: exact[0] }, exact[1]), { data: exact[1].verifiablePresentation });
112
+ const match = ([schemaId, entry]) => {
113
+ var _a, _b;
114
+ if (((_a = entry.credential) === null || _a === void 0 ? void 0 : _a.id) === id)
42
115
  return Object.assign(Object.assign({ schemaId }, entry), { data: entry.credential });
43
- }
44
- if (presId === id) {
116
+ if (((_b = entry.verifiablePresentation) === null || _b === void 0 ? void 0 : _b.id) === id)
45
117
  return Object.assign(Object.assign({ schemaId }, entry), { data: entry.verifiablePresentation });
46
- }
47
- }
48
- return null;
118
+ return null;
119
+ };
120
+ // several entries can carry the same presentation URL while a binding is replaced, so the entry
121
+ // the DID Document announces wins over a detached leftover
122
+ const matched = entries.map(match).filter(entry => entry !== null);
123
+ return (_b = (_a = matched.find(entry => entry.attached !== false)) !== null && _a !== void 0 ? _a : matched[0]) !== null && _b !== void 0 ? _b : null;
49
124
  }
50
- async function saveMetadataEntry(agent, didRecord, credential, verifiablePresentation, didDocumentServiceId, key, publicApiBaseUrl) {
51
- var _a, _b, _c, _d, _e, _f, _g, _h;
52
- const schema = key === '_vt/vtc' ? credential.credentialSchema : credential.credentialSubject;
125
+ async function saveMetadataEntry(agent, didRecord, credential, verifiablePresentation, didDocumentServiceId, key) {
126
+ var _a, _b, _c, _d, _e, _f;
127
+ const fields = trustCredentialFields(credential);
128
+ const schema = key === '_vt/vtc' ? fields.credentialSchema : fields.credentialSubject;
53
129
  const ref = Array.isArray(schema) ? (_a = schema[0]) === null || _a === void 0 ? void 0 : _a.id : schema === null || schema === void 0 ? void 0 : schema.id;
54
130
  if (!ref) {
55
131
  throw new Error('No ID was found in credentialSubject');
56
132
  }
57
133
  const record = (_b = didRecord.metadata.get(key)) !== null && _b !== void 0 ? _b : {};
58
134
  // Remove previous entry for this credential ID (if exists)
59
- const found = findMetadataEntry(didRecord, key, credential.id, ref);
135
+ const found = findMetadataEntry(didRecord, key, fields.id, ref);
136
+ // [VSA-PUB-VT-5]: the Type Metadata of a schema outlives every re-issue of its VTJSC
137
+ const typeMetadata = (found === null || found === void 0 ? void 0 : found.schemaId) === ref ? (_c = record[ref]) === null || _c === void 0 ? void 0 : _c.typeMetadata : undefined;
60
138
  if (found) {
61
- if ((_c = didRecord.didDocument) === null || _c === void 0 ? void 0 : _c.service) {
139
+ if ((_d = didRecord.didDocument) === null || _d === void 0 ? void 0 : _d.service) {
62
140
  didRecord.didDocument.service = didRecord.didDocument.service.filter(s => s.id !== found.didDocumentServiceId);
63
141
  }
64
142
  delete record[found.schemaId];
65
143
  }
66
- record[ref] = {
67
- credential: credential.jsonCredential,
68
- verifiablePresentation,
69
- didDocumentServiceId,
70
- };
71
- (_e = (_d = didRecord.didDocument) === null || _d === void 0 ? void 0 : _d.service) === null || _e === void 0 ? void 0 : _e.push(new core_1.DidDocumentService({
144
+ record[ref] = Object.assign(Object.assign({}, (typeof typeMetadata === 'string' ? { typeMetadata } : {})), { credential: trustCredentialEntry(credential), verifiablePresentation: trustPresentationEntry(verifiablePresentation), didDocumentServiceId });
145
+ (_f = (_e = didRecord.didDocument) === null || _e === void 0 ? void 0 : _e.service) === null || _f === void 0 ? void 0 : _f.push(new core_1.DidDocumentService({
72
146
  id: didDocumentServiceId,
73
- serviceEndpoint: verifiablePresentation.id,
147
+ serviceEndpoint: trustPresentationId(verifiablePresentation),
74
148
  type: 'LinkedVerifiablePresentation',
75
149
  }));
76
150
  didRecord.metadata.set(key, record);
77
- // Update #whois with new endpoint
78
- const service = (_g = (_f = didRecord.didDocument) === null || _f === void 0 ? void 0 : _f.service) === null || _g === void 0 ? void 0 : _g.find(s => s.id === `${agent.did}#whois`);
79
- if (service && ((_h = verifiablePresentation.id) === null || _h === void 0 ? void 0 : _h.includes('service')))
80
- service.serviceEndpoint = verifiablePresentation.id;
81
- // A JSON schema credential says nothing about this agent's own ECS identity, so only a real
82
- // trust credential replaces the self-issued ones.
83
- if (key === '_vt/vtc')
84
- updateVtcEntries(didRecord, false, publicApiBaseUrl);
85
- await updateDidRecord(agent, didRecord);
151
+ await (0, publishedDidRecord_1.updateDidRecord)(agent, didRecord);
86
152
  }
87
- async function deleteMetadataEntry(agent, id, didRecord, key, publicApiBaseUrl) {
153
+ async function deleteMetadataEntry(agent, id, didRecord, key) {
88
154
  const found = findMetadataEntry(didRecord, key, id, id);
89
155
  if (!found)
90
156
  return null;
@@ -93,66 +159,22 @@ async function deleteMetadataEntry(agent, id, didRecord, key, publicApiBaseUrl)
93
159
  return null;
94
160
  delete metadata[found.schemaId];
95
161
  didRecord.metadata.set(key, metadata);
96
- // If the last entry is removed, restore defaults
97
- restoreDefaultVtcEntries(didRecord, publicApiBaseUrl);
98
- await updateDidRecord(agent, didRecord);
162
+ await (0, publishedDidRecord_1.updateDidRecord)(agent, didRecord);
99
163
  return { schemaId: found.schemaId };
100
164
  }
101
- function restoreDefaultVtcEntries(didRecord, publicApiBaseUrl) {
102
- var _a, _b;
103
- const vtc = (_a = didRecord.metadata.get('_vt/vtc')) !== null && _a !== void 0 ? _a : {};
104
- const jsc = (_b = didRecord.metadata.get('_vt/jsc')) !== null && _b !== void 0 ? _b : {};
105
- // By default we have 2 Self-trusted VTCs
106
- if (Object.keys(vtc).length < 3 && Object.keys(jsc).length < 3) {
107
- updateVtcEntries(didRecord, true, publicApiBaseUrl);
108
- }
109
- }
110
- function updateVtcEntries(didRecord, attach, publicApiBaseUrl) {
111
- var _a;
112
- const record = (_a = didRecord.metadata.get('_vt/vtc')) !== null && _a !== void 0 ? _a : {};
113
- setupSelfTr_1.presentations.forEach(p => {
114
- var _a, _b, _c, _d;
115
- const schemaId = (0, setupSelfTr_1.mapToSelfTr)(p.schemaUrl, publicApiBaseUrl);
116
- const current = record[schemaId];
117
- if ((current === null || current === void 0 ? void 0 : current.attached) === attach)
118
- return;
119
- record[schemaId] = Object.assign(Object.assign({}, current), { attached: attach });
120
- const serviceId = current === null || current === void 0 ? void 0 : current.didDocumentServiceId;
121
- const serviceEndpoint = (_a = current === null || current === void 0 ? void 0 : current.verifiablePresentation) === null || _a === void 0 ? void 0 : _a.id;
122
- if (!((_b = didRecord.didDocument) === null || _b === void 0 ? void 0 : _b.service))
123
- return;
124
- if (attach) {
125
- const alreadyExists = didRecord.didDocument.service.some(s => s.id === serviceId);
126
- if (!alreadyExists && serviceId && serviceEndpoint) {
127
- didRecord.didDocument.service.push(new core_1.DidDocumentService({
128
- id: serviceId,
129
- serviceEndpoint,
130
- type: 'LinkedVerifiablePresentation',
131
- }));
132
- }
133
- // Return to self-trusted VTC in #whois endpoint
134
- const service = (_d = (_c = didRecord.didDocument) === null || _c === void 0 ? void 0 : _c.service) === null || _d === void 0 ? void 0 : _d.find(s => s.id === `${didRecord.did}#whois`);
135
- if (service && (serviceEndpoint === null || serviceEndpoint === void 0 ? void 0 : serviceEndpoint.includes('service')))
136
- service.serviceEndpoint = serviceEndpoint;
137
- }
138
- else {
139
- didRecord.didDocument.service = didRecord.didDocument.service.filter(s => s.id !== serviceId);
140
- }
141
- });
142
- didRecord.metadata.set('_vt/vtc', record);
143
- }
144
165
  async function createVtc(agent, publicApiBaseUrl, id, credential) {
145
166
  const didRecord = await getDidRecord(agent);
146
167
  const schemaId = `schemas-${id}-vtc-vp.json`;
147
168
  const didDocumentServiceId = `${agent.did}#vpr-${schemaId.replace('.json', '')}`;
148
169
  const serviceEndpoint = `${publicApiBaseUrl}/vt/${schemaId}`;
149
- const unsignedPresentation = (0, setupSelfTr_1.createPresentation)({
150
- id: serviceEndpoint,
151
- holder: agent.did,
152
- verifiableCredential: [credential],
153
- });
154
- const verifiablePresentation = await (0, setupSelfTr_1.signerW3c)(agent, unsignedPresentation, (0, setupSelfTr_1.getVerificationMethodId)(agent.config.logger, didRecord));
155
- await saveMetadataEntry(agent, didRecord, credential, verifiablePresentation, didDocumentServiceId, '_vt/vtc', publicApiBaseUrl);
170
+ const verificationMethodId = (0, setupSelfTr_1.getVerificationMethodId)(agent.config.logger, didRecord);
171
+ // The linked VP takes the data model of the credential it carries: a Data Integrity credential
172
+ // cannot be embedded in a data model 1.1 presentation, nor the other way round
173
+ const linkedVp = { id: serviceEndpoint, holder: agent.did, verificationMethodId };
174
+ const verifiablePresentation = credential instanceof core_1.W3cV2DataIntegrityVerifiableCredential
175
+ ? await signLinkedDataIntegrityPresentation(agent, Object.assign(Object.assign({}, linkedVp), { credential }))
176
+ : await signLinkedDataProofPresentation(agent, Object.assign(Object.assign({}, linkedVp), { credential }));
177
+ await saveMetadataEntry(agent, didRecord, credential, verifiablePresentation, didDocumentServiceId, '_vt/vtc');
156
178
  return verifiablePresentation;
157
179
  }
158
180
  async function createJsc(agent, publicApiBaseUrl, ecsSchemas, options) {
@@ -161,53 +183,86 @@ async function createJsc(agent, publicApiBaseUrl, ecsSchemas, options) {
161
183
  const { id: subjectId, claims } = (0, setupSelfTr_1.createJsonSubjectRef)(jsonSchemaRef);
162
184
  const subjectClaims = precomputedDigestSRI
163
185
  ? Object.assign(Object.assign({}, claims), { digestSRI: precomputedDigestSRI }) : await (0, setupSelfTr_1.addDigestSRI)(subjectId, claims, ecsSchemas);
164
- const credentialSubject = {
165
- id: subjectId,
166
- claims: subjectClaims,
167
- };
186
+ const credentialSubject = Object.assign(Object.assign({}, subjectClaims), { id: subjectId });
168
187
  const schemaPresentation = `schemas-${schemaBaseId}-vtjsc-vp.json`;
169
188
  const schemaCredential = `schemas-${schemaBaseId}-jsc.json`;
170
189
  const serviceEndpoint = `${publicApiBaseUrl}/vt/${schemaPresentation}`;
171
190
  const didDocumentServiceId = `${agent.did}#vpr-schemas-${schemaBaseId}-vtjsc-vp`;
191
+ const credentialId = `${publicApiBaseUrl}/vt/${schemaCredential}`;
172
192
  const unsignedCredential = (0, setupSelfTr_1.createCredential)({
173
- id: `${publicApiBaseUrl}/vt/${schemaCredential}`,
193
+ id: credentialId,
174
194
  type: ['VerifiableCredential', 'JsonSchemaCredential'],
175
195
  issuer: agent.did,
176
196
  credentialSubject,
197
+ credentialSchema: await (0, setupSelfTr_1.addDigestSRI)(setupSelfTr_1.createJsonSchema.id, { id: setupSelfTr_1.createJsonSchema.id, type: setupSelfTr_1.createJsonSchema.type }, ecsSchemas),
177
198
  });
178
- unsignedCredential.credentialSchema = await (0, setupSelfTr_1.addDigestSRI)(setupSelfTr_1.createJsonSchema.id, setupSelfTr_1.createJsonSchema, ecsSchemas);
179
199
  const verificationMethodId = (0, setupSelfTr_1.getVerificationMethodId)(agent.config.logger, didRecord);
180
- const credential = await (0, setupSelfTr_1.signerW3c)(agent, core_1.JsonTransformer.fromJSON(unsignedCredential, core_1.W3cCredential), verificationMethodId);
181
- const unsignedPresentation = (0, setupSelfTr_1.createPresentation)({
200
+ const credential = await (0, setupSelfTr_1.signerW3c)(agent, unsignedCredential, verificationMethodId);
201
+ const verifiablePresentation = await signLinkedDataIntegrityPresentation(agent, {
182
202
  id: serviceEndpoint,
183
203
  holder: agent.did,
184
- verifiableCredential: [credential],
204
+ credential,
205
+ verificationMethodId,
185
206
  });
186
- const verifiablePresentation = await (0, setupSelfTr_1.signerW3c)(agent, unsignedPresentation, verificationMethodId);
187
- await saveMetadataEntry(agent, didRecord, credential, verifiablePresentation, didDocumentServiceId, '_vt/jsc', publicApiBaseUrl);
188
- return credential.jsonCredential;
207
+ await saveMetadataEntry(agent, didRecord, credential, verifiablePresentation, didDocumentServiceId, '_vt/jsc');
208
+ // the credential as published; its id is the one assigned above
209
+ return credential.securedCredential;
189
210
  }
190
- async function removeTrustCredential(agent, publicApiBaseUrl, schemaId, key) {
211
+ async function removeTrustCredential(agent, schemaId, key) {
191
212
  var _a;
192
213
  const didRecord = await getDidRecord(agent);
193
- const record = findMetadataEntry(didRecord, key, schemaId);
214
+ const record = findMetadataEntry(didRecord, key, schemaId, schemaId);
194
215
  // Currently, we only use one serviceEndpoint per ID.
195
216
  // In the future, if multiple serviceEndpoints exist for the same ID,
196
217
  // we should review the serviceEndpoint content and remove only the specific one.
197
218
  if ((record === null || record === void 0 ? void 0 : record.didDocumentServiceId) && ((_a = didRecord.didDocument) === null || _a === void 0 ? void 0 : _a.service)) {
198
219
  didRecord.didDocument.service = didRecord.didDocument.service.filter(s => s.id !== record.didDocumentServiceId);
199
220
  }
200
- return await deleteMetadataEntry(agent, schemaId, didRecord, key, publicApiBaseUrl);
221
+ return await deleteMetadataEntry(agent, schemaId, didRecord, key);
201
222
  }
202
- async function removeStoredTrustCredential(agent, publicApiBaseUrl, credentialExchangeRecordId) {
203
- var _a, _b;
204
- const formatData = await agent.didcomm.credentials.getFormatData(credentialExchangeRecordId);
205
- const credentialId = (_b = (_a = formatData.credential) === null || _a === void 0 ? void 0 : _a.jsonld) === null || _b === void 0 ? void 0 : _b.id;
206
- if (!credentialId)
223
+ /**
224
+ * Reads the id of the credential from the RFC 0809 format data of an exchange.
225
+ * Returns `undefined` when the exchange is deleted. Returns `undefined` also when the exchange
226
+ * used a different format.
227
+ */
228
+ async function findExchangeCredentialId(agent, credentialExchangeRecordId) {
229
+ var _a, _b, _c;
230
+ const formatData = await agent.didcomm.credentials
231
+ .getFormatData(credentialExchangeRecordId)
232
+ .catch(error => {
233
+ if (error instanceof core_1.RecordNotFoundError)
234
+ return undefined;
235
+ throw error;
236
+ });
237
+ const credentialId = (_c = (_b = (_a = formatData === null || formatData === void 0 ? void 0 : formatData.credential) === null || _a === void 0 ? void 0 : _a.dataIntegrity) === null || _b === void 0 ? void 0 : _b.credential) === null || _c === void 0 ? void 0 : _c.id;
238
+ return typeof credentialId === 'string' ? credentialId : undefined;
239
+ }
240
+ /**
241
+ * Removes the linked VP and the stored credential of the applicant, as
242
+ * [VSA-VTI-FLOW-OP-REVOKE] requires. The caller supplies the credential id from the flow record.
243
+ * An older record has no credential id. Then this function reads the id from the RFC 0809 format
244
+ * data of the exchange. Returns the credential id. Returns `undefined` when no id is available,
245
+ * and removes nothing.
246
+ */
247
+ async function removeStoredTrustCredential(agent, options) {
248
+ var _a;
249
+ const { credentialExchangeRecordId } = options;
250
+ const credentialId = (_a = options.credentialId) !== null && _a !== void 0 ? _a : (credentialExchangeRecordId
251
+ ? await findExchangeCredentialId(agent, credentialExchangeRecordId)
252
+ : undefined);
253
+ if (!credentialId) {
254
+ agent.config.logger.warn(`[trust-credential] Credential exchange ${credentialExchangeRecordId} carries no RFC 0809 credential id; its linked VP and stored credential are left in place`);
207
255
  return undefined;
208
- await removeTrustCredential(agent, publicApiBaseUrl, credentialId, '_vt/vtc');
209
- const stored = await agent.w3cCredentials.getAll();
210
- for (const storedRecord of stored) {
256
+ }
257
+ await removeTrustCredential(agent, credentialId, '_vt/vtc');
258
+ // The RFC 0809 format holds a data model 2.0 credential in a W3cV2CredentialRecord and a data
259
+ // model 1.1 one in a W3cCredentialRecord; the credential id is tagged on both
260
+ for (const storedRecord of await agent.w3cV2Credentials.getAll()) {
261
+ if (storedRecord.getTags().givenId === credentialId) {
262
+ await agent.w3cV2Credentials.deleteById(storedRecord.id);
263
+ }
264
+ }
265
+ for (const storedRecord of await agent.w3cCredentials.getAll()) {
211
266
  if (storedRecord.getTags().givenId === credentialId) {
212
267
  await agent.w3cCredentials.deleteById(storedRecord.id);
213
268
  }
@@ -239,69 +294,219 @@ async function migrateVtjscServiceIds(agent) {
239
294
  if (!changed)
240
295
  return;
241
296
  didRecord.metadata.set('_vt/jsc', metadata);
242
- await updateDidRecord(agent, didRecord);
297
+ await (0, publishedDidRecord_1.updateDidRecord)(agent, didRecord);
243
298
  agent.config.logger.info('[VTJSC] migrated stored service ids to the -vtjsc-vp naming');
244
299
  }
300
+ /**
301
+ * Takes the linked VPs of the given `_vt/jsc` entries out of the DID Document, publishing it once.
302
+ *
303
+ * The entries stay: `SelfTrController` serves them, and issued credentials name that URL in
304
+ * `credentialSchema.id`. Dropping one would 404 it and leave those credentials unverifiable.
305
+ */
306
+ async function detachVtjscPublications(agent, schemaRefs) {
307
+ var _a, _b;
308
+ if (schemaRefs.length === 0)
309
+ return [];
310
+ const didRecord = await getDidRecord(agent);
311
+ if (!((_a = didRecord === null || didRecord === void 0 ? void 0 : didRecord.didDocument) === null || _a === void 0 ? void 0 : _a.service))
312
+ return [];
313
+ const metadata = didRecord.metadata.get('_vt/jsc');
314
+ if (!metadata)
315
+ return [];
316
+ const detached = [];
317
+ for (const schemaRef of schemaRefs) {
318
+ const serviceId = (_b = metadata[schemaRef]) === null || _b === void 0 ? void 0 : _b.didDocumentServiceId;
319
+ if (!serviceId || !didRecord.didDocument.service.some(s => s.id === serviceId))
320
+ continue;
321
+ didRecord.didDocument.service = didRecord.didDocument.service.filter(s => s.id !== serviceId);
322
+ detached.push(schemaRef);
323
+ }
324
+ if (detached.length === 0)
325
+ return [];
326
+ await (0, publishedDidRecord_1.updateDidRecord)(agent, didRecord);
327
+ return detached;
328
+ }
329
+ /**
330
+ * Puts a detached `_vt/jsc` entry back in the DID Document.
331
+ *
332
+ * The publication pass skips an entry whose digest still matches, so without this the agent would
333
+ * keep serving a VTJSC it never announces again.
334
+ */
335
+ async function reattachVtjscPublication(agent, schemaRef) {
336
+ var _a, _b, _c;
337
+ const didRecord = await getDidRecord(agent);
338
+ if (!(didRecord === null || didRecord === void 0 ? void 0 : didRecord.didDocument))
339
+ return false;
340
+ const entry = (_a = didRecord.metadata.get('_vt/jsc')) === null || _a === void 0 ? void 0 : _a[schemaRef];
341
+ const serviceId = entry === null || entry === void 0 ? void 0 : entry.didDocumentServiceId;
342
+ const serviceEndpoint = (_b = entry === null || entry === void 0 ? void 0 : entry.verifiablePresentation) === null || _b === void 0 ? void 0 : _b.id;
343
+ if (!serviceId || !serviceEndpoint)
344
+ return false;
345
+ const services = (_c = didRecord.didDocument.service) !== null && _c !== void 0 ? _c : [];
346
+ if (services.some(s => s.id === serviceId))
347
+ return false;
348
+ didRecord.didDocument.service = [
349
+ ...services,
350
+ new core_1.DidDocumentService({ id: serviceId, serviceEndpoint, type: 'LinkedVerifiablePresentation' }),
351
+ ];
352
+ await (0, publishedDidRecord_1.updateDidRecord)(agent, didRecord);
353
+ return true;
354
+ }
245
355
  function getTrustMetadata(didRecord, key, schemaId) {
246
356
  return findMetadataEntry(didRecord, key, schemaId);
247
357
  }
248
- async function anchorCredentialDigest(agent, schemaId, credential) {
249
- if (!agent.veranaChain)
358
+ /**
359
+ * Stores the serialized SD-JWT VC Type Metadata of a VTJSC with its `_vt/jsc` entry, once. A
360
+ * stored document is never replaced: [VSA-PUB-VT-5] serves identical bytes while the schema exists.
361
+ * Returns false when the entry does not exist or already carries a document.
362
+ */
363
+ async function saveVtjscTypeMetadata(agent, schemaRef, typeMetadata) {
364
+ const didRecord = await getDidRecord(agent);
365
+ const metadata = didRecord === null || didRecord === void 0 ? void 0 : didRecord.metadata.get('_vt/jsc');
366
+ const entry = metadata === null || metadata === void 0 ? void 0 : metadata[schemaRef];
367
+ if (!entry || typeof entry.typeMetadata === 'string')
368
+ return false;
369
+ entry.typeMetadata = typeMetadata;
370
+ didRecord.metadata.set('_vt/jsc', metadata);
371
+ // the DID Document does not change, so the record is saved without a DID update
372
+ const repo = agent.context.dependencyManager.resolve(core_1.DidRepository);
373
+ await repo.update(agent.context, didRecord);
374
+ return true;
375
+ }
376
+ /**
377
+ * The stored Type Metadata of the `CredentialSchema` with the given on-chain id, as serialized.
378
+ * The `_vt/jsc` key of such a schema is `vpr:verana:{chainId}:cs:{id}`.
379
+ */
380
+ function findVtjscTypeMetadata(didRecord, chainId, credentialSchemaId) {
381
+ var _a;
382
+ const entry = (_a = didRecord.metadata.get('_vt/jsc')) === null || _a === void 0 ? void 0 : _a[`vpr:verana:${chainId}:cs:${credentialSchemaId}`];
383
+ return typeof (entry === null || entry === void 0 ? void 0 : entry.typeMetadata) === 'string' ? entry.typeMetadata : undefined;
384
+ }
385
+ /**
386
+ * Anchors a self-issued ECS credential, the same way as any other issuance: through
387
+ * CreateOrUpdateParticipantSession, which stores the digest in the `di` module keeper-to-keeper.
388
+ *
389
+ * `issuerParticipantId` must name an active ISSUER participant of this agent for the schema, whose
390
+ * vs_operator is this agent's account — the chain accepts the session from no other signer. The
391
+ * caller resolves it, because it already lists the agent's participants to decide what to rebind.
392
+ */
393
+ async function anchorCredentialDigest(agent, schemaId, credential, issuerParticipantId) {
394
+ const chain = agent.veranaChain;
395
+ if (!chain)
250
396
  return;
251
397
  if (!credential)
252
398
  throw new Error(`[DigestAnchor] The presentation for schema ${schemaId} has no credential`);
253
- const schema = await agent.veranaChain.getCredentialSchema(schemaId);
399
+ if (!agent.did)
400
+ throw new Error('[DigestAnchor] The agent has no public DID');
401
+ const schema = await agent.indexer.getCredentialSchema(schemaId).catch(() => undefined);
254
402
  if (!schema)
255
403
  throw new Error(`[DigestAnchor] Credential schema ${schemaId} is not on chain`);
256
- const digest = (0, verre_1.computeCredentialDigestJCS)(core_1.JsonTransformer.toJSON(credential), schema.digestAlgorithm);
404
+ // the credential as published, which is what a verifier digests
405
+ const digest = (0, verre_1.computeCredentialDigestJCS)(credential, schema.digest_algorithm);
257
406
  // the same credential gives the same digest on each run, so an anchored digest needs no second transaction
258
- if (await agent.veranaChain.getDigest(digest))
407
+ if (await agent.indexer.getDigest(digest))
259
408
  return;
260
- const { txHash } = await agent.veranaChain.storeDigest(digest);
261
- agent.config.logger.info(`[DigestAnchor] Anchored digest ${digest} for schema ${schemaId} (tx ${txHash})`);
409
+ // A self-issued credential has no counterparty, so the session names only the issuer.
410
+ const { txHash } = await chain.createOrUpdateParticipantSession({
411
+ id: core_1.utils.uuid(),
412
+ issuerParticipantId,
413
+ agentParticipantId: 0,
414
+ walletAgentParticipantId: 0,
415
+ digest,
416
+ }, { granter: (0, feePreflight_1.feeGranterFor)(agent, issuerParticipantId) });
417
+ agent.config.logger.info(`[DigestAnchor] Anchored digest ${digest} for schema ${schemaId} against issuer participant ${issuerParticipantId} (tx ${txHash})`);
418
+ await agent.indexer.waitForDigest(digest);
262
419
  }
263
420
  // replaces the self-TR example JSC binding with the on-chain VTJSC so resolvers can link the credential to the VPR
264
- async function rebindEcsCredentialSchema(agent, publicApiBaseUrl, schemaId, schemaKey, defaults) {
265
- var _a, _b, _c, _d;
266
- if (!['ecs-service', 'ecs-org'].includes(schemaKey) || !agent.did)
421
+ /**
422
+ * @param jsonSchemaCredentialId the VTJSC the Ecosystem published for this schema. Only the
423
+ * Ecosystem publishes it, so an agent that issues against another Corporation's Ecosystem must
424
+ * resolve it there — see resolveJsonSchemaCredentialId.
425
+ * @param issuerParticipantId this agent's ISSUER participant for the schema, which anchors the
426
+ * credential digest.
427
+ * @param schemaKey the ECS schema of a self-issued credential.
428
+ */
429
+ async function rebindEcsCredentialSchema(agent, publicApiBaseUrl, schemaId, schemaKey, ecsClaims, jsonSchemaCredentialId, issuerParticipantId, onChainJsonSchema, effectiveUntil) {
430
+ var _a, _b, _c, _d, _e, _f, _g, _h;
431
+ if (!agent.did)
267
432
  return;
268
433
  const vpUrl = `${publicApiBaseUrl}/vt/${schemaKey}-vtc-vp.json`;
269
- const jscUrl = `${publicApiBaseUrl}/vt/schemas-${schemaId}-jsc.json`;
434
+ const jscUrl = jsonSchemaCredentialId;
270
435
  const didRecord = await getDidRecord(agent);
271
436
  const record = (_a = didRecord.metadata.get('_vt/vtc')) !== null && _a !== void 0 ? _a : {};
437
+ const stored = record[jscUrl];
438
+ const storedIssuer = typeof ((_b = stored === null || stored === void 0 ? void 0 : stored.credential) === null || _b === void 0 ? void 0 : _b.issuer) === 'string' ? stored.credential.issuer : (_d = (_c = stored === null || stored === void 0 ? void 0 : stored.credential) === null || _c === void 0 ? void 0 : _c.issuer) === null || _d === void 0 ? void 0 : _d.id;
439
+ if ((stored === null || stored === void 0 ? void 0 : stored.credential) && !stored.integrityData && storedIssuer !== agent.did) {
440
+ agent.config.logger.debug(`[SelfTR] Keeping the ${schemaKey} credential issued by ${storedIssuer} instead of rebinding a self-issued one`);
441
+ return;
442
+ }
272
443
  let removedStale = false;
273
444
  for (const [key, entry] of Object.entries(record)) {
274
445
  // findMetadataEntry serves the first entry matching the VP url, so stale bindings must go
275
446
  if (key !== jscUrl &&
276
- ((_b = entry.verifiablePresentation) === null || _b === void 0 ? void 0 : _b.id) === vpUrl) {
447
+ ((_e = entry.verifiablePresentation) === null || _e === void 0 ? void 0 : _e.id) === vpUrl) {
277
448
  delete record[key];
278
449
  removedStale = true;
279
450
  }
280
451
  }
281
452
  if (removedStale) {
282
453
  didRecord.metadata.set('_vt/vtc', record);
283
- await updateDidRecord(agent, didRecord);
454
+ await (0, publishedDidRecord_1.updateDidRecord)(agent, didRecord);
284
455
  }
285
- await (0, setupSelfTr_1.generateVerifiablePresentation)(agent, vpUrl, (0, data_1.getEcsSchemas)(publicApiBaseUrl), schemaKey, ['VerifiableCredential', 'VerifiableTrustCredential'], { id: jscUrl, type: 'JsonSchemaCredential' }, defaults, async (verifiablePresentation) => {
286
- var _a;
287
- return await anchorCredentialDigest(agent, Number(schemaId), (_a = verifiablePresentation === null || verifiablePresentation === void 0 ? void 0 : verifiablePresentation.verifiableCredential) === null || _a === void 0 ? void 0 : _a[0]);
288
- });
456
+ await (0, selfIssuedEcsCredential_1.publishSelfIssuedEcsPresentation)(agent, vpUrl, Object.assign(Object.assign({}, (0, data_1.getEcsSchemas)(publicApiBaseUrl)), (onChainJsonSchema ? { [schemaKey]: onChainJsonSchema } : {})), schemaKey, schemaId, ['VerifiableCredential', 'VerifiableTrustCredential'], { id: jscUrl, type: 'JsonSchemaCredential' }, ecsClaims, async (verifiablePresentation) => await anchorCredentialDigest(agent, Number(schemaId), verifiablePresentation.verifiableCredential[0], issuerParticipantId), effectiveUntil);
289
457
  const freshRecord = await getDidRecord(agent);
458
+ let recordChanged = false;
459
+ const vtc = (_f = freshRecord.metadata.get('_vt/vtc')) !== null && _f !== void 0 ? _f : {};
460
+ const entry = vtc[jscUrl];
461
+ if (entry && (entry.issuerParticipantId !== issuerParticipantId || entry.schemaKey !== schemaKey)) {
462
+ vtc[jscUrl] = Object.assign(Object.assign({}, entry), { issuerParticipantId, schemaKey });
463
+ freshRecord.metadata.set('_vt/vtc', vtc);
464
+ recordChanged = true;
465
+ }
290
466
  const doc = freshRecord.didDocument;
291
467
  if (doc) {
292
468
  // resolvers match the [VT-CRED-W3C-LINKED-VP] fragment; #whois alone is not enough
293
469
  const expectedServiceId = `${agent.did}#${(0, setupSelfTr_1.linkedVpFragment)(schemaKey)}`;
294
- const linked = (_c = doc.service) === null || _c === void 0 ? void 0 : _c.some(s => s.id === expectedServiceId);
470
+ const linked = (_g = doc.service) === null || _g === void 0 ? void 0 : _g.some(s => s.id === expectedServiceId);
295
471
  if (!linked) {
296
- doc.service = (_d = doc.service) !== null && _d !== void 0 ? _d : [];
472
+ doc.service = (_h = doc.service) !== null && _h !== void 0 ? _h : [];
297
473
  doc.service.push(new core_1.DidDocumentService({
298
474
  id: expectedServiceId,
299
475
  serviceEndpoint: vpUrl,
300
476
  type: 'LinkedVerifiablePresentation',
301
477
  }));
302
- await updateDidRecord(agent, freshRecord);
478
+ recordChanged = true;
303
479
  }
480
+ if (recordChanged)
481
+ await (0, publishedDidRecord_1.updateDidRecord)(agent, freshRecord);
304
482
  }
305
483
  agent.config.logger.info(`[SelfTR] Rebound ${schemaKey} credential to VTJSC ${jscUrl}`);
306
484
  }
485
+ /**
486
+ * Withdraws the ECS credentials anchored against `issuerParticipantId`, and the #whois entry
487
+ * when the Service credential is among them.
488
+ */
489
+ async function withdrawSelfIssuedEcsCredentials(agent, issuerParticipantId) {
490
+ var _a, _b;
491
+ const didRecord = await getDidRecord(agent);
492
+ const withdrawn = [];
493
+ let whoisWithdrawn = false;
494
+ for (const [jscUrl, value] of Object.entries((_a = didRecord.metadata.get('_vt/vtc')) !== null && _a !== void 0 ? _a : {})) {
495
+ const entry = value;
496
+ if (entry.issuerParticipantId !== issuerParticipantId)
497
+ continue;
498
+ await removeTrustCredential(agent, jscUrl, '_vt/vtc');
499
+ withdrawn.push(jscUrl);
500
+ if (entry.schemaKey === 'ecs-service')
501
+ whoisWithdrawn = true;
502
+ }
503
+ if (whoisWithdrawn) {
504
+ const record = await getDidRecord(agent);
505
+ if ((_b = record.didDocument) === null || _b === void 0 ? void 0 : _b.service) {
506
+ record.didDocument.service = record.didDocument.service.filter(s => s.id !== `${agent.did}#whois`);
507
+ await (0, publishedDidRecord_1.updateDidRecord)(agent, record);
508
+ }
509
+ }
510
+ return withdrawn;
511
+ }
307
512
  //# sourceMappingURL=trustCredentialStore.js.map