Metadata-Version: 2.4
Name: ledgdex
Version: 1.0.20
Summary: Simple market on a dex: signed, append-only ledgers with triple-entry accounting
Author: Noorul Ali
License: CC0-1.0
Project-URL: Homepage, https://ledgdex.github.io
Project-URL: Documentation, https://ledgdex.github.io/docs/
Project-URL: Source, https://github.com/ledgdex/ledgdex
Classifier: Programming Language :: Python :: 3
Classifier: License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
Classifier: Operating System :: OS Independent
Classifier: Environment :: Console
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: dexweb<5,>=4.2.5
Provides-Extra: fast
Requires-Dist: cryptography>=42; extra == "fast"
Dynamic: license-file

# Ledgdex

A simple market on a dex. Every seller and buyer keeps a signed, append-only ledger (`ledgdex.jsonl`) in their own dex, a freely shared website built with [dexweb](https://github.com/matrixdex/dexweb). A trade exists three times: the buyer's signed claim, the seller's signed record of it, and the buyer's copy of that record. No server, no blockchain: SHA-256, Ed25519 and plain files.

**Site: https://ledgdex.github.io** (viewer and live markets). **Docs: https://ledgdex.github.io/docs/** (getting started, setting up a marketplace, extending ledgdex, bots, the command line and API references, and tested examples). The docs are a dex built with dexweb, and a ledgdex too: dex is the core; dex + ledger is a ledgdex. Their source is [guide/](guide/).

The specification is [architecture/SPEC.md](architecture/SPEC.md). Everything in it is built: the market (offers, claims, payment, delivery, receipts), disputes, sealed-bid auctions, device keys, key rotation and recovery, indexes, the root, dex pages, publishing and tamper checks, in Python, plus a JavaScript version and a browser viewer.

## Install

```
pip install "ledgdex[fast]"
```

From the source on GitHub: `pip install "ledgdex[fast] @ git+https://github.com/ledgdex/ledgdex@v1.0.20"`. From a clone,
to work on ledgdex: `pip install -e ".[fast]"`. Releases before 1.0.20 are on GitHub only, as tags; add
`#subdirectory=py` to their address.

Python 3.8 or later. The only dependency is dexweb (4.2.5 or later, before 5). Private keys live in `~/.ledgdex` (or `$LEDGDEX_HOME`), never in a dex, with mode 600: ledgdex refuses a key file others can read.

The `fast` extra adds the `cryptography` package: signing and verifying get about 20 to 25 times faster. Without it ledgdex uses its own pure-Python Ed25519, with the same results. Verified ledgers are also cached (in `~/.cache/ledgdex`, or `$LEDGDEX_CACHE`) by the exact bytes already verified, so a ledger that grew is only verified from where it was verified before; any changed byte is verified in full. `ledgdex verify --full` and `LEDGDEX_NO_CACHE=1` skip the cache, and `LEDGDEX_PURE=1` forces the pure-Python Ed25519.

## Sell

```
ledgdex init shop --name "Mango Farm" --about "Alphonso mangoes from Ratnagiri" --key farm \
    --url https://farm.github.io --dest git@github.com:farm/farm.github.io.git --branch main
ledgdex offer shop offer.json
ledgdex publish shop
```

`offer.json`:

```json
{"item": {"title": "Alphonso mangoes", "text": "One dozen, ripe."},
 "quantity": 10, "unit": "dozen", "currency": "INR", "price": 120000,
 "pay": [{"method": "upi", "to": "farm@bank"}]}
```

Prices are integers in the currency's smallest unit (120000 is INR 1,200.00). Left out: `allow` is `"any"`, `terms` is empty, and the arbiter is the seller.

## A whole shop from a catalog

```
ledgdex shop catalog.json --repo OWNER/NAME
```

One command makes the shop from a JSON catalog: the ledgdex (if it is missing), one offer for each product, and the
publish with dexweb to GitHub. Run it again after the catalog changes: unchanged products keep their offers, new and
changed products get new offers, and the offers of changed and removed products are withdrawn. The same catalog again
signs nothing. In Python: `from ledgdex.shop import shop; shop("catalog.json", repo="OWNER/NAME")`.

```json
{"shop": {"name": "Penguin Shop", "currency": "USD", "pay": [{"method": "card", "to": "https://pay.example/penguin"}]},
 "products": [{"title": "Ice axe", "price": 4999, "quantity": 20, "text": "Steel, 70 cm"}]}
```

`shop.name`, `shop.currency` and `shop.pay` (one or more methods) are necessary; each product needs `title`, `price`
and `quantity`. Unknown fields are refused, and a wrong product stops the run before anything is signed. The full
format is in spec 7.5. GitHub Pages must be on for the repository (`OWNER.github.io` always has it).

## Buy

```
ledgdex init me --name "Asha" --key asha --url https://asha.github.io --dest git@github.com:asha/asha.github.io.git
ledgdex claim me https://farm.github.io OFFER_ID --quantity 2
ledgdex publish me
```

The claim is kept in your ledger as `sent`. Publishing your dex is sending it: the seller collects it with `ledgdex record shop --from https://asha.github.io`, or you send the claim file any other way and the seller runs `ledgdex record shop claim-....json`.

## The rest of a trade

```
ledgdex receipt me https://farm.github.io                  buyer keeps the seller's record: the receipt and the claim id
ledgdex pay me https://farm.github.io CLAIM_ID --method upi --ref UTR123
ledgdex record shop --from https://asha.github.io          seller records the payment notice
ledgdex received shop CLAIM_ID 240000
ledgdex delivered shop CLAIM_ID --note "by courier"
ledgdex confirm me https://farm.github.io CLAIM_ID
ledgdex record shop --from https://asha.github.io          the claim is now closed
```

Publish after each step that others need to see. Anyone can check any ledger:

```
ledgdex verify https://farm.github.io
ledgdex state https://farm.github.io
```

## Disputes

```
ledgdex dispute me CLAIM_ID --seller https://farm.github.io --text "never arrived"   buyer
ledgdex record shop --from https://asha.github.io
ledgdex ruling judge https://farm.github.io DISPUTE_ID --outcome refund            the offer's arbiter
ledgdex record shop --from https://judge.github.io
```

## Sealed-bid auctions

```
ledgdex auction shop auction.json        {"item": {"title": "Old map"}, "currency": "INR", "close": "...", "reveal_until": "...", "reserve": 10000}
ledgdex bid me https://farm.github.io AUCTION_ID --amount 25000      only a commitment is sent; the amount stays in ~/.ledgdex/bids
ledgdex reveal me https://farm.github.io AUCTION_ID                 after the close, before reveal_until
```

The seller records bids and reveals with `ledgdex record`. The best revealed bid that meets the reserve wins; payment and delivery then use the auction id in place of a claim id.

## Keys

```
ledgdex keygen phone                                  on the phone: prints its public key
ledgdex device add shop ed25519:... --name phone      with the owner key: the phone may now sign
ledgdex device revoke shop ed25519:... --reason lost
ledgdex rotate shop --key farm2                       replace the owner key
ledgdex recover root shop ed25519:NEW                 the root names a new key for a lost one
ledgdex recovered shop --root ROOT --key farm-new     the owner takes the ledger back
```

On a machine with a device key, everyday commands sign with it. Messages to other ledgers (claims, payments, bids) are signed with the owner key, which is a self's identity in other ledgers.

## Indexes and the root

```
ledgdex list market https://farm.github.io --note "mangoes"     an index lists ledgers
ledgdex discover https://market.github.io --offers              what the listed ledgers sell
ledgdex admit root ed25519:...                                  the root admits selves to The Matrix
ledgdex state shop --root https://root.github.io                "allow": "admitted" then needs the root too
```

## Running the root

The root is the ledger whose admissions define who is in The Matrix, and its owner is the default arbiter. Today
Noorul Ali, founder of The Matrix, runs it. It is an ordinary ledgdex:

```
ledgdex init root --name "The Matrix root" --about "Admission to The Matrix" --key matrix-root \
    --url https://ROOT_ADDRESS --dest git@github.com:YOU/root.git --branch main
ledgdex publish root
```

Keep the owner key (`~/.ledgdex/matrix-root.key`) backed up offline. For daily work, give the laptop a device key,
which may admit and revoke but not change keys. Recovery hands a ledger to a new key, so `ledgdex recover` needs the
owner key itself:

```
ledgdex keygen root-laptop                                   prints ed25519:...
ledgdex device add root ed25519:... --name laptop            with the owner key, once
```

Then, with `ledgdex publish root` after each change:

```
ledgdex admit root ed25519:THEIR_KEY --name "Shop 12" --note "ground floor"      admit a self
ledgdex revoke root ed25519:THEIR_KEY --reason "left the mall"                   take it back
ledgdex recover root https://THEIR_DEX ed25519:THEIR_NEW_KEY                     with the owner key, after checking who they are
ledgdex list root https://THEIR_DEX --note "Shop 12"                             optional: the root as an index too
```

Every dex that trades in The Matrix names the root in its `config.json`, as `"ledgdex": {"root": "https://ROOT_ADDRESS"}`.
The first time ledgdex reads it, it adds `"root_id"`, the root's ledger id, and from then on refuses any other ledger
at that address (or a broken one): whoever controls the address cannot swap in a root of their own. Set `root_id`
yourself, from a source you trust, to pin it before the first read. The dex also keeps the last root it read, in `.ledgdex-root.jsonl`: when
the root's address is down, commands work on with that copy, and an address serving an older root is ignored.
Its "admitted" offers then need admission by the root too, and its offers and auctions default to the root's owner as
arbiter. Rulings are signed with the owner key, since that is the arbiter key offers name:

```
ledgdex ruling root https://SELLER_DEX DISPUTE_ID --outcome refund --text "..."
```

Run `ledgdex check` on the root (the GitHub workflow, or cron) so any change to its history is caught.

## JavaScript and the viewer

`js/` holds the same core in plain browser JavaScript (no npm): canonical JSON, SHA-256/512, Ed25519, verification, the state function, the dex pages (`render.js`) and the dexweb build (`dexweb.js`). `node js/test.mjs` checks it reproduces the shared vectors in `vectors/` byte for byte: states, pages, and whole dexs that dexweb built. `python py/tests/make_vectors.py` regenerates them.

The site https://ledgdex.github.io comes from three dexes. Dexweb publishes them to [ledgdex/ledgdex.github.io](https://github.com/ledgdex/ledgdex.github.io), and GitHub Pages serves that repository.

| Source | Published to | Address | Publish with |
|---|---|---|---|
| `home/` (home page, Live Markets, About) | the root | https://ledgdex.github.io | `cd home && python run.py` |
| `viewer/` (the viewer and the JavaScript modules) | `viewer/` | https://ledgdex.github.io/viewer/index.html | `cd viewer && python run.py` |
| `guide/dex/` (the docs) | `docs/` | https://ledgdex.github.io/docs/ | `cd guide/dex && ledgdex publish .` |

After you change `viewer/` or `js/`, publish the viewer again. `python run.py --build-only` builds `gen/` and does not publish. The home pages run no scripts. Only the viewer runs scripts.

The viewer page loads any ledger by address or file, verifies it in the browser, and shows it as the same pages its dex has. It signs claims, payments, confirmations, disputes, bids and reveals with a key kept in the browser, using the browser's own constant-time Web Crypto Ed25519 when it has one, else ledgdex's built-in code. The key is stored only sealed with your passphrase ("Keep, sealed"), and "Download key backup" gives you a copy to keep offline. The viewer also keeps your own ledger: every message you sign is kept as `sent`, and "Collect receipts" keeps the seller's records of them. "Download your dex" gives you your ledger as a complete dex (a zip), file for file what `ledgdex init` and dexweb make; publish it like any dex. So a buyer with only a browser has a ledgdex too. Open a ledger directly with `https://ledgdex.github.io/viewer/index.html?ledger=https://farm.github.io`.

## Checking for tampering

`ledgdex verify` checks one ledger once. `ledgdex check` checks ledgers over time:

```
ledgdex check shop https://farm.github.io            once; exit code 1 on tampering
ledgdex check shop --every 3600                      as a worker, every hour
17 6 * * * cd /srv/ledgdex && ledgdex check shop --json report.json      from cron
ledgdex workflow seller/ledgdex.jsonl > .github/workflows/ledgdex-check.yml   as a GitHub workflow
```

It keeps the last copy of every ledger it has seen (in `.ledgdex-check/`) and reports an error, with signed proof in `.ledgdex-check/proofs/`, when a ledger stops extending that copy (two entries at one seq), when a receipt no longer matches the ledger it came from, or when an address starts serving a different ledger. Receipts in the ledgers you check are followed to the sellers' ledgers automatically. `--media` also checks offer pictures against their hashes; `--published` compares a dex folder with its published ledger. A copy that is only behind (a cache or an old snapshot) is a warning, not an error. The GitHub workflow runs on every push, daily and by hand, keeps its last-seen copies in the Actions cache, and attaches the report and proofs to each run.

## Every ledgdex is a dex

Each command rewrites the ledgdex pages in `data.json` (pages whose first paragraph starts with `<!-- ledgdex -->`) and rebuilds the site with dexweb. Your own pages are never touched. `ledgdex publish` publishes with dexweb; `config.json` has `"append_only": ["ledgdex.jsonl"]`, so dexweb refuses to publish a ledger that does not extend the published one. Publishing from two devices is safe: the second one catches up and re-sequences its unpublished entries.

## Production (v1.0)

Before you trade for real, read [SECURITY.md](SECURITY.md). In short:

- Install the `fast` extra (`pip install "ledgdex[fast]"`) on every machine that signs: it signs in constant time. ledgdex warns when it signs without it.
- Keep owner keys offline (backed up), and use device keys day to day. Revoke a lost device at once.
- Run `ledgdex check` on your ledger and the ledgers you trade with, daily (the workflow, cron or a worker). An error there is tampering, with proof.
- In the browser, keep your key sealed with a long passphrase and download a backup. Use the viewer at https://ledgdex.github.io/viewer/index.html, or host it on an address of its own: every page on one origin can read what pages there store.
- Generated workflows install ledgdex from the release tag and pin each action to a commit.

## Tests

Two suites. `python run_tests.py` is the essential one (about 30 s): every Python test except the docs examples and
the tutorial, with the fast backend, and the JavaScript core against the shared vectors; run it after small changes.
`python run_tests.py --full` is the full one (about 10 min): every Python test on both Ed25519 backends, the
JavaScript vectors, and a round of differential fuzzing; run it after larger changes and before a release. CI runs
the essential suite on every push (on Linux), and the full suite (on Linux, macOS and Windows) for release tags, by
hand, weekly, and for a push whose commit message contains `[full tests]`. Both work in a clone and in
the source package on PyPI, which holds the tests and their data; the wheel holds only the code. Steps:
https://ledgdex.github.io/docs/testlocalledgdexinstallation.html. It needs git and node 20 or later. The parts one by one:

```
cd py && python -m unittest discover -s tests
node js/test.mjs
cd py && python tests/fuzz.py --seed 7 --ledgers 1000      Python and JavaScript must agree on random and corrupted ledgers
```
