From 56a10b024a97537023194ba3da42d44b40b9008f Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 00:17:12 +1100 Subject: [PATCH 1/8] feat(recipher-js): encrypt a VcUuid as the engine's UUID The engine now holds a UUID, so the addon stops refusing one. A VcUuid is written as the engine's UUID and decrypts back as a VcUuid. A string that holds a UUID is still text. - js.rs and error.rs: delete `is_uuid` and the `recipher_js::uuid` error. - The TypeScript types: `VcUuid` is a `Scalar` and decrypts as a `VcUuid`, and `kinds.uuid` names the kind, for `Ciphertext.fromBytes` and for a record field. - Tests: a VcUuid round trip and a read with `kinds.uuid`; the engine opens the addon's UUID as its own UUID type, and the addon opens the engine's; a record with a uuid field is stored, found by equality (the same UUID gives the stored term, another does not) and read back as a VcUuid; an order index on a uuid field is refused when the declaration is checked, with the engine's reason `index_not_admitted`. - The crate's tests turn on stack-encrypt's `uuid` feature for the engine's UUID type. Cargo.lock records the uuid crate as a dependency of vitaminc-aead; no new package is added. `OneValue` stays. The engine encrypts one value as one ciphertext only through its opaque scalar path, which takes a type whose reference is a vitaminc `Scalar`. An OwnedFrame can hold an array or a map, so it is not one. `OneValue` is the type that checks the frame holds one scalar, or holds a JSON document, before the engine reads it. --- .../packages/recipher-js/Cargo.lock | 1 + .../typescript/packages/recipher-js/README.md | 11 ++- .../__tests__/addon/records.test.ts | 66 +++++++++++++++ .../__tests__/addon/values.test.ts | 35 ++++++-- .../packages/recipher-js/crate/Cargo.toml | 4 +- .../recipher-js/crate/src/engine_tests.rs | 20 ++++- .../packages/recipher-js/crate/src/error.rs | 7 -- .../packages/recipher-js/crate/src/js.rs | 13 +-- .../recipher-js/crate/src/record_tests.rs | 82 +++++++++++++++++++ .../packages/recipher-js/src/kinds.ts | 9 +- .../packages/recipher-js/src/value-types.ts | 19 +++-- .../packages/recipher-js/src/values.ts | 7 +- .../recipher-js/type-tests/records.ts | 3 + .../packages/recipher-js/type-tests/values.ts | 11 ++- 14 files changed, 238 insertions(+), 50 deletions(-) diff --git a/languages/typescript/packages/recipher-js/Cargo.lock b/languages/typescript/packages/recipher-js/Cargo.lock index 30094cce9..410b00aae 100644 --- a/languages/typescript/packages/recipher-js/Cargo.lock +++ b/languages/typescript/packages/recipher-js/Cargo.lock @@ -3791,6 +3791,7 @@ dependencies = [ "rust_decimal", "serde", "thiserror 2.0.21", + "uuid", "vitaminc-aead-derive 0.10.0", "vitaminc-context 0.10.0", "vitaminc-protected 0.10.0", diff --git a/languages/typescript/packages/recipher-js/README.md b/languages/typescript/packages/recipher-js/README.md index 07b76926c..ed3cc5117 100644 --- a/languages/typescript/packages/recipher-js/README.md +++ b/languages/typescript/packages/recipher-js/README.md @@ -62,6 +62,7 @@ The `recipher-value-napi` crate, in `value-napi/`, converts JavaScript values by | `new VcTimestamp('2026-11-23T01:02:03.000000001Z')` | a timestamp, in nanoseconds | `Date` when it is whole milliseconds, else `VcTimestamp` | | `new VcDate('2026-11-23')` | a calendar date | `VcDate` | | `new VcDecimal('1.50')` | a decimal, with its scale | `VcDecimal` | +| `new VcUuid('123e4567-e89b-12d3-a456-426614174000')` | a UUID | `VcUuid` | | `null` | null | `null` | | `json(value)` | a JSON document, as its JSON text | the document, from the `Ciphertext` that `encrypt` returned or one read with `kinds.json` | @@ -70,9 +71,8 @@ The `recipher-value-napi` crate, in `value-napi/`, converts JavaScript values by The `number` `5` does not equal the integer `5n`. - A plain object is refused. Wrap it in `json(..)`. -- The package exports `VcUuid`, for a universally unique identifier (UUID). - The engine has no plaintext type for a UUID yet. - Encrypting a `VcUuid` is an `Err` with the code `recipher_js::uuid`. +- A UUID is a `VcUuid`, not a string. + A string that holds a UUID is encrypted as text. - vitaminc's rules refuse each of these values, with an error code for each: - a `Map` or a `Set`; - a class instance; @@ -152,7 +152,7 @@ const opened = await cipher.decrypt(user, stored.unwrap()) - The package has an ECMAScript module (ESM) build and a CommonJS build. A declaration made by either build works with a cipher from the other. - Each encrypted field names its kind, by the engine's name for it. - The kinds are `kinds.string`, `kinds.boolean`, `kinds.int8` to `kinds.int128`, `kinds.uint8` to `kinds.uint128`, `kinds.float32`, `kinds.float64`, `kinds.decimal`, `kinds.date`, `kinds.timestamp`, and `kinds.bytes`. + The kinds are `kinds.string`, `kinds.boolean`, `kinds.int8` to `kinds.int128`, `kinds.uint8` to `kinds.uint128`, `kinds.float32`, `kinds.float64`, `kinds.decimal`, `kinds.date`, `kinds.timestamp`, `kinds.bytes`, and `kinds.uuid`. - An integer field takes a `bigint` at every width. The field refuses a `bigint` outside its range. - A context is data that a ciphertext is bound to. @@ -164,6 +164,9 @@ const opened = await cipher.decrypt(user, stored.unwrap()) `equality` finds exact matches. `match` finds text matches. `ore` and `ope` compare order. + A field's kind decides which indexes it takes. + For example, a `kinds.uuid` field takes `equality` only. + When it checks the declaration, the engine refuses any other index on it, with the reason `index_not_admitted` in `error.fields`. - The stored row has one object for each encrypted field. The object has a `ciphertext` property. The object also has one property for each index the field declares: `equality`, `match`, `ore`, or `ope`. diff --git a/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts b/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts index 46be7f2dc..53d5d549f 100644 --- a/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts +++ b/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts @@ -10,6 +10,7 @@ import { record, VcDate, VcDecimal, + VcUuid, } from '../../src/index' import { testCipher } from '../../src/testing' @@ -128,6 +129,71 @@ describe('a record', () => { }) }) +describe('a UUID field', () => { + const device = record('devices', { + id: encrypt(kinds.uuid, { index: ['equality'] }), + name: encrypt(kinds.string), + }) + const text = '123e4567-e89b-12d3-a456-426614174000' + + it('is stored, found by equality, and read back as a VcUuid', async () => { + const c = await cipher() + const keyset = c.defaultKeyset() + const row = { id: new VcUuid(text), name: 'sensor' } + const stored = (await keyset.encrypt(device, row)).unwrap() + expect(Object.keys(stored.id).sort()).toEqual(['ciphertext', 'equality']) + + // A query term for the same UUID matches the stored term, and a query + // term for another UUID does not. + const same = ( + await keyset.encryptField(device, 'id', new VcUuid(text)) + ).unwrap() + const other = ( + await keyset.encryptField( + device, + 'id', + new VcUuid('123e4567-e89b-12d3-a456-426614174001'), + ) + ).unwrap() + expect(Buffer.from(same.equality)).toEqual(Buffer.from(stored.id.equality)) + expect(Buffer.from(other.equality)).not.toEqual( + Buffer.from(stored.id.equality), + ) + + const opened = (await c.decrypt(device, stored)).unwrap() + expect(opened.id).toBeInstanceOf(VcUuid) + expect(opened.id.value).toBe(text) + expect(opened.name).toBe('sensor') + }) + + it('refuses the UUID as a string', async () => { + const keyset = (await cipher()).defaultKeyset() + const result = await keyset.encrypt(device, { + // @ts-expect-error: a UUID field takes a VcUuid, not its text. + id: text, + name: 'sensor', + }) + expect(result.isErr()).toBe(true) + expect(result.err()?.field).toBe('id') + }) + + it('refuses an order index when the declaration is checked', async () => { + for (const index of ['ore', 'ope'] as const) { + const ordered = record('devices', { + id: encrypt(kinds.uuid, { index: [index] }), + }) + const checked = checkDeclarations([ordered]) + expect(checked.err()?.code).toBe('stack_encrypt::dynamic_plan') + expect(checked.err()?.fields.reason).toBe('index_not_admitted') + expect(checked.err()?.field).toBe('id') + expect(checked.err()?.record).toBe('devices') + + const result = await Cipher.create({ records: [ordered] }) + expect(result.err()?.fields.reason).toBe('index_not_admitted') + } + }) +}) + describe('one field', () => { it('encrypts alone to the form it has in a whole row', async () => { const c = await cipher() diff --git a/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts b/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts index 1b23051ac..5d9911c73 100644 --- a/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts +++ b/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts @@ -35,6 +35,7 @@ async function encryptOne( | VcDate | VcTimestamp | VcDecimal + | VcUuid | null, ) { return (await c.defaultKeyset().encrypt(value)).unwrap() @@ -104,6 +105,14 @@ describe('every value in the mapping round-trips', () => { expect((price as VcDecimal).value).toBe('1.50') }) + it('a UUID, as a VcUuid', async () => { + const id = await roundTrip( + new VcUuid('123e4567-e89b-12d3-a456-426614174000'), + ) + expect(id).toBeInstanceOf(VcUuid) + expect((id as VcUuid).value).toBe('123e4567-e89b-12d3-a456-426614174000') + }) + it('null', async () => { expect(await roundTrip(null)).toBeNull() }) @@ -278,6 +287,23 @@ describe('a ciphertext read back with a kind', () => { expect(opened).toBe(42n) }) + it('opens a UUID with kinds.uuid, and refuses its text', async () => { + const c = await cipher() + const keyset = c.defaultKeyset() + const text = '123e4567-e89b-12d3-a456-426614174000' + const sealed = (await keyset.encrypt(new VcUuid(text))).unwrap() + const stored = Ciphertext.fromBytes(sealed.toBytes(), kinds.uuid).unwrap() + const opened: VcUuid = (await c.decrypt(stored)).unwrap() + expect(opened.value).toBe(text) + + // The same UUID as a string is text, not a UUID. + const asText = (await keyset.encrypt(text)).unwrap() + const refused = await c.decrypt( + Ciphertext.fromBytes(asText.toBytes(), kinds.uuid).unwrap(), + ) + expect(refused.err()?.code).toBe('recipher_js::opened_type') + }) + it('refuses a value sealed as another type', async () => { const c = await cipher() const sealed = (await c.defaultKeyset().encrypt('text')).unwrap() @@ -295,15 +321,6 @@ describe('a ciphertext read back with a kind', () => { }) describe('refusals', () => { - it('a UUID, which the engine does not hold yet', async () => { - const keyset = (await cipher()).defaultKeyset() - const uuid = new VcUuid('123e4567-e89b-12d3-a456-426614174000') - // @ts-expect-error: a UUID is not a scalar of the engine's value model. - const result = await keyset.encrypt(uuid) - expect(result.err()?.code).toBe('recipher_js::uuid') - expect(result.err()?.message).toContain('UUID') - }) - it("an integer vitaminc cannot hold, with vitaminc's code", async () => { const keyset = (await cipher()).defaultKeyset() const result = await keyset.encrypt(2n ** 128n) diff --git a/languages/typescript/packages/recipher-js/crate/Cargo.toml b/languages/typescript/packages/recipher-js/crate/Cargo.toml index d2fad4c24..5c3b5484b 100644 --- a/languages/typescript/packages/recipher-js/crate/Cargo.toml +++ b/languages/typescript/packages/recipher-js/crate/Cargo.toml @@ -57,7 +57,9 @@ zeroize = "1" serde = { version = "1", features = ["derive"] } [dev-dependencies] -stack-encrypt = { path = "../../../../../packages/stack-encrypt", default-features = false, features = ["dynamic", "http", "test-support"] } +# `uuid` gives the tests the engine's UUID type, to seal and open one +# through the engine's typed API. +stack-encrypt = { path = "../../../../../packages/stack-encrypt", default-features = false, features = ["dynamic", "http", "test-support", "uuid"] } tokio = { version = "1", features = ["rt", "macros"] } # The values the engine-interop tests seal through the engine's typed API, # and the types the counting key source names. The versions the engine diff --git a/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs b/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs index 2f531ba9e..bfa2795ac 100644 --- a/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs +++ b/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs @@ -13,7 +13,7 @@ use stack_encrypt::kms::{ IdentifiedBy, IndexKey, IndexKeySource, KeySourceKind, OwnIdentity, RetrieveKeyPayload, UnverifiedContext, }; -use stack_encrypt::value::{Decimal, Timestamp}; +use stack_encrypt::value::{Decimal, Timestamp, Uuid as EngineUuid}; use stack_encrypt::{Encrypt, StackCipher}; use uuid::Uuid; @@ -103,6 +103,24 @@ async fn the_sdk_opens_what_the_engine_sealed_of_every_type() { } } +/// A UUID crosses both ways: the engine opens the SDK's as its own UUID +/// type, and the SDK opens the engine's as the same frame. +#[tokio::test] +async fn a_uuid_crosses_between_the_sdk_and_the_engine() { + let id = Uuid::from_u128(0x123e_4567_e89b_12d3_a456_4266_1417_4000); + let cipher = engine().await; + let keyset = cipher.default_keyset(); + + let sealed = sdk_seal(vec![one(EngineUuid::new(id))]).await.remove(0); + let opened: EngineUuid = keyset.decrypt(sealed).await.unwrap(); + assert_eq!(opened.to_uuid(), id); + + let sealed = keyset.encrypt(EngineUuid::new(id)).await.unwrap(); + let expected = OwnedFrame::of(EngineUuid::new(id)).unwrap(); + assert_eq!(sdk_open(sealed).await.frame(), expected.frame()); + assert_eq!(expected.frame().kind(), Some(ValueKind::Uuid)); +} + #[tokio::test] async fn a_json_document_the_engine_sealed_opens_as_json_with_its_big_integer() { let document = JsonPlaintext::from(serde_json::json!({"id": u64::MAX, "n": 5})); diff --git a/languages/typescript/packages/recipher-js/crate/src/error.rs b/languages/typescript/packages/recipher-js/crate/src/error.rs index a38530b9b..d781bb831 100644 --- a/languages/typescript/packages/recipher-js/crate/src/error.rs +++ b/languages/typescript/packages/recipher-js/crate/src/error.rs @@ -40,11 +40,6 @@ pub(crate) enum Error { help("Use `null` for a value that is not there.") )] Undefined, - /// A `VcUuid`. The engine's value model, the set of value types the - /// engine reads, has no UUID type. - #[error("a UUID cannot be encrypted yet: the engine's value model does not hold one")] - #[diagnostic(code(recipher_js::uuid), help("Encrypt its text instead."))] - Uuid, /// A value outside the engine's value model, or a value that Node-API /// failed to read. #[error("the value cannot be encrypted: it is not one the engine reads")] @@ -614,7 +609,6 @@ mod tests { let name = || "x".to_owned(); let all = vec![ Error::Undefined, - Error::Uuid, Error::Value, Error::Threw, Error::NotAScalar, @@ -652,7 +646,6 @@ mod tests { for error in &all { match error { Error::Undefined - | Error::Uuid | Error::Value | Error::Threw | Error::NotAScalar diff --git a/languages/typescript/packages/recipher-js/crate/src/js.rs b/languages/typescript/packages/recipher-js/crate/src/js.rs index 85be07627..1688ae3ef 100644 --- a/languages/typescript/packages/recipher-js/crate/src/js.rs +++ b/languages/typescript/packages/recipher-js/crate/src/js.rs @@ -24,7 +24,7 @@ use napi::bindgen_prelude::{FromNapiValue, JsObjectValue, JsValue, Object, Unknown}; use napi::{Env, Status, ValueType}; use napi::{Property, PropertyAttributes}; -use recipher_value_napi::{JsBuilder, JsFault, JsRef, Refusal, Wrapper, Wrappers}; +use recipher_value_napi::{JsBuilder, JsFault, JsRef, Refusal, Wrappers}; use stack_encrypt::dynamic::frame::OwnedFrame; use crate::error::{Error, RecipherErrorFields}; @@ -37,7 +37,6 @@ use crate::outcome::not_made; /// Returns one of these errors: /// /// - `recipher_js::undefined` for `undefined`; -/// - `recipher_js::uuid` for a `VcUuid`; /// - `recipher_js::value` when the bridge's bytes are not one well-formed /// frame the engine reads (`FrameError`); /// - `recipher_js::threw` when reading the value threw; @@ -46,9 +45,6 @@ pub fn read(env: &Env, value: Unknown<'_>) -> Result RecipherErrorFields { } } -/// Returns whether `value` is a vitaminc `VcUuid`. -fn is_uuid(env: &Env, value: &Unknown<'_>) -> bool { - Wrappers::get(env) - .and_then(|wrappers| wrappers.classify(value)) - .is_ok_and(|wrapper| wrapper == Some(Wrapper::Uuid)) -} - /// Returns the property `name` of `value`, when `value` is an object and /// the property is not `undefined`. /// diff --git a/languages/typescript/packages/recipher-js/crate/src/record_tests.rs b/languages/typescript/packages/recipher-js/crate/src/record_tests.rs index 555a6d014..9dcb787d1 100644 --- a/languages/typescript/packages/recipher-js/crate/src/record_tests.rs +++ b/languages/typescript/packages/recipher-js/crate/src/record_tests.rs @@ -248,6 +248,88 @@ fn the_engine_refuses_a_declaration_naming_the_record_and_the_field() { assert_eq!(unknown.code, "recipher_js::kind"); } +fn devices() -> Arc { + plan(json!({ + "type": "record", + "name": "devices", + "fields": { + "id": { "type": "sealed", "kind": { "name": "uuid" }, "index": ["equality"] }, + }, + })) + .unwrap() +} + +fn device_row(id: Uuid) -> OwnedFrame { + let id = OwnedFrame::of(stack_encrypt::value::Uuid::new(id)).unwrap(); + let frame = OwnedFrame::of(&Map(vec![("id", Entry::Value(&id))])).unwrap(); + conform_row(&devices(), &frame).unwrap() +} + +/// The `equality` term of the field `name` of a stored row. +fn equality_of(stored: &OwnedFrame, name: &str) -> Vec { + let field = field_value(stored, name).unwrap(); + let (_, term) = entries(&field) + .unwrap() + .into_iter() + .find(|(key, _)| key == "equality") + .unwrap(); + term.frame().as_bytes().to_vec() +} + +#[tokio::test] +async fn a_uuid_field_is_stored_found_by_equality_and_read_back() { + let cipher = engine().await; + let keyset = cipher.default_keyset(); + let id = Uuid::from_u128(0x123e_4567_e89b_12d3_a456_4266_1417_4000); + let other = Uuid::from_u128(0x123e_4567_e89b_12d3_a456_4266_1417_4001); + let stored = seal_rows(&keyset, devices(), vec![device_row(id)]) + .await + .unwrap() + .remove(0); + assert_eq!( + keys(&field_value(&stored, "id").unwrap()), + ["ciphertext", "equality"] + ); + + // A query for the same UUID derives the stored term, and another UUID + // does not. + let field = devices().field("id").unwrap(); + let query = |id: Uuid| { + let value = OwnedFrame::of(stack_encrypt::value::Uuid::new(id)).unwrap(); + conform_row(&field, &one_field_row("id", &value).unwrap()).unwrap() + }; + let found = seal_rows(&keyset, Arc::clone(&field), vec![query(id), query(other)]) + .await + .unwrap(); + assert_eq!(equality_of(&found[0], "id"), equality_of(&stored, "id")); + assert_ne!(equality_of(&found[1], "id"), equality_of(&stored, "id")); + + let opened = open_rows(&cipher, devices(), vec![stored]) + .await + .unwrap() + .remove(0); + let expected = OwnedFrame::of(stack_encrypt::value::Uuid::new(id)).unwrap(); + assert_eq!( + field_value(&opened, "id").unwrap().frame(), + expected.frame() + ); +} + +#[test] +fn an_order_index_on_a_uuid_field_is_refused_when_the_plan_is_built() { + for index in ["ore", "ope"] { + let refused = plan(json!({ + "type": "record", + "name": "t", + "fields": { "id": { "type": "sealed", "kind": { "name": "uuid" }, "index": [index] } }, + })) + .unwrap_err(); + assert_eq!(refused.code, "stack_encrypt::dynamic_plan", "{index}"); + assert_eq!(refused.reason(), Some("index_not_admitted"), "{index}"); + assert_eq!(refused.field.as_deref(), Some("id"), "{index}"); + } +} + #[test] fn a_json_field_needs_the_eql_extension() { let refused = plan(json!({ diff --git a/languages/typescript/packages/recipher-js/src/kinds.ts b/languages/typescript/packages/recipher-js/src/kinds.ts index 5976b785f..d19fd3f3f 100644 --- a/languages/typescript/packages/recipher-js/src/kinds.ts +++ b/languages/typescript/packages/recipher-js/src/kinds.ts @@ -7,7 +7,13 @@ * type. The kind's TypeScript type records the type of the kind's values. * An integer kind's values are a `bigint` at every width. */ -import type { OpenedJson, VcDate, VcDecimal, VcTimestamp } from './value-types' +import type { + OpenedJson, + VcDate, + VcDecimal, + VcTimestamp, + VcUuid, +} from './value-types' /** A kind: the type of a value, by the engine's name for it. */ export interface Kind { @@ -48,5 +54,6 @@ export const kinds = Object.freeze({ date: kind<'date', VcDate>('date'), timestamp: kind<'timestamp', Date | VcTimestamp>('timestamp'), bytes: kind<'bytes', Uint8Array>('bytes'), + uuid: kind<'uuid', VcUuid>('uuid'), json: kind<'json', OpenedJson>('json'), }) diff --git a/languages/typescript/packages/recipher-js/src/value-types.ts b/languages/typescript/packages/recipher-js/src/value-types.ts index 77cc09f74..bece7b08e 100644 --- a/languages/typescript/packages/recipher-js/src/value-types.ts +++ b/languages/typescript/packages/recipher-js/src/value-types.ts @@ -34,9 +34,9 @@ export interface VcDecimal { } /** - * A universally unique identifier (UUID). The class comes from vitaminc. - * The engine has no plaintext type for a UUID yet. Encrypting a `VcUuid` is - * an `Err` with the code `recipher_js::uuid`. `VcUuid` is not a `Scalar`. + * A universally unique identifier (UUID), such as + * `new VcUuid('123e4567-e89b-12d3-a456-426614174000')`. The class comes from + * vitaminc. */ export interface VcUuid { /** The UUID as hyphenated hex. */ @@ -56,6 +56,7 @@ export type Scalar = | VcTimestamp | VcDate | VcDecimal + | VcUuid | null /** @@ -114,11 +115,13 @@ export type Opened = V extends string ? VcDate : V extends VcDecimal ? VcDecimal - : V extends null - ? null - : V extends JsonMarker - ? OpenedJson - : never + : V extends VcUuid + ? VcUuid + : V extends null + ? null + : V extends JsonMarker + ? OpenedJson + : never /** Any scalar type that a stored ciphertext can decrypt to. */ export type OpenedScalar = Opened diff --git a/languages/typescript/packages/recipher-js/src/values.ts b/languages/typescript/packages/recipher-js/src/values.ts index 0143cbaa2..3c6fd42b4 100644 --- a/languages/typescript/packages/recipher-js/src/values.ts +++ b/languages/typescript/packages/recipher-js/src/values.ts @@ -16,6 +16,7 @@ * | `VcTimestamp` | a timestamp, in nanoseconds | `Date` when it is whole milliseconds, else `VcTimestamp` | * | `VcDate` | a calendar date | `VcDate` | * | `VcDecimal` | a decimal, with its scale | `VcDecimal` | + * | `VcUuid` | a UUID | `VcUuid` | * | `null` | null | `null` | * | `json(value)` | a JSON document, as its text | the document | * @@ -54,9 +55,9 @@ export type VcTimestamp = VcTimestampValue /** A decimal with its scale, such as `new VcDecimal('1.50')`. The class comes from vitaminc. */ export type VcDecimal = VcDecimalValue /** - * A universally unique identifier (UUID). The class comes from vitaminc. - * The engine has no plaintext type for a UUID yet. Encrypting a `VcUuid` is - * an `Err` with the code `recipher_js::uuid`. `VcUuid` is not a `Scalar`. + * A universally unique identifier (UUID), such as + * `new VcUuid('123e4567-e89b-12d3-a456-426614174000')`. The class comes from + * vitaminc. */ export type VcUuid = VcUuidValue diff --git a/languages/typescript/packages/recipher-js/type-tests/records.ts b/languages/typescript/packages/recipher-js/type-tests/records.ts index 536a4cf67..05ef888c9 100644 --- a/languages/typescript/packages/recipher-js/type-tests/records.ts +++ b/languages/typescript/packages/recipher-js/type-tests/records.ts @@ -10,6 +10,7 @@ import type { VcDate, VcDecimal, VcTimestamp, + VcUuid, } from '../src/index' import { encrypt, kinds, passthrough, record } from '../src/index' @@ -69,6 +70,7 @@ const all = record('all', { at: encrypt(kinds.timestamp), blob: encrypt(kinds.bytes), small: encrypt(kinds.uint8), + id: encrypt(kinds.uuid, { index: ['equality'] }), }) check< Exact< @@ -81,6 +83,7 @@ check< at: Date | VcTimestamp blob: Uint8Array small: bigint + id: VcUuid } > >() diff --git a/languages/typescript/packages/recipher-js/type-tests/values.ts b/languages/typescript/packages/recipher-js/type-tests/values.ts index cfeb57813..85e68a3be 100644 --- a/languages/typescript/packages/recipher-js/type-tests/values.ts +++ b/languages/typescript/packages/recipher-js/type-tests/values.ts @@ -45,6 +45,9 @@ check, RecipherError>>>() declare const price: VcDecimal const priced = await keyset.encrypt(price) check, RecipherError>>>() +declare const id: VcUuid +const identified = await keyset.encrypt(id) +check, RecipherError>>>() const document = await keyset.encrypt(json({ a: [1, 2n, null] })) check, RecipherError>>>() @@ -75,10 +78,6 @@ expectType(plainObject) // What `json(..)` returns is both. expectType(json({ a: 1 })) expectType(json({ a: 1 })) -// A UUID is not in the engine's value model yet. -declare const uuid: VcUuid -// @ts-expect-error: a UUID is not a value. -void keyset.encrypt(uuid) // @ts-expect-error: undefined is not a value. void keyset.encrypt(undefined) // @ts-expect-error: a Map is not a value. @@ -90,12 +89,16 @@ void json({ d: new Date() }) // The wrapper types are distinct. // @ts-expect-error: a VcDate is not a VcDecimal. expectType(day) +// @ts-expect-error: a VcUuid is not a VcDate. +expectType(id) // A ciphertext read from stored bytes is typed by the kind named, and as // any scalar when none is. declare const stored: Uint8Array const typed = Ciphertext.fromBytes(stored, kinds.int64) check, RecipherError>>>() +const typedUuid = Ciphertext.fromBytes(stored, kinds.uuid) +check, RecipherError>>>() const typedJson = Ciphertext.fromBytes(stored, kinds.json) check, RecipherError>>>() const untyped = Ciphertext.fromBytes(stored) From eba70fb49a82cfe572211dcd28537e8b112c38b8 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 00:24:18 +1100 Subject: [PATCH 2/8] test(recipher-js): cross a UUID with the engine through its stored bytes The engine's UUID is now a case of the every-type test, which opens each ciphertext from its stored bytes. The other direction, the engine opening a UUID the SDK sealed, also reads it back from its bytes first. --- .../recipher-js/crate/src/engine_tests.rs | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs b/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs index bfa2795ac..7f0434942 100644 --- a/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs +++ b/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs @@ -77,6 +77,9 @@ async fn the_sdk_opens_what_the_engine_sealed_of_every_type() { let decimal = Decimal::new(rust_decimal::Decimal::new(150, 2)); let timestamp_frame = OwnedFrame::of(×tamp).unwrap(); let decimal_frame = OwnedFrame::of(&decimal).unwrap(); + let id = EngineUuid::new(Uuid::from_u128(0x123e_4567_e89b_12d3_a456_4266_1417_4000)); + let uuid_frame = OwnedFrame::of(&id).unwrap(); + assert_eq!(uuid_frame.frame().kind(), Some(ValueKind::Uuid)); let cases: Vec<(stack_encrypt::Ciphertext, OwnedFrame)> = vec![ ( keyset.encrypt("text".to_owned()).await.unwrap(), @@ -96,6 +99,7 @@ async fn the_sdk_opens_what_the_engine_sealed_of_every_type() { ), (keyset.encrypt(timestamp).await.unwrap(), timestamp_frame), (keyset.encrypt(decimal).await.unwrap(), decimal_frame), + (keyset.encrypt(id).await.unwrap(), uuid_frame), ]; for (sealed, expected) in cases { let stored = stack_encrypt::Ciphertext::from_bytes(&sealed.to_bytes()).unwrap(); @@ -103,22 +107,21 @@ async fn the_sdk_opens_what_the_engine_sealed_of_every_type() { } } -/// A UUID crosses both ways: the engine opens the SDK's as its own UUID -/// type, and the SDK opens the engine's as the same frame. +/// The engine opens a UUID the SDK sealed, read back from its stored +/// bytes, as the engine's own UUID type. The other direction is a case of +/// the test above. #[tokio::test] -async fn a_uuid_crosses_between_the_sdk_and_the_engine() { +async fn the_engine_opens_a_uuid_the_sdk_sealed_from_its_stored_bytes() { let id = Uuid::from_u128(0x123e_4567_e89b_12d3_a456_4266_1417_4000); - let cipher = engine().await; - let keyset = cipher.default_keyset(); - let sealed = sdk_seal(vec![one(EngineUuid::new(id))]).await.remove(0); - let opened: EngineUuid = keyset.decrypt(sealed).await.unwrap(); + let stored = stack_encrypt::Ciphertext::from_bytes(&sealed.to_bytes()).unwrap(); + let opened: EngineUuid = engine() + .await + .default_keyset() + .decrypt(stored) + .await + .unwrap(); assert_eq!(opened.to_uuid(), id); - - let sealed = keyset.encrypt(EngineUuid::new(id)).await.unwrap(); - let expected = OwnedFrame::of(EngineUuid::new(id)).unwrap(); - assert_eq!(sdk_open(sealed).await.frame(), expected.frame()); - assert_eq!(expected.frame().kind(), Some(ValueKind::Uuid)); } #[tokio::test] From 03eb7a74e2f912a12589e93f4e23b92de3b0611c Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 00:25:46 +1100 Subject: [PATCH 3/8] docs(recipher-js): say how a VcUuid's text is read and how it decrypts A VcUuid takes hyphenated hex in either case, and decrypts as lowercase hyphenated hex. Other text, such as text in braces, of the wrong length or with a digit that is not hex, is an Err with vitaminc's code ERR_INVALID_UUID when it is encrypted, not a thrown exception. Tests: an uppercase VcUuid decrypts as lowercase, and the three kinds of malformed text are each an Err with ERR_INVALID_UUID whose message does not contain the text. --- .../typescript/packages/recipher-js/README.md | 3 +++ .../__tests__/addon/values.test.ts | 27 +++++++++++++++++++ .../packages/recipher-js/src/value-types.ts | 7 ++++- .../packages/recipher-js/src/values.ts | 5 ++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/languages/typescript/packages/recipher-js/README.md b/languages/typescript/packages/recipher-js/README.md index ed3cc5117..b269da3aa 100644 --- a/languages/typescript/packages/recipher-js/README.md +++ b/languages/typescript/packages/recipher-js/README.md @@ -73,6 +73,9 @@ The `recipher-value-napi` crate, in `value-napi/`, converts JavaScript values by Wrap it in `json(..)`. - A UUID is a `VcUuid`, not a string. A string that holds a UUID is encrypted as text. + A `VcUuid` takes hyphenated hex, in either case. + Other text, such as text in braces, is an `Err` with the code `ERR_INVALID_UUID` when it is encrypted. + A decrypted `VcUuid` holds lowercase hyphenated hex, whatever case it was built with. - vitaminc's rules refuse each of these values, with an error code for each: - a `Map` or a `Set`; - a class instance; diff --git a/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts b/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts index 5d9911c73..7feb63d5c 100644 --- a/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts +++ b/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts @@ -113,6 +113,14 @@ describe('every value in the mapping round-trips', () => { expect((id as VcUuid).value).toBe('123e4567-e89b-12d3-a456-426614174000') }) + it('a UUID built in uppercase, which decrypts as lowercase', async () => { + const id = await roundTrip( + new VcUuid('123E4567-E89B-12D3-A456-426614174ABC'), + ) + expect(id).toBeInstanceOf(VcUuid) + expect((id as VcUuid).value).toBe('123e4567-e89b-12d3-a456-426614174abc') + }) + it('null', async () => { expect(await roundTrip(null)).toBeNull() }) @@ -321,6 +329,25 @@ describe('a ciphertext read back with a kind', () => { }) describe('refusals', () => { + it('UUID text that is not hyphenated hex, as an Err', async () => { + const keyset = (await cipher()).defaultKeyset() + for (const text of [ + // One digit short. + '123e4567-e89b-12d3-a456-42661417400', + // In braces. + '{123e4567-e89b-12d3-a456-426614174000}', + // A digit that is not hex. + '123e4567-e89b-12d3-a456-42661417400g', + ]) { + const pending = keyset.encrypt(new VcUuid(text)) + expect(pending, text).toBeInstanceOf(Promise) + const result = await pending + expect(result.isErr(), text).toBe(true) + expect(result.err()?.code, text).toBe('ERR_INVALID_UUID') + expect(result.err()?.message, text).not.toContain(text) + } + }) + it("an integer vitaminc cannot hold, with vitaminc's code", async () => { const keyset = (await cipher()).defaultKeyset() const result = await keyset.encrypt(2n ** 128n) diff --git a/languages/typescript/packages/recipher-js/src/value-types.ts b/languages/typescript/packages/recipher-js/src/value-types.ts index bece7b08e..5a7ad6afb 100644 --- a/languages/typescript/packages/recipher-js/src/value-types.ts +++ b/languages/typescript/packages/recipher-js/src/value-types.ts @@ -37,9 +37,14 @@ export interface VcDecimal { * A universally unique identifier (UUID), such as * `new VcUuid('123e4567-e89b-12d3-a456-426614174000')`. The class comes from * vitaminc. + * + * The text is hyphenated hex, in either case. Encrypting other text, such + * as text in braces, of the wrong length, or with a digit that is not hex, + * is an `Err` with the code `ERR_INVALID_UUID`. A decrypted `VcUuid` holds + * lowercase hyphenated hex, whatever case it was built with. */ export interface VcUuid { - /** The UUID as hyphenated hex. */ + /** The UUID as hyphenated hex: as given, or lowercase when decrypted. */ readonly value: string toJSON(): string readonly [wrapper]: 'VcUuid' diff --git a/languages/typescript/packages/recipher-js/src/values.ts b/languages/typescript/packages/recipher-js/src/values.ts index 3c6fd42b4..cf725698b 100644 --- a/languages/typescript/packages/recipher-js/src/values.ts +++ b/languages/typescript/packages/recipher-js/src/values.ts @@ -58,6 +58,11 @@ export type VcDecimal = VcDecimalValue * A universally unique identifier (UUID), such as * `new VcUuid('123e4567-e89b-12d3-a456-426614174000')`. The class comes from * vitaminc. + * + * The text is hyphenated hex, in either case. Encrypting other text, such + * as text in braces, of the wrong length, or with a digit that is not hex, + * is an `Err` with the code `ERR_INVALID_UUID`. A decrypted `VcUuid` holds + * lowercase hyphenated hex, whatever case it was built with. */ export type VcUuid = VcUuidValue From 9133b315ecdb4ce07dafb36d0e482213510f1237 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 00:25:46 +1100 Subject: [PATCH 4/8] test(recipher-js): a uuid record field's malformed text and nulls - Malformed UUID text in a record field is an Err with ERR_INVALID_UUID that names the field and the record. - A uuid field with null 'sealed' stores a null as a ciphertext of a value's length, and one with null 'sql' stores null. Both decrypt as null. - A uuid field with no null setting refuses a null, naming the field, with the engine's code stack_encrypt::dynamic_source. --- .../__tests__/addon/records.test.ts | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts b/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts index 53d5d549f..f5a554544 100644 --- a/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts +++ b/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts @@ -166,6 +166,65 @@ describe('a UUID field', () => { expect(opened.name).toBe('sensor') }) + it('refuses UUID text that is not hyphenated hex, naming the field', async () => { + const keyset = (await cipher()).defaultKeyset() + for (const bad of [ + '123e4567-e89b-12d3-a456-42661417400', + '{123e4567-e89b-12d3-a456-426614174000}', + '123e4567-e89b-12d3-a456-42661417400g', + ]) { + const result = await keyset.encrypt(device, { + id: new VcUuid(bad), + name: 'sensor', + }) + expect(result.isErr(), bad).toBe(true) + expect(result.err()?.code, bad).toBe('ERR_INVALID_UUID') + expect(result.err()?.field, bad).toBe('id') + expect(result.err()?.record, bad).toBe('devices') + } + }) + + it('stores and decrypts a null under each null setting, and refuses one by default', async () => { + const nullable = record('nullable_devices', { + sealed: encrypt(kinds.uuid, { index: ['equality'], null: 'sealed' }), + sql: encrypt(kinds.uuid, { null: 'sql' }), + required: encrypt(kinds.uuid), + }) + const c = await cipher() + const keyset = c.defaultKeyset() + const id = new VcUuid(text) + + const empty = { sealed: null, sql: null, required: id } + const stored = (await keyset.encrypt(nullable, empty)).unwrap() + // A sealed null is stored like a value, padded to a value's length. + // A SQL null is stored as null. + const full = ( + await keyset.encrypt(nullable, { sealed: id, sql: id, required: id }) + ).unwrap() + expect(stored.sealed).not.toBeNull() + expect(stored.sealed.ciphertext.length).toBe(full.sealed.ciphertext.length) + expect(Buffer.from(stored.sealed.equality)).not.toEqual( + Buffer.from(full.sealed.equality), + ) + expect(stored.sql).toBeNull() + + const opened = (await c.decrypt(nullable, stored)).unwrap() + expect(opened.sealed).toBeNull() + expect(opened.sql).toBeNull() + expect(opened.required.value).toBe(text) + + // A field with the default null setting takes no null. + // @ts-expect-error: `required` has no null setting, so it takes no null. + const refused = await keyset.encrypt(nullable, { + sealed: id, + sql: id, + required: null, + }) + expect(refused.err()?.code).toBe('stack_encrypt::dynamic_source') + expect(refused.err()?.field).toBe('required') + expect(refused.err()?.record).toBe('nullable_devices') + }) + it('refuses the UUID as a string', async () => { const keyset = (await cipher()).defaultKeyset() const result = await keyset.encrypt(device, { From a868956a330a532310e55b92e7674ce550c727e7 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 00:41:13 +1100 Subject: [PATCH 5/8] test(recipher-js): pin the code of a UUID field given a string The test that a uuid field refuses a string now asserts the code, recipher_js::field_type, and the record, as the other refusal tests do. Any refusal of the field passed before. The malformed UUID test says why it checks that encrypt returns a promise: await passes a settled Result through unchanged, so the other assertions would not notice. --- .../packages/recipher-js/__tests__/addon/records.test.ts | 2 ++ .../packages/recipher-js/__tests__/addon/values.test.ts | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts b/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts index f5a554544..cf7808617 100644 --- a/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts +++ b/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts @@ -233,7 +233,9 @@ describe('a UUID field', () => { name: 'sensor', }) expect(result.isErr()).toBe(true) + expect(result.err()?.code).toBe('recipher_js::field_type') expect(result.err()?.field).toBe('id') + expect(result.err()?.record).toBe('devices') }) it('refuses an order index when the declaration is checked', async () => { diff --git a/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts b/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts index 7feb63d5c..8d365b8cf 100644 --- a/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts +++ b/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts @@ -340,6 +340,10 @@ describe('refusals', () => { '123e4567-e89b-12d3-a456-42661417400g', ]) { const pending = keyset.encrypt(new VcUuid(text)) + // A synchronous throw would fail the line above. This line catches + // `encrypt` returning a settled `Result` instead of a promise: `await` + // passes a value that is not a promise through unchanged, so the + // assertions below would still pass. expect(pending, text).toBeInstanceOf(Promise) const result = await pending expect(result.isErr(), text).toBe(true) From 7b9d19bf8fd7f6aa11e4ee3092793c8b1304380a Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 19:22:54 +1100 Subject: [PATCH 6/8] docs(recipher-js): plain language for the UUID prose Rewrite the comments, doc comments and README text that the UUID work added, so that each passes the plain-language checker. No code changes. - One idea in each sentence, and a list for the kinds of text that an `RcUuid` refuses. - A value "contains" its text, and a field "accepts" an index. - The comments say "encrypt" and "decrypt", as the README does. --- .../typescript/packages/recipher-js/README.md | 17 +++++++++-------- .../recipher-js/__tests__/addon/records.test.ts | 10 +++++----- .../recipher-js/__tests__/addon/values.test.ts | 4 ++-- .../packages/recipher-js/crate/Cargo.toml | 4 ++-- .../recipher-js/crate/src/engine_tests.rs | 6 +++--- .../recipher-js/crate/src/record_tests.rs | 4 ++-- .../packages/recipher-js/src/value-types.ts | 14 ++++++++++---- .../packages/recipher-js/src/values.ts | 14 ++++++++++---- 8 files changed, 43 insertions(+), 30 deletions(-) diff --git a/languages/typescript/packages/recipher-js/README.md b/languages/typescript/packages/recipher-js/README.md index ea8544309..bd2701fcc 100644 --- a/languages/typescript/packages/recipher-js/README.md +++ b/languages/typescript/packages/recipher-js/README.md @@ -63,7 +63,7 @@ The value bridge converts JavaScript values in both directions: | `new RcTimestamp('2026-11-23T01:02:03.000000001Z')` | a timestamp, in nanoseconds | `Date` when it is whole milliseconds, else `RcTimestamp` | | `new RcDate('2026-11-23')` | a calendar date | `RcDate` | | `new RcDecimal('1.50')` | a decimal, with its scale | `RcDecimal` | -| `new RcUuid('123e4567-e89b-12d3-a456-426614174000')` | a UUID | `RcUuid` | +| `new RcUuid('123e4567-e89b-12d3-a456-426614174000')` | a universally unique identifier (UUID) | `RcUuid` | | `null` | null | `null` | | `json(value)` | a JSON document, as its JSON text | the document, from the `Ciphertext` that `encrypt` returned or one read with `kinds.json` | @@ -78,10 +78,10 @@ The value bridge converts JavaScript values in both directions: `json(..)` encrypts the object as a JSON document. `opaque(..)`, for a whole object in the engine's own format, is not built yet. - A UUID is an `RcUuid`, not a string. - A string that holds a UUID is encrypted as text. - An `RcUuid` takes hyphenated hex, in either case. - Other text, such as text in braces, is an `Err` with the code `ERR_INVALID_UUID` when it is encrypted. - A decrypted `RcUuid` holds lowercase hyphenated hex, whatever case it was built with. + A string that contains a UUID is encrypted as text. + The text of an `RcUuid` must be hyphenated hex, in upper or lower case. + Encrypting an `RcUuid` with other text, such as text in braces, is an `Err` with the code `ERR_INVALID_UUID`. + A decrypted `RcUuid` contains lowercase hyphenated hex, whatever case it was built with. - The value bridge refuses each of these values, with an error code for each: - a `Map` or a `Set`; - a class instance; @@ -208,9 +208,10 @@ const opened = await cipher.decrypt(user, stored.unwrap()) `equality` finds exact matches. `match` finds text matches. `ore` and `ope` compare order. - A field's kind decides which indexes it takes. - For example, a `kinds.uuid` field takes `equality` only. - When it checks the declaration, the engine refuses any other index on it, with the reason `index_not_admitted` in `error.fields`. + A field's kind decides which indexes the field accepts. + For example, a `kinds.uuid` field accepts `equality` only. + When the engine checks the declaration, it refuses any other index on that field. + The refusal has the reason `index_not_admitted` in `error.fields`. - The stored row has one object for each encrypted field. The object has a `ciphertext` property. The object also has one property for each index the field declares: `equality`, `match`, `ore`, or `ope`. diff --git a/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts b/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts index 3da3ef5f9..082ac6ccd 100644 --- a/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts +++ b/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts @@ -143,8 +143,8 @@ describe('a UUID field', () => { const stored = (await keyset.encrypt(device, row)).unwrap() expect(Object.keys(stored.id).sort()).toEqual(['ciphertext', 'equality']) - // A query term for the same UUID matches the stored term, and a query - // term for another UUID does not. + // A query term for the same UUID matches the stored term. A query term + // for another UUID does not. const same = ( await keyset.encryptField(device, 'id', new RcUuid(text)) ).unwrap() @@ -213,8 +213,8 @@ describe('a UUID field', () => { expect(opened.sql).toBeNull() expect(opened.required.value).toBe(text) - // A field with the default null setting takes no null. - // @ts-expect-error: `required` has no null setting, so it takes no null. + // A field with the default null setting accepts no null. + // @ts-expect-error: `required` has no null setting, so it accepts no null. const refused = await keyset.encrypt(nullable, { sealed: id, sql: id, @@ -228,7 +228,7 @@ describe('a UUID field', () => { it('refuses the UUID as a string', async () => { const keyset = (await cipher()).defaultKeyset() const result = await keyset.encrypt(device, { - // @ts-expect-error: a UUID field takes an RcUuid, not its text. + // @ts-expect-error: a UUID field accepts an RcUuid, not its text. id: text, name: 'sensor', }) diff --git a/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts b/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts index 858fbc4e4..c2253c509 100644 --- a/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts +++ b/languages/typescript/packages/recipher-js/__tests__/addon/values.test.ts @@ -686,8 +686,8 @@ describe('refusals', () => { '123e4567-e89b-12d3-a456-42661417400g', ]) { const pending = keyset.encrypt(new RcUuid(text)) - // A synchronous throw would fail the line above. This line catches - // `encrypt` returning a settled `Result` instead of a promise: `await` + // A synchronous throw would fail the line above. This line fails if + // `encrypt` returns a settled `Result` instead of a promise. `await` // passes a value that is not a promise through unchanged, so the // assertions below would still pass. expect(pending, text).toBeInstanceOf(Promise) diff --git a/languages/typescript/packages/recipher-js/crate/Cargo.toml b/languages/typescript/packages/recipher-js/crate/Cargo.toml index a894e2664..91a1e8381 100644 --- a/languages/typescript/packages/recipher-js/crate/Cargo.toml +++ b/languages/typescript/packages/recipher-js/crate/Cargo.toml @@ -60,8 +60,8 @@ zeroize = "1" serde = { version = "1", features = ["derive"] } [dev-dependencies] -# `uuid` gives the tests the engine's UUID type, to seal and open one -# through the engine's typed API. +# `uuid` gives the tests the engine's UUID type. The tests encrypt and decrypt +# a UUID through the engine's typed API. stack-encrypt = { path = "../../../../../packages/stack-encrypt", default-features = false, features = ["dynamic", "http", "test-support", "uuid"] } tokio = { version = "1", features = ["rt", "macros"] } # The values the engine-interop tests encrypt through the engine's typed API, diff --git a/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs b/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs index 122502238..8ffcaac6f 100644 --- a/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs +++ b/languages/typescript/packages/recipher-js/crate/src/engine_tests.rs @@ -107,9 +107,9 @@ async fn the_sdk_opens_what_the_engine_sealed_of_every_type() { } } -/// The engine opens a UUID the SDK sealed, read back from its stored -/// bytes, as the engine's own UUID type. The other direction is a case of -/// the test above. +/// The engine decrypts a UUID that the SDK encrypted. The ciphertext is +/// read back from its stored bytes. The engine decrypts it as the engine's +/// own UUID type. The other direction is a case of the test above. #[tokio::test] async fn the_engine_opens_a_uuid_the_sdk_sealed_from_its_stored_bytes() { let id = Uuid::from_u128(0x123e_4567_e89b_12d3_a456_4266_1417_4000); diff --git a/languages/typescript/packages/recipher-js/crate/src/record_tests.rs b/languages/typescript/packages/recipher-js/crate/src/record_tests.rs index 88859d0bf..cb0bb5e8d 100644 --- a/languages/typescript/packages/recipher-js/crate/src/record_tests.rs +++ b/languages/typescript/packages/recipher-js/crate/src/record_tests.rs @@ -291,8 +291,8 @@ async fn a_uuid_field_is_stored_found_by_equality_and_read_back() { ["ciphertext", "equality"] ); - // A query for the same UUID derives the stored term, and another UUID - // does not. + // A query for the same UUID derives the stored term. A query for + // another UUID does not. let field = devices().field("id").unwrap(); let query = |id: Uuid| { let value = OwnedFrame::of(stack_encrypt::value::Uuid::new(id)).unwrap(); diff --git a/languages/typescript/packages/recipher-js/src/value-types.ts b/languages/typescript/packages/recipher-js/src/value-types.ts index d73c3d85a..16a971f9a 100644 --- a/languages/typescript/packages/recipher-js/src/value-types.ts +++ b/languages/typescript/packages/recipher-js/src/value-types.ts @@ -37,10 +37,16 @@ export interface RcDecimal { * A universally unique identifier (UUID), such as * `new RcUuid('123e4567-e89b-12d3-a456-426614174000')`. * - * The text is hyphenated hex, in either case. Encrypting other text, such - * as text in braces, of the wrong length, or with a digit that is not hex, - * is an `Err` with the code `ERR_INVALID_UUID`. A decrypted `RcUuid` holds - * lowercase hyphenated hex, whatever case it was built with. + * The text must be hyphenated hex, in upper or lower case. Encrypting other + * text is an `Err` with the code `ERR_INVALID_UUID`. Examples of other text + * are: + * + * - text in braces; + * - text of the wrong length; + * - text with a digit that is not hex. + * + * A decrypted `RcUuid` contains lowercase hyphenated hex, whatever case it + * was built with. */ export interface RcUuid { /** The UUID as hyphenated hex: as given, or lowercase when decrypted. */ diff --git a/languages/typescript/packages/recipher-js/src/values.ts b/languages/typescript/packages/recipher-js/src/values.ts index 4e224671e..459cc5c1f 100644 --- a/languages/typescript/packages/recipher-js/src/values.ts +++ b/languages/typescript/packages/recipher-js/src/values.ts @@ -138,10 +138,16 @@ export type RcDecimal = Types.RcDecimal * A universally unique identifier (UUID), such as * `new RcUuid('123e4567-e89b-12d3-a456-426614174000')`. * - * The text is hyphenated hex, in either case. Encrypting other text, such - * as text in braces, of the wrong length, or with a digit that is not hex, - * is an `Err` with the code `ERR_INVALID_UUID`. A decrypted `RcUuid` holds - * lowercase hyphenated hex, whatever case it was built with. + * The text must be hyphenated hex, in upper or lower case. Encrypting other + * text is an `Err` with the code `ERR_INVALID_UUID`. Examples of other text + * are: + * + * - text in braces; + * - text of the wrong length; + * - text with a digit that is not hex. + * + * A decrypted `RcUuid` contains lowercase hyphenated hex, whatever case it + * was built with. */ export const RcUuid: WrapperConstructor = wrapperClass('RcUuid') export type RcUuid = Types.RcUuid From 4d11d0c8f66c61e563c38b8939e4c06b541193a5 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sun, 29 Nov 2026 02:42:27 +1100 Subject: [PATCH 7/8] docs(stack-encrypt): the dynamic path takes a UUID The rustdoc of `value::Uuid` said the dynamic path refuses a UUID. It has taken one as `ValueKind::Uuid` since the dynamic value model added the kind, and this branch relies on that. The `uuid` feature comment in `Cargo.toml` was corrected on `next` in 85c160545. This fixes the same claim in the rustdoc. --- packages/stack-encrypt/src/value.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/stack-encrypt/src/value.rs b/packages/stack-encrypt/src/value.rs index 92ed1a385..ecba56c10 100644 --- a/packages/stack-encrypt/src/value.rs +++ b/packages/stack-encrypt/src/value.rs @@ -116,7 +116,7 @@ pub use vitaminc_aead::Decimal; /// cannot be wiped. Hold a UUID you encrypt as this type. /// /// The dynamic path a language binding uses (`stack_encrypt::dynamic`) -/// does not take a UUID yet: it refuses one as a value outside its value -/// model. +/// takes a UUID as `ValueKind::Uuid`, and encrypts and indexes it. It does +/// not need this type or the `uuid` feature. #[cfg(feature = "uuid")] pub use vitaminc_aead::Uuid; From 961617e7693a05d76353e5077f99dd46b34a338b Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sun, 29 Nov 2026 20:50:16 +1100 Subject: [PATCH 8/8] test(recipher-js): call the UUID record tests with the record in the options The merge of feat/recipher-js-records brings the new record calls. The UUID tests still passed the record first and called encryptField. They now pass the row first and `{ record: DeviceRecord }` second. The two query terms for a UUID come from `{ record: DeviceRecord, field: 'id' }`. The declarations are named DeviceRecord and NullableDeviceRecord. --- .../__tests__/addon/records.test.ts | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts b/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts index 407bcf324..4dd75d1a3 100644 --- a/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts +++ b/languages/typescript/packages/recipher-js/__tests__/addon/records.test.ts @@ -154,7 +154,7 @@ describe('a record', () => { }) describe('a UUID field', () => { - const device = record('devices', { + const DeviceRecord = record('devices', { id: encrypt(kinds.uuid, { index: ['equality'] }), name: encrypt(kinds.string), }) @@ -164,19 +164,23 @@ describe('a UUID field', () => { const c = await cipher() const keyset = c.defaultKeyset() const row = { id: RcUuid.from(text).unwrap(), name: 'sensor' } - const stored = (await keyset.encrypt(device, row)).unwrap() + const stored = ( + await keyset.encrypt(row, { record: DeviceRecord }) + ).unwrap() expect(Object.keys(stored.id).sort()).toEqual(['ciphertext', 'equality']) // A query term for the same UUID matches the stored term. A query term // for another UUID does not. const same = ( - await keyset.encryptField(device, 'id', RcUuid.from(text).unwrap()) + await keyset.encrypt(RcUuid.from(text).unwrap(), { + record: DeviceRecord, + field: 'id', + }) ).unwrap() const other = ( - await keyset.encryptField( - device, - 'id', + await keyset.encrypt( RcUuid.from('123e4567-e89b-12d3-a456-426614174001').unwrap(), + { record: DeviceRecord, field: 'id' }, ) ).unwrap() expect(Buffer.from(same.equality)).toEqual(Buffer.from(stored.id.equality)) @@ -184,7 +188,7 @@ describe('a UUID field', () => { Buffer.from(stored.id.equality), ) - const opened = (await c.decrypt(device, stored)).unwrap() + const opened = (await c.decrypt(stored, { record: DeviceRecord })).unwrap() expect(opened.id).toBeInstanceOf(RcUuid) expect(opened.id.value).toBe(text) expect(opened.name).toBe('sensor') @@ -203,10 +207,13 @@ describe('a UUID field', () => { // so the field's text is checked again when the row is encrypted. const forged: RcUuid = Object.create(RcUuid.prototype) Object.defineProperty(forged, 'value', { value: bad, enumerable: true }) - const result = await keyset.encrypt(device, { - id: forged, - name: 'sensor', - }) + const result = await keyset.encrypt( + { + id: forged, + name: 'sensor', + }, + { record: DeviceRecord }, + ) expect(result.isErr(), bad).toBe(true) expect(result.err()?.code, bad).toBe('ERR_INVALID_UUID') expect(result.err()?.field, bad).toBe('id') @@ -215,7 +222,7 @@ describe('a UUID field', () => { }) it('stores and decrypts a null under each null setting, and refuses one by default', async () => { - const nullable = record('nullable_devices', { + const NullableDeviceRecord = record('nullable_devices', { sealed: encrypt(kinds.uuid, { index: ['equality'], null: 'sealed' }), sql: encrypt(kinds.uuid, { null: 'sql' }), required: encrypt(kinds.uuid), @@ -225,11 +232,16 @@ describe('a UUID field', () => { const id = RcUuid.from(text).unwrap() const empty = { sealed: null, sql: null, required: id } - const stored = (await keyset.encrypt(nullable, empty)).unwrap() + const stored = ( + await keyset.encrypt(empty, { record: NullableDeviceRecord }) + ).unwrap() // A sealed null is stored like a value, padded to a value's length. // A SQL null is stored as null. const full = ( - await keyset.encrypt(nullable, { sealed: id, sql: id, required: id }) + await keyset.encrypt( + { sealed: id, sql: id, required: id }, + { record: NullableDeviceRecord }, + ) ).unwrap() expect(stored.sealed).not.toBeNull() expect(stored.sealed.ciphertext.length).toBe(full.sealed.ciphertext.length) @@ -238,7 +250,9 @@ describe('a UUID field', () => { ) expect(stored.sql).toBeNull() - const opened = (await c.decrypt(nullable, stored)).unwrap() + const opened = ( + await c.decrypt(stored, { record: NullableDeviceRecord }) + ).unwrap() expect(opened.sealed).toBeNull() expect(opened.sql).toBeNull() expect(opened.required.value).toBe(text) @@ -246,7 +260,7 @@ describe('a UUID field', () => { // A field with the default null setting accepts no null. const row = { sealed: id, sql: id, required: null } // @ts-expect-error: `required` has no null setting, so it accepts no null. - const refused = await keyset.encrypt(nullable, row) + const refused = await keyset.encrypt(row, { record: NullableDeviceRecord }) expect(refused.err()?.code).toBe('stack_encrypt::dynamic_source') expect(refused.err()?.field).toBe('required') expect(refused.err()?.record).toBe('nullable_devices') @@ -254,11 +268,14 @@ describe('a UUID field', () => { it('refuses the UUID as a string', async () => { const keyset = (await cipher()).defaultKeyset() - const result = await keyset.encrypt(device, { - // @ts-expect-error: a UUID field accepts an RcUuid, not its text. - id: text, - name: 'sensor', - }) + const result = await keyset.encrypt( + { + // @ts-expect-error: a UUID field accepts an RcUuid, not its text. + id: text, + name: 'sensor', + }, + { record: DeviceRecord }, + ) expect(result.isErr()).toBe(true) expect(result.err()?.code).toBe('recipher_js::field_type') expect(result.err()?.field).toBe('id')