Skip to content
This repository has been archived by the owner on Jan 24, 2020. It is now read-only.

Commit

Permalink
Start getting two wallets to communicate
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgordstewart committed Dec 8, 2019
1 parent 6d6ebe2 commit 5b3f6f6
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { AnyEventObject, interpret, Interpreter } from 'xstate';
import { getChannelID, pretty } from '.';
import { messageService } from './messaging';
import { Wallet } from './protocols';
import { AddressableMessage } from './wire-protocol';

import { CreateChannelEvent } from './protocols/wallet/protocol';
import { Store } from './store';

const store = name => {
const privateKeys = { [name]: name };
const _store = new Store({ privateKeys });
messageService.on('message', (m: AddressableMessage) => {
if (m.to === name) {
switch (m.type) {
case 'SendStates':
_store.receiveStates(m.signedStates);
}
}
});

return _store;
};

const first = 'first';
const second = 'second';
const stores = {
first: store(first),
second: store(second),
};

const logEvents = name => event =>
console.log(`${name} received ${event.type}`);

const wallet = name => {
return interpret(Wallet.machine(stores[name]))
.onEvent(logEvents(name))
.start();
};

const wallets: Record<string, Interpreter<Wallet.Init, any, AnyEventObject>> = {
first: wallet(first),
second: wallet(second),
};

// This is sort of the "dispatcher"
messageService.on('message', ({ to, ...event }: AddressableMessage) => {
switch (event.type) {
case 'SendStates': {
stores[to].receiveStates(event.signedStates);
const channelId = getChannelID(event.signedStates[0].state.channel);

wallets[to].send({
type: 'CHANNEL_UPDATED',
channelId,
});
break;
}
case 'OPEN_CHANNEL': {
wallets[to].send(event);
break;
}
}
});

const createChannel: CreateChannelEvent = {
type: 'CREATE_CHANNEL',
participants: [
{
participantId: first,
signingAddress: first,
destination: first,
},
{
participantId: second,
signingAddress: second,
destination: second,
},
],
allocations: [
{ destination: first, amount: '3' },
{ destination: second, amount: '1' },
],
appDefinition: '0x',
appData: '0x',
};

wallets[first].send(createChannel);

0 comments on commit 5b3f6f6

Please sign in to comment.