derec-library 0.1.0

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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.

//! Inbound shape of the recovered [`Secret`] a binding hands back to
//! [`crate::protocol::DeRecProtocol::restore`].
//!
//! It is the deserializing counterpart of the `SecretRecovered` event's
//! `secret` field: an application passes that object back verbatim, so every
//! field name and encoding here matches what the event emits. Both the WASM
//! and FFI bindings decode through this one type so the two cannot disagree on
//! what a well-formed recovered secret is.
//!
//! Every `u64` identifier (`channel_id`, `replica_id`) is a decimal string, the
//! encoding the event emits. It is required and parsed strictly: an absent,
//! empty, signed, or out-of-range id is rejected rather than read as `0`,
//! which would restore state under an id the recovered secret never named.

use std::collections::HashMap;

use derec_proto::TransportProtocol;

use crate::protocol::types::{HelperInfo, ReplicaInfo, ReplicaRole, Replicas, Secret, UserSecret};

/// One advertised endpoint: a URI plus the protocol name (`"https"`,
/// `"grpc"`), the same shape `SecretRecovered` carries, so no protocol is
/// inferred from a scheme.
#[derive(serde::Deserialize)]
struct EndpointIn {
    uri: String,
    #[serde(deserialize_with = "crate::interop::protocol_names::protocol_from_name")]
    protocol: i32,
}

impl From<EndpointIn> for TransportProtocol {
    fn from(e: EndpointIn) -> Self {
        TransportProtocol {
            uri: e.uri,
            protocol: e.protocol,
        }
    }
}

/// `transports` absent, `null` or `[]` all decode to an empty list: an
/// entry with no endpoint is the core's to judge, and `restore` skips it
/// with a `PeerNotRestored` event rather than refusing the secret.
#[derive(serde::Deserialize)]
struct HelperIn {
    channel_id: String,
    #[serde(default)]
    transports: Option<Vec<EndpointIn>>,
    shared_key: Vec<u8>,
    /// Absent, `null` or `{}` decode to an empty map.
    #[serde(default)]
    communication_info: Option<HashMap<String, String>>,
    /// The Helper's subshare count. Absent or `null` is `1`.
    #[serde(default)]
    weight: Option<u32>,
}

#[derive(serde::Deserialize)]
struct ReplicaIn {
    replica_id: String,
    /// Absent, `null` or `[]` decode to an empty list, as on [`HelperIn`].
    #[serde(default)]
    transports: Option<Vec<EndpointIn>>,
    /// `"Source"` or `"Destination"`.
    role: String,
    /// Absent, `null` or `{}` decode to an empty map, as on [`HelperIn`].
    #[serde(default)]
    communication_info: Option<HashMap<String, String>>,
}

#[derive(serde::Deserialize)]
struct ReplicasIn {
    channel_id: String,
    #[serde(default)]
    members: Vec<ReplicaIn>,
    #[serde(default)]
    shared_key: Vec<u8>,
}

#[derive(serde::Deserialize)]
struct UserSecretIn {
    id: Vec<u8>,
    name: String,
    data: Vec<u8>,
}

/// The recovered secret as a binding receives it. Decode with serde, then
/// call [`RecoveredSecretIn::into_secret`].
#[derive(serde::Deserialize)]
pub(crate) struct RecoveredSecretIn {
    #[serde(default)]
    helpers: Vec<HelperIn>,
    #[serde(default)]
    secrets: Vec<UserSecretIn>,
    #[serde(default)]
    replicas: Option<ReplicasIn>,
    /// The threshold the secret was split with. Absent or `null` is none.
    #[serde(default)]
    threshold: Option<u32>,
}

fn endpoints(transports: Option<Vec<EndpointIn>>) -> Vec<TransportProtocol> {
    transports
        .unwrap_or_default()
        .into_iter()
        .map(Into::into)
        .collect()
}

/// Parse a decimal `u64` identifier. `field` names the offending field in the
/// error.
pub(crate) fn parse_decimal_u64(s: &str, field: &str) -> Result<u64, String> {
    if s.is_empty() || !s.bytes().all(|b| b.is_ascii_digit()) {
        return Err(format!("{field} must be a decimal u64 string, got {s:?}"));
    }
    s.parse::<u64>()
        .map_err(|e| format!("{field} must be a decimal u64 string, got {s:?}: {e}"))
}

impl RecoveredSecretIn {
    /// Convert into the typed [`Secret`]. The error names the first field
    /// that is malformed.
    pub(crate) fn into_secret(self) -> Result<Secret, String> {
        let helpers = self
            .helpers
            .into_iter()
            .map(|h| -> Result<_, String> {
                let weight = match h.weight {
                    None => 1,
                    Some(0) => return Err("helper.weight must be at least 1, got 0".to_owned()),
                    Some(w) => w,
                };
                Ok(HelperInfo {
                    channel_id: parse_decimal_u64(&h.channel_id, "helper.channel_id")?,
                    transports: endpoints(h.transports),
                    shared_key: h.shared_key,
                    communication_info: h.communication_info.unwrap_or_default(),
                    weight,
                })
            })
            .collect::<Result<Vec<_>, String>>()?;

        let replicas = self
            .replicas
            .map(|g| -> Result<_, String> {
                let members = g
                    .members
                    .into_iter()
                    .map(|r| -> Result<_, String> {
                        let role = match r.role.as_str() {
                            "Source" => ReplicaRole::Source,
                            "Destination" => ReplicaRole::Destination,
                            other => {
                                return Err(format!(
                                    "replica.role must be \"Source\" or \"Destination\", got {other:?}"
                                ));
                            }
                        };
                        Ok(ReplicaInfo {
                            replica_id: parse_decimal_u64(&r.replica_id, "replica.replica_id")?,
                            transports: endpoints(r.transports),
                            role: role as i32,
                            communication_info: r.communication_info.unwrap_or_default(),
                        })
                    })
                    .collect::<Result<Vec<_>, String>>()?;
                Ok(Replicas {
                    channel_id: parse_decimal_u64(&g.channel_id, "replicas.channel_id")?,
                    members,
                    shared_key: g.shared_key,
                })
            })
            .transpose()?;

        let secrets = self
            .secrets
            .into_iter()
            .map(|s| UserSecret {
                id: s.id,
                name: s.name,
                data: s.data,
            })
            .collect();

        let threshold = match self.threshold {
            Some(t) if t < 2 => return Err(format!("threshold must be at least 2, got {t}")),
            other => other,
        };

        Ok(Secret {
            helpers,
            secrets,
            replicas,
            threshold,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use derec_proto::Protocol;

    fn decode(json: &str) -> Result<Secret, String> {
        serde_json::from_str::<RecoveredSecretIn>(json)
            .map_err(|e| e.to_string())?
            .into_secret()
    }

    const KEY: &str = "[1,2,3]";

    fn helper(channel_id: &str) -> String {
        format!(
            r#"{{"helpers":[{{"channel_id":{channel_id},"transports":[],"shared_key":{KEY}}}]}}"#
        )
    }

    #[test]
    fn decimal_ids_round_trip_the_full_u64_range() {
        let s = decode(&helper("\"18446744073709551615\"")).unwrap();
        assert_eq!(s.helpers[0].channel_id, u64::MAX);
        let s = decode(&helper("\"0\"")).unwrap();
        assert_eq!(s.helpers[0].channel_id, 0);
    }

    #[test]
    fn an_empty_id_is_rejected_not_read_as_zero() {
        let err = decode(&helper("\"\"")).unwrap_err();
        assert!(err.contains("helper.channel_id"), "{err}");
    }

    #[test]
    fn malformed_ids_are_rejected() {
        for bad in [
            "\"-1\"",
            "\"+1\"",
            "\" 1\"",
            "\"1.0\"",
            "\"0x10\"",
            "\"18446744073709551616\"",
        ] {
            assert!(decode(&helper(bad)).is_err(), "{bad} must be rejected");
        }
    }

    /// Endpoints are read by protocol name, the shape `SecretRecovered`
    /// carries; a discriminant or an unknown name is malformed.
    #[test]
    fn endpoint_protocols_are_read_by_name() {
        let with = |protocol: &str| {
            format!(
                r#"{{"helpers":[{{"channel_id":"1","transports":[{{"uri":"grpcs://h","protocol":{protocol}}}],"shared_key":{KEY}}}]}}"#
            )
        };
        let secret = decode(&with(r#""grpc""#)).unwrap();
        assert_eq!(
            secret.helpers[0].transports[0].protocol,
            Protocol::Grpc as i32
        );
        assert!(decode(&with("1")).is_err());
        assert!(decode(&with(r#""ftp""#)).is_err());
    }

    #[test]
    fn an_absent_id_is_rejected() {
        let json = format!(r#"{{"helpers":[{{"transports":[],"shared_key":{KEY}}}]}}"#);
        assert!(decode(&json).is_err());
        let json = format!(r#"{{"replicas":{{"members":[],"shared_key":{KEY}}}}}"#);
        assert!(decode(&json).is_err());
    }

    #[test]
    fn replica_ids_and_roles_decode() {
        let json = format!(
            r#"{{"replicas":{{"channel_id":"21","shared_key":{KEY},"members":[
                {{"replica_id":"51966","transports":[{{"uri":"https://a","protocol":"https"}}],"role":"Source"}},
                {{"replica_id":"7","transports":[],"role":"Destination"}}]}}}}"#
        );
        let g = decode(&json).unwrap().replicas.unwrap();
        assert_eq!(g.channel_id, 21);
        assert_eq!(g.members[0].replica_id, 51966);
        assert_eq!(g.members[0].role, ReplicaRole::Source as i32);
        assert_eq!(g.members[1].role, ReplicaRole::Destination as i32);
        assert_eq!(g.members[0].transports[0].protocol, Protocol::Https as i32);
        let json = format!(
            r#"{{"replicas":{{"channel_id":"21","shared_key":{KEY},"members":[
                {{"replica_id":"","transports":[],"role":"Source"}}]}}}}"#
        );
        assert!(decode(&json).unwrap_err().contains("replica.replica_id"));
    }

    /// An empty endpoint list is the core's to judge, so absent, `null` and
    /// `[]` all reach it as the same empty list. Go encodes a nil slice as
    /// `null`.
    #[test]
    fn absent_null_and_empty_transports_all_decode_to_no_endpoint() {
        for transports in [r#""#, r#""transports":null,"#, r#""transports":[],"#] {
            let json = format!(
                r#"{{"helpers":[{{"channel_id":"1",{transports}"shared_key":{KEY}}}],
                    "replicas":{{"channel_id":"21","shared_key":{KEY},"members":[
                    {{"replica_id":"7",{transports}"role":"Source"}}]}}}}"#
            );
            let secret = decode(&json).unwrap_or_else(|e| panic!("{transports}: {e}"));
            assert!(secret.helpers[0].transports.is_empty(), "{transports}");
            assert!(
                secret.replicas.unwrap().members[0].transports.is_empty(),
                "{transports}"
            );
        }
    }

    /// `communication_info` absent, `null` and `{}` all decode to an empty
    /// map on both a Helper and a replica member; a populated map is kept.
    #[test]
    fn absent_null_empty_and_populated_communication_info_decode() {
        for (info, expected) in [
            (r#""#, 0),
            (r#""communication_info":null,"#, 0),
            (r#""communication_info":{},"#, 0),
            (r#""communication_info":{"name":"Alice"},"#, 1),
        ] {
            let json = format!(
                r#"{{"helpers":[{{"channel_id":"1",{info}"shared_key":{KEY}}}],
                    "replicas":{{"channel_id":"21","shared_key":{KEY},"members":[
                    {{"replica_id":"7",{info}"role":"Source"}}]}}}}"#
            );
            let secret = decode(&json).unwrap_or_else(|e| panic!("{info}: {e}"));
            let helper = &secret.helpers[0].communication_info;
            let member = &secret.replicas.unwrap().members[0].communication_info;
            assert_eq!(helper.len(), expected, "{info}");
            assert_eq!(member.len(), expected, "{info}");
            if expected == 1 {
                assert_eq!(helper.get("name").map(String::as_str), Some("Alice"));
                assert_eq!(member.get("name").map(String::as_str), Some("Alice"));
            }
        }
    }

    /// `weight` is the roster's subshare count: absent or `null` reads as
    /// `1`, the value `SecretRecovered` omits nothing for, and `0` is
    /// malformed.
    #[test]
    fn a_helper_weight_decodes_and_defaults_to_one() {
        let with = |weight: &str| {
            format!(
                r#"{{"helpers":[{{"channel_id":"1","transports":[],"shared_key":{KEY}{weight}}}]}}"#
            )
        };
        assert_eq!(decode(&with("")).unwrap().helpers[0].weight, 1);
        assert_eq!(
            decode(&with(r#","weight":null"#)).unwrap().helpers[0].weight,
            1
        );
        assert_eq!(
            decode(&with(r#","weight":3"#)).unwrap().helpers[0].weight,
            3
        );
        assert!(
            decode(&with(r#","weight":0"#))
                .unwrap_err()
                .contains("helper.weight")
        );
    }

    /// The restore params every SDK builds byte-for-byte decode here with the
    /// weights and the threshold they carry.
    #[test]
    fn the_shared_restore_golden_carries_its_weights() {
        let fixture: serde_json::Value =
            serde_json::from_str(include_str!("../../tests/fixtures/wire_golden.json"))
                .expect("fixture parses");
        let secret = serde_json::from_value::<RecoveredSecretIn>(
            fixture["restore"]["recovered_secret"].clone(),
        )
        .expect("golden decodes")
        .into_secret()
        .expect("golden is well formed");
        let weights: Vec<u32> = secret.helpers.iter().map(|h| h.weight).collect();
        assert_eq!(weights, vec![1, 2]);
        assert_eq!(secret.threshold, Some(3));
    }

    /// `threshold` is the `t` the secret was split with: absent or `null` is
    /// none, and a value below `2` is malformed.
    #[test]
    fn a_threshold_decodes_and_a_value_below_two_is_rejected() {
        let with = |threshold: &str| format!(r#"{{"helpers":[]{threshold}}}"#);
        assert_eq!(decode(&with("")).unwrap().threshold, None);
        assert_eq!(
            decode(&with(r#","threshold":null"#)).unwrap().threshold,
            None
        );
        assert_eq!(
            decode(&with(r#","threshold":3"#)).unwrap().threshold,
            Some(3)
        );
        for bad in ["0", "1"] {
            let err = decode(&with(&format!(r#","threshold":{bad}"#))).unwrap_err();
            assert!(err.contains("threshold"), "{err}");
        }
    }

    #[test]
    fn an_unknown_role_is_rejected() {
        let json = format!(
            r#"{{"replicas":{{"channel_id":"21","shared_key":{KEY},"members":[
                {{"replica_id":"1","transports":[],"role":"source"}}]}}}}"#
        );
        assert!(decode(&json).unwrap_err().contains("replica.role"));
    }
}