clawdentity-core 0.1.7

Core Rust library for Clawdentity identity, registry auth, relay, connector, and provider flows.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Duration;

use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use getrandom::fill as getrandom_fill;
use serde::{Deserialize, Serialize};

use crate::constants::{AGENTS_DIR, AIT_FILE_NAME, SECRET_KEY_FILE_NAME};
use crate::db::SqliteStore;
use crate::did::parse_agent_did;
use crate::error::{CoreError, Result};
use crate::http::blocking_client;
use crate::identity::decode_secret_key;
use crate::peers::{
    PersistPeerInput, load_peers_config, persist_peer, sync_openclaw_relay_peers_snapshot,
};
use crate::qr::decode_ticket_from_png;
use crate::signing::{SignHttpRequestInput, sign_http_request};

pub const PAIR_START_PATH: &str = "/pair/start";
pub const PAIR_CONFIRM_PATH: &str = "/pair/confirm";
pub const PAIR_STATUS_PATH: &str = "/pair/status";
pub const PAIRING_TICKET_PREFIX: &str = "clwpair1_";

pub const DEFAULT_STATUS_WAIT_SECONDS: u64 = 300;
pub const DEFAULT_STATUS_POLL_INTERVAL_SECONDS: u64 = 3;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PairProfile {
    pub agent_name: String,
    pub human_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub proxy_origin: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PairStartResult {
    pub initiator_agent_did: String,
    pub initiator_profile: PairProfile,
    pub ticket: String,
    pub expires_at: String,
    pub proxy_url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub qr_path: Option<PathBuf>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PairConfirmResult {
    pub paired: bool,
    pub initiator_agent_did: String,
    pub initiator_profile: PairProfile,
    pub responder_agent_did: String,
    pub responder_profile: PairProfile,
    pub proxy_url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub peer_alias: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PairStatusKind {
    Pending,
    Confirmed,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PairStatusResult {
    pub status: PairStatusKind,
    pub initiator_agent_did: String,
    pub initiator_profile: PairProfile,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub responder_agent_did: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub responder_profile: Option<PairProfile>,
    pub expires_at: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub confirmed_at: Option<String>,
    pub proxy_url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub peer_alias: Option<String>,
}

#[derive(Debug, Clone)]
pub enum PairConfirmInput {
    Ticket(String),
    QrFile(PathBuf),
}

#[derive(Debug, Clone)]
pub struct PairStatusOptions {
    pub wait: bool,
    pub wait_seconds: u64,
    pub poll_interval_seconds: u64,
}

impl Default for PairStatusOptions {
    fn default() -> Self {
        Self {
            wait: false,
            wait_seconds: DEFAULT_STATUS_WAIT_SECONDS,
            poll_interval_seconds: DEFAULT_STATUS_POLL_INTERVAL_SECONDS,
        }
    }
}

#[derive(Debug, Clone)]
struct LocalAgentProofMaterial {
    ait: String,
    secret_key: ed25519_dalek::SigningKey,
    agent_did: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PairStartResponsePayload {
    ticket: String,
    initiator_agent_did: String,
    initiator_profile: PairProfile,
    expires_at: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PairConfirmResponsePayload {
    paired: bool,
    initiator_agent_did: String,
    initiator_profile: PairProfile,
    responder_agent_did: String,
    responder_profile: PairProfile,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PairStatusResponsePayload {
    status: String,
    initiator_agent_did: String,
    initiator_profile: PairProfile,
    responder_agent_did: Option<String>,
    responder_profile: Option<PairProfile>,
    expires_at: String,
    confirmed_at: Option<String>,
}

fn parse_non_empty(value: &str, field: &str) -> Result<String> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return Err(CoreError::InvalidInput(format!("{field} is required")));
    }
    Ok(trimmed.to_string())
}

fn parse_proxy_url(candidate: &str) -> Result<String> {
    let parsed = url::Url::parse(candidate)
        .map_err(|_| CoreError::InvalidInput("proxyUrl is invalid".to_string()))?;
    if parsed.scheme() != "https" && parsed.scheme() != "http" {
        return Err(CoreError::InvalidInput("proxyUrl is invalid".to_string()));
    }
    Ok(parsed.to_string())
}

fn parse_pair_profile(profile: &PairProfile) -> Result<PairProfile> {
    Ok(PairProfile {
        agent_name: parse_non_empty(&profile.agent_name, "agentName")?,
        human_name: parse_non_empty(&profile.human_name, "humanName")?,
        proxy_origin: profile
            .proxy_origin
            .as_deref()
            .map(|value| value.trim().to_string())
            .filter(|value| !value.is_empty()),
    })
}

/// TODO(clawdentity): document `parse_pairing_ticket`.
pub fn parse_pairing_ticket(value: &str) -> Result<String> {
    let mut ticket = value.trim().trim_matches('`').to_string();
    ticket.retain(|character| !character.is_whitespace());
    if !ticket.starts_with(PAIRING_TICKET_PREFIX) {
        return Err(CoreError::InvalidInput(
            "pairing ticket is invalid".to_string(),
        ));
    }

    let encoded_payload = &ticket[PAIRING_TICKET_PREFIX.len()..];
    if encoded_payload.is_empty() {
        return Err(CoreError::InvalidInput(
            "pairing ticket is invalid".to_string(),
        ));
    }

    let payload_raw = URL_SAFE_NO_PAD
        .decode(encoded_payload)
        .map_err(|_| CoreError::InvalidInput("pairing ticket is invalid".to_string()))?;
    let payload_json = std::str::from_utf8(&payload_raw)
        .map_err(|_| CoreError::InvalidInput("pairing ticket is invalid".to_string()))?;
    let _: serde_json::Value = serde_json::from_str(payload_json)
        .map_err(|_| CoreError::InvalidInput("pairing ticket is invalid".to_string()))?;

    Ok(ticket)
}

/// TODO(clawdentity): document `parse_pairing_ticket_issuer_origin`.
pub fn parse_pairing_ticket_issuer_origin(ticket: &str) -> Result<String> {
    let ticket = parse_pairing_ticket(ticket)?;
    let encoded_payload = &ticket[PAIRING_TICKET_PREFIX.len()..];
    let payload_raw = URL_SAFE_NO_PAD
        .decode(encoded_payload)
        .map_err(|_| CoreError::InvalidInput("pairing ticket is invalid".to_string()))?;
    let payload: serde_json::Value = serde_json::from_slice(&payload_raw)
        .map_err(|_| CoreError::InvalidInput("pairing ticket is invalid".to_string()))?;
    let issuer = payload
        .get("iss")
        .and_then(|value| value.as_str())
        .ok_or_else(|| CoreError::InvalidInput("pairing ticket is invalid".to_string()))?;
    let issuer_url = url::Url::parse(issuer)
        .map_err(|_| CoreError::InvalidInput("pairing ticket is invalid".to_string()))?;
    if issuer_url.scheme() != "https" && issuer_url.scheme() != "http" {
        return Err(CoreError::InvalidInput(
            "pairing ticket is invalid".to_string(),
        ));
    }
    Ok(issuer_url.origin().unicode_serialization())
}

/// TODO(clawdentity): document `assert_ticket_issuer_matches_proxy`.
pub fn assert_ticket_issuer_matches_proxy(ticket: &str, proxy_url: &str) -> Result<()> {
    let issuer_origin = parse_pairing_ticket_issuer_origin(ticket)?;
    let proxy_origin = url::Url::parse(proxy_url)
        .map_err(|_| CoreError::InvalidInput("proxyUrl is invalid".to_string()))?
        .origin()
        .unicode_serialization();
    if issuer_origin != proxy_origin {
        return Err(CoreError::InvalidInput(format!(
            "pairing ticket issuer {issuer_origin} does not match proxy origin {proxy_origin}"
        )));
    }
    Ok(())
}

fn read_local_agent_proof_material(
    config_dir: &Path,
    agent_name: &str,
) -> Result<LocalAgentProofMaterial> {
    let normalized_agent_name = parse_non_empty(agent_name, "agentName")?;
    let agent_dir = config_dir.join(AGENTS_DIR).join(normalized_agent_name);
    let ait_path = agent_dir.join(AIT_FILE_NAME);
    let secret_key_path = agent_dir.join(SECRET_KEY_FILE_NAME);

    let ait = fs::read_to_string(&ait_path).map_err(|source| CoreError::Io {
        path: ait_path.clone(),
        source,
    })?;
    let ait = parse_non_empty(&ait, AIT_FILE_NAME)?;
    let secret_key_raw = fs::read_to_string(&secret_key_path).map_err(|source| CoreError::Io {
        path: secret_key_path.clone(),
        source,
    })?;
    let secret_key = decode_secret_key(secret_key_raw.trim())?;
    let agent_did = parse_ait_agent_did(&ait)?;

    Ok(LocalAgentProofMaterial {
        ait,
        secret_key,
        agent_did,
    })
}

fn parse_ait_agent_did(ait: &str) -> Result<String> {
    let parts: Vec<&str> = ait.split('.').collect();
    if parts.len() < 2 {
        return Err(CoreError::InvalidInput("agent AIT is invalid".to_string()));
    }
    let payload = URL_SAFE_NO_PAD
        .decode(parts[1])
        .map_err(|_| CoreError::InvalidInput("agent AIT is invalid".to_string()))?;
    let value: serde_json::Value = serde_json::from_slice(&payload)
        .map_err(|_| CoreError::InvalidInput("agent AIT is invalid".to_string()))?;
    let sub = value
        .get("sub")
        .and_then(|entry| entry.as_str())
        .ok_or_else(|| CoreError::InvalidInput("agent AIT is invalid".to_string()))?;
    parse_agent_did(sub)
        .map_err(|_| CoreError::InvalidInput("agent AIT is invalid".to_string()))?;
    Ok(sub.to_string())
}

fn to_request_url(proxy_url: &str, path: &str) -> Result<String> {
    let normalized_proxy = parse_proxy_url(proxy_url)?;
    let base = if normalized_proxy.ends_with('/') {
        normalized_proxy
    } else {
        format!("{normalized_proxy}/")
    };
    let joined = url::Url::parse(&base)
        .map_err(|_| CoreError::InvalidInput("proxyUrl is invalid".to_string()))?
        .join(path.trim_start_matches('/'))
        .map_err(|_| CoreError::InvalidInput("proxyUrl is invalid".to_string()))?;
    Ok(joined.to_string())
}

fn to_path_with_query(url: &str) -> Result<String> {
    let parsed = url::Url::parse(url)
        .map_err(|_| CoreError::InvalidInput("requestUrl is invalid".to_string()))?;
    Ok(match parsed.query() {
        Some(query) => format!("{}?{query}", parsed.path()),
        None => parsed.path().to_string(),
    })
}

fn build_signed_headers(
    method: &str,
    request_url: &str,
    body_bytes: &[u8],
    secret_key: &ed25519_dalek::SigningKey,
) -> Result<Vec<(String, String)>> {
    let mut nonce_bytes = [0_u8; 24];
    getrandom_fill(&mut nonce_bytes).map_err(|error| CoreError::InvalidInput(error.to_string()))?;
    let nonce = URL_SAFE_NO_PAD.encode(nonce_bytes);
    let timestamp = format!("{}", chrono::Utc::now().timestamp());
    let signed = sign_http_request(&SignHttpRequestInput {
        method,
        path_with_query: &to_path_with_query(request_url)?,
        timestamp: &timestamp,
        nonce: &nonce,
        body: body_bytes,
        secret_key,
    })?;
    Ok(signed.headers)
}

fn parse_registry_message(payload: &serde_json::Value, fallback: &str) -> String {
    payload
        .get("error")
        .and_then(|value| value.get("message"))
        .and_then(|value| value.as_str())
        .map(|value| value.to_string())
        .unwrap_or_else(|| fallback.to_string())
}

fn execute_pair_request(
    request_url: &str,
    ait: &str,
    body: serde_json::Value,
    secret_key: &ed25519_dalek::SigningKey,
) -> Result<serde_json::Value> {
    let request_body = serde_json::to_string(&body)?;
    let body_bytes = request_body.as_bytes();
    let signed_headers = build_signed_headers("POST", request_url, body_bytes, secret_key)?;
    tracing::debug!(request_url, "pair request sending");

    let mut request = blocking_client()?
        .post(request_url.to_string())
        .header("authorization", format!("Claw {}", ait.trim()))
        .header("content-type", "application/json");
    for (header_name, header_value) in signed_headers {
        request = request.header(header_name, header_value);
    }

    let response = request
        .body(request_body)
        .send()
        .map_err(|error| CoreError::Http(error.to_string()))?;
    let status = response.status().as_u16();
    tracing::debug!(request_url, status, "pair request completed");
    let payload: serde_json::Value = response
        .json()
        .map_err(|error| CoreError::Http(error.to_string()))?;
    if status >= 400 {
        return Err(CoreError::HttpStatus {
            status,
            message: parse_registry_message(&payload, "pairing request failed"),
        });
    }
    Ok(payload)
}

fn resolve_peer_proxy_url(
    ticket: &str,
    profile: &PairProfile,
    peer_proxy_origin: Option<String>,
) -> Result<String> {
    let resolved_origin = peer_proxy_origin
        .and_then(|value| {
            let trimmed = value.trim().to_string();
            if trimmed.is_empty() {
                None
            } else {
                Some(trimmed)
            }
        })
        .or_else(|| profile.proxy_origin.clone())
        .unwrap_or(parse_pairing_ticket_issuer_origin(ticket)?);
    let origin = parse_proxy_url(&resolved_origin)?;
    to_request_url(&origin, "/hooks/agent")
}

fn persist_confirmed_peer(
    store: &SqliteStore,
    config_dir: &Path,
    ticket: &str,
    peer_did: &str,
    peer_profile: &PairProfile,
    peer_proxy_origin: Option<String>,
) -> Result<String> {
    let peer_proxy_url = resolve_peer_proxy_url(ticket, peer_profile, peer_proxy_origin)?;
    let record = persist_peer(
        store,
        PersistPeerInput {
            alias: None,
            did: peer_did.to_string(),
            proxy_url: peer_proxy_url,
            agent_name: Some(peer_profile.agent_name.clone()),
            human_name: Some(peer_profile.human_name.clone()),
        },
    )?;
    let peers_config = load_peers_config(store)?;
    sync_openclaw_relay_peers_snapshot(config_dir, &peers_config)?;
    Ok(record.alias)
}

/// TODO(clawdentity): document `start_pairing`.
pub fn start_pairing(
    config_dir: &Path,
    agent_name: &str,
    proxy_url: &str,
    initiator_profile: PairProfile,
    ttl_seconds: Option<u64>,
) -> Result<PairStartResult> {
    let proof = read_local_agent_proof_material(config_dir, agent_name)?;
    let request_url = to_request_url(proxy_url, PAIR_START_PATH)?;
    let mut request_body = serde_json::Map::new();
    request_body.insert(
        "initiatorProfile".to_string(),
        serde_json::to_value(parse_pair_profile(&initiator_profile)?)?,
    );
    if let Some(ttl_seconds) = ttl_seconds {
        request_body.insert(
            "ttlSeconds".to_string(),
            serde_json::Value::Number(ttl_seconds.into()),
        );
    }
    let payload = execute_pair_request(
        &request_url,
        &proof.ait,
        serde_json::Value::Object(request_body),
        &proof.secret_key,
    )?;
    let parsed: PairStartResponsePayload = serde_json::from_value(payload)
        .map_err(|error| CoreError::InvalidInput(error.to_string()))?;
    Ok(PairStartResult {
        initiator_agent_did: parse_non_empty(&parsed.initiator_agent_did, "initiatorAgentDid")?,
        initiator_profile: parse_pair_profile(&parsed.initiator_profile)?,
        ticket: parse_pairing_ticket(&parsed.ticket)?,
        expires_at: parse_non_empty(&parsed.expires_at, "expiresAt")?,
        proxy_url: parse_proxy_url(proxy_url)?,
        qr_path: None,
    })
}

/// TODO(clawdentity): document `confirm_pairing`.
#[allow(clippy::too_many_lines)]
pub fn confirm_pairing(
    config_dir: &Path,
    store: &SqliteStore,
    agent_name: &str,
    confirm_input: PairConfirmInput,
    responder_profile: PairProfile,
) -> Result<PairConfirmResult> {
    let ticket = match confirm_input {
        PairConfirmInput::Ticket(ticket) => parse_pairing_ticket(&ticket)?,
        PairConfirmInput::QrFile(path) => {
            let image = fs::read(&path).map_err(|source| CoreError::Io { path, source })?;
            parse_pairing_ticket(&decode_ticket_from_png(&image)?)?
        }
    };

    let proof = read_local_agent_proof_material(config_dir, agent_name)?;
    let proxy_url = parse_pairing_ticket_issuer_origin(&ticket)?;
    let request_url = to_request_url(&proxy_url, PAIR_CONFIRM_PATH)?;
    let payload = execute_pair_request(
        &request_url,
        &proof.ait,
        serde_json::json!({
            "ticket": ticket,
            "responderProfile": parse_pair_profile(&responder_profile)?,
        }),
        &proof.secret_key,
    )?;

    let parsed: PairConfirmResponsePayload = serde_json::from_value(payload)
        .map_err(|error| CoreError::InvalidInput(error.to_string()))?;
    if !parsed.paired {
        return Err(CoreError::InvalidInput(
            "pair confirm response is invalid".to_string(),
        ));
    }

    let peer_alias = persist_confirmed_peer(
        store,
        config_dir,
        &ticket,
        &parsed.initiator_agent_did,
        &parsed.initiator_profile,
        parsed.initiator_profile.proxy_origin.clone(),
    )?;

    Ok(PairConfirmResult {
        paired: true,
        initiator_agent_did: parse_non_empty(&parsed.initiator_agent_did, "initiatorAgentDid")?,
        initiator_profile: parse_pair_profile(&parsed.initiator_profile)?,
        responder_agent_did: parse_non_empty(&parsed.responder_agent_did, "responderAgentDid")?,
        responder_profile: parse_pair_profile(&parsed.responder_profile)?,
        proxy_url,
        peer_alias: Some(peer_alias),
    })
}

#[allow(clippy::too_many_lines)]
fn get_pairing_status_once(
    config_dir: &Path,
    store: &SqliteStore,
    agent_name: &str,
    proxy_url: &str,
    ticket: &str,
) -> Result<PairStatusResult> {
    let ticket = parse_pairing_ticket(ticket)?;
    assert_ticket_issuer_matches_proxy(&ticket, proxy_url)?;
    let proof = read_local_agent_proof_material(config_dir, agent_name)?;
    let request_url = to_request_url(proxy_url, PAIR_STATUS_PATH)?;
    let payload = execute_pair_request(
        &request_url,
        &proof.ait,
        serde_json::json!({ "ticket": ticket }),
        &proof.secret_key,
    )?;

    let parsed: PairStatusResponsePayload = serde_json::from_value(payload)
        .map_err(|error| CoreError::InvalidInput(error.to_string()))?;
    let status = match parsed.status.as_str() {
        "pending" => PairStatusKind::Pending,
        "confirmed" => PairStatusKind::Confirmed,
        _ => {
            return Err(CoreError::InvalidInput(
                "pair status response is invalid".to_string(),
            ));
        }
    };

    let mut peer_alias = None;
    if status == PairStatusKind::Confirmed {
        let responder_agent_did = parsed.responder_agent_did.clone().ok_or_else(|| {
            CoreError::InvalidInput("pair status response is invalid".to_string())
        })?;
        let responder_profile = parsed.responder_profile.clone().ok_or_else(|| {
            CoreError::InvalidInput("pair status response is invalid".to_string())
        })?;

        let (peer_did, peer_profile) = if proof.agent_did == parsed.initiator_agent_did {
            (responder_agent_did, responder_profile)
        } else if proof.agent_did == responder_agent_did {
            (
                parsed.initiator_agent_did.clone(),
                parsed.initiator_profile.clone(),
            )
        } else {
            return Err(CoreError::InvalidInput(
                "local agent is not a pairing participant".to_string(),
            ));
        };

        peer_alias = Some(persist_confirmed_peer(
            store,
            config_dir,
            &ticket,
            &peer_did,
            &peer_profile,
            peer_profile.proxy_origin.clone(),
        )?);
    }

    Ok(PairStatusResult {
        status,
        initiator_agent_did: parsed.initiator_agent_did,
        initiator_profile: parsed.initiator_profile,
        responder_agent_did: parsed.responder_agent_did,
        responder_profile: parsed.responder_profile,
        expires_at: parsed.expires_at,
        confirmed_at: parsed.confirmed_at,
        proxy_url: parse_proxy_url(proxy_url)?,
        peer_alias,
    })
}

/// TODO(clawdentity): document `get_pairing_status`.
pub fn get_pairing_status(
    config_dir: &Path,
    store: &SqliteStore,
    agent_name: &str,
    proxy_url: &str,
    ticket: &str,
    options: PairStatusOptions,
) -> Result<PairStatusResult> {
    if !options.wait {
        return get_pairing_status_once(config_dir, store, agent_name, proxy_url, ticket);
    }

    let wait_seconds = if options.wait_seconds == 0 {
        DEFAULT_STATUS_WAIT_SECONDS
    } else {
        options.wait_seconds
    };
    let poll_interval_seconds = if options.poll_interval_seconds == 0 {
        DEFAULT_STATUS_POLL_INTERVAL_SECONDS
    } else {
        options.poll_interval_seconds
    };
    let deadline = chrono::Utc::now().timestamp() + wait_seconds as i64;
    loop {
        let status = get_pairing_status_once(config_dir, store, agent_name, proxy_url, ticket)?;
        if status.status == PairStatusKind::Confirmed {
            return Ok(status);
        }

        if chrono::Utc::now().timestamp() >= deadline {
            return Err(CoreError::InvalidInput(format!(
                "pairing is still pending after {wait_seconds} seconds"
            )));
        }
        std::thread::sleep(Duration::from_secs(poll_interval_seconds));
    }
}

#[cfg(test)]
#[path = "pairing_tests.rs"]
mod tests;