@cooperation/vc-storage 1.0.32 → 1.0.33
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.
@@ -248,6 +248,11 @@ export class CredentialEngine {
|
|
248
248
|
try {
|
249
249
|
let keyPair;
|
250
250
|
let didDocument;
|
251
|
+
// Check if VC already exists
|
252
|
+
const existing = await this.storage.checkEmailExists(email);
|
253
|
+
if (existing) {
|
254
|
+
return { signedVC: existing.data, fileId: existing.id };
|
255
|
+
}
|
251
256
|
// Require SEED from environment
|
252
257
|
if (!encodedSeed) {
|
253
258
|
throw new Error('SEED environment variable not set. Cannot generate or use any DID.');
|
@@ -641,4 +641,40 @@ export class GoogleDriveStorage {
|
|
641
641
|
return [];
|
642
642
|
}
|
643
643
|
}
|
644
|
+
/**
|
645
|
+
* Check if an email VC exists and return its content
|
646
|
+
* @param email - The email address to check
|
647
|
+
* @returns {Promise<{data: any, id: string} | null>} - The email VC content and ID if exists, null otherwise
|
648
|
+
*/
|
649
|
+
async checkEmailExists(email) {
|
650
|
+
try {
|
651
|
+
// Get root folders
|
652
|
+
const rootFolders = await this.findFolders();
|
653
|
+
// Find Credentials folder
|
654
|
+
const credentialsFolder = rootFolders.find((f) => f.name === 'Credentials');
|
655
|
+
if (!credentialsFolder) {
|
656
|
+
console.log('Credentials folder not found');
|
657
|
+
return null;
|
658
|
+
}
|
659
|
+
// Find EMAIL_VC subfolder
|
660
|
+
const subfolders = await this.findFolders(credentialsFolder.id);
|
661
|
+
const emailVcFolder = subfolders.find((f) => f.name === 'EMAIL_VC');
|
662
|
+
if (!emailVcFolder) {
|
663
|
+
console.log('EMAIL_VC folder not found');
|
664
|
+
return null;
|
665
|
+
}
|
666
|
+
// Search for file with exact email name (no extension)
|
667
|
+
const files = await this.searchFiles(`'${emailVcFolder.id}' in parents and name='${email}' and mimeType='application/json'`);
|
668
|
+
if (files.length === 0) {
|
669
|
+
return null;
|
670
|
+
}
|
671
|
+
// Get the content of the email VC
|
672
|
+
const emailVC = await this.retrieve(files[0].id);
|
673
|
+
return emailVC;
|
674
|
+
}
|
675
|
+
catch (error) {
|
676
|
+
console.error('Error checking email existence:', error);
|
677
|
+
return null;
|
678
|
+
}
|
679
|
+
}
|
644
680
|
}
|
package/dist/tests/email.test.js
CHANGED
@@ -11,7 +11,12 @@ async function testEmailVC() {
|
|
11
11
|
const testEmail = 'test@example.com';
|
12
12
|
console.log('Starting email VC generation test...');
|
13
13
|
console.log('Test email:', testEmail);
|
14
|
-
|
14
|
+
// First check if email VC exists
|
15
|
+
console.log('\nChecking if email VC exists...');
|
16
|
+
const existingVC = await storage.checkEmailExists(testEmail);
|
17
|
+
console.log('🚀 ~ testEmailVC ~ existingVC:', existingVC);
|
18
|
+
console.log('Existing VC check:', existingVC ? 'Found' : 'Not found');
|
19
|
+
const encodedSeed = 'z1AdiEjvNdC18HdruehySWKe4HnsXdUqCXMYPEs1fQ8cY2S'; // Replace with your actual encoded seed
|
15
20
|
// Generate and sign the email VC
|
16
21
|
const result = await engine.generateAndSignEmailVC(testEmail, encodedSeed);
|
17
22
|
console.log('\nTest Results:');
|
@@ -22,6 +27,19 @@ async function testEmailVC() {
|
|
22
27
|
console.log('\nRetrieving VC from storage...');
|
23
28
|
const retrievedVC = await storage.retrieve(result.fileId);
|
24
29
|
console.log('Retrieved VC:', retrievedVC ? 'Success' : 'Failed');
|
30
|
+
// Test checkEmailExists again after creation
|
31
|
+
console.log('\nChecking if email VC exists after creation...');
|
32
|
+
const newExistingVC = await storage.checkEmailExists(testEmail);
|
33
|
+
console.log('Existing VC check after creation:', newExistingVC ? 'Found' : 'Not found');
|
34
|
+
if (newExistingVC) {
|
35
|
+
console.log('VC Content:', JSON.stringify(newExistingVC.data, null, 2));
|
36
|
+
console.log('VC ID:', newExistingVC.id);
|
37
|
+
}
|
38
|
+
// Test with non-existent email
|
39
|
+
console.log('\nTesting with non-existent email...');
|
40
|
+
const nonExistentEmail = 'nonexistent@example.com';
|
41
|
+
const nonExistentVC = await storage.checkEmailExists(nonExistentEmail);
|
42
|
+
console.log('Non-existent email check:', nonExistentVC ? 'Found (unexpected)' : 'Not found (expected)');
|
25
43
|
console.log('\nTest completed successfully!');
|
26
44
|
}
|
27
45
|
catch (error) {
|
@@ -90,5 +90,14 @@ export declare class GoogleDriveStorage {
|
|
90
90
|
update(fileId: string, data: any): Promise<any>;
|
91
91
|
getFileIdsFromAppDataFolder(): Promise<any>;
|
92
92
|
getAllFilesData(): Promise<any>;
|
93
|
+
/**
|
94
|
+
* Check if an email VC exists and return its content
|
95
|
+
* @param email - The email address to check
|
96
|
+
* @returns {Promise<{data: any, id: string} | null>} - The email VC content and ID if exists, null otherwise
|
97
|
+
*/
|
98
|
+
checkEmailExists(email: string): Promise<{
|
99
|
+
data: any;
|
100
|
+
id: string;
|
101
|
+
} | null>;
|
93
102
|
}
|
94
103
|
export {};
|