@cedulon/cose 0.1.0

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 ADDED
@@ -0,0 +1,192 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the
44
+ purposes of this License, Derivative Works shall not include works
45
+ that remain separable from, or merely link (or bind by name) to
46
+ the interfaces of, the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of,
58
+ the Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as
159
+ a result of this License or out of the use or inability to use
160
+ the Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ Copyright 2026 Emekcan Dogru
181
+
182
+ Licensed under the Apache License, Version 2.0 (the "License");
183
+ you may not use this file except in compliance with the License.
184
+ You may obtain a copy of the License at
185
+
186
+ http://www.apache.org/licenses/LICENSE-2.0
187
+
188
+ Unless required by applicable law or agreed to in writing, software
189
+ distributed under the License is distributed on an "AS IS" BASIS,
190
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
191
+ See the License for the specific language governing permissions and
192
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # `@cedulon/cose`
2
+
3
+ Deterministic CBOR and COSE_Sign1 (Ed25519). Cedulon receipts, manifests,
4
+ checkpoints, and decision tokens share this encoder.
5
+
6
+ ```ts
7
+ import { signCoseSign1, verifyCoseSign1, CTY_RECEIPT } from "@cedulon/cose";
8
+
9
+ const cose = signCoseSign1(payload, privateKeyPem, CTY_RECEIPT);
10
+ verifyCoseSign1(cose, publicKeyPem, CTY_RECEIPT);
11
+ ```
12
+
13
+ Repository: https://github.com/dogrucanemek-alt/cedulon
package/dist/cbor.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ /** Deterministic CBOR (RFC 8949 §4.2.1) for Cedulon claim types. Zero deps. */
2
+ export type CborVal = null | boolean | number | string | Uint8Array | CborVal[] | CborMap;
3
+ export type CborMap = {
4
+ readonly $map: Array<[CborVal, CborVal]>;
5
+ };
6
+ export declare function cborMap(entries: Array<[CborVal, CborVal]>): CborMap;
7
+ export declare function isCborMap(value: CborVal): value is CborMap;
8
+ export declare function encodeCbor(value: CborVal): Uint8Array;
9
+ /** RFC 8949 §4.2.1: bytewise lexicographic order of encoded keys. */
10
+ export declare function compareEncodedKeys(a: Buffer, b: Buffer): number;
11
+ export declare function decodeCbor(bytes: Uint8Array): CborVal;
12
+ export declare function mapGet(map: CborMap, key: number | string): CborVal | undefined;
13
+ export declare function asMap(value: CborVal): CborMap;
14
+ export declare function asArray(value: CborVal): CborVal[];
15
+ export declare function bytesToHex(bytes: Uint8Array): string;
16
+ export declare function hexToBytes(hex: string): Uint8Array;
17
+ //# sourceMappingURL=cbor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cbor.d.ts","sourceRoot":"","sources":["../src/cbor.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAE/E,MAAM,MAAM,OAAO,GACf,IAAI,GACJ,OAAO,GACP,MAAM,GACN,MAAM,GACN,UAAU,GACV,OAAO,EAAE,GACT,OAAO,CAAC;AAEZ,MAAM,MAAM,OAAO,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAA;CAAE,CAAC;AAEnE,wBAAgB,OAAO,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,GAAG,OAAO,CAEnE;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,OAAO,CAE1D;AASD,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,CAErD;AA2CD,qEAAqE;AACrE,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAQ/D;AAmCD,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAOrD;AAuED,wBAAgB,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAK9E;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAK7C;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,EAAE,CAKjD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAEpD;AAED,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAQlD"}
package/dist/cbor.js ADDED
@@ -0,0 +1,210 @@
1
+ /** Deterministic CBOR (RFC 8949 §4.2.1) for Cedulon claim types. Zero deps. */
2
+ export function cborMap(entries) {
3
+ return { $map: entries };
4
+ }
5
+ export function isCborMap(value) {
6
+ return typeof value === "object" && value !== null && !Array.isArray(value) && !ArrayBuffer.isView(value) && "$map" in value;
7
+ }
8
+ const MT_UINT = 0;
9
+ const MT_NINT = 1;
10
+ const MT_BSTR = 2;
11
+ const MT_TSTR = 3;
12
+ const MT_ARRAY = 4;
13
+ const MT_MAP = 5;
14
+ export function encodeCbor(value) {
15
+ return Buffer.concat(encodeParts(value));
16
+ }
17
+ function encodeParts(value) {
18
+ if (value === null) {
19
+ return [Buffer.from([0xf6])];
20
+ }
21
+ if (typeof value === "boolean") {
22
+ return [Buffer.from([value ? 0xf5 : 0xf4])];
23
+ }
24
+ if (typeof value === "number") {
25
+ if (!Number.isInteger(value) || !Number.isSafeInteger(value)) {
26
+ throw new Error("cbor-non-integer");
27
+ }
28
+ return [encodeInt(value)];
29
+ }
30
+ if (typeof value === "string") {
31
+ const utf8 = Buffer.from(value, "utf8");
32
+ return [head(MT_TSTR, utf8.length), utf8];
33
+ }
34
+ if (value instanceof Uint8Array) {
35
+ const bytes = Buffer.from(value);
36
+ return [head(MT_BSTR, bytes.length), bytes];
37
+ }
38
+ if (isCborMap(value)) {
39
+ const entries = value.$map.map(([k, v]) => {
40
+ const key = Buffer.concat(encodeParts(k));
41
+ const val = Buffer.concat(encodeParts(v));
42
+ return { key, val };
43
+ });
44
+ entries.sort((a, b) => compareEncodedKeys(a.key, b.key));
45
+ const parts = [head(MT_MAP, entries.length)];
46
+ for (const e of entries) {
47
+ parts.push(e.key, e.val);
48
+ }
49
+ return parts;
50
+ }
51
+ const parts = [head(MT_ARRAY, value.length)];
52
+ for (const item of value) {
53
+ parts.push(...encodeParts(item));
54
+ }
55
+ return parts;
56
+ }
57
+ /** RFC 8949 §4.2.1: bytewise lexicographic order of encoded keys. */
58
+ export function compareEncodedKeys(a, b) {
59
+ const n = Math.min(a.length, b.length);
60
+ for (let i = 0; i < n; i += 1) {
61
+ if (a[i] !== b[i]) {
62
+ return a[i] - b[i];
63
+ }
64
+ }
65
+ return a.length - b.length;
66
+ }
67
+ function encodeInt(n) {
68
+ if (n >= 0) {
69
+ return head(MT_UINT, n);
70
+ }
71
+ return head(MT_NINT, -1 - n);
72
+ }
73
+ function head(major, n) {
74
+ const hi = major << 5;
75
+ if (n < 24) {
76
+ return Buffer.from([hi | n]);
77
+ }
78
+ if (n < 256) {
79
+ return Buffer.from([hi | 24, n]);
80
+ }
81
+ if (n < 65536) {
82
+ const b = Buffer.alloc(3);
83
+ b[0] = hi | 25;
84
+ b.writeUInt16BE(n, 1);
85
+ return b;
86
+ }
87
+ if (n <= 0xffffffff) {
88
+ const b = Buffer.alloc(5);
89
+ b[0] = hi | 26;
90
+ b.writeUInt32BE(n, 1);
91
+ return b;
92
+ }
93
+ const b = Buffer.alloc(9);
94
+ b[0] = hi | 27;
95
+ b.writeBigUInt64BE(BigInt(n), 1);
96
+ return b;
97
+ }
98
+ export function decodeCbor(bytes) {
99
+ const r = { bytes: Buffer.from(bytes), offset: 0 };
100
+ const value = readVal(r);
101
+ if (r.offset !== r.bytes.length) {
102
+ throw new Error("cbor-trailing");
103
+ }
104
+ return value;
105
+ }
106
+ function readVal(r) {
107
+ const ib = r.bytes[r.offset];
108
+ if (ib === undefined) {
109
+ throw new Error("cbor-eof");
110
+ }
111
+ r.offset += 1;
112
+ const major = ib >> 5;
113
+ const ai = ib & 31;
114
+ if (ib === 0xf4)
115
+ return false;
116
+ if (ib === 0xf5)
117
+ return true;
118
+ if (ib === 0xf6)
119
+ return null;
120
+ const n = readLength(r, ai);
121
+ if (major === MT_UINT)
122
+ return n;
123
+ if (major === MT_NINT)
124
+ return -1 - n;
125
+ if (major === MT_BSTR) {
126
+ const slice = r.bytes.subarray(r.offset, r.offset + n);
127
+ r.offset += n;
128
+ return new Uint8Array(slice);
129
+ }
130
+ if (major === MT_TSTR) {
131
+ const slice = r.bytes.subarray(r.offset, r.offset + n);
132
+ r.offset += n;
133
+ return slice.toString("utf8");
134
+ }
135
+ if (major === MT_ARRAY) {
136
+ const arr = [];
137
+ for (let i = 0; i < n; i += 1) {
138
+ arr.push(readVal(r));
139
+ }
140
+ return arr;
141
+ }
142
+ if (major === MT_MAP) {
143
+ const entries = [];
144
+ for (let i = 0; i < n; i += 1) {
145
+ entries.push([readVal(r), readVal(r)]);
146
+ }
147
+ return cborMap(entries);
148
+ }
149
+ throw new Error("cbor-unsupported");
150
+ }
151
+ function readLength(r, ai) {
152
+ if (ai < 24)
153
+ return ai;
154
+ if (ai === 24) {
155
+ const v = r.bytes[r.offset];
156
+ r.offset += 1;
157
+ return v;
158
+ }
159
+ if (ai === 25) {
160
+ const v = r.bytes.readUInt16BE(r.offset);
161
+ r.offset += 2;
162
+ return v;
163
+ }
164
+ if (ai === 26) {
165
+ const v = r.bytes.readUInt32BE(r.offset);
166
+ r.offset += 4;
167
+ return v;
168
+ }
169
+ if (ai === 27) {
170
+ const v = r.bytes.readBigUInt64BE(r.offset);
171
+ r.offset += 8;
172
+ if (v > BigInt(Number.MAX_SAFE_INTEGER)) {
173
+ throw new Error("cbor-too-large");
174
+ }
175
+ return Number(v);
176
+ }
177
+ throw new Error("cbor-indefinite");
178
+ }
179
+ export function mapGet(map, key) {
180
+ for (const [k, v] of map.$map) {
181
+ if (k === key)
182
+ return v;
183
+ }
184
+ return undefined;
185
+ }
186
+ export function asMap(value) {
187
+ if (!isCborMap(value)) {
188
+ throw new Error("cbor-not-map");
189
+ }
190
+ return value;
191
+ }
192
+ export function asArray(value) {
193
+ if (!Array.isArray(value)) {
194
+ throw new Error("cbor-not-array");
195
+ }
196
+ return value;
197
+ }
198
+ export function bytesToHex(bytes) {
199
+ return Buffer.from(bytes).toString("hex");
200
+ }
201
+ export function hexToBytes(hex) {
202
+ if (hex.length % 2 !== 0) {
203
+ throw new Error("hex-odd");
204
+ }
205
+ if (hex !== hex.toLowerCase()) {
206
+ throw new Error("hex-not-lowercase");
207
+ }
208
+ return Uint8Array.from(Buffer.from(hex, "hex"));
209
+ }
210
+ //# sourceMappingURL=cbor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cbor.js","sourceRoot":"","sources":["../src/cbor.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAa/E,MAAM,UAAU,OAAO,CAAC,OAAkC;IACxD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AAC3B,CAAC;AAED,MAAM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AC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,KAAiB;IAC1C,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AAC5C,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,GAAW;IACpC,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;IAC7B,CAAC;IACD,IAAI,GAAG,KAAK,GAAG,CAAC,WAAW,EAAE,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;IACvC,CAAC;IACD,OAAO,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;AAClD,CAAC"}
package/dist/cose.d.ts ADDED
@@ -0,0 +1,44 @@
1
+ /** RFC 9052 EdDSA. Deprecated for this profile; use Ed25519 (-19). */
2
+ export declare const COSE_ALG_EDDSA = -8;
3
+ /** Ed25519, RFC 9864. */
4
+ export declare const COSE_ALG_ED25519 = -19;
5
+ /** COSE header label alg. */
6
+ export declare const COSE_HDR_ALG = 1;
7
+ /** COSE header label content type. */
8
+ export declare const COSE_HDR_CONTENT_TYPE = 3;
9
+ /** COSE header label kid. */
10
+ export declare const COSE_HDR_KID = 4;
11
+ export declare const CTY_RECEIPT = "application/cedulon-receipt+cbor";
12
+ export declare const CTY_CHECKPOINT = "application/cedulon-checkpoint+cbor";
13
+ export declare const CTY_MANIFEST = "application/cedulon-manifest+cbor";
14
+ export declare const CTY_INCLUSION = "application/cedulon-inclusion+cbor";
15
+ export declare const CTY_DECISION = "application/cedulon-decision+cbor";
16
+ export declare const CTY_COUNTERSIGN = "application/cedulon-countersign+cbor";
17
+ export type CoseSign1 = {
18
+ protectedHeader: Uint8Array;
19
+ unprotected: Record<string, never>;
20
+ payload: Uint8Array;
21
+ signature: Uint8Array;
22
+ };
23
+ export type ProtectedHeader = {
24
+ alg: number;
25
+ kid: Uint8Array;
26
+ contentType: string;
27
+ };
28
+ export declare function kidFromPublicKeyPem(publicKeyPem: string): Uint8Array;
29
+ export declare function publicKeyPemFromPrivate(privateKeyPem: string): string;
30
+ export declare function encodeProtectedHeader(header: ProtectedHeader): Uint8Array;
31
+ export declare function decodeProtectedHeader(bytes: Uint8Array): ProtectedHeader;
32
+ export declare function sigStructure(protectedHeader: Uint8Array, payload: Uint8Array): Uint8Array;
33
+ export declare function signCoseSign1(payload: Uint8Array, privateKeyPem: string, contentType: string): Uint8Array;
34
+ export declare function encodeCoseSign1(msg: CoseSign1): Uint8Array;
35
+ export declare function decodeCoseSign1(bytes: Uint8Array): CoseSign1;
36
+ export declare function verifyCoseSign1(bytes: Uint8Array, publicKeyPem: string, expectedContentType?: string): boolean;
37
+ export { bytesToHex as coseToHex, hexToBytes as coseFromHex } from "./cbor.ts";
38
+ /** RFC 8032 Ed25519 secret #1 — fixture only, never a production key. */
39
+ export declare const FIXTURE_ED25519_SEED_HEX = "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60";
40
+ export declare function fixtureEd25519Pems(): {
41
+ publicKeyPem: string;
42
+ privateKeyPem: string;
43
+ };
44
+ //# sourceMappingURL=cose.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cose.d.ts","sourceRoot":"","sources":["../src/cose.ts"],"names":[],"mappings":"AAGA,sEAAsE;AACtE,eAAO,MAAM,cAAc,KAAK,CAAC;AACjC,yBAAyB;AACzB,eAAO,MAAM,gBAAgB,MAAM,CAAC;AACpC,6BAA6B;AAC7B,eAAO,MAAM,YAAY,IAAI,CAAC;AAC9B,sCAAsC;AACtC,eAAO,MAAM,qBAAqB,IAAI,CAAC;AACvC,6BAA6B;AAC7B,eAAO,MAAM,YAAY,IAAI,CAAC;AAE9B,eAAO,MAAM,WAAW,qCAAqC,CAAC;AAC9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EAAE,UAAU,IAAI,SAAS,EAAE,UAAU,IAAI,WAAW,EAAE,MAAM,WAAW,CAAC;AAE/E,yEAAyE;AACzE,eAAO,MAAM,wBAAwB,qEAC+B,CAAC;AAErE,wBAAgB,kBAAkB,IAAI;IAAE,YAAY,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,MAAM,CAAA;CAAE,CASpF"}
package/dist/cose.js ADDED
@@ -0,0 +1,130 @@
1
+ import { createHash, createPrivateKey, createPublicKey, sign, verify } from "node:crypto";
2
+ import { asArray, asMap, cborMap, decodeCbor, encodeCbor, mapGet } from "./cbor.js";
3
+ /** RFC 9052 EdDSA. Deprecated for this profile; use Ed25519 (-19). */
4
+ export const COSE_ALG_EDDSA = -8;
5
+ /** Ed25519, RFC 9864. */
6
+ export const COSE_ALG_ED25519 = -19;
7
+ /** COSE header label alg. */
8
+ export const COSE_HDR_ALG = 1;
9
+ /** COSE header label content type. */
10
+ export const COSE_HDR_CONTENT_TYPE = 3;
11
+ /** COSE header label kid. */
12
+ export const COSE_HDR_KID = 4;
13
+ export const CTY_RECEIPT = "application/cedulon-receipt+cbor";
14
+ export const CTY_CHECKPOINT = "application/cedulon-checkpoint+cbor";
15
+ export const CTY_MANIFEST = "application/cedulon-manifest+cbor";
16
+ export const CTY_INCLUSION = "application/cedulon-inclusion+cbor";
17
+ export const CTY_DECISION = "application/cedulon-decision+cbor";
18
+ export const CTY_COUNTERSIGN = "application/cedulon-countersign+cbor";
19
+ export function kidFromPublicKeyPem(publicKeyPem) {
20
+ const der = createPublicKey(publicKeyPem).export({ type: "spki", format: "der" });
21
+ return new Uint8Array(createHash("sha256").update(der).digest().subarray(0, 8));
22
+ }
23
+ export function publicKeyPemFromPrivate(privateKeyPem) {
24
+ return createPublicKey(createPrivateKey(privateKeyPem))
25
+ .export({ type: "spki", format: "pem" })
26
+ .toString();
27
+ }
28
+ export function encodeProtectedHeader(header) {
29
+ return encodeCbor(cborMap([
30
+ [COSE_HDR_ALG, header.alg],
31
+ [COSE_HDR_CONTENT_TYPE, header.contentType],
32
+ [COSE_HDR_KID, header.kid],
33
+ ]));
34
+ }
35
+ export function decodeProtectedHeader(bytes) {
36
+ const map = asMap(decodeCbor(bytes));
37
+ const alg = mapGet(map, COSE_HDR_ALG);
38
+ const contentType = mapGet(map, COSE_HDR_CONTENT_TYPE);
39
+ const kid = mapGet(map, COSE_HDR_KID);
40
+ if (typeof alg !== "number" || typeof contentType !== "string" || !(kid instanceof Uint8Array)) {
41
+ throw new Error("cose-protected-header");
42
+ }
43
+ return { alg, contentType, kid };
44
+ }
45
+ export function sigStructure(protectedHeader, payload) {
46
+ return encodeCbor(["Signature1", protectedHeader, new Uint8Array(0), payload]);
47
+ }
48
+ export function signCoseSign1(payload, privateKeyPem, contentType) {
49
+ const publicKeyPem = publicKeyPemFromPrivate(privateKeyPem);
50
+ const protectedHeader = encodeProtectedHeader({
51
+ alg: COSE_ALG_ED25519,
52
+ kid: kidFromPublicKeyPem(publicKeyPem),
53
+ contentType,
54
+ });
55
+ const toBeSigned = sigStructure(protectedHeader, payload);
56
+ const signature = sign(null, Buffer.from(toBeSigned), privateKeyPem);
57
+ return encodeCoseSign1({
58
+ protectedHeader,
59
+ unprotected: {},
60
+ payload,
61
+ signature: new Uint8Array(signature),
62
+ });
63
+ }
64
+ export function encodeCoseSign1(msg) {
65
+ return encodeCbor([
66
+ msg.protectedHeader,
67
+ cborMap([]),
68
+ msg.payload,
69
+ msg.signature,
70
+ ]);
71
+ }
72
+ export function decodeCoseSign1(bytes) {
73
+ const arr = asArray(decodeCbor(bytes));
74
+ if (arr.length !== 4) {
75
+ throw new Error("cose-sign1-length");
76
+ }
77
+ if (!(arr[0] instanceof Uint8Array) || !(arr[2] instanceof Uint8Array) || !(arr[3] instanceof Uint8Array)) {
78
+ throw new Error("cose-sign1-types");
79
+ }
80
+ return {
81
+ protectedHeader: arr[0],
82
+ unprotected: {},
83
+ payload: arr[2],
84
+ signature: arr[3],
85
+ };
86
+ }
87
+ function bytesEqual(a, b) {
88
+ if (a.length !== b.length) {
89
+ return false;
90
+ }
91
+ let d = 0;
92
+ for (let i = 0; i < a.length; i += 1) {
93
+ d |= a[i] ^ b[i];
94
+ }
95
+ return d === 0;
96
+ }
97
+ export function verifyCoseSign1(bytes, publicKeyPem, expectedContentType) {
98
+ try {
99
+ const msg = decodeCoseSign1(bytes);
100
+ const header = decodeProtectedHeader(msg.protectedHeader);
101
+ if (header.alg !== COSE_ALG_ED25519) {
102
+ return false;
103
+ }
104
+ if (!bytesEqual(header.kid, kidFromPublicKeyPem(publicKeyPem))) {
105
+ return false;
106
+ }
107
+ if (expectedContentType && header.contentType !== expectedContentType) {
108
+ return false;
109
+ }
110
+ const toBeSigned = sigStructure(msg.protectedHeader, msg.payload);
111
+ return verify(null, Buffer.from(toBeSigned), publicKeyPem, Buffer.from(msg.signature));
112
+ }
113
+ catch {
114
+ return false;
115
+ }
116
+ }
117
+ export { bytesToHex as coseToHex, hexToBytes as coseFromHex } from "./cbor.js";
118
+ /** RFC 8032 Ed25519 secret #1 — fixture only, never a production key. */
119
+ export const FIXTURE_ED25519_SEED_HEX = "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60";
120
+ export function fixtureEd25519Pems() {
121
+ const seed = Buffer.from(FIXTURE_ED25519_SEED_HEX, "hex");
122
+ const pkcs8 = Buffer.concat([Buffer.from("302e020100300506032b657004220420", "hex"), seed]);
123
+ const privateKey = createPrivateKey({ key: pkcs8, format: "der", type: "pkcs8" });
124
+ const publicKey = createPublicKey(privateKey);
125
+ return {
126
+ privateKeyPem: privateKey.export({ type: "pkcs8", format: "pem" }).toString(),
127
+ publicKeyPem: publicKey.export({ type: "spki", format: "pem" }).toString(),
128
+ };
129
+ }
130
+ //# sourceMappingURL=cose.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cose.js","sourceRoot":"","sources":["../src/cose.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,eAAe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AY,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;IACzF,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,OAAO,EAAE,UAAU,IAAI,SAAS,EAAE,UAAU,IAAI,WAAW,EAAE,MAAM,WAAW,CAAC;AAE/E,yEAAyE;AACzE,MAAM,CAAC,MAAM,wBAAwB,GACnC,kEAAkE,CAAC;AAErE,MAAM,UAAU,kBAAkB;IAChC,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,wBAAwB,EAAE,KAAK,CAAC,CAAC;IAC1D,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,kCAAkC,EAAE,KAAK,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;IAC5F,MAAM,UAAU,GAAG,gBAAgB,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;IAClF,MAAM,SAAS,GAAG,eAAe,CAAC,UAAU,CAAC,CAAC;IAC9C,OAAO;QACL,aAAa,EAAE,UAAU,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC,QAAQ,EAAE;QAC7E,YAAY,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC,QAAQ,EAAE;KAC3E,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { asArray, asMap, bytesToHex, cborMap, compareEncodedKeys, decodeCbor, encodeCbor, hexToBytes, isCborMap, mapGet, type CborMap, type CborVal, } from "./cbor.ts";
2
+ export { COSE_ALG_ED25519, COSE_ALG_EDDSA, COSE_HDR_ALG, COSE_HDR_CONTENT_TYPE, COSE_HDR_KID, CTY_CHECKPOINT, CTY_COUNTERSIGN, CTY_DECISION, CTY_INCLUSION, CTY_MANIFEST, CTY_RECEIPT, coseFromHex, coseToHex, decodeCoseSign1, decodeProtectedHeader, encodeCoseSign1, encodeProtectedHeader, fixtureEd25519Pems, FIXTURE_ED25519_SEED_HEX, kidFromPublicKeyPem, publicKeyPemFromPrivate, sigStructure, signCoseSign1, verifyCoseSign1, type CoseSign1, type ProtectedHeader, } from "./cose.ts";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,KAAK,EACL,UAAU,EACV,OAAO,EACP,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,UAAU,EACV,SAAS,EACT,MAAM,EACN,KAAK,OAAO,EACZ,KAAK,OAAO,GACb,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,qBAAqB,EACrB,YAAY,EACZ,cAAc,EACd,eAAe,EACf,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,WAAW,EACX,WAAW,EACX,SAAS,EACT,eAAe,EACf,qBAAqB,EACrB,eAAe,EACf,qBAAqB,EACrB,kBAAkB,EAClB,wBAAwB,EACxB,mBAAmB,EACnB,uBAAuB,EACvB,YAAY,EACZ,aAAa,EACb,eAAe,EACf,KAAK,SAAS,EACd,KAAK,eAAe,GACrB,MAAM,WAAW,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { asArray, asMap, bytesToHex, cborMap, compareEncodedKeys, decodeCbor, encodeCbor, hexToBytes, isCborMap, mapGet, } from "./cbor.js";
2
+ export { COSE_ALG_ED25519, COSE_ALG_EDDSA, COSE_HDR_ALG, COSE_HDR_CONTENT_TYPE, COSE_HDR_KID, CTY_CHECKPOINT, CTY_COUNTERSIGN, CTY_DECISION, CTY_INCLUSION, CTY_MANIFEST, CTY_RECEIPT, coseFromHex, coseToHex, decodeCoseSign1, decodeProtectedHeader, encodeCoseSign1, encodeProtectedHeader, fixtureEd25519Pems, FIXTURE_ED25519_SEED_HEX, kidFromPublicKeyPem, publicKeyPemFromPrivate, sigStructure, signCoseSign1, verifyCoseSign1, } from "./cose.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,KAAK,EACL,UAAU,EACV,OAAO,EACP,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,UAAU,EACV,SAAS,EACT,MAAM,GAGP,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,qBAAqB,EACrB,YAAY,EACZ,cAAc,EACd,eAAe,EACf,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,WAAW,EACX,WAAW,EACX,SAAS,EACT,eAAe,EACf,qBAAqB,EACrB,eAAe,EACf,qBAAqB,EACrB,kBAAkB,EAClB,wBAAwB,EACxB,mBAAmB,EACnB,uBAAuB,EACvB,YAAY,EACZ,aAAa,EACb,eAAe,GAGhB,MAAM,WAAW,CAAC"}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@cedulon/cose",
3
+ "version": "0.1.0",
4
+ "description": "Deterministic CBOR and COSE_Sign1 (Ed25519) used by Cedulon.",
5
+ "license": "Apache-2.0",
6
+ "type": "module",
7
+ "engines": {
8
+ "node": ">=20"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/dogrucanemek-alt/cedulon.git",
13
+ "directory": "packages/cose"
14
+ },
15
+ "homepage": "https://github.com/dogrucanemek-alt/cedulon/tree/master/packages/cose",
16
+ "bugs": {
17
+ "url": "https://github.com/dogrucanemek-alt/cedulon/issues"
18
+ },
19
+ "keywords": [
20
+ "cedulon",
21
+ "cose",
22
+ "cbor",
23
+ "ed25519"
24
+ ],
25
+ "scripts": {
26
+ "build": "tsc -p tsconfig.build.json"
27
+ },
28
+ "main": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.js"
34
+ }
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "README.md",
39
+ "LICENSE"
40
+ ],
41
+ "publishConfig": {
42
+ "access": "public"
43
+ }
44
+ }