graphshell 0.0.2

Graphshell presentation host and loopback acceptance view.
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
//! H6 destination: fetch, reconnect, resume, apply, then test revocation.

use std::path::Path;
use std::time::Instant;

use chirograph::{
    CapabilityProfile, CarrierRequest, CarrierRequestBody, CarrierResponse, CarrierResponseBody,
    ContentHash, IntentInvocation, IntentResult, PortableCardV1, ProtocolVersion, ResourceRequest,
    ResourceResponse, ResumeReply, ResumeRequest, SessionOpen,
};
use eidetic::PrivacyClass;
use graphshell::access::AccessContext;
use graphshell::admission::open_session;
use graphshell::carrier::projection_alpn;
use graphshell::mere_host::{MereHost, SelectedPersonaRef, fixture_handlers};
use graphshell::transfer::{
    ApplyTransferContext, TransferAuthorization, TransferManifestV1, TransferOperation,
    apply_transfer,
};
use graphshell::transfer_endpoint::{TRANSFER_BEGIN_INTENT, TransferBeginV1};
use muniment::{BlobStore, MemoryBackend};
use notochord::{NetworkId, SessionReply, TrafficClass, initiate_session};
use personae::{IdentityProvider, InMemoryProvider};
use sceno::InstanceId;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader, Lines, ReadHalf, WriteHalf};
use tokio::time::sleep;
use transport::p2panda_transport::{MdnsDiscoveryMode, P2pandaStream, P2pandaTransport};
use transport::{Transport, initiator_binding};

use crate::DIAL_DEADLINE;
use crate::identity::{assert_same_key, device, grant, hex, now_ms, profile, short};

struct AdmittedIo {
    lines: Lines<BufReader<ReadHalf<P2pandaStream>>>,
    writer: WriteHalf<P2pandaStream>,
    grant_id: String,
}

impl AdmittedIo {
    async fn request(
        &mut self,
        id: u64,
        body: CarrierRequestBody,
    ) -> Result<CarrierResponse, String> {
        let mut line = serde_json::to_vec(&CarrierRequest { id, body })
            .map_err(|error| format!("encode request: {error}"))?;
        line.push(b'\n');
        self.writer
            .write_all(&line)
            .await
            .map_err(|error| format!("write request: {error}"))?;
        self.writer
            .flush()
            .await
            .map_err(|error| format!("flush request: {error}"))?;
        let line = self
            .lines
            .next_line()
            .await
            .map_err(|error| format!("read response: {error}"))?
            .ok_or("the source closed without answering")?;
        let response: CarrierResponse =
            serde_json::from_str(&line).map_err(|error| format!("decode response: {error}"))?;
        if response.id != id {
            return Err(format!(
                "response id {} did not match request {id}",
                response.id
            ));
        }
        Ok(response)
    }
}

#[allow(clippy::too_many_arguments)]
pub(crate) async fn connect(
    owner: InMemoryProvider,
    me: InMemoryProvider,
    seed: [u8; 32],
    source_key: [u8; 32],
    network: NetworkId,
    ticket: &str,
    receipt_path: Option<&Path>,
    expect_revoked: bool,
) -> Result<(), String> {
    let carrier = P2pandaTransport::builder_from_seed(seed)
        .alpns(vec![projection_alpn()])
        .mdns(MdnsDiscoveryMode::Active)
        .bind()
        .await
        .map_err(|error| format!("bind: {error}"))?;
    assert_same_key(&carrier, &me)?;
    let peer = carrier
        .add_peer_ticket(ticket)
        .await
        .map_err(|error| format!("ticket: {error}"))?;
    if peer.to_bytes() != source_key {
        return Err("ticket peer does not match H6_PEER".to_string());
    }

    println!("h6_transfer_peer connect");
    println!("  source: {}", short(&source_key));

    println!("  --- session 1: manifest ---");
    let mut first = dial(&carrier, peer, &me, &owner, network).await?;
    let first_opened = opened(ok(first.request(1, open_body()).await?)?)?;
    let projection = first_opened
        .descriptor
        .projections
        .first()
        .ok_or("source advertised no transfer projection")?
        .request
        .clone();
    let snapshot = snapshot(ok(first
        .request(2, CarrierRequestBody::Snapshot(projection.clone()))
        .await?)?)?;
    let transfer_id_text = projection
        .session
        .0
        .rsplit(':')
        .next()
        .ok_or("transfer session has no id")?;
    let transfer_id = uuid::Uuid::parse_str(transfer_id_text).map_err(|error| error.to_string())?;
    let begin = IntentInvocation {
        session: projection.session.clone(),
        target: InstanceId(0),
        observed_epoch: snapshot.scene.epoch,
        observed_revision: snapshot.scene.revision,
        intent: TRANSFER_BEGIN_INTENT.to_string(),
        payload: serde_json::to_vec(&TransferBeginV1 { transfer_id })
            .map_err(|error| error.to_string())?,
    };
    match intent(ok(first
        .request(3, CarrierRequestBody::Intent(begin.clone()))
        .await?)?)?
    {
        IntentResult::Accepted => println!("  transfer disclosure accepted"),
        other => return Err(format!("transfer disclosure was not accepted: {other:?}")),
    }

    let card_offer = snapshot
        .presentation
        .offers_for(InstanceId(0))
        .and_then(|offers| offers.first())
        .ok_or("transfer snapshot advertised no card")?;
    let card_response = resource(ok(first
        .request(
            4,
            CarrierRequestBody::Resource(ResourceRequest {
                session: projection.session.clone(),
                resource: card_offer.resource,
            }),
        )
        .await?)?)?;
    verify_address(&card_response)?;
    let card: PortableCardV1 =
        serde_json::from_slice(&card_response.bytes).map_err(|error| error.to_string())?;
    let manifest_address = *card.media.first().ok_or("transfer card has no manifest")?;
    let manifest_response = resource(ok(first
        .request(
            5,
            CarrierRequestBody::Resource(ResourceRequest {
                session: projection.session.clone(),
                resource: manifest_address,
            }),
        )
        .await?)?)?;
    verify_address(&manifest_response)?;
    let manifest: TransferManifestV1 =
        serde_json::from_slice(&manifest_response.bytes).map_err(|error| error.to_string())?;
    if manifest.transfer_id != transfer_id {
        return Err("card session and manifest name different transfers".to_string());
    }
    if manifest.operation != TransferOperation::Replicate {
        return Err("the physical H6 proof expects replicate".to_string());
    }
    suspended(ok(first.request(6, CarrierRequestBody::Suspend).await?)?)?;
    println!(
        "  cached manifest {}; connection suspended before {} blob(s)",
        manifest.transfer_id,
        manifest.blobs.len()
    );
    drop(first);

    println!("  --- interruption: new carrier admission ---");
    println!("  --- session 2: resume and blobs ---");
    let mut second = dial(&carrier, peer, &me, &owner, network).await?;
    let authorization_grant = second.grant_id.clone();
    opened(ok(second.request(7, open_body()).await?)?)?;
    match resume(ok(second
        .request(
            8,
            CarrierRequestBody::Resume(ResumeRequest {
                session: projection.session.clone(),
                epoch: snapshot.scene.epoch,
                revision: snapshot.scene.revision,
            }),
        )
        .await?)?)?
    {
        ResumeReply::Current(ack)
            if ack.epoch == snapshot.scene.epoch && ack.revision == snapshot.scene.revision =>
        {
            println!("  resumed current projection without a fresh snapshot")
        }
        other => return Err(format!("unexpected resume answer: {other:?}")),
    }

    let staging_backend = MemoryBackend::new();
    let staging_blobs = BlobStore::new(staging_backend.clone());
    for (index, descriptor) in manifest.blobs.iter().enumerate() {
        let response = resource(ok(second
            .request(
                9 + index as u64,
                CarrierRequestBody::Resource(ResourceRequest {
                    session: projection.session.clone(),
                    resource: ContentHash(*descriptor.content_hash.as_bytes()),
                }),
            )
            .await?)?)?;
        verify_address(&response)?;
        if eidetic::Hash::of(&response.bytes) != descriptor.content_hash
            || response.bytes.len() as u64 != descriptor.byte_len
        {
            return Err(format!(
                "blob {} failed its manifest address",
                descriptor.content_hash
            ));
        }
        let stored = staging_blobs
            .put(&response.bytes)
            .await
            .map_err(|error| error.to_string())?;
        if stored.to_hex() != descriptor.content_hash.to_hex() {
            return Err("staging store changed the blob address".to_string());
        }
        println!(
            "  fetched blob {} ({} bytes)",
            descriptor.content_hash, descriptor.byte_len
        );
    }
    let close_id = 9 + manifest.blobs.len() as u64;
    closed(ok(second
        .request(close_id, CarrierRequestBody::Close)
        .await?)?)?;
    drop(second);

    let destination_key = me.master_public_key().to_bytes();
    if manifest.destination.device != device(&destination_key) {
        return Err("manifest destination is not this device".to_string());
    }
    let destination_backend = MemoryBackend::new();
    let mut destination_host = MereHost::empty(
        destination_backend.clone(),
        SelectedPersonaRef {
            persona: manifest.destination.persona.clone(),
            profile: "mere.base".to_string(),
        },
        fixture_handlers(),
        AccessContext {
            persona: manifest.destination.persona.clone(),
            device: manifest.destination.device.clone(),
            at_ms: now_ms(),
        },
    );
    let destination_blobs = BlobStore::new(destination_backend.clone());
    let mut destination_authority = destination_backend.clone();
    let receipt = apply_transfer(
        &mut destination_host,
        &staging_blobs,
        &destination_blobs,
        &mut destination_authority,
        &manifest,
        &ApplyTransferContext {
            authorization: TransferAuthorization {
                grant_id: authorization_grant,
                revoked: false,
            },
            application: "graphshell".to_string(),
            handler: "graphshell.transfer/v1".to_string(),
            completed_at_ms: now_ms(),
            access_privacy: PrivacyClass::LocalOnly,
        },
    )
    .await
    .map_err(|error| error.to_string())?;
    verify_destination(&destination_host, &destination_blobs, &manifest, &receipt).await?;
    if let Some(path) = receipt_path {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .map_err(|error| format!("create {}: {error}", parent.display()))?;
        }
        let bytes = serde_json::to_vec_pretty(&receipt).map_err(|error| error.to_string())?;
        std::fs::write(path, bytes)
            .map_err(|error| format!("write {}: {error}", path.display()))?;
        println!("  receipt: {}", path.display());
    }
    println!(
        "  applied: {} objects, {} relation(s), {} destination access record(s)",
        receipt.nodes,
        receipt.relations,
        receipt.destination_access_records.len()
    );

    println!("  --- session 3: transfer intent first ---");
    let mut third = dial(&carrier, peer, &me, &owner, network).await?;
    let response = third
        .request(close_id + 1, CarrierRequestBody::Intent(begin))
        .await?;
    match response.body {
        Err(failure) if expect_revoked && failure.message.contains("revoked") => {
            println!("  revoked transfer intent refused before endpoint dispatch");
        }
        Ok(CarrierResponseBody::Intent(IntentResult::Accepted)) if !expect_revoked => {
            println!("  granted transfer intent accepted");
            closed(ok(third
                .request(close_id + 2, CarrierRequestBody::Close)
                .await?)?)?;
        }
        Err(failure) => return Err(format!("unexpected transfer refusal: {}", failure.message)),
        Ok(body) => return Err(format!("unexpected transfer-intent answer: {body:?}")),
    }

    println!("H6 physical transfer proof passed");
    Ok(())
}

async fn dial(
    carrier: &P2pandaTransport,
    peer: transport::PeerID,
    me: &InMemoryProvider,
    owner: &InMemoryProvider,
    network: NetworkId,
) -> Result<AdmittedIo, String> {
    let started = Instant::now();
    let mut stream = loop {
        match carrier.connect(peer, projection_alpn()).await {
            Ok(stream) => break stream,
            Err(error) => {
                if started.elapsed() >= DIAL_DEADLINE {
                    return Err(format!("connect: {error}"));
                }
                sleep(std::time::Duration::from_millis(250)).await;
            }
        }
    };
    let subject = me.master_public_key().to_bytes();
    let local = transport::PeerID::from_bytes(&subject).map_err(|error| error.to_string())?;
    let binding = initiator_binding(&projection_alpn(), local);
    let certificate = grant(owner, subject, network, now_ms() + 3_600_000);
    let grant_id = hex(&certificate.certificate.id().0);
    let hello = open_session(
        me,
        network,
        profile(),
        TrafficClass::Interactive,
        [5; 32],
        &binding,
        vec![certificate],
    )
    .map_err(|error| format!("hello: {error}"))?;
    match initiate_session(&mut stream, &hello, &Default::default())
        .await
        .map_err(|error| format!("handshake: {error}"))?
    {
        SessionReply::Reject { reason } => return Err(format!("admission refused: {reason:?}")),
        SessionReply::Accept { .. } => println!("  admitted"),
    }
    let (reader, writer) = tokio::io::split(stream);
    Ok(AdmittedIo {
        lines: BufReader::new(reader).lines(),
        writer,
        grant_id,
    })
}

fn open_body() -> CarrierRequestBody {
    CarrierRequestBody::Open(Box::new(SessionOpen {
        version: ProtocolVersion::V1,
        capabilities: CapabilityProfile::default(),
    }))
}

fn ok(response: CarrierResponse) -> Result<CarrierResponseBody, String> {
    response.body.map_err(|failure| failure.message)
}

fn opened(body: CarrierResponseBody) -> Result<Box<chirograph::SessionOpened>, String> {
    match body {
        CarrierResponseBody::Opened(opened) => Ok(opened),
        other => Err(format!("expected opened, found {other:?}")),
    }
}

fn snapshot(body: CarrierResponseBody) -> Result<Box<chirograph::ProjectionSnapshot>, String> {
    match body {
        CarrierResponseBody::Snapshot(snapshot) => Ok(snapshot),
        other => Err(format!("expected snapshot, found {other:?}")),
    }
}

fn resource(body: CarrierResponseBody) -> Result<ResourceResponse, String> {
    match body {
        CarrierResponseBody::Resource(resource) => Ok(resource),
        other => Err(format!("expected resource, found {other:?}")),
    }
}

fn intent(body: CarrierResponseBody) -> Result<IntentResult, String> {
    match body {
        CarrierResponseBody::Intent(intent) => Ok(intent),
        other => Err(format!("expected intent result, found {other:?}")),
    }
}

fn resume(body: CarrierResponseBody) -> Result<ResumeReply, String> {
    match body {
        CarrierResponseBody::Resume(reply) => Ok(reply),
        other => Err(format!("expected resume, found {other:?}")),
    }
}

fn suspended(body: CarrierResponseBody) -> Result<(), String> {
    match body {
        CarrierResponseBody::Suspended => Ok(()),
        other => Err(format!("expected suspended, found {other:?}")),
    }
}

fn closed(body: CarrierResponseBody) -> Result<(), String> {
    match body {
        CarrierResponseBody::Closed => Ok(()),
        other => Err(format!("expected closed, found {other:?}")),
    }
}

fn verify_address(response: &ResourceResponse) -> Result<(), String> {
    if !response.has_valid_address() {
        return Err(format!("resource {} failed its address", response.resource));
    }
    Ok(())
}

async fn verify_destination(
    host: &MereHost<MemoryBackend>,
    blobs: &BlobStore<MemoryBackend>,
    manifest: &TransferManifestV1,
    receipt: &graphshell::transfer::TransferReceiptV1,
) -> Result<(), String> {
    if receipt.nodes != 2
        || receipt.relations != 1
        || receipt.destination_access_records.len() != 2
        || receipt
            .id_map
            .iter()
            .any(|mapping| mapping.source != mapping.destination)
    {
        return Err(format!("destination receipt is incomplete: {receipt:?}"));
    }
    for mapping in &receipt.id_map {
        let (_, node) = host
            .graph()
            .get_node_by_id(mapping.destination)
            .ok_or_else(|| format!("destination is missing {}", mapping.destination))?;
        if !node.tags.contains("h6") || !node.tags.contains("physical") {
            return Err(format!("{} lost its transfer tags", mapping.destination));
        }
    }
    if host.graph().relations().count() != 1 {
        return Err("destination did not preserve the Cites relation".to_string());
    }
    for descriptor in &manifest.blobs {
        let hash = muniment::Hash::from_hex(&descriptor.content_hash.to_hex())
            .ok_or("invalid blob hash")?;
        if !blobs.has(&hash).await.map_err(|error| error.to_string())? {
            return Err(format!(
                "destination is missing blob {}",
                descriptor.content_hash
            ));
        }
    }
    Ok(())
}