@verii/cose-key 1.0.0-pre.1753171788
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/LICENSE +202 -0
- package/NOTICE +1 -0
- package/index.js +22 -0
- package/package.json +42 -0
- package/src/cose-iana.js +259 -0
- package/src/cose-key.js +148 -0
- package/src/cose-utils.js +49 -0
- package/src/derive-ec-y-values.js +93 -0
- package/src/jwk-converters.js +89 -0
- package/src/kv-map.js +287 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Copyright 2025 Velocity Team Authors
|
package/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2025 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
module.exports = {
|
|
18
|
+
...require('./src/derive-ec-y-values'),
|
|
19
|
+
...require('./src/cose-key'),
|
|
20
|
+
...require('./src/cose-iana'),
|
|
21
|
+
...require('./src/jwk-converters'),
|
|
22
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@verii/cose-key",
|
|
3
|
+
"version": "1.0.0-pre.1753171788",
|
|
4
|
+
"description": "Set of cose key functions used in Verii projects",
|
|
5
|
+
"repository": "https://github.com/LFDT-Verii/core",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --config ./jest.config.js --runInBand --coverage",
|
|
12
|
+
"lint": "eslint . --format json >> eslint.json",
|
|
13
|
+
"lint:fix": "eslint --fix --ext .js ."
|
|
14
|
+
},
|
|
15
|
+
"author": "Velocity Team",
|
|
16
|
+
"license": "Apache-2.0",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"bigint-crypto-utils": "3.3.0",
|
|
19
|
+
"cborg": "4.2.12",
|
|
20
|
+
"lodash": "^4.17.21"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@verii/crypto": "1.0.0-pre.1753171788",
|
|
24
|
+
"eslint": "8.57.1",
|
|
25
|
+
"eslint-config-airbnb-base": "14.2.1",
|
|
26
|
+
"eslint-config-prettier": "8.10.0",
|
|
27
|
+
"eslint-plugin-autofix": "1.1.0",
|
|
28
|
+
"eslint-plugin-better-mutation": "1.7.0",
|
|
29
|
+
"eslint-plugin-import": "2.32.0",
|
|
30
|
+
"eslint-plugin-prefer-arrow-functions": "3.6.2",
|
|
31
|
+
"eslint-plugin-prettier": "4.2.1",
|
|
32
|
+
"eslint-watch": "7.0.0",
|
|
33
|
+
"jest": "29.7.0",
|
|
34
|
+
"prettier": "2.8.8"
|
|
35
|
+
},
|
|
36
|
+
"nx": {
|
|
37
|
+
"tags": [
|
|
38
|
+
"lib"
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
"gitHead": "e6116b2410d04efa61841268ab1b378e1c1c6060"
|
|
42
|
+
}
|
package/src/cose-iana.js
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// Code snippet inspired by: https://github.com/ldclabs/cose-ts
|
|
19
|
+
|
|
20
|
+
// IANA-registered COSE common key parameters.
|
|
21
|
+
//
|
|
22
|
+
// From IANA registry <https://www.iana.org/assignments/cose/cose.xhtml#key-common-parameters>
|
|
23
|
+
// as of 2022-12-19.
|
|
24
|
+
|
|
25
|
+
const KeyParameters = {
|
|
26
|
+
Reserved: 0,
|
|
27
|
+
Kty: 1,
|
|
28
|
+
Kid: 2,
|
|
29
|
+
Alg: 3,
|
|
30
|
+
KeyOps: 4,
|
|
31
|
+
BaseIV: 5,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const KeyTypes = {
|
|
35
|
+
Reserved: 0,
|
|
36
|
+
OKP: 1,
|
|
37
|
+
EC2: 2,
|
|
38
|
+
RSA: 3,
|
|
39
|
+
Symmetric: 4,
|
|
40
|
+
HSS_LMS: 5,
|
|
41
|
+
WalnutDSA: 6,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const OKPKeyParameters = {
|
|
45
|
+
Crv: -1,
|
|
46
|
+
X: -2,
|
|
47
|
+
D: -4,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const EC2KeyParameters = {
|
|
51
|
+
Crv: -1,
|
|
52
|
+
X: -2,
|
|
53
|
+
Y: -3,
|
|
54
|
+
D: -4,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const RSAKeyParameters = {
|
|
58
|
+
N: -1,
|
|
59
|
+
E: -2,
|
|
60
|
+
D: -3,
|
|
61
|
+
P: -4,
|
|
62
|
+
Q: -5,
|
|
63
|
+
DP: -6,
|
|
64
|
+
DQ: -7,
|
|
65
|
+
QInv: -8,
|
|
66
|
+
Other: -9,
|
|
67
|
+
RI: -10,
|
|
68
|
+
DI: -11,
|
|
69
|
+
TI: -12,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const SymmetricKeyParameters = {
|
|
73
|
+
K: -1,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const HSSLMSKeyParameters = {
|
|
77
|
+
Pub: -1,
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const WalnutDSAKeyParameters = {
|
|
81
|
+
N: -1,
|
|
82
|
+
Q: -2,
|
|
83
|
+
TValues: -3,
|
|
84
|
+
Matrix1: -4,
|
|
85
|
+
Permutation1: -5,
|
|
86
|
+
Matrix2: -6,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const Algorithms = {
|
|
90
|
+
RS1: -65535,
|
|
91
|
+
WalnutDSA: -260,
|
|
92
|
+
RS512: -259,
|
|
93
|
+
RS384: -258,
|
|
94
|
+
RS256: -257,
|
|
95
|
+
ES256K: -47,
|
|
96
|
+
HSS_LMS: -46,
|
|
97
|
+
SHAKE256: -45,
|
|
98
|
+
SHA_512: -44,
|
|
99
|
+
SHA_384: -43,
|
|
100
|
+
RSAES_OAEP_SHA_512: -42,
|
|
101
|
+
RSAES_OAEP_SHA_256: -41,
|
|
102
|
+
RSAES_OAEP_RFC_8017_default: -40,
|
|
103
|
+
PS512: -39,
|
|
104
|
+
PS384: -38,
|
|
105
|
+
PS256: -37,
|
|
106
|
+
ES512: -36,
|
|
107
|
+
ES384: -35,
|
|
108
|
+
ECDH_SS_A256KW: -34,
|
|
109
|
+
ECDH_SS_A192KW: -33,
|
|
110
|
+
ECDH_SS_A128KW: -32,
|
|
111
|
+
ECDH_ES_A256KW: -31,
|
|
112
|
+
ECDH_ES_A192KW: -30,
|
|
113
|
+
ECDH_ES_A128KW: -29,
|
|
114
|
+
ECDH_SS_HKDF_512: -28,
|
|
115
|
+
ECDH_SS_HKDF_256: -27,
|
|
116
|
+
ECDH_ES_HKDF_512: -26,
|
|
117
|
+
ECDH_ES_HKDF_256: -25,
|
|
118
|
+
SHAKE128: -18,
|
|
119
|
+
SHA_512_256: -17,
|
|
120
|
+
SHA_256: -16,
|
|
121
|
+
SHA_256_64: -15,
|
|
122
|
+
SHA_1: -14,
|
|
123
|
+
Direct_HKDF_AES_256: -13,
|
|
124
|
+
Direct_HKDF_AES_128: -12,
|
|
125
|
+
Direct_HKDF_SHA_512: -11,
|
|
126
|
+
Direct_HKDF_SHA_256: -10,
|
|
127
|
+
EdDSA: -8,
|
|
128
|
+
ES256: -7,
|
|
129
|
+
Direct: -6,
|
|
130
|
+
A256KW: -5,
|
|
131
|
+
A192KW: -4,
|
|
132
|
+
A128KW: -3,
|
|
133
|
+
Reserved: 0,
|
|
134
|
+
A128GCM: 1,
|
|
135
|
+
A192GCM: 2,
|
|
136
|
+
A256GCM: 3,
|
|
137
|
+
HMAC_256_64: 4,
|
|
138
|
+
HMAC_256_256: 5,
|
|
139
|
+
HMAC_384_384: 6,
|
|
140
|
+
HMAC_512_512: 7,
|
|
141
|
+
AES_CCM_16_64_128: 10,
|
|
142
|
+
AES_CCM_16_64_256: 11,
|
|
143
|
+
AES_CCM_64_64_128: 12,
|
|
144
|
+
AES_CCM_64_64_256: 13,
|
|
145
|
+
AES_MAC_128_64: 14,
|
|
146
|
+
AES_MAC_256_64: 15,
|
|
147
|
+
ChaCha20Poly1305: 24,
|
|
148
|
+
AES_MAC_128_128: 25,
|
|
149
|
+
AES_MAC_256_128: 26,
|
|
150
|
+
AES_CCM_16_128_128: 30,
|
|
151
|
+
AES_CCM_16_128_256: 31,
|
|
152
|
+
AES_CCM_64_128_128: 32,
|
|
153
|
+
AES_CCM_64_128_256: 33,
|
|
154
|
+
IV_GENERATION: 34,
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const EllipticCurves = {
|
|
158
|
+
Reserved: 0,
|
|
159
|
+
P_256: 1,
|
|
160
|
+
P_384: 2,
|
|
161
|
+
P_521: 3,
|
|
162
|
+
X25519: 4,
|
|
163
|
+
X448: 5,
|
|
164
|
+
Ed25519: 6,
|
|
165
|
+
Ed448: 7,
|
|
166
|
+
Secp256k1: 8,
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const HeaderParameters = {
|
|
170
|
+
Reserved: 0,
|
|
171
|
+
Alg: 1,
|
|
172
|
+
Crit: 2,
|
|
173
|
+
ContentType: 3,
|
|
174
|
+
Kid: 4,
|
|
175
|
+
IV: 5,
|
|
176
|
+
PartialIV: 6,
|
|
177
|
+
CounterSignature: 7,
|
|
178
|
+
CounterSignature0: 9,
|
|
179
|
+
KidContext: 10,
|
|
180
|
+
CountersignatureV2: 11,
|
|
181
|
+
Countersignature0V2: 11,
|
|
182
|
+
X5Bag: 32,
|
|
183
|
+
X5Chain: 33,
|
|
184
|
+
X5T: 34,
|
|
185
|
+
X5U: 35,
|
|
186
|
+
CuphNonce: 256,
|
|
187
|
+
CuphOwnerPubKey: 257,
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const HeaderAlgorithmParameters = {
|
|
191
|
+
X5ChainSender: -29,
|
|
192
|
+
X5USender: -28,
|
|
193
|
+
X5TSender: -27,
|
|
194
|
+
PartyVOther: -26,
|
|
195
|
+
PartyVNonce: -25,
|
|
196
|
+
PartyVIdentity: -24,
|
|
197
|
+
PartyUOther: -23,
|
|
198
|
+
PartyUNonce: -22,
|
|
199
|
+
PartyUIdentity: -21,
|
|
200
|
+
Salt: -20,
|
|
201
|
+
StaticKeyId: -3,
|
|
202
|
+
StaticKey: -2,
|
|
203
|
+
EphemeralKey: -1,
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const KeyOperations = {
|
|
207
|
+
Sign: 1,
|
|
208
|
+
Verify: 2,
|
|
209
|
+
Encrypt: 3,
|
|
210
|
+
Decrypt: 4,
|
|
211
|
+
WrapKey: 5,
|
|
212
|
+
UnwrapKey: 6,
|
|
213
|
+
DeriveKey: 7,
|
|
214
|
+
DeriveBits: 8,
|
|
215
|
+
MacCreate: 9,
|
|
216
|
+
MacVerify: 10,
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const CBORTags = {
|
|
220
|
+
COSEEncrypt0: 16,
|
|
221
|
+
COSEMac0: 17,
|
|
222
|
+
COSESign1: 18,
|
|
223
|
+
CWT: 61,
|
|
224
|
+
COSEEncrypt: 96,
|
|
225
|
+
COSEMac: 97,
|
|
226
|
+
COSESign: 98,
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const CWTClaims = {
|
|
230
|
+
Reserved: 0,
|
|
231
|
+
Iss: 1,
|
|
232
|
+
Sub: 2,
|
|
233
|
+
Aud: 3,
|
|
234
|
+
Exp: 4,
|
|
235
|
+
Nbf: 5,
|
|
236
|
+
Iat: 6,
|
|
237
|
+
Cti: 7,
|
|
238
|
+
Cnf: 8,
|
|
239
|
+
Scope: 9,
|
|
240
|
+
Nonce: 10,
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
module.exports = {
|
|
244
|
+
KeyParameters,
|
|
245
|
+
KeyTypes,
|
|
246
|
+
OKPKeyParameters,
|
|
247
|
+
EC2KeyParameters,
|
|
248
|
+
RSAKeyParameters,
|
|
249
|
+
SymmetricKeyParameters,
|
|
250
|
+
HSSLMSKeyParameters,
|
|
251
|
+
WalnutDSAKeyParameters,
|
|
252
|
+
Algorithms,
|
|
253
|
+
EllipticCurves,
|
|
254
|
+
HeaderParameters,
|
|
255
|
+
HeaderAlgorithmParameters,
|
|
256
|
+
KeyOperations,
|
|
257
|
+
CBORTags,
|
|
258
|
+
CWTClaims,
|
|
259
|
+
};
|
package/src/cose-key.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// Code snippet inspired by: https://github.com/ldclabs/cose-ts
|
|
19
|
+
const { KeyParameters, KeyTypes, EC2KeyParameters } = require('./cose-iana');
|
|
20
|
+
|
|
21
|
+
const { decodeCBOR } = require('./cose-utils');
|
|
22
|
+
|
|
23
|
+
const { KVMap, assertBytes, assertIntOrText } = require('./kv-map');
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Interface for encryption/decryption.
|
|
27
|
+
* @typedef {Object} Encryptor
|
|
28
|
+
* @property {function(): number} nonceSize
|
|
29
|
+
* @property {function(Uint8Array, Uint8Array, Uint8Array=): Promise<Uint8Array>} encrypt
|
|
30
|
+
* @property {function(Uint8Array, Uint8Array, Uint8Array=): Promise<Uint8Array>} decrypt
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Interface for MAC generation.
|
|
35
|
+
* @typedef {Object} MACer
|
|
36
|
+
* @property {function(Uint8Array): Uint8Array} mac
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Interface for signing.
|
|
41
|
+
* @typedef {Object} Signer
|
|
42
|
+
* @property {function(Uint8Array): Uint8Array} sign
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Interface for signature verification.
|
|
47
|
+
* @typedef {Object} Verifier
|
|
48
|
+
* @property {function(Uint8Array, Uint8Array): boolean} verify
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Interface for ECDH key exchange.
|
|
53
|
+
* @typedef {Object} ECDHer
|
|
54
|
+
* @property {function(CoseKey): Uint8Array} ecdh
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* COSE CoseKey representation based on RFC9052 and RFC9053.
|
|
59
|
+
* @see https://datatracker.ietf.org/doc/html/rfc9052
|
|
60
|
+
* @see https://datatracker.ietf.org/doc/html/rfc9053
|
|
61
|
+
*/
|
|
62
|
+
class CoseKey extends KVMap {
|
|
63
|
+
/**
|
|
64
|
+
* Decode a CoseKey from CBOR bytes.
|
|
65
|
+
* @param {Uint8Array} data
|
|
66
|
+
* @returns {Promise<CoseKey>}
|
|
67
|
+
*/
|
|
68
|
+
static async fromBytes(data) {
|
|
69
|
+
return new CoseKey(await decodeCBOR(data));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {Map<any, any>} [kv=new Map()]
|
|
74
|
+
*/
|
|
75
|
+
constructor(kv = new Map()) {
|
|
76
|
+
super(kv);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** @returns {number|string} */
|
|
80
|
+
get kty() {
|
|
81
|
+
return this.getType(KeyParameters.Kty, assertIntOrText, 'kty');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** @param {number|string} kty */
|
|
85
|
+
set kty(kty) {
|
|
86
|
+
this.setParam(KeyParameters.Kty, assertIntOrText(kty, 'kty'));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** @returns {Uint8Array} */
|
|
90
|
+
get kid() {
|
|
91
|
+
return this.getBytes(KeyParameters.Kid, 'kid');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** @param {Uint8Array} kid */
|
|
95
|
+
set kid(kid) {
|
|
96
|
+
this.setParam(KeyParameters.Kid, assertBytes(kid, 'kid'));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** @returns {number|string} */
|
|
100
|
+
get alg() {
|
|
101
|
+
return this.getType(KeyParameters.Alg, assertIntOrText, 'alg');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** @param {number|string} alg */
|
|
105
|
+
set alg(alg) {
|
|
106
|
+
this.setParam(KeyParameters.Alg, assertIntOrText(alg, 'alg'));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** @returns {(number|string)[]} */
|
|
110
|
+
get ops() {
|
|
111
|
+
return this.getArray(KeyParameters.KeyOps, assertIntOrText, 'ops');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @param {(number|string)[]} ops */
|
|
115
|
+
set ops(ops) {
|
|
116
|
+
if (!Array.isArray(ops)) {
|
|
117
|
+
throw new TypeError('ops must be an array');
|
|
118
|
+
}
|
|
119
|
+
ops.forEach((op) => assertIntOrText(op, 'ops'));
|
|
120
|
+
this.setParam(KeyParameters.KeyOps, ops);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** @returns {Uint8Array} */
|
|
124
|
+
get baseIV() {
|
|
125
|
+
return this.getBytes(KeyParameters.BaseIV, 'Base IV');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** @param {Uint8Array} iv */
|
|
129
|
+
set baseIV(iv) {
|
|
130
|
+
this.setParam(KeyParameters.BaseIV, assertBytes(iv, 'Base IV'));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Get the key material (secret) depending on key type.
|
|
135
|
+
* @returns {Uint8Array}
|
|
136
|
+
* @throws {Error} If the key type is not supported.
|
|
137
|
+
*/
|
|
138
|
+
getSecret() {
|
|
139
|
+
switch (this.kty) {
|
|
140
|
+
case KeyTypes.EC2:
|
|
141
|
+
return this.getBytes(EC2KeyParameters.D, 'd');
|
|
142
|
+
default:
|
|
143
|
+
throw new Error('unsupported key type');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
module.exports = { CoseKey };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// Code snippet inspired by: https://github.com/ldclabs/cose-ts
|
|
19
|
+
|
|
20
|
+
// TODO convert to use ESM directly and remove the async from all cbor code and clients
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Decodes a CBOR-encoded Uint8Array into a JavaScript value.
|
|
24
|
+
*
|
|
25
|
+
* @template T
|
|
26
|
+
* @param {Uint8Array} data - The CBOR-encoded data to decode.
|
|
27
|
+
* @returns {T} The decoded JavaScript value.
|
|
28
|
+
* @throws {Error} If decoding fails or duplicate keys are encountered.
|
|
29
|
+
*/
|
|
30
|
+
const decodeCBOR = async (data) => {
|
|
31
|
+
const { decode } = await import('cborg');
|
|
32
|
+
return decode(data, {
|
|
33
|
+
useMaps: true,
|
|
34
|
+
rejectDuplicateMapKeys: true,
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Encodes a JavaScript value into CBOR format.
|
|
40
|
+
*
|
|
41
|
+
* @param {unknown} data - The JavaScript value to encode (e.g., Map, Array, Object).
|
|
42
|
+
* @returns {Uint8Array} The CBOR-encoded result.
|
|
43
|
+
*/
|
|
44
|
+
const encodeCBOR = async (data) => {
|
|
45
|
+
const { encode } = await import('cborg');
|
|
46
|
+
return encode(data, {});
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
module.exports = { decodeCBOR, encodeCBOR };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
const { modPow } = require('bigint-crypto-utils');
|
|
18
|
+
const { padCharsStart } = require('lodash/fp');
|
|
19
|
+
/**
|
|
20
|
+
* Is an even coordinate
|
|
21
|
+
* @param {Buffer} y hex coordinate
|
|
22
|
+
* @return {boolean} true it is even
|
|
23
|
+
*/
|
|
24
|
+
const isEcYValueEven = (y) => {
|
|
25
|
+
return y[y.length - 1] % 2 === 0;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const bigIntToHex = (bigint) => {
|
|
29
|
+
const hex = bigint.toString(16);
|
|
30
|
+
if (hex.length % 2 === 1) {
|
|
31
|
+
return `0${hex}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return hex;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Is an even coordinate
|
|
39
|
+
* @param {string} crv the crv P-256 or secp256k1
|
|
40
|
+
* @param {Buffer} x coordinate
|
|
41
|
+
* @param {boolean} true if the Y coordinate is even
|
|
42
|
+
* @return {Buffer} y coordinate
|
|
43
|
+
*/
|
|
44
|
+
const deriveEcYValue = (crv, x, yIsEven) => {
|
|
45
|
+
const expectedMod = yIsEven ? 0n : 1n;
|
|
46
|
+
const bigX = BigInt(`0x${x.toString('hex')}`);
|
|
47
|
+
|
|
48
|
+
// y^2 = x^3 - ax + b
|
|
49
|
+
let bigY = modPow(
|
|
50
|
+
bigX ** 3n - bigX * curveParams[crv].a + curveParams[crv].b,
|
|
51
|
+
curveParams[crv].pIdent,
|
|
52
|
+
curveParams[crv].prime
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// If the parity doesn't match it's the *other* root
|
|
56
|
+
if (bigY % 2n !== expectedMod) {
|
|
57
|
+
bigY = curveParams[crv].prime - bigY;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const hexY = padCharsStart('0', 64, bigIntToHex(bigY));
|
|
61
|
+
|
|
62
|
+
return Buffer.from(hexY, 'hex');
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const curveParams = {
|
|
66
|
+
'P-256': {
|
|
67
|
+
prime: 2n ** 256n - 2n ** 224n + 2n ** 192n + 2n ** 96n - 1n,
|
|
68
|
+
a: 3n,
|
|
69
|
+
b: 41058363725152142129326129780047268409114441015993725554835256314039467401291n,
|
|
70
|
+
},
|
|
71
|
+
secp256k1: {
|
|
72
|
+
prime:
|
|
73
|
+
2n ** 256n -
|
|
74
|
+
2n ** 32n -
|
|
75
|
+
2n ** 9n -
|
|
76
|
+
2n ** 8n -
|
|
77
|
+
2n ** 7n -
|
|
78
|
+
2n ** 6n -
|
|
79
|
+
2n ** 4n -
|
|
80
|
+
1n,
|
|
81
|
+
a: 0n,
|
|
82
|
+
b: 7n,
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const calcPIdent = ({ prime }) => (prime + 1n) / 4n;
|
|
87
|
+
curveParams['P-256'].pIdent = calcPIdent(curveParams['P-256']);
|
|
88
|
+
curveParams.secp256k1.pIdent = calcPIdent(curveParams.secp256k1);
|
|
89
|
+
|
|
90
|
+
module.exports = {
|
|
91
|
+
isEcYValueEven,
|
|
92
|
+
deriveEcYValue,
|
|
93
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const {
|
|
19
|
+
RSAKeyParameters,
|
|
20
|
+
EC2KeyParameters,
|
|
21
|
+
EllipticCurves,
|
|
22
|
+
KeyTypes,
|
|
23
|
+
KeyParameters,
|
|
24
|
+
} = require('./cose-iana');
|
|
25
|
+
|
|
26
|
+
const { CoseKey } = require('./cose-key');
|
|
27
|
+
|
|
28
|
+
const { deriveEcYValue, isEcYValueEven } = require('./derive-ec-y-values');
|
|
29
|
+
|
|
30
|
+
const toJwk = (coseKey) => {
|
|
31
|
+
const kty = coseKey.getParam(KeyParameters.Kty);
|
|
32
|
+
if (kty === KeyTypes.RSA) {
|
|
33
|
+
return {
|
|
34
|
+
kty: 'RSA',
|
|
35
|
+
n: Buffer.from(coseKey.getParam(RSAKeyParameters.N)).toString(
|
|
36
|
+
'base64url'
|
|
37
|
+
),
|
|
38
|
+
e: Buffer.from(coseKey.getParam(RSAKeyParameters.E)).toString(
|
|
39
|
+
'base64url'
|
|
40
|
+
),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (kty === KeyTypes.EC2) {
|
|
44
|
+
const crv =
|
|
45
|
+
coseKey.getParam(EC2KeyParameters.Crv) === EllipticCurves.Secp256k1
|
|
46
|
+
? 'secp256k1'
|
|
47
|
+
: 'P-256';
|
|
48
|
+
const x = Buffer.from(coseKey.getParam(EC2KeyParameters.X));
|
|
49
|
+
const y = deriveEcYValue(crv, x, coseKey.getParam(EC2KeyParameters.Y));
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
kty: 'EC',
|
|
53
|
+
crv,
|
|
54
|
+
x: x.toString('base64url'),
|
|
55
|
+
y: y.toString('base64url'),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
throw new Error('Unsupported COSE Key Type (kty)');
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const fromJwk = (jwk) => {
|
|
63
|
+
const key = new CoseKey();
|
|
64
|
+
if (jwk.kty === 'RSA') {
|
|
65
|
+
key.kty = KeyTypes.RSA;
|
|
66
|
+
return key
|
|
67
|
+
.setParam(RSAKeyParameters.N, Buffer.from(jwk.n, 'base64url'))
|
|
68
|
+
.setParam(RSAKeyParameters.E, Buffer.from(jwk.e, 'base64url'));
|
|
69
|
+
}
|
|
70
|
+
if (jwk.kty === 'EC') {
|
|
71
|
+
const y = isEcYValueEven(Buffer.from(jwk.y, 'base64url'));
|
|
72
|
+
key.kty = KeyTypes.EC2;
|
|
73
|
+
return key
|
|
74
|
+
.setParam(
|
|
75
|
+
EC2KeyParameters.Crv,
|
|
76
|
+
jwk.crv === 'secp256k1'
|
|
77
|
+
? EllipticCurves.Secp256k1
|
|
78
|
+
: EllipticCurves.P_256
|
|
79
|
+
)
|
|
80
|
+
.setParam(EC2KeyParameters.X, Buffer.from(jwk.x, 'base64url'))
|
|
81
|
+
.setParam(EC2KeyParameters.Y, y);
|
|
82
|
+
}
|
|
83
|
+
throw new Error('Unsupported JWK Key Type (kty)');
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
module.exports = {
|
|
87
|
+
toJwk,
|
|
88
|
+
fromJwk,
|
|
89
|
+
};
|
package/src/kv-map.js
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Velocity Team
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// Code snippet inspired by: https://github.com/ldclabs/cose-ts
|
|
19
|
+
|
|
20
|
+
const { encodeCBOR, decodeCBOR } = require('./cose-utils');
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A label used as a key in the map. Can be a number or a string.
|
|
24
|
+
* @typedef {number|string} Label
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A value allowed in the map. Can be a number, string, boolean, Uint8Array, array of values, or another RawMap.
|
|
29
|
+
* @typedef {number|string|Uint8Array|boolean|Value[]|RawMap} Value
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A raw key-value map used internally by KVMap.
|
|
34
|
+
* @typedef {Map<Label, Value>} RawMap
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Assertion function that validates and returns a value of type T.
|
|
39
|
+
* @template T
|
|
40
|
+
* @callback AssertFn
|
|
41
|
+
* @param {unknown} value
|
|
42
|
+
* @param {string} name
|
|
43
|
+
* @returns {T}
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {unknown} value
|
|
48
|
+
* @param {string} name
|
|
49
|
+
* @returns {string}
|
|
50
|
+
* @throws {TypeError}
|
|
51
|
+
*/
|
|
52
|
+
const assertText = (value, name) => {
|
|
53
|
+
if (typeof value === 'string') {
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
throw new TypeError(`${name} must be a string, but got ${String(value)}`);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param {unknown} value
|
|
62
|
+
* @param {string} name
|
|
63
|
+
* @returns {number}
|
|
64
|
+
* @throws {TypeError}
|
|
65
|
+
*/
|
|
66
|
+
const assertInt = (value, name) => {
|
|
67
|
+
if (Number.isSafeInteger(value)) {
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
throw new TypeError(`${name} must be an integer, but got ${String(value)}`);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @param {unknown} value
|
|
76
|
+
* @param {string} name
|
|
77
|
+
* @returns {number|string}
|
|
78
|
+
* @throws {TypeError}
|
|
79
|
+
*/
|
|
80
|
+
const assertIntOrText = (value, name) => {
|
|
81
|
+
if (typeof value === 'string') {
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (Number.isSafeInteger(value)) {
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
throw new TypeError(
|
|
90
|
+
`${name} must be an integer or string, but got ${String(value)}`
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @param {unknown} value
|
|
96
|
+
* @param {string} name
|
|
97
|
+
* @returns {Uint8Array}
|
|
98
|
+
* @throws {TypeError}
|
|
99
|
+
*/
|
|
100
|
+
const assertBytes = (value, name) => {
|
|
101
|
+
if (value instanceof Uint8Array) {
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
throw new TypeError(`${name} must be a Uint8Array, but got ${String(value)}`);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @param {unknown} value
|
|
110
|
+
* @param {string} name
|
|
111
|
+
* @returns {boolean}
|
|
112
|
+
* @throws {TypeError}
|
|
113
|
+
*/
|
|
114
|
+
const assertBool = (value, name) => {
|
|
115
|
+
if (typeof value === 'boolean') {
|
|
116
|
+
return value;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
throw new TypeError(`${name} must be a Boolean, but got ${String(value)}`);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @param {unknown} value
|
|
124
|
+
* @param {string} name
|
|
125
|
+
* @returns {RawMap}
|
|
126
|
+
* @throws {TypeError}
|
|
127
|
+
*/
|
|
128
|
+
const assertMap = (value, name) => {
|
|
129
|
+
if (value instanceof Map) {
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
throw new TypeError(`${name} must be a Map, but got ${String(value)}`);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A class representing a key-value map with typed assertions and CBOR serialization.
|
|
138
|
+
*/
|
|
139
|
+
class KVMap {
|
|
140
|
+
/**
|
|
141
|
+
* @param {RawMap} [kv=new Map()]
|
|
142
|
+
*/
|
|
143
|
+
constructor(kv = new Map()) {
|
|
144
|
+
if (!(kv instanceof Map)) {
|
|
145
|
+
throw new TypeError('key/value must be a Map');
|
|
146
|
+
}
|
|
147
|
+
this._raw = kv;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Decode a KVMap from CBOR bytes.
|
|
152
|
+
* @param {Uint8Array} data
|
|
153
|
+
* @returns {KVMap}
|
|
154
|
+
*/
|
|
155
|
+
static async fromBytes(data) {
|
|
156
|
+
return new KVMap(await decodeCBOR(data));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @param {Label} key
|
|
161
|
+
* @returns {boolean}
|
|
162
|
+
*/
|
|
163
|
+
has(key) {
|
|
164
|
+
return this._raw.has(key);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @param {Label} key
|
|
169
|
+
* @returns {boolean}
|
|
170
|
+
*/
|
|
171
|
+
delete(key) {
|
|
172
|
+
return this._raw.delete(key);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @param {Label} key
|
|
177
|
+
* @param {string} [name]
|
|
178
|
+
* @returns {number}
|
|
179
|
+
*/
|
|
180
|
+
getInt(key, name) {
|
|
181
|
+
return assertInt(this._raw.get(key), name ?? String(key));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @param {Label} key
|
|
186
|
+
* @param {string} [name]
|
|
187
|
+
* @returns {string}
|
|
188
|
+
*/
|
|
189
|
+
getText(key, name) {
|
|
190
|
+
return assertText(this._raw.get(key), name ?? String(key));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* @param {Label} key
|
|
195
|
+
* @param {string} [name]
|
|
196
|
+
* @returns {Uint8Array}
|
|
197
|
+
*/
|
|
198
|
+
getBytes(key, name) {
|
|
199
|
+
return assertBytes(this._raw.get(key), name ?? String(key));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @param {Label} key
|
|
204
|
+
* @param {string} [name]
|
|
205
|
+
* @returns {boolean}
|
|
206
|
+
*/
|
|
207
|
+
getBool(key, name) {
|
|
208
|
+
return assertBool(this._raw.get(key), name ?? String(key));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @template T
|
|
213
|
+
* @param {Label} key
|
|
214
|
+
* @param {AssertFn<T>} assertFn
|
|
215
|
+
* @param {string} [name]
|
|
216
|
+
* @returns {T}
|
|
217
|
+
*/
|
|
218
|
+
getType(key, assertFn, name) {
|
|
219
|
+
return assertFn(this._raw.get(key), name ?? String(key));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* @template T
|
|
224
|
+
* @param {Label} key
|
|
225
|
+
* @param {AssertFn<T>} assertFn
|
|
226
|
+
* @param {string} [name]
|
|
227
|
+
* @returns {T[]}
|
|
228
|
+
* @throws {TypeError}
|
|
229
|
+
*/
|
|
230
|
+
getArray(key, assertFn, name) {
|
|
231
|
+
const na = name || String(key);
|
|
232
|
+
const arr = this._raw.get(key);
|
|
233
|
+
|
|
234
|
+
if (!Array.isArray(arr)) {
|
|
235
|
+
throw new TypeError(`${na} must be an array, but got ${String(arr)}`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
for (const item of arr) {
|
|
239
|
+
assertFn(item, na);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return arr;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* @template T
|
|
247
|
+
* @param {Label} key
|
|
248
|
+
* @returns {T | undefined}
|
|
249
|
+
*/
|
|
250
|
+
getParam(key) {
|
|
251
|
+
return this._raw.get(key);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* @param {Label} key
|
|
256
|
+
* @param {Value} value
|
|
257
|
+
* @returns {this}
|
|
258
|
+
*/
|
|
259
|
+
setParam(key, value) {
|
|
260
|
+
this._raw.set(key, value);
|
|
261
|
+
return this;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @returns {RawMap}
|
|
266
|
+
*/
|
|
267
|
+
clone() {
|
|
268
|
+
return new Map(this._raw);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* @returns {Promise<Uint8Array>}
|
|
273
|
+
*/
|
|
274
|
+
async toBytes() {
|
|
275
|
+
return encodeCBOR(this._raw);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
module.exports = {
|
|
280
|
+
assertText,
|
|
281
|
+
assertInt,
|
|
282
|
+
assertIntOrText,
|
|
283
|
+
assertBytes,
|
|
284
|
+
assertBool,
|
|
285
|
+
assertMap,
|
|
286
|
+
KVMap,
|
|
287
|
+
};
|