@certcrypt/sdk 0.0.0-stage → 1.0.1
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.
Potentially problematic release.
This version of @certcrypt/sdk might be problematic. Click here for more details.
- package/CHANGELOG.md +74 -0
- package/LICENSE +178 -0
- package/README.md +557 -2
- package/SECURITY.md +55 -0
- package/dist/src/artifacts/base-certificate.d.ts +336 -0
- package/dist/src/artifacts/base-certificate.js +373 -0
- package/dist/src/artifacts/base-certificate.js.map +1 -0
- package/dist/src/artifacts/capacity-evidence.d.ts +195 -0
- package/dist/src/artifacts/capacity-evidence.js +202 -0
- package/dist/src/artifacts/capacity-evidence.js.map +1 -0
- package/dist/src/artifacts/commitment-inclusion.d.ts +23 -0
- package/dist/src/artifacts/commitment-inclusion.js +91 -0
- package/dist/src/artifacts/commitment-inclusion.js.map +1 -0
- package/dist/src/artifacts/holder-keyset.d.ts +67 -0
- package/dist/src/artifacts/holder-keyset.js +149 -0
- package/dist/src/artifacts/holder-keyset.js.map +1 -0
- package/dist/src/artifacts/rfc3339.d.ts +9 -0
- package/dist/src/artifacts/rfc3339.js +39 -0
- package/dist/src/artifacts/rfc3339.js.map +1 -0
- package/dist/src/client.d.ts +552 -0
- package/dist/src/client.js +1347 -0
- package/dist/src/client.js.map +1 -0
- package/dist/src/crypto/ai-decision.d.ts +73 -0
- package/dist/src/crypto/ai-decision.js +94 -0
- package/dist/src/crypto/ai-decision.js.map +1 -0
- package/dist/src/crypto/canonical-subject.d.ts +33 -0
- package/dist/src/crypto/canonical-subject.js +240 -0
- package/dist/src/crypto/canonical-subject.js.map +1 -0
- package/dist/src/crypto/collection-merkle.d.ts +58 -0
- package/dist/src/crypto/collection-merkle.js +164 -0
- package/dist/src/crypto/collection-merkle.js.map +1 -0
- package/dist/src/crypto/composite.d.ts +53 -0
- package/dist/src/crypto/composite.js +69 -0
- package/dist/src/crypto/composite.js.map +1 -0
- package/dist/src/crypto/digest.d.ts +3 -0
- package/dist/src/crypto/digest.js +31 -0
- package/dist/src/crypto/digest.js.map +1 -0
- package/dist/src/crypto/ecdsa-p256.d.ts +79 -0
- package/dist/src/crypto/ecdsa-p256.js +277 -0
- package/dist/src/crypto/ecdsa-p256.js.map +1 -0
- package/dist/src/crypto/jcs.d.ts +13 -0
- package/dist/src/crypto/jcs.js +65 -0
- package/dist/src/crypto/jcs.js.map +1 -0
- package/dist/src/crypto/merkle.d.ts +18 -0
- package/dist/src/crypto/merkle.js +150 -0
- package/dist/src/crypto/merkle.js.map +1 -0
- package/dist/src/crypto/ml-dsa.d.ts +28 -0
- package/dist/src/crypto/ml-dsa.js +60 -0
- package/dist/src/crypto/ml-dsa.js.map +1 -0
- package/dist/src/crypto/pseudonym.d.ts +8 -0
- package/dist/src/crypto/pseudonym.js +50 -0
- package/dist/src/crypto/pseudonym.js.map +1 -0
- package/dist/src/crypto/strict-json.d.ts +25 -0
- package/dist/src/crypto/strict-json.js +180 -0
- package/dist/src/crypto/strict-json.js.map +1 -0
- package/dist/src/crypto/subject-json-preconditions.d.ts +46 -0
- package/dist/src/crypto/subject-json-preconditions.js +381 -0
- package/dist/src/crypto/subject-json-preconditions.js.map +1 -0
- package/dist/src/index.d.ts +18 -0
- package/dist/src/index.js +80 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/issuer.d.ts +668 -0
- package/dist/src/issuer.js +1066 -0
- package/dist/src/issuer.js.map +1 -0
- package/dist/src/retry.d.ts +67 -0
- package/dist/src/retry.js +78 -0
- package/dist/src/retry.js.map +1 -0
- package/dist/src/types.d.ts +297 -0
- package/dist/src/types.js +15 -0
- package/dist/src/types.js.map +1 -0
- package/package.json +62 -4
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Copyright © 2025-2026 CERTCRYPT, Inc., its affiliates or licensors. All rights reserved.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
/**
|
|
15
|
+
* Issuer-side assembly of zero-data `capacity.evidence/1` — the projected,
|
|
16
|
+
* unlinkable view of a `capacity.commitment/1`: stable identifiers become
|
|
17
|
+
* operation-scoped pseudonyms, and the holder signs with a non-reused
|
|
18
|
+
* per-operation leaf key proven to belong to the authorized `holder_key_set_root`.
|
|
19
|
+
*
|
|
20
|
+
* DIVISION OF SIGNING LABOR: the Level-1 binding and its signature are NOT
|
|
21
|
+
* produced here. They are minted during activation and handed to the issuer, who
|
|
22
|
+
* passes them in verbatim — the key that signs them never exists in the SDK. The
|
|
23
|
+
* SDK signs ONLY the Level-3 projected commitment, with the leaf key it does hold.
|
|
24
|
+
*
|
|
25
|
+
* BYTE PARITY IS LOAD-BEARING: the projected commitment's signing bytes are its
|
|
26
|
+
* fields in DECLARATION order (insertion order, NOT JCS). Every implementation
|
|
27
|
+
* must agree exactly; it is pinned by the conformance vectors.
|
|
28
|
+
*/
|
|
29
|
+
import { Buffer } from "node:buffer";
|
|
30
|
+
import { jcsCanonicalizeString } from "../crypto/jcs.js";
|
|
31
|
+
import { digestHexText } from "../crypto/digest.js";
|
|
32
|
+
import { toRfc3339Seconds } from "./rfc3339.js";
|
|
33
|
+
import { derivePseudonym } from "../crypto/pseudonym.js";
|
|
34
|
+
export const ZERO_DATA_PROFILE = "zero-data.unlinkable";
|
|
35
|
+
/**
|
|
36
|
+
* The bytes the per-operation leaf key signs: the projected commitment in its
|
|
37
|
+
* exact declared field order (insertion order, NOT JCS).
|
|
38
|
+
*/
|
|
39
|
+
export function projectedCommitmentSigningBytes(pc) {
|
|
40
|
+
return new TextEncoder().encode(JSON.stringify(pc));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The canonical evidence digest bound into `independence.proof/1`: JCS (sorted
|
|
44
|
+
* keys) + SHA3-256, lowercase hex.
|
|
45
|
+
*/
|
|
46
|
+
export function evidenceDigestHex(evidence) {
|
|
47
|
+
return digestHexText(jcsCanonicalizeString(evidence));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Assemble zero-data capacity evidence for one commitment: project the
|
|
51
|
+
* identifiers to operation-scoped pseudonyms, sign the projected commitment with
|
|
52
|
+
* the disclosed leaf key, and carry the pre-signed Level-1 anchor verbatim.
|
|
53
|
+
*
|
|
54
|
+
* Throws if `keySet.root` does not equal the binding's `holder_key_set_root`
|
|
55
|
+
* (the leaf membership the verifier checks would fail otherwise — fail early),
|
|
56
|
+
* or if `leafIndex` is out of range.
|
|
57
|
+
*/
|
|
58
|
+
export function assembleEvidence(inputs) {
|
|
59
|
+
const { level1_binding, level1_signature } = inputs.signedLevel1;
|
|
60
|
+
if (inputs.keySet.root !== level1_binding.holder_key_set_root) {
|
|
61
|
+
throw new Error(`key set root (${inputs.keySet.root}) does not match the authorized ` +
|
|
62
|
+
`holder_key_set_root (${level1_binding.holder_key_set_root})`);
|
|
63
|
+
}
|
|
64
|
+
const salt = inputs.operationSalt;
|
|
65
|
+
const holderPseudonym = derivePseudonym(salt, "holder", inputs.stable.holder_id);
|
|
66
|
+
const poolPseudonym = derivePseudonym(salt, "pool", inputs.stable.pool_id);
|
|
67
|
+
const authorizationPseudonym = derivePseudonym(salt, "authorization", inputs.stable.authorization_id);
|
|
68
|
+
const projected_commitment = {
|
|
69
|
+
commitment_version: "capacity.commitment/1",
|
|
70
|
+
system_id: level1_binding.system_id,
|
|
71
|
+
system_root_fingerprint: level1_binding.system_root_fingerprint,
|
|
72
|
+
rules_version: level1_binding.rules_version,
|
|
73
|
+
authorization_pseudonym: authorizationPseudonym,
|
|
74
|
+
holder_pseudonym: holderPseudonym,
|
|
75
|
+
pool_pseudonym: poolPseudonym,
|
|
76
|
+
certificate_commitment_digest: inputs.certificate_commitment_digest,
|
|
77
|
+
consumed_quantity: inputs.consumed_quantity,
|
|
78
|
+
declared_effective_units: inputs.declared_effective_units,
|
|
79
|
+
issued_at: inputs.issued_at,
|
|
80
|
+
expires_at: inputs.expires_at,
|
|
81
|
+
};
|
|
82
|
+
const signature = inputs.keySet.sign(inputs.leafIndex, projectedCommitmentSigningBytes(projected_commitment));
|
|
83
|
+
const disclosure = inputs.keySet.disclosure(inputs.leafIndex);
|
|
84
|
+
const projected_authorization = {
|
|
85
|
+
system_id: level1_binding.system_id,
|
|
86
|
+
rules_version: level1_binding.rules_version,
|
|
87
|
+
system_root_fingerprint: level1_binding.system_root_fingerprint,
|
|
88
|
+
authorized_role: level1_binding.authorized_role,
|
|
89
|
+
authorization_issuer_kid: level1_binding.authorization_issuer_kid,
|
|
90
|
+
valid_from: level1_binding.valid_from,
|
|
91
|
+
valid_until: level1_binding.valid_until,
|
|
92
|
+
holder_key_set_root: level1_binding.holder_key_set_root,
|
|
93
|
+
level1_binding,
|
|
94
|
+
level1_signature,
|
|
95
|
+
authorization_pseudonym: authorizationPseudonym,
|
|
96
|
+
holder_pseudonym: holderPseudonym,
|
|
97
|
+
};
|
|
98
|
+
return {
|
|
99
|
+
profile: ZERO_DATA_PROFILE,
|
|
100
|
+
operation_salt_hex: Buffer.from(salt).toString("hex"),
|
|
101
|
+
projected_authorization,
|
|
102
|
+
projected_commitment,
|
|
103
|
+
operation_key: disclosure,
|
|
104
|
+
commitment_signature_b64: Buffer.from(signature).toString("base64"),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export const ZERO_DATA_SUBMISSION_SET_VERSION = "submission-set/1";
|
|
108
|
+
/**
|
|
109
|
+
* Maximum number of members a conforming `submission-set/1` may contain
|
|
110
|
+
* (submission-set/1). Normative, fail-closed protocol constant. The issuer
|
|
111
|
+
* SDK runs IN the client process, so this is the fail-fast, ergonomic guard: a
|
|
112
|
+
* caller who checks a count with `assertSubmissionSetWithinBound` (as the set
|
|
113
|
+
* assembler, `Certcrypt.submit()` and `CertcryptNodeClient.issue()` do) gets a
|
|
114
|
+
* typed error before any network call — it is not a security boundary (a client controls its own
|
|
115
|
+
* process). The authoritative enforcement is the node assembler and the CERTCRYPT
|
|
116
|
+
* submit endpoint, which reject an over-cardinality set fail-closed.
|
|
117
|
+
*
|
|
118
|
+
* The value is 64, pinned by the conformance contract
|
|
119
|
+
* (`fixtures/primitives/submission-set-limits`). It keeps a set of zero-data
|
|
120
|
+
* members within request sizes common transports accept (submission-set/1
|
|
121
|
+
* §8.1); per-member size is independent of subject count / collection
|
|
122
|
+
* cardinality.
|
|
123
|
+
*/
|
|
124
|
+
export const MAX_SUBMISSION_SET_MEMBERS = 64;
|
|
125
|
+
/** Thrown when a submission set would exceed `MAX_SUBMISSION_SET_MEMBERS`. */
|
|
126
|
+
export class SubmissionSetTooLargeError extends Error {
|
|
127
|
+
count;
|
|
128
|
+
max;
|
|
129
|
+
constructor(count) {
|
|
130
|
+
super(`submission set exceeds the maximum of ${MAX_SUBMISSION_SET_MEMBERS} members ` +
|
|
131
|
+
`(got ${count}); the set is rejected as a whole (submission-set/1)`);
|
|
132
|
+
this.name = "SubmissionSetTooLargeError";
|
|
133
|
+
this.count = count;
|
|
134
|
+
this.max = MAX_SUBMISSION_SET_MEMBERS;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Assert a member count is within the submission-set/1 cardinality bound.
|
|
139
|
+
* Fail-fast guard for any path that builds a multi-member set — a caller that
|
|
140
|
+
* groups prepared commitments, or hand-builds a `ZeroDataSubmissionSetV1`.
|
|
141
|
+
* `Certcrypt.submit()` and `CertcryptNodeClient.issue()` call it before
|
|
142
|
+
* sending, so an over-bound set is refused locally; the node and the submit
|
|
143
|
+
* endpoint refuse one independently.
|
|
144
|
+
*/
|
|
145
|
+
export function assertSubmissionSetWithinBound(memberCount) {
|
|
146
|
+
if (memberCount > MAX_SUBMISSION_SET_MEMBERS) {
|
|
147
|
+
throw new SubmissionSetTooLargeError(memberCount);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Assemble a single-certificate zero-data submission set from one assembled
|
|
152
|
+
* `CapacityEvidence` and its operative context. The `submission_set_id` is
|
|
153
|
+
* computed deterministically over the set without the id (JCS + SHA3).
|
|
154
|
+
*/
|
|
155
|
+
export function assembleZeroDataSubmissionSet(evidence, operative, opts) {
|
|
156
|
+
const closure_time = toRfc3339Seconds(opts.closure_time);
|
|
157
|
+
// The canonical member: exactly `capacity_evidence` + `operative`
|
|
158
|
+
// (submission-set/1 §7.1). Both are covered by the `submission_set_id` digest,
|
|
159
|
+
// so the member shape and the identifier are fixed together.
|
|
160
|
+
const certificates = [
|
|
161
|
+
{
|
|
162
|
+
capacity_evidence: evidence,
|
|
163
|
+
operative,
|
|
164
|
+
},
|
|
165
|
+
];
|
|
166
|
+
// cardinality bound — fail-fast in-process before the id is computed.
|
|
167
|
+
// (This single-certificate assembler is always within bounds; the assertion
|
|
168
|
+
// makes the contract explicit at the construction point.)
|
|
169
|
+
assertSubmissionSetWithinBound(certificates.length);
|
|
170
|
+
// The system domain comes from the SIGNED evidence, never from a constant in this
|
|
171
|
+
// SDK. The two projections carry it because the domain signed them; anything else
|
|
172
|
+
// would make a conforming operator on another domain emit a set that misreports
|
|
173
|
+
// its own domain — and `system_id` is covered by the `submission_set_id` digest,
|
|
174
|
+
// so the id would be wrong too.
|
|
175
|
+
//
|
|
176
|
+
// The spec already requires `(system_id, rules_version, system_root_fingerprint)`
|
|
177
|
+
// to AGREE between the two projections (anti-splicing). Enforce it here, at the
|
|
178
|
+
// one point that reads it: a mismatch means the two halves came from different
|
|
179
|
+
// authorizations, which must fail before anything is signed or sent.
|
|
180
|
+
const commitmentSystemId = evidence.projected_commitment?.system_id;
|
|
181
|
+
const authorizationSystemId = evidence.projected_authorization?.system_id;
|
|
182
|
+
if (!commitmentSystemId) {
|
|
183
|
+
throw new Error("capacity.evidence/1 carries no projected_commitment.system_id; the submission " +
|
|
184
|
+
"set's system domain is derived from the signed evidence and cannot be assumed");
|
|
185
|
+
}
|
|
186
|
+
if (authorizationSystemId && authorizationSystemId !== commitmentSystemId) {
|
|
187
|
+
throw new Error(`capacity.evidence/1 system_id mismatch: projected_commitment says ` +
|
|
188
|
+
`"${commitmentSystemId}" but projected_authorization says ` +
|
|
189
|
+
`"${authorizationSystemId}" — the two projections must agree (anti-splicing)`);
|
|
190
|
+
}
|
|
191
|
+
const withoutId = {
|
|
192
|
+
schema: ZERO_DATA_SUBMISSION_SET_VERSION,
|
|
193
|
+
system_id: commitmentSystemId,
|
|
194
|
+
rules_version: opts.rules_version,
|
|
195
|
+
system_root_fingerprint: opts.system_root_fingerprint,
|
|
196
|
+
closure_time,
|
|
197
|
+
certificates,
|
|
198
|
+
};
|
|
199
|
+
const submission_set_id = digestHexText(jcsCanonicalizeString(withoutId));
|
|
200
|
+
return { ...withoutId, submission_set_id };
|
|
201
|
+
}
|
|
202
|
+
//# sourceMappingURL=capacity-evidence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"capacity-evidence.js","sourceRoot":"","sources":["../../../src/artifacts/capacity-evidence.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD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AAgF;IAChF,+EAA+E;IAC/E,qEAAqE;IACrE,MAAM,kBAAkB,GAAG,QAAQ,CAAC,oBAAoB,EAAE,SAAS,CAAC;IACpE,MAAM,qBAAqB,GAAG,QAAQ,CAAC,uBAAuB,EAAE,SAAS,CAAC;IAC1E,IAAI,CAAC,kBAAkB,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CACb,gFAAgF;YAC9E,+EAA+E,CAClF,CAAC;IACJ,CAAC;IACD,IAAI,qBAAqB,IAAI,qBAAqB,KAAK,kBAAkB,EAAE,CAAC;QAC1E,MAAM,IAAI,KAAK,CACb,oEAAoE;YAClE,IAAI,kBAAkB,qCAAqC;YAC3D,IAAI,qBAAqB,oDAAoD,CAChF,CAAC;IACJ,CAAC;IAED,MAAM,SAAS,GAAG;QAChB,MAAM,EAAE,gCAAgC;QACxC,SAAS,EAAE,kBAAkB;QAC7B,aAAa,EAAE,IAAI,CAAC,aAAa;QACjC,uBAAuB,EAAE,IAAI,CAAC,uBAAuB;QACrD,YAAY;QACZ,YAAY;KACb,CAAC;IACF,MAAM,iBAAiB,GAAG,aAAa,CAAC,qBAAqB,CAAC,SAAS,CAAC,CAAC,CAAC;IAC1E,OAAO,EAAE,GAAG,SAAS,EAAE,iBAAiB,EAAE,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Version tag of the commitment sub-tree leaf preimage. */
|
|
2
|
+
export declare const COMMITMENT_LEAF_VERSION = "ccab1-commit";
|
|
3
|
+
/** The `CommitmentInclusionProof` shape a verifier consumes. */
|
|
4
|
+
export interface CommitmentInclusionProof {
|
|
5
|
+
commitment_id: string;
|
|
6
|
+
commitment_leaf_hash: string;
|
|
7
|
+
commitment_index: string;
|
|
8
|
+
commitment_siblings: string[];
|
|
9
|
+
subtree_size: string;
|
|
10
|
+
}
|
|
11
|
+
/** Commitment leaf hash: SHA3-256(0x00 || orderedJson({v, commitment_id})). */
|
|
12
|
+
export declare function commitmentLeafHash(commitmentId: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Build the `commitment → subtree_root` inclusion proof for the commitment at
|
|
15
|
+
* `commitmentIndex` among the leaf's accepted `commitmentIds` (their canonical
|
|
16
|
+
* leaf order = `independence.proof/1.consumed_commitment_ids`). The result
|
|
17
|
+
* self-verifies against `subtree_root` from the same inputs.
|
|
18
|
+
*
|
|
19
|
+
* Throws on an empty set (a settlement leaf must carry ≥1 accepted commitment;) or an out-of-range index.
|
|
20
|
+
*/
|
|
21
|
+
export declare function buildCommitmentInclusionProof(commitmentIds: string[], commitmentIndex: number): CommitmentInclusionProof;
|
|
22
|
+
/** Subtree root over the accepted commitments (for an optional self-check). */
|
|
23
|
+
export declare function commitmentSubtreeRoot(commitmentIds: string[]): string;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// Copyright © 2025-2026 CERTCRYPT, Inc., its affiliates or licensors. All rights reserved.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
/**
|
|
15
|
+
* Commitment inclusion proof — the first level of the chained inclusion path
|
|
16
|
+
* `commitment_id → subtree_root`.
|
|
17
|
+
*
|
|
18
|
+
* The verifier does NOT receive this artifact from the server/node — it is derived
|
|
19
|
+
* client-side (issuer-side) from data already in `independence.proof/1`
|
|
20
|
+
* (`consumed_commitment_ids` + `subtree_root`). This module lets the SDK assemble
|
|
21
|
+
* it so the issuer can build a complete `verification-package/1`.
|
|
22
|
+
*
|
|
23
|
+
* BYTE-EXACTNESS MATTERS, and this is the one place it is easy to get wrong: the
|
|
24
|
+
* leaf preimage is ordered-JSON `{v, commitment_id}` in INSERTION order, NOT
|
|
25
|
+
* JCS/alphabetical. Every implementation emits `{"v":"…","commitment_id":"…"}`
|
|
26
|
+
* with no spaces; canonicalizing alphabetically here would swap the two members
|
|
27
|
+
* and every inclusion proof would fail to verify.
|
|
28
|
+
*/
|
|
29
|
+
import { computeLeafHash, computeMerkleRoot, generateInclusionProof } from "../crypto/merkle.js";
|
|
30
|
+
/** Version tag of the commitment sub-tree leaf preimage. */
|
|
31
|
+
export const COMMITMENT_LEAF_VERSION = "ccab1-commit";
|
|
32
|
+
/**
|
|
33
|
+
* Ordered-JSON bytes preserving field insertion order (NOT alphabetical): the
|
|
34
|
+
* verifier's `canonicalJsonBytes` and struct-ordered JSON
|
|
35
|
+
* both produce `{"k":v,...}` with no whitespace. Kept private + minimal — only
|
|
36
|
+
* strings are needed for the commitment leaf preimage.
|
|
37
|
+
*/
|
|
38
|
+
function orderedJsonBytes(orderedFields) {
|
|
39
|
+
const parts = ["{"];
|
|
40
|
+
for (let i = 0; i < orderedFields.length; i++) {
|
|
41
|
+
if (i > 0)
|
|
42
|
+
parts.push(",");
|
|
43
|
+
const [key, value] = orderedFields[i];
|
|
44
|
+
parts.push(JSON.stringify(key) + ":" + JSON.stringify(value));
|
|
45
|
+
}
|
|
46
|
+
parts.push("}");
|
|
47
|
+
return new TextEncoder().encode(parts.join(""));
|
|
48
|
+
}
|
|
49
|
+
/** Commitment leaf hash: SHA3-256(0x00 || orderedJson({v, commitment_id})). */
|
|
50
|
+
export function commitmentLeafHash(commitmentId) {
|
|
51
|
+
return computeLeafHash(orderedJsonBytes([
|
|
52
|
+
["v", COMMITMENT_LEAF_VERSION],
|
|
53
|
+
["commitment_id", commitmentId],
|
|
54
|
+
]));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Build the `commitment → subtree_root` inclusion proof for the commitment at
|
|
58
|
+
* `commitmentIndex` among the leaf's accepted `commitmentIds` (their canonical
|
|
59
|
+
* leaf order = `independence.proof/1.consumed_commitment_ids`). The result
|
|
60
|
+
* self-verifies against `subtree_root` from the same inputs.
|
|
61
|
+
*
|
|
62
|
+
* Throws on an empty set (a settlement leaf must carry ≥1 accepted commitment;) or an out-of-range index.
|
|
63
|
+
*/
|
|
64
|
+
export function buildCommitmentInclusionProof(commitmentIds, commitmentIndex) {
|
|
65
|
+
if (commitmentIds.length === 0) {
|
|
66
|
+
throw new Error("commitment inclusion proof requires at least one commitment id");
|
|
67
|
+
}
|
|
68
|
+
if (commitmentIndex < 0 || commitmentIndex >= commitmentIds.length) {
|
|
69
|
+
throw new Error(`commitmentIndex ${commitmentIndex} out of range (${commitmentIds.length} commitments)`);
|
|
70
|
+
}
|
|
71
|
+
const leafHashes = commitmentIds.map(commitmentLeafHash);
|
|
72
|
+
// computeMerkleRoot is not needed for the proof itself (the verifier derives
|
|
73
|
+
// the root from the siblings), but computing it here would let a caller assert
|
|
74
|
+
// it equals the proof's subtree_root; we keep the builder minimal.
|
|
75
|
+
const siblings = generateInclusionProof(leafHashes, commitmentIndex);
|
|
76
|
+
return {
|
|
77
|
+
commitment_id: commitmentIds[commitmentIndex],
|
|
78
|
+
commitment_leaf_hash: leafHashes[commitmentIndex],
|
|
79
|
+
commitment_index: String(commitmentIndex),
|
|
80
|
+
commitment_siblings: siblings,
|
|
81
|
+
subtree_size: String(leafHashes.length),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** Subtree root over the accepted commitments (for an optional self-check). */
|
|
85
|
+
export function commitmentSubtreeRoot(commitmentIds) {
|
|
86
|
+
if (commitmentIds.length === 0) {
|
|
87
|
+
throw new Error("subtree root requires at least one commitment id");
|
|
88
|
+
}
|
|
89
|
+
return computeMerkleRoot(commitmentIds.map(commitmentLeafHash));
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=commitment-inclusion.js.map
|
|
@@ -0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
|
+
export interface OperationKey {
|
|
3
|
+
publicKey: Uint8Array;
|
|
4
|
+
secretKey: Uint8Array;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The per-commitment object revealing one operation public key, its index, the
|
|
8
|
+
* tree size and its inclusion proof. Field names match the wire shape the
|
|
9
|
+
* verifier consumes (`OperationKeyDisclosure` in capacity-evidence).
|
|
10
|
+
*/
|
|
11
|
+
export interface OperationKeyDisclosure {
|
|
12
|
+
leaf_index: number;
|
|
13
|
+
public_key_b64: string;
|
|
14
|
+
inclusion_proof: string[];
|
|
15
|
+
tree_size: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A holder key set: N operation keys, their leaf hashes, and the committed
|
|
19
|
+
* Merkle root. Struct fields; `secretKey`s are sensitive and never
|
|
20
|
+
* serialized.
|
|
21
|
+
*/
|
|
22
|
+
export declare class HolderKeySet {
|
|
23
|
+
private readonly keys;
|
|
24
|
+
private readonly leafHashes;
|
|
25
|
+
/** `holder_key_set_root`: the value the Level-2 authorization commits to. */
|
|
26
|
+
readonly root: string;
|
|
27
|
+
private constructor();
|
|
28
|
+
/** Generate a fresh key set of `size` per-operation keys. */
|
|
29
|
+
static generate(size: number): HolderKeySet;
|
|
30
|
+
/**
|
|
31
|
+
* Rebuild a key set from already-generated keys (e.g. restored from holder
|
|
32
|
+
* custody). Recomputes leaves and root deterministically.
|
|
33
|
+
*/
|
|
34
|
+
static fromKeys(keys: OperationKey[]): HolderKeySet;
|
|
35
|
+
/**
|
|
36
|
+
* Derive the full operation-key tree DETERMINISTICALLY from one 32-byte master
|
|
37
|
+
* seed — the canonical "seed + large tree" issuer custody model. The issuer
|
|
38
|
+
* keeps only the 32-byte seed secret; the whole tree (and its `root`, the
|
|
39
|
+
* `holder_key_set_root` the Level-1 authorization commits to) is reproducible
|
|
40
|
+
* from it, so the same seed always yields the same root on any machine.
|
|
41
|
+
*
|
|
42
|
+
* Per-leaf seed = SHA3-256(masterSeed || utf8("op-key/<index>")). This is the
|
|
43
|
+
* single canonical derivation: an issuer, the tooling,
|
|
44
|
+
* and onboarding must all agree on the root, so it lives here in
|
|
45
|
+
* the SDK rather than being re-implemented per integration.
|
|
46
|
+
*
|
|
47
|
+
* @param masterSeed 32-byte master seed (Uint8Array or 64-char hex string).
|
|
48
|
+
* @param size number of operation keys / leaves (e.g. 4096).
|
|
49
|
+
*/
|
|
50
|
+
static fromMasterSeed(masterSeed: Uint8Array | string, size: number): HolderKeySet;
|
|
51
|
+
/**
|
|
52
|
+
* Compute ONLY the `holder_key_set_root` for a master seed, without retaining
|
|
53
|
+
* the derived keys — the value the issuer supplies at onboarding to
|
|
54
|
+
* mint its Level-1 authorization. Convenience over `fromMasterSeed(...).root`.
|
|
55
|
+
*/
|
|
56
|
+
static rootFromMasterSeed(masterSeed: Uint8Array | string, size: number): string;
|
|
57
|
+
get treeSize(): number;
|
|
58
|
+
/** The operation key at `leafIndex` (for signing exactly one commitment). */
|
|
59
|
+
key(leafIndex: number): OperationKey;
|
|
60
|
+
/** The inclusion-proof disclosure binding `leafIndex` to the root. */
|
|
61
|
+
disclosure(leafIndex: number): OperationKeyDisclosure;
|
|
62
|
+
/**
|
|
63
|
+
* Sign `msg` with the leaf at `leafIndex`. The caller (wallet) is responsible
|
|
64
|
+
* for never reusing a leaf — see the non-reuse invariant above.
|
|
65
|
+
*/
|
|
66
|
+
sign(leafIndex: number, msg: Uint8Array): Uint8Array;
|
|
67
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// Copyright © 2025-2026 CERTCRYPT, Inc., its affiliates or licensors. All rights reserved.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
/**
|
|
15
|
+
* Holder operation-key tree (`holder-keyset.merkle-sha3-256/1`), issuer side.
|
|
16
|
+
*
|
|
17
|
+
* The holder commits to a Merkle root over a set of per-operation keys
|
|
18
|
+
* (`holder_key_set_root`). The Level-2 authorization is signed (Level-1, by
|
|
19
|
+
* CERTCRYPT) over that root **once**; thereafter the holder signs each projected
|
|
20
|
+
* commitment with a DISTINCT, non-reused leaf key and discloses that leaf + its
|
|
21
|
+
* inclusion proof. Two commitments from the same holder are therefore not
|
|
22
|
+
* linkable BY KEY: no leaf repeats.
|
|
23
|
+
*
|
|
24
|
+
* They ARE linkable by root. `holder_key_set_root` is copied unprojected into
|
|
25
|
+
* every `ProjectedAuthorization` (see `assembleEvidence` in capacity-evidence.ts,
|
|
26
|
+
* which salts `holder_id`, `pool_id` and `authorization_id` through
|
|
27
|
+
* `derivePseudonym` but passes the root through as-is), so any party holding two
|
|
28
|
+
* artifacts can tell they share an issuer by comparing that one field. Unlinkability
|
|
29
|
+
* is a key-mode property, not a property of the whole surface.
|
|
30
|
+
*
|
|
31
|
+
* Non-reuse is an emission invariant the verifier cannot enforce (it has no
|
|
32
|
+
* global state); the holder (wallet) is responsible for never handing the same
|
|
33
|
+
* `leaf_index` to two commitments. This module provides the keys and the
|
|
34
|
+
* disclosure; the no-reuse bookkeeping lives in the wallet.
|
|
35
|
+
*
|
|
36
|
+
* Leaf hash = SHA3-256(0x00 || public_key_bytes). Root/proofs via the shared
|
|
37
|
+
* SHA3-256 Merkle (`../crypto/merkle.js`), byte-identical to the
|
|
38
|
+
* verifier.
|
|
39
|
+
*/
|
|
40
|
+
import { Buffer } from "node:buffer";
|
|
41
|
+
import { createHash } from "node:crypto";
|
|
42
|
+
import { keygenMlDsa65, signMlDsa65 } from "../crypto/ml-dsa.js";
|
|
43
|
+
import { computeLeafHash, computeMerkleRoot, generateInclusionProof, } from "../crypto/merkle.js";
|
|
44
|
+
/**
|
|
45
|
+
* A holder key set: N operation keys, their leaf hashes, and the committed
|
|
46
|
+
* Merkle root. Struct fields; `secretKey`s are sensitive and never
|
|
47
|
+
* serialized.
|
|
48
|
+
*/
|
|
49
|
+
export class HolderKeySet {
|
|
50
|
+
keys;
|
|
51
|
+
leafHashes;
|
|
52
|
+
/** `holder_key_set_root`: the value the Level-2 authorization commits to. */
|
|
53
|
+
root;
|
|
54
|
+
constructor(keys) {
|
|
55
|
+
if (keys.length === 0) {
|
|
56
|
+
throw new Error("holder key set must have at least one operation key");
|
|
57
|
+
}
|
|
58
|
+
this.keys = keys;
|
|
59
|
+
this.leafHashes = keys.map((k) => computeLeafHash(k.publicKey));
|
|
60
|
+
this.root = computeMerkleRoot(this.leafHashes);
|
|
61
|
+
}
|
|
62
|
+
/** Generate a fresh key set of `size` per-operation keys. */
|
|
63
|
+
static generate(size) {
|
|
64
|
+
const keys = [];
|
|
65
|
+
for (let i = 0; i < size; i++) {
|
|
66
|
+
const { publicKey, secretKey } = keygenMlDsa65();
|
|
67
|
+
keys.push({ publicKey, secretKey });
|
|
68
|
+
}
|
|
69
|
+
return new HolderKeySet(keys);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Rebuild a key set from already-generated keys (e.g. restored from holder
|
|
73
|
+
* custody). Recomputes leaves and root deterministically.
|
|
74
|
+
*/
|
|
75
|
+
static fromKeys(keys) {
|
|
76
|
+
return new HolderKeySet(keys.map((k) => ({ ...k })));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Derive the full operation-key tree DETERMINISTICALLY from one 32-byte master
|
|
80
|
+
* seed — the canonical "seed + large tree" issuer custody model. The issuer
|
|
81
|
+
* keeps only the 32-byte seed secret; the whole tree (and its `root`, the
|
|
82
|
+
* `holder_key_set_root` the Level-1 authorization commits to) is reproducible
|
|
83
|
+
* from it, so the same seed always yields the same root on any machine.
|
|
84
|
+
*
|
|
85
|
+
* Per-leaf seed = SHA3-256(masterSeed || utf8("op-key/<index>")). This is the
|
|
86
|
+
* single canonical derivation: an issuer, the tooling,
|
|
87
|
+
* and onboarding must all agree on the root, so it lives here in
|
|
88
|
+
* the SDK rather than being re-implemented per integration.
|
|
89
|
+
*
|
|
90
|
+
* @param masterSeed 32-byte master seed (Uint8Array or 64-char hex string).
|
|
91
|
+
* @param size number of operation keys / leaves (e.g. 4096).
|
|
92
|
+
*/
|
|
93
|
+
static fromMasterSeed(masterSeed, size) {
|
|
94
|
+
const seed = typeof masterSeed === "string" ? Buffer.from(masterSeed, "hex") : Buffer.from(masterSeed);
|
|
95
|
+
if (seed.length !== 32) {
|
|
96
|
+
throw new Error("holder key set master seed must be 32 bytes (64 hex chars)");
|
|
97
|
+
}
|
|
98
|
+
if (!Number.isInteger(size) || size < 1) {
|
|
99
|
+
throw new Error("holder key set size must be a positive integer");
|
|
100
|
+
}
|
|
101
|
+
const keys = [];
|
|
102
|
+
for (let i = 0; i < size; i++) {
|
|
103
|
+
const leafSeed = createHash("sha3-256")
|
|
104
|
+
.update(seed)
|
|
105
|
+
.update(Buffer.from(`op-key/${i}`, "utf8"))
|
|
106
|
+
.digest();
|
|
107
|
+
const { publicKey, secretKey } = keygenMlDsa65(leafSeed);
|
|
108
|
+
keys.push({ publicKey, secretKey });
|
|
109
|
+
}
|
|
110
|
+
return new HolderKeySet(keys);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Compute ONLY the `holder_key_set_root` for a master seed, without retaining
|
|
114
|
+
* the derived keys — the value the issuer supplies at onboarding to
|
|
115
|
+
* mint its Level-1 authorization. Convenience over `fromMasterSeed(...).root`.
|
|
116
|
+
*/
|
|
117
|
+
static rootFromMasterSeed(masterSeed, size) {
|
|
118
|
+
return HolderKeySet.fromMasterSeed(masterSeed, size).root;
|
|
119
|
+
}
|
|
120
|
+
get treeSize() {
|
|
121
|
+
return this.keys.length;
|
|
122
|
+
}
|
|
123
|
+
/** The operation key at `leafIndex` (for signing exactly one commitment). */
|
|
124
|
+
key(leafIndex) {
|
|
125
|
+
const k = this.keys[leafIndex];
|
|
126
|
+
if (!k) {
|
|
127
|
+
throw new Error(`leaf_index ${leafIndex} out of range (tree_size ${this.keys.length})`);
|
|
128
|
+
}
|
|
129
|
+
return k;
|
|
130
|
+
}
|
|
131
|
+
/** The inclusion-proof disclosure binding `leafIndex` to the root. */
|
|
132
|
+
disclosure(leafIndex) {
|
|
133
|
+
const k = this.key(leafIndex);
|
|
134
|
+
return {
|
|
135
|
+
leaf_index: leafIndex,
|
|
136
|
+
public_key_b64: Buffer.from(k.publicKey).toString("base64"),
|
|
137
|
+
inclusion_proof: generateInclusionProof(this.leafHashes, leafIndex),
|
|
138
|
+
tree_size: this.keys.length,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Sign `msg` with the leaf at `leafIndex`. The caller (wallet) is responsible
|
|
143
|
+
* for never reusing a leaf — see the non-reuse invariant above.
|
|
144
|
+
*/
|
|
145
|
+
sign(leafIndex, msg) {
|
|
146
|
+
return signMlDsa65(this.key(leafIndex).secretKey, msg);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=holder-keyset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"holder-keyset.js","sourceRoot":"","sources":["../../../src/artifacts/holder-keyset.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO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AAC,IAAI,CAAC;IAC5D,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;IAC1B,CAAC;IAED,6EAA6E;IAC7E,GAAG,CAAC,SAAiB;QACnB,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC/B,IAAI,CAAC,CAAC,EAAE,CAAC;YACP,MAAM,IAAI,KAAK,CACb,cAAc,SAAS,4BAA4B,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CACvE,CAAC;QACJ,CAAC;QACD,OAAO,CAAC,CAAC;IACX,CAAC;IAED,sEAAsE;IACtE,UAAU,CAAC,SAAiB;QAC1B,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC9B,OAAO;YACL,UAAU,EAAE,SAAS;YACrB,cAAc,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC;YAC3D,eAAe,EAAE,sBAAsB,CAAC,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC;YACnE,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,MAAM;SAC5B,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,IAAI,CAAC,SAAiB,EAAE,GAAe;QACrC,OAAO,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IACzD,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC 3339 second-precision UTC timestamp normalization.
|
|
3
|
+
*
|
|
4
|
+
* `Date.prototype.toISOString()` always emits ".000Z" for whole seconds;
|
|
5
|
+
* the canonical form strips zero subseconds. To produce byte-identical
|
|
6
|
+
* canonical bytes we must drop the fractional part. Inputs are required to fall on a whole second (millis === 0);
|
|
7
|
+
* a non-zero millisecond component is rejected rather than silently truncated.
|
|
8
|
+
*/
|
|
9
|
+
export declare function toRfc3339Seconds(value: Date | string): string;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Copyright © 2025-2026 CERTCRYPT, Inc., its affiliates or licensors. All rights reserved.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
/**
|
|
15
|
+
* RFC 3339 second-precision UTC timestamp normalization.
|
|
16
|
+
*
|
|
17
|
+
* `Date.prototype.toISOString()` always emits ".000Z" for whole seconds;
|
|
18
|
+
* the canonical form strips zero subseconds. To produce byte-identical
|
|
19
|
+
* canonical bytes we must drop the fractional part. Inputs are required to fall on a whole second (millis === 0);
|
|
20
|
+
* a non-zero millisecond component is rejected rather than silently truncated.
|
|
21
|
+
*/
|
|
22
|
+
export function toRfc3339Seconds(value) {
|
|
23
|
+
if (typeof value === "string") {
|
|
24
|
+
// Trust a caller-supplied string only if it is already second-precision Z.
|
|
25
|
+
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/.test(value)) {
|
|
26
|
+
throw new Error(`timestamp string must be RFC 3339 UTC second precision (…Z, no fractional seconds), got ${JSON.stringify(value)}`);
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
if (Number.isNaN(value.getTime())) {
|
|
31
|
+
throw new Error("timestamp is an invalid Date");
|
|
32
|
+
}
|
|
33
|
+
if (value.getUTCMilliseconds() !== 0) {
|
|
34
|
+
throw new Error("timestamp must fall on a whole second (no sub-second precision is allowed in the canonical bytes)");
|
|
35
|
+
}
|
|
36
|
+
// toISOString() => "2026-05-09T12:00:00.000Z"; strip ".000".
|
|
37
|
+
return value.toISOString().replace(/\.\d{3}Z$/, "Z");
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=rfc3339.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rfc3339.js","sourceRoot":"","sources":["../../../src/artifacts/rfc3339.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAoB;IACnD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,2EAA2E;QAC3E,IAAI,CAAC,wCAAwC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAC1D,MAAM,IAAI,KAAK,CACb,2FAA2F,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CACnH,CAAC;QACJ,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;IAClD,CAAC;IACD,IAAI,KAAK,CAAC,kBAAkB,EAAE,KAAK,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CACb,mGAAmG,CACpG,CAAC;IACJ,CAAC;IACD,6DAA6D;IAC7D,OAAO,KAAK,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;AACvD,CAAC"}
|