@trustvc/trustvc 1.8.0 → 2.0.0
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.
- package/README.md +144 -48
- package/dist/cjs/core/documentBuilder.js +38 -14
- package/dist/cjs/open-attestation/utils.js +0 -2
- package/dist/cjs/token-registry-functions/returnToken.js +1 -1
- package/dist/cjs/utils/documents/index.js +27 -0
- package/dist/cjs/verify/fragments/document-integrity/ecdsaW3CSignatureIntegrity.js +87 -0
- package/dist/cjs/verify/fragments/document-integrity/w3cSignatureIntegrity.js +2 -2
- package/dist/cjs/verify/fragments/index.js +5 -0
- package/dist/cjs/verify/verify.js +2 -0
- package/dist/cjs/w3c/derive.js +11 -0
- package/dist/cjs/w3c/index.js +7 -0
- package/dist/cjs/w3c/sign.js +2 -2
- package/dist/esm/core/documentBuilder.js +39 -15
- package/dist/esm/open-attestation/utils.js +1 -2
- package/dist/esm/token-registry-functions/returnToken.js +1 -1
- package/dist/esm/utils/documents/index.js +26 -1
- package/dist/esm/verify/fragments/document-integrity/ecdsaW3CSignatureIntegrity.js +85 -0
- package/dist/esm/verify/fragments/document-integrity/w3cSignatureIntegrity.js +1 -1
- package/dist/esm/verify/fragments/index.js +1 -0
- package/dist/esm/verify/verify.js +2 -0
- package/dist/esm/w3c/derive.js +9 -0
- package/dist/esm/w3c/index.js +1 -0
- package/dist/esm/w3c/sign.js +2 -2
- package/dist/types/core/documentBuilder.d.ts +14 -4
- package/dist/types/core/index.d.ts +1 -1
- package/dist/types/{index-Bc5NlE8f.d.ts → index-1ws_BWZW.d.ts} +2 -2
- package/dist/types/{index-CRVFHzes.d.ts → index-LpXMEhhr.d.ts} +3 -1
- package/dist/types/index.d.ts +7 -5
- package/dist/types/open-attestation/index.d.ts +1 -2
- package/dist/types/open-attestation/utils.d.ts +1 -3
- package/dist/types/utils/documents/index.d.ts +4 -2
- package/dist/types/utils/index.d.ts +2 -2
- package/dist/types/verify/fragments/document-integrity/ecdsaW3CSignatureIntegrity.d.ts +5 -0
- package/dist/types/verify/fragments/index.d.ts +1 -0
- package/dist/types/verify/index.d.ts +2 -1
- package/dist/types/w3c/derive.d.ts +11 -0
- package/dist/types/w3c/index.d.ts +2 -1
- package/dist/types/w3c/sign.d.ts +7 -3
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -16,15 +16,16 @@ TrustVC is a comprehensive wrapper library designed to simplify the signing and
|
|
|
16
16
|
- [2. **Signing**](#2-signing)
|
|
17
17
|
- [a) OpenAttestation Signing (signOA) v2 v3](#a-openattestation-signing-signoa-v2-v3)
|
|
18
18
|
- [b) TrustVC W3C Signing (signW3C)](#b-trustvc-w3c-signing-signw3c)
|
|
19
|
-
- [3. **
|
|
20
|
-
- [4. **
|
|
21
|
-
- [5. **
|
|
22
|
-
- [6. **
|
|
19
|
+
- [3. **Deriving (Selective Disclosure)**](#3-deriving-selective-disclosure)
|
|
20
|
+
- [4. **Verifying**](#4-verifying)
|
|
21
|
+
- [5. **Encryption**](#5-encryption)
|
|
22
|
+
- [6. **Decryption**](#6-decryption)
|
|
23
|
+
- [7. **TradeTrust Token Registry**](#7-tradetrust-token-registry)
|
|
23
24
|
- [Usage](#usage-2)
|
|
24
25
|
- [TradeTrustToken](#tradetrusttoken)
|
|
25
26
|
- [a) Token Registry v4](#a-token-registry-v4)
|
|
26
27
|
- [b) Token Registry V5](#b-token-registry-v5)
|
|
27
|
-
- [
|
|
28
|
+
- [8. **Document Builder**](#8-document-builder)
|
|
28
29
|
|
|
29
30
|
## Installation
|
|
30
31
|
|
|
@@ -154,15 +155,17 @@ const signedWrappedDocument = await signOA(wrappedDocument, {
|
|
|
154
155
|
|
|
155
156
|
#### b) TrustVC W3C Signing (signW3C)
|
|
156
157
|
|
|
158
|
+
The `signW3C` function signs W3C Verifiable Credentials using the provided cryptographic suite and key pair. By default, it uses the **ecdsa-sd-2023** crypto suite unless otherwise specified.
|
|
159
|
+
|
|
157
160
|
```ts
|
|
158
161
|
import { signW3C, VerificationType } from '@trustvc/trustvc';
|
|
159
162
|
|
|
160
163
|
const rawDocument = {
|
|
161
164
|
'@context': [
|
|
162
|
-
'https://www.w3.org/
|
|
163
|
-
'https://
|
|
164
|
-
'https://w3id.org/security/bbs/v1',
|
|
165
|
+
'https://www.w3.org/ns/credentials/v2',
|
|
166
|
+
'https://w3id.org/security/data-integrity/v2',
|
|
165
167
|
'https://w3id.org/vc/status-list/2021/v1',
|
|
168
|
+
'https://w3c-ccg.github.io/citizenship-vocab/contexts/citizenship-v2.jsonld',
|
|
166
169
|
],
|
|
167
170
|
credentialStatus: {
|
|
168
171
|
id: 'https://trustvc.github.io/did/credentials/statuslist/1#1',
|
|
@@ -172,29 +175,113 @@ const rawDocument = {
|
|
|
172
175
|
statusListCredential: 'https://trustvc.github.io/did/credentials/statuslist/1',
|
|
173
176
|
},
|
|
174
177
|
credentialSubject: {
|
|
175
|
-
|
|
178
|
+
type: ['Person']
|
|
179
|
+
givenName: 'TrustVC',
|
|
176
180
|
birthDate: '2024-04-01T12:19:52Z',
|
|
177
|
-
type: ['PermanentResident', 'Person'],
|
|
178
181
|
},
|
|
179
|
-
expirationDate: '2029-12-03T12:19:52Z',
|
|
180
182
|
issuer: 'did:web:trustvc.github.io:did:1',
|
|
181
183
|
type: ['VerifiableCredential'],
|
|
182
|
-
|
|
184
|
+
validFrom: '2024-04-01T12:19:52Z',
|
|
185
|
+
validUntil: '2029-12-03T12:19:52Z'
|
|
183
186
|
};
|
|
184
187
|
|
|
188
|
+
// Using default ecdsa-sd-2023 crypto suite
|
|
185
189
|
const signingResult = await signW3C(rawDocument, {
|
|
186
|
-
|
|
190
|
+
'@context': 'https://w3id.org/security/multikey/v1',
|
|
191
|
+
id: 'did:web:trustvc.github.io:did:1#multikey-1',
|
|
192
|
+
type: VerificationType.Multikey,
|
|
187
193
|
controller: 'did:web:trustvc.github.io:did:1',
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
194
|
+
publicKeyMultibase: 'zDnaemDNwi4G5eTzGfRooFFu5Kns3be6yfyVNtiaMhWkZbwtc',
|
|
195
|
+
secretKeyMultibase: '<secretKeyMultibase>'
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// You can also specify mandatory pointers for selective disclosure with ecdsa-sd-2023
|
|
199
|
+
const signingResultWithPointers = await signW3C(
|
|
200
|
+
rawDocument,
|
|
201
|
+
{
|
|
202
|
+
'@context': 'https://w3id.org/security/multikey/v1',
|
|
203
|
+
id: 'did:web:trustvc.github.io:did:1#multikey-1',
|
|
204
|
+
type: VerificationType.Multikey,
|
|
205
|
+
controller: 'did:web:trustvc.github.io:did:1',
|
|
206
|
+
publicKeyMultibase: 'zDnaemDNwi4G5eTzGfRooFFu5Kns3be6yfyVNtiaMhWkZbwtc',
|
|
207
|
+
secretKeyMultibase: '<secretKeyMultibase>'
|
|
208
|
+
},
|
|
209
|
+
'ecdsa-sd-2023',
|
|
210
|
+
{
|
|
211
|
+
mandatoryPointers: ['/credentialStatus']
|
|
212
|
+
}
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
// Alternatively, specify a different crypto suite. Ensure the context is updated to include the crypto suite.
|
|
216
|
+
const signingResultWithBbs = await signW3C(
|
|
217
|
+
rawDocument,
|
|
218
|
+
{
|
|
219
|
+
id: 'did:web:trustvc.github.io:did:1#keys-1',
|
|
220
|
+
controller: 'did:web:trustvc.github.io:did:1',
|
|
221
|
+
type: VerificationType.Bls12381G2Key2020,
|
|
222
|
+
publicKeyBase58: 'oRfEeWFresvhRtXCkihZbxyoi2JER7gHTJ5psXhHsdCoU1MttRMi3Yp9b9fpjmKh7bMgfWKLESiK2YovRd8KGzJsGuamoAXfqDDVhckxuc9nmsJ84skCSTijKeU4pfAcxeJ',
|
|
223
|
+
privateKeyBase58: '<privateKeyBase58>',
|
|
224
|
+
},
|
|
225
|
+
'BbsBlsSignature2020'
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
### 3. **Deriving (Selective Disclosure)**
|
|
233
|
+
|
|
234
|
+
> When using ECDSA-SD-2023 crypto suite, we can derive a new credential with selective disclosure. This means you can choose which parts of the credential to reveal while keeping others hidden.
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
import { deriveW3C } from '@trustvc/trustvc';
|
|
238
|
+
|
|
239
|
+
// This is a signed document using ecdsa-sd-2023
|
|
240
|
+
const signedDocument = {
|
|
241
|
+
'@context': [
|
|
242
|
+
'https://www.w3.org/ns/credentials/v2',
|
|
243
|
+
'https://w3id.org/security/data-integrity/v2',
|
|
244
|
+
'https://w3id.org/vc/status-list/2021/v1',
|
|
245
|
+
'https://w3c-ccg.github.io/citizenship-vocab/contexts/citizenship-v2.jsonld'
|
|
246
|
+
],
|
|
247
|
+
credentialStatus: {
|
|
248
|
+
id: 'https://trustvc.github.io/did/credentials/statuslist/1#1',
|
|
249
|
+
type: 'StatusList2021Entry',
|
|
250
|
+
statusPurpose: 'revocation',
|
|
251
|
+
statusListIndex: '10',
|
|
252
|
+
statusListCredential: 'https://trustvc.github.io/did/credentials/statuslist/1'
|
|
253
|
+
},
|
|
254
|
+
credentialSubject: {
|
|
255
|
+
type: ['Person'],
|
|
256
|
+
givenName: 'TrustVC',
|
|
257
|
+
birthDate: '2024-04-01T12:19:52Z'
|
|
258
|
+
},
|
|
259
|
+
issuer: 'did:web:trustvc.github.io:did:1',
|
|
260
|
+
type: ['VerifiableCredential'],
|
|
261
|
+
validFrom: '2024-04-01T12:19:52Z',
|
|
262
|
+
validUntil: '2029-12-03T12:19:52Z',
|
|
263
|
+
id: 'urn:uuid:0198bd9e-6686-7ccd-9b2a-ce763ae710d7',
|
|
264
|
+
proof: {
|
|
265
|
+
type: 'DataIntegrityProof',
|
|
266
|
+
created: '2025-08-18T14:38:51Z',
|
|
267
|
+
verificationMethod: 'did:web:trustvc.github.io:did:1#multikey-1',
|
|
268
|
+
cryptosuite: 'ecdsa-sd-2023',
|
|
269
|
+
proofPurpose: 'assertionMethod',
|
|
270
|
+
proofValue: 'u2V0AhVhAxfLFkbv8J_O3zJAQrSWrEY3sgeMwN02b2eaHEgjnJYu1rnCBYORfZUVZwRoRuNIiY1NTGHmQpzlgqtQz7A0R3FgjgCQDzt3_aUvSMrlIZdsyVcB4KPHHjA4BbSv-PZ4Bbm4GpY5YIA1mQ8LYmpjJ7vNvN3DsfIengZrnziTLO9exbZjn1KqFilhA0lp1y6BZ-fhiUdWsojYesLDSzCy6Tq_AICaIvCjYSJMEaY7SomJnCkdpuhM0GQHDTy5kjzb7sSzowACqDDf9OVhAfOC7vg4WQGrI6M3dvLZW3KlBzp1SurRz1PPeHcqOGEDrqybzIlolwNXMhc2T8rcVLl-E04wNsiVjamvqWAQN-lhA4HmVqIxKuR0QvCMEVq3cjUU7G1pQbgMdp9HZDasOT9nh_k5l3JfcXB1_qtRblljXWN0FRKAr9T-DhxzDzGl3-lhA4nNDzd-6xl74rWqr_7U9XZE7LoE-mbgBsyOAOlfHGumMxwddnEZp2iD2uZ7lLXX8Q-nSDXJVvUqKLksy1l2vqVhAm3daNYjH1kVrTW7V-DElcj3K_QfbHEvjd1F2TGVGtBVhF8o01yCxXRX0vzk-AZLZnpDnAUBTSTF5Q8rF-t7L9lhAO7NeIXQtQsdncqtLm2qk1XzFYL2FM5Hx4GZOX39VyT4T0AlFRZQuY9WXYnvMZSvacRvJaSJk5S3cZ6uBminQgVhAExuTEvJQu42-SiaOJ_6M0EjuQfqIgJE-JHirmYs3AAoH_4EKUtPU3y_jRB8XFZxA-wtFDv3KJjqXtNo5aA_6f1hAaokZPSJghFufTaVR8LAwHpXOncGJblKpUZQjKWuA_o2s6tGmx-ja0wgpsqSxvAGMTtkhFTMOI2-tzUuGE05tk1hAzABtV2yEX-RAQFpxkuV0XydAsJDh2dPscrpPHqMfmORsC3xRNL73uDaqqlaL99CvOgq4kJWmChw7TUYO62yaSVhA5-F-snwj-OZtws7_qMwvBgeNK9wvkZTlFLjRV6GDYx6r5TaLkR05GVzyBMv0Qs2z-cXPRZByS7p7_hbeykoYSYJnL2lzc3VlcmovdmFsaWRGcm9t'
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
// Derive a new credential with only specific fields disclosed
|
|
275
|
+
const derivationResult = await deriveW3C(signedDocument, {
|
|
276
|
+
// Only reveal the credential type and givenName, hide birthDate
|
|
277
|
+
selectivePointers: ['/type', '/credentialSubject/givenName']
|
|
192
278
|
});
|
|
279
|
+
|
|
193
280
|
```
|
|
194
281
|
|
|
195
282
|
---
|
|
196
283
|
|
|
197
|
-
###
|
|
284
|
+
### 4. **Verifying**
|
|
198
285
|
|
|
199
286
|
> TrustVC simplifies the verification process with a single function that supports both W3C Verifiable Credentials (VCs) and OpenAttestation Verifiable Documents (VDs). Whether you're working with W3C standards or OpenAttestation standards, TrustVC handles the verification seamlessly.
|
|
200
287
|
|
|
@@ -239,7 +326,7 @@ const resultFragments = await verifyDocument(signedDocument);
|
|
|
239
326
|
|
|
240
327
|
---
|
|
241
328
|
|
|
242
|
-
###
|
|
329
|
+
### 5. **Encryption**
|
|
243
330
|
|
|
244
331
|
> The `encrypt` function encrypts plaintext messages using the **ChaCha20** encryption algorithm, ensuring the security and integrity of the input data. It supports custom keys and nonces, returning the encrypted message in hexadecimal format.
|
|
245
332
|
|
|
@@ -316,7 +403,7 @@ It also relies on the `ts-chacha20` library for encryption operations.
|
|
|
316
403
|
|
|
317
404
|
---
|
|
318
405
|
|
|
319
|
-
###
|
|
406
|
+
### 6. **Decryption**
|
|
320
407
|
|
|
321
408
|
> The `decrypt` function decrypts messages encrypted with the **ChaCha20** algorithm. It converts the input from a hexadecimal format back into plaintext using the provided key and nonce.
|
|
322
409
|
|
|
@@ -399,7 +486,7 @@ It also relies on the `ts-chacha20` library for decryption operations.
|
|
|
399
486
|
|
|
400
487
|
---
|
|
401
488
|
|
|
402
|
-
###
|
|
489
|
+
### 7. **TradeTrust Token Registry**
|
|
403
490
|
|
|
404
491
|
> The Electronic Bill of Lading (eBL) is a digital document that can be used to prove the ownership of goods. It is a standardized document that is accepted by all major shipping lines and customs authorities. The [Token Registry](https://github.com/TradeTrust/token-registry) repository contains both the smart contract (v4 and v5) code for token registry (in `/contracts`) as well as the node package for using this library (in `/src`).
|
|
405
492
|
> The TrustVC library not only simplifies signing and verification but also imports and integrates existing TradeTrust libraries and smart contracts for token registry (V4 and V5), making it a versatile tool for decentralized identity and trust solutions.
|
|
@@ -589,8 +676,8 @@ function rejectTransferOwners(bytes calldata _remark) external;
|
|
|
589
676
|
|
|
590
677
|
For more information on Token Registry and Title Escrow contracts **version v5**, please visit the readme of [TradeTrust Token Registry V5](https://github.com/TradeTrust/token-registry/blob/master/README.md)
|
|
591
678
|
|
|
592
|
-
###
|
|
593
|
-
> The `DocumentBuilder` class helps build and manage W3C Verifiable Credentials (VCs) with credential status features. It supports creating documents with two types of credential statuses: `transferableRecords` and `verifiableDocument`. It can sign the document using a private key, verify its signature, and serialize the document to a JSON format. Additionally, it allows for configuration of document rendering methods and expiration dates.
|
|
679
|
+
### 8. **Document Builder**
|
|
680
|
+
> The `DocumentBuilder` class helps build and manage W3C Verifiable Credentials (VCs) with credential status features, implementing the **W3C VC Data Model 2.0** specification. It supports creating documents with two types of credential statuses: `transferableRecords` and `verifiableDocument`. It can sign the document using a private key, verify its signature, and serialize the document to a JSON format. Additionally, it allows for configuration of document rendering methods and expiration dates.
|
|
594
681
|
|
|
595
682
|
#### Usage
|
|
596
683
|
|
|
@@ -603,7 +690,7 @@ To learn more about defining custom contexts, check out the [Credential Subject
|
|
|
603
690
|
// Adds a custom vocabulary used to define terms in the `credentialSubject`.
|
|
604
691
|
// Users can define their own context if they have domain-specific fields or custom data structures.
|
|
605
692
|
const builder = new DocumentBuilder({
|
|
606
|
-
'@context': 'https://w3c-ccg.github.io/citizenship-vocab/contexts/citizenship-
|
|
693
|
+
'@context': 'https://w3c-ccg.github.io/citizenship-vocab/contexts/citizenship-v2.jsonld'
|
|
607
694
|
});
|
|
608
695
|
```
|
|
609
696
|
|
|
@@ -612,8 +699,8 @@ Set the subject of the Verifiable Credential, which typically contains informati
|
|
|
612
699
|
|
|
613
700
|
```ts
|
|
614
701
|
builder.credentialSubject({
|
|
615
|
-
|
|
616
|
-
|
|
702
|
+
type: ['Person'],
|
|
703
|
+
givenName: 'TrustVC',
|
|
617
704
|
});
|
|
618
705
|
```
|
|
619
706
|
|
|
@@ -649,7 +736,7 @@ builder.credentialStatus({
|
|
|
649
736
|
```
|
|
650
737
|
|
|
651
738
|
##### Set Expiration Date
|
|
652
|
-
You can set
|
|
739
|
+
You can set a valid until date (expiration) for the document.
|
|
653
740
|
|
|
654
741
|
```ts
|
|
655
742
|
builder.expirationDate('2026-01-01T00:00:00Z');
|
|
@@ -677,16 +764,17 @@ builder.qrCode({
|
|
|
677
764
|
```
|
|
678
765
|
|
|
679
766
|
##### Sign the Document
|
|
680
|
-
To sign the document, provide a `PrivateKeyPair` from `@trustvc/trustvc`.
|
|
767
|
+
To sign the document, provide a `PrivateKeyPair` from `@trustvc/trustvc`. The builder uses ECDSA key for signing by default.
|
|
681
768
|
|
|
682
769
|
```ts
|
|
683
770
|
const privateKey: PrivateKeyPair = {
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
771
|
+
'@context': 'https://w3id.org/security/multikey/v1',
|
|
772
|
+
id: 'did:web:example.com#multikey-1',
|
|
773
|
+
type: VerificationType.Multikey,
|
|
774
|
+
controller: 'did:web:example.com',
|
|
775
|
+
publicKeyMultibase: 'your-public-key-multibase',
|
|
776
|
+
secretKeyMultibase: 'your-secret-key-multibase',
|
|
777
|
+
}
|
|
690
778
|
|
|
691
779
|
const signedDocument = await builder.sign(privateKey);
|
|
692
780
|
console.log(signedDocument);
|
|
@@ -696,19 +784,18 @@ Example Output After Signing
|
|
|
696
784
|
```json
|
|
697
785
|
{
|
|
698
786
|
"@context": [
|
|
699
|
-
"https://www.w3.org/
|
|
700
|
-
"https://w3c-ccg.github.io/citizenship-vocab/contexts/citizenship-
|
|
701
|
-
"https://
|
|
702
|
-
"https://trustvc.io/context/render-method-context.json",
|
|
787
|
+
"https://www.w3.org/ns/credentials/v2",
|
|
788
|
+
"https://w3c-ccg.github.io/citizenship-vocab/contexts/citizenship-v2.jsonld",
|
|
789
|
+
"https://trustvc.io/context/render-method-context-v2.json",
|
|
703
790
|
"https://trustvc.io/context/qrcode-context.json",
|
|
704
|
-
"https://w3id.org/security/
|
|
791
|
+
"https://w3id.org/security/data-integrity/v2"
|
|
705
792
|
],
|
|
706
793
|
"type": ["VerifiableCredential"],
|
|
707
794
|
"credentialSubject": {
|
|
708
|
-
"
|
|
709
|
-
"
|
|
795
|
+
"type": ["Person"],
|
|
796
|
+
"givenName": "TrustVC",
|
|
710
797
|
},
|
|
711
|
-
"
|
|
798
|
+
"validUntil": "2026-01-01T00:00:00Z",
|
|
712
799
|
"renderMethod": [
|
|
713
800
|
{
|
|
714
801
|
"id": "https://example.com/rendering-method",
|
|
@@ -727,21 +814,30 @@ Example Output After Signing
|
|
|
727
814
|
"statusListIndex": "<placeholder>",
|
|
728
815
|
"statusListCredential": "https://example.com/status-list"
|
|
729
816
|
},
|
|
730
|
-
"issuer": "did:example
|
|
731
|
-
"
|
|
817
|
+
"issuer": "did:web:example.com",
|
|
818
|
+
"validFrom": "2025-01-01T00:00:00Z",
|
|
732
819
|
"id": "urn:bnid:_:0195fec2-4ae1-7cca-9182-03fd7da5142b",
|
|
733
820
|
"proof": {
|
|
734
|
-
"type": "
|
|
821
|
+
"type": "DataIntegrityProof",
|
|
735
822
|
"created": "2025-01-01T00:00:01Z",
|
|
823
|
+
"verificationMethod": "did:web:example.com#multikey-1",
|
|
824
|
+
"cryptosuite": "ecdsa-sd-2023",
|
|
736
825
|
"proofPurpose": "assertionMethod",
|
|
737
|
-
"proofValue": "
|
|
738
|
-
"verificationMethod": "did:example:456#key1"
|
|
826
|
+
"proofValue": "u2V0AhVhAh1oLoiuV2AwmSa2ZspbmrG2gCDbpZW.......",
|
|
739
827
|
}
|
|
740
828
|
}
|
|
741
829
|
```
|
|
742
830
|
|
|
831
|
+
##### Deriving the Document
|
|
832
|
+
Provide the attributes to reveal to the `derive` method.
|
|
833
|
+
|
|
834
|
+
```ts
|
|
835
|
+
const derivedDocument = await builder.derive(['/credentialSubject/givenName']);
|
|
836
|
+
console.log(derivedDocument);
|
|
837
|
+
```
|
|
838
|
+
|
|
743
839
|
##### Verify the Document
|
|
744
|
-
To verify the signature of the signed document
|
|
840
|
+
To verify the signature of the signed document, ensure the document is derived first and then call the `verify` method.
|
|
745
841
|
|
|
746
842
|
```ts
|
|
747
843
|
const isVerified = await builder.verify();
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var w3cIssuer = require('@trustvc/w3c-issuer');
|
|
3
4
|
var w3c = require('../w3c');
|
|
4
5
|
var w3cCredentialStatus = require('@trustvc/w3c-credential-status');
|
|
5
6
|
var w3cVc = require('@trustvc/w3c-vc');
|
|
@@ -9,6 +10,7 @@ var tokenRegistryV5$1 = require('@tradetrust-tt/token-registry-v5');
|
|
|
9
10
|
var tokenRegistryV4 = require('../token-registry-v4');
|
|
10
11
|
var tokenRegistryV5 = require('../token-registry-v5');
|
|
11
12
|
var utils = require('../utils');
|
|
13
|
+
var w3cContext = require('@trustvc/w3c-context');
|
|
12
14
|
|
|
13
15
|
var __defProp = Object.defineProperty;
|
|
14
16
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
@@ -30,6 +32,8 @@ class DocumentBuilder {
|
|
|
30
32
|
// Required fields that must be present in the document.
|
|
31
33
|
isSigned = false;
|
|
32
34
|
// Tracks if a document is signed
|
|
35
|
+
isDerived = false;
|
|
36
|
+
// Tracks if a document is derived
|
|
33
37
|
/**
|
|
34
38
|
* Constructor to initialize the document builder.
|
|
35
39
|
* @param {Partial<VerifiableCredential>} input - The input document.
|
|
@@ -63,18 +67,17 @@ class DocumentBuilder {
|
|
|
63
67
|
tokenRegistry: config.tokenRegistry
|
|
64
68
|
};
|
|
65
69
|
this.rpcProviderUrl = config.rpcProviderUrl;
|
|
66
|
-
this.addContext(
|
|
70
|
+
this.addContext(w3cContext.TR_CONTEXT_URL);
|
|
67
71
|
} else if (isVerifiable) {
|
|
68
72
|
this.selectedStatusType = "verifiableDocument";
|
|
69
73
|
this.statusConfig = {
|
|
70
74
|
id: `${config.url}#${config.index}`,
|
|
71
|
-
type: "
|
|
75
|
+
type: "BitstringStatusListEntry",
|
|
72
76
|
statusPurpose: config.purpose || "revocation",
|
|
73
77
|
// Set status purpose to "revocation" by default.
|
|
74
78
|
statusListIndex: config.index,
|
|
75
79
|
statusListCredential: config.url
|
|
76
80
|
};
|
|
77
|
-
this.addContext("https://w3id.org/vc/status-list/2021/v1");
|
|
78
81
|
} else {
|
|
79
82
|
throw new Error("Configuration Error: Missing required fields for credential status.");
|
|
80
83
|
}
|
|
@@ -83,25 +86,25 @@ class DocumentBuilder {
|
|
|
83
86
|
// Sets the expiration date of the document.
|
|
84
87
|
expirationDate(date) {
|
|
85
88
|
if (this.isSigned) throw new Error("Configuration Error: Document is already signed.");
|
|
86
|
-
this.document.
|
|
89
|
+
this.document.validUntil = typeof date === "string" ? date : date.toISOString();
|
|
87
90
|
return this;
|
|
88
91
|
}
|
|
89
92
|
// Defines the rendering method for the document.
|
|
90
93
|
renderMethod(method) {
|
|
91
94
|
if (this.isSigned) throw new Error("Configuration Error: Document is already signed.");
|
|
92
95
|
this.document.renderMethod = [method];
|
|
93
|
-
this.addContext(
|
|
96
|
+
this.addContext(w3cContext.RENDER_CONTEXT_V2_URL);
|
|
94
97
|
return this;
|
|
95
98
|
}
|
|
96
99
|
// Defines the qrcode for the document.
|
|
97
100
|
qrCode(method) {
|
|
98
101
|
if (this.isSigned) throw new Error("Configuration Error: Document is already signed.");
|
|
99
102
|
this.document.qrCode = method;
|
|
100
|
-
this.addContext(
|
|
103
|
+
this.addContext(w3cContext.QRCODE_CONTEXT_URL);
|
|
101
104
|
return this;
|
|
102
105
|
}
|
|
103
106
|
// Sign the document using the provided private key and an optional cryptographic suite.
|
|
104
|
-
async sign(privateKey, cryptoSuite) {
|
|
107
|
+
async sign(privateKey, cryptoSuite, options) {
|
|
105
108
|
if (this.isSigned) throw new Error("Configuration Error: Document is already signed.");
|
|
106
109
|
if (this.selectedStatusType) {
|
|
107
110
|
this.document.credentialStatus = this.statusConfig;
|
|
@@ -119,16 +122,36 @@ class DocumentBuilder {
|
|
|
119
122
|
await this.verifyTokenRegistry();
|
|
120
123
|
}
|
|
121
124
|
this.document.issuer = privateKey.id.split("#")[0];
|
|
122
|
-
this.document.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
+
this.document.validFrom = this.document.validFrom || (/* @__PURE__ */ new Date()).toISOString();
|
|
126
|
+
if (!cryptoSuite || cryptoSuite === "ecdsa-sd-2023") {
|
|
127
|
+
this.addContext(w3cContext.DATA_INTEGRITY_V2_URL);
|
|
128
|
+
} else {
|
|
129
|
+
this.addContext(w3cContext.BBS_V1_URL);
|
|
130
|
+
}
|
|
131
|
+
const signedVC = await w3c.signW3C(this.document, privateKey, cryptoSuite, options);
|
|
125
132
|
if (signedVC.error) throw new Error(`Signing Error: ${signedVC.error}`);
|
|
126
133
|
this.isSigned = true;
|
|
127
134
|
return signedVC.signed;
|
|
128
135
|
}
|
|
136
|
+
async derive(revealedAttributes) {
|
|
137
|
+
if (!this.isSigned) throw new Error("Configuration Error: Document is not signed yet.");
|
|
138
|
+
if (this.isDerived) throw new Error("Configuration Error: Document is already derived.");
|
|
139
|
+
const derivedCredential = await w3c.deriveW3C(
|
|
140
|
+
this.document,
|
|
141
|
+
revealedAttributes
|
|
142
|
+
);
|
|
143
|
+
if (derivedCredential.error) throw new Error(`Derivation Error: ${derivedCredential.error}`);
|
|
144
|
+
this.document = derivedCredential.derived;
|
|
145
|
+
this.isDerived = true;
|
|
146
|
+
return derivedCredential.derived;
|
|
147
|
+
}
|
|
129
148
|
// Verify the document.
|
|
130
149
|
async verify() {
|
|
131
150
|
if (!this.isSigned) throw new Error("Verification Error: Document is not signed yet.");
|
|
151
|
+
const cryptosuite = this.document?.proof?.cryptosuite;
|
|
152
|
+
if (cryptosuite === w3cIssuer.CryptoSuite.EcdsaSd2023 && !this.isDerived) {
|
|
153
|
+
throw new Error("Verification Error: Document is not derived yet. Use derive() first.");
|
|
154
|
+
}
|
|
132
155
|
const verificationResult = await w3c.verifyW3CSignature(
|
|
133
156
|
this.document
|
|
134
157
|
);
|
|
@@ -167,10 +190,11 @@ class DocumentBuilder {
|
|
|
167
190
|
}
|
|
168
191
|
// Private helper method to build the context for the document, ensuring uniqueness and adding the default W3C context.
|
|
169
192
|
buildContext(context) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
193
|
+
const arrayContext = Array.isArray(context) ? context : context ? [context] : [];
|
|
194
|
+
if (arrayContext.includes(w3cContext.VC_V1_URL)) {
|
|
195
|
+
throw new Error("Document builder does not support data model v1.1.");
|
|
196
|
+
}
|
|
197
|
+
return [w3cContext.VC_V2_URL, ...arrayContext].filter((v, i, a) => a.indexOf(v) === i);
|
|
174
198
|
}
|
|
175
199
|
// Private helper method to add a new context to the document if it does not already exist.
|
|
176
200
|
addContext(context) {
|
|
@@ -12,7 +12,6 @@ const {
|
|
|
12
12
|
isSignedWrappedV3Document,
|
|
13
13
|
isRawV2Document,
|
|
14
14
|
isRawV3Document,
|
|
15
|
-
isObfuscated,
|
|
16
15
|
getDocumentData,
|
|
17
16
|
getIssuerAddress,
|
|
18
17
|
diagnose,
|
|
@@ -49,7 +48,6 @@ exports.getDocumentData = getDocumentData;
|
|
|
49
48
|
exports.getIssuerAddress = getIssuerAddress;
|
|
50
49
|
exports.getTemplateURL = getTemplateURL;
|
|
51
50
|
exports.isDocumentRevokable = isDocumentRevokable;
|
|
52
|
-
exports.isObfuscated = isObfuscated;
|
|
53
51
|
exports.isRawV2Document = isRawV2Document;
|
|
54
52
|
exports.isRawV3Document = isRawV3Document;
|
|
55
53
|
exports.isSignedWrappedV2Document = isSignedWrappedV2Document;
|
|
@@ -187,7 +187,7 @@ const acceptReturned = /* @__PURE__ */ __name(async (contractOptions, signer, pa
|
|
|
187
187
|
const encryptedRemarks = remarks && isV5TT ? `0x${core.encrypt(remarks, options.id)}` : "0x";
|
|
188
188
|
try {
|
|
189
189
|
const isV6 = ethers.isV6EthersProvider(signer.provider);
|
|
190
|
-
const args = isV5TT ? [encryptedRemarks] : [];
|
|
190
|
+
const args = isV5TT ? [tokenId, encryptedRemarks] : [tokenId];
|
|
191
191
|
if (isV6) {
|
|
192
192
|
await tradeTrustTokenContract.burn.staticCall(...args);
|
|
193
193
|
} else {
|
|
@@ -73,9 +73,36 @@ const getChainId = /* @__PURE__ */ __name((document) => {
|
|
|
73
73
|
return void 0;
|
|
74
74
|
}
|
|
75
75
|
}, "getChainId");
|
|
76
|
+
const isObfuscated = /* @__PURE__ */ __name((document) => {
|
|
77
|
+
if (openAttestation.isWrappedV3Document(document)) {
|
|
78
|
+
return !!document.proof.privacy?.obfuscated?.length;
|
|
79
|
+
}
|
|
80
|
+
if (openAttestation.isWrappedV2Document(document)) {
|
|
81
|
+
return !!document.privacy?.obfuscatedData?.length;
|
|
82
|
+
}
|
|
83
|
+
if (vc.isSignedDocument(document)) {
|
|
84
|
+
return document.proof?.type === "BbsBlsSignatureProof2020";
|
|
85
|
+
}
|
|
86
|
+
throw new Error(
|
|
87
|
+
"Unsupported document type: Can only check if there are obfuscated data from wrapped OpenAttestation v2, v3 documents and signed verifiable credentials."
|
|
88
|
+
);
|
|
89
|
+
}, "isObfuscated");
|
|
90
|
+
const getObfuscatedData = /* @__PURE__ */ __name((document) => {
|
|
91
|
+
if (openAttestation.isWrappedV3Document(document)) {
|
|
92
|
+
return document.proof.privacy?.obfuscated;
|
|
93
|
+
}
|
|
94
|
+
if (openAttestation.isWrappedV2Document(document)) {
|
|
95
|
+
return document.privacy?.obfuscatedData || [];
|
|
96
|
+
}
|
|
97
|
+
throw new Error(
|
|
98
|
+
"Unsupported document type: Can only retrieve obfuscated data from wrapped OpenAttestation v2 & v3 documents."
|
|
99
|
+
);
|
|
100
|
+
}, "getObfuscatedData");
|
|
76
101
|
|
|
77
102
|
exports.getChainId = getChainId;
|
|
103
|
+
exports.getObfuscatedData = getObfuscatedData;
|
|
78
104
|
exports.getTokenId = getTokenId;
|
|
79
105
|
exports.getTokenRegistryAddress = getTokenRegistryAddress;
|
|
80
106
|
exports.getTransferableRecordsCredentialStatus = getTransferableRecordsCredentialStatus;
|
|
107
|
+
exports.isObfuscated = isObfuscated;
|
|
81
108
|
exports.isTransferableRecord = isTransferableRecord;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var verify = require('../../../w3c/verify');
|
|
4
|
+
var w3cVc = require('@trustvc/w3c-vc');
|
|
5
|
+
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
const PROOF_TYPE = "DataIntegrityProof";
|
|
9
|
+
const CRYPTOSUITE = "ecdsa-sd-2023";
|
|
10
|
+
const DERIVE_CREDENTIAL_ERROR = "Use deriveCredential() first";
|
|
11
|
+
function isSignedVerifiableCredential(document) {
|
|
12
|
+
return typeof document === "object" && document !== null && "proof" in document;
|
|
13
|
+
}
|
|
14
|
+
__name(isSignedVerifiableCredential, "isSignedVerifiableCredential");
|
|
15
|
+
const ecdsaW3CSignatureIntegrity = {
|
|
16
|
+
skip: /* @__PURE__ */ __name(async () => {
|
|
17
|
+
return {
|
|
18
|
+
type: "DOCUMENT_INTEGRITY",
|
|
19
|
+
name: "EcdsaW3CSignatureIntegrity",
|
|
20
|
+
reason: {
|
|
21
|
+
code: 0,
|
|
22
|
+
codeString: "SKIPPED",
|
|
23
|
+
message: `Document either has no proof or proof type is not '${PROOF_TYPE}' or proof cryptosuite is not '${CRYPTOSUITE}'.`
|
|
24
|
+
},
|
|
25
|
+
status: "SKIPPED"
|
|
26
|
+
};
|
|
27
|
+
}, "skip"),
|
|
28
|
+
test: /* @__PURE__ */ __name((document) => {
|
|
29
|
+
const doc = document;
|
|
30
|
+
return doc.proof?.type === "DataIntegrityProof" && doc.proof?.cryptosuite === "ecdsa-sd-2023";
|
|
31
|
+
}, "test"),
|
|
32
|
+
verify: /* @__PURE__ */ __name(async (document, verifierOptions) => {
|
|
33
|
+
if (!isSignedVerifiableCredential(document)) {
|
|
34
|
+
return {
|
|
35
|
+
type: "DOCUMENT_INTEGRITY",
|
|
36
|
+
name: "EcdsaW3CSignatureIntegrity",
|
|
37
|
+
data: false,
|
|
38
|
+
reason: {
|
|
39
|
+
message: "Document is not a valid SignedVerifiableCredential"
|
|
40
|
+
},
|
|
41
|
+
status: "INVALID"
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
let verificationResult = await verify.verifyW3CSignature(document, verifierOptions);
|
|
46
|
+
let isDerived = true;
|
|
47
|
+
if (!verificationResult.verified && verificationResult.error?.includes(DERIVE_CREDENTIAL_ERROR)) {
|
|
48
|
+
const derivedCredential = await w3cVc.deriveCredential(document, []);
|
|
49
|
+
verificationResult = await verify.verifyW3CSignature(derivedCredential.derived, verifierOptions);
|
|
50
|
+
isDerived = false;
|
|
51
|
+
}
|
|
52
|
+
if (verificationResult.verified) {
|
|
53
|
+
return {
|
|
54
|
+
type: "DOCUMENT_INTEGRITY",
|
|
55
|
+
name: "EcdsaW3CSignatureIntegrity",
|
|
56
|
+
data: true,
|
|
57
|
+
reason: {
|
|
58
|
+
message: isDerived ? "Document verified successfully" : "Document verified after derivation"
|
|
59
|
+
},
|
|
60
|
+
status: "VALID"
|
|
61
|
+
};
|
|
62
|
+
} else {
|
|
63
|
+
return {
|
|
64
|
+
type: "DOCUMENT_INTEGRITY",
|
|
65
|
+
name: "EcdsaW3CSignatureIntegrity",
|
|
66
|
+
data: false,
|
|
67
|
+
reason: {
|
|
68
|
+
message: verificationResult.error || "Verification failed"
|
|
69
|
+
},
|
|
70
|
+
status: "INVALID"
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
} catch (error) {
|
|
74
|
+
return {
|
|
75
|
+
type: "DOCUMENT_INTEGRITY",
|
|
76
|
+
name: "EcdsaW3CSignatureIntegrity",
|
|
77
|
+
data: false,
|
|
78
|
+
reason: {
|
|
79
|
+
message: error instanceof Error ? error.message : "Unknown verification error"
|
|
80
|
+
},
|
|
81
|
+
status: "INVALID"
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}, "verify")
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
exports.ecdsaW3CSignatureIntegrity = ecdsaW3CSignatureIntegrity;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var verify = require('../../../w3c/verify');
|
|
4
4
|
|
|
5
5
|
var __defProp = Object.defineProperty;
|
|
6
6
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
@@ -23,7 +23,7 @@ const w3cSignatureIntegrity = {
|
|
|
23
23
|
}, "test"),
|
|
24
24
|
verify: /* @__PURE__ */ __name(async (document, verifierOptions) => {
|
|
25
25
|
const doc = document;
|
|
26
|
-
const verificationResult = await
|
|
26
|
+
const verificationResult = await verify.verifyW3CSignature(doc, verifierOptions);
|
|
27
27
|
if (verificationResult.verified) {
|
|
28
28
|
return {
|
|
29
29
|
type: "DOCUMENT_INTEGRITY",
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var ttVerify = require('@tradetrust-tt/tt-verify');
|
|
4
4
|
var w3cSignatureIntegrity = require('./document-integrity/w3cSignatureIntegrity');
|
|
5
|
+
var ecdsaW3CSignatureIntegrity = require('./document-integrity/ecdsaW3CSignatureIntegrity');
|
|
5
6
|
var transferableRecordVerifier = require('./document-status/transferableRecords/transferableRecordVerifier');
|
|
6
7
|
var w3cCredentialStatus = require('./document-status/w3cCredentialStatus');
|
|
7
8
|
var w3cIssuerIdentity = require('./issuer-identity/w3cIssuerIdentity');
|
|
@@ -41,6 +42,10 @@ Object.defineProperty(exports, "w3cSignatureIntegrity", {
|
|
|
41
42
|
enumerable: true,
|
|
42
43
|
get: function () { return w3cSignatureIntegrity.w3cSignatureIntegrity; }
|
|
43
44
|
});
|
|
45
|
+
Object.defineProperty(exports, "ecdsaW3CSignatureIntegrity", {
|
|
46
|
+
enumerable: true,
|
|
47
|
+
get: function () { return ecdsaW3CSignatureIntegrity.ecdsaW3CSignatureIntegrity; }
|
|
48
|
+
});
|
|
44
49
|
Object.defineProperty(exports, "TRANSFERABLE_RECORDS_TYPE", {
|
|
45
50
|
enumerable: true,
|
|
46
51
|
get: function () { return transferableRecordVerifier.TRANSFERABLE_RECORDS_TYPE; }
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var ttVerify = require('@tradetrust-tt/tt-verify');
|
|
4
4
|
var w3cSignatureIntegrity = require('./fragments/document-integrity/w3cSignatureIntegrity');
|
|
5
|
+
var ecdsaW3CSignatureIntegrity = require('./fragments/document-integrity/ecdsaW3CSignatureIntegrity');
|
|
5
6
|
var transferableRecordVerifier = require('./fragments/document-status/transferableRecords/transferableRecordVerifier');
|
|
6
7
|
var w3cCredentialStatus = require('./fragments/document-status/w3cCredentialStatus');
|
|
7
8
|
var w3cIssuerIdentity = require('./fragments/issuer-identity/w3cIssuerIdentity');
|
|
@@ -33,6 +34,7 @@ const openAttestationVerifiers = [
|
|
|
33
34
|
];
|
|
34
35
|
const w3cVerifiers = [
|
|
35
36
|
w3cSignatureIntegrity.w3cSignatureIntegrity,
|
|
37
|
+
ecdsaW3CSignatureIntegrity.ecdsaW3CSignatureIntegrity,
|
|
36
38
|
w3cCredentialStatus.w3cCredentialStatus,
|
|
37
39
|
transferableRecordVerifier.credentialStatusTransferableRecordVerifier,
|
|
38
40
|
fragments.w3cEmptyCredentialStatus,
|