ForcePilot/backend/package/yuxi/channel/extensions/nostr/account.py
Kris 16455cb303 feat(channel): 添加 Nostr 渠道扩展
新增 Nostr 渠道扩展,支持在 Yuxi 平台中集成 Nostr 去中心化社交协议。

包含以下功能模块:
- bus: 事件总线与中继通信
- account: 账户管理
- key_utils: 密钥工具
- gateway: SSE/WebSocket 网关接入
- outbound: 外发消息管理
- gift_wrap: Gift Wrap 加密
- nip44: NIP-44 加密协议
- config_schema: 配置模式
- defaults: 默认配置
- profile_core: 用户资料核心
- profile_publisher: 资料发布
- event_utils: 事件工具
- deletion: 事件删除
- reactions: 表情反应
- metrics: 指标监控
- seen_tracker: 已读追踪
- session_route: 会话路由
- state_store: 状态存储
2026-05-21 11:31:51 +08:00

54 lines
1.5 KiB
Python

from dataclasses import dataclass, field
from .defaults import DEFAULT_RELAYS
from .key_utils import get_public_key
@dataclass
class ResolvedNostrAccount:
account_id: str = "default"
name: str | None = None
enabled: bool = True
configured: bool = False
private_key: str = ""
public_key: str = ""
relays: list[str] = field(default_factory=lambda: [*DEFAULT_RELAYS])
profile: dict | None = None
config: dict = field(default_factory=dict)
def list_nostr_account_ids(config: dict) -> list[str]:
accounts = config.get("channels", {}).get("nostr", {}).get("accounts", {})
if accounts:
return list(accounts.keys())
return ["default"]
def resolve_nostr_account(config: dict, account_id: str = "default") -> ResolvedNostrAccount:
nostr_cfg = config.get("channels", {}).get("nostr", {})
account_cfg = nostr_cfg.get("accounts", {}).get(account_id, nostr_cfg)
private_key = account_cfg.get("privateKey", "")
configured = bool(private_key)
public_key = ""
if configured:
try:
public_key = get_public_key(private_key)
except Exception:
pass
relays = account_cfg.get("relays", []) or DEFAULT_RELAYS
return ResolvedNostrAccount(
account_id=account_id,
name=account_cfg.get("name"),
enabled=account_cfg.get("enabled", True),
configured=configured,
private_key=private_key,
public_key=public_key,
relays=relays,
profile=account_cfg.get("profile"),
config=account_cfg,
)