This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start getting two wallets to communicate
- Loading branch information
1 parent
b8d0ba9
commit 315d671
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |