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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.
use crate::;
use ;
use Message;
/// Produces a **successful** unpair acknowledgement envelope on the responder side.
///
/// Called by the party that **received** an [`derec_proto::UnpairRequestMessage`]
/// after it has dropped (or is about to drop) its local state for the
/// channel. The envelope carries `result.status == StatusEnum::Ok` with an
/// empty `memo`.
///
/// Deletion of local state is **not** performed by this primitive — it lives
/// at the [`crate::protocol`] orchestrator layer.
///
/// # Arguments
///
/// * `channel_id` - Channel identifier for the requesting peer.
/// * `shared_key` - 32-byte symmetric channel key.
///
/// # Errors
///
/// Returns [`crate::Error`] if outer envelope construction or encryption fails.
///
/// # Example
///
/// ```
/// use derec_library::primitives::unpairing::response;
/// use derec_library::types::ChannelId;
///
/// let channel_id = ChannelId(42);
/// let shared_key = [7u8; 32];
///
/// let response::ProduceResult { envelope } = response::produce(channel_id, &shared_key)
/// .expect("failed to build unpair response");
///
/// assert!(!envelope.is_empty());
/// ```
/// Decrypts and decodes an [`derec_proto::UnpairResponseMessage`] from an
/// outer [`derec_proto::DeRecMessage`] envelope.
///
/// This function:
///
/// 1. Decodes the outer [`derec_proto::DeRecMessage`] envelope from
/// `envelope_bytes`.
/// 2. Decrypts and decodes the inner [`derec_proto::UnpairResponseMessage`]
/// using `shared_key`.
/// 3. Validates the invariant `envelope.timestamp == response.timestamp`.
///
/// # Errors
///
/// Returns [`crate::Error`] if:
///
/// - `envelope_bytes` cannot be decoded as a valid [`derec_proto::DeRecMessage`]
/// - decryption or inner-message decoding fails
/// - `envelope.timestamp != response.timestamp`
/// - the inner message is not a [`derec_proto::UnpairResponseMessage`]
///
/// # Security: no freshness or replay protection
///
/// The timestamp check enforced here only binds the envelope to the
/// inner body (`envelope.timestamp == body.timestamp`). It does NOT
/// enforce a freshness window against the receiver's clock and does
/// NOT detect replays of a previously-captured ciphertext. Because
/// the channel key is long-lived, a recorded envelope stays
/// decryptable indefinitely. Callers MUST add a freshness window
/// and per-channel anti-replay (monotonic counter or nonce log) on
/// top before driving any side-effecting state off the parsed body.
///
/// # Example
///
/// ```
/// use derec_library::primitives::unpairing::{request, response};
/// use derec_library::types::ChannelId;
///
/// let channel_id = ChannelId(42);
/// let shared_key = [7u8; 32];
///
/// // Initiator: send an unpair request.
/// let request::ProduceResult { envelope: req_envelope } =
/// request::produce(channel_id, "no longer needed", &shared_key, None)
/// .expect("produce request failed");
///
/// // Responder: extract and ack with a successful response.
/// let _ = request::extract(&req_envelope, &shared_key).expect("extract request failed");
/// let response::ProduceResult { envelope: resp_envelope } =
/// response::produce(channel_id, &shared_key).expect("produce response failed");
///
/// // Initiator: extract the response.
/// let response::ExtractResult { response: ack } =
/// response::extract(&resp_envelope, &shared_key).expect("extract response failed");
///
/// assert!(ack.result.is_some());
/// ```
/// Verifies the result field of a decoded [`derec_proto::UnpairResponseMessage`]
/// and reports whether the responder acknowledged the unpair.
///
/// - `Ok(ProcessResult { acknowledged: true })` — the responder reports
/// success (`result.status == StatusEnum::Ok`).
/// - `Err(UnpairingError::NonOkStatus { … })` — the responder rejected the
/// unpair; carries the peer's status code and memo.
/// - `Err(crate::Error::Invariant(_))` — the response carried no result.
///
/// The initiator's [`crate::protocol`] orchestrator uses the outcome to
/// decide whether to delete its own local state (success) or surface a
/// rejection event to the application.
///
/// # Example
///
/// ```
/// use derec_library::primitives::unpairing::{request, response};
/// use derec_library::types::ChannelId;
///
/// let channel_id = ChannelId(42);
/// let shared_key = [7u8; 32];
///
/// // Initiator → Responder → Initiator roundtrip.
/// let request::ProduceResult { envelope: req_envelope } =
/// request::produce(channel_id, "no longer needed", &shared_key, None)
/// .expect("produce request failed");
/// let _ = request::extract(&req_envelope, &shared_key).expect("extract request failed");
/// let response::ProduceResult { envelope: resp_envelope } =
/// response::produce(channel_id, &shared_key).expect("produce response failed");
/// let response::ExtractResult { response: ack } =
/// response::extract(&resp_envelope, &shared_key).expect("extract response failed");
///
/// let response::ProcessResult { acknowledged } =
/// response::process(&ack).expect("process failed");
///
/// assert!(acknowledged);
/// ```