tvc 0.13.0

CLI for Turnkey Verifiable Cloud
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
//! Create a hosted quorum key from operator encryption public keys.

use crate::{
    client::build_client,
    config::turnkey::Config,
    operator::{
        OperatorPublicKey, ensure_authenticated_org, hosted_activity_error,
        resolve_hosted_operator_encrypt_key,
    },
    outcome::Outcome,
    output::StdCtx,
};
use anyhow::{Context, Result, ensure};
use clap::{ArgAction, ArgGroup, Args as ClapArgs, builder::RangedU64ValueParser};
use serde::Serialize;
use std::{
    collections::HashSet,
    fmt::{self, Display, Formatter},
    time::{SystemTime, UNIX_EPOCH},
};
use turnkey_client::generated::{CreateTvcQuorumKeyIntent, CreateTvcQuorumKeyResult};
use uuid::Uuid;

// `qos_crypto::shamir::shares_generate` supports at most 255 shares. Hosted
// quorum generation keeps the operator key count strictly below that bound.
const MAX_OPERATOR_ENCRYPT_KEY_COUNT_EXCLUSIVE: usize = 255;

fn threshold_parser() -> RangedU64ValueParser<u8> {
    RangedU64ValueParser::new().range(2..)
}

/// Create a hosted quorum key encrypted to hosted operator keys.
#[derive(Debug, ClapArgs)]
#[command(about, long_about = None)]
#[command(group(
    ArgGroup::new("operator_source")
        .args(["operator_encrypt_keys", "operator_ids"])
        .required(true)
        .multiple(false)
))]
pub struct Args {
    /// Number of operator shares required to reconstruct the quorum key.
    #[arg(
        long,
        env = "TVC_QUORUM_KEY_THRESHOLD",
        value_parser = threshold_parser()
    )]
    threshold: u8,

    /// Comma-separated, uncompressed P-256 operator encryption public keys.
    #[arg(
        long,
        value_name = "HEX",
        value_delimiter = ',',
        action = ArgAction::Set,
        env = "TVC_OPERATOR_ENCRYPT_KEYS"
    )]
    operator_encrypt_keys: Vec<OperatorPublicKey>,

    /// Comma-separated hosted operator UUIDs from the active organization.
    #[arg(
        long,
        value_name = "UUID",
        value_delimiter = ',',
        value_parser = parse_operator_id,
        action = ArgAction::Set,
        env = "TVC_OPERATOR_IDS"
    )]
    operator_ids: Vec<Uuid>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct QuorumKeyCreated {
    quorum_key_id: String,
    quorum_public_key: String,
    share_ids: Vec<String>,
}

impl Display for QuorumKeyCreated {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Quorum Key ID: {}\nQuorum Public Key: {}\nShare IDs: {}",
            self.quorum_key_id,
            self.quorum_public_key,
            self.share_ids.join(", ")
        )
    }
}

enum OperatorSource {
    EncryptKeys(Vec<OperatorPublicKey>),
    OperatorIds(Vec<Uuid>),
}

impl OperatorSource {
    fn len(&self) -> usize {
        match self {
            Self::EncryptKeys(keys) => keys.len(),
            Self::OperatorIds(ids) => ids.len(),
        }
    }
}

/// Run the hosted quorum-key creation command.
pub async fn run(_ctx: &mut StdCtx, args: Args) -> Result<Outcome> {
    let Args {
        threshold,
        operator_encrypt_keys,
        operator_ids,
    } = args;
    // The required, mutually exclusive Clap group guarantees exactly one
    // source is non-empty.
    let operator_source = if operator_ids.is_empty() {
        OperatorSource::EncryptKeys(operator_encrypt_keys)
    } else {
        OperatorSource::OperatorIds(operator_ids)
    };
    validate_operator_count(operator_source.len())?;
    validate_threshold(threshold, operator_source.len())?;

    let (operator_encrypt_keys, configured_org_id) =
        resolve_operator_encrypt_keys(operator_source).await?;
    let operator_encrypt_keys = normalize_operator_encrypt_keys(operator_encrypt_keys)?;

    let intent = build_create_tvc_quorum_key_intent(threshold, operator_encrypt_keys);
    let expected_share_count = intent.operator_encrypt_keys.len();
    let auth = build_client().await?;
    if let Some(configured_org_id) = configured_org_id {
        ensure_authenticated_org(&auth.org_id, &configured_org_id)?;
    }
    let timestamp_ms = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .context("system time before unix epoch")?
        .as_millis();

    let result = auth
        .client
        .create_tvc_quorum_key(auth.org_id, timestamp_ms, intent)
        .await
        .map_err(|error| hosted_activity_error("create hosted TVC quorum key", error))?;
    let output = validate_result(result.result, expected_share_count)?;

    Ok(Outcome::KeysCreateQuorumKey(output))
}

fn parse_operator_id(value: &str) -> std::result::Result<Uuid, String> {
    Uuid::parse_str(value.trim()).map_err(|_| "must be a UUID".to_string())
}

async fn resolve_operator_encrypt_keys(
    operator_source: OperatorSource,
) -> Result<(Vec<OperatorPublicKey>, Option<String>)> {
    match operator_source {
        OperatorSource::EncryptKeys(keys) => Ok((keys, None)),
        OperatorSource::OperatorIds(operator_ids) => {
            validate_operator_ids(&operator_ids)?;
            let config = Config::load().await?;
            resolve_operator_ids(&config, &operator_ids)
        }
    }
}

fn resolve_operator_ids(
    config: &Config,
    operator_ids: &[Uuid],
) -> Result<(Vec<OperatorPublicKey>, Option<String>)> {
    let (_, org) = config
        .active_org_config()
        .context("No active organization. Run `tvc login` first.")?;
    let configured_org_id = org.id.clone();
    let mut keys = Vec::with_capacity(operator_ids.len());

    for operator_id in operator_ids {
        keys.push(resolve_hosted_operator_encrypt_key(config, operator_id)?);
    }

    Ok((keys, Some(configured_org_id)))
}

fn validate_operator_ids(operator_ids: &[Uuid]) -> Result<()> {
    let mut seen = HashSet::new();
    for (index, operator_id) in operator_ids.iter().enumerate() {
        ensure!(
            seen.insert(operator_id),
            "duplicate operator ID at index {index}: {operator_id}"
        );
    }
    Ok(())
}

fn normalize_operator_encrypt_keys(input: Vec<OperatorPublicKey>) -> Result<Vec<String>> {
    let normalized: Vec<_> = input.into_iter().map(|key| key.to_string()).collect();
    let mut seen = HashSet::new();
    for (index, key) in normalized.iter().enumerate() {
        ensure!(
            seen.insert(key),
            "duplicate operator encryption public key at index {index}"
        );
    }
    Ok(normalized)
}

fn validate_operator_count(operator_count: usize) -> Result<()> {
    ensure!(
        operator_count < MAX_OPERATOR_ENCRYPT_KEY_COUNT_EXCLUSIVE,
        "operator encryption public key count ({operator_count}) must be less than {MAX_OPERATOR_ENCRYPT_KEY_COUNT_EXCLUSIVE}"
    );
    Ok(())
}

fn validate_threshold(threshold: u8, operator_count: usize) -> Result<()> {
    ensure!(
        threshold as usize <= operator_count,
        "threshold ({threshold}) cannot exceed operator encryption public key count ({operator_count})"
    );
    Ok(())
}

fn build_create_tvc_quorum_key_intent(
    threshold: u8,
    operator_encrypt_keys: Vec<String>,
) -> CreateTvcQuorumKeyIntent {
    CreateTvcQuorumKeyIntent {
        threshold: u32::from(threshold),
        operator_encrypt_keys,
    }
}

fn validate_result(
    result: CreateTvcQuorumKeyResult,
    expected_share_count: usize,
) -> Result<QuorumKeyCreated> {
    ensure!(
        !result.quorum_key_id.trim().is_empty(),
        "create TVC quorum key response contained an empty quorum key ID"
    );
    ensure!(
        !result.quorum_public_key.trim().is_empty(),
        "create TVC quorum key response contained an empty quorum public key"
    );
    ensure!(
        result.share_ids.len() == expected_share_count,
        "create TVC quorum key response returned {} share IDs for {expected_share_count} operator encryption public keys",
        result.share_ids.len()
    );
    ensure!(
        result.share_ids.iter().all(|id| !id.trim().is_empty()),
        "create TVC quorum key response contained an empty share ID"
    );

    Ok(QuorumKeyCreated {
        quorum_key_id: result.quorum_key_id,
        quorum_public_key: result.quorum_public_key,
        share_ids: result.share_ids,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::turnkey::{
        HostedOperatorRecord, OperatorKind, OperatorRecord, OperatorRecordKind, OrgConfig,
    };
    use qos_p256::P256Pair;
    use serde_json::Value;
    use std::{collections::HashMap, path::PathBuf};

    const FIRST_OPERATOR_ID: &str = "11111111-1111-4111-8111-111111111111";
    const SECOND_OPERATOR_ID: &str = "22222222-2222-4222-8222-222222222222";

    fn operator_encrypt_key() -> String {
        let public_key = P256Pair::generate().unwrap().public_key().to_bytes();
        hex::encode(&public_key[..65])
    }

    fn result_with_share_ids(share_ids: &[&str]) -> CreateTvcQuorumKeyResult {
        CreateTvcQuorumKeyResult {
            quorum_key_id: "quorum-key-id".to_string(),
            quorum_public_key: "quorum-public-key".to_string(),
            share_ids: share_ids.iter().map(|id| (*id).to_string()).collect(),
        }
    }

    fn hosted_operator(name: &str, operator_id: &str, wallet_id: &str) -> (OperatorRecord, String) {
        let public = P256Pair::generate().unwrap().public_key().to_bytes();
        let encrypt_public_key = hex::encode(&public[..65]);
        (
            OperatorRecord {
                name: name.to_string(),
                kind: OperatorRecordKind::Hosted(HostedOperatorRecord {
                    operator_id: Uuid::parse_str(operator_id).unwrap(),
                    wallet_id: Uuid::parse_str(wallet_id).unwrap(),
                    path: "m/5527107'/0'/0'".to_string(),
                    encrypt_public_key: encrypt_public_key.clone(),
                    sign_public_key: hex::encode(&public[65..]),
                    extra: toml::Table::new(),
                }),
            },
            encrypt_public_key,
        )
    }

    fn config_with_operators(operators: Vec<OperatorRecord>) -> Config {
        Config {
            active_org: Some("active".to_string()),
            orgs: HashMap::from([(
                "active".to_string(),
                OrgConfig {
                    id: "org-id".to_string(),
                    api_key_path: PathBuf::from("api-key.json"),
                    api_base_url: "https://api.turnkey.com".to_string(),
                    default_operator_kind: OperatorKind::Local,
                    operators,
                    extra: toml::Table::new(),
                },
            )]),
            ..Config::default()
        }
    }

    #[test]
    fn normalizes_operator_encrypt_keys() {
        let first = operator_encrypt_key();
        let second = operator_encrypt_key();
        let keys = vec![
            format!("  {}  ", first.to_uppercase()).parse().unwrap(),
            second.to_uppercase().parse().unwrap(),
        ];
        let parsed = normalize_operator_encrypt_keys(keys).unwrap();

        assert_eq!(parsed, vec![first, second]);
    }

    #[test]
    fn rejects_duplicate_operator_encrypt_keys_after_normalization() {
        let key = operator_encrypt_key();
        let keys = vec![key.parse().unwrap(), key.to_uppercase().parse().unwrap()];
        let error = normalize_operator_encrypt_keys(keys)
            .unwrap_err()
            .to_string();

        assert_eq!(error, "duplicate operator encryption public key at index 1");
    }

    #[test]
    fn rejects_duplicate_operator_ids() {
        let operator_id = Uuid::parse_str("11111111-1111-4111-8111-111111111111").unwrap();
        let error = validate_operator_ids(&[operator_id, operator_id])
            .unwrap_err()
            .to_string();

        assert_eq!(
            error,
            "duplicate operator ID at index 1: 11111111-1111-4111-8111-111111111111"
        );
    }

    #[test]
    fn validates_threshold_boundaries() {
        assert_eq!(
            validate_threshold(3, 2).unwrap_err().to_string(),
            "threshold (3) cannot exceed operator encryption public key count (2)"
        );
        assert!(validate_threshold(2, 2).is_ok());
    }

    #[test]
    fn validates_operator_count_boundaries() {
        assert!(validate_operator_count(254).is_ok());
        assert_eq!(
            validate_operator_count(255).unwrap_err().to_string(),
            "operator encryption public key count (255) must be less than 255"
        );
    }

    #[test]
    fn builds_exact_create_intent() {
        let operator_encrypt_keys = vec![operator_encrypt_key(), operator_encrypt_key()];
        let intent = build_create_tvc_quorum_key_intent(2, operator_encrypt_keys.clone());

        assert_eq!(
            intent,
            CreateTvcQuorumKeyIntent {
                threshold: 2,
                operator_encrypt_keys,
            }
        );
    }

    #[test]
    fn operator_ids_reject_duplicate_resolved_keys() {
        let (first, shared_key) = hosted_operator(
            "first",
            FIRST_OPERATOR_ID,
            "33333333-3333-4333-8333-333333333333",
        );
        let (mut second_record, _) = hosted_operator(
            "second",
            SECOND_OPERATOR_ID,
            "44444444-4444-4444-8444-444444444444",
        );
        let OperatorRecordKind::Hosted(second) = &mut second_record.kind else {
            panic!("expected hosted operator")
        };
        second.encrypt_public_key = shared_key;
        let config = config_with_operators(vec![first, second_record]);
        let operator_ids = [
            Uuid::parse_str(FIRST_OPERATOR_ID).unwrap(),
            Uuid::parse_str(SECOND_OPERATOR_ID).unwrap(),
        ];

        let (keys, _) = resolve_operator_ids(&config, &operator_ids).unwrap();
        assert_eq!(
            normalize_operator_encrypt_keys(keys)
                .unwrap_err()
                .to_string(),
            "duplicate operator encryption public key at index 1"
        );
    }

    #[test]
    fn validates_and_formats_result() {
        let output = validate_result(result_with_share_ids(&["share-1", "share-2"]), 2).unwrap();

        assert_eq!(
            output,
            QuorumKeyCreated {
                quorum_key_id: "quorum-key-id".to_string(),
                quorum_public_key: "quorum-public-key".to_string(),
                share_ids: vec!["share-1".to_string(), "share-2".to_string()],
            }
        );
        assert_eq!(
            output.to_string(),
            "Quorum Key ID: quorum-key-id\nQuorum Public Key: quorum-public-key\nShare IDs: share-1, share-2"
        );

        // The payload serializes without a `reason`; the reason is the
        // `Outcome`'s responsibility.
        let json: Value = serde_json::to_value(&output).unwrap();
        assert_eq!(
            json,
            serde_json::json!({
                "quorumKeyId": "quorum-key-id",
                "quorumPublicKey": "quorum-public-key",
                "shareIds": ["share-1", "share-2"],
            })
        );
    }
}