-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignSkillClaim.ts
More file actions
67 lines (60 loc) · 2.11 KB
/
signSkillClaim.ts
File metadata and controls
67 lines (60 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
import {
CredentialEngine,
GoogleDriveStorage,
saveToGoogleDrive
} from '@cooperation/vc-storage'
import type { ISkillClaimCredential } from 'hr-context'
import type { FormData } from '../[formType]/form/types/Types'
import { normalizeSkillClaimFormData, SkillClaimFormData } from './normalization/hrContextSkillClaim'
/**
* Sign a SkillClaimCredential using the HR Context data model.
* Uses the shared storage and engine. Creates a DID if keyPair/issuerId are not provided.
*
* @param storage - Shared GoogleDriveStorage instance (e.g. from useGoogleDrive)
* @param engine - Shared CredentialEngine instance (e.g. from getCredentialEngine)
* @param input - Form data + skills (or pre-built SkillClaimFormData)
* @param options - Optional keyPair and issuerId (if already have DID); saveToDrive to persist
* @returns The signed SkillClaimCredential
*/
export async function signSkillClaim(
storage: GoogleDriveStorage,
engine: CredentialEngine,
formData: FormData,
options?: {
keyPair?: any
issuerId?: string
saveToDrive?: boolean
}
): Promise<ISkillClaimCredential | { signedVC: ISkillClaimCredential; file: any }> {
if (!storage || !engine)
throw new Error('Storage and CredentialEngine are required.')
const normalizedData: SkillClaimFormData = normalizeSkillClaimFormData(formData)
let keyPair = options?.keyPair
let issuerId = options?.issuerId
if (!keyPair || !issuerId) {
const { didDocument, keyPair: kp } = await engine.createDID()
keyPair = kp
issuerId = didDocument.id
normalizedData.personId = normalizedData.personId ?? issuerId
}
if (!issuerId) throw new Error('Issuer DID is required.')
try {
const signedVC = await engine.signSkillClaimVC(
normalizedData as unknown as ISkillClaimCredential,
keyPair,
issuerId
)
if (options?.saveToDrive) {
const file = await saveToGoogleDrive({
storage,
data: signedVC,
type: 'VC'
})
return { signedVC, file }
}
return signedVC
} catch (error) {
console.error('🚀 ~ signSkillClaim ~ error:', JSON.stringify(error, null, 2))
throw error
}
}