This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
setup.ts
84 lines (66 loc) · 2.13 KB
/
setup.ts
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
import { AssetType, NetworkContext } from "@counterfactual/types";
import { SetupCommitment } from "../ethereum";
import { StateChannel } from "../models/state-channel";
import { Opcode } from "../opcodes";
import { ProtocolMessage } from "../protocol-types-tbd";
import { Context } from "../types";
import { prepareToSendSignature } from "./signature-forwarder";
/**
* @description This exchange is described at the following URL:
*
* specs.counterfactual.com/04-setup-protocol#messages
*
*/
export const SETUP_PROTOCOL = {
0: [
// Compute the next state of the channel
proposeStateTransition,
// Decide whether or not to sign the transition
Opcode.OP_SIGN,
// Wrap the signature into a message to be sent
prepareToSendSignature,
// Send the message to your counterparty
Opcode.IO_SEND,
// Wait for them to countersign the message
Opcode.IO_WAIT,
// Verify they did indeed countersign the right thing
Opcode.OP_SIGN_VALIDATE,
// Consider the state transition finished and commit it
Opcode.STATE_TRANSITION_COMMIT
],
1: [
// Compute the _proposed_ next state of the channel
proposeStateTransition,
// Validate your counterparties signature is for the above proposal
Opcode.OP_SIGN_VALIDATE,
// Sign the same state update yourself
Opcode.OP_SIGN,
// Wrap the signature into a message to be sent
prepareToSendSignature,
// Send the message to your counterparty
Opcode.IO_SEND,
// Consider the state transition finished and commit it
Opcode.STATE_TRANSITION_COMMIT
]
};
function proposeStateTransition(
message: ProtocolMessage,
context: Context,
state: StateChannel
) {
context.stateChannel = state.setupChannel(context.network);
context.operation = constructSetupOp(context.network, context.stateChannel);
}
export function constructSetupOp(
network: NetworkContext,
stateChannel: StateChannel
) {
const freeBalance = stateChannel.getFreeBalanceFor(AssetType.ETH);
return new SetupCommitment(
network,
stateChannel.multisigAddress,
stateChannel.multisigOwners,
freeBalance.identity,
freeBalance.terms
);
}