vta-sdk 0.79.0

SDK for Verifiable Trust Agents operating in Verifiable Trust Communities
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
//! Hidden-vetting PCS engine client methods (`vta/pcs/*/0.1`) — the VTA holds its principal's
//! PCS keys and runs every member-side hidden-vetting operation; the caller carries messages
//! between the VTA, the community and the other party, and never holds a key.
//!
//! This is how a client with no PCS implementation (a mobile wallet) takes part in hidden
//! vetting. The applicant's round:
//!
//! 1. [`VtaClient::pcs_applications_create`] — the application's PCS identifier, which goes in
//!    each `vetting/request`'s `ext` under the community's hidden-vetting namespace;
//! 2. [`VtaClient::pcs_verify_attestation`] — each `vetting/attestation` a vetter sends;
//! 3. [`VtaClient::pcs_prove`] — after `vtc/vetting/pcs-challenge`, the proof that goes in the
//!    join submission's `extensions.hiddenVetting`.
//!
//! A vetter's: [`VtaClient::pcs_root_request`] / [`VtaClient::pcs_root_accept`] around
//! `vtc/vetting/vetters/pcs-root`, [`VtaClient::pcs_tokens_request`] /
//! [`VtaClient::pcs_tokens_accept`] (or [`VtaClient::pcs_tokens_abandon`]) around each tick of
//! `vtc/vetting/vetters/pcs-tokens`, and [`VtaClient::pcs_attest`] after a session. When the
//! community refuses a root request with `identifierRebound`, [`VtaClient::pcs_vetter_recover`]
//! restores the key it bound.
//!
//! Every method takes the task's generated payload type and returns its generated response
//! type (`vti_specs::vta::pcs`), so a caller cannot build a request the schema would
//! refuse.

use serde::Serialize;
use serde::de::DeserializeOwned;
use vti_specs::vta::pcs;

use super::VtaClient;
use crate::error::VtaError;
use crate::trust_tasks;

/// Round-trip timeout (seconds). Proving and attesting are pairing-heavy, so they get more.
const PCS_TT_TIMEOUT: u64 = 30;
const PCS_PROOF_TIMEOUT: u64 = 90;

impl VtaClient {
    async fn pcs_call<P: Serialize, R: DeserializeOwned>(
        &self,
        uri: &str,
        payload: &P,
        timeout: u64,
    ) -> Result<R, VtaError> {
        let payload = serde_json::to_value(payload)
            .map_err(|e| VtaError::Validation(format!("{uri} request: {e}")))?;
        let answer = self.dispatch_trust_task(uri, payload, timeout).await?;
        serde_json::from_value(answer).map_err(|e| {
            VtaError::Protocol(format!("{uri} response does not match its schema: {e}"))
        })
    }

    /// `vta/pcs/applications/create/0.1` — start a hidden-vetting application.
    pub async fn pcs_applications_create(
        &self,
        req: &pcs::applications::create::v0_1::Payload,
    ) -> Result<pcs::applications::create::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_APPLICATIONS_CREATE_0_1,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/applications/show/0.1`.
    pub async fn pcs_applications_show(
        &self,
        req: &pcs::applications::show::v0_1::Payload,
    ) -> Result<pcs::applications::show::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_APPLICATIONS_SHOW_0_1,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/applications/delete/0.1` — destroys the application's key and attestations.
    pub async fn pcs_applications_delete(
        &self,
        req: &pcs::applications::delete::v0_1::Payload,
    ) -> Result<pcs::applications::delete::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_APPLICATIONS_DELETE_0_1,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/verify-attestation/0.1` — take in an attestation a vetter sent.
    pub async fn pcs_verify_attestation(
        &self,
        req: &pcs::verify_attestation::v0_1::Payload,
    ) -> Result<pcs::verify_attestation::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_VERIFY_ATTESTATION_0_1,
            req,
            PCS_PROOF_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/prove/0.1` — the proof a hidden-vetting join submission carries.
    pub async fn pcs_prove(
        &self,
        req: &pcs::prove::v0_1::Payload,
    ) -> Result<pcs::prove::v0_1::Response, VtaError> {
        self.pcs_call(trust_tasks::TASK_VTA_PCS_PROVE_0_1, req, PCS_PROOF_TIMEOUT)
            .await
    }

    /// `vta/pcs/root-request/0.1` — a vetter's blinded enrolment request.
    pub async fn pcs_root_request(
        &self,
        req: &pcs::root_request::v0_1::Payload,
    ) -> Result<pcs::root_request::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_ROOT_REQUEST_0_1,
            req,
            PCS_PROOF_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/root-accept/0.1` — unblind the community's answer into a class credential.
    pub async fn pcs_root_accept(
        &self,
        req: &pcs::root_accept::v0_1::Payload,
    ) -> Result<pcs::root_accept::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_ROOT_ACCEPT_0_1,
            req,
            PCS_PROOF_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/tokens/request/0.1` — one draw of the token drip.
    pub async fn pcs_tokens_request(
        &self,
        req: &pcs::tokens::request::v0_1::Payload,
    ) -> Result<pcs::tokens::request::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_TOKENS_REQUEST_0_1,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/tokens/accept/0.1` — take the community's answer to a draw into the wallet.
    pub async fn pcs_tokens_accept(
        &self,
        req: &pcs::tokens::accept::v0_1::Payload,
    ) -> Result<pcs::tokens::accept::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_TOKENS_ACCEPT_0_1,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/tokens/abandon/0.1` — discard a draw the community refused.
    pub async fn pcs_tokens_abandon(
        &self,
        req: &pcs::tokens::abandon::v0_1::Payload,
    ) -> Result<pcs::tokens::abandon::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_TOKENS_ABANDON_0_1,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/attest/0.1` — spend a token and attest for an applicant, after the human check.
    pub async fn pcs_attest(
        &self,
        req: &pcs::attest::v0_1::Payload,
    ) -> Result<pcs::attest::v0_1::Response, VtaError> {
        self.pcs_call(trust_tasks::TASK_VTA_PCS_ATTEST_0_1, req, PCS_PROOF_TIMEOUT)
            .await
    }

    /// `vta/pcs/vetter/show/0.1`.
    pub async fn pcs_vetter_show(
        &self,
        req: &pcs::vetter::show::v0_1::Payload,
    ) -> Result<pcs::vetter::show::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_VETTER_SHOW_0_1,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/vetter/recover/0.1` — restore the vetter key the community bound the member to
    /// (`boundId`, from `vtc/vetting/vetters/pcs-binding/show`), when the community refuses a
    /// root request with `pcs-root:identifierRebound`. The VTA searches the keys it can derive for
    /// the context, so this gets the longer timeout.
    pub async fn pcs_vetter_recover(
        &self,
        req: &pcs::vetter::recover::v0_1::Payload,
    ) -> Result<pcs::vetter::recover::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_VETTER_RECOVER_0_1,
            req,
            PCS_PROOF_TIMEOUT,
        )
        .await
    }

    /// One `vta/pcs/*` task with the payload and the answer as JSON, carried exactly as given
    /// and as received.
    async fn pcs_call_json(
        &self,
        uri: &str,
        payload: serde_json::Value,
        timeout: u64,
    ) -> Result<serde_json::Value, VtaError> {
        self.dispatch_trust_task(uri, payload, timeout).await
    }

    /// [`Self::pcs_voter_register`], with the payload and the answer as JSON.
    ///
    /// # Why the voter tasks have JSON forms
    ///
    /// The typed voter methods round-trip through generated structs that hold timestamps as
    /// chrono `DateTime`s, and that changes bytes. A manifest the community published with
    /// `votingOpensAt: "2027-01-01T00:00:00.000Z"` reaches the VTA as `…T00:00:00Z` — another
    /// manifest, whose digest is not the community's, so the ticket is refused. A signed read, a
    /// cast or a ticket request coming back through a typed response is re-serialised the same
    /// way, and is no longer the bytes its proof covers.
    ///
    /// A caller carrying documents between the community, the VTA and the ballot box should use
    /// the `_json` forms: [`Self::pcs_voter_read_json`], [`Self::pcs_voter_register_json`],
    /// [`Self::pcs_voter_accept_json`], [`Self::pcs_vote_json`],
    /// [`Self::pcs_vote_encrypted_json`] and [`Self::pcs_voter_receipt_json`]. Each is checked
    /// against its pinned schema before it is sent, as the typed forms are.
    pub async fn pcs_voter_register_json(
        &self,
        req: serde_json::Value,
    ) -> Result<serde_json::Value, VtaError> {
        self.pcs_call_json(
            trust_tasks::TASK_VTA_PCS_VOTER_REGISTER_0_5,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// [`Self::pcs_voter_read`], with the payload and the answer as JSON: the answer's
    /// `document` is the signed read to send to the community unchanged. See
    /// [`Self::pcs_voter_register_json`] for why.
    pub async fn pcs_voter_read_json(
        &self,
        req: serde_json::Value,
    ) -> Result<serde_json::Value, VtaError> {
        self.pcs_call_json(
            trust_tasks::TASK_VTA_PCS_VOTER_READ_0_6,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// [`Self::pcs_voter_accept`], with the payload and the answer as JSON. See
    /// [`Self::pcs_voter_register_json`] for why.
    pub async fn pcs_voter_accept_json(
        &self,
        req: serde_json::Value,
    ) -> Result<serde_json::Value, VtaError> {
        self.pcs_call_json(
            trust_tasks::TASK_VTA_PCS_VOTER_ACCEPT_0_1,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// [`Self::pcs_vote`], with the payload and the answer as JSON: the answer's
    /// `castDocument` is the bytes to post to the ballot box. See
    /// [`Self::pcs_voter_register_json`] for why.
    pub async fn pcs_vote_json(
        &self,
        req: serde_json::Value,
    ) -> Result<serde_json::Value, VtaError> {
        self.pcs_call_json(trust_tasks::TASK_VTA_PCS_VOTE_0_4, req, PCS_PROOF_TIMEOUT)
            .await
    }

    /// [`Self::pcs_vote_encrypted`], with the payload and the answer as JSON. See
    /// [`Self::pcs_voter_register_json`] for why.
    pub async fn pcs_vote_encrypted_json(
        &self,
        req: serde_json::Value,
    ) -> Result<serde_json::Value, VtaError> {
        self.pcs_call_json(
            trust_tasks::TASK_VTA_PCS_VOTE_ENCRYPTED_0_5,
            req,
            PCS_PROOF_TIMEOUT,
        )
        .await
    }

    /// [`Self::pcs_voter_receipt`], with the payload and the answer as JSON: the ballot box's
    /// receipt goes in exactly as it answered. See [`Self::pcs_voter_register_json`] for why.
    pub async fn pcs_voter_receipt_json(
        &self,
        req: serde_json::Value,
    ) -> Result<serde_json::Value, VtaError> {
        self.pcs_call_json(
            trust_tasks::TASK_VTA_PCS_VOTER_RECEIPT_0_1,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/voter/register/0.5` — this election's voter key and the blinded ticket request
    /// to carry to `vtc/elections/tickets/issue/0.3`; under a credential roll, with `member` and
    /// `membershipCredential`, the `ticketDocument` carries the member's presentation of that
    /// credential (VTI-VOT-025). A community that answers it `indeterminate` is sent the same
    /// document again, after any `retryAfter` and with backoff.
    ///
    /// Re-serialises timestamps; to carry signed documents byte for byte use
    /// [`Self::pcs_voter_register_json`].
    pub async fn pcs_voter_register(
        &self,
        req: &pcs::voter::register::v0_5::Payload,
    ) -> Result<pcs::voter::register::v0_5::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_VOTER_REGISTER_0_5,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/voter/read/0.6` — `vtc/elections/list/0.5` or `vtc/elections/show/0.6`, signed as
    /// the member, to send to the community unchanged; a `show` with `membershipCredential`
    /// carries the member's presentation of it, issued by `memberOf` where that is named
    /// (VTI-VOT-015, VTI-VOT-016).
    ///
    /// Re-serialises timestamps; to carry signed documents byte for byte use
    /// [`Self::pcs_voter_read_json`].
    pub async fn pcs_voter_read(
        &self,
        req: &pcs::voter::read::v0_6::Payload,
    ) -> Result<pcs::voter::read::v0_6::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_VOTER_READ_0_6,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/voter/accept/0.1` — unblind the community's answer into the ticket.
    ///
    /// Re-serialises timestamps; to carry signed documents byte for byte use
    /// [`Self::pcs_voter_accept_json`].
    pub async fn pcs_voter_accept(
        &self,
        req: &pcs::voter::accept::v0_1::Payload,
    ) -> Result<pcs::voter::accept::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_VOTER_ACCEPT_0_1,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/vote/0.4` — the signed cast document. Send it to the ballot box yourself, from
    /// your own connection, and resend that same document if the answer is lost.
    ///
    /// Re-serialises timestamps; to carry signed documents byte for byte use
    /// [`Self::pcs_vote_json`].
    pub async fn pcs_vote(
        &self,
        req: &pcs::vote::v0_4::Payload,
    ) -> Result<pcs::vote::v0_4::Response, VtaError> {
        self.pcs_call(trust_tasks::TASK_VTA_PCS_VOTE_0_4, req, PCS_PROOF_TIMEOUT)
            .await
    }

    /// `vta/pcs/voter/receipt/0.1` — keep a receipt, and check its inclusion in a head the
    /// community kept.
    ///
    /// Re-serialises timestamps; to carry signed documents byte for byte use
    /// [`Self::pcs_voter_receipt_json`].
    pub async fn pcs_voter_receipt(
        &self,
        req: &pcs::voter::receipt::v0_1::Payload,
    ) -> Result<pcs::voter::receipt::v0_1::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_VOTER_RECEIPT_0_1,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/vote-encrypted/0.5` — the signed `voting/ballot/cast-encrypted` document for a
    /// sealed or tallied-encrypted election, under the election key `voting/trustees/status`
    /// reports. Send it as [`Self::pcs_vote`]'s.
    ///
    /// Re-serialises timestamps; to carry signed documents byte for byte use
    /// [`Self::pcs_vote_encrypted_json`].
    pub async fn pcs_vote_encrypted(
        &self,
        req: &pcs::vote_encrypted::v0_5::Payload,
    ) -> Result<pcs::vote_encrypted::v0_5::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_VOTE_ENCRYPTED_0_5,
            req,
            PCS_PROOF_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/trustee/enrol/0.4` — the trustee's transport key, and its signed enrolment.
    pub async fn pcs_trustee_enrol(
        &self,
        req: &pcs::trustee::enrol::v0_4::Payload,
    ) -> Result<pcs::trustee::enrol::v0_4::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_TRUSTEE_ENROL_0_4,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/trustee/deal/0.5` — the trustee's dealing, signed.
    pub async fn pcs_trustee_deal(
        &self,
        req: &pcs::trustee::deal::v0_5::Payload,
    ) -> Result<pcs::trustee::deal::v0_5::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_TRUSTEE_DEAL_0_5,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/trustee/keys/0.5` — check the shares dealt and confirm; with the formed election
    /// key, keep the trustee's share of it.
    pub async fn pcs_trustee_keys(
        &self,
        req: &pcs::trustee::keys::v0_5::Payload,
    ) -> Result<pcs::trustee::keys::v0_5::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_TRUSTEE_KEYS_0_5,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/trustee/justify/0.5` — answer a complaint with the share that was dealt.
    pub async fn pcs_trustee_justify(
        &self,
        req: &pcs::trustee::justify::v0_5::Payload,
    ) -> Result<pcs::trustee::justify::v0_5::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_TRUSTEE_JUSTIFY_0_5,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/trustee/decrypt/0.1` — the trustee's partial decryptions, signed.
    pub async fn pcs_trustee_decrypt(
        &self,
        req: &pcs::trustee::decrypt::v0_2::Payload,
    ) -> Result<pcs::trustee::decrypt::v0_2::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_TRUSTEE_DECRYPT_0_2,
            req,
            PCS_PROOF_TIMEOUT,
        )
        .await
    }

    /// `vta/pcs/trustee/read/0.4` — `voting/trustees/status` or `voting/election/show`, signed as
    /// the trustee, to send to the ballot box yourself: a private board answers them only to a
    /// trustee.
    pub async fn pcs_trustee_read(
        &self,
        req: &pcs::trustee::read::v0_4::Payload,
    ) -> Result<pcs::trustee::read::v0_4::Response, VtaError> {
        self.pcs_call(
            trust_tasks::TASK_VTA_PCS_TRUSTEE_READ_0_4,
            req,
            PCS_TT_TIMEOUT,
        )
        .await
    }
}