Skip to content

feat(recipher-js): every value type, arrays and JSON documents - #1775

Draft
coderdan wants to merge 1 commit into
feat/recipher-js-skeletonfrom
feat/recipher-js-values
Draft

coderdan wants to merge 1 commit into
feat/recipher-js-skeletonfrom
feat/recipher-js-values

Conversation

@coderdan

Copy link
Copy Markdown
Contributor

Summary

Step 2 of phase 1 of the TypeScript SDK plan (#1747), on top of #1774. @cipherstash/recipher now encrypts every value in vitaminc's JavaScript mapping, an array of values in one key request, and a JSON document with json(..). A value's type picks how it is encrypted; no call names a kind.

The package is still private and takes no changeset.

Changes

Values. keyset.encrypt(value) takes a string, boolean, number (a float), bigint (an integer at the narrowest width that holds it), Uint8Array or Buffer (bytes), Date, VcTimestamp, VcDate, VcDecimal or null, and returns a Ciphertext<T> typed by what it decrypts as (Opened<V>). vitaminc's four wrapper classes are re-exported. The addon reads and builds every value with vitaminc-aead-napi (JsRef::to_frame, JsBuilder::build); it has no conversion of its own.

The engine's own path. The core crate now seals each value through the engine's opaque scalar path, keyset.encrypt(value), by way of a small Sealable type that is one vitaminc Scalar. So each ciphertext is exactly what Rust's keyset.encrypt and Go write for that value. (Step 1 used encrypt_tree with an empty context; a test showed the bytes open either way, and this removes the question.)

Arrays. keyset.encrypt([a, b, c]) returns an array of ciphertexts, and decrypt of an array returns an array, in order. Both go through the engine's collection path (Vec<_>::encrypt_into, Vec<ValueFrame>::decrypt_from), which merges the elements' key requests into one. All or nothing: one refused element is an Err for the array, before any request.

JSON documents. json(value) seals a document as the engine's JsonPlaintext: one ciphertext holding its JSON text. The core crate converts the frame to a document (crates/core/src/json.rs): an integer outside a number's safe range is a bigint; a number that is an integer outside that range is refused, because its digits are not the number the caller meant; a bigint is written as its digits; bytes, dates and undefined are refused. On decrypt, the engine's parser reads the text, and an integer outside the safe range comes back as a bigint.

Refusals, as Err values. A plain object, Map, Set or class instance (recipher::plain_object), undefined, an array inside an array, a bigint of more than 128 bits and a string with an unpaired surrogate (vitaminc's refusals), and a VcUuid (the engine's).

What the tests prove

Rust (crates/core/tests/values.rs):

  • Every scalar of the engine's value model (text, bool, float, i8, u64::MAX, i128::MIN, bytes, null, a date, a nanosecond timestamp, a decimal) round-trips as itself, byte for byte.
  • An array is one key request each way. A test key source counts the requests: three values make one generate_keys and one retrieve_keys. An empty array makes none. A refused element makes none.
  • Each element of an array is the ciphertext the engine seals for one value: the engine's typed keyset.decrypt::<String> and ::<i64> open them.
  • A json(..) ciphertext opens with the engine's typed keyset.decrypt::<JsonPlaintext>, as the same document, u64::MAX included.
  • A UUID frame is refused by the engine's value model.

TypeScript (__tests__/addon/values.test.ts, 22 tests): each row of the mapping round-trips through stored bytes; a timestamp of whole milliseconds opens as a Date and a finer one as a VcTimestamp; arrays (in order, one element alone, mixed types, empty, refused whole); JSON documents (nested, bigint outside the safe range, refusals); and each refusal resolves to an Err, with no plaintext in its message. type-tests/values.ts checks the types: each value's Ciphertext<T>, a literal widening to its type, arrays, decrypt's result, and that a plain object, a VcUuid, undefined, a Map, and bytes or a Date inside json(..) do not compile.

Verification

Run locally on macOS (arm64), Rust 1.94.1, Node 24, pnpm 10.33.2:

  • cargo fmt --all --check: passed.
  • cargo clippy --locked --all-targets --all-features -- -D warnings: passed.
  • cargo test --locked --all-features: 30 passed (13 unit, 3 boundary, 7 round-trip, 7 value).
  • pnpm --filter @cipherstash/recipher build: passed.
  • pnpm --filter @cipherstash/recipher build:debug then test:addon: 35 passed.
  • pnpm --filter @cipherstash/recipher test (unit suites and tsc with the type tests): 16 passed, typecheck clean.
  • pnpm run code:check: no errors.
  • pnpm run test:scripts: 96 files passed.

Not run: anything against a live ZeroKMS. CI runs on this PR's push; see the checks.

Deviations from the plan

  • opaque(..) is not here. See "Needs from the engine".
  • A VcUuid is refused. The plan's table lists it. The engine's value model (ValueFrame) refuses a UUID, and the plan says to follow the engine. The class is still re-exported, and its type is left out of Scalar, so keyset.encrypt(uuid) does not compile.
  • A JSON document read back with no type opens as its text. The engine seals a document as a text leaf, so its bytes do not say it is JSON. A Ciphertext from encrypt(json(..)) remembers it; one from Ciphertext.fromBytes(bytes) does not, and opens as the JSON text. Typed fromBytes (the plan's Ciphertext.fromBytes(bytes, kinds.json)) needs kinds, which arrive with records in step 3.
  • A Date is stored as nanoseconds, not milliseconds. vitaminc reads a Date as milliseconds, and the engine's value model stores every timestamp in nanoseconds. It still opens as the same Date.
  • Not done in this step: the value profile of the conformance suite (record-values-v1), which is a record profile and needs step 3; running vitaminc's own value-conformance.cjs against this addon, which is written against vitaminc's test addon's exports; and the fuzz target. Each is listed under "Issues to open" in the stack's report.
  • The wrapper classes need the addon. They are vitaminc's classes, defined by the addon when it loads. If it did not load, constructing one throws a TypeError that says why. Every other call still returns an Err.

Needs from the engine

  • opaque(..) (the engine's OpaqueBlob format) for a value whose type is known only at run time. OpaqueBlob<T> writes vitaminc's storage profile through serde::Serialize, so it needs a Rust type. A binding holds a frame, and the engine has no function that writes the storage profile from a frame or reads one back into a frame. Writing that mapping in the SDK would make choices that decide whether Rust and Go can read the bytes (a JavaScript null as None or as unit, a map key's kind, how a date or a decimal is written). So this step does not invent it. What is missing: an engine (or vitaminc) entry point, frame to storage-profile bytes and back, with the mapping written down once for every binding.
  • A UUID in the value model. ValueFrame refuses a scalar with vitaminc's Uuid logical type, so no binding can encrypt one through dynamic, while Rust can encrypt a uuid::Uuid.

Related

Refs #1747 (the plan). Based on #1774.

Encrypt every value in vitaminc's JavaScript mapping: text, booleans,
numbers, bigints at every width, bytes, a Date, and vitaminc's VcDate,
VcTimestamp and VcDecimal, which the package re-exports. Values are
read and built only through vitaminc-aead-napi.

Each value goes through the engine's own opaque scalar path
(keyset.encrypt), so its ciphertext is the one Rust and Go write for
it. An array in is an array out: the engine's collection path makes
one key request each way, which a Rust test counts.

json(value) seals a document as the engine's JsonPlaintext. The
conversion from a frame to the document is in the core crate: an
integer outside a number's safe range is a bigint, an unsafe integer
held as a number is refused, and the engine's parser reads the result.

A plain object is refused, as is a UUID, which the engine's value
model does not hold.
@changeset-bot

changeset-bot Bot commented Nov 23, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 44064c7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant