@sphereon/ssi-sdk.sd-jwt 0.32.1-feature.IATAB2B.52.190 → 0.32.1-feature.MWALL.715.121

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 (37) hide show
  1. package/dist/action-handler.d.ts.map +1 -1
  2. package/dist/action-handler.js +34 -4
  3. package/dist/action-handler.js.map +1 -1
  4. package/dist/defaultCallbacks.d.ts +2 -2
  5. package/dist/defaultCallbacks.d.ts.map +1 -1
  6. package/dist/defaultCallbacks.js.map +1 -1
  7. package/dist/types.d.ts +5 -4
  8. package/dist/types.d.ts.map +1 -1
  9. package/dist/types.js.map +1 -1
  10. package/dist/utils.d.ts +15 -2
  11. package/dist/utils.d.ts.map +1 -1
  12. package/dist/utils.js +29 -4
  13. package/dist/utils.js.map +1 -1
  14. package/package.json +14 -14
  15. package/src/__tests__/resources/BoardingPassCredential-vct.json +264 -0
  16. package/src/__tests__/resources/LoyaltyProgram Account VC Schema V0.1 sd-jwt-schema.json +97 -0
  17. package/src/__tests__/resources/LoyaltyProgramAccountCredential-vct.json +152 -0
  18. package/src/__tests__/resources/boarding Pass VC Schema V1.0 sd-jwt.json +156 -0
  19. package/src/__tests__/resources/boardingpass-logo.png +0 -0
  20. package/src/__tests__/resources/boardingpass.svg +1 -0
  21. package/src/__tests__/resources/e-passport.svg +1 -0
  22. package/src/__tests__/resources/ePassport VC Schema V1.0.sd-jwt.json +303 -0
  23. package/src/__tests__/resources/ePassportCredential-vct.json +226 -0
  24. package/src/__tests__/resources/epassport-logo.png +0 -0
  25. package/src/__tests__/resources/loyaltyprogramaccount-icon.png +0 -0
  26. package/src/__tests__/resources/loyaltyprogramaccount.png +0 -0
  27. package/src/__tests__/resources/loyaltyprogramaccount.svg +1 -0
  28. package/src/__tests__/resources/travel-agency VC Employee v0.1 sd-jwt-schema.json +115 -0
  29. package/src/__tests__/resources/travel-agency-EmployeeAgencyCredential-vct.json +181 -0
  30. package/src/__tests__/resources/travel-agency-vc-employee-logo.png +0 -0
  31. package/src/__tests__/resources/travel-agency-vc-employee.svg +1 -0
  32. package/src/__tests__/sd-jwt-integrity.test.ts +100 -0
  33. package/src/__tests__/sd-jwt.test.ts +2 -3
  34. package/src/action-handler.ts +48 -14
  35. package/src/defaultCallbacks.ts +2 -3
  36. package/src/types.ts +5 -4
  37. package/src/utils.ts +48 -4
package/src/utils.ts CHANGED
@@ -1,5 +1,7 @@
1
- import { AsyncHasher, SdJwtTypeMetadata } from '@sphereon/ssi-types'
1
+ import { SdJwtTypeMetadata } from '@sphereon/ssi-types'
2
2
  import * as u8a from 'uint8arrays'
3
+ import { HasherSync } from '@sd-jwt/types/src/type'
4
+ import { Hasher } from '@sd-jwt/types'
3
5
 
4
6
  // Helper function to fetch API with error handling
5
7
  export async function fetchUrlWithErrorHandling(url: string): Promise<Response> {
@@ -10,9 +12,51 @@ export async function fetchUrlWithErrorHandling(url: string): Promise<Response>
10
12
  return response
11
13
  }
12
14
 
13
- export async function validateIntegrity(input: any, integrityValue: string, hasher: AsyncHasher, alg?: string): Promise<boolean> {
14
- const hash = await hasher(input, alg ?? 'sha256')
15
- return u8a.toString(hash, 'utf-8') === integrityValue
15
+ export type IntegrityAlg = 'sha256' | 'sha384' | 'sha512'
16
+
17
+ function extractHashAlgFromIntegrity(integrityValue?: string): IntegrityAlg | undefined {
18
+ const val = integrityValue?.toLowerCase().trim().split('-')[0]
19
+ if (val === 'sha256' || val === 'sha384' || val === 'sha512') {
20
+ return val as IntegrityAlg
21
+ }
22
+ return undefined
23
+ }
24
+
25
+ export function extractHashFromIntegrity(integrityValue?: string): string | undefined {
26
+ return integrityValue?.toLowerCase().trim().split('-')[1]
27
+ }
28
+
29
+ export async function validateIntegrity({
30
+ input,
31
+ integrityValue,
32
+ hasher,
33
+ }: {
34
+ input: any
35
+ integrityValue?: string
36
+ hasher: HasherSync | Hasher
37
+ }): Promise<boolean> {
38
+ if (!integrityValue) {
39
+ return true
40
+ }
41
+ const alg = extractHashAlgFromIntegrity(integrityValue)
42
+ if (!alg) {
43
+ return false
44
+ }
45
+ const calculatedHash = await createIntegrity({ hasher, input, alg })
46
+ return calculatedHash == integrityValue
47
+ }
48
+
49
+ export async function createIntegrity({
50
+ input,
51
+ hasher,
52
+ alg = 'sha256',
53
+ }: {
54
+ input: any
55
+ hasher: HasherSync | Hasher
56
+ alg?: IntegrityAlg
57
+ }): Promise<string> {
58
+ const calculatedHash = await hasher(typeof input === 'string' ? input : JSON.stringify(input), alg)
59
+ return `${alg}-${u8a.toString(calculatedHash, 'base64')}`
16
60
  }
17
61
 
18
62
  export function assertValidTypeMetadata(metadata: SdJwtTypeMetadata, vct: string): void {