2026-06-21 16:14:07 +02:00
|
|
|
// waste-daemon: the local peer process.
|
|
|
|
|
// Run one of these on each friend's machine.
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
2026-06-22 21:44:48 +02:00
|
|
|
"fmt"
|
2026-06-21 16:14:07 +02:00
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"github.com/waste-go/internal/crypto"
|
|
|
|
|
"github.com/waste-go/internal/ipc"
|
Multi-network foundation: netmgr, derived identities, additive IPC protocol
YAW/2 peer wire protocol is unchanged. Changes are local only.
internal/crypto:
- DeriveForNetwork(master, networkHash) — HKDF-SHA256 from master seed + network hash;
same master + same network always produces the same Ed25519 keypair (stable peer ID)
internal/proto:
- NetworkInfo type (network_id, network_name, local_peer)
- NetworkID field on IpcMessage (optional; commands default to first network when absent)
- Networks []NetworkInfo on state_snapshot (additive alongside existing local_peer)
- EvtNetworkJoined / EvtNetworkLeft events
internal/netmgr (new):
- Manager holds N independent Network contexts (derived identity, mesh, store, anchor)
- Join(name) creates context, derives identity, opens per-network DB, starts anchor client
- Leave(id) / LeaveAll() cancel contexts and close stores
- Resolve(netID) returns named network, or Default() when netID is empty (backward compat)
- Fan-out: Manager.Subscribe() receives tagged events from all networks
- Network IDs are the first 8 hex chars of SHA-256("yaw2-net:"+name) — stable and short
internal/ipc:
- Run(mgr, port) replaces Run(m *mesh.Mesh, port, anchorURL, joinFn)
- Commands without network_id route to mgr.Default() (backward compat)
- state_snapshot includes Networks array; local_peer/connected_peers still populated from first network
- generate_invite, get_file_list, send_message all respect network_id routing
cmd/daemon:
- Creates netmgr.Manager instead of mesh.Mesh directly
- --join and --share-dir pass through Config
- Auto-join via mgr.Join() before IPC starts
test-network.sh:
- Fix peer_name bash bug: $() with && inside triggers set -e; use if/elif/else instead
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-21 19:13:54 +02:00
|
|
|
"github.com/waste-go/internal/invite"
|
|
|
|
|
"github.com/waste-go/internal/netmgr"
|
2026-06-21 16:14:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2026-06-22 21:44:48 +02:00
|
|
|
dataDir := flag.String("data-dir", "~/.waste", "path to identity/config directory")
|
|
|
|
|
alias := flag.String("alias", "anon", "display name shown to peers (advisory only)")
|
|
|
|
|
ipcPort := flag.Int("ipc-port", 17337, "port for local IPC (UI connects here)")
|
|
|
|
|
anchorURL := flag.String("anchor", "", "anchor WebSocket URL, e.g. ws://your-vps:17339/ws")
|
|
|
|
|
shareDir := flag.String("share-dir", "", "directory to share with peers on the network")
|
|
|
|
|
joinInvite := flag.String("join", "", "waste: invite string — sets anchor URL and auto-joins the network on startup")
|
|
|
|
|
importBackup := flag.String("import-identity", "", "path to a yaw-key-backup-1 JSON file to import")
|
|
|
|
|
importPassword := flag.String("import-passphrase", "", "passphrase for --import-identity")
|
2026-06-21 16:14:07 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
|
Multi-network foundation: netmgr, derived identities, additive IPC protocol
YAW/2 peer wire protocol is unchanged. Changes are local only.
internal/crypto:
- DeriveForNetwork(master, networkHash) — HKDF-SHA256 from master seed + network hash;
same master + same network always produces the same Ed25519 keypair (stable peer ID)
internal/proto:
- NetworkInfo type (network_id, network_name, local_peer)
- NetworkID field on IpcMessage (optional; commands default to first network when absent)
- Networks []NetworkInfo on state_snapshot (additive alongside existing local_peer)
- EvtNetworkJoined / EvtNetworkLeft events
internal/netmgr (new):
- Manager holds N independent Network contexts (derived identity, mesh, store, anchor)
- Join(name) creates context, derives identity, opens per-network DB, starts anchor client
- Leave(id) / LeaveAll() cancel contexts and close stores
- Resolve(netID) returns named network, or Default() when netID is empty (backward compat)
- Fan-out: Manager.Subscribe() receives tagged events from all networks
- Network IDs are the first 8 hex chars of SHA-256("yaw2-net:"+name) — stable and short
internal/ipc:
- Run(mgr, port) replaces Run(m *mesh.Mesh, port, anchorURL, joinFn)
- Commands without network_id route to mgr.Default() (backward compat)
- state_snapshot includes Networks array; local_peer/connected_peers still populated from first network
- generate_invite, get_file_list, send_message all respect network_id routing
cmd/daemon:
- Creates netmgr.Manager instead of mesh.Mesh directly
- --join and --share-dir pass through Config
- Auto-join via mgr.Join() before IPC starts
test-network.sh:
- Fix peer_name bash bug: $() with && inside triggers set -e; use if/elif/else instead
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-21 19:13:54 +02:00
|
|
|
dir := expandHome(*dataDir)
|
|
|
|
|
|
2026-06-22 21:44:48 +02:00
|
|
|
// --import-identity: decrypt backup and write identity.json, then exit.
|
|
|
|
|
if *importBackup != "" {
|
|
|
|
|
if *importPassword == "" {
|
|
|
|
|
log.Fatal("--import-passphrase is required with --import-identity")
|
|
|
|
|
}
|
|
|
|
|
raw, err := os.ReadFile(*importBackup)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("import-identity: read file: %v", err)
|
|
|
|
|
}
|
|
|
|
|
imported, err := crypto.ImportIdentity(raw, *importPassword)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("import-identity: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
|
|
|
log.Fatalf("import-identity: mkdir: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if err := crypto.SaveIdentity(dir, imported); err != nil {
|
|
|
|
|
log.Fatalf("import-identity: save: %v", err)
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("identity imported: %s\n", imported.PeerID())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
Multi-network foundation: netmgr, derived identities, additive IPC protocol
YAW/2 peer wire protocol is unchanged. Changes are local only.
internal/crypto:
- DeriveForNetwork(master, networkHash) — HKDF-SHA256 from master seed + network hash;
same master + same network always produces the same Ed25519 keypair (stable peer ID)
internal/proto:
- NetworkInfo type (network_id, network_name, local_peer)
- NetworkID field on IpcMessage (optional; commands default to first network when absent)
- Networks []NetworkInfo on state_snapshot (additive alongside existing local_peer)
- EvtNetworkJoined / EvtNetworkLeft events
internal/netmgr (new):
- Manager holds N independent Network contexts (derived identity, mesh, store, anchor)
- Join(name) creates context, derives identity, opens per-network DB, starts anchor client
- Leave(id) / LeaveAll() cancel contexts and close stores
- Resolve(netID) returns named network, or Default() when netID is empty (backward compat)
- Fan-out: Manager.Subscribe() receives tagged events from all networks
- Network IDs are the first 8 hex chars of SHA-256("yaw2-net:"+name) — stable and short
internal/ipc:
- Run(mgr, port) replaces Run(m *mesh.Mesh, port, anchorURL, joinFn)
- Commands without network_id route to mgr.Default() (backward compat)
- state_snapshot includes Networks array; local_peer/connected_peers still populated from first network
- generate_invite, get_file_list, send_message all respect network_id routing
cmd/daemon:
- Creates netmgr.Manager instead of mesh.Mesh directly
- --join and --share-dir pass through Config
- Auto-join via mgr.Join() before IPC starts
test-network.sh:
- Fix peer_name bash bug: $() with && inside triggers set -e; use if/elif/else instead
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-21 19:13:54 +02:00
|
|
|
id, err := crypto.LoadOrCreate(dir, *alias)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("identity: %v", err)
|
|
|
|
|
}
|
|
|
|
|
log.Printf("daemon: local peer id: %s alias: %s", id.PeerID().Short(), id.Alias)
|
|
|
|
|
|
2026-06-21 18:56:24 +02:00
|
|
|
// --join overrides/sets the anchor URL and triggers an auto-join.
|
|
|
|
|
var autoJoinNetwork string
|
|
|
|
|
if *joinInvite != "" {
|
|
|
|
|
inv, err := invite.Decode(*joinInvite)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("invalid invite: %v", err)
|
|
|
|
|
}
|
|
|
|
|
*anchorURL = inv.Anchor
|
|
|
|
|
autoJoinNetwork = inv.Network
|
|
|
|
|
log.Printf("daemon: invite decoded — anchor=%s network=%s", inv.Anchor, inv.Network)
|
|
|
|
|
}
|
|
|
|
|
|
Multi-network foundation: netmgr, derived identities, additive IPC protocol
YAW/2 peer wire protocol is unchanged. Changes are local only.
internal/crypto:
- DeriveForNetwork(master, networkHash) — HKDF-SHA256 from master seed + network hash;
same master + same network always produces the same Ed25519 keypair (stable peer ID)
internal/proto:
- NetworkInfo type (network_id, network_name, local_peer)
- NetworkID field on IpcMessage (optional; commands default to first network when absent)
- Networks []NetworkInfo on state_snapshot (additive alongside existing local_peer)
- EvtNetworkJoined / EvtNetworkLeft events
internal/netmgr (new):
- Manager holds N independent Network contexts (derived identity, mesh, store, anchor)
- Join(name) creates context, derives identity, opens per-network DB, starts anchor client
- Leave(id) / LeaveAll() cancel contexts and close stores
- Resolve(netID) returns named network, or Default() when netID is empty (backward compat)
- Fan-out: Manager.Subscribe() receives tagged events from all networks
- Network IDs are the first 8 hex chars of SHA-256("yaw2-net:"+name) — stable and short
internal/ipc:
- Run(mgr, port) replaces Run(m *mesh.Mesh, port, anchorURL, joinFn)
- Commands without network_id route to mgr.Default() (backward compat)
- state_snapshot includes Networks array; local_peer/connected_peers still populated from first network
- generate_invite, get_file_list, send_message all respect network_id routing
cmd/daemon:
- Creates netmgr.Manager instead of mesh.Mesh directly
- --join and --share-dir pass through Config
- Auto-join via mgr.Join() before IPC starts
test-network.sh:
- Fix peer_name bash bug: $() with && inside triggers set -e; use if/elif/else instead
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-21 19:13:54 +02:00
|
|
|
mgr := netmgr.New(netmgr.Config{
|
|
|
|
|
MasterIdentity: id,
|
|
|
|
|
StoreDir: dir,
|
|
|
|
|
AnchorURL: *anchorURL,
|
|
|
|
|
ShareDir: expandHome(*shareDir),
|
|
|
|
|
})
|
2026-06-21 18:04:42 +02:00
|
|
|
|
2026-06-21 18:56:24 +02:00
|
|
|
if autoJoinNetwork != "" {
|
2026-06-22 15:13:26 +02:00
|
|
|
if _, err := mgr.Join(autoJoinNetwork, ""); err != nil {
|
Multi-network foundation: netmgr, derived identities, additive IPC protocol
YAW/2 peer wire protocol is unchanged. Changes are local only.
internal/crypto:
- DeriveForNetwork(master, networkHash) — HKDF-SHA256 from master seed + network hash;
same master + same network always produces the same Ed25519 keypair (stable peer ID)
internal/proto:
- NetworkInfo type (network_id, network_name, local_peer)
- NetworkID field on IpcMessage (optional; commands default to first network when absent)
- Networks []NetworkInfo on state_snapshot (additive alongside existing local_peer)
- EvtNetworkJoined / EvtNetworkLeft events
internal/netmgr (new):
- Manager holds N independent Network contexts (derived identity, mesh, store, anchor)
- Join(name) creates context, derives identity, opens per-network DB, starts anchor client
- Leave(id) / LeaveAll() cancel contexts and close stores
- Resolve(netID) returns named network, or Default() when netID is empty (backward compat)
- Fan-out: Manager.Subscribe() receives tagged events from all networks
- Network IDs are the first 8 hex chars of SHA-256("yaw2-net:"+name) — stable and short
internal/ipc:
- Run(mgr, port) replaces Run(m *mesh.Mesh, port, anchorURL, joinFn)
- Commands without network_id route to mgr.Default() (backward compat)
- state_snapshot includes Networks array; local_peer/connected_peers still populated from first network
- generate_invite, get_file_list, send_message all respect network_id routing
cmd/daemon:
- Creates netmgr.Manager instead of mesh.Mesh directly
- --join and --share-dir pass through Config
- Auto-join via mgr.Join() before IPC starts
test-network.sh:
- Fix peer_name bash bug: $() with && inside triggers set -e; use if/elif/else instead
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-21 19:13:54 +02:00
|
|
|
log.Fatalf("auto-join: %v", err)
|
|
|
|
|
}
|
2026-06-21 18:56:24 +02:00
|
|
|
}
|
|
|
|
|
|
Multi-network foundation: netmgr, derived identities, additive IPC protocol
YAW/2 peer wire protocol is unchanged. Changes are local only.
internal/crypto:
- DeriveForNetwork(master, networkHash) — HKDF-SHA256 from master seed + network hash;
same master + same network always produces the same Ed25519 keypair (stable peer ID)
internal/proto:
- NetworkInfo type (network_id, network_name, local_peer)
- NetworkID field on IpcMessage (optional; commands default to first network when absent)
- Networks []NetworkInfo on state_snapshot (additive alongside existing local_peer)
- EvtNetworkJoined / EvtNetworkLeft events
internal/netmgr (new):
- Manager holds N independent Network contexts (derived identity, mesh, store, anchor)
- Join(name) creates context, derives identity, opens per-network DB, starts anchor client
- Leave(id) / LeaveAll() cancel contexts and close stores
- Resolve(netID) returns named network, or Default() when netID is empty (backward compat)
- Fan-out: Manager.Subscribe() receives tagged events from all networks
- Network IDs are the first 8 hex chars of SHA-256("yaw2-net:"+name) — stable and short
internal/ipc:
- Run(mgr, port) replaces Run(m *mesh.Mesh, port, anchorURL, joinFn)
- Commands without network_id route to mgr.Default() (backward compat)
- state_snapshot includes Networks array; local_peer/connected_peers still populated from first network
- generate_invite, get_file_list, send_message all respect network_id routing
cmd/daemon:
- Creates netmgr.Manager instead of mesh.Mesh directly
- --join and --share-dir pass through Config
- Auto-join via mgr.Join() before IPC starts
test-network.sh:
- Fix peer_name bash bug: $() with && inside triggers set -e; use if/elif/else instead
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-21 19:13:54 +02:00
|
|
|
if err := ipc.Run(mgr, *ipcPort); err != nil {
|
2026-06-21 16:14:07 +02:00
|
|
|
log.Fatalf("ipc: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func expandHome(path string) string {
|
|
|
|
|
if len(path) >= 2 && path[:2] == "~/" {
|
|
|
|
|
if home, err := os.UserHomeDir(); err == nil {
|
|
|
|
|
return home + path[1:]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return path
|
|
|
|
|
}
|