@cardanowall/crypto-core 0.0.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 +202 -0
- package/README.md +192 -0
- package/dist/aead.cjs +44 -0
- package/dist/aead.cjs.map +1 -0
- package/dist/aead.d.cts +38 -0
- package/dist/aead.d.ts +38 -0
- package/dist/aead.js +38 -0
- package/dist/aead.js.map +1 -0
- package/dist/cbor.cjs +69 -0
- package/dist/cbor.cjs.map +1 -0
- package/dist/cbor.d.cts +17 -0
- package/dist/cbor.d.ts +17 -0
- package/dist/cbor.js +64 -0
- package/dist/cbor.js.map +1 -0
- package/dist/cose.cjs +430 -0
- package/dist/cose.cjs.map +1 -0
- package/dist/cose.d.cts +72 -0
- package/dist/cose.d.ts +72 -0
- package/dist/cose.js +398 -0
- package/dist/cose.js.map +1 -0
- package/dist/hash.cjs +165 -0
- package/dist/hash.cjs.map +1 -0
- package/dist/hash.d.cts +30 -0
- package/dist/hash.d.ts +30 -0
- package/dist/hash.js +155 -0
- package/dist/hash.js.map +1 -0
- package/dist/index.cjs +1856 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +1759 -0
- package/dist/index.js.map +1 -0
- package/dist/kdf.cjs +26 -0
- package/dist/kdf.cjs.map +1 -0
- package/dist/kdf.d.cts +25 -0
- package/dist/kdf.d.ts +25 -0
- package/dist/kdf.js +23 -0
- package/dist/kdf.js.map +1 -0
- package/dist/kem.cjs +86 -0
- package/dist/kem.cjs.map +1 -0
- package/dist/kem.d.cts +47 -0
- package/dist/kem.d.ts +47 -0
- package/dist/kem.js +73 -0
- package/dist/kem.js.map +1 -0
- package/dist/merkle.cjs +284 -0
- package/dist/merkle.cjs.map +1 -0
- package/dist/merkle.d.cts +24 -0
- package/dist/merkle.d.ts +24 -0
- package/dist/merkle.js +279 -0
- package/dist/merkle.js.map +1 -0
- package/dist/recipient.cjs +141 -0
- package/dist/recipient.cjs.map +1 -0
- package/dist/recipient.d.cts +16 -0
- package/dist/recipient.d.ts +16 -0
- package/dist/recipient.js +135 -0
- package/dist/recipient.js.map +1 -0
- package/dist/sealed-poe.cjs +851 -0
- package/dist/sealed-poe.cjs.map +1 -0
- package/dist/sealed-poe.d.cts +134 -0
- package/dist/sealed-poe.d.ts +134 -0
- package/dist/sealed-poe.js +838 -0
- package/dist/sealed-poe.js.map +1 -0
- package/dist/seed-derive.cjs +129 -0
- package/dist/seed-derive.cjs.map +1 -0
- package/dist/seed-derive.d.cts +28 -0
- package/dist/seed-derive.d.ts +28 -0
- package/dist/seed-derive.js +101 -0
- package/dist/seed-derive.js.map +1 -0
- package/dist/sig.cjs +77 -0
- package/dist/sig.cjs.map +1 -0
- package/dist/sig.d.cts +17 -0
- package/dist/sig.d.ts +17 -0
- package/dist/sig.js +53 -0
- package/dist/sig.js.map +1 -0
- package/dist/util.cjs +36 -0
- package/dist/util.cjs.map +1 -0
- package/dist/util.d.cts +5 -0
- package/dist/util.d.ts +5 -0
- package/dist/util.js +33 -0
- package/dist/util.js.map +1 -0
- package/package.json +122 -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/README.md
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# @cardanowall/crypto-core — closed-catalogue cryptographic primitives for CIP-309
|
|
2
|
+
|
|
3
|
+
Low-level, independently auditable building blocks for [CIP-309](https://cips.cardano.org/)
|
|
4
|
+
Proof-of-Existence tooling: hash, KDF, signature, KEM, AEAD, canonical CBOR, COSE_Sign1,
|
|
5
|
+
deterministic seed derivation, multi-recipient sealed-PoE construction, envelope discovery,
|
|
6
|
+
RFC 9162 Merkle trees, and age-style recipient encoding.
|
|
7
|
+
|
|
8
|
+
## What it is
|
|
9
|
+
|
|
10
|
+
`crypto-core` is the bottom layer of the CIP-309 package family. It is a **closed catalogue**:
|
|
11
|
+
it exposes only the named, vetted algorithms the CIP-309 wire format references — there is no
|
|
12
|
+
general-purpose "bring your own algorithm" surface. Every construction is byte-pinned against
|
|
13
|
+
known-answer test vectors so an independent implementation can reproduce identical bytes.
|
|
14
|
+
|
|
15
|
+
The package is portable and standalone — it produces and consumes bytes and depends on no
|
|
16
|
+
gateway, operator, or server. It is the TypeScript reference; a byte-identical Python parity
|
|
17
|
+
twin (`@cardanowall/sdk-py`) and a Rust twin (the `cardanowall` crate) track the same
|
|
18
|
+
conformance vectors. The higher-level wire-format library (`@cardanowall/poe-standard`) and
|
|
19
|
+
the SDKs (`@cardanowall/sdk-ts`, `@cardanowall/sdk-py`) are built on top of these primitives.
|
|
20
|
+
|
|
21
|
+
Hybrid post-quantum is first-class: the X-Wing KEM (`mlkem768x25519`, ML-KEM-768 + X25519 per
|
|
22
|
+
draft-connolly-cfrg-xwing-kem) is a supported sealed-PoE branch alongside classical X25519, and
|
|
23
|
+
every seed-derived identity carries an X-Wing keypair so it can always *receive* hybrid records.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
The package is pre-1.0 and not yet published. Build it from the workspace:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
pnpm install
|
|
31
|
+
pnpm --filter @cardanowall/crypto-core typecheck
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Once published, it will install as:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
npm install @cardanowall/crypto-core # once published
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
It ships as ESM, targets modern Node.js and browsers, and uses Web Crypto / `crypto.getRandomValues`
|
|
41
|
+
for randomness. The cryptography is built on the audited [`@noble/*`](https://paulmillr.com/noble/)
|
|
42
|
+
primitives plus `@noble/post-quantum`, `cbor2`, and `hash-wasm`.
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
Import from the package root, or cherry-pick a subpath export (`@cardanowall/crypto-core/hash`,
|
|
47
|
+
`/seed-derive`, `/sealed-poe`, `/merkle`, …).
|
|
48
|
+
|
|
49
|
+
### Hash content for a Proof-of-Existence
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { dualHash } from '@cardanowall/crypto-core';
|
|
53
|
+
|
|
54
|
+
const { sha256, blake2b256 } = dualHash(new TextEncoder().encode('hello world'));
|
|
55
|
+
// sha256 and blake2b256 are 32-byte Uint8Arrays
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
For large content, stream it without buffering the whole input:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { dualHashStream } from '@cardanowall/crypto-core';
|
|
62
|
+
|
|
63
|
+
const { sha256, blake2b256 } = await dualHashStream(fileChunkAsyncIterable);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Derive an identity and sign
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import {
|
|
70
|
+
deriveEd25519KeypairFromSeed,
|
|
71
|
+
signEd25519,
|
|
72
|
+
verifyEd25519,
|
|
73
|
+
} from '@cardanowall/crypto-core';
|
|
74
|
+
|
|
75
|
+
const seed = crypto.getRandomValues(new Uint8Array(32)); // 32-byte identity seed
|
|
76
|
+
const { secretKey, publicKey } = deriveEd25519KeypairFromSeed(seed);
|
|
77
|
+
|
|
78
|
+
const message = new TextEncoder().encode('attest this');
|
|
79
|
+
const signature = signEd25519({ seed: secretKey, message });
|
|
80
|
+
const ok = verifyEd25519({ publicKey, message, signature }); // true
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`deriveX25519KeypairFromSeed` and `deriveMlKem768X25519KeypairFromSeed` derive the encryption
|
|
84
|
+
keypairs from the same 32-byte seed using fixed CIP-309 HKDF labels — so one passphrase-protected
|
|
85
|
+
seed yields a complete, deterministic identity.
|
|
86
|
+
|
|
87
|
+
### Seal a record to multiple recipients and unwrap it
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import {
|
|
91
|
+
deriveX25519KeypairFromSeed,
|
|
92
|
+
eciesSealedPoeWrap,
|
|
93
|
+
eciesSealedPoeUnwrap,
|
|
94
|
+
} from '@cardanowall/crypto-core';
|
|
95
|
+
|
|
96
|
+
const recipient = deriveX25519KeypairFromSeed(crypto.getRandomValues(new Uint8Array(32)));
|
|
97
|
+
|
|
98
|
+
// Default KEM is classical X25519; pass kem: 'mlkem768x25519' for the hybrid PQ branch.
|
|
99
|
+
const { envelope, ciphertext } = eciesSealedPoeWrap({
|
|
100
|
+
plaintext: new TextEncoder().encode('secret payload'),
|
|
101
|
+
recipientPublicKeys: [recipient.publicKey],
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const result = eciesSealedPoeUnwrap({
|
|
105
|
+
envelope,
|
|
106
|
+
ciphertext,
|
|
107
|
+
recipientSecretKey: recipient.secretKey,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
if (result.matched) {
|
|
111
|
+
// result.plaintext recovers the original bytes
|
|
112
|
+
} else {
|
|
113
|
+
// result.reason is 'WRONG_RECIPIENT_KEY' | 'TAMPERED_HEADER' | 'TAMPERED_CIPHERTEXT'
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Recipients holding a rotated identity (current key plus archived keys, across both KEMs) pass the
|
|
118
|
+
whole `recipientKeyBundle` instead of a single key; the trial-decrypt loop is constant-time over the
|
|
119
|
+
slot count by default. `eciesSealedPoeTrialDecrypt` recovers the content key and slot index *without*
|
|
120
|
+
the off-chain ciphertext — the operation an inbox scanner runs to discover readable records before
|
|
121
|
+
fetching their blobs.
|
|
122
|
+
|
|
123
|
+
### Address a recipient as an age-style string
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import {
|
|
127
|
+
encodeAgeX25519Recipient,
|
|
128
|
+
encodeAgeXWingRecipient,
|
|
129
|
+
parseAgeRecipient,
|
|
130
|
+
} from '@cardanowall/crypto-core';
|
|
131
|
+
|
|
132
|
+
const classical = encodeAgeX25519Recipient(recipient.publicKey); // "age1…"
|
|
133
|
+
const hybrid = encodeAgeXWingRecipient(xwingPublicKey); // "age1pqc…"
|
|
134
|
+
|
|
135
|
+
const parsed = parseAgeRecipient(classical);
|
|
136
|
+
// { kem: 'x25519' | 'mlkem768x25519', publicKey: Uint8Array } — routed on the bech32 prefix
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## API overview — the primitive catalogue
|
|
140
|
+
|
|
141
|
+
Each group is also a subpath export. Names below are the actual exported symbols.
|
|
142
|
+
|
|
143
|
+
| Group | Catalogue |
|
|
144
|
+
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
145
|
+
| `hash` | `sha256` (FIPS 180-4), `blake2b256` / `blake2b224` (RFC 7693, CIP-19), `dualHash` / `dualHashStream`, `merkleSha2256Root` and inclusion-proof helpers (RFC 9162) |
|
|
146
|
+
| `kdf` | `hkdfSha256` (RFC 5869), `argon2idV13` (RFC 9106), `pbkdf2Sha256` (RFC 8018) |
|
|
147
|
+
| `sig` | `signEd25519` / `verifyEd25519` / `getPublicKeyEd25519` (RFC 8032, strict non-cofactored verification), identity-link challenge builder |
|
|
148
|
+
| `kem` | `x25519Keygen` / `x25519PublicKey` / `x25519Ecdh` (RFC 7748, low-order-point rejection); `mlkem768x25519Keygen` / `…Encapsulate` / `…Decapsulate` (X-Wing hybrid PQ KEM) |
|
|
149
|
+
| `aead` | `chacha20Poly1305*` (RFC 8439), `xchacha20Poly1305*`, `aes256Gcm*` |
|
|
150
|
+
| `cbor` | `encodeCanonicalCbor` / `decodeCanonicalCbor` (RFC 8949 §4.2.1) plus a permissive outer-wire decoder |
|
|
151
|
+
| `cose` | `coseSign1Cip309Build` / `coseSign1Cip309Verify`, `encodeCoseSign1` / `decodeCoseSign1`, `buildCip309SigStructure` (COSE_Sign1, RFC 9052) |
|
|
152
|
+
| `seed-derive` | `deriveEd25519KeypairFromSeed`, `deriveX25519KeypairFromSeed`, `deriveMlKem768X25519KeypairFromSeed` — deterministic long-term identity keys from one 32-byte seed |
|
|
153
|
+
| `discovery` | `derivePassphraseDiscoveryTag` (Argon2id → HMAC), `deriveWebauthnDiscoveryTagFromPrf` — envelope-discovery tags |
|
|
154
|
+
| `sealed-poe` | `eciesSealedPoeWrap` / `eciesSealedPoeUnwrap` / `eciesSealedPoeTrialDecrypt`, the slots codec, and `RecipientKeyBundle` (age-style ECIES with AEAD-bound slots) |
|
|
155
|
+
| `merkle` | `encodeLeavesList` / `decodeLeavesList` — canonical-CBOR codec for the off-chain Merkle leaves-list artefact |
|
|
156
|
+
| `recipient` | `encodeAgeX25519Recipient` / `encodeAgeXWingRecipient` / `parseAgeRecipient`, bech32 codec |
|
|
157
|
+
| `util` | `compareCt` (constant-time comparison), `hexToBytes` |
|
|
158
|
+
|
|
159
|
+
See `src/index.ts` and each submodule's `index.ts` for the exhaustive surface.
|
|
160
|
+
|
|
161
|
+
## Cross-implementation parity
|
|
162
|
+
|
|
163
|
+
`crypto-core` is the TypeScript reference. Its outputs are byte-identical to the Python twin
|
|
164
|
+
(`@cardanowall/sdk-py`) and the Rust twin (the `cardanowall` crate) across a shared set of
|
|
165
|
+
known-answer test vectors — public standards-derived vectors (RFC 7748, RFC 8032, RFC 8439,
|
|
166
|
+
RFC 8949, RFC 9106, FIPS 180-4) alongside the CIP-309 record, envelope, sealed-PoE, and
|
|
167
|
+
seed-derivation vectors. A divergence between any two implementations turns the parity check red.
|
|
168
|
+
This is what lets a record sealed or signed by one implementation be opened or verified by any
|
|
169
|
+
other, on any platform.
|
|
170
|
+
|
|
171
|
+
## Standard / service independence
|
|
172
|
+
|
|
173
|
+
These primitives carry no notion of a publisher or a server. A sealed PoE is opened, a record
|
|
174
|
+
signature is verified, and a Merkle proof is checked entirely from bytes the caller already holds —
|
|
175
|
+
the on-chain metadata, the optional content (or ciphertext) blob, and a recipient's own private
|
|
176
|
+
key. There is no issuer server in the loop at any step. `cardanowall.com` is one example deployment
|
|
177
|
+
of CIP-309 tooling; nothing here depends on it.
|
|
178
|
+
|
|
179
|
+
## Relation to the other packages
|
|
180
|
+
|
|
181
|
+
- **`@cardanowall/crypto-core`** (this package) — the closed-catalogue primitives; portable and
|
|
182
|
+
independently auditable building blocks.
|
|
183
|
+
- **`@cardanowall/poe-standard`** — the CIP-309 wire-format library: record schema, canonical-CBOR
|
|
184
|
+
encoder, pure structural validator, and the error-code catalogue.
|
|
185
|
+
- **`@cardanowall/sdk-ts`** — the browser + Node TypeScript SDK: the standalone verifier, the
|
|
186
|
+
gateway-agnostic HTTP client, off-host signing, and seed-derived identity helpers.
|
|
187
|
+
- **`@cardanowall/sdk-py`** — the Python SDK: a byte-identical parity twin of `sdk-ts`.
|
|
188
|
+
- **`cardanowall`** (Rust crate) — the Rust SDK twin; blocking HTTP, secure-by-default.
|
|
189
|
+
|
|
190
|
+
## License
|
|
191
|
+
|
|
192
|
+
Apache-2.0
|
package/dist/aead.cjs
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chacha_js = require('@noble/ciphers/chacha.js');
|
|
4
|
+
|
|
5
|
+
// src/aead/chacha20-poly1305.ts
|
|
6
|
+
|
|
7
|
+
// src/aead/errors.ts
|
|
8
|
+
var AeadVerificationError = class extends Error {
|
|
9
|
+
code = "aead_verification_failed";
|
|
10
|
+
constructor(message, options) {
|
|
11
|
+
super(message, options);
|
|
12
|
+
this.name = "AeadVerificationError";
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// src/aead/chacha20-poly1305.ts
|
|
17
|
+
function chacha20Poly1305Encrypt(opts) {
|
|
18
|
+
return chacha_js.chacha20poly1305(opts.key, opts.nonce, opts.aad).encrypt(opts.plaintext);
|
|
19
|
+
}
|
|
20
|
+
function chacha20Poly1305Decrypt(opts) {
|
|
21
|
+
try {
|
|
22
|
+
return chacha_js.chacha20poly1305(opts.key, opts.nonce, opts.aad).decrypt(opts.ciphertext);
|
|
23
|
+
} catch (cause) {
|
|
24
|
+
throw new AeadVerificationError("chacha20-poly1305 decrypt failed", { cause });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function xchacha20Poly1305Encrypt(opts) {
|
|
28
|
+
return chacha_js.xchacha20poly1305(opts.key, opts.nonce, opts.aad).encrypt(opts.plaintext);
|
|
29
|
+
}
|
|
30
|
+
function xchacha20Poly1305Decrypt(opts) {
|
|
31
|
+
try {
|
|
32
|
+
return chacha_js.xchacha20poly1305(opts.key, opts.nonce, opts.aad).decrypt(opts.ciphertext);
|
|
33
|
+
} catch (cause) {
|
|
34
|
+
throw new AeadVerificationError("xchacha20-poly1305 decrypt failed", { cause });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
exports.AeadVerificationError = AeadVerificationError;
|
|
39
|
+
exports.chacha20Poly1305Decrypt = chacha20Poly1305Decrypt;
|
|
40
|
+
exports.chacha20Poly1305Encrypt = chacha20Poly1305Encrypt;
|
|
41
|
+
exports.xchacha20Poly1305Decrypt = xchacha20Poly1305Decrypt;
|
|
42
|
+
exports.xchacha20Poly1305Encrypt = xchacha20Poly1305Encrypt;
|
|
43
|
+
//# sourceMappingURL=aead.cjs.map
|
|
44
|
+
//# sourceMappingURL=aead.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/aead/errors.ts","../src/aead/chacha20-poly1305.ts","../src/aead/xchacha20-poly1305.ts"],"names":["chacha20poly1305","xchacha20poly1305"],"mappings":";;;;;;;AAAO,IAAM,qBAAA,GAAN,cAAoC,KAAA,CAAM;AAAA,EACtC,IAAA,GAAe,0BAAA;AAAA,EAExB,WAAA,CAAY,SAAiB,OAAA,EAA+B;AAC1D,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AACtB,IAAA,IAAA,CAAK,IAAA,GAAO,uBAAA;AAAA,EACd;AACF;;;ACWO,SAAS,wBAAwB,IAAA,EAA+C;AACrF,EAAA,OAAOA,0BAAA,CAAiB,IAAA,CAAK,GAAA,EAAK,IAAA,CAAK,KAAA,EAAO,KAAK,GAAG,CAAA,CAAE,OAAA,CAAQ,IAAA,CAAK,SAAS,CAAA;AAChF;AAEO,SAAS,wBAAwB,IAAA,EAA+C;AACrF,EAAA,IAAI;AACF,IAAA,OAAOA,0BAAA,CAAiB,IAAA,CAAK,GAAA,EAAK,IAAA,CAAK,KAAA,EAAO,KAAK,GAAG,CAAA,CAAE,OAAA,CAAQ,IAAA,CAAK,UAAU,CAAA;AAAA,EACjF,SAAS,KAAA,EAAO;AACd,IAAA,MAAM,IAAI,qBAAA,CAAsB,kCAAA,EAAoC,EAAE,OAAO,CAAA;AAAA,EAC/E;AACF;ACVO,SAAS,yBAAyB,IAAA,EAAgD;AACvF,EAAA,OAAOC,2BAAA,CAAkB,IAAA,CAAK,GAAA,EAAK,IAAA,CAAK,KAAA,EAAO,KAAK,GAAG,CAAA,CAAE,OAAA,CAAQ,IAAA,CAAK,SAAS,CAAA;AACjF;AAEO,SAAS,yBAAyB,IAAA,EAAgD;AACvF,EAAA,IAAI;AACF,IAAA,OAAOA,2BAAA,CAAkB,IAAA,CAAK,GAAA,EAAK,IAAA,CAAK,KAAA,EAAO,KAAK,GAAG,CAAA,CAAE,OAAA,CAAQ,IAAA,CAAK,UAAU,CAAA;AAAA,EAClF,SAAS,KAAA,EAAO;AACd,IAAA,MAAM,IAAI,qBAAA,CAAsB,mCAAA,EAAqC,EAAE,OAAO,CAAA;AAAA,EAChF;AACF","file":"aead.cjs","sourcesContent":["export class AeadVerificationError extends Error {\n readonly code: string = 'aead_verification_failed';\n\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, options);\n this.name = 'AeadVerificationError';\n }\n}\n","import { chacha20poly1305 } from '@noble/ciphers/chacha.js';\n\nimport { AeadVerificationError } from './errors';\n\nexport interface ChaCha20Poly1305EncryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly plaintext: Uint8Array;\n}\n\nexport interface ChaCha20Poly1305DecryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly ciphertext: Uint8Array;\n}\n\nexport function chacha20Poly1305Encrypt(opts: ChaCha20Poly1305EncryptOpts): Uint8Array {\n return chacha20poly1305(opts.key, opts.nonce, opts.aad).encrypt(opts.plaintext);\n}\n\nexport function chacha20Poly1305Decrypt(opts: ChaCha20Poly1305DecryptOpts): Uint8Array {\n try {\n return chacha20poly1305(opts.key, opts.nonce, opts.aad).decrypt(opts.ciphertext);\n } catch (cause) {\n throw new AeadVerificationError('chacha20-poly1305 decrypt failed', { cause });\n }\n}\n","import { xchacha20poly1305 } from '@noble/ciphers/chacha.js';\n\nimport { AeadVerificationError } from './errors';\n\nexport interface XChaCha20Poly1305EncryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly plaintext: Uint8Array;\n}\n\nexport interface XChaCha20Poly1305DecryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly ciphertext: Uint8Array;\n}\n\nexport function xchacha20Poly1305Encrypt(opts: XChaCha20Poly1305EncryptOpts): Uint8Array {\n return xchacha20poly1305(opts.key, opts.nonce, opts.aad).encrypt(opts.plaintext);\n}\n\nexport function xchacha20Poly1305Decrypt(opts: XChaCha20Poly1305DecryptOpts): Uint8Array {\n try {\n return xchacha20poly1305(opts.key, opts.nonce, opts.aad).decrypt(opts.ciphertext);\n } catch (cause) {\n throw new AeadVerificationError('xchacha20-poly1305 decrypt failed', { cause });\n }\n}\n"]}
|
package/dist/aead.d.cts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
interface ChaCha20Poly1305EncryptOpts {
|
|
2
|
+
readonly key: Uint8Array;
|
|
3
|
+
readonly nonce: Uint8Array;
|
|
4
|
+
readonly aad: Uint8Array;
|
|
5
|
+
readonly plaintext: Uint8Array;
|
|
6
|
+
}
|
|
7
|
+
interface ChaCha20Poly1305DecryptOpts {
|
|
8
|
+
readonly key: Uint8Array;
|
|
9
|
+
readonly nonce: Uint8Array;
|
|
10
|
+
readonly aad: Uint8Array;
|
|
11
|
+
readonly ciphertext: Uint8Array;
|
|
12
|
+
}
|
|
13
|
+
declare function chacha20Poly1305Encrypt(opts: ChaCha20Poly1305EncryptOpts): Uint8Array;
|
|
14
|
+
declare function chacha20Poly1305Decrypt(opts: ChaCha20Poly1305DecryptOpts): Uint8Array;
|
|
15
|
+
|
|
16
|
+
interface XChaCha20Poly1305EncryptOpts {
|
|
17
|
+
readonly key: Uint8Array;
|
|
18
|
+
readonly nonce: Uint8Array;
|
|
19
|
+
readonly aad: Uint8Array;
|
|
20
|
+
readonly plaintext: Uint8Array;
|
|
21
|
+
}
|
|
22
|
+
interface XChaCha20Poly1305DecryptOpts {
|
|
23
|
+
readonly key: Uint8Array;
|
|
24
|
+
readonly nonce: Uint8Array;
|
|
25
|
+
readonly aad: Uint8Array;
|
|
26
|
+
readonly ciphertext: Uint8Array;
|
|
27
|
+
}
|
|
28
|
+
declare function xchacha20Poly1305Encrypt(opts: XChaCha20Poly1305EncryptOpts): Uint8Array;
|
|
29
|
+
declare function xchacha20Poly1305Decrypt(opts: XChaCha20Poly1305DecryptOpts): Uint8Array;
|
|
30
|
+
|
|
31
|
+
declare class AeadVerificationError extends Error {
|
|
32
|
+
readonly code: string;
|
|
33
|
+
constructor(message: string, options?: {
|
|
34
|
+
cause?: unknown;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { AeadVerificationError, type ChaCha20Poly1305DecryptOpts, type ChaCha20Poly1305EncryptOpts, type XChaCha20Poly1305DecryptOpts, type XChaCha20Poly1305EncryptOpts, chacha20Poly1305Decrypt, chacha20Poly1305Encrypt, xchacha20Poly1305Decrypt, xchacha20Poly1305Encrypt };
|
package/dist/aead.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
interface ChaCha20Poly1305EncryptOpts {
|
|
2
|
+
readonly key: Uint8Array;
|
|
3
|
+
readonly nonce: Uint8Array;
|
|
4
|
+
readonly aad: Uint8Array;
|
|
5
|
+
readonly plaintext: Uint8Array;
|
|
6
|
+
}
|
|
7
|
+
interface ChaCha20Poly1305DecryptOpts {
|
|
8
|
+
readonly key: Uint8Array;
|
|
9
|
+
readonly nonce: Uint8Array;
|
|
10
|
+
readonly aad: Uint8Array;
|
|
11
|
+
readonly ciphertext: Uint8Array;
|
|
12
|
+
}
|
|
13
|
+
declare function chacha20Poly1305Encrypt(opts: ChaCha20Poly1305EncryptOpts): Uint8Array;
|
|
14
|
+
declare function chacha20Poly1305Decrypt(opts: ChaCha20Poly1305DecryptOpts): Uint8Array;
|
|
15
|
+
|
|
16
|
+
interface XChaCha20Poly1305EncryptOpts {
|
|
17
|
+
readonly key: Uint8Array;
|
|
18
|
+
readonly nonce: Uint8Array;
|
|
19
|
+
readonly aad: Uint8Array;
|
|
20
|
+
readonly plaintext: Uint8Array;
|
|
21
|
+
}
|
|
22
|
+
interface XChaCha20Poly1305DecryptOpts {
|
|
23
|
+
readonly key: Uint8Array;
|
|
24
|
+
readonly nonce: Uint8Array;
|
|
25
|
+
readonly aad: Uint8Array;
|
|
26
|
+
readonly ciphertext: Uint8Array;
|
|
27
|
+
}
|
|
28
|
+
declare function xchacha20Poly1305Encrypt(opts: XChaCha20Poly1305EncryptOpts): Uint8Array;
|
|
29
|
+
declare function xchacha20Poly1305Decrypt(opts: XChaCha20Poly1305DecryptOpts): Uint8Array;
|
|
30
|
+
|
|
31
|
+
declare class AeadVerificationError extends Error {
|
|
32
|
+
readonly code: string;
|
|
33
|
+
constructor(message: string, options?: {
|
|
34
|
+
cause?: unknown;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { AeadVerificationError, type ChaCha20Poly1305DecryptOpts, type ChaCha20Poly1305EncryptOpts, type XChaCha20Poly1305DecryptOpts, type XChaCha20Poly1305EncryptOpts, chacha20Poly1305Decrypt, chacha20Poly1305Encrypt, xchacha20Poly1305Decrypt, xchacha20Poly1305Encrypt };
|
package/dist/aead.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { chacha20poly1305, xchacha20poly1305 } from '@noble/ciphers/chacha.js';
|
|
2
|
+
|
|
3
|
+
// src/aead/chacha20-poly1305.ts
|
|
4
|
+
|
|
5
|
+
// src/aead/errors.ts
|
|
6
|
+
var AeadVerificationError = class extends Error {
|
|
7
|
+
code = "aead_verification_failed";
|
|
8
|
+
constructor(message, options) {
|
|
9
|
+
super(message, options);
|
|
10
|
+
this.name = "AeadVerificationError";
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// src/aead/chacha20-poly1305.ts
|
|
15
|
+
function chacha20Poly1305Encrypt(opts) {
|
|
16
|
+
return chacha20poly1305(opts.key, opts.nonce, opts.aad).encrypt(opts.plaintext);
|
|
17
|
+
}
|
|
18
|
+
function chacha20Poly1305Decrypt(opts) {
|
|
19
|
+
try {
|
|
20
|
+
return chacha20poly1305(opts.key, opts.nonce, opts.aad).decrypt(opts.ciphertext);
|
|
21
|
+
} catch (cause) {
|
|
22
|
+
throw new AeadVerificationError("chacha20-poly1305 decrypt failed", { cause });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function xchacha20Poly1305Encrypt(opts) {
|
|
26
|
+
return xchacha20poly1305(opts.key, opts.nonce, opts.aad).encrypt(opts.plaintext);
|
|
27
|
+
}
|
|
28
|
+
function xchacha20Poly1305Decrypt(opts) {
|
|
29
|
+
try {
|
|
30
|
+
return xchacha20poly1305(opts.key, opts.nonce, opts.aad).decrypt(opts.ciphertext);
|
|
31
|
+
} catch (cause) {
|
|
32
|
+
throw new AeadVerificationError("xchacha20-poly1305 decrypt failed", { cause });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { AeadVerificationError, chacha20Poly1305Decrypt, chacha20Poly1305Encrypt, xchacha20Poly1305Decrypt, xchacha20Poly1305Encrypt };
|
|
37
|
+
//# sourceMappingURL=aead.js.map
|
|
38
|
+
//# sourceMappingURL=aead.js.map
|
package/dist/aead.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/aead/errors.ts","../src/aead/chacha20-poly1305.ts","../src/aead/xchacha20-poly1305.ts"],"names":[],"mappings":";;;;;AAAO,IAAM,qBAAA,GAAN,cAAoC,KAAA,CAAM;AAAA,EACtC,IAAA,GAAe,0BAAA;AAAA,EAExB,WAAA,CAAY,SAAiB,OAAA,EAA+B;AAC1D,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AACtB,IAAA,IAAA,CAAK,IAAA,GAAO,uBAAA;AAAA,EACd;AACF;;;ACWO,SAAS,wBAAwB,IAAA,EAA+C;AACrF,EAAA,OAAO,gBAAA,CAAiB,IAAA,CAAK,GAAA,EAAK,IAAA,CAAK,KAAA,EAAO,KAAK,GAAG,CAAA,CAAE,OAAA,CAAQ,IAAA,CAAK,SAAS,CAAA;AAChF;AAEO,SAAS,wBAAwB,IAAA,EAA+C;AACrF,EAAA,IAAI;AACF,IAAA,OAAO,gBAAA,CAAiB,IAAA,CAAK,GAAA,EAAK,IAAA,CAAK,KAAA,EAAO,KAAK,GAAG,CAAA,CAAE,OAAA,CAAQ,IAAA,CAAK,UAAU,CAAA;AAAA,EACjF,SAAS,KAAA,EAAO;AACd,IAAA,MAAM,IAAI,qBAAA,CAAsB,kCAAA,EAAoC,EAAE,OAAO,CAAA;AAAA,EAC/E;AACF;ACVO,SAAS,yBAAyB,IAAA,EAAgD;AACvF,EAAA,OAAO,iBAAA,CAAkB,IAAA,CAAK,GAAA,EAAK,IAAA,CAAK,KAAA,EAAO,KAAK,GAAG,CAAA,CAAE,OAAA,CAAQ,IAAA,CAAK,SAAS,CAAA;AACjF;AAEO,SAAS,yBAAyB,IAAA,EAAgD;AACvF,EAAA,IAAI;AACF,IAAA,OAAO,iBAAA,CAAkB,IAAA,CAAK,GAAA,EAAK,IAAA,CAAK,KAAA,EAAO,KAAK,GAAG,CAAA,CAAE,OAAA,CAAQ,IAAA,CAAK,UAAU,CAAA;AAAA,EAClF,SAAS,KAAA,EAAO;AACd,IAAA,MAAM,IAAI,qBAAA,CAAsB,mCAAA,EAAqC,EAAE,OAAO,CAAA;AAAA,EAChF;AACF","file":"aead.js","sourcesContent":["export class AeadVerificationError extends Error {\n readonly code: string = 'aead_verification_failed';\n\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, options);\n this.name = 'AeadVerificationError';\n }\n}\n","import { chacha20poly1305 } from '@noble/ciphers/chacha.js';\n\nimport { AeadVerificationError } from './errors';\n\nexport interface ChaCha20Poly1305EncryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly plaintext: Uint8Array;\n}\n\nexport interface ChaCha20Poly1305DecryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly ciphertext: Uint8Array;\n}\n\nexport function chacha20Poly1305Encrypt(opts: ChaCha20Poly1305EncryptOpts): Uint8Array {\n return chacha20poly1305(opts.key, opts.nonce, opts.aad).encrypt(opts.plaintext);\n}\n\nexport function chacha20Poly1305Decrypt(opts: ChaCha20Poly1305DecryptOpts): Uint8Array {\n try {\n return chacha20poly1305(opts.key, opts.nonce, opts.aad).decrypt(opts.ciphertext);\n } catch (cause) {\n throw new AeadVerificationError('chacha20-poly1305 decrypt failed', { cause });\n }\n}\n","import { xchacha20poly1305 } from '@noble/ciphers/chacha.js';\n\nimport { AeadVerificationError } from './errors';\n\nexport interface XChaCha20Poly1305EncryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly plaintext: Uint8Array;\n}\n\nexport interface XChaCha20Poly1305DecryptOpts {\n readonly key: Uint8Array;\n readonly nonce: Uint8Array;\n readonly aad: Uint8Array;\n readonly ciphertext: Uint8Array;\n}\n\nexport function xchacha20Poly1305Encrypt(opts: XChaCha20Poly1305EncryptOpts): Uint8Array {\n return xchacha20poly1305(opts.key, opts.nonce, opts.aad).encrypt(opts.plaintext);\n}\n\nexport function xchacha20Poly1305Decrypt(opts: XChaCha20Poly1305DecryptOpts): Uint8Array {\n try {\n return xchacha20poly1305(opts.key, opts.nonce, opts.aad).decrypt(opts.ciphertext);\n } catch (cause) {\n throw new AeadVerificationError('xchacha20-poly1305 decrypt failed', { cause });\n }\n}\n"]}
|
package/dist/cbor.cjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var cbor2 = require('cbor2');
|
|
4
|
+
var sorts = require('cbor2/sorts');
|
|
5
|
+
|
|
6
|
+
// src/cbor/canonical.ts
|
|
7
|
+
|
|
8
|
+
// src/cbor/errors.ts
|
|
9
|
+
var CanonicalCborError = class extends Error {
|
|
10
|
+
code;
|
|
11
|
+
constructor(code, message, options) {
|
|
12
|
+
super(message, options);
|
|
13
|
+
this.name = "CanonicalCborError";
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/cbor/canonical.ts
|
|
19
|
+
function encodeCanonicalCbor(value) {
|
|
20
|
+
return cbor2.encode(value, {
|
|
21
|
+
cde: true,
|
|
22
|
+
collapseBigInts: true,
|
|
23
|
+
rejectDuplicateKeys: true,
|
|
24
|
+
sortKeys: sorts.sortCoreDeterministic
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function decodeCanonicalCbor(bytes) {
|
|
28
|
+
try {
|
|
29
|
+
return cbor2.decode(bytes, {
|
|
30
|
+
...cbor2.cdeDecodeOptions,
|
|
31
|
+
rejectStreaming: true,
|
|
32
|
+
rejectDuplicateKeys: true,
|
|
33
|
+
// A CIP-309 record carries integers, byte/text strings, arrays, maps and
|
|
34
|
+
// `null` — and nothing else. Without these rejections the major-type-7
|
|
35
|
+
// surface leaks into the decoder: a float16/32/64 that happens to hold an
|
|
36
|
+
// integral value (e.g. 1.0) silently decodes to the integer 1 and passes
|
|
37
|
+
// a `z.literal(1)` / Number.isInteger schema check, so two byte strings
|
|
38
|
+
// that are NOT byte-identical canonicalise to the same record. That
|
|
39
|
+
// breaks the cross-implementation parity invariant (the Python twin
|
|
40
|
+
// already rejects non-integer `v` / `enc.scheme` outright). Reject the
|
|
41
|
+
// whole non-record surface — floats, negative zero, undefined, and
|
|
42
|
+
// non-{true,false,null} simple values — so any such input surfaces as
|
|
43
|
+
// MALFORMED_CBOR via mapDecodeError rather than decoding to a look-alike.
|
|
44
|
+
rejectFloats: true,
|
|
45
|
+
rejectNegativeZero: true,
|
|
46
|
+
rejectUndefined: true,
|
|
47
|
+
rejectSimple: true
|
|
48
|
+
});
|
|
49
|
+
} catch (cause) {
|
|
50
|
+
throw mapDecodeError(cause);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function mapDecodeError(cause) {
|
|
54
|
+
const message = cause instanceof Error ? cause.message : String(cause);
|
|
55
|
+
const lower = message.toLowerCase();
|
|
56
|
+
const isIndefinite = lower.includes("streaming") || lower.includes("indefinite");
|
|
57
|
+
const detail = isIndefinite ? `indefinite-length items are not permitted in canonical CBOR: ${message}` : message;
|
|
58
|
+
return new CanonicalCborError("MALFORMED_CBOR", `cbor decode failed: ${detail}`, { cause });
|
|
59
|
+
}
|
|
60
|
+
function decodeCbor(bytes) {
|
|
61
|
+
return cbor2.decode(bytes);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
exports.CanonicalCborError = CanonicalCborError;
|
|
65
|
+
exports.decodeCanonicalCbor = decodeCanonicalCbor;
|
|
66
|
+
exports.decodeCbor = decodeCbor;
|
|
67
|
+
exports.encodeCanonicalCbor = encodeCanonicalCbor;
|
|
68
|
+
//# sourceMappingURL=cbor.cjs.map
|
|
69
|
+
//# sourceMappingURL=cbor.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/cbor/errors.ts","../src/cbor/canonical.ts","../src/cbor/permissive.ts"],"names":["encode","sortCoreDeterministic","decode","cdeDecodeOptions"],"mappings":";;;;;;;;AAQO,IAAM,kBAAA,GAAN,cAAiC,KAAA,CAAM;AAAA,EACnC,IAAA;AAAA,EAET,WAAA,CAAY,IAAA,EAA8B,OAAA,EAAiB,OAAA,EAA+B;AACxF,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AACtB,IAAA,IAAA,CAAK,IAAA,GAAO,oBAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AAAA,EACd;AACF;;;ACAO,SAAS,oBAAoB,KAAA,EAAuC;AACzE,EAAA,OAAOA,aAAO,KAAA,EAAO;AAAA,IACnB,GAAA,EAAK,IAAA;AAAA,IACL,eAAA,EAAiB,IAAA;AAAA,IACjB,mBAAA,EAAqB,IAAA;AAAA,IACrB,QAAA,EAAUC;AAAA,GACX,CAAA;AACH;AAEO,SAAS,oBAAoB,KAAA,EAA4B;AAC9D,EAAA,IAAI;AACF,IAAA,OAAOC,aAAO,KAAA,EAAO;AAAA,MACnB,GAAGC,sBAAA;AAAA,MACH,eAAA,EAAiB,IAAA;AAAA,MACjB,mBAAA,EAAqB,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAYrB,YAAA,EAAc,IAAA;AAAA,MACd,kBAAA,EAAoB,IAAA;AAAA,MACpB,eAAA,EAAiB,IAAA;AAAA,MACjB,YAAA,EAAc;AAAA,KACf,CAAA;AAAA,EACH,SAAS,KAAA,EAAO;AACd,IAAA,MAAM,eAAe,KAAK,CAAA;AAAA,EAC5B;AACF;AAEA,SAAS,eAAe,KAAA,EAAoC;AAC1D,EAAA,MAAM,UAAU,KAAA,YAAiB,KAAA,GAAQ,KAAA,CAAM,OAAA,GAAU,OAAO,KAAK,CAAA;AACrE,EAAA,MAAM,KAAA,GAAQ,QAAQ,WAAA,EAAY;AAUlC,EAAA,MAAM,eAAe,KAAA,CAAM,QAAA,CAAS,WAAW,CAAA,IAAK,KAAA,CAAM,SAAS,YAAY,CAAA;AAC/E,EAAA,MAAM,MAAA,GAAS,YAAA,GACX,CAAA,6DAAA,EAAgE,OAAO,CAAA,CAAA,GACvE,OAAA;AACJ,EAAA,OAAO,IAAI,mBAAmB,gBAAA,EAAkB,CAAA,oBAAA,EAAuB,MAAM,CAAA,CAAA,EAAI,EAAE,OAAO,CAAA;AAC5F;AC1DO,SAAS,WAAW,KAAA,EAA4B;AACrD,EAAA,OAAOD,aAAO,KAAK,CAAA;AACrB","file":"cbor.cjs","sourcesContent":["// Every canonical-CBOR decode violation collapses to the single public CIP-309\n// taxonomy code MALFORMED_CBOR: indefinite-length (streaming) items, duplicate\n// keys, unsorted keys, non-minimal integer encodings, and invalid UTF-8 in text\n// strings. The taxonomy intentionally has one code for all of these; the\n// specific cause survives in the human-readable error message, not as a\n// separate code.\nexport type CanonicalCborErrorCode = 'MALFORMED_CBOR';\n\nexport class CanonicalCborError extends Error {\n readonly code: CanonicalCborErrorCode;\n\n constructor(code: CanonicalCborErrorCode, message: string, options?: { cause?: unknown }) {\n super(message, options);\n this.name = 'CanonicalCborError';\n this.code = code;\n }\n}\n","import { cdeDecodeOptions, decode, encode } from 'cbor2';\nimport { sortCoreDeterministic } from 'cbor2/sorts';\n\nimport { CanonicalCborError } from './errors';\n\nexport type CanonicalCborValue =\n | null\n | boolean\n | number\n | bigint\n | string\n | Uint8Array\n | readonly CanonicalCborValue[]\n | { readonly [key: string]: CanonicalCborValue }\n | ReadonlyMap<string | number, CanonicalCborValue>;\n\nexport function encodeCanonicalCbor(value: CanonicalCborValue): Uint8Array {\n return encode(value, {\n cde: true,\n collapseBigInts: true,\n rejectDuplicateKeys: true,\n sortKeys: sortCoreDeterministic,\n });\n}\n\nexport function decodeCanonicalCbor(bytes: Uint8Array): unknown {\n try {\n return decode(bytes, {\n ...cdeDecodeOptions,\n rejectStreaming: true,\n rejectDuplicateKeys: true,\n // A CIP-309 record carries integers, byte/text strings, arrays, maps and\n // `null` — and nothing else. Without these rejections the major-type-7\n // surface leaks into the decoder: a float16/32/64 that happens to hold an\n // integral value (e.g. 1.0) silently decodes to the integer 1 and passes\n // a `z.literal(1)` / Number.isInteger schema check, so two byte strings\n // that are NOT byte-identical canonicalise to the same record. That\n // breaks the cross-implementation parity invariant (the Python twin\n // already rejects non-integer `v` / `enc.scheme` outright). Reject the\n // whole non-record surface — floats, negative zero, undefined, and\n // non-{true,false,null} simple values — so any such input surfaces as\n // MALFORMED_CBOR via mapDecodeError rather than decoding to a look-alike.\n rejectFloats: true,\n rejectNegativeZero: true,\n rejectUndefined: true,\n rejectSimple: true,\n });\n } catch (cause) {\n throw mapDecodeError(cause);\n }\n}\n\nfunction mapDecodeError(cause: unknown): CanonicalCborError {\n const message = cause instanceof Error ? cause.message : String(cause);\n const lower = message.toLowerCase();\n // Every canonical-decode violation collapses to the single public taxonomy\n // code MALFORMED_CBOR: indefinite-length (streaming) items, duplicate keys,\n // non-canonical (unsorted) key ordering, non-minimal integer encodings, and\n // invalid UTF-8 in text strings. cbor2 raises the SAME \"Duplicate or out of\n // order key\" message for both true duplicates AND distinct-but-unsorted keys,\n // so the two are indistinguishable by message — and per the CIP-309 taxonomy\n // both belong under MALFORMED_CBOR anyway. The specific cause survives in the\n // human-readable message below; for indefinite-length we state it explicitly\n // so the diagnostic is not lost when the code is collapsed.\n const isIndefinite = lower.includes('streaming') || lower.includes('indefinite');\n const detail = isIndefinite\n ? `indefinite-length items are not permitted in canonical CBOR: ${message}`\n : message;\n return new CanonicalCborError('MALFORMED_CBOR', `cbor decode failed: ${detail}`, { cause });\n}\n","// Permissive (non-canonical) CBOR decoder for outer wire decode (e.g. Cardano tx CBOR),\n// where the input is not constrained to be canonical RFC 8949 §4.2.1 form.\n//\n// CIP-309 records themselves MUST be canonical and MUST go through\n// `decodeCanonicalCbor`. This decoder\n// exists to peel the outer Cardano tx structure ([body, witness_set, is_valid,\n// auxiliary_data]) so the label-309 byte string can be re-encoded canonically\n// for validator + signature verification.\n\nimport { decode } from 'cbor2';\n\nexport function decodeCbor(bytes: Uint8Array): unknown {\n return decode(bytes);\n}\n"]}
|