diff --git a/AGENTS.md b/AGENTS.md index 4f8d33f..c4f9a69 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -7,9 +7,10 @@ or work on its code. People should start with [README.md](README.md). ### Rules -1. **Never handle a secret.** Cloud tokens and recovery phrases go only into the person's own - terminal. `doctl auth init`, `hcloud context create` and `./outpost cli login` ask for them - in a hidden prompt that needs a real terminal, so leave those three commands to the person. +1. **Never handle a secret.** Cloud tokens, recovery phrases and custody keys go only into the + person's own terminal. `doctl auth init`, `hcloud context create`, `./outpost cli login` and + `./outpost app-account` ask for them in a hidden prompt that needs a real terminal, so leave + those four commands to the person. If a token or phrase is pasted into the chat anyway, say so and stop: the token should be revoked, and the account the phrase belongs to should be treated as exposed. 2. **Never invent values.** Server addresses, FIDs, regions, prices and hashes come from command @@ -26,7 +27,15 @@ or work on its code. People should start with [README.md](README.md). node lets anyone on the internet submit messages through it. The client reaches the node through an SSH tunnel; that is on purpose. 6. **Test accounts only for phrases.** The recovery-phrase login is for a test account. For a - real account, the web client's "Sign with a wallet" is the path. + real account, the web client's "Sign with a wallet" is the path, or for an account made in + the Farcaster app, "Approve in the Farcaster app". +7. **Say what an approval in the Farcaster app gives away, then wait for an OK.** + `./outpost cli login --app` (or the web client's "Approve in the Farcaster app") adds an + onchain key with full access and no expiry that Outpost cannot revoke; the person may pay an + onchain fee in the app. Tell them that first. Once they say OK, run it with `--yes`; it prints + a QR code and a link that only the person can approve, on their phone. It needs an app + account, which the person sets up with `./outpost app-account` (an account made for this, + never a main account). ### Steps and checkpoints @@ -35,11 +44,12 @@ Run one step at a time and check its output before the next. | Step | Command | Done when it prints | If not | |---|---|---|---| | 0. Check | `doctl account get` (or `hcloud server list`), `node --version` | doctl: the account e-mail. hcloud: a server list, empty is fine. Node 20 or newer | the person logs in to the provider CLI themselves | +| 0b. No server (instead of 1 and 2) | `./outpost client --public`; for "Approve in the Farcaster app", the person runs `./outpost app-account --public` first | `using Farcaster's public node`, then `open http://127.0.0.1:8381 in your browser` | costs nothing. Tell the person the node is Farcaster's, not theirs: it sees what they read and post, and may refuse posts. Clean up is `./outpost uninstall --public` (or the app's connected apps) only; there is nothing to destroy | | 1. Server | `./outpost provision --watchdog 24 --yes` | `next: ./outpost node` | `422 failed to create volume: invalid size specified`: the DigitalOcean account is limited to smaller volumes. Nothing billable was made. Offer `--provider hetzner` (not yet tested on a real server; say so), or `OUTPOST_HOST= ./outpost node` on a server they already have | | 2. Node | `./outpost node` | `synced: every shard is within N blocks of the tip`, then `next: ./outpost client` | see below | | 3. Client | `./outpost client` | `open http://127.0.0.1:8381 in your browser` | it runs until Ctrl-C, so run it in the background or let the person run it in their own terminal | -| 4. Account | the person, in the browser or with `./outpost cli login` | "Your app key" shows in the web client | the account needs storage; an account made in the Farcaster app cannot connect without its phrase (README, Limits) | -| 5. Clean up | `./outpost uninstall`, then `./outpost destroy --yes` | `revoked app key …`, then `done. Billing for these resources has stopped.` | a key added in the web client is revoked there: "Your app key", then "Revoke this key (uninstall)" | +| 4. Account | the person, in the browser or with `./outpost cli login`; for an account made in the Farcaster app, `./outpost app-account` once (the person), then "Approve in the Farcaster app" or `./outpost cli login --app --yes` after rule 7 | "Your app key" shows in the web client; `login --app` prints `key 0x… is live for FID …` | the account needs storage; `no app account on this computer`: the person runs `./outpost app-account` | +| 5. Clean up | `./outpost uninstall`, then `./outpost destroy --yes` | `revoked app key …`, then `done. Billing for these resources has stopped.` | a key added in the web client is revoked there: "Your app key", then "Revoke this key (uninstall)". A key approved in the Farcaster app is only forgotten (`this key was approved in the Farcaster app`); the person removes it in the app: Settings, Advanced, Manage connected apps. `./outpost app-account remove` deletes the app account's key | Step 2 takes 6 to 8 hours. Ctrl-C is safe: the node keeps going on the server, and `./outpost status` shows where it is. While it runs: @@ -59,6 +69,11 @@ To check that clean-up worked, list the servers at the provider: `doctl compute or `hcloud server list` should no longer show the Outpost server. Every command is safe to run twice. +Once an account is connected, `./outpost cli notifications` lists replies, mentions, likes, +recasts and new followers (`--mark-read` clears the new count), and `./outpost cli cast "gm @name"` +posts `@name` as a mention, stopping on a name the node does not know (`--no-mentions` posts it +as text). Neither costs anything. + If the person ran the whole tutorial on their own server, [VOUCHER.md](VOUCHER.md) has a checklist and a draft cast. @@ -74,7 +89,12 @@ Keep these properties. [TRUST.md](TRUST.md) and [SECURITY.md](SECURITY.md) expla (`src/core/verify.ts`), and shows only what it rebuilt from the signed bytes. - The node's HTTP and gRPC ports stay bound to 127.0.0.1. - The app key can cast, react and follow, and nothing else. It expires, and it revokes itself - without the recovery phrase. + without the recovery phrase. The one exception, a key approved in the Farcaster app, is + labelled as full access with no expiry everywhere it shows, and the warning comes before the + QR code. +- The web page talks only to its own origin or the society node. Farcaster's endpoint + (`api.farcaster.xyz`) is called only by the Outpost server, the society's sign-in service or + the CLI, and the app account's custody key never reaches a browser, a response or a log. - A recovery phrase is used in memory only. It is never written to disk or sent anywhere. - A charter changes only when more than half of its members like a proposal (`src/core/votes.ts`). Each passed change keeps the signed likes, so anyone can recount it, diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 41d698f..482bbe6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -39,6 +39,10 @@ These need Docker or a synced node, so CI does not run them. - `scripts/e2e-devnet.ts`: the whole client flow against a local Snapchain devnet. The commands to start and stop the devnet are at the top of the file. +- `scripts/e2e-phase1.ts`: mentions, notifications and `login --app` against the same devnet, + with a stand-in for Farcaster's signed key request endpoint. +- `test/phase1-demo.ts`: the web client on a fake node, to try mentions, notifications and + "Approve in the Farcaster app" by hand (`npm run build` first). - `./outpost cli selftest`: signs every message type with throwaway keys and asks your node to validate them. It submits nothing. - `scripts/live-checks.sh`: reads from a synced mainnet node and saves the output to diff --git a/README.md b/README.md index d01e602..161b0b5 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,19 @@ Every command is safe to run twice. `--dry-run` on `provision`, `shrink` and `de the exact provider commands without running them. `./outpost status` shows the server and how far the node has synced. +Try it without a server: the client can use Farcaster's public node instead of your own, for +reading and posting. Nothing is rented and no provider login is needed, only Node.js 20: + +``` +npm ci && npm run build +./outpost app-account --public # only for "Approve in the Farcaster app"; you type the app account's phrase or key +./outpost client --public # open http://127.0.0.1:8381 +``` + +`./outpost cli --public ` works the same way, and `--node https://:` (or +`OUTPOST_NODE_URL`) uses any other node. That node is not yours: it sees what you read and post, +and Farcaster's public node may refuse posts. See [Limits](#limits). + Only looking? The [hosted client](https://spider333.github.io/outpost/app/) reads through Farcaster's public node with no setup: press "Read with the public node". It cannot post there, and with only one node it cannot cross-check gasless keys ([TRUST.md](TRUST.md)). @@ -133,7 +146,8 @@ flowchart LR company's servers or a hosted API. - **A narrow app key.** Logging in adds a gasless app key (`KEY_ADD`) that can only cast, react and follow, and undo those. It expires 30 days after last use (`--ttl-days`, 1 to 90, in the - command-line client). + command-line client). The one exception is "Approve in the Farcaster app", where the app adds + a full-access onchain key; the client labels it and says so before you approve. - **Revoke without the phrase.** The key revokes itself: `./outpost uninstall`, or the "Your app key" panel in the web client. - **A labelled, chronological feed.** The following feed is in time order, and each cast is @@ -147,12 +161,15 @@ flowchart LR It passes when more than half of the members have liked it, and anyone can recount it from their own node. See [Society votes](#society-votes). +- **Notifications from your node.** Replies, mentions, likes, recasts and new followers, newest + first, with likes and recasts grouped per cast and an unread count. See + [Notifications and mentions](#notifications-and-mentions). - **Every message checked.** The client checks the hash and signature of every message it reads, and that the signing key belongs to the author, before showing it. A node that alters or makes up messages gets caught; [TRUST.md](TRUST.md) says how far that goes. -The client connects an account with a wallet signature or a test account's recovery phrase, and -casts, replies, likes, recasts and follows. On a society node with a sponsor, someone without an +The client connects an account with a wallet signature, an approval in the Farcaster app, or a +test account's recovery phrase, and casts (with @mentions), replies, likes, recasts and follows. On a society node with a sponsor, someone without an account can create one in the client (see below). The command-line client has the same features: `./outpost cli --help`. @@ -161,10 +178,77 @@ Which accounts can post from Outpost: | You have | Then | |---|---| | No Farcaster account | On a society node with a sponsor: "I have no Farcaster account" makes one in a minute. The society pays the $0.20 fee; you need no ETH, invite code or X account. | -| An account made in the Farcaster app | Read here, post in the app for now (see Limits). | +| An account made in the Farcaster app | "Approve in the Farcaster app": scan a QR code and approve on your phone. No phrase. The key has full access and no expiry (see below). Needs an app account on this Outpost or society node. | | An account whose owner key is in a browser wallet | "My account's owner key is in a browser wallet". | | A test account's recovery phrase | "I have a test account's recovery phrase". | +## Approve in the Farcaster app + +An account made in the Farcaster app keeps its owner key inside the app. To post from Outpost +with such an account, approve Outpost from the app, as with any other connected app: + +1. In the web client, choose "Approve in the Farcaster app" (or run `./outpost cli login --app`). +2. Read the warning, then scan the QR code with your phone, or open the link on it. +3. Approve in the Farcaster app. It shows any onchain fee first; you pay it there unless the + app account sponsors it. +4. Outpost waits until your own node lists the new key, then saves it. + +Your recovery phrase stays in the app. What to know before you approve: + +- The app adds an **onchain key with full access and no expiry**. It can do everything your + account can do apart from moving it, including changing your profile. Outpost's own app key + (the other ways to connect) can only cast, react and follow, and expires. +- **Outpost cannot revoke it.** Only you can, in the Farcaster app: Settings, then Advanced, + then Manage connected apps. "Forget this key" in Outpost only removes it from this browser. +- The request is signed by an **app account**: a Farcaster account that whoever runs this + Outpost set up once. Your posts show "via" its name. + +The app account is set up once, by the owner, in their own terminal: + +``` +./outpost app-account # this computer: ./outpost client and cli login --app use it +./outpost app-account status +./outpost app-account remove +./outpost app-account --society # a society node: members get the choice too +./outpost app-account --society off # stops it and deletes the key from the server +``` + +It asks for that account's recovery phrase or custody key in a hidden prompt, checks on your +node which FID it owns, and stores the custody key mode 600: in `~/.config/outpost/app-account.json`, +or with `--society` on the society node's server only. Use an account made for this, never your +main account: whoever can read that file owns the account. The key signs key requests and +nothing else, and never goes to a browser. `--sponsor` also signs Farcaster's sponsorship, so +people pay no fee; Farcaster honours that only for apps it has approved for it. + +This is the one route where Outpost talks to a host other than your node: the app account's +signature goes to Farcaster's endpoint `api.farcaster.xyz` (no API key), which returns the +approval link. The Outpost server or CLI makes that call, never the web page. Details in +[TRUST.md](TRUST.md#approve-in-the-farcaster-app). + +## Notifications and mentions + +The Notifications section, and `./outpost cli notifications`, list newest first: + +- replies to your last 25 casts, and likes and recasts on them (grouped: "dwr and 3 others + liked your cast"), +- casts that mention you, +- new followers. + +Snapchain has no notifications endpoint, so these are read from your node with +`castsByParent`, `reactionsByCast`, `castsByMention` and `linksByTargetFid`, and every message +passes the same checks as the feed. The unread count in the header covers what is newer than +the last "Mark all read" (stored per account in this browser; `--mark-read` in the CLI). Open +takes you to the cast with its replies. The list refreshes when the page opens, with Refresh, +and every 2 minutes while the page is visible. If the node is slow, what arrived is shown, with a +note about what is still loading. + +To mention someone, type `@` in the composer or a reply: suggestions come from accounts you +follow first, then other profiles loaded on the page, and a full username is looked up on your +node. Pick one (arrow keys and Enter, or a tap) and it is posted as a real mention, which other +Farcaster apps show as a link. Up to 10 per cast. A name you edit after picking it stays plain +text. In the CLI, `./outpost cli cast "gm @dwr"` looks each name up and stops on one it cannot +find; `--no-mentions` posts every `@` as plain text. + ## Society votes Members add or remove members and moderators by vote. Four rules (`src/core/votes.ts`): @@ -245,6 +329,8 @@ Environment variables: - `OUTPOST_TUNNEL_PORT` (default 13381): the local end of the SSH tunnel to the node. Change it if something on your machine already uses that port. - `OUTPOST_PORT` (default 8381): where the web client is served. +- `OUTPOST_NODE_URL=`: like `--node `: `client`, `cli` and `app-account` use that node + with no server or tunnel. It must be https, or http on this computer. To use the client from a phone, `./outpost client --phone` serves it from your server over HTTPS (a certificate for `.sslip.io`, no domain needed) behind a password, and opens 443/tcp. @@ -254,12 +340,13 @@ To use the client from a phone, `./outpost client --phone` serves it from your s - Outpost creates accounts only through a society's sponsor, and buys no extra storage. - The recovery-phrase login is for test accounts. With a real account, use the wallet path. -- An account made in the Farcaster app keeps its custody key inside that app, and as far as we - can tell the app's built-in wallet uses a separate address. So such an account cannot connect to Outpost - without exporting its recovery phrase, which we advise against. The app's own "approve a - connected app" flow would work, but it adds an onchain key with full access and no expiry that - Outpost cannot revoke. Supporting it, clearly labelled, is next. Until then, read with Outpost - and post from the app, or create a second account on a society node. +- An account made in the Farcaster app connects through "Approve in the Farcaster app", which + adds an onchain key with full access and no expiry that Outpost cannot revoke (see + [above](#approve-in-the-farcaster-app)). It needs an app account on the Outpost or society + node you use, and Farcaster's endpoint `api.farcaster.xyz` to answer. The hosted client on + GitHub Pages offers it only through a society node that has an app account. +- "Approve in the Farcaster app" was tested against a stand-in for Farcaster's endpoint and on a + local devnet, not yet with the real Farcaster app on mainnet. - An account made outside the Farcaster app may be ranked low in that app's feeds at first. Its posts always show in Outpost. - App labels come from the key that signed each cast. Keys that people signed for themselves all @@ -273,8 +360,9 @@ To use the client from a phone, `./outpost client --phone` serves it from your s the late ones; someone holding a leaked, removed key and running a dishonest node could serve one, and it would show marked the same way. - Casts you write are text up to 1,024 bytes (the limit for a normal account) with up to 4 - embedded URLs. There is no image upload, and an @name you type stays plain text; it does not - become a mention. There is no notifications view. + embedded URLs and up to 10 mentions. There is no image upload, and no channels view. +- Notifications cover replies and reactions to your last 25 casts only, and are not pushed: the + page refreshes them while it is open. - `./outpost cli selftest` asks the node to validate messages, which a society node's gate does not allow. Run it over your own node's SSH tunnel. - Outpost prints DigitalOcean prices only. On Hetzner it asks for a 1 TB volume, because new @@ -286,6 +374,13 @@ To use the client from a phone, `./outpost client --phone` serves it from your s - A server made by an older Outpost (before 21 October 2026) started at full size, so its disk is too big for the small plan and it cannot shrink. To run small, make a new one. - The node costs money for as long as it exists, whether or not you use the client. +- With `--public` (or `--node`) you use a node that is not yours. It sees what you read and + post, can withhold or delay messages, and may refuse posts: writing through Farcaster's public + node was not tested before release. It cannot post as you or change a message, since every + message is still checked on your computer. Gasless keys are checked against a second public + node, `crackle.farcaster.xyz`, which Farcaster also runs: it catches one node being wrong, not + Farcaster itself. Optimism checks are unchanged. The page reaches these only through + `./outpost client` on 127.0.0.1. ## Shard 0 did not load @@ -306,6 +401,11 @@ bash test/watchdog.sh # the watchdog, with a one-second hour (not in bash test/snapshot-restore.sh # Linux only: the snapshot loader against a local fake bucket ``` +Two more need no account: `node --import tsx test/phase1-demo.ts` (after `npm run build`) runs +the web client on a fake node to try mentions, notifications and "Approve in the Farcaster app" +with a stand-in for Farcaster's endpoint, and `npx tsx scripts/e2e-phase1.ts` checks the same +against a local devnet (start and stop commands at the top of `scripts/e2e-devnet.ts`). + CI runs all of these except the watchdog test, plus shellcheck, on pull requests and pushes to `main`. Tests against a live node are in [CONTRIBUTING.md](CONTRIBUTING.md). Outputs from real runs on mainnet and a local devnet are saved in [docs/evidence/](docs/evidence/README.md). @@ -326,6 +426,8 @@ src/web/ web client src/server/serve.ts local server for the web client, forwards /v1 to the node src/server/gate.ts society mode: member tokens, rate limits, read and post only src/server/sponsor* society mode: pays for members' new accounts (registerFor), with caps +src/server/appsignin* "Approve in the Farcaster app": the app account signs key requests, on + ./outpost client's server or, for members, on a society node scripts/ build, devnet end-to-end test, live checks, example charters, society node check test/ unit tests and shell tests, with fake provider CLIs diff --git a/SECURITY.md b/SECURITY.md index e77f570..c0a674e 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -30,6 +30,21 @@ until it is revoked or expires. - The web client stores it in the browser's localStorage under `outpost.appkey`. - `./outpost uninstall`, or the web client's "Your app key" panel, revokes it with the key itself. +**Keys approved in the Farcaster app.** "Approve in the Farcaster app" (`./outpost cli login +--app`, or that choice in the web client) adds an onchain key with full access and no expiry. +Outpost stores it in the same places as an app key, marked `"onchain": true`. Outpost cannot +revoke it; `uninstall` and "Forget this key" only delete it locally. The account's owner +removes it in the Farcaster app: Settings, then Advanced, then Manage connected apps. + +**App account.** `./outpost app-account` stores a Farcaster account's custody key, so that +account can sign key requests: in `~/.config/outpost/app-account.json` (mode 600), or with +`--society` only on the society node's server. Whoever reads that file owns that account. Use +an account made for this, never a main account. The prompt hides input and needs a terminal. +The key is used only to sign key requests and is never sent to a browser. It leaves the +computer only with `--society`, over SSH to that server, and the copy made locally on the way is +deleted. Only the signature goes to Farcaster's endpoint `api.farcaster.xyz`. The local server accepts +sign-in requests only from its own page. + **Node API ports.** Snapchain's HTTP (3381) and gRPC (3383) ports accept writes from anyone who can reach them. `infra/node-setup.sh` binds both to 127.0.0.1 and checks the result after every run; it stops with an error if either is published anywhere else. The client reaches the node diff --git a/TRUST.md b/TRUST.md index 68f3a58..70cc909 100644 --- a/TRUST.md +++ b/TRUST.md @@ -118,13 +118,34 @@ node. ## What picture hosts learn -With "Show pictures" on (the default), the web client loads profile pictures and image embeds -straight from the URLs in signed profiles and casts. Each host learns the reader's IP address and +With "Show pictures" on (the default, except with `--public` or `--node`), the web client loads +profile pictures and image embeds straight from the URLs in signed profiles and casts. Each host learns the reader's IP address and which of its pictures were shown, so roughly which accounts were on screen. The signature covers the URL, not the image, so a host can swap the image without changing the signed profile or cast. A picture cannot change a message or post anything. Only https URLs are loaded, and no referrer is sent. Untick "Show pictures" above the feed to stop these requests. +## Using Farcaster's public node + +`./outpost client --public` (and `cli`, `app-account`) uses Farcaster's public node +`https://snap.farcaster.xyz:3381` instead of your own; `--node ` uses any other https node. +That node is in the host's place above: it cannot post as you or change a message, because the +client checks every message the same way (`src/core/verify.ts`), and it can do everything listed +under "What the host can still do". Farcaster sees what you read and post, from your IP address. + +- **The second node** for gasless keys is `https://crackle.farcaster.xyz:3381`, another public + node on a separate machine (`checkNodeFor` in `src/core/secondnode.ts`). Farcaster runs both, + so a fake gasless key would need only Farcaster to lie. It catches one broken or tampered node, + not Farcaster itself. The page says so under "What does that change?". Choose another second + node in "Node and checks", or `--check-node` on the command line. +- **Onchain keys** are still checked on Optimism, as before. +- **Your browser** still talks only to `./outpost client` on 127.0.0.1. It forwards `/v1/*` to + the node, `/outpost/check/v1/*` (GET only) to the second node and `/outpost/optimism` (POST + only, read calls such as `eth_call`) to the Optimism RPC (`src/server/serve.ts`, tests in `test/remote-node.test.ts`). +- **No server** is involved: no SSH, no provider (`test/providers.sh` checks with fakes). +- **Posting** goes to the public node like any other submit. It may refuse it; that was not + tested before release. + ## What a society vote proves A passed amendment is kept in the charter log with the signed proposal and the signed likes that @@ -187,6 +208,44 @@ The optional sponsor (`src/server/sponsor.ts` and `sponsor-service.ts`, tests in - The wallet's key lives on the server. Whoever controls the server controls the ETH in it, and nothing else. +## Approve in the Farcaster app + +For accounts made in the Farcaster app (`src/core/appsignin.ts`, `src/server/appsignin-service.ts`, +tests in `test/appsignin.test.ts`, `scripts/e2e-phase1.ts` on a devnet): + +- **The one extra host.** The approval link comes from Farcaster's endpoint + `https://api.farcaster.xyz` (`POST /v2/signed-key-requests`, then + `GET /v2/signed-key-request?token=`), with no API key. Only the Outpost server + (`./outpost client`), the society node's sign-in service, or the CLI (`login --app`) calls it. + The web page calls only its own origin, or the society node. Farcaster learns the new public + key, the app account's FID and, once you approve, your FID. +- **What gets added.** An onchain key with full access and no expiry, added by the Farcaster app + when you approve. Outpost cannot revoke it: only the account's owner can, in the Farcaster app + (Settings, then Advanced, then Manage connected apps). The page says this before it shows the + QR code, and the key panel labels such a key. +- **The new key's private half** is made in the browser (or the CLI) and stays there. Only the + public half goes to the server. +- **The app account's custody key** is entered by its owner in a hidden prompt + (`./outpost app-account`) and stored mode 600: on the owner's computer, or with `--society` + only on the society node's server (`$DIR/appsignin/account.json`; `--society off` deletes it). + It is never sent to a browser, and no response or log carries it or the signature made with + it (checked in `test/appsignin.test.ts`). Whoever can read that file owns the app account, so + it should be an account made for this. +- **Who may ask.** `./outpost client` takes a request only from its own page (the browser's + `Origin`), so another website cannot make it sign. A society node takes requests only with a + member's token, from the published client origins. Only request tokens the server handed out + can be polled. +- **Before the key is used**, the client waits until the node lists it as an onchain key + requested by that app account (`/v1/signer`), and saves it only then. + +What a society host can still do here: the host's sign-in service could ask Farcaster to add a +key the host made instead of the one your browser made. The request token is opaque to the page, +so it cannot see which key you are approving; the Farcaster app shows the app asking. Your +browser then never sees its own key listed and says so after 5 minutes, and the host's key would +be on your account with full access until you remove it in the Farcaster app. Use this route only +on a society node whose host you trust, or with your own `./outpost client`, where the server is +your own computer. + ## Running a society node ``` diff --git a/infra/society.sh b/infra/society.sh index b30674f..d3963da 100755 --- a/infra/society.sh +++ b/infra/society.sh @@ -13,6 +13,12 @@ # and OUTPOST_SOCIETY_HOST; prints the wallet to fund # society.sh sponsor-off stops it; keeps the wallet key and the counts # society.sh sponsor-status +# society.sh app-signin-account the app account's JSON on stdin (fid, custodyKey, address); +# stored mode 600 in $DIR/appsignin +# society.sh app-signin-on the sign-in bundle (dist/appsignin.js) on stdin; needs society mode, +# the app account and OUTPOST_SOCIETY_HOST +# society.sh app-signin-off stops it and deletes the app account's key from this server +# society.sh app-signin-status # # Caddy (official image, pinned) gets a TLS certificate for .sslip.io and passes every request # to the gate, which checks the token and lets only GET /v1/* and POST /v1/submitMessage through @@ -24,6 +30,10 @@ # The sponsor (optional, src/server/sponsor.ts) pays for members' new Farcaster accounts from a # hot wallet whose key is made here and stays in $DIR/sponsor/key. Caddy sends /sponsor/* to it # and everything else to the gate. Keep only a little ETH in that wallet. +# +# "Approve in the Farcaster app" (optional, src/server/appsignin.ts) signs members' key requests +# with an app account whose custody key the host sent here ($DIR/appsignin/account.json, mode +# 600). Caddy sends /app-signin and /app-signin/* to it, members only. set -euo pipefail @@ -31,9 +41,11 @@ CADDY_IMAGE="caddy:2.10.2" NODE_IMAGE="node:24.21.0-alpine" GATE_PORT=3390 SPONSOR_PORT=3391 +APPSIGNIN_PORT=3392 DIR="${OUTPOST_SOCIETY_DIR:-/srv/outpost-society}" MEMBERS="$DIR/gate/members" SPONSOR="$DIR/sponsor" +APPSIGNIN="$DIR/appsignin" say() { printf '[society] %s\n' "$*"; } die() { printf '[society] error: %s\n' "$*" >&2; exit 1; } @@ -57,15 +69,25 @@ write_members() { # headers or client IP are dropped: http.log (incl. the error log when the gate is down), # http.handlers (reverse_proxy), http.stdlib (TLS handshake errors) and tls.handshake # (client addresses, at debug level). Start-up and certificate messages stay. -# /sponsor/* goes to the sponsor only while it is on ($SPONSOR/on exists). +# /sponsor/* goes to the sponsor only while it is on ($SPONSOR/on exists), /app-signin* to the +# sign-in service only while it is on ($APPSIGNIN/on). write_caddyfile() { : "${OUTPOST_SOCIETY_HOST:?missing OUTPOST_SOCIETY_HOST}" - local route=" reverse_proxy 127.0.0.1:${GATE_PORT}" + local route=" reverse_proxy 127.0.0.1:${GATE_PORT}" extra="" if [ -f "$SPONSOR/on" ]; then - route=" handle /sponsor/* { + extra=" handle /sponsor/* { reverse_proxy 127.0.0.1:${SPONSOR_PORT} } - handle { +" + fi + if [ -f "$APPSIGNIN/on" ]; then + extra="${extra} handle /app-signin* { + reverse_proxy 127.0.0.1:${APPSIGNIN_PORT} + } +" + fi + if [ -n "$extra" ]; then + route="${extra} handle { reverse_proxy 127.0.0.1:${GATE_PORT} }" fi @@ -125,9 +147,9 @@ case "$action" in say "serving the node API at https://${OUTPOST_SOCIETY_HOST} to members only (no web page)" ;; off) - docker rm -f outpost-society outpost-gate outpost-sponsor >/dev/null 2>&1 || true - rm -f "$SPONSOR/on" - say "stopped. Members, the sponsor's wallet key and the certificate are kept in $DIR" + docker rm -f outpost-society outpost-gate outpost-sponsor outpost-appsignin >/dev/null 2>&1 || true + rm -f "$SPONSOR/on" "$APPSIGNIN/on" + say "stopped. Members, the sponsor's wallet key, the app account and the certificate are kept in $DIR" ;; sponsor-on) exists outpost-society || die "society mode is off. Turn it on first: ./outpost society on" @@ -189,8 +211,69 @@ case "$action" in say "outpost-sponsor: not running" fi ;; + app-signin-account) + mkdir -p "$APPSIGNIN" + chmod 700 "$APPSIGNIN" + (umask 077; cat >"$APPSIGNIN/account.json.tmp") + if ! grep -Eq '"custodyKey": *"0x[0-9a-fA-F]{64}"' "$APPSIGNIN/account.json.tmp" || ! grep -Eq '"fid": *[0-9]+' "$APPSIGNIN/account.json.tmp"; then + rm -f "$APPSIGNIN/account.json.tmp" + die "expected the app account's JSON on stdin" + fi + mv "$APPSIGNIN/account.json.tmp" "$APPSIGNIN/account.json" + chmod 600 "$APPSIGNIN/account.json" + say "app account stored in $APPSIGNIN/account.json (mode 600)" + ;; + app-signin-on) + exists outpost-society || die "society mode is off. Turn it on first: ./outpost society on" + [ -s "$APPSIGNIN/account.json" ] || die "no app account on this server. Run ./outpost app-account --society" + cat >"$APPSIGNIN/appsignin.js.tmp" + [ -s "$APPSIGNIN/appsignin.js.tmp" ] || die "no sign-in bundle on stdin" + mv "$APPSIGNIN/appsignin.js.tmp" "$APPSIGNIN/appsignin.js" + appsignin_failed() { + docker logs outpost-appsignin 2>&1 | tail -5 >&2 || true + docker rm -f outpost-appsignin >/dev/null 2>&1 || true + rm -f "$APPSIGNIN/on" + write_caddyfile + reload_caddy + die "$1" + } + : >"$APPSIGNIN/on" + docker rm -f outpost-appsignin >/dev/null 2>&1 || true + # Host networking: it reaches Farcaster's endpoint and listens on 127.0.0.1 only. It reads + # the app account and the gate's members file, and writes nothing. + docker run -d --name outpost-appsignin --restart unless-stopped --network host \ + --read-only --cap-drop ALL --security-opt no-new-privileges \ + -e OUTPOST_APPSIGNIN_PORT="$APPSIGNIN_PORT" -e OUTPOST_APPSIGNIN_MEMBERS=/gate/members \ + -e OUTPOST_APPSIGNIN_ACCOUNT=/appsignin/account.json \ + -v "$APPSIGNIN/appsignin.js:/appsignin/appsignin.js:ro" -v "$APPSIGNIN/account.json:/appsignin/account.json:ro" \ + -v "$DIR/gate:/gate:ro" \ + "$NODE_IMAGE" node /appsignin/appsignin.js >/dev/null || appsignin_failed "could not start the sign-in container" + fid="" + for _ in $(seq 1 20); do + fid="$(docker logs outpost-appsignin 2>&1 | grep -Eo 'app account FID [0-9]+' | head -1 | awk '{ print $4 }')" && [ -n "$fid" ] && break + sleep 1 + done + [ -n "$fid" ] || appsignin_failed "the sign-in service did not start" + write_caddyfile + reload_caddy + say "approve in the Farcaster app: on for members, requests come from FID $fid" + ;; + app-signin-off) + docker rm -f outpost-appsignin >/dev/null 2>&1 || true + rm -f "$APPSIGNIN/on" "$APPSIGNIN/account.json" "$APPSIGNIN/account.json.tmp" + if exists outpost-society; then write_caddyfile; reload_caddy; fi + say "approve in the Farcaster app: off. The app account's key was deleted from this server" + ;; + app-signin-status) + if exists outpost-appsignin; then + say "outpost-appsignin: $(docker inspect -f '{{.State.Status}}' outpost-appsignin)" + docker logs outpost-appsignin 2>&1 | grep -E 'app-signin: ' | tail -1 + else + say "outpost-appsignin: not running" + fi + ;; status) - for c in outpost-society outpost-gate outpost-sponsor; do + for c in outpost-society outpost-gate outpost-sponsor outpost-appsignin; do if exists "$c"; then say "$c: $(docker inspect -f '{{.State.Status}}' "$c")"; else say "$c: not running"; fi done say "members: $(grep -c . "$MEMBERS" || true)" diff --git a/outpost b/outpost index 77cc4dc..e113bbd 100755 --- a/outpost +++ b/outpost @@ -7,7 +7,10 @@ # # Also: ./outpost status, ./outpost shrink (once synced: an 8 GB server instead of 32 GB, for a # quarter of its price), ./outpost cli , ./outpost uninstall, ./outpost destroy, -# ./outpost watchdog start|stop|status (deletes a trial server for you) +# ./outpost watchdog start|stop|status (deletes a trial server for you), +# ./outpost app-account [--society] [status|remove] (signs "Approve in the Farcaster app") +# No server: ./outpost client --public (and cli, app-account, uninstall) use Farcaster's public node instead +# of your own; --node (or OUTPOST_NODE_URL=) uses any other node. # Society node, used by members from their own browsers: ./outpost society on|off|status, # ./outpost members add , ./outpost members remove , ./outpost members list, # ./outpost sponsor on|off|status (you pay for members' new Farcaster accounts, $0.20 each) @@ -29,6 +32,10 @@ CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" DRY_RUN=0 ASSUME_YES=0 +# Set by --public or --node (or OUTPOST_NODE_URL): client, cli and app-account then use +# this node directly, with no server, SSH tunnel or provider. +PUBLIC_NODE="https://snap.farcaster.xyz:3381" +NODE_URL="" say() { printf '%s\n' "$*"; } warn() { printf 'warning: %s\n' "$*" >&2; } @@ -655,12 +662,45 @@ cmd_phone() { say "turn it off with: ./outpost client --phone off" } +# A node URL given with --node or OUTPOST_NODE_URL: https, or http on this machine, so what you +# post never crosses a network in the clear. +check_node_url() { + [[ "$1" =~ ^https://[^/[:space:]@]+(/[^[:space:]?#]*)?$ ]] && return 0 + [[ "$1" =~ ^http://(127\.0\.0\.1|localhost)(:[0-9]+)?/?$ ]] && return 0 + die "not a node URL Outpost can use: $1. It must be https://[:port], or http://127.0.0.1: for a node on this computer." +} + +# True when NODE_URL is a node on this computer (yours), not someone else's. +node_url_local() { [[ "$NODE_URL" =~ ^http://(127\.0\.0\.1|localhost)(:|/|$) ]]; } + +# ./outpost client --public / --node : no server, no tunnel. The page says the node is not +# yours, and its key checks go through serve.js like everything else. +client_without_server() { + ensure_build + local remote=1 + if node_url_local; then + remote=0 + say "using the node on this computer at $NODE_URL" + elif [ "$NODE_URL" = "$PUBLIC_NODE" ]; then + say "using Farcaster's public node $NODE_URL, not your own: it sees what you read and post, and may refuse posts" + else + say "using the node at $NODE_URL, not your own: its host sees what you read and post" + fi + say "open http://127.0.0.1:$CLIENT_PORT in your browser. Ctrl-C stops the client." + OUTPOST_NODE_URL="$NODE_URL" OUTPOST_NODE_REMOTE="$remote" OUTPOST_PORT="$CLIENT_PORT" node "$OUTPOST_DIR/dist/serve.js" +} + cmd_client() { if [ "${1:-}" = "--phone" ]; then + [ -z "$NODE_URL" ] || die "--phone serves the client from your own server; it does not work with --public or --node" shift cmd_phone "$@" return fi + if [ -n "$NODE_URL" ]; then + client_without_server + return + fi ensure_build tunnel_up # The tunnel dies with Wi-Fi changes and sleep, while the client keeps serving 502s (a tester's @@ -683,10 +723,69 @@ cmd_cli() { ensure_build # Help needs no server. case "${1:-}" in ""|-h|--help|help) node "$OUTPOST_DIR/dist/cli.js" --help; return ;; esac + if [ -n "$NODE_URL" ]; then + OUTPOST_NODE_URL="$NODE_URL" node "$OUTPOST_DIR/dist/cli.js" "$@" + return + fi tunnel_up OUTPOST_NODE_URL="http://127.0.0.1:$TUNNEL_PORT" node "$OUTPOST_DIR/dist/cli.js" "$@" } +# "Approve in the Farcaster app": the app account's custody key is stored on this computer +# (mode 600) and used by ./outpost client and ./outpost cli login --app. Setting it up looks the +# account up on your node; status and remove need no server. +# With --society the key goes to the society node's server instead (mode 600 there; the copy +# made here on the way is deleted) and members get "Approve in the Farcaster app". +cmd_app_account() { + ensure_build + if [ "${1:-}" = "--society" ]; then + [ -z "$NODE_URL" ] || die "--society sets up the society node's own server; it does not work with --public or --node" + shift + app_account_society "$@" + return + fi + case "${1:-}" in + status|remove) node "$OUTPOST_DIR/dist/cli.js" app-account "$@" ;; + *) + if [ -n "$NODE_URL" ]; then + OUTPOST_NODE_URL="$NODE_URL" node "$OUTPOST_DIR/dist/cli.js" app-account "$@" + return + fi + tunnel_up + OUTPOST_NODE_URL="http://127.0.0.1:$TUNNEL_PORT" node "$OUTPOST_DIR/dist/cli.js" app-account "$@" + ;; + esac +} + +app_account_society() { + local host tmp + case "${1:-}" in + status) society_run "" app-signin-status; return ;; + remove|off) + host="$(society_host)" + society_run "OUTPOST_SOCIETY_HOST='$host' " app-signin-off + return + ;; + esac + host="$(society_host)" + [ -f "$OUTPOST_DIR/dist/appsignin.js" ] || (cd "$OUTPOST_DIR" && node scripts/build.mjs) + tunnel_up + mkdir -p "$STATE_DIR" + tmp="$(umask 077; mktemp "$STATE_DIR/app-account.XXXXXX")" + # shellcheck disable=SC2064 # expand $tmp now: it is gone by the time the trap runs + trap "rm -f '$tmp'" EXIT + OUTPOST_NODE_URL="http://127.0.0.1:$TUNNEL_PORT" node "$OUTPOST_DIR/dist/cli.js" app-account --out "$tmp" "$@" + if [ ! -s "$tmp" ]; then + rm -f "$tmp" + say "nothing was sent to the server" + return + fi + society_run "" app-signin-account <"$tmp" + rm -f "$tmp" + society_run "OUTPOST_SOCIETY_HOST='$host' " app-signin-on <"$OUTPOST_DIR/dist/appsignin.js" + say "members now see \"Approve in the Farcaster app\". Turn it off (and delete the key there) with: ./outpost app-account --society off" +} + # ---- society # Society mode: members use your node from their own browsers, with their own Farcaster @@ -1172,12 +1271,30 @@ cmd_watchdog() { } usage() { - sed -n '2,15p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' + sed -n '2,18p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' } main() { local cmd="${1:-}" [ $# -gt 0 ] && shift + case "$cmd" in + client|cli|app-account|uninstall) + while [ $# -gt 0 ]; do + case "$1" in + --public) NODE_URL="$PUBLIC_NODE"; shift ;; + --node) [ $# -ge 2 ] || die "--node needs a URL, for example --node https://node.example:3381"; NODE_URL="$2"; shift 2 ;; + --node=*) NODE_URL="${1#--node=}"; shift ;; + *) break ;; + esac + done + for a in "$@"; do + case "$a" in --public) die "put $a right after ./outpost $cmd, for example: ./outpost $cmd $a ..." ;; esac + done + [ -n "$NODE_URL" ] || NODE_URL="${OUTPOST_NODE_URL:-}" + [ -z "$NODE_URL" ] || check_node_url "$NODE_URL" + NODE_URL="${NODE_URL%/}" + ;; + esac case "$cmd" in provision) cmd_provision "$@" ;; node) cmd_node "$@" ;; @@ -1186,6 +1303,7 @@ main() { members) cmd_members "$@" ;; sponsor) cmd_sponsor "$@" ;; cli) cmd_cli "$@" ;; + app-account) cmd_app_account "$@" ;; status) cmd_status "$@" ;; shrink) cmd_shrink "$@" ;; uninstall) cmd_uninstall "$@" ;; diff --git a/package-lock.json b/package-lock.json index e87921f..3418790 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@farcaster/core": "0.20.0", "@noble/curves": "1.9.7", "@noble/hashes": "1.8.0", + "uqr": "0.1.3", "viem": "2.57.1" }, "devDependencies": { @@ -1084,6 +1085,12 @@ "dev": true, "license": "MIT" }, + "node_modules/uqr": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/uqr/-/uqr-0.1.3.tgz", + "integrity": "sha512-0rjE8iEJe4YmT9TOhwsZtqCMRLc5DXZUI2UEYUUg63ikBkqqE5EYWaI0etFe/5KUcmcYwLih2RND1kq+hrUJXA==", + "license": "MIT" + }, "node_modules/viem": { "version": "2.57.1", "resolved": "https://registry.npmjs.org/viem/-/viem-2.57.1.tgz", diff --git a/package.json b/package.json index 81c6b17..f9c9fc5 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "@farcaster/core": "0.20.0", "@noble/curves": "1.9.7", "@noble/hashes": "1.8.0", + "uqr": "0.1.3", "viem": "2.57.1" }, "devDependencies": { diff --git a/scripts/build.mjs b/scripts/build.mjs index c4378cb..c483b33 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -60,6 +60,17 @@ await build({ outfile: "dist/sponsor.js", logLevel: "warning", }); +// "Approve in the Farcaster app" on a society node, one file like the sponsor. +await build({ + entryPoints: ["src/server/appsignin.ts"], + bundle: true, + platform: "node", + format: "esm", + target: ["node20"], + outfile: "dist/appsignin.js", + alias: { "@faker-js/faker": "./src/web/faker-stub.ts" }, + logLevel: "warning", +}); await copyFile("src/web/index.html", "dist/web/index.html"); await copyFile("src/web/style.css", "dist/web/style.css"); -console.log("built dist/web, dist/serve.js, dist/cli.js and dist/sponsor.js"); +console.log("built dist/web, dist/serve.js, dist/cli.js, dist/sponsor.js and dist/appsignin.js"); diff --git a/scripts/devnet-lib.ts b/scripts/devnet-lib.ts new file mode 100644 index 0000000..db575a3 --- /dev/null +++ b/scripts/devnet-lib.ts @@ -0,0 +1,115 @@ +// Helpers for scripts that run against the local Snapchain devnet (scripts/devnet-compose.yml). +// Accounts, keys and usernames are made through the devnet's admin RPC (synthetic onchain events +// and fname proofs, which only a devnet accepts). Never point these at mainnet. + +import { randomBytes } from "node:crypto"; +import { + FarcasterNetwork, + IdRegisterEventType, + makeUserDataAdd, + OnChainEvent, + OnChainEventType, + SignerEventType, + UserDataType, + UserNameProof, + UserNameType, + encodeGaslessSignedKeyRequestMetadata, +} from "@farcaster/core"; +import { getAdminRpcClient, getAuthMetadata } from "@farcaster/hub-nodejs"; +import { bytesToHex, hexToBytes } from "viem"; +import { farcasterNow, generatePrivateKey, Hub, publicKeyOf, signerOf, unwrap, type AppKey, type HexString } from "../src/core/index.js"; + +export const HTTP = process.env.OUTPOST_E2E_HTTP ?? "http://127.0.0.1:23381"; +export const GRPC = process.env.OUTPOST_E2E_GRPC ?? "127.0.0.1:23383"; +export const network = FarcasterNetwork.DEVNET; +export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); + +export async function admin(fn: (c: Awaited>, md: ReturnType) => Promise): Promise { + const c = await getAdminRpcClient(GRPC); + try { + return await fn(c, getAuthMetadata("dev", "dev")); + } finally { + c.close(); + } +} + +export async function submitEvents(events: OnChainEvent[]) { + await admin(async (c, md) => { + for (const e of events) { + const r = await c.submitOnChainEvent(e, md); + if (r.isErr()) throw new Error(`admin submit failed: ${r.error.message}`); + } + }); +} + +// Mirrors snapchain's cli/src/factory.rs: timestamp 10 s in the past, block number derived from it. +export function baseEvent(type: OnChainEventType, fid: number): OnChainEvent { + const ts = Math.floor(Date.now() / 1000) - 10; + return OnChainEvent.create({ + type, + chainId: 10, + blockNumber: ts * 1024 + Math.floor(Math.random() * 1000), + blockHash: new Uint8Array(), + blockTimestamp: ts, + transactionHash: randomBytes(32), + logIndex: 0, + fid, + txIndex: 0, + version: 1, + }); +} + +export async function registerFid(fid: number, custody: HexString) { + const rent = baseEvent(OnChainEventType.EVENT_TYPE_STORAGE_RENT, fid); + rent.storageRentEventBody = { expiry: 0, units: 1, payer: randomBytes(32) }; + const reg = baseEvent(OnChainEventType.EVENT_TYPE_ID_REGISTER, fid); + reg.idRegisterEventBody = { to: hexToBytes(custody), eventType: IdRegisterEventType.REGISTER, from: new Uint8Array(), recoveryAddress: new Uint8Array() }; + await submitEvents([rent, reg]); +} + +// An onchain key (full access, no expiry) whose SignedKeyRequest metadata names `requestFid`, as +// a KeyGateway add from the Farcaster app's approval would. +export async function addOnchainSigner(fid: number, key: Uint8Array, requestFid: number) { + const ev = baseEvent(OnChainEventType.EVENT_TYPE_SIGNER, fid); + const metadata = encodeGaslessSignedKeyRequestMetadata({ + requestFid: BigInt(requestFid), + requestSigner: "0x00000000000000000000000000000000000000aa", + signature: new Uint8Array(65), + deadline: 1n, + })._unsafeUnwrap(); + ev.signerEventBody = { key, keyType: 1, eventType: SignerEventType.ADD, metadata, metadataType: 1 }; + await submitEvents([ev]); +} + +export async function until(what: string, probe: () => Promise, ms = 20_000): Promise { + const end = Date.now() + ms; + for (;;) { + const v = await probe().catch(() => undefined); + if (v !== undefined && v !== null && (v as unknown) !== false) return v as T; + if (Date.now() > end) throw new Error(`timed out waiting for ${what}`); + await sleep(500); + } +} + +// A full-access onchain key for `fid`, requested by the account itself, as an AppKey. +export async function onchainKey(hub: Hub, fid: number, requestFid = fid): Promise { + const priv = generatePrivateKey(); + const pub = await publicKeyOf(priv); + await addOnchainSigner(fid, pub, requestFid); + await until(`onchain key for ${fid}`, () => hub.signer(fid, bytesToHex(pub))); + return { fid, privateKey: bytesToHex(priv), publicKey: bytesToHex(pub), requestFid, scopes: [], ttl: 0, network }; +} + +// An fname for `fid` (devnet skips the fname server's signature), then the USERNAME user data, +// signed with a key of the account that may sign user data. +export async function setUsername(hub: Hub, fid: number, owner: HexString, key: AppKey, name: string) { + const proof = UserNameProof.create({ timestamp: Math.floor(Date.now() / 1000), name: new TextEncoder().encode(name), owner: hexToBytes(owner), signature: new Uint8Array(65), fid, type: UserNameType.USERNAME_TYPE_FNAME }); + await admin(async (c, md) => { + const r = await c.submitUserNameProof(proof, md); + if (r.isErr()) throw new Error(`fname proof failed: ${r.error.message}`); + }); + await until(`fname ${name}`, () => hub.fidByUsername(name)); + const m = unwrap(await makeUserDataAdd({ type: UserDataType.USERNAME, value: name }, { fid, network, timestamp: farcasterNow() }, signerOf(key))); + await until(`username ${name} accepted`, () => hub.submit(m).then(() => true)); + await until(`username ${name} on the node`, async () => (await hub.userDataByFid(fid)).items.some((u) => u.data.userDataBody?.value === name) || undefined); +} diff --git a/scripts/e2e-phase1.ts b/scripts/e2e-phase1.ts new file mode 100644 index 0000000..0a821d5 --- /dev/null +++ b/scripts/e2e-phase1.ts @@ -0,0 +1,169 @@ +// End-to-end test of mentions and notifications against a local Snapchain devnet, over HTTP. +// Throwaway FIDs, keys and fnames, made through the devnet's admin RPC. +// +// docker compose -p kitdevnet -f scripts/devnet-compose.yml up -d +// npx tsx scripts/e2e-phase1.ts [--save ] +// docker compose -p kitdevnet -f scripts/devnet-compose.yml down +// +// --save writes the accounts' app keys there (mode 600) for trying the web client on the +// devnet; they are devnet keys and worth nothing. + +import { execFile } from "node:child_process"; +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { promisify } from "node:util"; +import { ViemLocalEip712Signer } from "@farcaster/core"; +import type { HexString } from "../src/core/index.js"; +import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; +import { readFileSync, statSync } from "node:fs"; +import { hexToBytes } from "viem"; +import { addAppKey, cast, FeedBuilder, requestFidOf, follow, Hub, notificationsFor, notificationText, react, resolveMentions, type AppKey } from "../src/core/index.js"; +import { addOnchainSigner, HTTP, network, onchainKey, registerFid, setUsername, until } from "./devnet-lib.js"; +import { fakeFarcaster } from "../test/fake-farcaster.js"; + +const hub = new Hub({ url: HTTP }); +let step = 0; +const log = (s: string) => console.log(`${String(++step).padStart(2)}. ${s}`); +function check(cond: unknown, what: string): asserts cond { + if (!cond) throw new Error(`FAILED: ${what}`); +} +const wait = { timeoutMs: 20_000, intervalMs: 500 }; + +async function account(fid: number, name: string): Promise<{ key: AppKey; name: string; custody: HexString }> { + const custodyKey = generatePrivateKey(); + const custody = privateKeyToAccount(custodyKey); + await registerFid(fid, custody.address); + await until(`FID ${fid}`, async () => ((await hub.storageLimits(fid)).units > 0 ? true : undefined)); + const full = await onchainKey(hub, fid); + await setUsername(hub, fid, custody.address, full, name); + const { key, confirmed } = await addAppKey(hub, { fid, custody: new ViemLocalEip712Signer(custody), network }, wait); + check(confirmed, `app key for ${fid}`); + return { key, name, custody: custodyKey }; +} + +async function main() { + const t0 = Date.now(); + const base = 300000 + Math.floor(Math.random() * 600000); + const tag = base.toString(36); + const save = process.argv.indexOf("--save") > 0 ? process.argv[process.argv.indexOf("--save") + 1] : undefined; + const [me, friend, other, app] = await Promise.all([ + account(base, `me-${tag}`), + account(base + 1, `friend-${tag}`), + account(base + 2, `other-${tag}`), + account(base + 3, `app-${tag}`), + ]); + log(`devnet ${HTTP}: @${me.name} (${me.key.fid}), @${friend.name} (${friend.key.fid}), @${other.name} (${other.key.fid}), each with a username and an app key`); + + // Mentions: the CLI path (names looked up on the node, checked against the signed profile). + const m = await resolveMentions(hub, `πŸŽ‰ gm @${friend.name} and @${other.name}!`); + check("mentions" in m, "names resolved to mentions"); + const enc = new TextEncoder(); + check(m.mentions[0] === friend.key.fid && m.mentionsPositions[0] === enc.encode("πŸŽ‰ gm ").length, "first mention FID and byte offset after the emoji"); + check(m.mentions[1] === other.key.fid && m.mentionsPositions[1] === enc.encode("πŸŽ‰ gm and ").length, "second mention"); + const mc = await cast(hub, me.key, m, wait); + check(mc.confirmed, "mention cast merged"); + const stored = await hub.castById(me.key.fid, mc.hash); + const b = stored?.data.castAddBody; + check(b && JSON.stringify(b.mentions) === JSON.stringify(m.mentions) && JSON.stringify(b.mentionsPositions) === JSON.stringify(m.mentionsPositions), "node stores the mentions and positions as sent"); + const feed = new FeedBuilder(hub); + const mine = await feed.fromAuthors([me.key.fid], { limit: 5 }); + check(mine[0]?.text === `πŸŽ‰ gm @${friend.name} and @${other.name}!`, `feed renders the names (got "${mine[0]?.text}")`); + log(`mention cast ${mc.hash.slice(0, 10)}: node has mentions [${b.mentions}] at bytes [${b.mentionsPositions}]; feed shows "${mine[0]!.text}"`); + const unknown = await resolveMentions(hub, "hi @nobody-here-xyz").then(() => "accepted", (e: Error) => e.message); + check(/unknown username @nobody-here-xyz/.test(unknown), "unknown name refused"); + log(`unknown @name refused: "${unknown.slice(0, 60)}…"`); + + // Notifications: friend replies, mentions, likes, recasts and follows; other also likes. + const target = await cast(hub, me.key, { text: "a cast to get notifications on" }, wait); + const t = { fid: me.key.fid, hash: target.hash }; + const reply = await cast(hub, friend.key, { text: "a reply from friend", replyTo: t }, wait); + const mention = await cast(hub, friend.key, await resolveMentions(hub, `hey @${me.name}, look`), wait); + const results = await Promise.all([ + react(hub, friend.key, "like", t, wait), + react(hub, other.key, "like", t, wait), + react(hub, friend.key, "recast", t, wait), + follow(hub, friend.key, me.key.fid, wait), + ]); + check(reply.confirmed && mention.confirmed && results.every((r) => r.confirmed), "friend's and other's messages merged"); + log("friend replied, mentioned, liked, recast and followed; other liked"); + + // Q4: the two new reads on Snapchain 0.15.0. + const byMention = await hub.castsByMention(me.key.fid); + const followers = await hub.linksByTarget(me.key.fid); + check(byMention.items.some((x) => x.hash === mention.hash), "/v1/castsByMention returns the mention"); + check(followers.items.some((x) => x.data.fid === friend.key.fid), "/v1/linksByTargetFid returns the follower"); + log(`node API: /v1/castsByMention -> ${byMention.items.length} cast(s), /v1/linksByTargetFid -> ${followers.items.length} follower(s)`); + + const r = await notificationsFor(new FeedBuilder(hub), me.key.fid); + const lines = r.items.map(notificationText); + for (const l of lines) console.log(` ${l}`); + const kinds = r.items.map((n) => n.kind); + check(kinds.includes("reply") && kinds.includes("mention") && kinds.includes("like") && kinds.includes("recast") && kinds.includes("follow"), `all five kinds (got ${kinds.join(", ")})`); + const likes = r.items.find((n) => n.kind === "like" && n.target?.hash === target.hash); + check(likes?.actors.length === 2, "two likes grouped into one item"); + for (let i = 1; i < r.items.length; i++) check(r.items[i - 1]!.time >= r.items[i]!.time, "newest first"); + check(r.failed.length === 0, "every source answered"); + log(`notifications: ${r.items.length} items, newest first, likes grouped (${likes.actors.map((a) => a.username).join(", ")})`); + + // The same from the CLI, with mark-read stored per FID. + const home = mkdtempSync(join(tmpdir(), "outpost-e2e-")); + writeFileSync(join(home, "appkey.json"), JSON.stringify(me.key), { mode: 0o600 }); + const cli = (...args: string[]) => + promisify(execFile)(process.execPath, ["--import", "tsx", "src/cli/main.ts", "notifications", "--node", HTTP, "--rpc", "off", "--check-node", "off", "--show-unverified", ...args], { env: { ...process.env, OUTPOST_HOME: home } }).then((x) => x.stdout); + const out1 = await cli(); + check(/followed you/.test(out1) && /replied/.test(out1) && /mentioned you/.test(out1) && /and @/.test(out1) && /recast your cast/.test(out1), "CLI lists all kinds"); + check(new RegExp(`${r.items.length} notifications for FID ${me.key.fid}, ${r.items.length} new`).test(out1), "CLI: all new before mark-read"); + await cli("--mark-read"); + const out3 = await cli(); + check(new RegExp(`, 0 new`).test(out3), "CLI: 0 new after mark-read"); + log("CLI: ./outpost cli notifications lists them; 0 new after --mark-read"); + + // Approve in the Farcaster app, from the CLI. A stand-in for Farcaster's endpoint checks the + // app account's signature; its "approval" adds the onchain key on the devnet, as the KeyGateway + // would. A fourth account, made in "the app", signs in with no phrase. + const appUser = await account(base + 4, `appuser-${tag}`); + const appAddress = privateKeyToAccount(app.custody).address; + const fc = await fakeFarcaster({ + appAddress, + userFid: appUser.key.fid, + autoApproveAfter: 2, + onApprove: (r) => void addOnchainSigner(appUser.key.fid, hexToBytes(r.body.key), app.key.fid), + }); + const appHome = mkdtempSync(join(tmpdir(), "outpost-e2e-app-")); + writeFileSync(join(appHome, "app-account.json"), JSON.stringify({ fid: app.key.fid, custodyKey: app.custody, address: appAddress }), { mode: 0o600 }); + const login = await promisify(execFile)(process.execPath, ["--import", "tsx", "src/cli/main.ts", "login", "--app", "--yes", "--network", "devnet", "--node", HTTP, "--rpc", "off", "--check-node", "off"], { + env: { ...process.env, OUTPOST_HOME: appHome, OUTPOST_SIGNIN_API: fc.url }, + }).then((x) => x.stdout); + fc.close(); + check(/full access and no expiry/.test(login) && /Manage connected apps/.test(login), "login --app warns before the QR code"); + check(/farcaster:\/\/signed-key-request\?token=0x/.test(login) && /[β–€β–„β–ˆ]/.test(login), "login --app prints the link and a QR code"); + check(!login.includes(app.custody.slice(2)), "the app account's key is not printed"); + const saved = JSON.parse(readFileSync(join(appHome, "appkey.json"), "utf8")) as AppKey; + check(saved.fid === appUser.key.fid && saved.onchain === true && saved.requestFid === app.key.fid, "saved key: the user's FID, onchain, requested by the app account"); + check((statSync(join(appHome, "appkey.json")).mode & 0o777) === 0o600, "key file mode 600"); + const signer = await hub.signer(saved.fid, saved.publicKey); + check(signer?.source === "SIGNER_SOURCE_ONCHAIN" && requestFidOf(signer) === app.key.fid, "node lists it as an onchain key for the app FID"); + const viaApp = await cast(hub, saved, { text: "posted with a key approved in the app" }, wait); + check(viaApp.confirmed, "cast with the approved key merged"); + const f2 = new FeedBuilder(hub); + const items = await f2.fromAuthors([saved.fid], { limit: 1 }); + const names = await f2.appNames(items); + check(items[0]?.app.requestFid === app.key.fid && names.get(app.key.fid) === app.name, `feed shows it via @${app.name}`); + log(`login --app: QR + link printed, approved, key saved (onchain, mode 600), cast shows "via @${names.get(app.key.fid)}"`); + + if (save) { + mkdirSync(save, { recursive: true, mode: 0o700 }); + writeFileSync(join(save, "app-account.json"), JSON.stringify({ fid: app.key.fid, custodyKey: app.custody, address: appAddress }), { mode: 0o600 }); + for (const [n, a] of [["me", me], ["friend", friend], ["other", other], ["appuser", appUser]] as const) { + writeFileSync(join(save, `${n}.json`), JSON.stringify({ ...a.key, username: a.name, custody: a.custody }), { mode: 0o600 }); + } + log(`devnet app keys saved in ${save}`); + } + console.log(`\nall ${step} steps passed in ${((Date.now() - t0) / 1000).toFixed(1)} s`); +} + +main().catch((e) => { + console.error(e instanceof Error ? e.message : e); + process.exitCode = 1; +}); diff --git a/src/cli/charter.ts b/src/cli/charter.ts index 54c8e53..72aae2e 100644 --- a/src/cli/charter.ts +++ b/src/cli/charter.ts @@ -139,8 +139,10 @@ async function changesFrom(hub: Hub, o: CharterOptions): Promise { export function verificationNote(hub: Hub): string { const v = hub.verifier; if (!v?.checked) return ""; - if (!v.failed && !v.unverified) return v.summary(); - return `warning: ${v.summary()}${v.unverified && !v.showUnverified ? " Run again with --show-unverified to see them, marked." : ""}`; + // Through the tunnel the node is on 127.0.0.1; anything else (--public, a society node) is not yours. + const node = /^http:\/\/(127\.0\.0\.1|localhost)(:|\/|$)/.test(hub.url) ? "Your node" : `The node ${hub.url}`; + if (!v.failed && !v.unverified) return v.summary(node); + return `warning: ${v.summary(node)}${v.unverified && !v.showUnverified ? " Run again with --show-unverified to see them, marked." : ""}`; } export function printCensus(c: Census, names: Map) { diff --git a/src/cli/main.ts b/src/cli/main.ts index 32e08e1..a4d1b5c 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -4,7 +4,9 @@ import { readFileSync } from "node:fs"; import { parseArgs } from "node:util"; import { FarcasterNetwork, MessageData, ViemLocalEip712Signer, type Message } from "@farcaster/core"; -import { privateKeyToAccount } from "viem/accounts"; +import { mnemonicToAccount, privateKeyToAccount } from "viem/accounts"; +import { bytesToHex } from "viem"; +import { renderUnicodeCompact } from "uqr"; import { addAppKey, buildCast, @@ -32,10 +34,26 @@ import { checkNodeUrl, checkToken, DEFAULT_CHECK_NODE, + checkNodeFor, + OTHER_PUBLIC_NODE, DEFAULT_OPTIMISM_RPC, Optimism, secondNode, Verifier, + resolveMentions, + createKeyRequest, + CUSTODY_PATH, + FARCASTER_API, + generatePrivateKey, + keyRequestState, + publicKeyOf, + signKeyRequest, + requestFidOf, + waitFor, + notificationsFor, + notificationText, + seenUpTo, + unreadCount, type AppFilter, type FeedItem, type AppKey, @@ -44,7 +62,7 @@ import { } from "../core/index.js"; import { askHidden, confirm, printable } from "./prompt.js"; import { cmdCharter, verificationNote } from "./charter.js"; -import { deleteKey, keyPath, loadKey, saveKey } from "./store.js"; +import { appAccountPath, deleteAppAccount, deleteKey, keyPath, loadAppAccount, loadKey, markNotificationsSeen, notificationsSeen, saveAppAccount, saveKey } from "./store.js"; const WARNING = ` STOP. Read this before you paste a recovery phrase. @@ -70,8 +88,19 @@ const USAGE = `usage: ./outpost cli [options] status node sync state login import a TEST account's phrase, add a gasless app key + login --app [--yes] approve in the Farcaster app instead: prints a QR code and a + link, waits, saves the key. No phrase. Needs an app account + on this computer (app-account). The key has full access and + no expiry; remove it in the Farcaster app + app-account [--sponsor] set up the app account that signs "Approve in the Farcaster + app" requests for this computer (./outpost client and login + --app). Asks for its phrase or custody key, hidden. Use an + account made for this, never a main account + app-account status | remove whoami show the saved app key and its expiry - cast [--reply fid:0xhash] [--channel url] [--embed url] + cast [--reply fid:0xhash] [--channel url] [--embed url] [--no-mentions] + each @username becomes a mention (looked up on your node); + an unknown one stops the cast. --no-mentions posts @ as text delete <0xhash> remove one of your casts follow | unfollow like <0xhash> | unlike <0xhash> @@ -79,6 +108,9 @@ const USAGE = `usage: ./outpost cli [options] feed [--fid n] [--channel url] [--include apps] [--exclude apps] [--limit n] following feed (or a channel) from your node. apps is a comma list of app FIDs, "self" or "unknown" + notifications [--fid n] [--limit n] [--mark-read] + replies, mentions, likes, recasts and new followers, newest + first; "*" marks what is new since you last used --mark-read charter new start a charter file charter sign sign it with the society's key ($CHARTER_PRIVATE_KEY or phrase) charter verify check the signature and the issuer's custody on your node; @@ -104,7 +136,8 @@ options: --rpc Optimism RPC for key and custody checks (default $OUTPOST_OPTIMISM_RPC or ${DEFAULT_OPTIMISM_RPC}; "off" skips them) --check-node second node asked about gasless app keys, which have no onchain record - (default $OUTPOST_CHECK_NODE or ${DEFAULT_CHECK_NODE}; "off" skips it) + (default $OUTPOST_CHECK_NODE or ${DEFAULT_CHECK_NODE}, or ${OTHER_PUBLIC_NODE} + when reading ${DEFAULT_CHECK_NODE}; "off" skips it) --show-unverified also show casts whose signing key could not be checked (marked); by default they are hidden --ttl-days app key expiry after last use, 1-90 (default 30) @@ -123,7 +156,7 @@ function hubFromEnv(v: { node?: string; "token-file"?: string; rpc?: string; "ch const auth = tokenFile ? { url: checkNodeUrl(url), token: checkToken(readFileSync(tokenFile, "utf8")) } : { url, username: process.env.OUTPOST_NODE_USER, password: process.env.OUTPOST_NODE_PASSWORD }; - const check = v["check-node"] ?? process.env.OUTPOST_CHECK_NODE ?? DEFAULT_CHECK_NODE; + const check = v["check-node"] ?? process.env.OUTPOST_CHECK_NODE ?? checkNodeFor(auth.url); const second = check === "off" ? undefined : secondNode(checkNodeUrl(check), auth.url); if (check !== "off" && !second) console.error("note: the second node is the node being read, so gasless keys are not checked"); const verifier = new Verifier(chain, second); @@ -213,10 +246,121 @@ async function cmdLogin(hub: Hub, ttlDays: number, network: FarcasterNetwork, ch console.log("Your recovery phrase was not saved anywhere. Revoke this key any time with: ./outpost cli uninstall"); } +// `where` is this computer's file, or the society node's server. +const APP_WARNING = (where: string, society: boolean) => ` + The app account signs every "Approve in the Farcaster app" request made ${society ? "through your society node" : "from this\n computer"}, and people see its name as the app their posts came from. + + - Use an account made for this. Never your main account. + - Its custody key is saved ${where} (mode 600). + Anyone who can read ${society ? "that server's" : "your"} files can take that account. + - The key is used there only to sign key requests. It is never sent to a browser, + to the node, or to Farcaster. +`; + +async function cmdAppAccount(hub: Hub, args: string[], v: { sponsor?: boolean; out?: string }, chain?: Optimism) { + const sub = args[0] ?? "setup"; + // --out: for a society node, `./outpost app-account --society` copies this file to the server + // and deletes it here. + const existing = v.out ? undefined : loadAppAccount(); + if (sub === "status") { + if (!existing) return console.log(`no app account on this computer. Set one up with: ./outpost app-account`); + console.log(`app account: FID ${existing.fid}, custody ${existing.address}${existing.sponsor ? ", sponsors the onchain fee" : ""}`); + console.log(`stored in ${appAccountPath()} (mode 600)`); + return; + } + if (sub === "remove") { + console.log(deleteAppAccount() ? `deleted ${appAccountPath()}. Restart ./outpost client to stop offering "Approve in the Farcaster app".` : "no app account on this computer; nothing to remove."); + console.log("Keys people already approved stay on their accounts; they remove them in the Farcaster app."); + return; + } + if (sub !== "setup") throw new Error("usage: ./outpost app-account [--sponsor] | status | remove"); + if (existing) { + console.log(`An app account (FID ${existing.fid}) is already set up in ${appAccountPath()}.`); + console.log("Run `./outpost app-account remove` first to use another one."); + return; + } + console.log(v.out ? APP_WARNING("on the society node's server", true) : APP_WARNING(`on this computer, in ${appAccountPath()}`, false)); + if (!(await confirm("Is this an account made for this, and do you accept the above?"))) return; + let secret = (await askHidden("App account's recovery phrase, or its custody private key (0x...), input hidden: ")).trim(); + let custodyKey: HexString; + if (/^0x[0-9a-fA-F]{64}$/.test(secret)) custodyKey = secret.toLowerCase() as HexString; + else { + const words = secret.toLowerCase().split(/\s+/); + if (![12, 15, 18, 21, 24].includes(words.length)) throw new Error("that is neither a 12 or 24 word phrase nor a 0x custody key"); + const hd = mnemonicToAccount(words.join(" "), { path: CUSTODY_PATH }).getHdKey().privateKey; + if (!hd) throw new Error("could not derive the custody key from that phrase"); + custodyKey = bytesToHex(hd); + } + secret = ""; // JS strings cannot be wiped; we drop the reference and keep no copy. + const address = privateKeyToAccount(custodyKey).address; + const account = await resolveAccount(hub, address, chain); + console.log(`\ncustody address ${account.custody}`); + console.log(printable(`FID ${account.fid}`)); + if (!(await confirm(`Use FID ${account.fid} as this computer's app account?`))) return; + const path = saveAppAccount({ fid: account.fid, custodyKey, address, ...(v.sponsor ? { sponsor: true } : {}) }, v.out); + if (v.out) return; + console.log(`\nsaved to ${path} (mode 600).`); + console.log(`./outpost client now offers "Approve in the Farcaster app" (restart it if it is running).`); + if (v.sponsor) console.log("Requests also carry a sponsorship: Farcaster pays the onchain fee only for apps it has approved for that."); +} + +// `./outpost cli login --app`: the same flow as the web client's "Approve in the Farcaster app", +// from the terminal. The key is made here; only its public half goes out. +async function cmdLoginApp(hub: Hub, network: FarcasterNetwork, yes: boolean) { + const existing = loadKey(); + if (existing) { + console.log(`An app key for FID ${existing.fid} is already saved at ${keyPath()}.`); + console.log("Run `./outpost cli uninstall` first if you want a new one."); + return; + } + const app = loadAppAccount(); + if (!app) throw new Error("no app account on this computer, so nothing can sign the request. Its owner sets one up once with: ./outpost app-account (an account made for this, never a main account)"); + if (!syncState(await hub.info()).synced) throw new Error("your node is still syncing. Run `./outpost cli status` and try again when it says synced: yes."); + console.log(` + This adds a key with full access and no expiry to your account. + + The Farcaster app adds it onchain. It can do everything your account can do apart + from moving it: post, delete, like, follow and change your profile. It never expires, + and Outpost cannot revoke it. To remove it, use the Farcaster app: Settings, then + Advanced, then Manage connected apps. + + The request comes from the app account FID ${app.fid}. ${app.sponsor ? "It offers to pay the onchain fee." : "The Farcaster app shows any onchain fee before you approve; you pay it there."} +`); + if (!yes && !(await confirm("Continue?"))) return; + const api = process.env.OUTPOST_SIGNIN_API || FARCASTER_API; + const priv = generatePrivateKey(); + const pub = bytesToHex(await publicKeyOf(priv)); + const body = await signKeyRequest(app, pub); + const req = await createKeyRequest(body, api); + console.log(renderUnicodeCompact(req.deeplinkUrl, { ecc: "M", border: 2 })); + console.log(`scan this with your phone's camera, or open this link on it:\n ${req.deeplinkUrl}\n`); + console.log("waiting for you to approve in the Farcaster app (Ctrl-C stops; nothing is saved until then)…"); + let userFid: number | undefined; + let last = ""; + while (!userFid) { + await new Promise((r) => setTimeout(r, 2000)); + if (Date.now() / 1000 > body.deadline) throw new Error("the request expired before it was approved. Nothing was added or saved. Run it again."); + const st = await keyRequestState(req.token, api).catch(() => undefined); + if (st && st.state !== last) { + last = st.state; + if (st.state === "approved") console.log("approved; waiting for the onchain transaction…"); + } + if (st?.state === "completed" && st.userFid) userFid = st.userFid; + } + console.log(`added onchain for FID ${userFid}; waiting for your node to list the key…`); + const s = await waitFor(() => hub.signer(userFid!, pub), { timeoutMs: 5 * 60_000, intervalMs: 3000 }); + if (!s) throw new Error(`Farcaster reports the key as added, but your node does not list it after 5 minutes. Nothing was saved. Remove the key in the Farcaster app and try again later.`); + if (s.source !== "SIGNER_SOURCE_ONCHAIN" || requestFidOf(s) !== app.fid) throw new Error("your node lists the key, but not as the onchain key this request asked for. Nothing was saved."); + const path = saveKey({ fid: userFid, privateKey: bytesToHex(priv), publicKey: pub, requestFid: app.fid, scopes: [], ttl: 0, network, onchain: true }); + console.log(`\nkey ${pub} is live for FID ${userFid}. Saved to ${path} (mode 600).`); + console.log("No recovery phrase was used. Remove the key any time in the Farcaster app: Settings, then Advanced, then Manage connected apps."); +} + async function cmdWhoami(hub: Hub) { const key = requireKey(); const s = await hub.signer(key.fid, key.publicKey); console.log(`FID ${key.fid}, app key ${key.publicKey}`); + if (key.onchain) console.log("kind: onchain key approved in the Farcaster app (full access, no expiry; remove it in the Farcaster app)"); if (!s) { console.log("status: NOT active on your node (revoked, expired, or not merged yet)"); return; @@ -263,6 +407,31 @@ async function cmdFeed(hub: Hub, v: { fid?: string; channel?: string; include?: console.log(verificationNote(hub)); } +async function cmdNotifications(hub: Hub, v: { fid?: string; limit?: string; "mark-read"?: boolean }) { + const fid = v.fid ? parseFid(v.fid) : requireKey().fid; + const limit = v.limit ? Number(v.limit) : 30; + if (!Number.isInteger(limit) || limit < 1 || limit > 500) throw new Error("--limit must be 1 to 500"); + const t0 = Date.now(); + const r = await notificationsFor(new FeedBuilder(hub), fid); + const seen = notificationsSeen(fid); + for (const n of r.items.slice(0, limit)) { + const about = n.kind === "reply" || n.kind === "mention" ? n.cast : n.target; + console.log(printable(`${n.time > (seen ?? 0) ? "*" : " "} ${new Date(n.time).toISOString().slice(0, 16).replace("T", " ")} ${notificationText(n)}`)); + if (about) { + console.log(` ${printable(about.text.replace(/\s+/g, " ")).slice(0, 160)}`); + console.log(` ${about.fid}:${about.hash}`); + } + } + const unread = unreadCount(r.items, seen); + console.log(`\n${r.items.length} notification${r.items.length === 1 ? "" : "s"} for FID ${fid}, ${unread} new, in ${((Date.now() - t0) / 1000).toFixed(2)} s from ${hub.url}`); + if (r.failed.length) console.log(printable(`the node did not answer for: ${r.failed.join(", ")}. Run it again to retry.`)); + if (v["mark-read"]) { + markNotificationsSeen(fid, seenUpTo(r.items)); + console.log("marked all read"); + } else if (unread) console.log("mark them read with: ./outpost cli notifications --mark-read"); + console.log(verificationNote(hub)); +} + async function cmdUninstall(hub: Hub) { const key = loadKey(); if (!key) { @@ -270,6 +439,14 @@ async function cmdUninstall(hub: Hub) { console.log("A key added in the web client lives in that browser: open the client, then 'Your app key' > 'Revoke this key (uninstall)'."); return; } + if (key.onchain) { + // Only the account's owner can remove an onchain key; this only forgets it here. + console.log("this key was approved in the Farcaster app. It is onchain, and Outpost cannot revoke it."); + console.log("Remove it in the Farcaster app: Settings, then Advanced, then Manage connected apps."); + deleteKey(); + console.log(`deleted ${keyPath()}`); + return; + } // The key file is the only way to revoke the key without the recovery phrase, so it is deleted // only once the node confirms the revocation. A node that is behind, or has not merged the key // yet, answers "not found" for a key that is live on the network. @@ -340,6 +517,12 @@ async function main(argv: string[]) { rpc: { type: "string" }, "check-node": { type: "string" }, "show-unverified": { type: "boolean" }, + "no-mentions": { type: "boolean" }, + "mark-read": { type: "boolean" }, + app: { type: "boolean" }, + yes: { type: "boolean" }, + sponsor: { type: "boolean" }, + out: { type: "string" }, "ttl-days": { type: "string", default: "30" }, // For testing against a local devnet. Everything real uses mainnet. network: { type: "string", default: "mainnet" }, @@ -361,6 +544,12 @@ async function main(argv: string[]) { return; } const { hub, chain } = hubFromEnv(values); + const cliNetwork = () => { + const nets: Record = { mainnet: FarcasterNetwork.MAINNET, devnet: FarcasterNetwork.DEVNET }; + const n = nets[values.network ?? "mainnet"]; + if (n === undefined) throw new Error("--network must be mainnet or devnet"); + return n; + }; const reaction = (r: Reaction, add: boolean) => async () => { const key = requireKey(); const target = { fid: parseFid(args[0]), hash: parseHash(args[1]) }; @@ -368,7 +557,10 @@ async function main(argv: string[]) { }; const commands: Record Promise> = { status: () => cmdStatus(hub), + // Devnet accounts are not on Optimism mainnet, so only mainnet looks the FID up there. + "app-account": () => cmdAppAccount(hub, args, values, cliNetwork() === FarcasterNetwork.MAINNET ? chain : undefined), login: () => { + if (values.app) return cmdLoginApp(hub, cliNetwork(), !!values.yes); const days = Number(values["ttl-days"]); if (!Number.isInteger(days) || days < 1 || days > 90) throw new Error("--ttl-days must be 1 to 90"); const nets: Record = { mainnet: FarcasterNetwork.MAINNET, devnet: FarcasterNetwork.DEVNET }; @@ -381,13 +573,15 @@ async function main(argv: string[]) { uninstall: () => cmdUninstall(hub), selftest: () => cmdSelftest(hub), feed: () => cmdFeed(hub, values), + notifications: () => cmdNotifications(hub, values), charter: () => cmdCharter(hub, args, requireKey, chain, values), cast: async () => { - const text = args.join(" "); + const key = requireKey(); + const text = values["no-mentions"] ? { text: args.join(" ") } : await resolveMentions(hub, args.join(" ")); report( "cast", - await cast(hub, requireKey(), { - text, + await cast(hub, key, { + ...text, embeds: values.embed, replyTo: values.reply ? parseCastRef(values.reply) : undefined, channelUrl: values.channel, diff --git a/src/cli/store.ts b/src/cli/store.ts index 4072e36..56fe3c0 100644 --- a/src/cli/store.ts +++ b/src/cli/store.ts @@ -3,7 +3,7 @@ import { chmodSync, existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, join } from "node:path"; -import { approvalCache, type ApprovalCache, type AppKey } from "../core/index.js"; +import { approvalCache, type ApprovalCache, type AppAccount, type AppKey } from "../core/index.js"; export function stateDir(): string { return process.env.OUTPOST_HOME ?? join(process.env.XDG_CONFIG_HOME ?? join(homedir(), ".config"), "outpost"); @@ -39,6 +39,51 @@ export function savedApprovals(): ApprovalCache { ); } +// Per FID, the time up to which notifications were marked read (`notifications --mark-read`). +const seenPath = () => join(stateDir(), "notifications-seen.json"); + +export function notificationsSeen(fid: number): number | undefined { + const p = seenPath(); + if (!existsSync(p)) return undefined; + const v = (JSON.parse(readFileSync(p, "utf8")) as Record)[String(fid)]; + return typeof v === "number" ? v : undefined; +} + +export function markNotificationsSeen(fid: number, at: number): void { + const p = seenPath(); + const all = existsSync(p) ? (JSON.parse(readFileSync(p, "utf8")) as Record) : {}; + all[String(fid)] = at; + mkdirSync(dirname(p), { recursive: true, mode: 0o700 }); + writeFileSync(p, JSON.stringify(all) + "\n", { mode: 0o600 }); +} + +// The app account for "Approve in the Farcaster app" (`./outpost app-account`). Its custody key +// sits here, mode 0600, and is read by the CLI and by `./outpost client`'s server; it is never +// sent to a browser. +export function appAccountPath(): string { + return join(stateDir(), "app-account.json"); +} + +export function loadAppAccount(): AppAccount | undefined { + const p = appAccountPath(); + if (!existsSync(p)) return undefined; + return JSON.parse(readFileSync(p, "utf8")) as AppAccount; +} + +export function saveAppAccount(a: AppAccount, p = appAccountPath()): string { + mkdirSync(dirname(p), { recursive: true, mode: 0o700 }); + writeFileSync(p, JSON.stringify(a, null, 2) + "\n", { mode: 0o600 }); + chmodSync(p, 0o600); + return p; +} + +export function deleteAppAccount(): boolean { + const p = appAccountPath(); + if (!existsSync(p)) return false; + unlinkSync(p); + return true; +} + export function deleteKey(): void { const p = keyPath(); if (existsSync(p)) unlinkSync(p); diff --git a/src/core/appkey.ts b/src/core/appkey.ts index 1e6df8b..6c5d64f 100644 --- a/src/core/appkey.ts +++ b/src/core/appkey.ts @@ -42,6 +42,9 @@ export interface AppKey { scopes: MessageType[]; ttl: number; network: FarcasterNetwork; + // Approved in the Farcaster app (appsignin.ts): an onchain key with full access and no expiry, + // which only the account's owner can remove. Outpost cannot revoke it. + onchain?: boolean; } export function generatePrivateKey(): Uint8Array { diff --git a/src/core/appsignin.ts b/src/core/appsignin.ts new file mode 100644 index 0000000..4a9b38e --- /dev/null +++ b/src/core/appsignin.ts @@ -0,0 +1,169 @@ +// "Approve in the Farcaster app": sign in without a recovery phrase, for accounts made in the +// Farcaster app. Farcaster's documented signed key request flow +// (farcasterxyz/docs, docs/reference/farcaster/signer-requests.md): +// +// 1. Outpost makes a new Ed25519 key. Its private part stays where it was made (the browser, +// or the CLI). +// 2. An "app account" (an FID set up once by the node or client owner) signs a +// SignedKeyRequest (EIP-712) for the public key with its custody key. That key lives only on +// the owner's machine or server (`./outpost app-account`), never in a browser. +// 3. The request goes to Farcaster's endpoint, which answers with a farcaster:// link (a QR +// code). A link alone cannot work: it carries the token this endpoint hands out. No API key. +// 4. The person approves in the Farcaster app, which adds the key onchain (KeyRegistry) and +// pays the fee, unless the app account sponsors it. +// 5. Outpost confirms the key on its own node (/v1/signer) before using it. +// +// Such a key is an ordinary onchain key: it can sign every kind of message for the account, has +// no expiry, and only the account's owner can remove it (in the Farcaster app). Outpost cannot +// revoke it, so the page says so before showing the QR code. + +import { ViemLocalEip712Signer } from "@farcaster/core"; +import { bytesToHex, hexToBytes } from "viem"; +import { privateKeyToAccount } from "viem/accounts"; +import { unwrap } from "./appkey.js"; +import type { HexString } from "./hub.js"; + +// Farcaster's client API: the one host besides your node that this flow talks to, and only from +// the Outpost server or CLI, never from the page. +export const FARCASTER_API = "https://api.farcaster.xyz"; + +// How long the app account's signature stays valid. Farcaster suggests a day; an approval that +// has not happened within this time is shown as expired. +export const REQUEST_SECONDS = 30 * 60; + +// The app account, as stored by `./outpost app-account` (mode 600). `custodyKey` is the +// account's custody private key: whoever has it owns that account. +export interface AppAccount { + fid: number; + custodyKey: HexString; + address: HexString; + sponsor?: boolean; // also sign Farcaster's sponsorship, so the person pays no fee +} + +export interface KeyRequestBody { + key: HexString; + requestFid: number; + signature: HexString; + deadline: number; // Unix seconds + sponsorship?: { sponsorFid: number; signature: HexString }; +} + +export function isPublicKey(k: unknown): k is HexString { + return typeof k === "string" && /^0x[0-9a-f]{64}$/i.test(k); +} + +export async function signKeyRequest(account: AppAccount, key: HexString, now = Math.floor(Date.now() / 1000)): Promise { + if (!isPublicKey(key)) throw new Error("the key must be a 32-byte Ed25519 public key in hex"); + const signer = privateKeyToAccount(account.custodyKey); + if (signer.address.toLowerCase() !== account.address.toLowerCase()) throw new Error("the app account's key does not match its address"); + const deadline = now + REQUEST_SECONDS; + const signature = bytesToHex( + unwrap(await new ViemLocalEip712Signer(signer).signKeyRequest({ requestFid: BigInt(account.fid), key: hexToBytes(key), deadline: BigInt(deadline) })), + ); + const body: KeyRequestBody = { key: key.toLowerCase() as HexString, requestFid: account.fid, signature, deadline }; + // Farcaster's sponsorship: an EIP-191 signature, by the sponsoring FID, of the request signature. + if (account.sponsor) body.sponsorship = { sponsorFid: account.fid, signature: await signer.signMessage({ message: { raw: signature } }) }; + return body; +} + +export type RequestState = "pending" | "approved" | "completed"; +// What the Outpost server reports: Farcaster's states, or expired once the deadline has passed. +export type SignInState = RequestState | "expired"; + +export interface KeyRequest { + token: string; + deeplinkUrl: string; + state: RequestState; + userFid?: number; +} + +const TOKEN = /^0x[0-9a-f]{1,128}$/i; +const LINK = /^farcaster:\/\/signed-key-request\?token=0x[0-9a-f]{1,128}$/i; + +export function isToken(t: unknown): t is string { + return typeof t === "string" && TOKEN.test(t); +} + +// Only the fields Outpost uses, checked, so nothing else from the endpoint reaches the page. +function keyRequestFrom(json: unknown): KeyRequest { + const r = (json as { result?: { signedKeyRequest?: Record } })?.result?.signedKeyRequest; + const { token, deeplinkUrl, state, userFid } = r ?? {}; + if (!isToken(token) || typeof deeplinkUrl !== "string" || !LINK.test(deeplinkUrl) || !["pending", "approved", "completed"].includes(state as string)) { + throw new Error("Farcaster's endpoint gave an answer Outpost does not understand"); + } + if (userFid !== undefined && !(Number.isSafeInteger(userFid) && (userFid as number) > 0)) throw new Error("Farcaster's endpoint returned a bad FID"); + return { token, deeplinkUrl, state: state as RequestState, userFid: userFid as number | undefined }; +} + +async function call(fetchImpl: typeof fetch, url: string, init?: RequestInit): Promise { + let res: Response; + try { + res = await fetchImpl(url, { ...init, signal: AbortSignal.timeout(15_000) }); + } catch (e) { + throw new Error(`cannot reach Farcaster's endpoint (${(e as Error).message})`); + } + const text = await res.text(); + if (!res.ok) throw new Error(`Farcaster's endpoint refused the request (HTTP ${res.status})`); + try { + return keyRequestFrom(JSON.parse(text)); + } catch (e) { + throw e instanceof SyntaxError ? new Error("Farcaster's endpoint did not answer with JSON") : e; + } +} + +export function createKeyRequest(body: KeyRequestBody, api = FARCASTER_API, fetchImpl: typeof fetch = globalThis.fetch.bind(globalThis)): Promise { + return call(fetchImpl, `${api}/v2/signed-key-requests`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); +} + +export function keyRequestState(token: string, api = FARCASTER_API, fetchImpl: typeof fetch = globalThis.fetch.bind(globalThis)): Promise { + if (!isToken(token)) return Promise.reject(new Error("not a request token")); + return call(fetchImpl, `${api}/v2/signed-key-request?token=${encodeURIComponent(token)}`); +} + +// ---- the page's side: it asks its own Outpost server (or a society node), never Farcaster. + +export interface SignInService { + configured: boolean; + fid?: number; + sponsored?: boolean; +} + +export interface StartedRequest { + token: string; + deeplinkUrl: string; + deadline: number; + requestFid: number; +} + +export class SignInClient { + constructor( + readonly base: string, // e.g. "" (same origin) or "https://society.example/app-signin" + private readonly headers: Record = {}, + private readonly fetchImpl: typeof fetch = globalThis.fetch.bind(globalThis), + ) {} + + private async json(path: string, init?: RequestInit): Promise { + const res = await this.fetchImpl(`${this.base}${path}`, { ...init, headers: { ...this.headers, ...(init?.headers as Record) } }); + const body = (await res.json().catch(() => ({}))) as T & { error?: string }; + if (!res.ok) throw new Error(body.error ?? `HTTP ${res.status}`); + return body; + } + + // Undefined when nothing answers there (a static host, or an Outpost too old for this). + async service(): Promise { + try { + const s = await this.json(""); + return typeof s.configured === "boolean" ? s : undefined; + } catch { + return undefined; + } + } + + start(key: HexString): Promise { + return this.json("", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ key }) }); + } + + state(token: string): Promise<{ state: SignInState; userFid?: number }> { + return this.json(`/state?token=${encodeURIComponent(token)}`); + } +} diff --git a/src/core/feed.ts b/src/core/feed.ts index d266986..6ce2f8c 100644 --- a/src/core/feed.ts +++ b/src/core/feed.ts @@ -165,6 +165,11 @@ export class FeedBuilder { return this.hydrate(casts, limit, filter, concurrency); } + // Casts read elsewhere (a thread, notifications) as feed items: key checks, authors, mentions. + items(casts: HubMessage[], limit = casts.length, concurrency = 24): Promise { + return this.hydrate(casts, Math.max(1, limit), undefined, concurrency); + } + // Walks the sorted casts, resolving apps, until `limit` casts pass the filter. private async hydrate(casts: HubMessage[], limit: number, filter: AppFilter | undefined, concurrency: number): Promise { const out: FeedItem[] = []; diff --git a/src/core/hub.ts b/src/core/hub.ts index 5b4d062..99d1d53 100644 --- a/src/core/hub.ts +++ b/src/core/hub.ts @@ -250,11 +250,12 @@ export class Hub { return evs.at(-1)?.idRegisterEventBody?.to; } - // fname (e.g. "dwr") -> FID. ENS names need an Ethereum RPC on the node, so we only - // look up plain fnames here. + // Username (an fname such as "dwr", or an ENS name such as "vitalik.eth") -> FID, from the + // username proof the node stores. The node's answer is not proof: check it against the + // account's signed profile before relying on it. async fidByUsername(name: string): Promise { const n = name.trim().replace(/^@/, "").toLowerCase(); - if (!/^[a-z0-9][a-z0-9-]{0,15}$/.test(n)) return undefined; + if (!/^[a-z0-9][a-z0-9-]{0,15}$/.test(n) && !/^[a-z0-9-]+(\.[a-z0-9-]+)*\.eth$/.test(n)) return undefined; return this.optional(async () => (await this.request<{ fid: number }>("GET", "/v1/userNameProofByName", { name: n })).fid); } @@ -331,6 +332,37 @@ export class Hub { ); } + recastsByCast(target: { fid: number; hash: HexString }, opts: { pageSize?: number; pageToken?: string } = {}) { + return this.paged( + "/v1/reactionsByCast", + { target_fid: target.fid, target_hash: target.hash, reaction_type: "Recast", pageSize: opts.pageSize, pageToken: opts.pageToken }, + (m) => { + const r = m.data.reactionBody; + return r?.type === "REACTION_TYPE_RECAST" && r.targetCastId?.fid === target.fid && r.targetCastId.hash.toLowerCase() === target.hash.toLowerCase(); + }, + ); + } + + // Casts that mention this FID. Read from every shard; each shard's page comes in its own order, + // and pageSize is per shard, so callers sort by timestamp. + castsByMention(fid: number, opts: { pageSize?: number; pageToken?: string; reverse?: boolean } = {}) { + return this.paged( + "/v1/castsByMention", + { fid, pageSize: opts.pageSize, pageToken: opts.pageToken, reverse: opts.reverse }, + (m) => !!m.data.castAddBody?.mentions?.includes(fid), + ); + } + + // Links to this FID (followers, for "follow"). Every shard, unsorted, like castsByMention. + linksByTarget(targetFid: number, opts: { linkType?: string; pageSize?: number; pageToken?: string; reverse?: boolean } = {}) { + const linkType = opts.linkType ?? "follow"; + return this.paged( + "/v1/linksByTargetFid", + { target_fid: targetFid, link_type: linkType, pageSize: opts.pageSize, pageToken: opts.pageToken, reverse: opts.reverse }, + (m) => m.data.linkBody?.targetFid === targetFid && m.data.linkBody.type === linkType, + ); + } + async castById(fid: number, hash: HexString): Promise { return this.one("/v1/castById", { fid, hash }, (m) => m.data.fid === fid && m.hash.toLowerCase() === hash.toLowerCase()); } diff --git a/src/core/index.ts b/src/core/index.ts index 9cdfaa0..0a43e66 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -11,4 +11,7 @@ export * from "./chain.js"; export * from "./society.js"; export * from "./secondnode.js"; export * from "./votes.js"; +export * from "./mentions.js"; +export * from "./notifications.js"; +export * from "./appsignin.js"; export * from "./sponsor.js"; diff --git a/src/core/mentions.ts b/src/core/mentions.ts new file mode 100644 index 0000000..b4bd569 --- /dev/null +++ b/src/core/mentions.ts @@ -0,0 +1,111 @@ +// @mentions. A cast does not carry "@name" in its text: the node stores the mentioned FIDs and, +// for each, the UTF-8 byte offset in the text where apps put the name back (feed.ts withMentions). +// Snapchain checks: at most 10, each offset at most the text's byte length, offsets ascending +// (validations/cast.rs). + +import { FeedBuilder } from "./feed.js"; +import type { Hub } from "./hub.js"; + +export const MAX_MENTIONS = 10; + +// A name the person picked from the suggestions (or that the CLI looked up), and its FID. +export interface MentionPick { + name: string; // without "@", e.g. "dwr" or "vitalik.eth" + fid: number; +} + +export interface MentionedText { + text: string; + mentions: number[]; + mentionsPositions: number[]; +} + +// Characters a username can hold. A "." only counts when a letter or digit follows it, so the +// full stop in "thanks @dwr." ends the name. +const NAME_CHAR = /[a-z0-9_-]/i; + +function nameEndsAt(text: string, i: number): boolean { + const c = text[i]; + if (c === undefined) return true; + if (NAME_CHAR.test(c)) return false; + return !(c === "." && NAME_CHAR.test(text[i + 1] ?? "")); +} + +// Every "@name" whose name was picked becomes a mention; any other "@word" stays plain text, so a +// picked name that is edited afterwards ("@dwr" -> "@dwrx") no longer mentions anyone. +export function extractMentions(input: string, picks: MentionPick[]): MentionedText { + const byName = new Map(); + for (const p of picks) byName.set(p.name.replace(/^@/, "").toLowerCase(), p.fid); + // Longest names first, so "@dwr.eth" is not read as "@dwr" followed by ".eth". + const names = [...byName.keys()].filter(Boolean).sort((a, b) => b.length - a.length); + const enc = new TextEncoder(); + let text = ""; + const mentions: number[] = []; + const positions: number[] = []; + let bytes = 0; + let i = 0; + while (i < input.length) { + const before = input[i - 1]; + if (input[i] === "@" && (before === undefined || !NAME_CHAR.test(before))) { + const rest = input.slice(i + 1).toLowerCase(); + const name = names.find((n) => rest.startsWith(n) && nameEndsAt(input, i + 1 + n.length)); + if (name) { + mentions.push(byName.get(name)!); + positions.push(bytes); + i += 1 + name.length; + continue; + } + } + // One code point at a time, so the byte count never splits a character. + const ch = String.fromCodePoint(input.codePointAt(i)!); + text += ch; + bytes += enc.encode(ch).length; + i += ch.length; + } + if (mentions.length > MAX_MENTIONS) throw new Error(`at most ${MAX_MENTIONS} mentions in one cast`); + // buildCast keeps text with mentions as it is, so trim here without moving a mention. + const lead = text.length - text.trimStart().length; + const leadBytes = enc.encode(text.slice(0, lead)).length; + if (!positions.length || positions[0]! >= leadBytes) { + text = text.slice(lead); + for (let j = 0; j < positions.length; j++) positions[j]! -= leadBytes; + } + const last = positions.at(-1) ?? 0; + while (/\s$/.test(text) && enc.encode(text).length - 1 >= last) text = text.slice(0, -1); + return { text, mentions, mentionsPositions: positions }; +} + +// The "@word" being typed just before the cursor, for the suggestion list. Undefined when the +// cursor is not at the end of an "@" word. +export function mentionQuery(text: string, cursor: number): { start: number; query: string } | undefined { + const m = /(^|[^a-z0-9_-])@([a-z0-9_.-]{0,20})$/i.exec(text.slice(0, cursor)); + if (!m) return undefined; + return { start: cursor - m[2]!.length - 1, query: m[2]! }; +} + +// Every "@word" in the text, in order: names to look up (CLI) or to check against picks. +export function mentionedNames(text: string): string[] { + const out: string[] = []; + const re = /(^|[^a-z0-9_-])@([a-z0-9][a-z0-9_-]*(?:\.[a-z0-9][a-z0-9_-]*)*)/gi; + for (let m = re.exec(text); m; m = re.exec(text)) out.push(m[2]!.toLowerCase()); + return out; +} + +// Typed text (CLI) -> mentions. Each "@name" is looked up on the node and kept only if that +// account's own signed profile carries the name. An unknown name throws, naming it, instead of +// quietly posting it as plain text. +export async function resolveMentions(hub: Hub, text: string, feed = new FeedBuilder(hub)): Promise { + const names = [...new Set(mentionedNames(text))]; + if (!names.length) return { text }; + const picks: MentionPick[] = []; + for (const name of names) { + const fid = await hub.fidByUsername(name); + const p = fid ? await feed.profile(fid).catch(() => undefined) : undefined; + if (!fid || p?.username?.toLowerCase() !== name) { + throw new Error(`unknown username @${name}: the node has no account with that name. Fix it, or add --no-mentions to post every @ as plain text.`); + } + picks.push({ name, fid }); + } + const m = extractMentions(text, picks); + return m.mentions.length ? m : { text }; +} diff --git a/src/core/notifications.ts b/src/core/notifications.ts new file mode 100644 index 0000000..8cb2b56 --- /dev/null +++ b/src/core/notifications.ts @@ -0,0 +1,167 @@ +// Notifications, read from your own node. Snapchain has no notifications endpoint, so they are +// put together from plain reads: +// replies: castsByParent on each of your recent casts +// likes: reactionsByCast (Like) on the same casts +// recasts: reactionsByCast (Recast) on the same casts +// mentions: castsByMention for your FID +// followers: linksByTargetFid for your FID +// Every message goes through the node client's verifier (hash, signature, "is this what was +// asked"), and the key that signed it must pass the same key check as the feed (verify.ts). +// Likes and recasts of one cast are grouped into one item. Newest first. + +import { FARCASTER_EPOCH_MS, mapLimit, notFuture, type FeedBuilder, type FeedItem, type Profile } from "./feed.js"; +import type { HexString, HubMessage, Paged } from "./hub.js"; + +export type NotificationKind = "reply" | "mention" | "like" | "recast" | "follow"; + +export interface Notification { + id: string; // stable, for the page + kind: NotificationKind; + time: number; // Unix ms of the newest action in the item + actors: Profile[]; // newest first; for a like or recast, everyone who did it + cast?: FeedItem; // the reply or the mentioning cast + target?: FeedItem; // your cast that was replied to, liked or recast +} + +export interface NotificationOptions { + recent?: number; // your latest casts whose replies and reactions are read (default 25) + perSource?: number; // messages read per source (default 50) + concurrency?: number; + // Called after each source finishes, with everything so far and how many sources are left, + // so a slow node shows partial results instead of nothing. + onUpdate?: (r: NotificationResult) => void; +} + +export interface NotificationResult { + items: Notification[]; + pending: number; // sources still loading (0 when done) + failed: string[]; // sources the node did not answer, e.g. "mentions" +} + +const timeOf = (m: HubMessage) => FARCASTER_EPOCH_MS + m.data.timestamp * 1000; + +export async function notificationsFor(feed: FeedBuilder, fid: number, opts: NotificationOptions = {}): Promise { + const { recent = 25, perSource = 50, concurrency = 8 } = opts; + const hub = feed.hub; + const failed: string[] = []; + let pending = 5; + + // A message counts only if its key passes the same check the feed uses. + const admitted = async (ms: HubMessage[]): Promise => { + const ok = await mapLimit(ms, concurrency, async (m) => !hub.verifier || hub.verifier.admits((await feed.apps.appOf(m.data.fid, m.signer)).keyCheck)); + return ms.filter((m, i) => ok[i] && m.data.fid !== fid && notFuture(m)); + }; + const profiles = (fids: number[]) => mapLimit(fids, concurrency, (f) => feed.profile(f).catch(() => ({ fid: f }) as Profile)); + + const replies: Notification[] = []; + const mentions: Notification[] = []; + const reactions: Notification[] = []; + const follows: Notification[] = []; + const result = (): NotificationResult => { + // A reply that also mentions you is shown once, as a reply. + const replyHashes = new Set(replies.map((n) => n.cast!.hash)); + const items = [...replies, ...mentions.filter((n) => !replyHashes.has(n.cast!.hash)), ...reactions, ...follows]; + items.sort((a, b) => b.time - a.time || a.id.localeCompare(b.id)); + return { items, pending, failed: [...failed] }; + }; + const source = async (name: string, fn: () => Promise) => { + try { + await fn(); + } catch { + failed.push(name); + } + pending--; + opts.onUpdate?.(result()); + }; + + const castNotes = async (kind: "reply" | "mention", ms: HubMessage[], targets?: Map) => { + const casts = (await admitted(ms.filter((m) => m.data.castAddBody))).sort((a, b) => b.data.timestamp - a.data.timestamp); + const items = await feed.items(casts); + return items.map((cast): Notification => { + const p = cast.parent && "hash" in cast.parent ? targets?.get(cast.parent.hash.toLowerCase()) : undefined; + return { id: `${kind}:${cast.hash}`, kind, time: cast.time, actors: [cast.author ?? { fid: cast.fid }], cast, target: p }; + }); + }; + + // Your recent casts, the targets of replies, likes and recasts. + const mine = await feed.hub.castsByFid(fid, { reverse: true, pageSize: recent }).then( + async (p) => feed.items(p.items.filter((m) => m.data.castAddBody && notFuture(m)).slice(0, recent)), + () => undefined, + ); + const targets = new Map((mine ?? []).map((c) => [c.hash.toLowerCase(), c])); + + const all = (p: Promise>) => p.then((r) => r.items); + await Promise.all([ + source("replies", async () => { + if (!mine) throw new Error("your casts could not be read"); + const pages = await mapLimit(mine, concurrency, (c) => all(hub.repliesTo({ fid, hash: c.hash }, { pageSize: perSource }))); + replies.push(...(await castNotes("reply", pages.flat(), targets))); + }), + source("mentions", async () => { + const ms = (await all(hub.castsByMention(fid, { reverse: true, pageSize: perSource }))).sort((a, b) => b.data.timestamp - a.data.timestamp); + mentions.push(...(await castNotes("mention", ms.slice(0, perSource), targets))); + }), + source("likes", async () => { + if (!mine) throw new Error("your casts could not be read"); + reactions.push(...(await grouped("like", mine, (t) => all(hub.likesByCast(t, { pageSize: perSource }))))); + }), + source("recasts", async () => { + if (!mine) throw new Error("your casts could not be read"); + reactions.push(...(await grouped("recast", mine, (t) => all(hub.recastsByCast(t, { pageSize: perSource }))))); + }), + source("followers", async () => { + const ms = await admitted(await all(hub.linksByTarget(fid, { linkType: "follow", reverse: true, pageSize: perSource }))); + ms.sort((a, b) => b.data.timestamp - a.data.timestamp); + const ps = await profiles(ms.map((m) => m.data.fid)); + follows.push(...ms.slice(0, perSource).map((m, i): Notification => ({ id: `follow:${m.data.fid}`, kind: "follow", time: timeOf(m), actors: [ps[i]!] }))); + }), + ]); + return result(); + + async function grouped(kind: "like" | "recast", casts: FeedItem[], read: (t: { fid: number; hash: HexString }) => Promise): Promise { + const pages = await mapLimit(casts, concurrency, (c) => read({ fid, hash: c.hash }).then(admitted)); + const out: Notification[] = []; + for (const [i, ms] of pages.entries()) { + if (!ms.length) continue; + ms.sort((a, b) => b.data.timestamp - a.data.timestamp); + const fids = [...new Set(ms.map((m) => m.data.fid))]; + out.push({ id: `${kind}:${casts[i]!.hash}`, kind, time: timeOf(ms[0]!), actors: await profiles(fids), target: casts[i] }); + } + return out; + } +} + +// Items newer than the last time you marked them read. +export function unreadCount(items: Notification[], lastSeen: number | undefined): number { + return items.filter((n) => n.time > (lastSeen ?? 0)).length; +} + +// The moment to store when marking everything read: never earlier than the newest item, so an +// item dated a little ahead of this clock does not stay unread. +export function seenUpTo(items: Notification[], now = Date.now()): number { + return Math.max(now, ...items.map((n) => n.time)); +} + +const nameOf = (p: Profile) => (p.username ? `@${p.username}` : `fid ${p.fid}`); + +// "@dwr", "@dwr and @v", "@dwr and 3 others". +export function actorText(actors: Profile[]): string { + if (actors.length <= 2) return actors.map(nameOf).join(" and "); + return `${nameOf(actors[0]!)} and ${actors.length - 1} others`; +} + +export function notificationText(n: Notification): string { + const who = actorText(n.actors); + switch (n.kind) { + case "reply": + return `${who} replied`; + case "mention": + return `${who} mentioned you`; + case "like": + return `${who} liked your cast`; + case "recast": + return `${who} recast your cast`; + case "follow": + return `${who} followed you`; + } +} diff --git a/src/core/secondnode.ts b/src/core/secondnode.ts index c932804..18b6dcb 100644 --- a/src/core/secondnode.ts +++ b/src/core/secondnode.ts @@ -13,6 +13,16 @@ import type { KeyLister, KeyScopes } from "./verify.js"; // Farcaster's public read node: plain GETs work from any page (Access-Control-Allow-Origin: *). export const DEFAULT_CHECK_NODE = "https://snap.farcaster.xyz:3381"; +// Another public node Farcaster runs, on a separate machine. Used as the second node when the node +// being read is snap.farcaster.xyz (./outpost client --public): same operator, so it catches one +// node being wrong, not Farcaster itself. +export const OTHER_PUBLIC_NODE = "https://crackle.farcaster.xyz:3381"; + +// The default second node for a node being read: Farcaster's public node, or, when that is the +// node being read, its other public node. +export function checkNodeFor(readingFrom: string): string { + return new URL(readingFrom).origin === new URL(DEFAULT_CHECK_NODE).origin ? OTHER_PUBLIC_NODE : DEFAULT_CHECK_NODE; +} export class SecondNode implements KeyLister { private byFid = new Map>>(); @@ -49,8 +59,10 @@ export class SecondNode implements KeyLister { } } -// Undefined when the second node is the node being read: a node cannot second itself. +// Undefined when the second node is the node being read: a node cannot second itself. Compared by +// base URL, so ./outpost client can forward one path to the node and another to the second node. export function secondNode(url: string, readingFrom: string, fetchImpl?: typeof fetch): SecondNode | undefined { - if (new URL(url).origin === new URL(readingFrom).origin) return undefined; + const base = (u: string) => new URL(u).href.replace(/\/+$/, ""); + if (base(url) === base(readingFrom)) return undefined; return new SecondNode(new Hub({ url, fetch: fetchImpl, timeoutMs: 10_000 })); } diff --git a/src/server/appsignin-service.ts b/src/server/appsignin-service.ts new file mode 100644 index 0000000..24cfa9d --- /dev/null +++ b/src/server/appsignin-service.ts @@ -0,0 +1,150 @@ +// The server side of "Approve in the Farcaster app" (src/core/appsignin.ts). The page sends only +// a public key; this signs the key request with the app account and asks Farcaster's endpoint +// for the approval link. The app account's key never leaves this process: no response carries +// it, or the signature made with it, and nothing about a request is logged. +// +// GET {configured, fid?, sponsored?} +// POST {key} {token, deeplinkUrl, deadline, requestFid} +// GET /state?token=.. {state: pending | approved | completed | expired, userFid?} +// +// Used by `./outpost client` (src/server/serve.ts, same origin as the page). Only tokens this +// process handed out can be polled, so it is no open relay to Farcaster's endpoint. + +import type { IncomingMessage, ServerResponse } from "node:http"; +import { createHash } from "node:crypto"; +import { createKeyRequest, isPublicKey, keyRequestState, signKeyRequest, FARCASTER_API, type AppAccount } from "../core/appsignin.js"; + +export interface SignInOptions { + account: () => AppAccount | undefined; // read on every request: setup applies without a restart + api?: string; + fetch?: typeof fetch; + now?: () => number; // Unix seconds, for tests + // Extra rule for each request (e.g. same origin, or a member token). A string refuses it (403). + refuse?: (req: IncomingMessage) => string | undefined; + headers?: (req: IncomingMessage) => Record; +} + +const MAX_BODY = 1024; +// New requests per process: a burst of 10, then one every 6 seconds. +const BURST = 10; +const EVERY_MS = 6_000; + +export function signInHandler(opts: SignInOptions) { + const api = opts.api ?? FARCASTER_API; + const now = opts.now ?? (() => Math.floor(Date.now() / 1000)); + const issued = new Map(); // token -> deadline + let tokens = BURST; + let at = Date.now(); + + return (req: IncomingMessage, res: ServerResponse, sub: string) => { + const extra = opts.headers?.(req) ?? {}; + const send = (status: number, body: unknown) => + res.writeHead(status, { "content-type": "application/json", "cache-control": "no-store", "x-content-type-options": "nosniff", ...extra }).end(JSON.stringify(body)); + const fail = (status: number, error: string) => send(status, { error }); + const refused = opts.refuse?.(req); + if (refused) return fail(403, refused); + const account = opts.account(); + + if (req.method === "GET" && sub === "") { + return send(200, account ? { configured: true, fid: account.fid, sponsored: !!account.sponsor } : { configured: false }); + } + if (!account) return fail(404, "no app account is set up here. Its owner runs ./outpost app-account once."); + + if (req.method === "GET" && sub === "/state") { + const token = new URL(req.url ?? "/", "http://x").searchParams.get("token") ?? ""; + const deadline = issued.get(token); + if (deadline === undefined) return fail(404, "unknown request. Start again."); + void keyRequestState(token, api, opts.fetch).then( + (r) => { + if (r.state !== "completed" && now() > deadline) { + issued.delete(token); + return send(200, { state: "expired" }); + } + if (r.state === "completed") issued.delete(token); + send(200, { state: r.state, ...(r.userFid ? { userFid: r.userFid } : {}) }); + }, + (e: Error) => fail(502, e.message), + ); + return; + } + + if (req.method === "POST" && sub === "") { + const ms = Date.now(); + tokens = Math.min(BURST, tokens + (ms - at) / EVERY_MS); + at = ms; + if (tokens < 1) return fail(429, "too many sign-in requests. Try again in a minute."); + tokens -= 1; + const chunks: Buffer[] = []; + let size = 0; + req.on("data", (c: Buffer) => { + size += c.length; + if (size <= MAX_BODY) chunks.push(c); + }); + req.on("end", () => { + if (size > MAX_BODY) return fail(413, "request too large"); + let key: unknown; + try { + key = (JSON.parse(Buffer.concat(chunks).toString("utf8")) as { key?: unknown }).key; + } catch { + return fail(400, "send JSON: {\"key\": \"0x...\"}"); + } + if (!isPublicKey(key)) return fail(400, "key must be a 32-byte Ed25519 public key in hex"); + void (async () => { + const body = await signKeyRequest(account, key, now()); + const r = await createKeyRequest(body, api, opts.fetch); + // Drop requests that can no longer be approved, so the map stays small. + for (const [t, d] of issued) if (d < now()) issued.delete(t); + issued.set(r.token, body.deadline); + send(200, { token: r.token, deeplinkUrl: r.deeplinkUrl, deadline: body.deadline, requestFid: account.fid }); + })().catch((e: Error) => fail(502, e.message)); + }); + return; + } + fail(405, "method not allowed"); + }; +} + +// For `./outpost client`: only the page this server serves may start a request. A browser sends +// Origin with every POST, so another website (or a link) cannot make this server sign for it. +export function sameOrigin(req: IncomingMessage): string | undefined { + if (req.method !== "POST") return undefined; + const origin = req.headers.origin; + const site = req.headers["sec-fetch-site"]; + if (site !== undefined && site !== "same-origin") return "requests must come from this page"; + const host = req.headers.host; + if (!origin || !host || (origin !== `http://${host}` && origin !== `https://${host}`)) return "requests must come from this page"; + return undefined; +} + +// For a society node (src/server/appsignin.ts behind Caddy at /app-signin): members only, by +// the same Bearer tokens as the gate, from the pages the gate allows (CORS). The page is served +// from another origin there, so the token, not the origin, is what keeps strangers out. +const ORIGINS = new Set(["https://spider333.github.io", "http://127.0.0.1:8381", "http://localhost:8381"]); + +export function societySignIn(opts: Omit & { members: () => Map }) { + const cors = (req: IncomingMessage): Record => { + const origin = req.headers.origin; + return origin && ORIGINS.has(origin) ? { vary: "Origin", "access-control-allow-origin": origin } : { vary: "Origin" }; + }; + const handler = signInHandler({ + ...opts, + headers: cors, + refuse: (req) => { + const token = /^Bearer (\S+)$/.exec(req.headers.authorization ?? "")?.[1]; + const known = token !== undefined && opts.members().has(createHash("sha256").update(token).digest("hex")); + return known ? undefined : "members only: this needs your access token for the society node"; + }, + }); + return (req: IncomingMessage, res: ServerResponse) => { + const path = (req.url ?? "/").split("?")[0]!; + if (req.method === "OPTIONS") { + const c = cors(req); + const allow = c["access-control-allow-origin"] ? { "access-control-allow-methods": "GET, POST, OPTIONS", "access-control-allow-headers": "Authorization, Content-Type" } : {}; + return void res.writeHead(204, { ...c, ...allow }).end(); + } + if (path !== "/app-signin" && !path.startsWith("/app-signin/")) { + return void res.writeHead(404, { "content-type": "application/json", ...cors(req) }).end(JSON.stringify({ error: "not found" })); + } + handler(req, res, path.slice("/app-signin".length)); + }; +} diff --git a/src/server/appsignin.ts b/src/server/appsignin.ts new file mode 100644 index 0000000..f29777e --- /dev/null +++ b/src/server/appsignin.ts @@ -0,0 +1,28 @@ +// "Approve in the Farcaster app" for a society node: signs members' key requests with the +// society's app account. It runs next to the gate, behind Caddy at /app-signin (see +// infra/society.sh). The rules are in src/server/appsignin-service.ts. +// +// - Members only: the gate's members file and Bearer tokens. +// - The app account's custody key is read from a file (mode 600) that `./outpost app-account +// --society` put on this server. It is never sent anywhere; the members' browsers send only +// public keys, and get back only Farcaster's approval link. +// - Nothing about a request is logged. +// +// Environment: OUTPOST_APPSIGNIN_ACCOUNT (/appsignin/account.json), OUTPOST_APPSIGNIN_MEMBERS +// (/gate/members), OUTPOST_APPSIGNIN_PORT (3392, 127.0.0.1 only). + +import { createServer } from "node:http"; +import { readFileSync } from "node:fs"; +import { privateKeyToAccount } from "viem/accounts"; +import type { AppAccount } from "../core/appsignin.js"; +import { societySignIn } from "./appsignin-service.js"; +import { membersFrom } from "./sponsor-service.js"; + +const account = JSON.parse(readFileSync(process.env.OUTPOST_APPSIGNIN_ACCOUNT ?? "/appsignin/account.json", "utf8")) as AppAccount; +if (!(Number.isSafeInteger(account.fid) && account.fid > 0) || !/^0x[0-9a-f]{64}$/i.test(account.custodyKey) || privateKeyToAccount(account.custodyKey).address.toLowerCase() !== account.address?.toLowerCase()) { + throw new Error("the app account file must hold fid, custodyKey and the matching address"); +} +const port = Number(process.env.OUTPOST_APPSIGNIN_PORT ?? 3392); +const server = createServer(societySignIn({ account: () => account, members: membersFrom(process.env.OUTPOST_APPSIGNIN_MEMBERS ?? "/gate/members") })); +for (const sig of ["SIGTERM", "SIGINT"] as const) process.on(sig, () => process.exit(0)); +server.listen(port, "127.0.0.1", () => console.log(`app-signin: app account FID ${account.fid}${account.sponsor ? ", sponsoring the fee" : ""}; 127.0.0.1:${port}`)); diff --git a/src/server/serve.ts b/src/server/serve.ts index 523c1f7..a708b8f 100644 --- a/src/server/serve.ts +++ b/src/server/serve.ts @@ -8,6 +8,10 @@ import { request as httpsRequest } from "node:https"; import { readFile, stat } from "node:fs/promises"; import { extname, join, normalize, resolve, sep } from "node:path"; import { fileURLToPath } from "node:url"; +import { loadAppAccount } from "../cli/store.js"; +import { sameOrigin, signInHandler } from "./appsignin-service.js"; +import { DEFAULT_OPTIMISM_RPC } from "../core/chain.js"; +import { checkNodeFor, DEFAULT_CHECK_NODE } from "../core/secondnode.js"; const here = fileURLToPath(new URL(".", import.meta.url)); const root = resolve(process.env.OUTPOST_WEB_ROOT ?? join(here, "web")); @@ -15,6 +19,15 @@ const node = new URL(process.env.OUTPOST_NODE_URL ?? "http://127.0.0.1:3381"); const nodeAuth = process.env.OUTPOST_NODE_USER ? "Basic " + Buffer.from(`${process.env.OUTPOST_NODE_USER}:${process.env.OUTPOST_NODE_PASSWORD ?? ""}`).toString("base64") : undefined; const host = process.env.OUTPOST_BIND ?? "127.0.0.1"; const port = Number(process.env.OUTPOST_PORT ?? 8381); +// A node that is not your own (./outpost client --public or --node ): Farcaster's public +// node or someone else's. The page says so, and its key checks go through this server too, so +// the browser still talks only to 127.0.0.1. +const remote = process.env.OUTPOST_NODE_REMOTE === "1"; +const optional = (v: string | undefined, fallback: string) => (v === "off" || v === "" ? undefined : new URL(v ?? fallback)); +const checkNode = remote ? optional(process.env.OUTPOST_CHECK_NODE, checkNodeFor(node.href)) : undefined; +const rpc = remote ? optional(process.env.OUTPOST_OPTIMISM_RPC, DEFAULT_OPTIMISM_RPC) : undefined; +const CHECK = "/outpost/check"; +const RPC = "/outpost/optimism"; const TYPES: Record = { ".html": "text/html; charset=utf-8", @@ -34,12 +47,13 @@ const SECURITY_HEADERS = { "content-security-policy": "default-src 'self'; img-src * data:; style-src 'self'; script-src 'self'; connect-src 'self' https:; frame-ancestors 'none'; base-uri 'none'", }; -function proxy(req: IncomingMessage, res: ServerResponse) { - const target = new URL(req.url ?? "/", node); +// Forwards the request to base + path; path keeps its query string. +function proxy(req: IncomingMessage, res: ServerResponse, base = node, path = req.url ?? "/", auth = nodeAuth) { + const target = new URL(base.href.replace(/\/+$/, "") + path); const headers: Record = {}; if (req.headers["content-type"]) headers["content-type"] = req.headers["content-type"]; if (req.headers["content-length"]) headers["content-length"] = req.headers["content-length"]; - if (nodeAuth) headers["authorization"] = nodeAuth; + if (auth) headers["authorization"] = auth; const send = target.protocol === "https:" ? httpsRequest : httpRequest; const up = send(target, { method: req.method, headers, timeout: 30_000 }, (r) => { res.writeHead(r.statusCode ?? 502, { "content-type": r.headers["content-type"] ?? "application/json", "cache-control": "no-store" }); @@ -48,7 +62,7 @@ function proxy(req: IncomingMessage, res: ServerResponse) { up.on("timeout", () => up.destroy(new Error("node did not answer in 30 s"))); up.on("error", (e) => { if (!res.headersSent) res.writeHead(502, { "content-type": "application/json" }); - res.end(JSON.stringify({ error: "cannot reach node", error_detail: `${node.origin}: ${e.message}` })); + res.end(JSON.stringify({ error: "cannot reach node", error_detail: `${base.origin}: ${e.message}` })); }); req.pipe(up); } @@ -79,10 +93,74 @@ async function serveStatic(req: IncomingMessage, res: ServerResponse) { // Which network this client talks to. Mainnet unless testing against a local devnet. const network = process.env.OUTPOST_NETWORK === "devnet" ? "devnet" : "mainnet"; +// "Approve in the Farcaster app": the app account (`./outpost app-account`) signs key requests +// here, and this server, not the page, talks to Farcaster's endpoint. OUTPOST_SIGNIN_API points +// it at a stand-in for tests. +const SIGNIN = "/outpost/app-signin"; +const signIn = signInHandler({ + account: () => { + try { + return loadAppAccount(); + } catch { + return undefined; // unreadable: as if none was set up + } + }, + api: process.env.OUTPOST_SIGNIN_API || undefined, + refuse: sameOrigin, +}); + +// Forwards one JSON-RPC call (or a batch) that only reads, such as eth_call; anything that sends +// a transaction is refused. +const READ_METHODS = new Set(["eth_call", "eth_chainId", "eth_blockNumber", "eth_getBlockByNumber", "eth_getCode", "eth_getBalance", "eth_getTransactionCount", "eth_getTransactionReceipt", "net_version"]); +async function readOnlyRpc(req: IncomingMessage, res: ServerResponse, base: URL) { + const chunks: Buffer[] = []; + let size = 0; + for await (const c of req as AsyncIterable) { + size += c.length; + if (size > 65536) { + res.writeHead(413, { "content-type": "application/json" }).end(JSON.stringify({ error: "request too large" })); + return; + } + chunks.push(c); + } + const body = Buffer.concat(chunks).toString("utf8"); + let calls: unknown; + try { + calls = JSON.parse(body); + } catch { + calls = undefined; + } + const list = Array.isArray(calls) ? calls : [calls]; + if (!list.length || !list.every((c) => typeof c === "object" && c !== null && READ_METHODS.has((c as { method?: unknown }).method as string))) { + res.writeHead(403, { "content-type": "application/json" }).end(JSON.stringify({ error: "only read calls are forwarded to Optimism" })); + return; + } + const up = (base.protocol === "https:" ? httpsRequest : httpRequest)(base, { method: "POST", headers: { "content-type": "application/json", "content-length": String(Buffer.byteLength(body)) }, timeout: 30_000 }, (r) => { + res.writeHead(r.statusCode ?? 502, { "content-type": r.headers["content-type"] ?? "application/json", "cache-control": "no-store" }); + r.pipe(res); + }); + up.on("timeout", () => up.destroy(new Error("Optimism RPC did not answer in 30 s"))); + up.on("error", (e) => { + if (!res.headersSent) res.writeHead(502, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "cannot reach Optimism RPC", error_detail: `${base.origin}: ${e.message}` })); + }); + up.end(body); +} + +function config() { + if (remote) return { network, node: { url: node.href.replace(/\/+$/, ""), farcaster: node.origin === new URL(DEFAULT_CHECK_NODE).origin, check: checkNode?.origin ?? "", rpc: rpc?.href ?? "" } }; + return network === "devnet" ? { network, checkNode: process.env.OUTPOST_CHECK_NODE ?? "" } : { network }; +} + const server = createServer((req, res) => { - if (req.url === "/outpost-config.json") { - res.writeHead(200, { "content-type": "application/json", "cache-control": "no-store" }).end(JSON.stringify({ network })); + const path = (req.url ?? "/").split("?")[0]!; + if (path === SIGNIN || path.startsWith(`${SIGNIN}/`)) signIn(req, res, path.slice(SIGNIN.length)); + else if (req.url === "/outpost-config.json") { + res.writeHead(200, { "content-type": "application/json", "cache-control": "no-store" }).end(JSON.stringify(config())); } else if (req.url?.startsWith("/v1/")) proxy(req, res); + // The second node is only read, and so is Optimism. + else if (checkNode && req.method === "GET" && req.url?.startsWith(`${CHECK}/v1/`)) proxy(req, res, checkNode, req.url.slice(CHECK.length), undefined); + else if (rpc && req.method === "POST" && path === RPC) void readOnlyRpc(req, res, rpc); else if (req.method === "GET" || req.method === "HEAD") void serveStatic(req, res); else res.writeHead(405).end(); }); @@ -90,5 +168,16 @@ const server = createServer((req, res) => { server.listen(port, host, () => { console.log(`client: http://${host === "0.0.0.0" ? "localhost" : host}:${port}`); console.log(`node: ${node.origin} (requests to /v1/* are forwarded there)`); + if (remote) { + console.log(` this is not your own node: it sees what you read and post, and can withhold or delay messages`); + console.log(`second node for gasless keys: ${checkNode?.origin ?? "off"}; Optimism RPC: ${rpc?.origin ?? "off"} (both through this server)`); + } if (network !== "mainnet") console.log(`network: ${network} (testing only)`); + let app; + try { + app = loadAppAccount(); + } catch { + console.log("app account: the file from ./outpost app-account cannot be read, so the page cannot offer that sign-in"); + } + if (app) console.log(`app account: FID ${app.fid} signs "Approve in the Farcaster app" requests (via ${process.env.OUTPOST_SIGNIN_API || "https://api.farcaster.xyz"})`); }); diff --git a/src/web/app.ts b/src/web/app.ts index 53edc59..515db03 100644 --- a/src/web/app.ts +++ b/src/web/app.ts @@ -3,7 +3,7 @@ // By default the page and the node share an origin: `./outpost client` serves this page and // forwards /v1/* to the node, so no node port has to be opened to the internet. -import { zeroAddress, type EIP1193Provider } from "viem"; +import { bytesToHex, zeroAddress, type EIP1193Provider } from "viem"; import { optimism } from "viem/chains"; import { FarcasterNetwork } from "@farcaster/core"; import { @@ -63,8 +63,23 @@ import { type Custody, type FeedItem, type HexString, + type Profile, + extractMentions, + mapLimit, + mentionQuery, + notificationsFor, + notificationText, + seenUpTo, + unreadCount, + generatePrivateKey, + publicKeyOf, + SignInClient, + requestFidOf, + waitFor, + type Notification, } from "../core/index.js"; import { httpsUrl, initial, isImageEmbed } from "./media.js"; +import { encode as qrEncode } from "uqr"; const KEY_STORAGE = "outpost.appkey"; const CHARTER_STORAGE = "outpost.charter"; @@ -74,6 +89,8 @@ const RPC_STORAGE = "outpost.rpc"; const CHECKNODE_STORAGE = "outpost.checknode"; const PICTURES_STORAGE = "outpost.pictures"; const APPROVALS_STORAGE = "outpost.approvals"; +// Per FID: the time up to which notifications were marked read. +const seenStorage = (fid: number) => `outpost.notifs.seen.${fid}`; // Set by scripts/build.mjs: the git commit this bundle was built from. declare const __OUTPOST_COMMIT__: string; @@ -158,22 +175,38 @@ try { inviteError = `Invite link not used: ${errText(e)}`; } const society = store(SOCIETY_STORAGE); +// What the local server says about itself. A static host (GitHub Pages, Caddy for phones) has no +// such file: mainnet, your own node. +type ServerConfig = { network?: string; checkNode?: string; node?: RemoteNode }; +// ./outpost client --public (or --node ): this page's origin forwards to a node that is not +// yours. Reads and writes still go only to this origin, and so do the key checks, which the local +// server forwards to the second node and Optimism. +type RemoteNode = { url: string; farcaster: boolean; check: string; rpc: string }; +const serverConfig: ServerConfig = await fetch("/outpost-config.json") + .then((r) => (r.ok ? (r.json() as Promise) : {})) + .catch(() => ({})); +const remote = society ? undefined : serverConfig.node; if (invite && invite.node === society?.node && invite.token === society.token) invite = undefined; // already in use // With no node of your own, Farcaster's public node can be read, never written to: a page may only // send it plain GETs (no token, no POST), and no app key is loaded, so no button that writes is shown. // A society node, once chosen, comes first. -const publicNode = !society && store(PUBLIC_STORAGE) === true; -const source = society ? "the society node" : publicNode ? "Farcaster's public node" : "your node"; +const publicNode = !society && !remote && store(PUBLIC_STORAGE) === true; +const remoteName = remote ? (remote.farcaster ? "Farcaster's public node" : `the node at ${new URL(remote.url).host}`) : ""; +const source = society ? "the society node" : remote ? remoteName : publicNode ? "Farcaster's public node" : "your node"; // An invite opened in this tab while the page is open only changes the fragment: start over to read it. addEventListener("hashchange", () => { if (/(^#|&)(node|token)=/.test(location.hash)) location.reload(); }); // Key and custody checks read Optimism through this RPC, never through the node. "" turns them off. -const rpc = store(RPC_STORAGE) ?? DEFAULT_OPTIMISM_RPC; +const RPC_VIA_SERVER = `${location.origin}/outpost/optimism`; +const CHECK_VIA_SERVER = `${location.origin}/outpost/check`; +const rpc = store(RPC_STORAGE) ?? (remote ? (remote.rpc && RPC_VIA_SERVER) : DEFAULT_OPTIMISM_RPC); const chain = rpc ? new Optimism(rpc) : undefined; // Gasless keys have no onchain record, so a second, independent node is asked. "" turns it off; // a second node on the same origin as the node being read is skipped. -const checkUrl = store(CHECKNODE_STORAGE) ?? DEFAULT_CHECK_NODE; +const checkUrl = store(CHECKNODE_STORAGE) ?? (remote ? (remote.check && CHECK_VIA_SERVER) : DEFAULT_CHECK_NODE); +// The node behind it, for what the page says. +const checkName = remote && checkUrl === CHECK_VIA_SERVER ? remote.check : checkUrl; const readUrl = society?.node ?? (publicNode ? DEFAULT_CHECK_NODE : location.origin); const second = checkUrl ? secondNode(checkUrl, readUrl) : undefined; const verifier = new Verifier(chain, second); @@ -221,13 +254,14 @@ const approvals = approvalCache( let draft: Array = []; const names = new Map(); // Profile pictures and image embeds load from their own hosts, not from the node. -let pictures = store(PICTURES_STORAGE) ?? true; +// Off by default on a node that is not yours, so the page then loads nothing but 127.0.0.1. +let pictures = store(PICTURES_STORAGE) ?? !remote; // ---- node status async function checkNode() { const pill = $("node-status"); - const which = society ? `society node ${new URL(society.node).host}` : publicNode ? "public node, read-only," : "node"; + const which = society ? `society node ${new URL(society.node).host}` : remote ? (remote.farcaster ? "public node" : `node ${new URL(remote.url).host}`) : publicNode ? "public node, read-only," : "node"; try { const info = await hub.info(); const { synced, maxDelay } = syncState(info); @@ -279,7 +313,7 @@ async function showUnverified() { verifier.showUnverified = true; verifier.reset(); feed = new FeedBuilder(hub); - await loadFeed(); + await Promise.all([loadFeed(), refreshNotifs()]); } // ---- node settings @@ -296,19 +330,24 @@ function wireNodeSettings() { }); $("soc-current").textContent = society ? `Using the society node ${society.node}.` - : publicNode + : remote + ? `Using ${remoteName} (${remote.url}) through ./outpost client on this computer. It is not your own node.` + : publicNode ? `Reading Farcaster's public node ${DEFAULT_CHECK_NODE}, read-only.` : `Using your own node (this page's origin, ${location.origin}).`; - $("rpc-url").value = rpc; - $("check-node").value = checkUrl; + $("rpc-url").value = remote && rpc === RPC_VIA_SERVER ? remote.rpc : rpc; + $("check-node").value = checkName; + if (remote) $("check-node").placeholder = remote.check || "https://:3381"; $("check-note").textContent = !checkUrl ? 'The second-node check is off: casts signed with gasless keys say "signer not verified".' : !second ? "The second node is the node you read from, so the gasless-key check is skipped. Choose a different node." - : `Gasless keys are checked with ${checkUrl}. It learns which accounts you look at (one request per account), not which casts you read. A fake cast now needs both nodes to lie.`; + : `Gasless keys are checked with ${checkName}${checkName === checkUrl ? "" : ", through ./outpost client"}. It learns which accounts you look at (one request per account), not which casts you read. A fake cast now needs both nodes to lie.`; $("node-note").textContent = society ? `Reads and writes go to the society node ${society.node}.` - : publicNode + : remote + ? `Reads and writes go to ${remoteName} ${remote.url}, through ./outpost client on this computer.` + : publicNode ? `Reads go to Farcaster's public node ${DEFAULT_CHECK_NODE}; nothing is written.` : "Reads and writes go only to your own node."; $("soc-save").addEventListener("click", () => @@ -363,15 +402,17 @@ function askToJoin(inv: Society): Promise { // ---- connect -type ConnectStep = "choose" | "app" | "create" | "phrase" | "confirm"; +type ConnectStep = "choose" | "app" | "approve" | "create" | "phrase" | "confirm"; function showConnectStep(step: ConnectStep) { + if (step !== "approve") stopApproval(); $("app-box").hidden = step !== "app"; + $("approve-box").hidden = step !== "approve"; $("create-box").hidden = step !== "create"; $("phrase-box").hidden = step !== "phrase"; $("confirm-box").hidden = step !== "confirm"; $("connect-choices").hidden = step === "confirm"; - for (const [id, s] of [["new-account", "create"], ["app-user", "app"], ["use-phrase", "phrase"]] as const) { + for (const [id, s] of [["new-account", "create"], ["app-user", "app"], ["use-phrase", "phrase"], ["use-app", "approve"]] as const) { $(id).setAttribute("aria-expanded", String(step === s)); } } @@ -404,6 +445,141 @@ async function authorize(custody: Custody, account: AccountInfo) { say(confirmed ? `App key added for FID ${k.fid}. Your node shows it as active.` : `App key sent for FID ${k.fid}; your node does not show it yet. Check the key panel in a minute.`); renderAccount(); await loadFeed(k.fid); + void refreshNotifs().catch(() => undefined); +} + +// ---- approve in the Farcaster app (appsignin.ts) + +// The page never talks to Farcaster: its own Outpost server, or the society node, signs the +// request with its app account and fetches the approval link. The new key is made here. +const signIn = society + ? new SignInClient(`${society.node.replace(/\/+$/, "")}/app-signin`, { authorization: `Bearer ${society.token}` }) + : new SignInClient("/outpost/app-signin"); +let approval: { cancelled: boolean } | undefined; + +function stopApproval() { + if (approval) approval.cancelled = true; + approval = undefined; + $("approve-qr-box").hidden = true; + $("approve-qr").replaceChildren(); +} + +// The QR code as SVG drawn here, dark squares on white, from uqr (MIT, bundled, no requests). +function qrSvg(text: string): SVGSVGElement { + const { data, size } = qrEncode(text, { ecc: "M", border: 2 }); + const ns = "http://www.w3.org/2000/svg"; + const svg = document.createElementNS(ns, "svg"); + svg.setAttribute("viewBox", `0 0 ${size} ${size}`); + svg.setAttribute("shape-rendering", "crispEdges"); + svg.setAttribute("aria-hidden", "true"); + let d = ""; + data.forEach((row, y) => row.forEach((on, x) => (d += on ? `M${x} ${y}h1v1h-1z` : ""))); + const path = document.createElementNS(ns, "path"); + path.setAttribute("d", d); + path.setAttribute("fill", "#000000"); + svg.append(path); + return svg; +} + +async function openApprove() { + showConnectStep("approve"); + const info = $("approve-info"); + const go = $("approve-go"); + go.hidden = true; + info.textContent = "Checking whether this Outpost can ask the Farcaster app…"; + const s = await signIn.service(); + if (!s) { + info.textContent = society + ? "This society node does not offer approval in the Farcaster app. Ask its host, or use another way to connect." + : "This page is not served by an Outpost client server, so it cannot ask the Farcaster app. Run ./outpost client on your computer and open the address it prints."; + return; + } + if (!s.configured) { + info.textContent = society + ? "This society node has no app account set up, so it cannot ask the Farcaster app yet. Its host sets one up once." + : 'This Outpost has no app account yet, so it cannot ask the Farcaster app. Whoever runs it sets one up once, in a terminal: ./outpost app-account (see "Approve in the Farcaster app" in the README). Use an account made for this, never a main account.'; + return; + } + const name = await feed.profile(s.fid!).then((p) => (p.username ? ` (@${p.username})` : ""), () => ""); + info.textContent = `The request comes from the app account FID ${s.fid}${name}, which ${society ? "this society node" : "this Outpost"} holds. ${s.sponsored ? "It offers to pay the onchain fee." : "The Farcaster app shows any onchain fee before you approve; you pay it there."} Your recovery phrase stays in the app.`; + go.hidden = false; +} + +async function approveInApp() { + stopApproval(); + const run = { cancelled: false }; + approval = run; + const state = $("approve-state"); + const priv = generatePrivateKey(); + const pub = bytesToHex(await publicKeyOf(priv)); + const req = await signIn.start(pub); + if (run.cancelled) return; + $("approve-qr").replaceChildren(qrSvg(req.deeplinkUrl)); + $("approve-link").href = req.deeplinkUrl; + $("approve-go").hidden = true; + $("approve-qr-box").hidden = false; + state.textContent = "Scan this with your phone's camera, then approve in the Farcaster app. Waiting…"; + $("approve-link").focus(); + let userFid: number | undefined; + for (;;) { + await new Promise((r) => setTimeout(r, 2000)); + if (run.cancelled) return; + if (Date.now() / 1000 > req.deadline + 30) throw new Error("The request expired before it was approved. Nothing was added. Try again."); + const st = await signIn.state(req.token).catch((e: Error) => ({ state: "error" as const, error: e.message })); + if (run.cancelled) return; + if (st.state === "expired") throw new Error("The request expired before it was approved. Nothing was added. Try again."); + if (st.state === "error") state.textContent = `Could not check the request (${"error" in st ? st.error : ""}). Trying again…`; + else if (st.state === "pending") state.textContent = "Waiting for you to approve in the Farcaster app…"; + else if (st.state === "approved") state.textContent = "Approved. Waiting for the onchain transaction…"; + else if (st.state === "completed" && "userFid" in st && st.userFid) { + userFid = st.userFid; + break; + } + } + state.textContent = `Added onchain for FID ${userFid}. Waiting for ${source} to show the key…`; + const seen = await waitFor(async () => (run.cancelled ? true : hub.signer(userFid!, pub)), { timeoutMs: 5 * 60_000, intervalMs: 3000 }); + if (run.cancelled) return; + if (!seen || seen === true) { + throw new Error( + `The Farcaster app reports a key as added, but ${source} does not list the key this page made, after 5 minutes. Nothing was saved. If the node is just slow, try again later. If not, a different key may have been added: remove the newest connected app in the Farcaster app (Settings, then Advanced, then Manage connected apps).`, + ); + } + if (seen.source !== "SIGNER_SOURCE_ONCHAIN" || requestFidOf(seen) !== req.requestFid) { + throw new Error(`${source[0]!.toUpperCase()}${source.slice(1)} lists the key, but not as the onchain key this request asked for. Nothing was saved.`); + } + const k: AppKey = { fid: userFid!, privateKey: bytesToHex(priv), publicKey: pub, requestFid: req.requestFid, scopes: [], ttl: 0, network, onchain: true }; + store(KEY_STORAGE, k); + key = k; + verifier.trust(k.fid, k.publicKey); + approval = undefined; + $("approve-qr-box").hidden = true; + say(`Signed in as FID ${k.fid} with a key approved in the Farcaster app.`); + renderAccount(); + await loadFeed(k.fid); + void refreshNotifs().catch(() => undefined); +} + +function wireApprove() { + $("use-app").addEventListener("click", () => void openApprove().catch((e) => say(errText(e), true))); + $("app-to-approve").addEventListener("click", () => void openApprove().catch((e) => say(errText(e), true))); + $("approve-go").addEventListener("click", (e) => + busy(e.currentTarget as HTMLButtonElement, "Asking…", async () => { + try { + await approveInApp(); + } catch (err) { + $("approve-state").textContent = errText(err); + $("approve-qr-box").hidden = true; + $("approve-go").hidden = false; + approval = undefined; + throw err; + } + }), + ); + $("approve-cancel").addEventListener("click", () => { + stopApproval(); + $("approve-go").hidden = false; + say("Approval cancelled. Nothing was saved."); + }); } // ---- create an account (paid by the society's sponsor) @@ -538,6 +714,7 @@ function wireCreate() { function wireConnect() { wireCreate(); + wireApprove(); $("use-wallet").addEventListener("click", (e) => busy(e.currentTarget as HTMLButtonElement, "Waiting for wallet…", async () => { const eth = walletProvider(); @@ -580,19 +757,31 @@ async function renderAccount() { $("connect").hidden = logged || publicNode; $("compose").hidden = !logged; $("key").hidden = !logged; + $("notifs").hidden = !logged; $("who").textContent = key ? `FID ${key.fid}` : ""; + if (!key) notifs = []; + renderUnread(); if (!key) { showConnectStep("choose"); return; } $("k-fid").textContent = String(key.fid); $("k-pub").textContent = key.publicKey; + $("k-kind").textContent = key.onchain + ? "Onchain key, approved in the Farcaster app: full access, no expiry. Outpost cannot revoke it." + : "Outpost's own app key: cast, react and follow only. Expires 30 days after last use."; + $("k-note").textContent = key.onchain + ? "The private part stays in this browser. To remove the key from your account, use the Farcaster app: Settings, then Advanced, then Manage connected apps. The button below only forgets it here." + : "The private part stays in this browser. Revoking needs no recovery phrase: the key signs its own removal."; + $("revoke").textContent = key.onchain ? "Forget this key in this browser" : "Revoke this key (uninstall)"; $("k-status").textContent = "checking…"; try { const s = await hub.signer(key.fid, key.publicKey); $("k-status").textContent = s - ? `active${s.expiresAt ? `, expires ${new Date(s.expiresAt * 1000).toLocaleDateString()} unless used` : ""}` - : "not active (revoked or expired). Revoke to remove it from this browser."; + ? `active${s.expiresAt ? `, expires ${new Date(s.expiresAt * 1000).toLocaleDateString()} unless used` : key.onchain ? ", no expiry" : ""}` + : key.onchain + ? "not active (removed in the Farcaster app). Forget it to sign in again." + : "not active (revoked or expired). Revoke to remove it from this browser."; myFollowing = new Set(await followingOf(hub, key.fid)); } catch (e) { $("k-status").textContent = `unknown (${errText(e)})`; @@ -603,6 +792,16 @@ function wireKey() { $("revoke").addEventListener("click", (e) => busy(e.currentTarget as HTMLButtonElement, "Revoking…", async () => { if (!key) return; + if (key.onchain) { + // An onchain key can only be removed by the account's owner, in the Farcaster app. + if (!confirm("Forget this key in this browser? It stays on your account until you remove it in the Farcaster app: Settings, then Advanced, then Manage connected apps.")) return; + store(KEY_STORAGE, null); + key = undefined; + say("Forgot the key in this browser. To remove it from your account, use the Farcaster app: Settings, then Advanced, then Manage connected apps."); + renderAccount(); + renderFeed(); + return; + } if (!confirm("Revoke this app key? This client will no longer be able to post for you.")) return; // Same rule as `./outpost cli uninstall`: the stored key is the only way to revoke it without // the recovery phrase, and a node that is behind says "not found" for a live key. @@ -635,14 +834,168 @@ function setReply(target?: { fid: number; hash: HexString; name: string }) { } } +// ---- @mentions in the composer + +// Signed profiles seen so far (feed authors, accounts you follow), for the suggestions. +const people = new Map(); +// Names picked from the suggestions. Only these become mentions when posting (mentions.ts). +let picks = new Map(); +let suggestions: Profile[] = []; +let activeSuggestion = -1; +let followedProfiles: Promise | undefined; +let lookupTimer: ReturnType | undefined; + +function remember(p: Profile | undefined) { + if (p?.username) people.set(p.fid, p); +} + +// Profiles of up to 300 accounts you follow, read once, the first time you type "@". +function loadFollowedProfiles(): Promise { + if (!myFollowing.size) return Promise.resolve(); // not read yet: try again on the next "@" + followedProfiles ??= mapLimit([...myFollowing].filter((f) => !people.has(f)).slice(0, 300), 8, (f) => + feed.profile(f).then(remember, () => undefined), + ).then(() => undefined); + return followedProfiles; +} + +function closeSuggestions() { + suggestions = []; + activeSuggestion = -1; + const list = $("mention-list"); + list.hidden = true; + list.replaceChildren(); + const text = $("text"); + text.setAttribute("aria-expanded", "false"); + text.removeAttribute("aria-activedescendant"); +} + +function renderSuggestions() { + const list = $("mention-list"); + const text = $("text"); + if (!suggestions.length) return closeSuggestions(); + list.replaceChildren( + ...suggestions.map((p, i) => + h( + "li", + { + id: `mention-${i}`, + role: "option", + "aria-selected": String(i === activeSuggestion), + // mousedown, not click: the textarea keeps focus and its cursor. + onmousedown: (e: Event) => { + e.preventDefault(); + pickSuggestion(i); + }, + }, + h("span", { class: "mn" }, `@${p.username}`), + p.displayName ? h("span", { class: "md" }, p.displayName) : null, + myFollowing.has(p.fid) ? h("span", { class: "md" }, "you follow") : null, + ), + ), + ); + list.hidden = false; + text.setAttribute("aria-expanded", "true"); + if (activeSuggestion >= 0) text.setAttribute("aria-activedescendant", `mention-${activeSuggestion}`); + else text.removeAttribute("aria-activedescendant"); + $("mention-note").textContent = `${suggestions.length} ${suggestions.length === 1 ? "person" : "people"} to mention. Up and down arrows to choose, Enter to pick, Escape to close.`; +} + +function matches(query: string): Profile[] { + const q = query.toLowerCase(); + const rank = (p: Profile) => (myFollowing.has(p.fid) ? 0 : 2) + (p.username!.toLowerCase().startsWith(q) ? 0 : 1); + return [...people.values()] + .filter((p) => p.username!.toLowerCase().startsWith(q) || (q && p.displayName?.toLowerCase().includes(q))) + .sort((a, b) => rank(a) - rank(b) || a.username!.localeCompare(b.username!)) + .slice(0, 6); +} + +// A full username nobody here has loaded is looked up on the node, and kept only if the account's +// own signed profile carries that name. +async function lookUp(query: string) { + const fid = await hub.fidByUsername(query).catch(() => undefined); + if (!fid) return; + const p = await feed.profile(fid).catch(() => undefined); + if (p?.username?.toLowerCase() !== query.toLowerCase()) return; + remember(p); + const now = mentionQuery($("text").value, $("text").selectionStart); + if (now?.query.toLowerCase() === query.toLowerCase()) { + suggestions = matches(query); + activeSuggestion = suggestions.length ? 0 : -1; + renderSuggestions(); + } +} + +function suggest() { + const text = $("text"); + const q = mentionQuery(text.value, text.selectionStart); + clearTimeout(lookupTimer); + if (!q || text.selectionStart !== text.selectionEnd) return closeSuggestions(); + void loadFollowedProfiles().then(() => { + const again = mentionQuery(text.value, text.selectionStart); + if (again?.query === q.query && !suggestions.length && matches(q.query).length) { + suggestions = matches(q.query); + activeSuggestion = 0; + renderSuggestions(); + } + }); + suggestions = matches(q.query); + activeSuggestion = suggestions.length ? 0 : -1; + renderSuggestions(); + if (!suggestions.some((p) => p.username!.toLowerCase() === q.query.toLowerCase()) && q.query.length >= 2) { + lookupTimer = setTimeout(() => void lookUp(q.query), 300); + } +} + +function pickSuggestion(i: number) { + const p = suggestions[i]; + const text = $("text"); + const q = mentionQuery(text.value, text.selectionStart); + if (!p?.username || !q) return closeSuggestions(); + const insert = `@${p.username} `; + text.value = text.value.slice(0, q.start) + insert + text.value.slice(text.selectionStart); + const at = q.start + insert.length; + text.setSelectionRange(at, at); + picks.set(p.username.toLowerCase(), p.fid); + closeSuggestions(); + $("mention-note").textContent = `Mentioning @${p.username}.`; + text.dispatchEvent(new Event("input")); + text.focus(); +} + +function wireMentions() { + const text = $("text"); + text.addEventListener("input", suggest); + text.addEventListener("click", suggest); + text.addEventListener("blur", () => setTimeout(closeSuggestions, 150)); + text.addEventListener("keydown", (e) => { + if ($("mention-list").hidden) return; + if (e.key === "ArrowDown" || e.key === "ArrowUp") { + e.preventDefault(); + const n = suggestions.length; + activeSuggestion = (activeSuggestion + (e.key === "ArrowDown" ? 1 : n - 1)) % n; + renderSuggestions(); + document.getElementById(`mention-${activeSuggestion}`)?.scrollIntoView({ block: "nearest" }); + } else if ((e.key === "Enter" || e.key === "Tab") && activeSuggestion >= 0) { + e.preventDefault(); + pickSuggestion(activeSuggestion); + } else if (e.key === "Escape") { + e.preventDefault(); + closeSuggestions(); + } + }); +} + function wireCompose() { + wireMentions(); const text = $("text"); text.addEventListener("input", () => ($("count").textContent = `${new TextEncoder().encode(text.value).length} / 1024`)); $("post").addEventListener("click", (e) => busy(e.currentTarget as HTMLButtonElement, "Posting…", async () => { if (!key) return; - const r = await cast(hub, key, { text: text.value, replyTo }); + const m = extractMentions(text.value, [...picks].map(([name, fid]) => ({ name, fid }))); + const r = await cast(hub, key, { text: m.text, replyTo, ...(m.mentions.length ? { mentions: m.mentions, mentionsPositions: m.mentionsPositions } : {}) }); text.value = ""; + picks = new Map(); $("count").textContent = "0 / 1024"; setReply(); say(r.confirmed ? `Cast ${r.hash.slice(0, 10)} is on your node.` : `Cast ${r.hash.slice(0, 10)} was accepted for relay; not visible yet.`); @@ -747,13 +1100,13 @@ function keyTag(it: FeedItem): HTMLSpanElement | null { const k = it.app.keyCheck; if (k === "onchain" || k === "own" || it.signer.toLowerCase() === key?.publicKey.toLowerCase()) return null; if (k === "confirmed") { - return h("span", { class: "app-tag", title: `A gasless key: no onchain record, but the second node (${checkUrl}) lists it as this account's key. Faking it would need both nodes to lie.` }, "signer confirmed by second node"); + return h("span", { class: "app-tag", title: `A gasless key: no onchain record, but the second node (${checkName}) lists it as this account's key. Faking it would need both nodes to lie.` }, "signer confirmed by second node"); } if (k === "revoked") { return h("span", { class: "app-tag", title: "This account's key on Optimism, removed since. What it signed before stays valid on the network; it cannot sign anything new." }, "key revoked since"); } if (k === "unlisted") { - return h("span", { class: "key-tag", title: `The second node (${checkUrl}) does not list this key for this account. It was revoked since, or it is not the account's key: the node's API cannot tell which.` }, "key not listed"); + return h("span", { class: "key-tag", title: `The second node (${checkName}) does not list this key for this account. It was revoked since, or it is not the account's key: the node's API cannot tell which.` }, "key not listed"); } return h( "span", @@ -791,9 +1144,10 @@ function castCard(it: FeedItem): HTMLLIElement { }, () => undefined); } const embeds = it.embeds.filter((u) => /^https?:\/\//.test(u)); + if (!thread || thread.root.hash !== it.hash) actions.append(h("button", { type: "button", onclick: () => void busy(null, "", () => openThread(target, it.hash)) }, "Thread")); return h( "li", - { class: "cast" }, + { class: `cast${thread?.focus === it.hash ? " focus" : ""}`, id: `cast-${it.hash}`, tabindex: "-1" }, avatar(it), h( "div", @@ -827,13 +1181,40 @@ function renderFeed() { ), ); } - $("more").hidden = items.length === 0; - if (charter) $("feed-title").textContent = `"${charter.name}" society feed, through its lens Β· ${shown.length} of ${items.length} casts`; + $("more").hidden = items.length === 0 || !!thread; + $("thread-back").hidden = !thread; + if (thread) $("feed-title").textContent = `A cast by ${thread.author} and ${shown.length - 1} repl${shown.length === 2 ? "y" : "ies"}, oldest first`; + else if (charter) $("feed-title").textContent = `"${charter.name}" society feed, through its lens Β· ${shown.length} of ${items.length} casts`; else if (viewFid) $("feed-title").textContent = `Following feed of FID ${viewFid} Β· ${shown.length} of ${items.length} casts`; else $("feed-title").textContent = ""; } +// ---- a cast and its replies + +let thread: { root: { fid: number; hash: HexString }; focus?: HexString; author: string } | undefined; + +async function openThread(root: { fid: number; hash: HexString }, focus?: HexString) { + say(`Loading the cast and its replies from ${source}…`); + const [cast, replies] = await Promise.all([hub.castById(root.fid, root.hash), hub.repliesTo(root, { pageSize: 100 })]); + if (!cast) throw new Error(`${source[0]!.toUpperCase()}${source.slice(1)} does not have that cast, or it did not pass the checks.`); + const msgs = [cast, ...replies.items.sort((a, b) => a.data.timestamp - b.data.timestamp)]; + const loaded = await feed.items(msgs); + if (!loaded.length || loaded[0]!.hash !== cast.hash) throw new Error("That cast did not pass the key check, so it is not shown."); + const a = loaded[0]!.author; + thread = { root, focus: focus ?? root.hash, author: a?.username ? `@${a.username}` : `fid ${root.fid}` }; + items = loaded; + for (const it of items) remember(it.author); + chipState.clear(); + renderFeed(); + showVerification(); + say(`Showing a cast and ${items.length - 1} repl${items.length === 2 ? "y" : "ies"}.`); + const el = document.getElementById(`cast-${thread.focus}`) ?? $("feed-h"); + el.scrollIntoView({ block: "center" }); + el.focus({ preventScroll: true }); +} + async function loadFeed(fid?: number) { + thread = undefined; if (fid) viewFid = fid; const t0 = performance.now(); if (!charter && !viewFid) return; @@ -849,6 +1230,7 @@ async function loadFeed(fid?: number) { items = await feed.following(viewFid!, { limit: 100, perAuthor }); } appNames = await feed.appNames(items); + for (const it of items) remember(it.author); } finally { list.removeAttribute("aria-busy"); } @@ -857,6 +1239,126 @@ async function loadFeed(fid?: number) { say(`Loaded ${items.length} cast${items.length === 1 ? "" : "s"} in ${((performance.now() - t0) / 1000).toFixed(1)} s.`); } +// ---- notifications + +let notifs: Notification[] = []; +let notifShown = 10; +let notifLoading = false; + +function lastSeen(): number | undefined { + return key ? store(seenStorage(key.fid)) : undefined; +} + +function renderUnread() { + const n = unreadCount(notifs, lastSeen()); + $("notif-pill").hidden = !key; + $("notif-count").textContent = `${n} new`; + $("notif-pill").setAttribute("aria-label", `Notifications: ${n} new`); +} + +function excerpt(text: string | undefined): string { + const t = (text ?? "").replace(/\s+/g, " ").trim(); + return t.length > 140 ? `${t.slice(0, 139)}…` : t; +} + +function notifItem(n: Notification, seen: number | undefined): HTMLLIElement { + const open = + n.kind === "follow" + ? h("button", { type: "button", onclick: () => void busy(null, "", () => loadFeed(n.actors[0]!.fid)) }, "Show their feed") + : h( + "button", + { + type: "button", + onclick: () => + void busy(null, "", async () => { + // A reply opens under your cast; a mention opens the cast that mentions you. + const c = n.cast; + if (n.kind === "reply" && c?.parent && "hash" in c.parent) await openThread({ fid: c.parent.fid, hash: c.parent.hash as HexString }, c.hash); + else if (c) await openThread({ fid: c.fid, hash: c.hash }); + else if (n.target) await openThread({ fid: n.target.fid, hash: n.target.hash }); + }), + }, + "Open", + ); + const about = n.kind === "reply" || n.kind === "mention" ? n.cast?.text : n.target?.text; + const unread = n.time > (seen ?? 0); + return h( + "li", + { class: `notif${unread ? " unread" : ""}` }, + h( + "div", + { class: "notif-main" }, + h("span", { class: "notif-what" }, notificationText(n), unread ? h("span", { class: "sr-only" }, " (new)") : null, h("span", { class: "meta" }, ` Β· ${ago(n.time)}`)), + about ? h("p", { class: "notif-excerpt" }, excerpt(about)) : null, + ), + open, + ); +} + +function renderNotifs(pending = 0, failed: string[] = []) { + const seen = lastSeen(); + $("notif-list").replaceChildren(...notifs.slice(0, notifShown).map((n) => notifItem(n, seen))); + $("notif-more").hidden = notifs.length <= notifShown; + const notes: string[] = []; + if (pending) notes.push(`Still loading (${pending} of 5 parts left)…`); + if (failed.length) notes.push(`${source[0]!.toUpperCase()}${source.slice(1)} did not answer for: ${failed.join(", ")}. Refresh to try again.`); + if (!pending && !notifs.length && !failed.length) notes.push("Nothing yet."); + $("notif-status").textContent = notes.join(" "); + renderUnread(); +} + +async function refreshNotifs() { + if (!key || notifLoading) return; + notifLoading = true; + const fid = key.fid; + $("notifs").hidden = false; + try { + const r = await notificationsFor(feed, fid, { + onUpdate: (u) => { + if (key?.fid !== fid) return; + notifs = u.items; + renderNotifs(u.pending, u.failed); + }, + }); + if (key?.fid !== fid) return; + notifs = r.items; + for (const n of notifs) n.actors.forEach(remember); + renderNotifs(0, r.failed); + showVerification(); + } finally { + notifLoading = false; + } +} + +function wireNotifs() { + $("notif-refresh").addEventListener("click", (e) => busy(e.currentTarget as HTMLButtonElement, "Refreshing…", refreshNotifs)); + $("notif-read").addEventListener("click", () => { + if (!key) return; + store(seenStorage(key.fid), seenUpTo(notifs)); + renderNotifs(); + say("All notifications marked read."); + }); + $("notif-more").addEventListener("click", () => { + notifShown += 20; + renderNotifs(); + }); + $("notif-pill").addEventListener("click", () => setTimeout(() => $("notifs-h").focus(), 0)); + $("thread-back").addEventListener("click", () => + busy(null, "", async () => { + thread = undefined; + if (charter || viewFid) await loadFeed(); + else { + items = []; + renderFeed(); + } + }), + ); + // A light poll while the page is visible; a society node rate-limits each member. + setInterval(() => { + if (document.visibilityState === "visible") void refreshNotifs().catch(() => undefined); + }, 120_000); +} + function showPicturesNote() { $("pictures-note").textContent = pictures ? "Pictures load straight from where their owners host them; those sites see your IP address." : ""; } @@ -876,7 +1378,7 @@ function wireFeed() { void busy(null, "", async () => { const v = $("view").value.trim(); const fid = /^\d+$/.test(v) ? Number(v) : await hub.fidByUsername(v); - if (!fid) throw new Error(`No FID found for "${v}". Plain usernames only; .eth names need an Ethereum RPC on the node.`); + if (!fid) throw new Error(`No FID found for "${v}" on ${source}.`); perAuthor = 5; if (charter) leaveCharter(); await loadFeed(fid); @@ -1263,11 +1765,24 @@ function wireCharter() { // ---- start async function main() { - try { - const cfg = (await (await fetch("/outpost-config.json")).json()) as { network?: string }; - if (cfg.network === "devnet") network = FarcasterNetwork.DEVNET; - } catch { - // Served without Outpost's server (e.g. Caddy for phones): mainnet. + if (serverConfig.network === "devnet") { + network = FarcasterNetwork.DEVNET; + // Optimism mainnet and Farcaster's public node know nothing of devnet keys. Unless chosen + // under "Node and checks", key checks use the devnet's own second node, or are off. + let changed = false; + if (store(RPC_STORAGE) === undefined) changed = store(RPC_STORAGE, "") === "" || changed; + if (store(CHECKNODE_STORAGE) === undefined) changed = store(CHECKNODE_STORAGE, serverConfig.checkNode ?? "") !== undefined || changed; + if (changed) return location.reload(); + // A devnet usually has no second node this page may reach (it allows https only), so its + // gasless keys cannot be checked: show what they signed, marked "signer not verified". + if (!second) verifier.showUnverified = true; + } + if (remote) { + $("remote-name").textContent = remote.farcaster ? "Farcaster's public node" : `the node at ${new URL(remote.url).host}`; + $("remote-check").textContent = checkName + ? `Casts signed with gasless keys are checked against a second node, ${new URL(checkName).host}.${remote.farcaster && checkName === remote.check ? " Farcaster runs that one too, so it catches one node being wrong, not Farcaster itself." : ""}` + : 'The second-node check is off: casts signed with gasless keys say "signer not verified".'; + $("remote").hidden = false; } $("build").replaceChildren( __OUTPOST_COMMIT__ === "unknown" @@ -1281,12 +1796,14 @@ async function main() { wireFeed(); wireCharter(); wireVotes(); + wireNotifs(); await renderAccount(); if (invite) await askToJoin(invite); if (await checkNode()) { const start = key?.fid ?? (params.get("fid") ? Number(params.get("fid")) : undefined); if (savedLog) await showCharter(savedLog, "saved in this browser", true).catch((e) => say(errText(e), true)); else if (start) await loadFeed(start).catch((e) => say(errText(e), true)); + if (key) void refreshNotifs().catch((e) => say(errText(e), true)); else if (publicNode) say('Enter an FID or username to read a feed. This is read-only: to post, run your own node or join a society (see "Node and checks").'); else say('Enter an FID or username to read a feed. To post, see "Post from Outpost" below.'); } diff --git a/src/web/index.html b/src/web/index.html index c010c94..c290ab9 100644 --- a/src/web/index.html +++ b/src/web/index.html @@ -16,10 +16,18 @@

Outpost

node: checking +
+

@@ -63,11 +71,28 @@

Post from Outpost

+ + +
diff --git a/src/web/style.css b/src/web/style.css index 05835d4..1ca6b8d 100644 --- a/src/web/style.css +++ b/src/web/style.css @@ -109,6 +109,8 @@ h1, h2, h3 { text-wrap: balance; } .verify { margin: 2px 0 0; overflow-wrap: anywhere; } .verify:empty { display: none; } .verify button { min-height: 32px; padding: 2px 10px; font-size: 12px; margin-left: 4px; } +.remote { margin: 12px 0 0; padding: 6px 12px; border-left: 2px solid var(--warn); } +.remote > p { margin: 0; } #verify-status.bad { color: var(--bad); } #verify-status.warn { color: var(--warn); } @@ -264,6 +266,25 @@ code { background: var(--surface-2); border: 1px solid var(--line-subtle); borde .compose { padding: 12px; } .compose textarea { border: 0; background: transparent; padding: 6px 4px; min-height: 72px; font-size: 16px; } .compose textarea:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; } +.mention-list { list-style: none; margin: 4px 0 0; padding: 4px; border: 1px solid var(--line-strong); border-radius: var(--radius); background: var(--surface-2); max-height: 264px; overflow-y: auto; } +.mention-list li { display: flex; align-items: baseline; gap: 8px; min-height: 44px; padding: 10px 10px; border-radius: var(--radius-sm); cursor: pointer; overflow-wrap: anywhere; } +.mention-list li[aria-selected="true"], .mention-list li:hover { background: var(--hover); outline: 2px solid var(--focus); outline-offset: -2px; } +.mention-list .mn { color: var(--text); font-weight: 500; } +.mention-list .md { color: var(--muted); font-size: 13px; } +.notif-pill { color: var(--accent-hover); border-color: currentColor; text-decoration: none; min-height: 44px; padding: 0 12px 0 10px; } +.notif-pill:hover { background: var(--hover); } +.notif-list { list-style: none; margin: 8px 0 0; padding: 0; border-top: 1px solid var(--line-subtle); } +.notif-list:empty { border-top: 0; } +.notif { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; padding: 10px 0; border-bottom: 1px solid var(--line-subtle); overflow-wrap: anywhere; } +.notif-main { flex: 1 1 240px; min-width: 0; } +.notif-what { color: var(--text); } +.notif.unread .notif-what::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-right: 8px; vertical-align: middle; } +.notif-excerpt { color: var(--muted); font-size: 13px; margin: 2px 0 0; } +.notif button { min-height: 44px; } +.cast.focus { background: var(--hover); box-shadow: inset 3px 0 0 var(--accent); } +.qr { display: inline-block; background: #ffffff; padding: 12px; border-radius: var(--radius); margin: 8px 0; line-height: 0; } +.qr svg { width: 220px; height: 220px; max-width: 100%; display: block; } +.big-link { display: inline-flex; align-items: center; min-height: 44px; font-weight: 600; color: var(--accent-hover); } .compose .row { border-top: 1px solid var(--line-subtle); padding-top: 10px; margin-top: 6px; } .reply-to { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; font-size: 13px; color: var(--muted); margin: 0 0 6px; } .reply-to button { min-height: 32px; padding: 2px 10px; font-size: 12px; } diff --git a/test/appsignin.test.ts b/test/appsignin.test.ts new file mode 100644 index 0000000..bc876dd --- /dev/null +++ b/test/appsignin.test.ts @@ -0,0 +1,162 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createServer, type Server } from "node:http"; +import { SIGNED_KEY_REQUEST_TYPE, SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN } from "@farcaster/core"; +import { verifyMessage, verifyTypedData } from "viem"; +import { privateKeyToAccount } from "viem/accounts"; +import { REQUEST_SECONDS, signKeyRequest, SignInClient, type AppAccount, type HexString } from "../src/core/index.js"; +import { sameOrigin, signInHandler } from "../src/server/appsignin-service.js"; +import { fakeFarcaster } from "./fake-farcaster.js"; + +// Hardhat test account 2: public, never holds anything. +const CUSTODY = "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a" as HexString; +const APP: AppAccount = { fid: 777, custodyKey: CUSTODY, address: privateKeyToAccount(CUSTODY).address }; +const KEY = "0xd75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a" as HexString; + +test("the key request is a valid EIP-712 SignedKeyRequest from the app account, deadline ahead", async () => { + const now = Math.floor(Date.now() / 1000); + const b = await signKeyRequest(APP, KEY, now); + assert.equal(b.requestFid, 777); + assert.equal(b.key, KEY); + assert.equal(b.deadline, now + REQUEST_SECONDS); + assert.ok(b.deadline > now); + assert.ok( + await verifyTypedData({ + address: APP.address, + domain: SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN, + types: { SignedKeyRequest: SIGNED_KEY_REQUEST_TYPE }, + primaryType: "SignedKeyRequest", + message: { requestFid: 777n, key: KEY, deadline: BigInt(b.deadline) }, + signature: b.signature, + }), + ); + assert.equal(b.sponsorship, undefined); + const s = await signKeyRequest({ ...APP, sponsor: true }, KEY, now); + assert.equal(s.sponsorship?.sponsorFid, 777); + assert.ok(await verifyMessage({ address: APP.address, message: { raw: s.signature }, signature: s.sponsorship!.signature })); + await assert.rejects(signKeyRequest(APP, "0x1234" as HexString), /32-byte/); + await assert.rejects(signKeyRequest({ ...APP, address: "0x0000000000000000000000000000000000000001" }, KEY), /does not match/); +}); + +async function serve(handler: ReturnType): Promise<{ url: string; server: Server; bodies: string[] }> { + const bodies: string[] = []; + const server = createServer((req, res) => { + const end = res.end.bind(res); + res.end = ((chunk?: unknown, ...rest: unknown[]) => { + if (chunk) bodies.push(String(chunk)); + return end(chunk as never, ...(rest as [])); + }) as typeof res.end; + const path = (req.url ?? "/").split("?")[0]!; + handler(req, res, path.slice("/outpost/app-signin".length)); + }); + await new Promise((ok) => server.listen(0, "127.0.0.1", ok)); + return { url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, server, bodies }; +} + +test("sign-in over HTTP: same origin only, approval polled, the app account's key never sent", async () => { + const logs: string[] = []; + const log = console.log; + console.log = (...a: unknown[]) => void logs.push(a.join(" ")); + const fc = await fakeFarcaster({ appAddress: APP.address, userFid: 4242 }); + const { url, server, bodies } = await serve(signInHandler({ account: () => APP, api: fc.url, refuse: sameOrigin })); + try { + const page = new SignInClient(`${url}/outpost/app-signin`, { origin: url }); + assert.deepEqual(await page.service(), { configured: true, fid: 777, sponsored: false }); + // Another website, or a request without Origin, cannot start one. + const foreign = await fetch(`${url}/outpost/app-signin`, { method: "POST", headers: { origin: "https://evil.example", "content-type": "application/json" }, body: JSON.stringify({ key: KEY }) }); + assert.equal(foreign.status, 403); + assert.equal((await fetch(`${url}/outpost/app-signin`, { method: "POST", body: JSON.stringify({ key: KEY }) })).status, 403); + assert.equal((await fetch(`${url}/outpost/app-signin`, { method: "POST", headers: { origin: url }, body: JSON.stringify({ key: "0x12" }) })).status, 400); + + const r = await page.start(KEY); + assert.match(r.deeplinkUrl, /^farcaster:\/\/signed-key-request\?token=0x[0-9a-f]+$/); + assert.equal(r.requestFid, 777); + assert.ok(r.deadline > Date.now() / 1000); + assert.deepEqual(Object.keys(r).sort(), ["deadline", "deeplinkUrl", "requestFid", "token"]); + assert.equal(fc.requests.length, 1, "the endpoint got one request, with a signature it accepted"); + assert.deepEqual(await page.state(r.token), { state: "pending" }); + fc.approve(r.token); + assert.deepEqual(await page.state(r.token), { state: "completed", userFid: 4242 }); + // Only tokens this server handed out can be polled. + await assert.rejects(page.state("0xabc"), /unknown request/); + + const secret = CUSTODY.slice(2).toLowerCase(); + const signature = fc.requests[0]!.body.signature.slice(2).toLowerCase(); + for (const b of [...bodies, ...logs]) { + assert.ok(!b.toLowerCase().includes(secret), "custody key in a response or log"); + assert.ok(!b.toLowerCase().includes(signature), "the app account's signature in a response or log"); + } + } finally { + console.log = log; + server.close(); + fc.close(); + } +}); + +test("a request not approved before its deadline is reported expired; no app account is explained", async () => { + const fc = await fakeFarcaster({ appAddress: APP.address, userFid: 4242 }); + let now = Math.floor(Date.now() / 1000); + const { url, server } = await serve(signInHandler({ account: () => APP, api: fc.url, now: () => now })); + const none = await serve(signInHandler({ account: () => undefined, api: fc.url })); + try { + const page = new SignInClient(`${url}/outpost/app-signin`); + const r = await page.start(KEY); + now += REQUEST_SECONDS + 1; + assert.deepEqual(await page.state(r.token), { state: "expired" }); + await assert.rejects(page.state(r.token), /unknown request/); + + const empty = new SignInClient(`${none.url}/outpost/app-signin`); + assert.deepEqual(await empty.service(), { configured: false }); + await assert.rejects(empty.start(KEY), /no app account is set up here/); + // A static host with no Outpost server behind it. + assert.equal(await new SignInClient("http://127.0.0.1:1/x").service(), undefined); + } finally { + server.close(); + none.server.close(); + fc.close(); + } +}); + +test("an answer from the endpoint that is not a Farcaster link is refused", async () => { + const bad = createServer((_req, res) => res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ result: { signedKeyRequest: { token: "0xab", deeplinkUrl: "https://phish.example/?token=0xab", state: "pending" } } }))); + await new Promise((ok) => bad.listen(0, "127.0.0.1", ok)); + const api = `http://127.0.0.1:${(bad.address() as { port: number }).port}`; + const { url, server } = await serve(signInHandler({ account: () => APP, api })); + try { + await assert.rejects(new SignInClient(`${url}/outpost/app-signin`).start(KEY), /does not understand/); + } finally { + server.close(); + bad.close(); + } +}); + +test("society node: members only by token, CORS for the hosted page, nothing outside /app-signin", async () => { + const { createHash } = await import("node:crypto"); + const { societySignIn } = await import("../src/server/appsignin-service.js"); + const fc = await fakeFarcaster({ appAddress: APP.address, userFid: 4242 }); + const members = () => new Map([[createHash("sha256").update("member-token").digest("hex"), "alice"]]); + const server = createServer(societySignIn({ account: () => APP, api: fc.url, members })); + await new Promise((ok) => server.listen(0, "127.0.0.1", ok)); + const url = `http://127.0.0.1:${(server.address() as { port: number }).port}`; + try { + const pages = "https://spider333.github.io"; + const pre = await fetch(`${url}/app-signin`, { method: "OPTIONS", headers: { origin: pages } }); + assert.equal(pre.status, 204); + assert.equal(pre.headers.get("access-control-allow-origin"), pages); + assert.match(pre.headers.get("access-control-allow-headers") ?? "", /Authorization/); + assert.equal((await fetch(`${url}/app-signin`, { method: "OPTIONS", headers: { origin: "https://evil.example" } })).headers.get("access-control-allow-origin"), null); + + const stranger = new SignInClient(`${url}/app-signin`, { authorization: "Bearer wrong" }); + assert.equal(await stranger.service(), undefined); + await assert.rejects(stranger.start(KEY), /members only/); + const member = new SignInClient(`${url}/app-signin`, { authorization: "Bearer member-token", origin: pages }); + assert.deepEqual(await member.service(), { configured: true, fid: 777, sponsored: false }); + const r = await member.start(KEY); + assert.match(r.deeplinkUrl, /^farcaster:\/\//); + assert.deepEqual(await member.state(r.token), { state: "pending" }); + assert.equal((await fetch(`${url}/v1/info`, { headers: { authorization: "Bearer member-token" } })).status, 404); + } finally { + server.close(); + fc.close(); + } +}); diff --git a/test/fake-farcaster.ts b/test/fake-farcaster.ts new file mode 100644 index 0000000..6b0a86e --- /dev/null +++ b/test/fake-farcaster.ts @@ -0,0 +1,72 @@ +// A stand-in for Farcaster's signed key request endpoint (api.farcaster.xyz/v2/signed-key-...), +// for tests and the demo. It checks the app account's EIP-712 SignedKeyRequest signature (and a +// sponsorship, when sent) the way the KeyGateway would, then "approves" when told to, or after +// `autoApproveAfter` polls, calling `onApprove` so a fake node can list the new onchain key. + +import { createServer, type Server } from "node:http"; +import { SIGNED_KEY_REQUEST_TYPE, SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN } from "@farcaster/core"; +import { verifyMessage, verifyTypedData } from "viem"; +import type { HexString, KeyRequestBody } from "../src/core/index.js"; + +export interface FakeRequest { + body: KeyRequestBody; + token: string; + state: "pending" | "approved" | "completed"; + polls: number; + userFid?: number; +} + +export async function fakeFarcaster(opts: { + appAddress: HexString; + userFid: number; + autoApproveAfter?: number; // polls; undefined: only approve() does it + onApprove?: (r: FakeRequest) => void; +}): Promise<{ url: string; requests: FakeRequest[]; approve: (token: string) => void; close: () => void; server: Server }> { + const requests: FakeRequest[] = []; + const approve = (token: string) => { + const r = requests.find((x) => x.token === token); + if (!r || r.state === "completed") return; + r.state = "completed"; + r.userFid = opts.userFid; + opts.onApprove?.(r); + }; + const out = (r: FakeRequest) => ({ + result: { signedKeyRequest: { token: r.token, deeplinkUrl: `farcaster://signed-key-request?token=${r.token}`, key: r.body.key, state: r.state, ...(r.userFid ? { userFid: r.userFid } : {}) } }, + }); + const server = createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", async () => { + const url = new URL(req.url ?? "/", "http://x"); + const send = (status: number, body: unknown) => res.writeHead(status, { "content-type": "application/json" }).end(JSON.stringify(body)); + if (req.method === "POST" && url.pathname === "/v2/signed-key-requests") { + const body = JSON.parse(Buffer.concat(chunks).toString()) as KeyRequestBody; + const ok = await verifyTypedData({ + address: opts.appAddress, + domain: SIGNED_KEY_REQUEST_VALIDATOR_EIP_712_DOMAIN, + types: { SignedKeyRequest: SIGNED_KEY_REQUEST_TYPE }, + primaryType: "SignedKeyRequest", + message: { requestFid: BigInt(body.requestFid), key: body.key, deadline: BigInt(body.deadline) }, + signature: body.signature, + }).catch(() => false); + if (!ok) return send(400, { errors: [{ message: "invalid signature" }] }); + if (body.sponsorship && !(await verifyMessage({ address: opts.appAddress, message: { raw: body.signature }, signature: body.sponsorship.signature }).catch(() => false))) { + return send(400, { errors: [{ message: "invalid sponsorship" }] }); + } + const r: FakeRequest = { body, token: `0x${Buffer.from(crypto.getRandomValues(new Uint8Array(12))).toString("hex")}`, state: "pending", polls: 0 }; + requests.push(r); + return send(200, out(r)); + } + if (req.method === "GET" && url.pathname === "/v2/signed-key-request") { + const r = requests.find((x) => x.token === url.searchParams.get("token")); + if (!r) return send(404, { errors: [{ message: "not found" }] }); + r.polls++; + if (opts.autoApproveAfter !== undefined && r.polls >= opts.autoApproveAfter) approve(r.token); + return send(200, out(r)); + } + send(404, { errors: [{ message: "not found" }] }); + }); + }); + await new Promise((ok) => server.listen(0, "127.0.0.1", ok)); + return { url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, requests, approve, close: () => server.close(), server }; +} diff --git a/test/fake-providers/server/docker b/test/fake-providers/server/docker index 15eeb42..26fe31f 100755 --- a/test/fake-providers/server/docker +++ b/test/fake-providers/server/docker @@ -10,5 +10,7 @@ case "$1" in [ -z "$n" ] || { echo "$*"; env | grep '^OUTPOST_GATE_' || true; } > "$C/$n" ;; rm) shift; [ "$1" != -f ] || shift; for n in "$@"; do rm -f "$C/$n"; done ;; kill|exec) n="${!#}"; [ "$1" = exec ] && n="$2"; [ -e "$C/$n" ] ;; + # What a test says the container printed. + logs) n="${!#}"; [ -e "$C/$n" ] && printf '%s\n' "${FAKE_DOCKER_LOGS:-}" ;; *) echo "fake docker: unhandled $*" >&2; exit 3 ;; esac diff --git a/test/mentions.test.ts b/test/mentions.test.ts new file mode 100644 index 0000000..eca16b1 --- /dev/null +++ b/test/mentions.test.ts @@ -0,0 +1,121 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { FarcasterNetwork } from "@farcaster/core"; +import { buildCast, extractMentions, Hub, mentionedNames, mentionQuery, resolveMentions, withMentions, type AppKey } from "../src/core/index.js"; + +const KEY: AppKey = { + fid: 424242, + privateKey: "0x9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60", + publicKey: "0xd75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a", + requestFid: 424242, + scopes: [], + ttl: 86400, + network: FarcasterNetwork.MAINNET, +}; +const bytes = (s: string) => new TextEncoder().encode(s).length; +const names = new Map([[3, "dwr"], [5650, "vitalik.eth"], [1, "a"], [2, "b"]]); +const back = (r: { text: string; mentions: number[]; mentionsPositions: number[] }) => withMentions(r.text, r.mentions, r.mentionsPositions, (f) => names.get(f)!); + +test("a picked @name becomes a mention at its byte offset", () => { + assert.deepEqual(extractMentions("hi @dwr", [{ name: "dwr", fid: 3 }]), { text: "hi ", mentions: [3], mentionsPositions: [3] }); +}); + +test("offsets are UTF-8 bytes after emoji and multibyte text", () => { + const r = extractMentions("πŸŽ‰ @dwr ok", [{ name: "dwr", fid: 3 }]); + assert.deepEqual(r, { text: "πŸŽ‰ ok", mentions: [3], mentionsPositions: [5] }); + assert.equal(r.mentionsPositions[0], bytes("πŸŽ‰ ")); + const r2 = extractMentions("Grüße, ζ—₯本 πŸ‘©β€πŸ‘©β€πŸ‘§ @dwr und @vitalik.eth!", [{ name: "dwr", fid: 3 }, { name: "vitalik.eth", fid: 5650 }]); + assert.deepEqual(r2.mentionsPositions, [bytes("Grüße, ζ—₯本 πŸ‘©β€πŸ‘©β€πŸ‘§ "), bytes("Grüße, ζ—₯本 πŸ‘©β€πŸ‘©β€πŸ‘§ und ")]); + assert.equal(back(r2), "Grüße, ζ—₯本 πŸ‘©β€πŸ‘©β€πŸ‘§ @dwr und @vitalik.eth!"); +}); + +test("two names give two mentions in ascending order", () => { + const r = extractMentions("@a and @b", [{ name: "b", fid: 2 }, { name: "a", fid: 1 }]); + assert.deepEqual(r, { text: " and ", mentions: [1, 2], mentionsPositions: [0, 5] }); + assert.equal(back(r), "@a and @b"); +}); + +test("more than 10 mentions is refused", () => { + const picks = Array.from({ length: 11 }, (_, i) => ({ name: `u${i}`, fid: 100 + i })); + assert.throws(() => extractMentions(picks.map((p) => `@${p.name}`).join(" "), picks), /at most 10 mentions/); + assert.equal(extractMentions(picks.slice(0, 10).map((p) => `@${p.name}`).join(" "), picks).mentions.length, 10); +}); + +test("an edited pick, or a name never picked, stays plain text", () => { + const picks = [{ name: "dwr", fid: 3 }]; + assert.deepEqual(extractMentions("hi @dwrx", picks), { text: "hi @dwrx", mentions: [], mentionsPositions: [] }); + assert.deepEqual(extractMentions("hi @dw", picks), { text: "hi @dw", mentions: [], mentionsPositions: [] }); + assert.deepEqual(extractMentions("hi @someone", picks).mentions, []); + assert.deepEqual(extractMentions("mail me@dwr", picks).mentions, []); // not after a name character + assert.deepEqual(extractMentions("hi @dwr.eth", picks).mentions, []); // a longer, different name +}); + +test("a full stop or comma after a name ends it; case does not matter", () => { + const r = extractMentions("thanks @DWR. and @dwr, bye", [{ name: "dwr", fid: 3 }]); + assert.deepEqual(r.mentions, [3, 3]); + assert.equal(back(r), "thanks @dwr. and @dwr, bye"); +}); + +test("the longest picked name wins", () => { + const r = extractMentions("gm @vitalik.eth", [{ name: "vitalik", fid: 9 }, { name: "vitalik.eth", fid: 5650 }]); + assert.deepEqual(r.mentions, [5650]); +}); + +test("trimming never moves a mention or puts it past the end", () => { + assert.deepEqual(extractMentions(" hi @dwr ", [{ name: "dwr", fid: 3 }]), { text: "hi ", mentions: [3], mentionsPositions: [3] }); + const r = extractMentions(" @dwr gm", [{ name: "dwr", fid: 3 }]); + assert.deepEqual(r, { text: " gm", mentions: [3], mentionsPositions: [1] }); + for (const t of ["hi @dwr", "@dwr", " @dwr ", "x @dwr "]) { + const m = extractMentions(t, [{ name: "dwr", fid: 3 }]); + assert.ok(m.mentionsPositions.every((p) => p <= bytes(m.text)), t); + } +}); + +test("buildCast signs the mentions with the extracted text unchanged", async () => { + const r = extractMentions("πŸŽ‰ @dwr ok", [{ name: "dwr", fid: 3 }]); + const msg = await buildCast(KEY, r, 182_000_000); + const body = msg.data!.castAddBody!; + assert.equal(body.text, "πŸŽ‰ ok"); + assert.deepEqual(body.mentions, [3]); + assert.deepEqual(body.mentionsPositions, [5]); +}); + +test("mentionQuery finds the @word being typed", () => { + assert.deepEqual(mentionQuery("hi @dw", 6), { start: 3, query: "dw" }); + assert.deepEqual(mentionQuery("@", 1), { start: 0, query: "" }); + assert.equal(mentionQuery("hi @dw ", 7), undefined); + assert.equal(mentionQuery("me@dw", 5), undefined); + assert.deepEqual(mentionQuery("πŸŽ‰ @v", "πŸŽ‰ @v".length), { start: 3, query: "v" }); +}); + +test("mentionedNames lists every @word", () => { + assert.deepEqual(mentionedNames("hi @dwr and @Vitalik.eth. me@x @-no"), ["dwr", "vitalik.eth"]); +}); + +// A node that knows one name: "dwr" is FID 3, and FID 3's profile says so. FID 9 claims "liar" +// in the proof, but its own profile carries a different name. +function nameNode() { + const userData = (fid: number, name: string) => ({ + messages: [{ data: { type: "MESSAGE_TYPE_USER_DATA_ADD", fid, timestamp: 1, network: "FARCASTER_NETWORK_MAINNET", userDataBody: { type: "USER_DATA_TYPE_USERNAME", value: name } }, hash: "0x00", hashScheme: "", signature: "", signatureScheme: "", signer: "0x00" }], + }); + const fetch = async (input: string | URL | Request) => { + const u = new URL(String(input)); + const body = + u.pathname === "/v1/userNameProofByName" && u.searchParams.get("name") === "dwr" ? { fid: 3 } + : u.pathname === "/v1/userNameProofByName" && u.searchParams.get("name") === "liar" ? { fid: 9 } + : u.pathname === "/v1/userDataByFid" && u.searchParams.get("fid") === "3" ? userData(3, "dwr") + : u.pathname === "/v1/userDataByFid" && u.searchParams.get("fid") === "9" ? userData(9, "someone-else") + : undefined; + return new Response(JSON.stringify(body ?? { error: "not found" }), { status: body ? 200 : 400 }); + }; + return new Hub({ url: "http://node.test", fetch: fetch as typeof globalThis.fetch }); +} + +test("resolveMentions looks names up and refuses an unknown one, naming it", async () => { + const hub = nameNode(); + assert.deepEqual(await resolveMentions(hub, "πŸŽ‰ hi @dwr!"), { text: "πŸŽ‰ hi !", mentions: [3], mentionsPositions: [bytes("πŸŽ‰ hi ")] }); + assert.deepEqual(await resolveMentions(hub, "no names here"), { text: "no names here" }); + await assert.rejects(resolveMentions(hub, "hi @dwr and @nobody"), /unknown username @nobody/); + // The node's proof alone is not enough: the account's own profile must carry the name. + await assert.rejects(resolveMentions(hub, "hi @liar"), /unknown username @liar/); +}); diff --git a/test/notifications.test.ts b/test/notifications.test.ts new file mode 100644 index 0000000..f8d7440 --- /dev/null +++ b/test/notifications.test.ts @@ -0,0 +1,101 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { buildReaction, extractMentions, FeedBuilder, notificationsFor, notificationText, seenUpTo, unreadCount, type HexString, type Notification } from "../src/core/index.js"; +import { appKey, json, World } from "./votes-world.js"; + +// Farcaster seconds, a few minutes apart, all in the past. +const NOW = Math.floor((Date.now() - 1609459200000) / 1000); +const t = (minutesAgo: number) => NOW - minutesAgo * 60; +const ME = 100; + +async function world() { + const w = new World(); + for (const f of [ME, 200, 300, 400, 500]) await w.member(f); + await w.name(ME, "me"); + await w.name(200, "alice"); + await w.name(300, "bob"); + return w; +} + +test("replies, mentions, likes, recasts and follows, newest first, likes grouped per cast", async () => { + const w = await world(); + const mine = await w.cast(ME, { text: "my first cast" }, t(60)); + const mine2 = await w.cast(ME, { text: "my second cast" }, t(50)); + const target = { fid: ME, hash: mine.hash }; + await w.cast(200, { text: "a reply", replyTo: target }, t(40)); + await w.cast(300, { ...extractMentions("hey @me look", [{ name: "me", fid: ME }]) }, t(30)); + for (const [f, ago] of [[200, 20], [300, 19], [400, 18]] as const) w.likes.push(await like(w, f, mine, t(ago))); + w.likes.push(await like(w, 500, mine2, t(15), "recast")); + await w.follow(400, ME, t(10)); + w.likes.push(await like(w, ME, mine, t(5))); // your own like is not a notification + + const r = await notificationsFor(new FeedBuilder(w.hub()), ME); + assert.equal(r.pending, 0); + assert.deepEqual(r.failed, []); + assert.deepEqual( + r.items.map((n) => [n.kind, n.actors.map((a) => a.fid)]), + [["follow", [400]], ["recast", [500]], ["like", [400, 300, 200]], ["mention", [300]], ["reply", [200]]], + ); + const texts = r.items.map(notificationText); + assert.deepEqual(texts, ["fid 400 followed you", "fid 500 recast your cast", "fid 400 and 2 others liked your cast", "@bob mentioned you", "@alice replied"]); + const likeItem = r.items.find((n) => n.kind === "like")!; + assert.equal(likeItem.target?.text, "my first cast"); + assert.equal(r.items.find((n) => n.kind === "mention")!.cast!.text, "hey @me look"); + assert.equal(r.items.find((n) => n.kind === "reply")!.target?.hash, mine.hash); + for (let i = 1; i < r.items.length; i++) assert.ok(r.items[i - 1]!.time >= r.items[i]!.time, "newest first"); +}); + +async function like(w: World, fid: number, cast: { data: { fid: number }; hash: HexString }, ts: number, kind: "like" | "recast" = "like") { + return json(await buildReaction(w.keys.get(fid)!, kind, { fid: cast.data.fid, hash: cast.hash }, ts)); +} + +test("a reply that also mentions you shows once, as a reply", async () => { + const w = await world(); + const mine = await w.cast(ME, { text: "hi" }, t(30)); + await w.cast(200, { ...extractMentions("@me yes", [{ name: "me", fid: ME }]), replyTo: { fid: ME, hash: mine.hash } }, t(20)); + const r = await notificationsFor(new FeedBuilder(w.hub()), ME); + assert.deepEqual(r.items.map((n) => n.kind), ["reply"]); +}); + +test("a forged reply and a like from a key that is not the liker's are dropped", async () => { + const w = await world(); + const mine = await w.cast(ME, { text: "hi" }, t(30)); + const reply = await w.cast(200, { text: "real reply", replyTo: { fid: ME, hash: mine.hash } }, t(20)); + // The node changes the reply's text after it was signed. + const forged = structuredClone(reply); + forged.data.castAddBody!.text = "forged text"; + forged.hash = (await w.cast(300, { text: "other", replyTo: { fid: ME, hash: mine.hash } }, t(19))).hash; + w.casts = w.casts.filter((m) => m.data.fid !== 300); + w.casts.unshift(forged); + // A like signed by a key nobody lists for FID 400. + const stranger = await appKey(400); + w.likes.push(json(await buildReaction(stranger, "like", { fid: ME, hash: mine.hash }, t(10)))); + const hub = w.hub(); + const r = await notificationsFor(new FeedBuilder(hub), ME); + assert.deepEqual(r.items.map((n) => [n.kind, n.cast?.text]), [["reply", "real reply"]]); + assert.ok(hub.verifier!.failed >= 1, "the forged reply was counted as failed"); +}); + +test("a source the node does not answer is reported; the rest still shows", async () => { + const w = await world(); + const mine = await w.cast(ME, { text: "hi" }, t(30)); + await w.cast(200, { text: "reply", replyTo: { fid: ME, hash: mine.hash } }, t(20)); + const base = w.answer.bind(w); + w.answer = (path, q, body) => (path === "/v1/castsByMention" ? { status: 500, body: { error: "boom" } } : base(path, q, body)); + const updates: number[] = []; + const r = await notificationsFor(new FeedBuilder(w.hub()), ME, { onUpdate: (u) => updates.push(u.pending) }); + assert.deepEqual(r.failed, ["mentions"]); + assert.deepEqual(r.items.map((n) => n.kind), ["reply"]); + assert.deepEqual(updates.sort(), [0, 1, 2, 3, 4]); +}); + +test("unread count is the items after the last-seen time; marking read covers clock skew", () => { + const n = (time: number): Notification => ({ id: String(time), kind: "follow", time, actors: [{ fid: 1 }] }); + const items = [n(5000), n(4000), n(3000), n(1000)]; + assert.equal(unreadCount(items, undefined), 4); + assert.equal(unreadCount(items, 3000), 2); + assert.equal(unreadCount(items, 5000), 0); + assert.equal(seenUpTo(items, 4500), 5000); + assert.equal(seenUpTo(items, 9000), 9000); + assert.equal(unreadCount(items, seenUpTo(items, 4500)), 0); +}); diff --git a/test/phase1-demo.ts b/test/phase1-demo.ts new file mode 100644 index 0000000..0947fe4 --- /dev/null +++ b/test/phase1-demo.ts @@ -0,0 +1,73 @@ +// Try mentions, notifications and "Approve in the Farcaster app" in the web client, with no +// mainnet account and no phone: a fake node, a stand-in for Farcaster's endpoint that approves on +// its own after a few polls, and the real sign-in handler of `./outpost client`. +// +// npm run build && node --import tsx test/phase1-demo.ts +// +// Open the printed URL. Everything the page loads comes from 127.0.0.1. Ctrl-C stops the demo. +// - Signed in as @me (FID 100, open /demo/as-me): notifications from @alice, @bob and FID 400, an unread count, +// "Mark all read", and "@" suggestions in the composer. +// - Fresh browser (/demo/fresh): no key; choose "Approve in the Farcaster app" to sign in as @carol +// (FID 700, an account "made in the Farcaster app") without a phrase. +// The app account's custody key is Hardhat's public test key 2 and holds nothing. + +import { createServer } from "node:http"; +import { resolve } from "node:path"; +import { privateKeyToAccount } from "viem/accounts"; +import { buildReaction, extractMentions, type AppAccount, type HexString } from "../src/core/index.js"; +import { sameOrigin, signInHandler } from "../src/server/appsignin-service.js"; +import { fakeFarcaster } from "./fake-farcaster.js"; +import { json, World } from "./votes-world.js"; + +const PORT = Number(process.env.OUTPOST_DEMO_PORT ?? 18390); +const CUSTODY = "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a" as HexString; +const APP: AppAccount = { fid: 900, custodyKey: CUSTODY, address: privateKeyToAccount(CUSTODY).address }; +const ME = 100; +const CAROL = 700; +const ago = (min: number) => Math.floor((Date.now() - 1609459200000) / 1000) - min * 60; + +const w = new World(); +for (const [fid, name] of [[ME, "me"], [200, "alice"], [300, "bob"], [400, "dave"], [500, "erin"], [CAROL, "carol"], [APP.fid, "outpost-demo"]] as const) { + await w.member(fid); + await w.name(fid, name); +} +for (const f of [200, 300, 500]) await w.follow(ME, f, ago(600)); +const like = async (fid: number, cast: { data: { fid: number }; hash: HexString }, min: number, kind: "like" | "recast" = "like") => + w.likes.push(json(await buildReaction(w.keys.get(fid)!, kind, { fid: cast.data.fid, hash: cast.hash }, ago(min)))); + +const first = await w.cast(ME, { text: "Trying Outpost: my own node, every message checked." }, ago(180)); +const second = await w.cast(ME, { text: "Second cast, for the recasts." }, ago(120)); +await w.cast(200, { text: "Welcome! Which node do you run?", replyTo: { fid: ME, hash: first.hash } }, ago(90)); +await w.cast(300, { ...extractMentions("Good thread with @me and @alice πŸŽ‰", [{ name: "me", fid: ME }, { name: "alice", fid: 200 }]) }, ago(60)); +await like(200, first, 50); +await like(300, first, 45); +await like(400, first, 40); +await like(500, second, 30, "recast"); +await w.follow(400, ME, ago(20)); +await w.cast(500, { text: "gm from erin" }, ago(10)); + +const second_ = await w.serve(0); +const fc = await fakeFarcaster({ + appAddress: APP.address, + userFid: CAROL, + autoApproveAfter: 3, + onApprove: (r) => w.approve(CAROL, r.body.key, APP.fid), +}); +const signIn = signInHandler({ account: () => APP, api: fc.url, refuse: sameOrigin }); +const node = w.handler({ "/outpost-config.json": { network: "devnet", checkNode: second_.url } }, resolve("dist/web")); +// Opening /demo/as-me stores @me's throwaway key in the page's storage, so it is never printed. +const asMe = ``; +const fresh = ``; +const server = createServer((req, res) => { + const path = (req.url ?? "/").split("?")[0]!; + if (path === "/demo/as-me" || path === "/demo/fresh") return void res.writeHead(200, { "content-type": "text/html" }).end(path === "/demo/as-me" ? asMe : fresh); + if (path === "/outpost/app-signin" || path.startsWith("/outpost/app-signin/")) return signIn(req, res, path.slice("/outpost/app-signin".length)); + node(req, res); +}); +await new Promise((ok) => server.listen(PORT, "127.0.0.1", ok)); +const url = `http://127.0.0.1:${PORT}`; + +console.log(`client, fake node and sign-in: ${url} (second node for key checks: ${second_.url}, Farcaster stand-in: ${fc.url})`); +console.log(`app account: FID ${APP.fid} (@outpost-demo); approvals sign in as @carol (FID ${CAROL}) after 3 polls, about 6 s`); +console.log(`To act as @me (notifications, mentions), open ${url}/demo/as-me`); +console.log(`For the sign-in, start from a clean page: ${url}/demo/fresh`); diff --git a/test/providers.sh b/test/providers.sh index 7f54d90..d3eacfe 100755 --- a/test/providers.sh +++ b/test/providers.sh @@ -137,6 +137,35 @@ rm -f "$C/outpost-phone" "$ROOT/outpost" destroy --yes >/dev/null echo "ok: phone access and society mode keep out of each other's way" +# "Approve in the Farcaster app" on the society node: infra/society.sh with the fake docker. The +# app account's key arrives on stdin, is stored mode 600, and is deleted again by app-signin-off. +SOC="$WORK/soc" +soc() { OUTPOST_SOCIETY_DIR="$SOC" OUTPOST_SOCIETY_HOST=203-0-113-7.sslip.io FAKE_DOCKER_LOGS='app-signin: app account FID 777; 127.0.0.1:3392' \ + PATH="$ROOT/test/fake-providers/server:$PATH" bash "$ROOT/infra/society.sh" "$@"; } +rm -f "$C"/* +soc on <"$ROOT/src/server/gate.ts" >/dev/null +! soc app-signin-on <"$ROOT/src/server/appsignin.ts" >/dev/null 2>&1 || fail "app-signin-on started without an app account" +account='{"fid": 777, "custodyKey": "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a", "address": "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC"}' +printf '%s\n' "$account" | soc app-signin-account >/dev/null || fail "app-signin-account refused a good account" +[ -n "$(find "$SOC/appsignin/account.json" -perm 600)" ] || fail "app account file is not mode 600" +! printf 'not json\n' | soc app-signin-account >/dev/null 2>&1 || fail "app-signin-account took garbage" +grep -q '"fid": 777' "$SOC/appsignin/account.json" || fail "garbage replaced the stored app account" +for run in 1 2; do + out="$(soc app-signin-on <"$ROOT/src/server/appsignin.ts")" || fail "app-signin-on failed (run $run)" + [[ "$out" == *"requests come from FID 777"* ]] || fail "app-signin-on: $out" +done +grep -q -- "--network host --read-only --cap-drop ALL --security-opt no-new-privileges" "$C/outpost-appsignin" || fail "sign-in container not locked down" +grep -q -- "-v $SOC/appsignin/account.json:/appsignin/account.json:ro" "$C/outpost-appsignin" || fail "app account not mounted read-only" +want="$(printf '{\n\tlog {\n\t\texclude http.log http.handlers http.stdlib tls.handshake\n\t}\n}\n203-0-113-7.sslip.io {\n\thandle /app-signin* {\n\t\treverse_proxy 127.0.0.1:3392\n\t}\n\thandle {\n\t\treverse_proxy 127.0.0.1:3390\n\t}\n\theader -Server\n}')" +[ "$(cat "$SOC/caddy/Caddyfile")" = "$want" ] || fail "Caddyfile with sign-in is not as expected: +$(cat "$SOC/caddy/Caddyfile")" +for run in 1 2; do soc app-signin-off >/dev/null || fail "app-signin-off failed (run $run)"; done +[ ! -e "$C/outpost-appsignin" ] || fail "app-signin-off left the container running" +[ ! -e "$SOC/appsignin/account.json" ] || fail "app-signin-off kept the app account's key on the server" +grep -q "reverse_proxy 127.0.0.1:3392" "$SOC/caddy/Caddyfile" && fail "app-signin-off left the route in the Caddyfile" +soc off >/dev/null +echo "ok: society app-signin account (mode 600), on x2, off x2 (key deleted)" + # DigitalOcean answering 503 for volume get and delete: destroy must fail and keep the state, # not call the volume gone. find "$FAKE_STATE" -type f -delete @@ -344,4 +373,32 @@ echo "ok: provision without a terminal asks for --yes" out="$(OUTPOST_HOME="$WORK/no-key" "$ROOT/outpost" uninstall 2>&1)" || fail "uninstall without a CLI key failed: $out" [[ "$out" == *"Revoke this key"* ]] || fail "uninstall without a CLI key does not point to the web client: $out" echo "ok: uninstall without a CLI key points to the web client" + +# client and cli reach your server through an SSH tunnel. With --public or --node they use no +# server: no ssh, no provider call, and a node URL that is not https (or this computer) is refused. +mkdir -p "$WORK/logssh" +printf '#!/bin/sh\necho "ssh $*" >>"%s/ssh.log"\nexit 255\n' "$WORK" >"$WORK/logssh/ssh" +chmod +x "$WORK/logssh/ssh" +for c in cli client; do + : >"$WORK/ssh.log" + PATH="$WORK/logssh:$PATH" OUTPOST_HOST=203.0.113.7 "$ROOT/outpost" "$c" status >/dev/null 2>&1 && fail "$c: ran with no tunnel" + grep -q -- "-L 127.0.0.1:13381:127.0.0.1:3381 root@203.0.113.7" "$WORK/ssh.log" || fail "$c: no SSH tunnel to the server" +done +echo "ok: client and cli use the SSH tunnel to your server" +: >"$WORK/ssh.log" +: >"$FAKE_STATE/calls.log" +out="$(PATH="$WORK/logssh:$PATH" "$ROOT/outpost" cli --node http://127.0.0.1:1 status 2>&1)" && fail "cli --node: a closed port answered" +[[ "$out" == *"cannot reach node at http://127.0.0.1:1"* ]] || fail "cli --node did not use the node given: $out" +port=$((20000 + RANDOM % 20000)) +PATH="$WORK/logssh:$PATH" OUTPOST_PORT="$port" "$ROOT/outpost" client --public >"$WORK/client.out" 2>&1 & +client=$! +for _ in $(seq 50); do grep -q "^client:" "$WORK/client.out" && break; sleep 0.2; done +cfg="$(curl -fsm 5 "http://127.0.0.1:$port/outpost-config.json" || true)" +kill "$client" 2>/dev/null; wait "$client" 2>/dev/null || true +[[ "$cfg" == *'"url":"https://snap.farcaster.xyz:3381","farcaster":true'* ]] || fail "client --public does not tell the page it uses Farcaster's public node: $cfg" +[ ! -s "$WORK/ssh.log" ] || fail "--public or --node called ssh: $(cat "$WORK/ssh.log")" +[ ! -s "$FAKE_STATE/calls.log" ] || fail "--public or --node called a provider: $(cat "$FAKE_STATE/calls.log")" +out="$("$ROOT/outpost" client --node http://example.com:3381 2>&1)" && fail "client took a plain http node URL" +[[ "$out" == *"must be https"* ]] || fail "no clear message for a plain http node URL: $out" +echo "ok: --public and --node use no server, and refuse plain http" find "$WORK" -type f -delete diff --git a/test/remote-node.test.ts b/test/remote-node.test.ts new file mode 100644 index 0000000..bb0b2d8 --- /dev/null +++ b/test/remote-node.test.ts @@ -0,0 +1,81 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { spawn } from "node:child_process"; +import { createServer, type Server } from "node:http"; +import { checkNodeFor, DEFAULT_CHECK_NODE, OTHER_PUBLIC_NODE, secondNode, SecondNode } from "../src/core/index.js"; + +test("the second node is never the node being read", () => { + assert.equal(checkNodeFor("http://127.0.0.1:13381"), DEFAULT_CHECK_NODE); + assert.equal(checkNodeFor("https://snap.farcaster.xyz:3381/"), OTHER_PUBLIC_NODE); + // ./outpost client forwards /v1/* to the node and /outpost/check/v1/* to the second node. + assert.ok(secondNode("http://127.0.0.1:8381/outpost/check", "http://127.0.0.1:8381") instanceof SecondNode); + assert.equal(secondNode("http://127.0.0.1:8381/", "http://127.0.0.1:8381"), undefined); +}); + +// A stand-in for a node, a second node or an RPC: answers with its name, the method and the path. +async function upstream(name: string): Promise<{ url: string; server: Server }> { + const server = createServer((req, res) => { + let body = ""; + req.on("data", (c) => (body += c)); + req.on("end", () => res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ name, method: req.method, path: req.url, body }))); + }); + await new Promise((r) => server.listen(0, "127.0.0.1", r)); + return { url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, server }; +} + +async function freePort(): Promise { + const s = createServer(); + await new Promise((r) => s.listen(0, "127.0.0.1", r)); + const port = (s.address() as { port: number }).port; + await new Promise((r) => s.close(r)); + return port; +} + +async function client(env: Record): Promise<{ url: string; stop: () => void }> { + const port = await freePort(); + const child = spawn(process.execPath, ["--import", "tsx", "src/server/serve.ts"], { env: { ...process.env, ...env, OUTPOST_PORT: String(port) }, stdio: ["ignore", "pipe", "inherit"] }); + await new Promise((ok, fail) => { + child.stdout.on("data", (d: Buffer) => d.toString().includes("client:") && ok()); + child.on("exit", (code) => fail(new Error(`serve.ts exited with ${code}`))); + }); + return { url: `http://127.0.0.1:${port}`, stop: () => child.kill() }; +} + +test("./outpost client --public: the page learns the node is not yours, and key checks go through this server", async () => { + const node = await upstream("node"); + const check = await upstream("check"); + const rpc = await upstream("rpc"); + const c = await client({ OUTPOST_NODE_URL: node.url, OUTPOST_NODE_REMOTE: "1", OUTPOST_CHECK_NODE: check.url, OUTPOST_OPTIMISM_RPC: rpc.url }); + try { + const cfg = await (await fetch(`${c.url}/outpost-config.json`)).json(); + assert.deepEqual(cfg, { network: "mainnet", node: { url: node.url, farcaster: false, check: check.url, rpc: `${rpc.url}/` } }); + assert.deepEqual(await (await fetch(`${c.url}/v1/info?x=1`)).json(), { name: "node", method: "GET", path: "/v1/info?x=1", body: "" }); + assert.deepEqual(await (await fetch(`${c.url}/outpost/check/v1/signersByFid?fid=3`)).json(), { name: "check", method: "GET", path: "/v1/signersByFid?fid=3", body: "" }); + const read = JSON.stringify({ jsonrpc: "2.0", id: 1, method: "eth_call", params: [] }); + const call = await fetch(`${c.url}/outpost/optimism`, { method: "POST", headers: { "content-type": "application/json" }, body: read }); + assert.deepEqual(await call.json(), { name: "rpc", method: "POST", path: "/", body: read }); + // Optimism is only read: a transaction is not forwarded. + const send = JSON.stringify([{ jsonrpc: "2.0", id: 1, method: "eth_call", params: [] }, { jsonrpc: "2.0", id: 2, method: "eth_sendRawTransaction", params: ["0x00"] }]); + assert.equal((await fetch(`${c.url}/outpost/optimism`, { method: "POST", body: send })).status, 403); + assert.equal((await fetch(`${c.url}/outpost/optimism`, { method: "POST", body: "{}" })).status, 403); + // The second node is only read, and nothing else leaves through these paths. + assert.notEqual((await fetch(`${c.url}/outpost/check/v1/submitMessage`, { method: "POST", body: "x" })).status, 200); + assert.notEqual((await fetch(`${c.url}/outpost/check/other`)).status, 200); + } finally { + c.stop(); + for (const s of [node, check, rpc]) s.server.close(); + } +}); + +test("./outpost client through the tunnel: no remote node, no forwarded checks", async () => { + const node = await upstream("node"); + const c = await client({ OUTPOST_NODE_URL: node.url, OUTPOST_NODE_REMOTE: "" }); + try { + assert.deepEqual(await (await fetch(`${c.url}/outpost-config.json`)).json(), { network: "mainnet" }); + assert.equal((await fetch(`${c.url}/outpost/check/v1/info`)).status, 404); + assert.equal((await fetch(`${c.url}/outpost/optimism`, { method: "POST", body: "{}" })).status, 405); + } finally { + c.stop(); + node.server.close(); + } +}); diff --git a/test/votes-world.ts b/test/votes-world.ts index 9eedc25..f5a18de 100644 --- a/test/votes-world.ts +++ b/test/votes-world.ts @@ -2,13 +2,17 @@ // Verifier asks. Used by votes.test.ts, and served over HTTP by votes-demo.ts for the web client. import { existsSync, readFileSync } from "node:fs"; -import { createServer } from "node:http"; +import { createServer, type IncomingMessage, type ServerResponse } from "node:http"; import { extname, join, normalize } from "node:path"; -import { FarcasterNetwork, Message, type Message as MessageT } from "@farcaster/core"; +import { encodeGaslessSignedKeyRequestMetadata, FarcasterNetwork, makeUserDataAdd, Message, UserDataType, type Message as MessageT } from "@farcaster/core"; import { bytesToHex } from "viem"; import { buildCast, + buildFollow, buildReaction, + signerOf, + unwrap, + type CastInput, genesisHead, Hub, proposalCast, @@ -50,6 +54,11 @@ type Answer = { status: number; body: unknown }; export class World { casts: HubMessage[] = []; likes: HubMessage[] = []; + links: HubMessage[] = []; + userData: HubMessage[] = []; + // Onchain keys added through an app's signed key request ("Approve in the Farcaster app"): + // "fid:key" -> the app's FID, listed by signersByFid with that metadata. + approved = new Map(); onchain = new Map(); gasless = new Map(); keys = new Map(); @@ -77,6 +86,26 @@ export class World { return m; } + // A cast by a member (a reply when input.replyTo is set), newest first like the real node. + async cast(fid: number, input: CastInput, now?: number): Promise { + const m = json(await buildCast(this.keys.get(fid)!, input, now)); + this.casts.unshift(m); + return m; + } + + async follow(fid: number, target: number, now?: number): Promise { + const m = json(await buildFollow(this.keys.get(fid)!, target, now)); + this.links.push(m); + return m; + } + + // The member's username, signed with their key, as userDataByFid returns it. + async name(fid: number, username: string): Promise { + const k = this.keys.get(fid)!; + const m = unwrap(await makeUserDataAdd({ type: UserDataType.USERNAME, value: username }, { fid, network: k.network, timestamp: Math.floor((Date.now() - 1609459200000) / 1000) }, signerOf(k))); + this.userData.push(json(m)); + } + verifier(): Verifier { const chain: KeyRegistry = { keyState: async (fid, key) => this.onchain.get(`${fid}:${key}`) ?? "none" }; const second = { @@ -93,11 +122,24 @@ export class World { if (m.data.type === "MESSAGE_TYPE_REACTION_ADD") this.likes.push(m); else if (m.data.type === "MESSAGE_TYPE_REACTION_REMOVE") this.likes = this.likes.filter((l) => !(l.data.fid === m.data.fid && l.data.reactionBody?.targetCastId?.hash === r?.targetCastId?.hash)); else if (m.data.type === "MESSAGE_TYPE_CAST_ADD") this.casts.unshift(m); + else if (m.data.type === "MESSAGE_TYPE_LINK_ADD") this.links.push(m); return { status: 200, body: m }; } + // An onchain key requested by `requestFid`, as the KeyGateway adds it after an approval. + approve(fid: number, key: HexString, requestFid: number) { + this.approved.set(`${fid}:${key.toLowerCase()}`, requestFid); + this.onchain.set(`${fid}:${key.toLowerCase()}`, "added"); + } + private signers(fid: number) { - return [...this.gasless].filter(([id]) => id.startsWith(`${fid}:`)).map(([id, s]) => ({ source: "SIGNER_SOURCE_OFFCHAIN", key: id.split(":")[1], keyType: 1, fid, requestFid: fid, scopes: s.scopes })); + const gasless = [...this.gasless].filter(([id]) => id.startsWith(`${fid}:`)).map(([id, s]) => ({ source: "SIGNER_SOURCE_OFFCHAIN", key: id.split(":")[1], keyType: 1, fid, requestFid: fid, scopes: s.scopes })); + const onchain = [...this.approved].filter(([id]) => id.startsWith(`${fid}:`)).map(([id, requestFid]) => { + const metadata = encodeGaslessSignedKeyRequestMetadata({ requestFid: BigInt(requestFid), requestSigner: "0x00000000000000000000000000000000000000aa", signature: new Uint8Array(65), deadline: 1n })._unsafeUnwrap(); + const key = id.split(":")[1] as HexString; + return { source: "SIGNER_SOURCE_ONCHAIN", key, keyType: 1, fid, requestFid, onChainEvent: { fid, signerEventBody: { key, eventType: "SIGNER_EVENT_TYPE_ADD", metadata: Buffer.from(metadata).toString("base64"), metadataType: 1 } } }; + }); + return [...gasless, ...onchain]; } // The node's answers. signersByFid lists the gasless keys, as a second node would. @@ -115,8 +157,18 @@ export class World { return messages(this.casts.filter((m) => m.data.fid === fid)); case "/v1/castById": return one(this.casts.find((m) => m.data.fid === fid && m.hash === hash)); - case "/v1/reactionsByCast": - return messages(this.lieAboutTargets ? this.likes : this.likes.filter((m) => m.data.reactionBody?.targetCastId?.hash.toLowerCase() === hash)); + case "/v1/reactionsByCast": { + const type = `REACTION_TYPE_${(q.get("reaction_type") ?? "Like").toUpperCase()}`; + return messages(this.lieAboutTargets ? this.likes : this.likes.filter((m) => m.data.reactionBody?.type === type && m.data.reactionBody.targetCastId?.hash.toLowerCase() === hash)); + } + case "/v1/castsByMention": + return messages(this.casts.filter((m) => m.data.castAddBody?.mentions.includes(fid))); + case "/v1/linksByTargetFid": + return messages(this.links.filter((m) => m.data.linkBody?.targetFid === Number(q.get("target_fid")))); + case "/v1/userNameProofByName": { + const u = this.userData.find((m) => m.data.userDataBody?.value === q.get("name")); + return u ? { status: 200, body: { fid: u.data.fid } } : { status: 400, body: { error: "not found" } }; + } case "/v1/reactionById": { const type = `REACTION_TYPE_${(q.get("reaction_type") ?? "").toUpperCase()}`; return one(this.likes.find((m) => m.data.fid === fid && m.data.reactionBody?.type === type && m.data.reactionBody.targetCastId?.hash === hash)); @@ -124,8 +176,9 @@ export class World { case "/v1/castsByParent": return messages(this.casts.filter((m) => m.data.castAddBody?.parentCastId?.hash === hash)); case "/v1/linksByFid": + return messages(this.links.filter((m) => m.data.fid === fid)); case "/v1/userDataByFid": - return messages([]); + return messages(this.userData.filter((m) => m.data.fid === fid)); case "/v1/signersByFid": { const signers = this.signers(fid); return { status: 200, body: { signers, gaslessSignerCount: signers.length, gaslessSignerLimit: 1000, currentUserNonce: 0 } }; @@ -153,8 +206,14 @@ export class World { // Over HTTP, open to any page (as Farcaster's public node is), plus JSON files at fixed paths, // and a built web client from `web` for every other path outside /v1/. async serve(port = 0, files: Record = {}, web?: string): Promise<{ url: string; close: () => void }> { + const server = createServer(this.handler(files, web)); + await new Promise((ok) => server.listen(port, "127.0.0.1", ok)); + return { url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, close: () => server.close() }; + } + + handler(files: Record = {}, web?: string): (req: IncomingMessage, res: ServerResponse) => void { const types: Record = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".json": "application/json" }; - const server = createServer((req, res) => { + return (req, res) => { const chunks: Buffer[] = []; req.on("data", (c: Buffer) => chunks.push(c)); req.on("end", () => { @@ -167,9 +226,7 @@ export class World { const r = url.pathname in files ? { status: 200, body: files[url.pathname] } : this.answer(url.pathname, url.searchParams, Buffer.concat(chunks)); res.writeHead(r.status, { "content-type": "application/json", "access-control-allow-origin": "*" }).end(JSON.stringify(r.body)); }); - }); - await new Promise((ok) => server.listen(port, "127.0.0.1", ok)); - return { url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, close: () => server.close() }; + }; } }