feat: Alice buys a widget from Bob, from the JS client

shop.mjs runs the ERTP story step by step as the principal of a key
file; connect.js holds the session setup it shares with counter.mjs.
The client stages any number of string, integer, and reference
arguments, and reports why an answer broke.

A new start from a caller closes that caller's old session, so a client
can come back with the same key.

Generated-by: Claude Code (claude-opus-5-5)
This commit is contained in:
dckc committed 2026-11-18 06:40:09 -06:00
1 parent ed7d662d56
commit 9db91b1b9c
8 files changed
+287 -86

No files matched your search

+32 -3
View File
@@ -40,12 +40,40 @@ value 2
With another key file, `use c1` breaks: that home is someone else's. `icp network stop` stops the local network. `zig build test` runs the same story in memory.
## Alice buys a widget from Bob
[ertp.zig](src/ertp.zig) is a small [ERTP](https://docs.agoric.com/guides/ertp/): mints, issuers, purses, payments, and a shop that sells one unit of stock for an exact price. A sale moves the payment, the proceeds, and the stock in one Loam transaction. With the network and canister from above:
```sh
shop() { node client/shop.mjs http://127.0.0.1:4960 $ID "$@"; }
shop alice.key mint # moola: 10 into Alice's wallet; issuer on the board
shop bob.key open # widgets: 1 in stock; shop on the board at 3 moola
shop alice.key buy
shop alice.key buy # sold out: the payment goes back to the wallet
icp canister install counter --mode upgrade -y
shop alice.key balances
shop bob.key balances
```
```
bought 1 widget into closet
refunded 3 moola to wallet
Error: answer 5 broke: SoldOut
Canister counter installed successfully
wallet 7
closet 1
proceeds 3
```
(Update-call lines omitted. Alice's `balances` also reports `proceeds none`, and Bob's reports no wallet or closet: each home holds only its own.)
## Homes, not swiss numbers
Nodes on a canister's subnet can read its state, so a canister can't keep a swiss number secret. Authority here starts instead with the caller principal, whose signature the Internet Computer checks:
- `fetch("home")` answers the caller's own home, a durable store of named objects: `init(name, object)` and `get(name)`. It's made the first time the caller asks.
- `fetch("makeCounter")` answers the one public maker; `make()` answers a fresh counter at zero.
- `fetch("board")` answers a home every caller shares, where Alice and Bob post issuers and the shop.
- `fetch("makeCounter")`, `fetch("makeIssuerKit")`, and `fetch("makeShop")` answer public makers; each one's `make(...)` answers what it makes.
- The anonymous principal is refused, since everyone who uses it would share one home.
Homes, counters, and the canister's CapTP identity live in a [Loam](https://github.com/glowcap-org/glowcap) store whose engine ([stable_log.zig](src/stable_log.zig)) appends each committed transaction to stable memory, the part of a canister's state that survives an upgrade; `post_upgrade` replays it. CapTP sessions live in the heap, so an upgrade ends them, and a client starts a new one and fetches its home again.
@@ -70,7 +98,8 @@ Homes, counters, and the canister's CapTP identity live in a [Loam](https://gith
## Known gaps
- **The entropy is not secret** from the subnet's nodes, nor from whoever installs; nothing rests on it but the CapTP keys. A real deployment would draw it from `raw_rand` in a first call after install.
- **One session per principal.** A second `start` from the same caller traps until an upgrade clears sessions; a client that loses its session can't reconnect with the same key.
- **No removal or collection.** A home holds 8 names and can't drop one; Loam garbage collection never runs.
- **One session per principal.** A new `start` from a caller closes the caller's old session.
- **Not offer-safe.** A buyer hands over a payment and trusts the shop's code, which is right here because it's in the same vat; across vats this needs Zoe's escrow. The board is init-only, but anyone may post any name first.
- **No removal or collection.** A home holds 8 names and can't drop one; spent payments keep their records; Loam garbage collection never runs.
- **The canister pays for every call** and admits any signed caller; it should refuse strangers before doing work (`canister_inspect_message`).
- **Third-party handoffs** need gift ids from entropy and dials to other peers; the canister refuses both.