Skip to main content

KeyRecord

Struct KeyRecord 

Source
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" (与 backend card_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

Source

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 落盘。

Source

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

Source

pub fn check_machine(&self) -> Result<(), MachineError>

检查本机是否在 allowed_machines 白名单里(None 时始终通过)

Source

pub fn matches(&self, plaintext: &str) -> bool

校验明文与当前记录的 hash 是否一致

Source

pub fn is_expired(&self, now: DateTime<Utc>) -> bool

是否已过期

Source

pub fn hours_range(&self) -> Result<Option<(NaiveTime, NaiveTime)>, String>

解析 hours_window 为 (start, end);None 表示不限

Source

pub fn limits(&self) -> Limits

导出为 Limits

Trait Implementations§

Source§

impl Clone for KeyRecord

Source§

fn clone(&self) -> KeyRecord

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KeyRecord

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for KeyRecord

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for KeyRecord

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,