feat(auth): add --network-config flag for self-hosted network support#21
feat(auth): add --network-config flag for self-hosted network support#21jmetrikat merged 8 commits intoanyproto:mainfrom
--network-config flag for self-hosted network support#21Conversation
- CreateWallet now accepts networkConfigPath parameter - Passes NetworkMode=CustomConfig and config path to AccountCreate/AccountSelect - Enables creating bot accounts on self-hosted Anytype networks
- Store networkConfigPath in config.json when account is created - Load network config on serve auto-login - serve command now properly connects to self-hosted networks - Add --network-config to login command as well
- join command now reads networkId from saved network config YAML - No longer need to pass --network flag manually for self-hosted
|
FWIW, I checked this on my instance and it worked as advertised (after transforming the invite link from anytype://) ❤️ |
There was a problem hiding this comment.
Pull request overview
This PR wires self-hosted Anytype/any-sync network support into the CLI by allowing account creation/login against a custom network configuration and reusing that configuration across commands.
Changes:
- Adds a
--network-configflag toauth createandauth login, threads the network config path throughcore.Authenticate/CreateWallet, and persists it into~/.anytype/config.json. - Extends the config layer with a
NetworkConfigPathfield plus helpers to read/write it and to extractnetworkIdfrom a YAML network config. - Updates the service program’s auto-login and
space joindefaulting logic to use the persisted network config (viaNetworkMode/NetworkCustomConfigFilePathandReadNetworkIdFromYAML) when present.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
cmd/auth/create/create.go |
Adds --network-config flag to auth create and passes the path into core.CreateWallet, enabling account creation on self-hosted networks. |
cmd/auth/login/login.go |
Adds --network-config flag to auth login, threads the path into core.Login, and updates help text accordingly. |
core/auth.go |
Extends Authenticate, Login, and CreateWallet to accept a networkConfigPath, set the appropriate NetworkMode, send NetworkCustomConfigFilePath to heart RPCs, and persist the path in the CLI config. |
core/config/config.go |
Adds a NetworkConfigPath field to the persisted Config struct and a setter on ConfigManager so the path can be stored on disk. |
core/config/config_helper.go |
Introduces helpers to get/set NetworkConfigPath via the config manager and a ReadNetworkIdFromYAML function to pull networkId from a custom network config file. |
core/serviceprogram/serviceprogram.go |
Enhances auto-login to load any saved NetworkConfigPath from config and pass it into core.Authenticate, aligning the background service with self-hosted setups. |
cmd/space/join/join.go |
Changes space join so that when --network is omitted, it first looks up the saved network config path and, if available, reads networkId from that YAML before falling back to the default Anytype network ID. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cmd/space/join/join.go
Outdated
| // Load network ID: prefer flag, then saved config YAML, then default | ||
| if networkId == "" { | ||
| networkId = config.AnytypeNetworkAddress | ||
| if networkConfigPath, _ := config.GetNetworkConfigPathFromConfig(); networkConfigPath != "" { | ||
| // Read network ID from the YAML config file | ||
| if id, err := config.ReadNetworkIdFromYAML(networkConfigPath); err == nil && id != "" { | ||
| networkId = id | ||
| } | ||
| } | ||
| if networkId == "" { | ||
| networkId = config.AnytypeNetworkAddress | ||
| } |
There was a problem hiding this comment.
When ReadNetworkIdFromYAML fails (e.g., missing or malformed YAML) we silently ignore the error and then fall back to AnytypeNetworkAddress, which means a misconfigured self-hosted network can unexpectedly be replaced by the default public network without any signal to the user. Consider at least logging a warning (and possibly failing the command) when a stored networkConfigPath cannot be read or yields an empty networkId, so users are aware their self-hosted network configuration isn't being applied.
| // Load network config path if configured (for self-hosted networks) | ||
| networkConfigPath, _ := config.GetNetworkConfigPathFromConfig() | ||
|
|
||
| output.Info("Found stored account key, attempting auto-login...") | ||
|
|
||
| maxRetries := 3 | ||
| for i := 0; i < maxRetries; i++ { | ||
| if err := core.Authenticate(accountKey, "", p.apiListenAddr); err != nil { | ||
| if err := core.Authenticate(accountKey, "", p.apiListenAddr, networkConfigPath); err != nil { |
There was a problem hiding this comment.
The error returned from GetNetworkConfigPathFromConfig() is discarded, so if loading the config fails we silently treat it as "no network config" and attempt auto-login against the default network instead. To make diagnosing self-hosted setups easier, consider at least logging a warning when loading the config fails so users understand why their custom network configuration isn't being used for auto-login.
|
Hey @jsandai! As I don't run self-hosted myself, I wanted to understand your testing approach:
Thanks for elaborating! |
Invite link support: - http(s)://<any-host>/<cid>#<key> (flexible host, not hardcoded) - anytype://invite/?cid=<cid>&key=<key> (app deep link format) NetworkId handling: - Cache networkId in config.json on first read from YAML - Fallback chain: cached → YAML → default network - Keeps NetworkConfigPath for full YAML access when needed This combines the best of PR anyproto#8 (flexible parsing, caching) with our approach (YAML path for advanced config access).
fd386f4 to
5ea2936
Compare
Hey @jmetrikat, thanks for the review! Invite links: Good catch — I've updated the PR to support flexible invite parsing: • http(s):///# (any host, not hardcoded) NetworkId caching: You're right, re-reading the YAML each time was unnecessary. I've updated the approach: • Cache networkId in config.json on first read from YAML Also added a SELF-HOSTED.md guide covering the setup flow. Let me know if you have any other feedback! |
|
@any contributor @jsandai code |
|
@jsandai thanks, i've cleaned up small parts and reduced duplication. otherwise lgtm. will start a build after merge - lmk how it goes once you've updated! |
Description
This PR adds support for creating accounts on self-hosted Anytype (any-sync) networks by allowing users to specify a network configuration file during account creation.
Problem
When running a self-hosted Anytype network, users cannot easily create CLI accounts that connect to their own infrastructure. The CLI defaults to the official Anytype network, and there's no way to specify a custom network during account creation.
This addresses part of issue #6 and complements PR #8 which handles custom invite links.
Solution
--network-configflag forauth createanytype auth create my-bot --network-config ~/network.yamlPersist network config path — The path is saved to
~/.anytype/config.jsonso subsequent commands (login, serve) automatically use the same network.Auto-load network ID for
space join— When joining a space, the network ID is read from the saved config YAML, eliminating manual extraction.Changes
cmd/auth/create/create.go— Add--network-configflagcmd/auth/login/login.go— Load network config on logincore/auth.go— Pass network config to heart initializationcore/config/config.go— AddNetworkConfigPathfieldcore/config/config_helper.go— Add helpers for network config and network ID extractioncore/serviceprogram/serviceprogram.go— Use persisted network configcmd/space/join/join.go— Auto-load network ID from config YAMLUsage