@sphereon/ssi-sdk-ext.key-utils 0.13.1-next.2 → 0.13.1-next.3
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/dist/functions.d.ts.map +1 -1
- package/dist/functions.js.map +1 -1
- package/package.json +2 -2
- package/src/functions.ts +150 -158
package/dist/functions.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAE/D,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAGzC,OAAO,EAAgB,iCAAiC,EAAE,SAAS,EAAmC,QAAQ,EAAE,MAAM,SAAS,CAAA;AAG/H;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,SAAgB,QAAQ,KAAG,QAAQ,MAAM,CAmB1E,CAAA;AAED;;;;;;GAMG;AACH,wBAAsB,4BAA4B,CAChD,IAAI,EAAE,iCAAiC,GAAG;IACxC,GAAG,EAAE,MAAM,CAAA;CACZ,EACD,OAAO,EAAE,aAAa,CAAC,WAAW,CAAC,GAClC,OAAO,CAAC,IAAI,CAAC,CAuCf;AAED;;;;GAIG;AACH,eAAO,MAAM,aAAa,UAAW,MAAM,WAO1C,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,KAAK,iBAAkB,MAAM,QAAQ,QAAQ,SAAS;IAAE,GAAG,CAAC,EAAE,SAAS,CAAC;IAAC,GAAG,CAAC,EAAE,IAAI,CAAA;CAAE,KAAG,UAoBpG,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,eAAe,SAAU,QAAQ,gBAAgB,SAAS,KAAG,SAAS,GAAG,SAQrF,CAAA"}
|
package/dist/functions.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"functions.js","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"functions.js","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAAmD;AACnD,gDAA8E;AAI9E,wDAA+B;AAC/B,iDAAkC;AAClC,mCAA+H;AAC/H,iCAAsF;AAEtF;;;;GAIG;AACI,MAAM,qBAAqB,GAAG,CAAO,IAAc,EAAmB,EAAE;IAC7E,QAAQ,IAAI,EAAE;QACZ,KAAK,SAAS,CAAC,CAAC;YACd,MAAM,cAAc,GAAG,IAAA,yBAAsB,GAAE,CAAA;YAC/C,OAAO,GAAG,CAAC,QAAQ,CAAC,cAAc,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;SACxD;QACD,4DAA4D;QAC5D,KAAK,WAAW,CAAC;QACjB,KAAK,WAAW,CAAC,CAAC;YAChB,MAAM,YAAY,GAAG,IAAA,oBAAW,EAAC,EAAE,CAAC,CAAA;YACpC,OAAO,GAAG,CAAC,QAAQ,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAA;SAC5C;QACD,KAAK,KAAK,CAAC,CAAC;YACV,MAAM,GAAG,GAAG,MAAM,IAAA,0BAAmB,EAAC,SAAS,EAAE,SAAS,EAAE,IAAI,CAAC,CAAA;YACjE,OAAO,IAAA,2BAAoB,EAAC,GAAG,CAAC,CAAA;SACjC;QACD;YACE,MAAM,KAAK,CAAC,2BAA2B,IAAI,oDAAoD,CAAC,CAAA;KACnG;AACH,CAAC,CAAA,CAAA;AAnBY,QAAA,qBAAqB,yBAmBjC;AAED;;;;;;GAMG;AACH,SAAsB,4BAA4B,CAChD,IAEC,EACD,OAAmC;;;QAEnC,aAAa;QACb,MAAM,IA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I,CAAC,YAA0B,CAAA;KAC3C;IAED,MAAM,YAAY,GAAG,MAAA,MAAA,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,IAAI,0CAAE,YAAY,mCAAI,IAAA,eAAQ,EAAC,YAAY,EAAE,QAAQ,CAAC,CAAA;IAChF,OAAO,IAAA,eAAQ,EAAC,YAAY,EAAE,QAAQ,CAAe,CAAA;AACvD,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sphereon/ssi-sdk-ext.key-utils",
|
|
3
3
|
"description": "Sphereon SSI-SDK plugin for key creation.",
|
|
4
|
-
"version": "0.13.1-next.
|
|
4
|
+
"version": "0.13.1-next.3+77704a2",
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"DID",
|
|
48
48
|
"Veramo"
|
|
49
49
|
],
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "77704a2064e1c1d3ffc23e580ddbb36063fc70ae"
|
|
51
51
|
}
|
package/src/functions.ts
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
|
-
import {randomBytes} from '@ethersproject/random'
|
|
2
|
-
import {generateKeyPair as generateSigningKeyPair} from '@stablelib/ed25519'
|
|
3
|
-
import {IAgentContext, IKey, IKeyManager} from '@veramo/core'
|
|
1
|
+
import { randomBytes } from '@ethersproject/random'
|
|
2
|
+
import { generateKeyPair as generateSigningKeyPair } from '@stablelib/ed25519'
|
|
3
|
+
import { IAgentContext, IKey, IKeyManager } from '@veramo/core'
|
|
4
4
|
|
|
5
|
-
import {JsonWebKey} from 'did-resolver'
|
|
5
|
+
import { JsonWebKey } from 'did-resolver'
|
|
6
6
|
import elliptic from 'elliptic'
|
|
7
7
|
import * as u8a from 'uint8arrays'
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
IImportProvidedOrGeneratedKeyArgs,
|
|
11
|
-
JwkKeyUse,
|
|
12
|
-
KeyCurve,
|
|
13
|
-
KeyType,
|
|
14
|
-
SIG_KEY_ALGS,
|
|
15
|
-
TKeyType
|
|
16
|
-
} from './types'
|
|
17
|
-
import {generateRSAKeyAsPEM, hexToPEM, PEMToJwk, privateKeyHexFromPEM} from './x509'
|
|
8
|
+
import { ENC_KEY_ALGS, IImportProvidedOrGeneratedKeyArgs, JwkKeyUse, KeyCurve, KeyType, SIG_KEY_ALGS, TKeyType } from './types'
|
|
9
|
+
import { generateRSAKeyAsPEM, hexToPEM, PEMToJwk, privateKeyHexFromPEM } from './x509'
|
|
18
10
|
|
|
19
11
|
/**
|
|
20
12
|
* Generates a random Private Hex Key for the specified key type
|
|
@@ -22,24 +14,24 @@ import {generateRSAKeyAsPEM, hexToPEM, PEMToJwk, privateKeyHexFromPEM} from './x
|
|
|
22
14
|
* @return The private key in Hex form
|
|
23
15
|
*/
|
|
24
16
|
export const generatePrivateKeyHex = async (type: TKeyType): Promise<string> => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
// The Secp256 types use the same method to generate the key
|
|
31
|
-
case 'Secp256r1':
|
|
32
|
-
case 'Secp256k1': {
|
|
33
|
-
const privateBytes = randomBytes(32)
|
|
34
|
-
return u8a.toString(privateBytes, 'base16')
|
|
35
|
-
}
|
|
36
|
-
case 'RSA': {
|
|
37
|
-
const pem = await generateRSAKeyAsPEM('RSA-PSS', 'SHA-256', 2048)
|
|
38
|
-
return privateKeyHexFromPEM(pem)
|
|
39
|
-
}
|
|
40
|
-
default:
|
|
41
|
-
throw Error(`not_supported: Key type ${type} not yet supported for this did:jwk implementation`)
|
|
17
|
+
switch (type) {
|
|
18
|
+
case 'Ed25519': {
|
|
19
|
+
const keyPairEd25519 = generateSigningKeyPair()
|
|
20
|
+
return u8a.toString(keyPairEd25519.secretKey, 'base16')
|
|
42
21
|
}
|
|
22
|
+
// The Secp256 types use the same method to generate the key
|
|
23
|
+
case 'Secp256r1':
|
|
24
|
+
case 'Secp256k1': {
|
|
25
|
+
const privateBytes = randomBytes(32)
|
|
26
|
+
return u8a.toString(privateBytes, 'base16')
|
|
27
|
+
}
|
|
28
|
+
case 'RSA': {
|
|
29
|
+
const pem = await generateRSAKeyAsPEM('RSA-PSS', 'SHA-256', 2048)
|
|
30
|
+
return privateKeyHexFromPEM(pem)
|
|
31
|
+
}
|
|
32
|
+
default:
|
|
33
|
+
throw Error(`not_supported: Key type ${type} not yet supported for this did:jwk implementation`)
|
|
34
|
+
}
|
|
43
35
|
}
|
|
44
36
|
|
|
45
37
|
/**
|
|
@@ -50,49 +42,49 @@ export const generatePrivateKeyHex = async (type: TKeyType): Promise<string> =>
|
|
|
50
42
|
* @private
|
|
51
43
|
*/
|
|
52
44
|
export async function importProvidedOrGeneratedKey(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
45
|
+
args: IImportProvidedOrGeneratedKeyArgs & {
|
|
46
|
+
kms: string
|
|
47
|
+
},
|
|
48
|
+
context: IAgentContext<IKeyManager>
|
|
57
49
|
): Promise<IKey> {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
const type = args.options?.type ?? args.options?.key?.type ?? args.options?.keyType ?? 'Secp256r1'
|
|
52
|
+
const key = args?.options?.key
|
|
53
|
+
// Make sure x509 options are also set on the metadata as that is what the kms will look for
|
|
54
|
+
if (args.options?.x509 && key) {
|
|
55
|
+
key.meta = {
|
|
56
|
+
...key.meta,
|
|
57
|
+
x509: {
|
|
58
|
+
...args.options.x509,
|
|
59
|
+
...key.meta?.x509,
|
|
60
|
+
},
|
|
70
61
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (args.options && args.options?.use === JwkKeyUse.Encryption && !ENC_KEY_ALGS.includes(type)) {
|
|
65
|
+
throw new Error(`${type} keys are not valid for encryption`)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let privateKeyHex: string
|
|
69
|
+
if (key) {
|
|
70
|
+
privateKeyHex = key.privateKeyHex ?? key.meta?.x509?.privateKeyHex
|
|
71
|
+
if ((!privateKeyHex || privateKeyHex.trim() === '') && key?.meta?.x509?.privateKeyPEM) {
|
|
72
|
+
// If we do not have a privateKeyHex but do have a PEM
|
|
73
|
+
privateKeyHex = privateKeyHexFromPEM(key.meta.x509.privateKeyPEM)
|
|
74
74
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (key) {
|
|
78
|
-
privateKeyHex = key.privateKeyHex ?? key.meta?.x509?.privateKeyHex
|
|
79
|
-
if ((!privateKeyHex || privateKeyHex.trim() === '') && key?.meta?.x509?.privateKeyPEM) {
|
|
80
|
-
// If we do not have a privateKeyHex but do have a PEM
|
|
81
|
-
privateKeyHex = privateKeyHexFromPEM(key.meta.x509.privateKeyPEM)
|
|
82
|
-
}
|
|
83
|
-
if (!privateKeyHex && !key.meta?.x509?.privateKeyPEM) {
|
|
84
|
-
throw new Error(`We need to have a private key in Hex or PEM when importing a key`)
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
privateKeyHex = await generatePrivateKeyHex(type)
|
|
75
|
+
if (!privateKeyHex && !key.meta?.x509?.privateKeyPEM) {
|
|
76
|
+
throw new Error(`We need to have a private key in Hex or PEM when importing a key`)
|
|
88
77
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
78
|
+
} else {
|
|
79
|
+
privateKeyHex = await generatePrivateKeyHex(type)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return context.agent.keyManagerImport({
|
|
83
|
+
...key,
|
|
84
|
+
kms: args.kms,
|
|
85
|
+
type,
|
|
86
|
+
privateKeyHex,
|
|
87
|
+
})
|
|
96
88
|
}
|
|
97
89
|
|
|
98
90
|
/**
|
|
@@ -101,12 +93,12 @@ export async function importProvidedOrGeneratedKey(
|
|
|
101
93
|
* @return Base64Url encoded value
|
|
102
94
|
*/
|
|
103
95
|
export const hex2base64url = (value: string) => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
96
|
+
//fixme: Buffer to u8a
|
|
97
|
+
const buffer = Buffer.from(value, 'hex')
|
|
98
|
+
const base64 = buffer.toString('base64')
|
|
99
|
+
const base64url = base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
|
|
108
100
|
|
|
109
|
-
|
|
101
|
+
return base64url
|
|
110
102
|
}
|
|
111
103
|
|
|
112
104
|
/**
|
|
@@ -117,25 +109,25 @@ export const hex2base64url = (value: string) => {
|
|
|
117
109
|
* @return The JWK
|
|
118
110
|
*/
|
|
119
111
|
export const toJwk = (publicKeyHex: string, type: TKeyType, opts?: { use?: JwkKeyUse; key?: IKey }): JsonWebKey => {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
112
|
+
const { key } = opts ?? {}
|
|
113
|
+
if (key && key.publicKeyHex !== publicKeyHex) {
|
|
114
|
+
throw Error(`Provided key with id ${key.kid}, has a different public key hex than supplied public key ${publicKeyHex}`)
|
|
115
|
+
}
|
|
116
|
+
switch (type) {
|
|
117
|
+
case 'Ed25519':
|
|
118
|
+
return toEd25519OrX25519Jwk(publicKeyHex, { ...opts, crv: KeyCurve.Ed25519 })
|
|
119
|
+
case 'X25519':
|
|
120
|
+
return toEd25519OrX25519Jwk(publicKeyHex, { ...opts, crv: KeyCurve.X25519 })
|
|
121
|
+
case 'Secp256k1':
|
|
122
|
+
return toSecp256k1Jwk(publicKeyHex, opts)
|
|
123
|
+
case 'Secp256r1':
|
|
124
|
+
return toSecp256r1Jwk(publicKeyHex, opts)
|
|
125
|
+
case 'RSA':
|
|
126
|
+
return toRSAJwk(publicKeyHex, opts)
|
|
127
|
+
|
|
128
|
+
default:
|
|
129
|
+
throw new Error(`not_supported: Key type ${type} not yet supported for this did:jwk implementation`)
|
|
130
|
+
}
|
|
139
131
|
}
|
|
140
132
|
|
|
141
133
|
/**
|
|
@@ -145,13 +137,13 @@ export const toJwk = (publicKeyHex: string, type: TKeyType, opts?: { use?: JwkKe
|
|
|
145
137
|
* @param suppliedUse A supplied use. Will be used in case it is present
|
|
146
138
|
*/
|
|
147
139
|
export const jwkDetermineUse = (type: TKeyType, suppliedUse?: JwkKeyUse): JwkKeyUse | undefined => {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
140
|
+
return suppliedUse
|
|
141
|
+
? suppliedUse
|
|
142
|
+
: SIG_KEY_ALGS.includes(type)
|
|
143
|
+
? JwkKeyUse.Signature
|
|
144
|
+
: ENC_KEY_ALGS.includes(type)
|
|
145
|
+
? JwkKeyUse.Encryption
|
|
146
|
+
: undefined
|
|
155
147
|
}
|
|
156
148
|
|
|
157
149
|
/**
|
|
@@ -161,17 +153,17 @@ export const jwkDetermineUse = (type: TKeyType, suppliedUse?: JwkKeyUse): JwkKey
|
|
|
161
153
|
* @param expectedKeyLength Expected key length(s)
|
|
162
154
|
*/
|
|
163
155
|
const assertProperKeyLength = (keyHex: string, expectedKeyLength: number | number[]) => {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
} else if (keyHex.length !== expectedKeyLength) {
|
|
173
|
-
throw Error(`Invalid key length. Needs to be a hex string with length ${expectedKeyLength} instead of ${keyHex.length}. Input: ${keyHex}`)
|
|
156
|
+
if (Array.isArray(expectedKeyLength)) {
|
|
157
|
+
if (expectedKeyLength.includes(keyHex.length)) {
|
|
158
|
+
throw Error(
|
|
159
|
+
`Invalid key length. Needs to be a hex string with length from ${JSON.stringify(expectedKeyLength)} instead of ${
|
|
160
|
+
keyHex.length
|
|
161
|
+
}. Input: ${keyHex}`
|
|
162
|
+
)
|
|
174
163
|
}
|
|
164
|
+
} else if (keyHex.length !== expectedKeyLength) {
|
|
165
|
+
throw Error(`Invalid key length. Needs to be a hex string with length ${expectedKeyLength} instead of ${keyHex.length}. Input: ${keyHex}`)
|
|
166
|
+
}
|
|
175
167
|
}
|
|
176
168
|
|
|
177
169
|
/**
|
|
@@ -181,16 +173,16 @@ const assertProperKeyLength = (keyHex: string, expectedKeyLength: number | numbe
|
|
|
181
173
|
* @return The JWK
|
|
182
174
|
*/
|
|
183
175
|
const toSecp256k1Jwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse }): JsonWebKey => {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
176
|
+
assertProperKeyLength(publicKeyHex, 130)
|
|
177
|
+
const { use } = opts ?? {}
|
|
178
|
+
return {
|
|
179
|
+
alg: 'ES256K',
|
|
180
|
+
...(use !== undefined && { use }),
|
|
181
|
+
kty: KeyType.EC,
|
|
182
|
+
crv: KeyCurve.Secp256k1,
|
|
183
|
+
x: hex2base64url(publicKeyHex.substr(2, 64)),
|
|
184
|
+
y: hex2base64url(publicKeyHex.substr(66, 64)),
|
|
185
|
+
}
|
|
194
186
|
}
|
|
195
187
|
|
|
196
188
|
/**
|
|
@@ -200,21 +192,21 @@ const toSecp256k1Jwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse }): JsonW
|
|
|
200
192
|
* @return The JWK
|
|
201
193
|
*/
|
|
202
194
|
const toSecp256r1Jwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse }): JsonWebKey => {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
195
|
+
const { use } = opts ?? {}
|
|
196
|
+
const publicKey = publicKeyHex
|
|
197
|
+
assertProperKeyLength(publicKey, 66)
|
|
198
|
+
|
|
199
|
+
const secp256r1 = new elliptic.ec('p256')
|
|
200
|
+
const key = secp256r1.keyFromPublic(publicKey, 'hex')
|
|
201
|
+
const pubPoint = key.getPublic()
|
|
202
|
+
return {
|
|
203
|
+
alg: 'ES256',
|
|
204
|
+
...(use !== undefined && { use }),
|
|
205
|
+
kty: KeyType.EC,
|
|
206
|
+
crv: KeyCurve.P_256,
|
|
207
|
+
x: hex2base64url(pubPoint.getX().toString('hex')),
|
|
208
|
+
y: hex2base64url(pubPoint.getY().toString('hex')),
|
|
209
|
+
}
|
|
218
210
|
}
|
|
219
211
|
|
|
220
212
|
/**
|
|
@@ -224,32 +216,32 @@ const toSecp256r1Jwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse }): JsonW
|
|
|
224
216
|
* @return The JWK
|
|
225
217
|
*/
|
|
226
218
|
const toEd25519OrX25519Jwk = (
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
219
|
+
publicKeyHex: string,
|
|
220
|
+
opts: {
|
|
221
|
+
use?: JwkKeyUse
|
|
222
|
+
crv: KeyCurve.Ed25519 | KeyCurve.X25519
|
|
223
|
+
}
|
|
232
224
|
): JsonWebKey => {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
225
|
+
assertProperKeyLength(publicKeyHex, 64)
|
|
226
|
+
const { use } = opts ?? {}
|
|
227
|
+
return {
|
|
228
|
+
alg: 'EdDSA',
|
|
229
|
+
...(use !== undefined && { use }),
|
|
230
|
+
kty: KeyType.OKP,
|
|
231
|
+
crv: opts?.crv ?? KeyCurve.Ed25519,
|
|
232
|
+
x: hex2base64url(publicKeyHex.substr(0, 64)),
|
|
233
|
+
}
|
|
242
234
|
}
|
|
243
235
|
|
|
244
236
|
const toRSAJwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse; key?: IKey }): JsonWebKey => {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
237
|
+
const { key } = opts ?? {}
|
|
238
|
+
// const publicKey = publicKeyHex
|
|
239
|
+
// assertProperKeyLength(publicKey, [2048, 3072, 4096])
|
|
248
240
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
241
|
+
if (key?.meta?.publicKeyJwk) {
|
|
242
|
+
return key.meta.publicKeyJwk as JsonWebKey
|
|
243
|
+
}
|
|
252
244
|
|
|
253
|
-
|
|
254
|
-
|
|
245
|
+
const publicKeyPEM = key?.meta?.publicKeyPEM ?? hexToPEM(publicKeyHex, 'public')
|
|
246
|
+
return PEMToJwk(publicKeyPEM, 'public') as JsonWebKey
|
|
255
247
|
}
|