pub struct KeyRecord {Show 17 fields
pub id: String,
pub hash: String,
pub scopes: HashSet<Scope>,
pub allowed_markets: Option<HashSet<String>>,
pub allowed_symbols: Option<HashSet<String>>,
pub max_order_value: Option<f64>,
pub max_daily_value: Option<f64>,
pub hours_window: Option<String>,
pub max_orders_per_minute: Option<u32>,
pub allowed_trd_sides: Option<HashSet<String>>,
pub allowed_acc_ids: Option<HashSet<u64>>,
pub allowed_card_nums: Option<Vec<String>>,
pub expires_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
pub note: Option<String>,
pub allowed_machines: Option<Vec<String>>,
pub raw_explicit_acc_ids: Option<HashSet<u64>>,
}Expand description
单条 key 的持久化格式
Fields§
§id: String人读 ID(审计日志用)
hash: String“sha256:<64 hex>”
scopes: HashSet<Scope>§allowed_markets: Option<HashSet<String>>§allowed_symbols: Option<HashSet<String>>§max_order_value: Option<f64>§max_daily_value: Option<f64>§hours_window: Option<String>“HH:MM-HH:MM” 服务器本地时区;跨午夜用 “22:00-04:00”
max_orders_per_minute: Option<u32>每 60s 最多下单次数(滑动窗口,None 表示不限)
allowed_trd_sides: Option<HashSet<String>>允许的交易方向,例如 ["SELL"];None 不限
allowed_acc_ids: Option<HashSet<u64>>v1.4.35:per-key acc_id 白名单。详见 Limits::allowed_acc_ids。
None / 空集 → 不限(向后兼容老 key)。
allowed_card_nums: Option<Vec<String>>v1.4.103 (B10): per-key card_num 白名单 (string format).
字符串格式接受:
- 完整 16 位 card_num:
"1001100100800000"(与 backendcard_number字段一致) - 末 4 位 suffix:
"7680"(App 显示的 “保证金综合账户(7680)” 末 4 位)
daemon 启动后调 GetAccList → 把 card_num 列表 resolve 成 acc_id, 合并
进 allowed_acc_ids (内部 storage 仍 u64). 多 suffix 撞 → log warn + skip
该条 (loud, 不静默接受). 找不到 → log warn + skip (用户后续 cache load
后可补回, 不 abort 防影响其他 keys).
设计动机 (用户 2026-04-29 反馈): App 显示的是 card_num 末 4 位
“保证金综合账户(7680)” / 完整 16 位 1001100100800000, 用户看不到内部
acc_id (e.g. 281756455983133952). 若 keys.json 只接受 acc_id, 用户必须
先调 /api/accounts 拿映射 → 认知负担. 加 allowed_card_nums 后用户可直接
写 App 看到的 4 位 / 16 位.
None / 空 → 不限 (与 allowed_acc_ids 一致语义, 向后兼容).
expires_at: Option<DateTime<Utc>>§created_at: DateTime<Utc>§note: Option<String>§allowed_machines: Option<Vec<String>>机器绑定指纹列表(软绑定,见 machine 模块)
None→ 未启用绑定,所有机器可用(向后兼容 v0.7.0 ~ v0.7.x 早期 key)Some(vec![])→ 强制锁定(无机器能通过),可用于临时冻结Some(vec!["<fingerprint_hex>", ...])→ 只允许这些机器
指纹由 machine::fingerprint_for(key_id) 生成,和 key_id 强耦合:
同一台机器上不同 key 的指纹不同。
raw_explicit_acc_ids: Option<HashSet<u64>>v1.4.106 F-P2-D: runtime-only — keys.json 文件源里原始
allowed_acc_ids 集合 (load_file 后立即 snapshot, sentinel 注入 + card_num
expansion 都不影响本字段).
用途: expand_allowed_card_nums 每次重新计算 resolved set 时从本字段
起步, 而不是从已 mutate 的 allowed_acc_ids 起步. 这样若 keys.json
没动但 cache 里某个 acc 不再可见, 重 expand 会自然不再含旧 resolved acc_id.
None = 文件源没设 allowed_acc_ids (与 file Option<HashSet> 区分:
Some(empty_set) 表示文件源显式空, None 表示文件源没字段).
#[serde(skip)] 不序列化进 keys.json (纯运行时), 不影响文件 wire format.
Implementations§
Source§impl KeyRecord
impl KeyRecord
Sourcepub fn generate(
id: impl Into<String>,
scopes: HashSet<Scope>,
limits: Option<Limits>,
expires_at: Option<DateTime<Utc>>,
note: Option<String>,
) -> (String, KeyRecord)
pub fn generate( id: impl Into<String>, scopes: HashSet<Scope>, limits: Option<Limits>, expires_at: Option<DateTime<Utc>>, note: Option<String>, ) -> (String, KeyRecord)
生成新 key:返回 (plaintext, record)
plaintext 只会返回给调用方一次,必须立即展示给用户;record 落盘。
Sourcepub fn generate_with_machines(
id: impl Into<String>,
scopes: HashSet<Scope>,
limits: Option<Limits>,
expires_at: Option<DateTime<Utc>>,
note: Option<String>,
allowed_machines: Option<Vec<String>>,
) -> (String, KeyRecord)
pub fn generate_with_machines( id: impl Into<String>, scopes: HashSet<Scope>, limits: Option<Limits>, expires_at: Option<DateTime<Utc>>, note: Option<String>, allowed_machines: Option<Vec<String>>, ) -> (String, KeyRecord)
同 [generate],但允许一次性设置 allowed_machines
Sourcepub fn check_machine(&self) -> Result<(), MachineError>
pub fn check_machine(&self) -> Result<(), MachineError>
检查本机是否在 allowed_machines 白名单里(None 时始终通过)
Sourcepub fn is_expired(&self, now: DateTime<Utc>) -> bool
pub fn is_expired(&self, now: DateTime<Utc>) -> bool
是否已过期