Cotton code
265 lines · 10.5 KB · MarkdownRaw
# CottonCotton is a family of small apps, each for one thing, over one database that you own. Each app is available on iPhone and Android. This repository holds the source for the apps, the shared core they are built on, the server, and the public website.The apps described on the public site are:- **Days**: your calendar and your to-dos, in one list.- **Words**: notes, lists and longer writing.- **Story**: a screenplay, written on your phone.- **Comms**: messages and calls with the people you know.More apps live in the repository at various stages, and not every one is released.The apps work before you sign in. Signing in creates your own database on the server and copies what you made into it. From then on your phones and the server stay in sync both ways, and the apps work offline. Sign-in is a phone number and a code sent by text message through Twilio. There is no email, password, or Google or Apple sign-in.Each person's database is one SQLite file, held in its own Cloudflare Durable Object (Cloudflare's unit of compute with its own private storage). Files are stored in Cloudflare R2, which is Cloudflare's object storage.What you write is encrypted on your phone before it leaves, with keys only your own devices hold. The server stores it but cannot read it. The server can see some plain details such as dates, flags and sizes, but not the words. To get back in on a new phone, you approve it from another of your devices or use a recovery code.In Comms, messages and one-to-one calls are end-to-end encrypted. Group calls (up to nine people) go through Cloudflare's relay and are not end-to-end encrypted.Cotton has no artificial intelligence of its own. You can connect an assistant you already use, for example Claude, to your own database at `cottondb.com/mcp`. The connection uses the Model Context Protocol (MCP), a standard way for an assistant to call tools on another system. The assistant can read only inside a time window that you allow from your phone.## What is here| Path | What it holds || --- | --- || `crates/` | The shared core in Rust, a Cargo workspace of 14 crates. || `apps/business` | The server, a Cloudflare Worker written in TypeScript. || `apps/cotton` | The iPhone apps, in Swift and SwiftUI. || `apps/loom` | A Swift package holding the look of every screen, with no database or Rust in it. The iPhone apps depend on it. || `apps/android` | The Android apps, in Kotlin and Jetpack Compose. || `apps/cotton-android` | An older single Android app, no longer developed. || `apps/cotton-site` | The public website at cottondb.com, also a Cloudflare Worker. || `apps/registry.json` | The list of apps, their names and their identifiers on each platform. || `scripts/` | Build and test scripts. |The rules and behavior of every app are decided in the Rust core, and the phone apps draw what the core answers. The crates you are most likely to open first are:- `cotton-store`: the file format.- `cotton-core`: the domain logic.- `cotton-tools`: the MCP tools.- `cotton-ffi`: the library the iPhone app links.- `cotton-android-jni`: the library the Android app loads.On iPhone, one Xcode project definition (`project.yml`, turned into an Xcode project by XcodeGen) builds every app. Each app is a target with its own scheme, named after the app, for example `books`. On Android, the apps share one Gradle project, with one product flavor per app.## Before you startYou only need the tools for the parts you plan to build.For the server, the scripts and the tests:- Node.js 22 or newer.- pnpm 10. The repository pins pnpm 10.11.0.For the Rust core, and so for both phone platforms:- Rust 1.98.1. The version is pinned in `crates/rust-toolchain.toml`, and rustup installs it automatically.For the iPhone apps:- A Mac with Xcode. The project targets iOS 26 and Swift 6.- XcodeGen, installed with `brew install xcodegen`.- The Rust targets `aarch64-apple-ios` and `aarch64-apple-ios-sim`.For the Android apps:- JDK 21.- The Android SDK, platform 36.- NDK 27.1.12297006.- cargo-ndk 4.1.2.The Gradle wrapper (Gradle 8.13) is included. In this project's own setup the Rust library for Android is built on Linux. Other hosts are not tested.For the server, you need a Cloudflare account to deploy. Local development and tests need none. Wrangler, Cloudflare's command-line tool, is installed with the project's dependencies.## Building and testingInstall everything once from the repository root:```shpnpm install```To run every package's tests:```shpnpm test```### ServerRun these from `apps/business`.Write the generated assets folder that the Worker serves. The tests also write it, so you can skip this step if you run them first.```shpnpm kit```Run the Worker locally with `wrangler dev`:```shpnpm dev```Run the Worker's tests. They run inside the Workers runtime and use no remote services.```shpnpm test```Deploy with `wrangler deploy`, to the Cloudflare account you are logged in to:```shpnpm run deploy```### Rust coreBuild the workspace:```shcargo build --manifest-path crates/Cargo.toml --release```Test one crate. Any crate name works in place of `cotton-core`.```shcargo test --manifest-path crates/Cargo.toml -p cotton-core```Run check, clippy with warnings as errors, and every crate's tests in one go:```shpnpm rust:verify```### iPhoneFirst build the Rust core as an XCFramework, the packaged form of a library that Xcode links. This command builds it for the simulator only. Drop the flag to build for both device and simulator.```shscripts/build-xcframework.sh --simulator-only```Then generate `apps/cotton/Cotton.xcodeproj` with XcodeGen. The generated project is not checked in.```shpnpm ios:project```Open `apps/cotton/Cotton.xcodeproj` in Xcode, pick an app's scheme (for example `books`) and a simulator, and run.To run the iPhone unit tests on a simulator:```shpnpm ios:test```Set `COTTON_SIM` to a simulator's id to choose which one the tests use.Each app builds in two variants, chosen by the `COTTON_FLAVOR` environment variable. `COTTON_FLAVOR=dev`, the default, builds the development variant, which talks to a development server. `COTTON_FLAVOR=real` builds the release variant.### AndroidBuild the Rust library first. Run this inside `crates/`:```shcargo ndk -t arm64-v8a -t x86_64 -P 28 build --locked -p cotton-android-jni --profile android-debug```Then copy each `libcotton_android_jni.so` from the target folder into the Android project:| From, in the target folder | To || --- | --- || `aarch64-linux-android/android-debug` | `apps/android/app/src/debug/jniLibs/arm64-v8a` || `x86_64-linux-android/android-debug` | `apps/android/app/src/debug/jniLibs/x86_64` |The remaining commands run from `apps/android`. This one builds a single app against the development server as a debug build:```sh./gradlew :app:assembleBooksDevDebug```The pattern for other builds is `:app:assemble<App><Live|Dev><Debug|Release>`.To run the unit tests:```sh./gradlew :shell:testDebugUnitTest :writing:testDebugUnitTest :app:testBooksDevDebugUnitTest```## Running your own serverThe server is one Cloudflare Worker, in `apps/business`, with its configuration in `apps/business/wrangler.jsonc`. Running your own means deploying that Worker to your Cloudflare account and then building the apps to point at it.### Cloudflare resourcesThe Worker uses:- Durable Objects (migrations are included).- A Cloudflare Queue and its dead-letter queue. Create them first with `wrangler queues create`.- Two R2 buckets.- Cloudflare Images.- Workers AI.- A rate limit binding.- A service binding to the website Worker.In `wrangler.jsonc`, change the Worker name, the domain route, and the bucket and queue names to your own.The same file has a dev environment. It signs in with a fixed code instead of Twilio, which is useful for testing. Put your dev Worker's host in `config/dev-server.json`, then deploy it from `apps/business` with `pnpm run deploy:dev`, which passes that host to the Worker.### SecretsSecrets are set with `wrangler secret put`. The server's core keys are:- `ADMIN_TOKEN`- `VAULT_KEY`- `SERVICE_TOKEN_KEY`- `WINDOW_KEY`- `STREAM_SIGNING_KEY`- `JOB_TOKEN_KEY`A server that is not the dev environment signs people in through Twilio Verify and also needs:- `TWILIO_ACCOUNT_SID`- `TWILIO_AUTH_TOKEN`- `TWILIO_VERIFY_SID`Push notifications, calls, and the Google, Spotify and film-information integrations have their own optional secrets. All of them are named in `apps/business/src/env.ts`.### Pointing the apps at your serverThe apps find their server through build settings, not at run time, so you build the apps yourself.- The development server's host is set once, in `config/dev-server.json`. Every app's `flavour.json` names its development server as `https://{dev-host}`, and the iPhone and Android builds fill in the host from that file.- The release server's address is in each app's `flavour.json`, under `apps/cotton/App/Flavours/<app>/`. The Android build reads the same file; an Android app with no iPhone version keeps its own under `apps/android/app/flavours/<app>/`. On Android, the release server's host for invitation links is also in `apps/android/app/build.gradle.kts`.The apps are signed with this project's identifiers (`com.cottondb.*`). To install your own builds, change these to identifiers you own, with your own Apple developer team and Android signing key. Then update the app association settings in `wrangler.jsonc` to match: `APPLE_APP_SITE_ASSOCIATION`, `ANDROID_ASSET_LINKS` and `PASSKEY_ANDROID_ORIGINS`.The website address cottondb.com also appears in a few places in the code, for example the settings screen's link to the site. Search for it when you move to your own domain.## LicenseCotton is licensed under the GNU Affero General Public License, version 3 only (AGPL-3.0-only). The full text is in `LICENSE`. If you run a modified version as a service for others, the license asks you to offer them your source.`NOTICE` lists the third-party software Cotton uses and each one's license.Copyright (C) 2026 DECEMBERFM, INC.## ContributingContributions are welcome. By sending one, you agree that it is licensed under the same license as the rest of the repository.Files do not each carry a license header. The license of every file in this repository is AGPL-3.0-only unless the file says otherwise. If you add a header, use this one line, written as a comment in the file's language:```SPDX-License-Identifier: AGPL-3.0-only```## ContactWrite to mukesh@tinfactory.co.