1 |
|
2 |
|
3 | ### Getting Started
|
4 |
|
5 |
|
6 | ### Get the code
|
7 | The example requires both MEWconnect-web-client (this repo) and MEWconnect-Signal-Server (MEWconnect-hanshake-server)
|
8 | ```
|
9 | git clone https://github.com/MyEtherWallet/MEWconnect-web-client.git
|
10 | ```
|
11 | Install the dependencies:
|
12 |
|
13 | ```
|
14 | npm install
|
15 | ```
|
16 |
|
17 | Start the server serving the example initiator and receiver:
|
18 | ```
|
19 | npm start
|
20 | ```
|
21 |
|
22 | ###### Get the signaling server
|
23 | Clone the repo:
|
24 |
|
25 | ```
|
26 | git clone https://github.com/MyEtherWallet/MEWconnect-hanshake-server.git
|
27 | ```
|
28 | Install the dependencies:
|
29 |
|
30 | ```
|
31 | npm install
|
32 | ```
|
33 |
|
34 | Start the signaling server:
|
35 | ```
|
36 | npm start
|
37 | ```
|
38 |
|
39 | ### Usage
|
40 | Two Peers are needed with one designated as the Initiator and the other as the Receiver.
|
41 |
|
42 | Require the MEWconnect client
|
43 | ```javascript
|
44 | let mewConnect = require('@myetherwallet/mewconnect-web-client').Client;
|
45 | ```
|
46 |
|
47 | Initiate the client
|
48 | ```javascript
|
49 | let mewConnectClient = mewConnect.init();
|
50 | ```
|
51 |
|
52 | MEWconnect Client functions as an event emitter.
|
53 | The connection details are passed along with the 'codeDisplay' event
|
54 | ```javascript
|
55 | mewConnectClient.on('codeDisplay', code => {
|
56 | // do something with the code.
|
57 | // to work with the MEWconnect Mobile applications display it as a qrcode
|
58 | }
|
59 | ```
|
60 |
|
61 |
|
62 | Now call the initiatorStart method to create the connection details:
|
63 | ```javascript
|
64 | mewConnectClient.initiatorStart('https://signal-server-url')
|
65 | ```
|
66 |
|
67 | Once a p2p connection is established the client will emit a 'rtcConnected' event
|
68 | ```javascript
|
69 | mewConnectClient.on('rtcConnected', () =>{
|
70 | alert('congrats you are connected to mew connect!')
|
71 | })
|
72 | ```
|
73 |
|
74 | Once a connection is extablished call the 'sendRtcMessage' method to interact with the app
|
75 | ```javascript
|
76 | mewConnectClient.sendRtcMessage('address', {})
|
77 | ```
|
78 |
|
79 | The 'sendRtcMessage' method takes two parameters (message type, message data)
|
80 |
|
81 | To get the response listen for an event matching the sent message type
|
82 | ```javascript
|
83 | mewConnectClient.on('address', address => {
|
84 | alert('got address: ' + address)
|
85 | })
|
86 | ```
|
87 |
|
88 | Currently the app supports two other message types: 'signMessage', and 'signTx'
|
89 |
|
90 | exists you can get the address or send a transaction or message to the mobile app for signing.
|
91 |
|
92 | The data portion of those two message types are:
|
93 |
|
94 | **signMessage**
|
95 | ```json
|
96 | {
|
97 | hash: 'hash of the message to be signed',
|
98 | text: 'text of the message to be signed'
|
99 | }
|
100 | ```
|
101 |
|
102 | **signTx**
|
103 | ```json
|
104 | {
|
105 | nonce:"0x00",
|
106 | gasPrice:"0x098bca5a00",
|
107 | gas:"0x5208",
|
108 | to:"0xc3982F1DbAB6DA9d95F579B9A5f9c5CAb13F8cfC",
|
109 | value:"0xb1a2bc2ec50000",
|
110 | data:"",
|
111 | chainId:3
|
112 |
|
113 | }
|
114 | ```
|
115 |
|
116 | If the p2p connection fails to be established the client can attempt to use an intermediate TURN server to facilitate the connection.
|
117 | To signal a failed p2p attempt the client can call the 'useFallback' method on the client
|
118 | ```javascript
|
119 | mewConnectClient.useFallback()
|
120 | ```
|
121 |
|
122 | Additional events are emitted at various points to signal various stages of the connection
|
123 |
|
124 | **SocketConnectedEvent**
|
125 | - successfully connected to the signal server
|
126 |
|
127 | **RtcInitiatedEvent**
|
128 | - Peer identified via the signal server, and a p2p connection will be attempted
|
129 |
|
130 | **UsingFallback**
|
131 | - One of the peers failed to establish a p2p connection and will attempt to use an intermediate TURN server to facilitate the connection
|
132 |
|
133 | **RtcConnectedEvent**
|
134 | - p2p connection established
|
135 |
|
136 | **RtcClosedEvent**
|
137 | - p2p connection closed
|
138 |
|
139 | **RtcDisconnectEvent**
|
140 | - p2p disconnected
|
141 |
|
142 | **RtcErrorEvent**
|
143 | - p2p connection error occured
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | ##### Browser
|
152 | mew-connect-client can be included for use in the browser via webpack or browerfy
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | ## Run Tests
|
161 | *first the ```import WebSocket from 'promise-ws'``` line needs to be uncommented in websocketWrapper.js*
|
162 | ```npm run test``` |
\ | No newline at end of file |