Skip to content

Commit

Permalink
Add notes and plans, and todos. oh my! much to do before I can even g…
Browse files Browse the repository at this point in the history
…et this running
  • Loading branch information
NullVoxPopuli committed Sep 30, 2019
1 parent 53b2049 commit 2b36b95
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Schema {
AUTHORIZED: {}
}
},
RECEIVED_REQUEST: {
SCANNED_TARGET: {
states: {
WAITING_FOR_CODE: {},
WAITING_FOR_DATA: {},
Expand All @@ -24,7 +24,7 @@ export interface Schema {

export type Event =
| { type: 'INITIATE_TRANSFER_REQUEST' }
| { type: 'RECEIVE_TRANSFER_REQUEST' }
| { type: 'SCAN_TARGET_PROFILE' }
| { type: 'RECEIVE_READY' }
| { type: 'RECEIVE_CODE' }
| { type: 'DATA_SENT' }
Expand All @@ -34,7 +34,7 @@ export type Event =

export const TRANSITION = {
START: 'INITIATE_TRANSFER_REQUEST',
RECEIVE_REQUEST: 'RECEIVE_TRANSFER_REQUEST',
SCAN_TARGET_PROFILE: 'SCAN_TARGET_PROFILE',
RECEIVE_READY: 'RECEIVE_READY',
RECEIVE_CODE: 'RECEIVE_CODE',
DATA_SENT: 'DATA_SENT',
Expand All @@ -50,7 +50,7 @@ export const transferToDeviceMachine = Machine<{}, Schema, Event>({
'IDLE': {
on: {
[TRANSITION.START]: 'WAITING_FOR_START',
[TRANSITION.RECEIVE_REQUEST]: 'RECEIVED_REQUEST',
[TRANSITION.SCAN_TARGET_PROFILE]: 'SCANNED_TARGET',
},
},

Expand Down Expand Up @@ -90,7 +90,7 @@ export const transferToDeviceMachine = Machine<{}, Schema, Event>({
// generate ehemeral keys
// - scanned public key is originator
// send "ready" message to originator
'RECEIVED_REQUEST': {
'SCANNED_TARGET': {
on: {
[TRANSITION.SEND_READY]: 'WAITING_FOR_CODE'
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import StoreService from 'ember-data/store';
import RouterService from '@ember/routing/router-service';

import Service, { inject as service } from '@ember/service';
import { interpret, Interpreter, State } from 'xstate';
import { transferToDeviceMachine, Schema, Event, TRANSITION } from './machine';
Expand All @@ -17,6 +19,7 @@ import MessageDispatcher from 'emberclear/src/services/messages/dispatcher';
export default class TransferToDevice extends Service {
@service currentUser!: CurrentUserService;
@service store!: StoreService;
@service router!: RouterService;
@service('messages/dispatcher') messageDispatcher!: MessageDispatcher;

machine: Interpreter<{}, Schema, Event>;
Expand All @@ -32,17 +35,73 @@ export default class TransferToDevice extends Service {
private handleEvent(listenerState: State<{}, Event>) {
const { event: { type: eventName }, value: currentState } = listenerState;

// This represents the linear flow of the state machine between the two computers
switch (eventName) {
case TRANSITION.START:
console.log('started');
// ---- Initiator: 1
// generate ephemeral keys
// generate QR Code to Scan
// - only contains public key
// create a second connection to the relay
// TODO: need to support additional connections
//
// these things could be route based.
// maybe this transition handler just ... transitions to a route.
this.router.transitionTo('settings.transfer-to-device');
break;
case TRANSITION.SCAN_TARGET_PROFILE:
// ---- Receiver: 1
// prior to this step, the receiver will need to go to the
// login screen, and click import from another device or something.
// this person must scan the QR code on the initiator's screen.
// then, generate ephemeral keys in order to communicate encryptedly
// send the ready message.

// NOTE: this step should be very fast (as it's automated)
// not sure if it's worth showing a loading/spinner or anything
this.router.transitionTo('login.import-from-device');
break;
case TRANSITION.SEND_READY:
case TRANSITION.SEND_CODE:
case TRANSITION.RECEIVE_REQUEST:
// ---- Receiver: 2
// ready message is sent to initiator, as we are now connected
// transition to a screen showing that we can enter the code
// TODO: we may want a way to integrate the state machine restrictions
// into the router
this.router.transitionTo('login.import-from-device.enter-code');
break;
case TRANSITION.RECEIVE_READY:
// ---- Initiator 2:
// A connection is now established with both sets of ephemeral keys
// Generate a short code that can be typed on the receiver's screen,
// This code is only transferred visually, and not via QR, in case
// there are other QR scanners nearby.
this.router.transitionTo('settings.transfer-to-device.waiting-for-auth');
break;
case TRANSITION.SEND_CODE:
// ---- Receiver: 3
// Once the code is manually read off the screen and typed in on
// the receiver's computer, the code is sent over the ephemeral
// channel so that the initiator can confirm it.
// This is an auth attempt.
this.router.transitionTo('login.import-from-device.waiting-for-data');
break;
case TRANSITION.RECEIVE_CODE:
// ---- Initiator: 3
// upon receiving the code, we can transition to another route that
// begins the sending of the data: keys, contacts, messages, etc
this.router.transitionTo('settings.transfer-to-device.send-data');
break;
case TRANSITION.DATA_SENT:
// ---- Initiator: 4
// data sent. Waiting on the receiver to finish.
// waiting for confirmation (via sha)
this.router.transitionTo('settings.transfer-to-device.done');
break;
case TRANSITION.DATA_RECEIVED:
// ---- Receiver: 4
// data received, import it.
this.router.transitionTo('login.import-from-device.done');
break;
default:
console.log('not handled', eventName);

Expand Down

0 comments on commit 2b36b95

Please sign in to comment.