@nakafa/aksara-contracts 2.0.0 → 3.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.
Files changed (71) hide show
  1. package/README.md +2 -2
  2. package/dist/artifact/integrity.d.ts +3 -3
  3. package/dist/artifact/integrity.js +2 -2
  4. package/dist/artifact/source.d.ts +2 -2
  5. package/dist/artifact/source.js +2 -8
  6. package/dist/artifact/spec.d.ts +0 -16
  7. package/dist/artifact/spec.js +0 -6
  8. package/dist/artifact/verify.d.ts +1 -1
  9. package/dist/assessment/hash.d.ts +6 -6
  10. package/dist/assessment/snapshot.d.ts +3 -3
  11. package/dist/date.js +1 -1
  12. package/dist/hash/digest.d.ts +7 -7
  13. package/dist/hash/digest.js +9 -30
  14. package/dist/hash/stream.d.ts +1 -1
  15. package/dist/hash/text.d.ts +3 -12
  16. package/dist/hash/text.js +3 -6
  17. package/dist/ids.js +8 -8
  18. package/dist/preview/auth.d.ts +1 -1
  19. package/dist/preview/auth.js +29 -25
  20. package/dist/program/snapshot/digest.d.ts +1 -2
  21. package/dist/program/snapshot/digest.js +2 -4
  22. package/dist/program/snapshot/hash.d.ts +6 -21
  23. package/dist/program/snapshot/hash.js +3 -9
  24. package/dist/projection/digest.d.ts +7 -16
  25. package/dist/projection/digest.js +7 -13
  26. package/dist/projection/hash.d.ts +2 -11
  27. package/dist/projection/hash.js +2 -5
  28. package/dist/projection/material.js +1 -1
  29. package/dist/projection/verify.d.ts +1 -1
  30. package/dist/projection/verify.js +1 -1
  31. package/dist/quran/notes.js +1 -1
  32. package/dist/quran/provenance.d.ts +2 -8
  33. package/dist/quran/provenance.js +1 -4
  34. package/dist/quran/snapshot/digest.d.ts +1 -2
  35. package/dist/quran/snapshot/digest.js +2 -2
  36. package/dist/quran/snapshot/hash.d.ts +3 -10
  37. package/dist/quran/snapshot/hash.js +2 -5
  38. package/dist/quran/snapshot/row/hash.d.ts +3 -12
  39. package/dist/quran/snapshot/row/hash.js +2 -5
  40. package/dist/release/digest.d.ts +7 -16
  41. package/dist/release/digest.js +7 -13
  42. package/dist/release/hash.d.ts +2 -11
  43. package/dist/release/hash.js +2 -6
  44. package/dist/release/items.d.ts +1 -1
  45. package/dist/release/items.js +1 -1
  46. package/dist/release/result/digest.d.ts +5 -13
  47. package/dist/release/result/digest.js +4 -9
  48. package/dist/release/rollback/digest.d.ts +7 -16
  49. package/dist/release/rollback/digest.js +7 -13
  50. package/dist/release/route/digest.d.ts +7 -16
  51. package/dist/release/route/digest.js +7 -13
  52. package/dist/release/route/verify.d.ts +1 -1
  53. package/dist/release/route/verify.js +1 -1
  54. package/dist/release/snapshot/evidence/assessment.d.ts +1 -1
  55. package/dist/release/snapshot/evidence/program.d.ts +1 -1
  56. package/dist/release/snapshot/evidence/quran.d.ts +1 -1
  57. package/dist/release/snapshot/evidence.d.ts +1 -1
  58. package/dist/release/snapshot/verify.d.ts +1 -1
  59. package/dist/release/verify.d.ts +2 -2
  60. package/dist/renderer/compatibility.d.ts +1 -1
  61. package/dist/renderer/component.js +1 -1
  62. package/dist/renderer/contract.d.ts +0 -8
  63. package/dist/renderer/contract.js +0 -3
  64. package/dist/renderer/manifest.d.ts +3 -3
  65. package/dist/renderer/manifest.js +2 -2
  66. package/dist/runtime/verify.d.ts +2 -2
  67. package/dist/signature/key.d.ts +0 -2
  68. package/dist/signature/key.js +37 -88
  69. package/dist/signature/verify.js +31 -46
  70. package/dist/tree.js +1 -1
  71. package/package.json +2 -2
@@ -1,22 +1,18 @@
1
- import { Effect } from "effect";
1
+ import { Effect, Layer } from "effect";
2
+ import { KeyFormat, SecureRandom, Signature } from "effect/crypto";
3
+ import { Base64, Base64Url, Hex } from "effect/encoding";
2
4
  import { ContentVerificationKeyResolver, PublicKeyParseError, PublicKeyTypeError, SignatureCheckError, SignatureInvalidError, } from "#contracts/signature/spec";
3
5
  const PUBLIC_KEY_HEADER = "-----BEGIN PUBLIC KEY-----\n";
4
6
  const PUBLIC_KEY_FOOTER = "-----END PUBLIC KEY-----";
7
+ /** The DER header of one Ed25519 SubjectPublicKeyInfo, before its 32-byte key. */
5
8
  const ED25519_SPKI_PREFIX = "302a300506032b6570032100";
6
- const PUBLIC_KEY_BASE64_PATTERN = /^[A-Za-z0-9+/]+={0,2}$/u;
7
- /** Decodes canonical base64 text without a Node Buffer dependency. */
8
- function decodeBase64(value) {
9
- const decoded = atob(value);
10
- return Uint8Array.from(decoded, (character) => character.charCodeAt(0));
11
- }
12
- /** Encodes bytes as lowercase hexadecimal for one exact key-type check. */
13
- function encodeHex(value) {
14
- let hex = "";
15
- for (const byte of value) {
16
- hex += byte.toString(16).padStart(2, "0");
17
- }
18
- return hex;
19
- }
9
+ const ED25519_SPKI_BYTES = 44;
10
+ /**
11
+ * Ed25519 verification on Web Crypto. Every runtime that Aksara contracts run
12
+ * in has `crypto.subtle`, so the layer is provided here and callers need no
13
+ * runtime layer.
14
+ */
15
+ const SignatureLayer = Signature.layerWebCrypto.pipe(Layer.provide(SecureRandom.layerWebCrypto));
20
16
  /** Reads the reviewed SPKI bytes from one exact public-key PEM. */
21
17
  function decodePublicKeyPem(keyId, publicKeyPem, subject) {
22
18
  const normalizedPublicKeyPem = publicKeyPem.replaceAll("\r\n", "\n");
@@ -29,44 +25,33 @@ function decodePublicKeyPem(keyId, publicKeyPem, subject) {
29
25
  .replace(PUBLIC_KEY_FOOTER, "")
30
26
  .replaceAll("\n", "")
31
27
  .trim();
32
- if (!PUBLIC_KEY_BASE64_PATTERN.test(body) || body.length % 4 !== 0) {
33
- return Effect.fail(new PublicKeyParseError({ keyId, subject }));
34
- }
35
- return Effect.succeed(decodeBase64(body));
36
- }
37
- /** Imports one exact Ed25519 SPKI key through Web Crypto. */
38
- function importEd25519PublicKey(keyId, publicKeyPem, subject) {
39
- return Effect.gen(function* () {
40
- const bytes = yield* decodePublicKeyPem(keyId, publicKeyPem, subject);
41
- if (bytes.byteLength !== 44 ||
42
- !encodeHex(bytes.slice(0, 12)).startsWith(ED25519_SPKI_PREFIX)) {
43
- return yield* new PublicKeyTypeError({ keyId, subject });
44
- }
45
- return yield* Effect.tryPromise({
46
- catch: () => new PublicKeyParseError({ keyId, subject }),
47
- try: () => crypto.subtle.importKey("spki", bytes, { name: "Ed25519" }, false, [
48
- "verify",
49
- ]),
50
- });
51
- });
28
+ return Effect.fromResult(Base64.decode(body)).pipe(Effect.mapError(() => new PublicKeyParseError({ keyId, subject })));
52
29
  }
53
- /** Decodes one unpadded base64url Ed25519 signature. */
30
+ /** Imports one exact Ed25519 SPKI key, refusing a key of any other type before it reaches Web Crypto. */
31
+ const importEd25519PublicKey = Effect.fn("AksaraContracts.importEd25519PublicKey")(function* (keyId, publicKeyPem, subject) {
32
+ const der = yield* decodePublicKeyPem(keyId, publicKeyPem, subject);
33
+ if (der.byteLength !== ED25519_SPKI_BYTES ||
34
+ !Hex.encode(der).startsWith(ED25519_SPKI_PREFIX)) {
35
+ return yield* new PublicKeyTypeError({ keyId, subject });
36
+ }
37
+ return yield* KeyFormat.decodePublic(KeyFormat.spki, Signature.ed25519, der).pipe(Effect.mapError(() => new PublicKeyParseError({ keyId, subject })));
38
+ });
39
+ /** Decodes one unpadded base64url Ed25519 signature, which its schema has already checked. */
54
40
  function decodeSignature(signature) {
55
- const base64 = signature.replaceAll("-", "+").replaceAll("_", "/");
56
- return decodeBase64(base64.padEnd(Math.ceil(base64.length / 4) * 4, "="));
41
+ return Effect.fromResult(Base64Url.decode(signature)).pipe(Effect.orDie);
57
42
  }
58
43
  /** Resolves trust and verifies domain-separated canonical bytes with Ed25519. */
59
44
  export const verifyEd25519Signature = Effect.fn("AksaraContracts.verifyEd25519Signature")(function* (input) {
60
45
  const resolver = yield* ContentVerificationKeyResolver;
61
46
  const publicKeyPem = yield* resolver.resolve(input.keyId);
62
47
  const publicKey = yield* importEd25519PublicKey(input.keyId, publicKeyPem, input.subject);
63
- const valid = yield* Effect.tryPromise({
64
- catch: () => new SignatureCheckError({
65
- keyId: input.keyId,
66
- subject: input.subject,
67
- }),
68
- try: () => crypto.subtle.verify("Ed25519", publicKey, decodeSignature(input.signature), new TextEncoder().encode(input.message)),
69
- });
48
+ const signature = yield* Signature.Signature;
49
+ const valid = yield* signature
50
+ .verify(publicKey, yield* decodeSignature(input.signature), new TextEncoder().encode(input.message))
51
+ .pipe(Effect.mapError(() => new SignatureCheckError({
52
+ keyId: input.keyId,
53
+ subject: input.subject,
54
+ })));
70
55
  if (valid) {
71
56
  return;
72
57
  }
@@ -74,4 +59,4 @@ export const verifyEd25519Signature = Effect.fn("AksaraContracts.verifyEd25519Si
74
59
  keyId: input.keyId,
75
60
  subject: input.subject,
76
61
  });
77
- });
62
+ }, Effect.provide(SignatureLayer));
package/dist/tree.js CHANGED
@@ -5,7 +5,7 @@ const ELEMENT_KEY = "$node";
5
5
  /** Name of the element that renders its children without an element of its own. */
6
6
  const FRAGMENT_NAME = "fragment";
7
7
  /** Renderer components are capitalized JSX names, never intrinsic tags. */
8
- const COMPONENT_NAME_PATTERN = /^[A-Z][A-Za-z0-9]*$/;
8
+ const COMPONENT_NAME_PATTERN = /^[A-Z][A-Za-z0-9]*$/u;
9
9
  /** Intrinsic HTML tags that the Markdown and MDX pipeline may emit, in code-unit order. */
10
10
  export const CONTENT_TREE_INTRINSIC_TAGS = [
11
11
  "a",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nakafa/aksara-contracts",
3
- "version": "2.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "Schemas and verification contracts for Nakafa content releases.",
5
5
  "homepage": "https://github.com/nakafaai/aksara#readme",
6
6
  "license": "SEE LICENSE IN LICENSE",
@@ -21,7 +21,7 @@
21
21
  "directory": "packages/contracts"
22
22
  },
23
23
  "peerDependencies": {
24
- "effect": "4.0.5"
24
+ "effect": "4.1.0"
25
25
  },
26
26
  "exports": {
27
27
  "./artifact/integrity": {