derec-library 0.0.4

Rust SDK for the DeRec protocol, including native and WebAssembly bindings.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.

//! The pre-pairing leg: `PrePairRequest` → `PrePairResponse`.
//!
//! A [`HashedKeys`](derec_proto::ContactMode::HashedKeys) or
//! [`NoKeys`](derec_proto::ContactMode::NoKeys) contact carries no usable key
//! material — a commitment or nothing at all — so the responder fetches it
//! over this round before the encrypted handshake in
//! [`pair`](super::pair) can begin. An `InlineKeys` contact skips this
//! leg entirely.
//!
//! This round travels in plaintext: no shared or asymmetric key exists yet,
//! so there is nothing to encrypt under. That is why its inbound halves are
//! reachable from outside [`handle`](super::handle) — the plaintext dispatch
//! in [`DeRecProtocol::process`](crate::protocol::DeRecProtocol::process)
//! decodes the body before any key is available and calls them directly.

use crate::extensions::advertised_endpoints::AdvertisedEndpoints as _;
use crate::extensions::sender_kind::SenderKindExt as _;
use crate::protocol::context::{Local, PairingConfig};
use crate::protocol::stores::{StoreSet, Stores};
use crate::protocol::{
    DeRecChannelStore, DeRecEvent, DeRecSecretStore, DeRecTransport, PairingKeyMaterial,
    PendingAction, SecretKind, SecretValue,
};
use crate::{
    Error, Result,
    derec_message::current_timestamp,
    primitives::pairing::{request, response},
    types::ChannelId,
};
use derec_proto::{
    ContactMessage, ContactMode, DeRecResult, MessageBody, PrePairRequestMessage,
    PrePairResponseMessage, SenderKind, StatusEnum, TransportProtocol,
};
use prost::Message;
use std::collections::HashMap;

// The same pairing inputs its sibling `pair::start` takes, and one more:
// the round token. Both legs are the same flow entered two ways.
#[allow(clippy::too_many_arguments)]
pub(super) async fn start<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    channel_id: ChannelId,
    contact: ContactMessage,
    peer_communication_info: HashMap<String, String>,
    endpoints: Vec<TransportProtocol>,
    kind: SenderKind,
    trace_id: u64,
) -> Result<u64> {
    super::reject_start_on_paired_channel(stores, local, channel_id).await?;

    let result = request::produce_pre_pair_request(local.own_transports.to_vec(), &contact)?;

    stores
        .secrets
        .save(
            local.secret_id,
            channel_id,
            SecretValue::PairingContact(contact),
        )
        .await?;

    super::persist_start_record(
        stores,
        local,
        channel_id,
        endpoints.clone(),
        peer_communication_info,
        kind,
        local.replica_id,
    )
    .await?;

    #[cfg(feature = "logging")]
    tracing::info!("PrePair request sent (scanner side)");

    let envelope = crate::derec_message::apply_trace_id(&result.envelope, trace_id)?;
    stores.transport.send(&endpoints, envelope).await?;

    Ok(channel_id.0)
}

/// `accept` arm for [`PendingAction::PrePair`]. Branches on the stored
/// contact's mode:
///
/// - `HashedKeys`: loads the previously-generated `PairingSecret` and
///   publishes its keys (the scanner will match them against the
///   contact's binding hash).
/// - `NoKeys`: no `PairingSecret` exists yet — the contact was created
///   without any key material. Generates fresh key material on the fly,
///   persists it as `PairingSecret`, and publishes the keys. First
///   authenticates the request by matching `request.nonce` against the
///   stored contact's `nonce`.
// Compatibility, not oversight — see the `transport` module docs.
#[allow(deprecated)]
pub(in crate::protocol) async fn accept<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    channel_id: ChannelId,
    request: &PrePairRequestMessage,
    trace_id: u64,
) -> Result<Vec<DeRecEvent>> {
    let policy = local.policy;
    let secret_id = local.secret_id;
    if let Some(SecretValue::PairingContact(contact)) = stores
        .secrets
        .load(secret_id, channel_id, SecretKind::PairingContact)
        .await?
        && contact.contact_mode == ContactMode::NoKeys as i32
    {
        if request.nonce != contact.nonce {
            #[cfg(feature = "logging")]
            tracing::warn!(
                channel_id = channel_id.0,
                "NoKeys PrePair nonce mismatch — rejecting"
            );
            return Err(crate::primitives::pairing::PairingError::ProtocolViolation(
                "NoKeys PrePair nonce mismatch",
            )
            .into());
        }

        let result = response::produce_pre_pair_no_keys(channel_id, request)?;
        stores
            .secrets
            .save(
                secret_id,
                channel_id,
                SecretValue::PairingSecret(PairingKeyMaterial::from_secret(
                    &result.pairing_secret_key_material,
                )),
            )
            .await?;

        let envelope = crate::derec_message::apply_trace_id(&result.envelope, trace_id)?;
        // Every endpoint the requester advertised, so a reply that cannot
        // reach the first can fall back. Never empty: `validate` refuses a
        // request naming none.
        let endpoints = policy.admit_peer_endpoints(request.advertised_endpoints())?;
        let endpoint = endpoints[0].clone();
        // PrePair is plaintext and takes its own dispatch path, so it never
        // passes the `peer_supplied_endpoints` funnel the encrypted paths
        // run. This is the only point the policy can be applied to an
        // endpoint the peer chose, and it is where the endpoint is dialled.
        policy.check_peer(&endpoint)?;
        stores
            .transport
            .send(std::slice::from_ref(&endpoint), envelope)
            .await?;

        #[cfg(feature = "logging")]
        tracing::info!(
            channel_id = channel_id.0,
            "NoKeys PrePair accepted; fresh keys generated and sent to scanner"
        );

        return Ok(vec![]);
    }

    let Some(SecretValue::PairingSecret(pairing_secret)) = stores
        .secrets
        .load(secret_id, channel_id, SecretKind::PairingSecret)
        .await?
    else {
        return Err(Error::Invariant(
            "PrePair accept: missing PairingSecret (initiator state lost)",
        ));
    };
    let pairing_secret = pairing_secret.to_secret()?;

    let result = response::produce_pre_pair(channel_id, request, &pairing_secret)?;
    let envelope = crate::derec_message::apply_trace_id(&result.envelope, trace_id)?;

    // Same as the NoKeys branch: take every advertised endpoint, filtered.
    let endpoints = policy.admit_peer_endpoints(request.advertised_endpoints())?;
    let endpoint = endpoints[0].clone();
    // Same gate as the NoKeys branch above: this plaintext path bypasses the
    // `peer_supplied_endpoints` funnel, so the policy is applied here.
    policy.check_peer(&endpoint)?;
    stores
        .transport
        .send(std::slice::from_ref(&endpoint), envelope)
        .await?;

    #[cfg(feature = "logging")]
    tracing::info!(
        channel_id = channel_id.0,
        "PrePair accepted; published keys sent to scanner"
    );

    Ok(vec![])
}

/// `reject` arm for [`PendingAction::PrePair`]. Builds a non-Ok
/// `PrePairResponse` (no keys) and sends it to the scanner's `replyTo`.
/// Does NOT load `PairingSecret` — rejection carries no crypto material.
// Compatibility, not oversight — see the `transport` module docs.
#[allow(deprecated)]
pub(in crate::protocol) async fn reject<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    channel_id: ChannelId,
    request: &PrePairRequestMessage,
    status: StatusEnum,
    memo: &str,
    trace_id: u64,
) -> Result<()> {
    // The rejection is dialled at a peer-chosen endpoint just like the
    // acceptance, so it passes the same local.policy gate.
    let endpoints = local
        .policy
        .admit_peer_endpoints(request.advertised_endpoints())?;
    let endpoint = endpoints[0].clone();

    let timestamp = current_timestamp();
    let response = PrePairResponseMessage {
        result: Some(DeRecResult {
            status: status as i32,
            memo: memo.to_owned(),
        }),
        mlkem_encapsulation_key: None,
        ecies_public_key: None,
        nonce: request.nonce,
        timestamp: Some(timestamp),
    };

    use crate::protocol_version::ProtocolVersion;
    use derec_proto::DeRecMessage;
    let protocol_version = ProtocolVersion::current();
    let envelope = DeRecMessage {
        protocol_version_major: protocol_version.major,
        protocol_version_minor: protocol_version.minor,
        sequence: 0,
        channel_id: channel_id.into(),
        timestamp: Some(timestamp),
        message: MessageBody::PrePairResponse(response).encode_to_vec(),
        trace_id,
    }
    .encode_to_vec();

    stores
        .transport
        .send(std::slice::from_ref(&endpoint), envelope)
        .await?;

    #[cfg(feature = "logging")]
    tracing::info!(
        channel_id = channel_id.0,
        status = status as i32,
        "PrePair rejected by initiator"
    );

    Ok(())
}

/// Inbound `PrePairResponse` on the **scanner side**. Validates the
/// published keys against the stored contact's `contact_binding_hash`,
/// synthesizes a filled-in `InlineKeys`-shaped contact, and auto-proceeds
/// to a regular `PairRequest`.
///
/// PrePair success is invisible to the application on this side — the
/// next event the app sees is `PairingCompleted` once the PairResponse
/// round-trip lands. Failure surfaces as either a [`PrePairRejected`]
/// event (non-Ok status on the response) or a
/// [`crate::primitives::pairing::PairingError::PrePairHashMismatch`]
/// error (binding-hash mismatch).
///
/// The local sender kind is recovered from whichever record `start` wrote,
/// and the two shapes differ: a helper or owner is stored as a
/// [`ChannelRecord::Helper`](crate::protocol::types::ChannelRecord) carrying
/// the peer's role, while a replica is stored as its **own roster row** —
/// the roster is absolute, so a member records itself rather than a view of
/// its peer. Resolving only the helper shape made `HashedKeys` and `NoKeys`
/// unusable for replica pairing, those being the two modes with a PrePair
/// leg.
pub(in crate::protocol) async fn on_response<S: StoreSet>(
    stores: &mut Stores<'_, S>,
    local: &Local<'_>,
    pairing: &PairingConfig<'_>,
    channel_id: ChannelId,
    original_contact: &ContactMessage,
    response: &PrePairResponseMessage,
) -> Result<Vec<DeRecEvent>> {
    let replica_id = local.replica_id;
    let secret_id = local.secret_id;
    let processed = if original_contact.contact_mode == ContactMode::NoKeys as i32 {
        response::process_pre_pair_no_keys(original_contact, response)
    } else {
        response::process_pre_pair(original_contact, response)
    };
    let validated = match processed {
        Ok(v) => v,
        Err(Error::Pairing(crate::primitives::pairing::PairingError::NonOkStatus {
            status,
            memo,
        })) => {
            #[cfg(feature = "logging")]
            tracing::warn!(
                channel_id = channel_id.0,
                status,
                memo = %memo,
                "PrePair rejected by initiator"
            );

            return Ok(vec![DeRecEvent::PrePairRejected {
                channel_id,
                status,
                memo,
            }]);
        }
        Err(e) => return Err(e),
    };

    // The keys have arrived, so the contact is now inline-keys-shaped and can
    // drive `request::produce` like any other.
    let filled_in_contact = ContactMessage {
        mlkem_encapsulation_key: Some(validated.mlkem_encapsulation_key),
        ecies_public_key: Some(validated.ecies_public_key),
        contact_mode: ContactMode::InlineKeys as i32,
        contact_binding_hash: None,
        ..original_contact.clone()
    };

    // What gets *stored*, though, must keep the mode the handshake actually
    // ran under — it is what decides whether the finished channel waits for a
    // fingerprint. For `HashedKeys` the relabel above is truthful: the keys
    // were just checked against the commitment, so they are worth exactly what
    // inlined keys are worth. For `NoKeys` nothing verified them, and calling
    // the contact `InlineKeys` would claim a trust level the flow never
    // earned. This value never reaches the wire and is dropped on rekey;
    // `response::process` reads only the key fields from it.
    let stored_contact = ContactMessage {
        contact_mode: original_contact.contact_mode,
        ..filled_in_contact.clone()
    };

    let local_kind = match stores
        .channels
        .load(
            secret_id,
            crate::protocol::types::ChannelQuery::Helper { channel_id },
        )
        .await?
        .and_then(|r| r.as_helper().cloned())
    {
        Some(helper) => helper.peer_role.counterparty(),
        None => {
            let own_replica_id = replica_id.ok_or(Error::Invariant(
                "channel record missing on PrePair response — start must be called first",
            ))?;
            stores
                .channels
                .load(
                    secret_id,
                    crate::protocol::types::ChannelQuery::Replica {
                        channel_id,
                        replica_id: crate::types::ReplicaId::try_from(own_replica_id)?,
                    },
                )
                .await?
                .and_then(|r| r.as_replica().cloned())
                .ok_or(Error::Invariant(
                    "channel record missing on PrePair response — start must be called first",
                ))?
                .role
                .to_sender_kind()
        }
    };

    let replica_id_to_inject = super::require_replica_id_for_kind(local_kind, replica_id)?;
    let comm_info =
        super::build_communication_info(pairing.communication_info, replica_id_to_inject);
    let result = request::produce(
        local_kind,
        local.own_transports.to_vec(),
        &filled_in_contact,
        comm_info,
        pairing.parameter_range.cloned(),
    )?;

    stores
        .secrets
        .save(
            secret_id,
            channel_id,
            SecretValue::PairingSecret(PairingKeyMaterial::from_secret(&result.secret_key)),
        )
        .await?;
    stores
        .secrets
        .save(
            secret_id,
            channel_id,
            SecretValue::PairingContact(stored_contact),
        )
        .await?;

    let endpoints = local
        .policy
        .admit_peer_endpoints(original_contact.advertised_endpoints())?;
    let envelope = crate::derec_message::apply_trace_id(
        &result.envelope,
        crate::derec_message::fresh_trace_id(),
    )?;
    stores.transport.send(&endpoints, envelope).await?;

    #[cfg(feature = "logging")]
    tracing::info!(
        channel_id = channel_id.0,
        "PrePair validated; PairRequest sent (HASHED_KEYS scanner side)"
    );

    Ok(vec![])
}

/// Inbound `PrePairRequest` on the **initiator side**. Reached either by
/// the party that:
///
/// - created a `HashedKeys` contact and saved its `PairingSecret`, or
/// - created a `NoKeys` contact and saved only its `PairingContact`.
///
/// Pure function — surfaces a [`DeRecEvent::ActionRequired`] event. The
/// application decides whether to accept (publish the keys — for NoKeys
/// keys are generated on the fly at accept time) or reject (refuse to
/// participate) by calling [`DeRecProtocol::accept`](crate::protocol::DeRecProtocol::accept)
/// or [`DeRecProtocol::reject`](crate::protocol::DeRecProtocol::reject) with
/// the carried [`PendingAction::PrePair`].
pub(in crate::protocol) fn on_request(
    channel_id: ChannelId,
    request: &PrePairRequestMessage,
    trace_id: u64,
) -> Result<Vec<DeRecEvent>> {
    Ok(vec![DeRecEvent::ActionRequired {
        channel_id,
        action: PendingAction::PrePair {
            channel_id,
            request: request.clone(),
            trace_id,
        },
    }])
}