Cotton code

Cotton

Cotton 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:

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

PathWhat it holds
crates/The shared core in Rust, a Cargo workspace of 14 crates.
apps/businessThe server, a Cloudflare Worker written in TypeScript.
apps/cottonThe iPhone apps, in Swift and SwiftUI.
apps/loomA Swift package holding the look of every screen, with no database or Rust in it. The iPhone apps depend on it.
apps/androidThe Android apps, in Kotlin and Jetpack Compose.
apps/cotton-androidAn older single Android app, no longer developed.
apps/cotton-siteThe public website at cottondb.com, also a Cloudflare Worker.
apps/registry.jsonThe 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:

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 start

You only need the tools for the parts you plan to build.

For the server, the scripts and the tests:

For the Rust core, and so for both phone platforms:

For the iPhone apps:

For the Android apps:

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 testing

Install everything once from the repository root:

pnpm install

To run every package's tests:

pnpm test

Server

Run 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.

pnpm kit

Run the Worker locally with wrangler dev:

pnpm dev

Run the Worker's tests. They run inside the Workers runtime and use no remote services.

pnpm test

Deploy with wrangler deploy, to the Cloudflare account you are logged in to:

pnpm run deploy

Rust core

Build the workspace:

cargo build --manifest-path crates/Cargo.toml --release

Test one crate. Any crate name works in place of cotton-core.

cargo test --manifest-path crates/Cargo.toml -p cotton-core

Run check, clippy with warnings as errors, and every crate's tests in one go:

pnpm rust:verify

iPhone

First 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.

scripts/build-xcframework.sh --simulator-only

Then generate apps/cotton/Cotton.xcodeproj with XcodeGen. The generated project is not checked in.

pnpm 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:

pnpm 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.

Android

Build the Rust library first. Run this inside crates/:

cargo 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 folderTo
aarch64-linux-android/android-debugapps/android/app/src/debug/jniLibs/arm64-v8a
x86_64-linux-android/android-debugapps/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:

./gradlew :app:assembleBooksDevDebug

The pattern for other builds is :app:assemble<App><Live|Dev><Debug|Release>.

To run the unit tests:

./gradlew :shell:testDebugUnitTest :writing:testDebugUnitTest :app:testBooksDevDebugUnitTest

Running your own server

The 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 resources

The Worker uses:

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.

Secrets

Secrets are set with wrangler secret put. The server's core keys are:

A server that is not the dev environment signs people in through Twilio Verify and also needs:

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 server

The apps find their server through build settings, not at run time, so you build the apps yourself.

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.

License

Cotton 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.

Contributing

Contributions 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

Contact

Write to mukesh@tinfactory.co.