@sphereon/ssi-sdk-ext.key-utils 0.12.2-unstable.11 → 0.13.1-next.2
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 +10 -6
- package/dist/functions.js.map +1 -1
- package/package.json +2 -2
- package/src/functions.ts +157 -145
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,EAAC,aAAa,EAAE,IAAI,EAAE,WAAW,EAAC,MAAM,cAAc,CAAA;AAE7D,OAAO,EAAC,UAAU,EAAC,MAAM,cAAc,CAAA;AAGvC,OAAO,EAEH,iCAAiC,EACjC,SAAS,EAIT,QAAQ,EACX,MAAM,SAAS,CAAA;AAGhB;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,SAAgB,QAAQ,KAAG,QAAQ,MAAM,CAmB1E,CAAA;AAED;;;;;;GAMG;AACH,wBAAsB,4BAA4B,CAC9C,IAAI,EAAE,iCAAiC,GAAG;IACtC,GAAG,EAAE,MAAM,CAAA;CACd,EACD,OAAO,EAAE,aAAa,CAAC,WAAW,CAAC,GACpC,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
CHANGED
|
@@ -76,22 +76,26 @@ exports.generatePrivateKeyHex = generatePrivateKeyHex;
|
|
|
76
76
|
* @private
|
|
77
77
|
*/
|
|
78
78
|
function importProvidedOrGeneratedKey(args, context) {
|
|
79
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
|
|
79
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
|
|
80
80
|
return __awaiter(this, void 0, void 0, function* () {
|
|
81
81
|
// @ts-ignore
|
|
82
82
|
const type = (_g = (_e = (_b = (_a = args.options) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : (_d = (_c = args.options) === null || _c === void 0 ? void 0 : _c.key) === null || _d === void 0 ? void 0 : _d.type) !== null && _e !== void 0 ? _e : (_f = args.options) === null || _f === void 0 ? void 0 : _f.keyType) !== null && _g !== void 0 ? _g : 'Secp256r1';
|
|
83
83
|
const key = (_h = args === null || args === void 0 ? void 0 : args.options) === null || _h === void 0 ? void 0 : _h.key;
|
|
84
84
|
// Make sure x509 options are also set on the metadata as that is what the kms will look for
|
|
85
|
-
if (((_j = args.options) === null || _j === void 0 ? void 0 : _j.x509) && key
|
|
86
|
-
key.meta = Object.assign(Object.assign({}, key.meta), { x509: Object.assign(Object.assign({}, (
|
|
85
|
+
if (((_j = args.options) === null || _j === void 0 ? void 0 : _j.x509) && key) {
|
|
86
|
+
key.meta = Object.assign(Object.assign({}, key.meta), { x509: Object.assign(Object.assign({}, args.options.x509), (_k = key.meta) === null || _k === void 0 ? void 0 : _k.x509) });
|
|
87
87
|
}
|
|
88
|
-
if (args.options && ((
|
|
88
|
+
if (args.options && ((_l = args.options) === null || _l === void 0 ? void 0 : _l.use) === types_1.JwkKeyUse.Encryption && !types_1.ENC_KEY_ALGS.includes(type)) {
|
|
89
89
|
throw new Error(`${type} keys are not valid for encryption`);
|
|
90
90
|
}
|
|
91
91
|
let privateKeyHex;
|
|
92
92
|
if (key) {
|
|
93
|
-
privateKeyHex = (
|
|
94
|
-
if (!privateKeyHex &&
|
|
93
|
+
privateKeyHex = (_m = key.privateKeyHex) !== null && _m !== void 0 ? _m : (_p = (_o = key.meta) === null || _o === void 0 ? void 0 : _o.x509) === null || _p === void 0 ? void 0 : _p.privateKeyHex;
|
|
94
|
+
if ((!privateKeyHex || privateKeyHex.trim() === '') && ((_r = (_q = key === null || key === void 0 ? void 0 : key.meta) === null || _q === void 0 ? void 0 : _q.x509) === null || _r === void 0 ? void 0 : _r.privateKeyPEM)) {
|
|
95
|
+
// If we do not have a privateKeyHex but do have a PEM
|
|
96
|
+
privateKeyHex = (0, x509_1.privateKeyHexFromPEM)(key.meta.x509.privateKeyPEM);
|
|
97
|
+
}
|
|
98
|
+
if (!privateKeyHex && !((_t = (_s = key.meta) === null || _s === void 0 ? void 0 : _s.x509) === null || _t === void 0 ? void 0 : _t.privateKeyPEM)) {
|
|
95
99
|
throw new Error(`We need to have a private key in Hex or PEM when importing a key`);
|
|
96
100
|
}
|
|
97
101
|
}
|
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,kDAAiD;AACjD,gDAA4E;AAI5E,wDAA+B;AAC/B,iDAAkC;AAClC,mCAQgB;AAChB,iCAAoF;AAEpF;;;;GAIG;AACI,MAAM,qBAAqB,GAAG,CAAO,IAAc,EAAmB,EAAE;IAC3E,QAAQ,IAAI,EAAE;QACV,KAAK,SAAS,CAAC,CAAC;YACZ,MAAM,cAAc,GAAG,IAAA,yBAAsB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EAAC,GAAG,EAAC,GAAG,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,EAAE,CAAA;IACxB,qCACI,GAAG,EAAE,OAAO,IACT,CAAC,GAAG,KAAK,SAAS,IAAI,EAAC,GAAG,EAAC,CAAC,KAC/B,GAAG,EAAE,eAAO,CAAC,GAAG,EAChB,GAAG,EAAE,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,GAAG,mCAAI,gBAAQ,CAAC,OAAO,EAClC,CAAC,EAAE,IAAA,qBAAa,EAAC,YAAY,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAC/C;AACL,CAAC,CAAA;AAED,MAAM,QAAQ,GAAG,CAAC,YAAoB,EAAE,IAAsC,EAAc,EAAE;;IAC1F,MAAM,EAAC,GAAG,EAAC,GAAG,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,EAAE,CAAA;IACxB,iCAAiC;IACjC,uDAAuD;IAEvD,IAAI,MAAA,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,IAAI,0CAAE,YAAY,EAAE;QACzB,OAAO,GAAG,CAAC,IAAI,CAAC,YAA0B,CAAA;KAC7C;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;AACzD,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.
|
|
4
|
+
"version": "0.13.1-next.2+52c560b",
|
|
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": "52c560b4d4fef999554ec00130cf7136dc2db1c6"
|
|
51
51
|
}
|
package/src/functions.ts
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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 {
|
|
5
|
+
import {JsonWebKey} from 'did-resolver'
|
|
6
6
|
import elliptic from 'elliptic'
|
|
7
7
|
import * as u8a from 'uint8arrays'
|
|
8
|
-
import {
|
|
9
|
-
|
|
8
|
+
import {
|
|
9
|
+
ENC_KEY_ALGS,
|
|
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'
|
|
10
18
|
|
|
11
19
|
/**
|
|
12
20
|
* Generates a random Private Hex Key for the specified key type
|
|
@@ -14,24 +22,24 @@ import { generateRSAKeyAsPEM, hexToPEM, PEMToJwk, privateKeyHexFromPEM } from '.
|
|
|
14
22
|
* @return The private key in Hex form
|
|
15
23
|
*/
|
|
16
24
|
export const generatePrivateKeyHex = async (type: TKeyType): Promise<string> => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
switch (type) {
|
|
26
|
+
case 'Ed25519': {
|
|
27
|
+
const keyPairEd25519 = generateSigningKeyPair()
|
|
28
|
+
return u8a.toString(keyPairEd25519.secretKey, 'base16')
|
|
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`)
|
|
21
42
|
}
|
|
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
|
-
}
|
|
35
43
|
}
|
|
36
44
|
|
|
37
45
|
/**
|
|
@@ -42,45 +50,49 @@ export const generatePrivateKeyHex = async (type: TKeyType): Promise<string> =>
|
|
|
42
50
|
* @private
|
|
43
51
|
*/
|
|
44
52
|
export async function importProvidedOrGeneratedKey(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
args: IImportProvidedOrGeneratedKeyArgs & {
|
|
54
|
+
kms: string
|
|
55
|
+
},
|
|
56
|
+
context: IAgentContext<IKeyManager>
|
|
49
57
|
): Promise<IKey> {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
const type = args.options?.type ?? args.options?.key?.type ?? args.options?.keyType ?? 'Secp256r1'
|
|
60
|
+
const key = args?.options?.key
|
|
61
|
+
// Make sure x509 options are also set on the metadata as that is what the kms will look for
|
|
62
|
+
if (args.options?.x509 && key) {
|
|
63
|
+
key.meta = {
|
|
64
|
+
...key.meta,
|
|
65
|
+
x509: {
|
|
66
|
+
...args.options.x509,
|
|
67
|
+
...key.meta?.x509,
|
|
68
|
+
},
|
|
69
|
+
}
|
|
61
70
|
}
|
|
62
|
-
}
|
|
63
71
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
72
|
+
if (args.options && args.options?.use === JwkKeyUse.Encryption && !ENC_KEY_ALGS.includes(type)) {
|
|
73
|
+
throw new Error(`${type} keys are not valid for encryption`)
|
|
74
|
+
}
|
|
67
75
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
76
|
+
let privateKeyHex: string
|
|
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)
|
|
73
88
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
type,
|
|
82
|
-
privateKeyHex,
|
|
83
|
-
})
|
|
89
|
+
|
|
90
|
+
return context.agent.keyManagerImport({
|
|
91
|
+
...key,
|
|
92
|
+
kms: args.kms,
|
|
93
|
+
type,
|
|
94
|
+
privateKeyHex,
|
|
95
|
+
})
|
|
84
96
|
}
|
|
85
97
|
|
|
86
98
|
/**
|
|
@@ -89,12 +101,12 @@ export async function importProvidedOrGeneratedKey(
|
|
|
89
101
|
* @return Base64Url encoded value
|
|
90
102
|
*/
|
|
91
103
|
export const hex2base64url = (value: string) => {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
104
|
+
//fixme: Buffer to u8a
|
|
105
|
+
const buffer = Buffer.from(value, 'hex')
|
|
106
|
+
const base64 = buffer.toString('base64')
|
|
107
|
+
const base64url = base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
|
|
96
108
|
|
|
97
|
-
|
|
109
|
+
return base64url
|
|
98
110
|
}
|
|
99
111
|
|
|
100
112
|
/**
|
|
@@ -105,25 +117,25 @@ export const hex2base64url = (value: string) => {
|
|
|
105
117
|
* @return The JWK
|
|
106
118
|
*/
|
|
107
119
|
export const toJwk = (publicKeyHex: string, type: TKeyType, opts?: { use?: JwkKeyUse; key?: IKey }): JsonWebKey => {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
120
|
+
const {key} = opts ?? {}
|
|
121
|
+
if (key && key.publicKeyHex !== publicKeyHex) {
|
|
122
|
+
throw Error(`Provided key with id ${key.kid}, has a different public key hex than supplied public key ${publicKeyHex}`)
|
|
123
|
+
}
|
|
124
|
+
switch (type) {
|
|
125
|
+
case 'Ed25519':
|
|
126
|
+
return toEd25519OrX25519Jwk(publicKeyHex, {...opts, crv: KeyCurve.Ed25519})
|
|
127
|
+
case 'X25519':
|
|
128
|
+
return toEd25519OrX25519Jwk(publicKeyHex, {...opts, crv: KeyCurve.X25519})
|
|
129
|
+
case 'Secp256k1':
|
|
130
|
+
return toSecp256k1Jwk(publicKeyHex, opts)
|
|
131
|
+
case 'Secp256r1':
|
|
132
|
+
return toSecp256r1Jwk(publicKeyHex, opts)
|
|
133
|
+
case 'RSA':
|
|
134
|
+
return toRSAJwk(publicKeyHex, opts)
|
|
135
|
+
|
|
136
|
+
default:
|
|
137
|
+
throw new Error(`not_supported: Key type ${type} not yet supported for this did:jwk implementation`)
|
|
138
|
+
}
|
|
127
139
|
}
|
|
128
140
|
|
|
129
141
|
/**
|
|
@@ -133,13 +145,13 @@ export const toJwk = (publicKeyHex: string, type: TKeyType, opts?: { use?: JwkKe
|
|
|
133
145
|
* @param suppliedUse A supplied use. Will be used in case it is present
|
|
134
146
|
*/
|
|
135
147
|
export const jwkDetermineUse = (type: TKeyType, suppliedUse?: JwkKeyUse): JwkKeyUse | undefined => {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
148
|
+
return suppliedUse
|
|
149
|
+
? suppliedUse
|
|
150
|
+
: SIG_KEY_ALGS.includes(type)
|
|
151
|
+
? JwkKeyUse.Signature
|
|
152
|
+
: ENC_KEY_ALGS.includes(type)
|
|
153
|
+
? JwkKeyUse.Encryption
|
|
154
|
+
: undefined
|
|
143
155
|
}
|
|
144
156
|
|
|
145
157
|
/**
|
|
@@ -149,17 +161,17 @@ export const jwkDetermineUse = (type: TKeyType, suppliedUse?: JwkKeyUse): JwkKey
|
|
|
149
161
|
* @param expectedKeyLength Expected key length(s)
|
|
150
162
|
*/
|
|
151
163
|
const assertProperKeyLength = (keyHex: string, expectedKeyLength: number | number[]) => {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
164
|
+
if (Array.isArray(expectedKeyLength)) {
|
|
165
|
+
if (expectedKeyLength.includes(keyHex.length)) {
|
|
166
|
+
throw Error(
|
|
167
|
+
`Invalid key length. Needs to be a hex string with length from ${JSON.stringify(expectedKeyLength)} instead of ${
|
|
168
|
+
keyHex.length
|
|
169
|
+
}. Input: ${keyHex}`
|
|
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}`)
|
|
159
174
|
}
|
|
160
|
-
} else if (keyHex.length !== expectedKeyLength) {
|
|
161
|
-
throw Error(`Invalid key length. Needs to be a hex string with length ${expectedKeyLength} instead of ${keyHex.length}. Input: ${keyHex}`)
|
|
162
|
-
}
|
|
163
175
|
}
|
|
164
176
|
|
|
165
177
|
/**
|
|
@@ -169,16 +181,16 @@ const assertProperKeyLength = (keyHex: string, expectedKeyLength: number | numbe
|
|
|
169
181
|
* @return The JWK
|
|
170
182
|
*/
|
|
171
183
|
const toSecp256k1Jwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse }): JsonWebKey => {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
184
|
+
assertProperKeyLength(publicKeyHex, 130)
|
|
185
|
+
const {use} = opts ?? {}
|
|
186
|
+
return {
|
|
187
|
+
alg: 'ES256K',
|
|
188
|
+
...(use !== undefined && {use}),
|
|
189
|
+
kty: KeyType.EC,
|
|
190
|
+
crv: KeyCurve.Secp256k1,
|
|
191
|
+
x: hex2base64url(publicKeyHex.substr(2, 64)),
|
|
192
|
+
y: hex2base64url(publicKeyHex.substr(66, 64)),
|
|
193
|
+
}
|
|
182
194
|
}
|
|
183
195
|
|
|
184
196
|
/**
|
|
@@ -188,21 +200,21 @@ const toSecp256k1Jwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse }): JsonW
|
|
|
188
200
|
* @return The JWK
|
|
189
201
|
*/
|
|
190
202
|
const toSecp256r1Jwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse }): JsonWebKey => {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
203
|
+
const {use} = opts ?? {}
|
|
204
|
+
const publicKey = publicKeyHex
|
|
205
|
+
assertProperKeyLength(publicKey, 66)
|
|
206
|
+
|
|
207
|
+
const secp256r1 = new elliptic.ec('p256')
|
|
208
|
+
const key = secp256r1.keyFromPublic(publicKey, 'hex')
|
|
209
|
+
const pubPoint = key.getPublic()
|
|
210
|
+
return {
|
|
211
|
+
alg: 'ES256',
|
|
212
|
+
...(use !== undefined && {use}),
|
|
213
|
+
kty: KeyType.EC,
|
|
214
|
+
crv: KeyCurve.P_256,
|
|
215
|
+
x: hex2base64url(pubPoint.getX().toString('hex')),
|
|
216
|
+
y: hex2base64url(pubPoint.getY().toString('hex')),
|
|
217
|
+
}
|
|
206
218
|
}
|
|
207
219
|
|
|
208
220
|
/**
|
|
@@ -212,32 +224,32 @@ const toSecp256r1Jwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse }): JsonW
|
|
|
212
224
|
* @return The JWK
|
|
213
225
|
*/
|
|
214
226
|
const toEd25519OrX25519Jwk = (
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
227
|
+
publicKeyHex: string,
|
|
228
|
+
opts: {
|
|
229
|
+
use?: JwkKeyUse
|
|
230
|
+
crv: KeyCurve.Ed25519 | KeyCurve.X25519
|
|
231
|
+
}
|
|
220
232
|
): JsonWebKey => {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
233
|
+
assertProperKeyLength(publicKeyHex, 64)
|
|
234
|
+
const {use} = opts ?? {}
|
|
235
|
+
return {
|
|
236
|
+
alg: 'EdDSA',
|
|
237
|
+
...(use !== undefined && {use}),
|
|
238
|
+
kty: KeyType.OKP,
|
|
239
|
+
crv: opts?.crv ?? KeyCurve.Ed25519,
|
|
240
|
+
x: hex2base64url(publicKeyHex.substr(0, 64)),
|
|
241
|
+
}
|
|
230
242
|
}
|
|
231
243
|
|
|
232
244
|
const toRSAJwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse; key?: IKey }): JsonWebKey => {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
245
|
+
const {key} = opts ?? {}
|
|
246
|
+
// const publicKey = publicKeyHex
|
|
247
|
+
// assertProperKeyLength(publicKey, [2048, 3072, 4096])
|
|
236
248
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
249
|
+
if (key?.meta?.publicKeyJwk) {
|
|
250
|
+
return key.meta.publicKeyJwk as JsonWebKey
|
|
251
|
+
}
|
|
240
252
|
|
|
241
|
-
|
|
242
|
-
|
|
253
|
+
const publicKeyPEM = key?.meta?.publicKeyPEM ?? hexToPEM(publicKeyHex, 'public')
|
|
254
|
+
return PEMToJwk(publicKeyPEM, 'public') as JsonWebKey
|
|
243
255
|
}
|