@sphereon/ssi-types 0.30.1-unstable.4 → 0.30.1

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.
@@ -1,2 +1,2 @@
1
- export * from './credential-mapper'
2
- export * from './jsonld-language-values'
1
+ export * from './credential-mapper'
2
+ export * from './jsonld-language-values'
@@ -1,91 +1,91 @@
1
- import { ObjectUtils } from '../utils'
2
-
3
- export type LanguageValueClaim = {
4
- language: string
5
- value: string | string[] | number | number[]
6
- }
7
-
8
- export const isLanguageValueObject = (claim?: unknown): claim is LanguageValueClaim => {
9
- if (!claim || !ObjectUtils.isObject(claim) || Array.isArray(claim)) {
10
- return false
11
- }
12
- const keys = Object.keys(claim)
13
- if (keys.length !== 2) {
14
- return false // Only 'language' and 'value' for now
15
- } else if (!('language' in claim && !!claim.language)) {
16
- return false
17
- } else if (!('value' in claim && !!claim.value)) {
18
- return false
19
- }
20
- return true
21
- }
22
-
23
- export const isLanguageValueObjects = (claim?: unknown): claim is LanguageValueClaim[] => {
24
- if (!claim || !Array.isArray(claim)) {
25
- return false
26
- }
27
- return claim.every((val) => isLanguageValueObject(val))
28
- }
29
-
30
- export const toLanguageValueObject = (claim?: unknown): LanguageValueClaim | undefined => {
31
- return isLanguageValueObject(claim) ? claim : undefined
32
- }
33
-
34
- export const toLanguageValueObjects = (claim?: unknown): LanguageValueClaim[] | undefined => {
35
- if (isLanguageValueObject(claim)) {
36
- return ObjectUtils.asArray(toLanguageValueObject(claim) as LanguageValueClaim)
37
- } else if (isLanguageValueObjects(claim)) {
38
- return claim
39
- }
40
- return undefined // no empty array on purpose, as this really would not be a language value object
41
- }
42
-
43
- export const mapLanguageValue = (
44
- claim?: unknown,
45
- opts?: {
46
- language?: string
47
- fallbackToFirstObject?: boolean
48
- },
49
- ): any => {
50
- const langValues = toLanguageValueObjects(claim)
51
- if (Array.isArray(langValues)) {
52
- if (langValues.length === 0) {
53
- // should not happen, but let's return original claim to be sure
54
- return claim
55
- }
56
- const filteredLangValues = langValues.filter((val) => (opts?.language ? val.language.toLowerCase().includes(opts.language.toLowerCase()) : true))
57
-
58
- let langValue: LanguageValueClaim
59
- if (filteredLangValues.length > 0) {
60
- langValue = filteredLangValues[0]
61
- } else {
62
- if (opts?.fallbackToFirstObject === false) {
63
- // No match and we also do not fallback to the first value, so return the original claim
64
- return claim
65
- }
66
- // Fallback to the first value
67
- langValue = langValues[0]
68
- }
69
- return langValue.value
70
- }
71
-
72
- return claim
73
- }
74
-
75
- export const mapLanguageValues = <T extends object>(
76
- claimsOrCredential: T,
77
- opts?: {
78
- language?: string
79
- fallbackToFirstObject?: boolean
80
- noDeepClone?: boolean
81
- },
82
- ): T => {
83
- const result = opts?.noDeepClone ? claimsOrCredential : JSON.parse(JSON.stringify(claimsOrCredential))
84
- Object.keys(claimsOrCredential).forEach((key) => {
85
- result[key] = mapLanguageValue(result[key], opts)
86
- if (ObjectUtils.isObject(result[key]) || Array.isArray(result[key])) {
87
- result[key] = mapLanguageValues(result[key], { ...opts, noDeepClone: true })
88
- }
89
- })
90
- return result
91
- }
1
+ import { ObjectUtils } from '../utils'
2
+
3
+ export type LanguageValueClaim = {
4
+ language: string
5
+ value: string | string[] | number | number[]
6
+ }
7
+
8
+ export const isLanguageValueObject = (claim?: unknown): claim is LanguageValueClaim => {
9
+ if (!claim || !ObjectUtils.isObject(claim) || Array.isArray(claim)) {
10
+ return false
11
+ }
12
+ const keys = Object.keys(claim)
13
+ if (keys.length !== 2) {
14
+ return false // Only 'language' and 'value' for now
15
+ } else if (!('language' in claim && !!claim.language)) {
16
+ return false
17
+ } else if (!('value' in claim && !!claim.value)) {
18
+ return false
19
+ }
20
+ return true
21
+ }
22
+
23
+ export const isLanguageValueObjects = (claim?: unknown): claim is LanguageValueClaim[] => {
24
+ if (!claim || !Array.isArray(claim)) {
25
+ return false
26
+ }
27
+ return claim.every((val) => isLanguageValueObject(val))
28
+ }
29
+
30
+ export const toLanguageValueObject = (claim?: unknown): LanguageValueClaim | undefined => {
31
+ return isLanguageValueObject(claim) ? claim : undefined
32
+ }
33
+
34
+ export const toLanguageValueObjects = (claim?: unknown): LanguageValueClaim[] | undefined => {
35
+ if (isLanguageValueObject(claim)) {
36
+ return ObjectUtils.asArray(toLanguageValueObject(claim) as LanguageValueClaim)
37
+ } else if (isLanguageValueObjects(claim)) {
38
+ return claim
39
+ }
40
+ return undefined // no empty array on purpose, as this really would not be a language value object
41
+ }
42
+
43
+ export const mapLanguageValue = (
44
+ claim?: unknown,
45
+ opts?: {
46
+ language?: string
47
+ fallbackToFirstObject?: boolean
48
+ },
49
+ ): any => {
50
+ const langValues = toLanguageValueObjects(claim)
51
+ if (Array.isArray(langValues)) {
52
+ if (langValues.length === 0) {
53
+ // should not happen, but let's return original claim to be sure
54
+ return claim
55
+ }
56
+ const filteredLangValues = langValues.filter((val) => (opts?.language ? val.language.toLowerCase().includes(opts.language.toLowerCase()) : true))
57
+
58
+ let langValue: LanguageValueClaim
59
+ if (filteredLangValues.length > 0) {
60
+ langValue = filteredLangValues[0]
61
+ } else {
62
+ if (opts?.fallbackToFirstObject === false) {
63
+ // No match and we also do not fallback to the first value, so return the original claim
64
+ return claim
65
+ }
66
+ // Fallback to the first value
67
+ langValue = langValues[0]
68
+ }
69
+ return langValue.value
70
+ }
71
+
72
+ return claim
73
+ }
74
+
75
+ export const mapLanguageValues = <T extends object>(
76
+ claimsOrCredential: T,
77
+ opts?: {
78
+ language?: string
79
+ fallbackToFirstObject?: boolean
80
+ noDeepClone?: boolean
81
+ },
82
+ ): T => {
83
+ const result = opts?.noDeepClone ? claimsOrCredential : JSON.parse(JSON.stringify(claimsOrCredential))
84
+ Object.keys(claimsOrCredential).forEach((key) => {
85
+ result[key] = mapLanguageValue(result[key], opts)
86
+ if (ObjectUtils.isObject(result[key]) || Array.isArray(result[key])) {
87
+ result[key] = mapLanguageValues(result[key], { ...opts, noDeepClone: true })
88
+ }
89
+ })
90
+ return result
91
+ }
package/src/types/cose.ts CHANGED
@@ -1,69 +1,69 @@
1
- /**
2
- * See our mdl-mdoc and crypto library for more information
3
- * https://github.com/Sphereon-Opensource/mdoc-cbor-crypto-multiplatform
4
- *
5
- * Conversion functions are available in above library.
6
- * Conversion functions are also available for TS in our @sphereon/ssi-sdk-ext.key-utils package
7
- *
8
- */
9
- export interface ICoseKeyJson {
10
- kty: ICoseKeyType
11
- kid?: string
12
- alg?: ICoseSignatureAlgorithm
13
- key_ops?: Array<ICoseKeyOperation>
14
- baseIV?: string
15
- crv?: ICoseCurve
16
- x?: string
17
- y?: string
18
- d?: string
19
- x5chain?: Array<string>
20
-
21
- [k: string]: unknown
22
- }
23
-
24
- export enum ICoseKeyType {
25
- OKP = 1,
26
- EC2 = 2,
27
- RSA = 3,
28
- Symmetric = 4,
29
- Reserved = 0,
30
- }
31
-
32
- export enum ICoseSignatureAlgorithm {
33
- ES256 = -7,
34
- ES256K = -47,
35
- ES384 = -35,
36
- ES512 = -36,
37
- EdDSA = -8,
38
- HS256_64 = 4,
39
- HS256 = 5,
40
- HS384 = 6,
41
- HS512 = 7,
42
- PS256 = -37,
43
- PS384 = -38,
44
- PS512 = -39,
45
- }
46
-
47
- export enum ICoseKeyOperation {
48
- SIGN = 1,
49
- VERIFY = 2,
50
- ENCRYPT = 3,
51
- DECRYPT = 4,
52
- WRAP_KEY = 5,
53
- UNWRAP_KEY = 6,
54
- DERIVE_KEY = 7,
55
- DERIVE_BITS = 8,
56
- MAC_CREATE = 9,
57
- MAC_VERIFY = 10,
58
- }
59
-
60
- export enum ICoseCurve {
61
- P_256 = 1,
62
- P_384 = 2,
63
- P_521 = 3,
64
- X25519 = 4,
65
- X448 = 5,
66
- Ed25519 = 6,
67
- Ed448 = 7,
68
- secp256k1 = -1,
69
- }
1
+ /**
2
+ * See our mdl-mdoc and crypto library for more information
3
+ * https://github.com/Sphereon-Opensource/mdoc-cbor-crypto-multiplatform
4
+ *
5
+ * Conversion functions are available in above library.
6
+ * Conversion functions are also available for TS in our @sphereon/ssi-sdk-ext.key-utils package
7
+ *
8
+ */
9
+ export interface ICoseKeyJson {
10
+ kty: ICoseKeyType
11
+ kid?: string
12
+ alg?: ICoseSignatureAlgorithm
13
+ key_ops?: Array<ICoseKeyOperation>
14
+ baseIV?: string
15
+ crv?: ICoseCurve
16
+ x?: string
17
+ y?: string
18
+ d?: string
19
+ x5chain?: Array<string>
20
+
21
+ [k: string]: unknown
22
+ }
23
+
24
+ export enum ICoseKeyType {
25
+ OKP = 1,
26
+ EC2 = 2,
27
+ RSA = 3,
28
+ Symmetric = 4,
29
+ Reserved = 0,
30
+ }
31
+
32
+ export enum ICoseSignatureAlgorithm {
33
+ ES256 = -7,
34
+ ES256K = -47,
35
+ ES384 = -35,
36
+ ES512 = -36,
37
+ EdDSA = -8,
38
+ HS256_64 = 4,
39
+ HS256 = 5,
40
+ HS384 = 6,
41
+ HS512 = 7,
42
+ PS256 = -37,
43
+ PS384 = -38,
44
+ PS512 = -39,
45
+ }
46
+
47
+ export enum ICoseKeyOperation {
48
+ SIGN = 1,
49
+ VERIFY = 2,
50
+ ENCRYPT = 3,
51
+ DECRYPT = 4,
52
+ WRAP_KEY = 5,
53
+ UNWRAP_KEY = 6,
54
+ DERIVE_KEY = 7,
55
+ DERIVE_BITS = 8,
56
+ MAC_CREATE = 9,
57
+ MAC_VERIFY = 10,
58
+ }
59
+
60
+ export enum ICoseCurve {
61
+ P_256 = 1,
62
+ P_384 = 2,
63
+ P_521 = 3,
64
+ X25519 = 4,
65
+ X448 = 5,
66
+ Ed25519 = 6,
67
+ Ed448 = 7,
68
+ secp256k1 = -1,
69
+ }