|
1 | 1 | [MEWconnect Demo](https://myetherwallet.github.io/MEWconnect-web-client/#/home)
|
2 | 2 |
|
3 | 3 |
|
4 |
| -MEWconnect Client for MEW wallet |
5 |
| -========== |
6 |
| - |
7 |
| - |
8 |
| -With the MEWconnect client, users can use your DApp in any desktop browser without |
9 |
| -installing an extension. Additionally, end-to-end encryption using client-side generated |
10 |
| -keys keeps all user activity private. |
11 |
| - |
12 |
| -For DApp developers to integrate with MEW wallet using MEWconnect, all you need to do is drop a |
13 |
| -few lines of code into your application, and the MEW wallet app and MEWconnect client will take care of the |
14 |
| -rest. |
15 |
| - |
16 |
| -## Getting Started |
17 |
| - |
18 |
| -### Installation |
19 |
| - |
20 |
| -```shell |
21 |
| -# With NPM |
22 |
| -npm install @myetherwallet/mewconnect-web-client |
23 |
| -``` |
24 |
| - |
25 |
| -### Initializing MEWconnect and a Web3 instance using the default node |
26 |
| - |
27 |
| -```javascript |
28 |
| -import MEWconnect from "@myetherwallet/mewconnect-web-client" |
29 |
| -import Web3 from "web3" |
30 |
| - |
31 |
| -const CHAIN_ID = 1 |
32 |
| - |
33 |
| -// Initialize |
34 |
| -export const mewConnect = new MEWconnect.Provider() |
35 |
| - |
36 |
| -// Initialize a Web3 Provider object |
37 |
| -export const ethereum = mewConnect.makeWeb3Provider(CHAIN_ID) |
38 |
| - |
39 |
| -// Initialize a Web3 object |
40 |
| -export const web3 = new Web3(ethereum) |
41 |
| - |
42 |
| -``` |
43 |
| - |
44 |
| -#### Alternatively, a node rpc url may be supplied |
45 |
| - |
46 |
| -- Note: only websocket urls are supported. |
47 |
| -```javascript |
48 |
| -import MEWconnect from "@myetherwallet/mewconnect-web-client" |
49 |
| -import Web3 from "web3" |
50 |
| - |
51 |
| -const ETH_JSONRPC_URL = "wss://mainnet.infura.io/v3/<YOUR_INFURA_API_KEY>" |
52 |
| -const CHAIN_ID = 1 |
53 |
| - |
54 |
| -// Initialize MEWconnect |
55 |
| -export const mewConnect = new MEWconnect.Provider() |
56 |
| - |
57 |
| -// Initialize a Web3 Provider object |
58 |
| -export const ethereum = mewConnect.makeWeb3Provider(CHAIN_ID, ETH_JSONRPC_URL) |
59 |
| - |
60 |
| -// Initialize a Web3 object |
61 |
| -export const web3 = new Web3(ethereum) |
62 |
| - |
63 |
| -``` |
64 |
| - |
65 |
| -### Use EIP-1102 to obtain authorization and get Ethereum accounts |
66 |
| - |
67 |
| -Invoking EIP-1102 will show a QR code dialog if the user's mobile wallet is not |
68 |
| -already connected to their browser. The following code should run in response to |
69 |
| -a user-initiated action such as clicking a button to ensure the pop up is not |
70 |
| -blocked by the browser. |
71 |
| - |
72 |
| -```javascript |
73 |
| -// Use eth_RequestAccounts |
74 |
| -ethereum.send("eth_requestAccounts").then((accounts) => { |
75 |
| - console.log(`User's address is ${accounts[0]}`) |
76 |
| - |
77 |
| -}) |
78 |
| - |
79 |
| -// Alternatively, you can use ethereum.enable() |
80 |
| -ethereum.enable().then((accounts) => { |
81 |
| - console.log(`User's address is ${accounts[0]}`) |
82 |
| -}) |
83 |
| -``` |
84 |
| - |
85 |
| -That's it! Once the connection between the phone and the site is established, the Web3 object |
86 |
| -(`web3`) and the Web3 Provider (`ethereum`) are ready to be used as usual. |
87 |
| - |
88 |
| -## MEWconnect.Provider |
89 |
| - |
90 |
| -### Options |
91 |
| -The MEWconnect.Provider can take an options object with the following fields: |
92 |
| - |
93 |
| - - windowClosedError (default ```false```) Indicates whether the provider should throw an error when the popup window is closed by the user. |
94 |
| - |
95 |
| -### Events |
96 |
| -```javascript |
97 |
| -const mewConnect = new MEWconnect.Provider() |
98 |
| -mewConnect.on('[event]') |
99 |
| -``` |
100 |
| - - popupWindowClosed |
101 |
| - |
102 |
| - Emitted when the popup window is closed by the user |
103 |
| - |
104 |
| -- disconnected |
105 |
| - |
106 |
| - Emitted when the user becomes disconnected (Both by disconnecting and if the connection is broken) |
107 |
| - |
108 |
| ---- |
109 |
| -## Example |
110 |
| -An example may be found in the example directory |
111 |
| - |
112 |
| ---- |
113 |
| - |
114 |
| -## Tokens |
115 |
| -In order to have your token included in the list of tokens identified by the app the token needs to be included as a token in the [ethereum-lists](https://github.com/MyEtherWallet/ethereum-lists) repository. |
116 |
| - |
117 |
| -Instructions one how to add a token there may be found [here](https://kb.myetherwallet.com/en/tokens/adding-token-as-a-default/) |
118 |
| - |
119 |
| -## Debugging |
120 |
| - |
121 |
| -The MEWconnect client uses the debug library to provide verbose debug logging. In local storage add the key 'debug' with the value: |
122 |
| - - \* |
123 |
| - - to see everything |
124 |
| - - MEWconnect: * |
125 |
| - - to see everything related to the MEWconnect client |
126 |
| - - MEWconnect:connection-state |
127 |
| - - to see the connection state when it changes |
128 |
| - - MEWconnect:webRTC-communication |
129 |
| - - to see the events and signals related to webRTC |
130 |
| - - MEWconnect:websocketWrapper |
131 |
| - - to see the events related to setting up the webRTC connection |
132 |
| - - MEWconnect:*,MEWconnectVerbose:*,simple-peer |
133 |
| - - what we usually use for debugging |
134 |
| - |
135 |
| - |
136 |
| -<br> |
137 |
| -<br> |
138 |
| -<br> |
139 |
| -<br> |
140 |
| -<br> |
141 |
| -<br> |
142 |
| -<br> |
143 |
| -<br> |
144 |
| - |
145 |
| ----- |
146 |
| - |
147 |
| - |
148 |
| - |
149 |
| - |
150 |
| - |
151 |
| - |
152 |
| - |
153 |
| - |
154 |
| - |
155 |
| - |
156 |
| - |
157 |
| - |
158 |
| - |
159 |
| - |
160 |
| - |
161 |
| - |
162 |
| - |
163 |
| - |
164 |
| - |
165 |
| - |
166 |
| - |
167 |
| - |
168 |
| -### Using only the wallet with an external web3 instance (not recommended) |
169 |
| - |
170 |
| -Install the client |
171 |
| -```npm i @myetherwallet/mewconnect-web-client``` |
172 |
| - |
173 |
| - |
174 |
| - |
175 |
| - |
176 |
| -### Get the code |
177 |
| -The example requires both MEWconnect-web-client (this repo) and MEWconnect-Signal-Server (MEWconnect-hanshake-server) |
178 |
| - |
179 |
| -``` |
180 |
| -git clone https://github.com/MyEtherWallet/MEWconnect-web-client.git |
181 |
| -``` |
182 |
| -Install the dependencies: |
183 |
| - |
184 |
| -``` |
185 |
| -npm install |
186 |
| -``` |
187 |
| - |
188 |
| -Start the server serving the example initiator and receiver: |
189 |
| -``` |
190 |
| -npm start |
191 |
| -``` |
192 |
| - |
193 |
| -###### Get the signaling server |
194 |
| -Clone the repo: |
195 |
| - |
196 |
| -``` |
197 |
| -git clone https://github.com/MyEtherWallet/MEWconnect-hanshake-server.git |
198 |
| -``` |
199 |
| -Install the dependencies: |
200 |
| - |
201 |
| -``` |
202 |
| -npm install |
203 |
| -``` |
204 |
| - |
205 |
| -Start the signaling server: |
206 |
| -``` |
207 |
| -npm start |
208 |
| -``` |
209 |
| - |
210 |
| -### Usage |
211 |
| -Two Peers are needed with one designated as the Initiator and the other as the Receiver. |
212 |
| - |
213 |
| -Require the MEWconnect client |
214 |
| -```javascript |
215 |
| -let mewConnect = require('@myetherwallet/mewconnect-web-client').Client; |
216 |
| -``` |
217 |
| - |
218 |
| -Initiate the client |
219 |
| -```javascript |
220 |
| -let mewConnectClient = mewConnect.init(); |
221 |
| -``` |
222 |
| - |
223 |
| -MEWconnect Client functions as an event emitter. |
224 |
| -The connection details are passed along with the 'codeDisplay' event |
225 |
| -```javascript |
226 |
| -mewConnectClient.on('codeDisplay', code => { |
227 |
| -// do something with the code. |
228 |
| -// to work with the MEWconnect Mobile applications display it as a qrcode |
229 |
| -} |
230 |
| -``` |
231 |
| -
|
232 |
| -
|
233 |
| -Now call the initiatorStart method to create the connection details: |
234 |
| -```javascript |
235 |
| -mewConnectClient.initiatorStart('https://signal-server-url') |
236 |
| -``` |
237 |
| -
|
238 |
| -Once a p2p connection is established the client will emit a 'rtcConnected' event |
239 |
| -```javascript |
240 |
| -mewConnectClient.on('rtcConnected', () =>{ |
241 |
| - alert('congrats you are connected to mew connect!') |
242 |
| -}) |
243 |
| -``` |
244 |
| -
|
245 |
| -Once a connection is extablished call the 'sendRtcMessage' method to interact with the app |
246 |
| -```javascript |
247 |
| -mewConnectClient.sendRtcMessage('address', {}) |
248 |
| -``` |
249 |
| -
|
250 |
| -The 'sendRtcMessage' method takes two parameters (message type, message data) |
251 |
| -
|
252 |
| -To get the response listen for an event matching the sent message type |
253 |
| -```javascript |
254 |
| -mewConnectClient.on('address', address => { |
255 |
| - alert('got address: ' + address) |
256 |
| -}) |
257 |
| -``` |
258 |
| -
|
259 |
| -Currently the app supports two other message types: 'signMessage', and 'signTx' |
260 |
| -
|
261 |
| -exists you can get the address or send a transaction or message to the mobile app for signing. |
262 |
| -
|
263 |
| -The data portion of those two message types are: |
264 |
| -
|
265 |
| -**signMessage** |
266 |
| -```json |
267 |
| -{ |
268 |
| - hash: 'hash of the message to be signed', |
269 |
| - text: 'text of the message to be signed' |
270 |
| -} |
271 |
| -``` |
272 |
| -
|
273 |
| -**signTx** |
274 |
| -```json |
275 |
| -{ |
276 |
| - nonce:"0x00", |
277 |
| - gasPrice:"0x098bca5a00", |
278 |
| - gas:"0x5208", |
279 |
| - to:"0xc3982F1DbAB6DA9d95F579B9A5f9c5CAb13F8cfC", |
280 |
| - value:"0xb1a2bc2ec50000", |
281 |
| - data:"", |
282 |
| - chainId:3 |
283 |
| - |
284 |
| -} |
285 |
| -``` |
286 |
| -
|
287 |
| -If the p2p connection fails to be established the client can attempt to use an intermediate TURN server to facilitate the connection. |
288 |
| -To signal a failed p2p attempt the client can call the 'useFallback' method on the client |
289 |
| -```javascript |
290 |
| -mewConnectClient.useFallback() |
291 |
| -``` |
292 |
| -
|
293 |
| -Additional events are emitted at various points to signal various stages of the connection |
294 |
| -
|
295 |
| -**SocketConnectedEvent** |
296 |
| -- successfully connected to the signal server |
297 |
| -
|
298 |
| -**RtcInitiatedEvent** |
299 |
| -- Peer identified via the signal server, and a p2p connection will be attempted |
300 |
| -
|
301 |
| -**UsingFallback** |
302 |
| -- One of the peers failed to establish a p2p connection and will attempt to use an intermediate TURN server to facilitate the connection |
303 |
| -
|
304 |
| -**RtcConnectedEvent** |
305 |
| -- p2p connection established |
306 |
| -
|
307 |
| -**RtcClosedEvent** |
308 |
| -- p2p connection closed |
309 |
| -
|
310 |
| -**RtcDisconnectEvent** |
311 |
| -- p2p disconnected |
312 |
| -
|
313 |
| -**RtcErrorEvent** |
314 |
| -- p2p connection error occured |
315 |
| -
|
316 |
| -
|
317 |
| -
|
318 |
| -
|
319 |
| -
|
320 |
| -
|
321 |
| ---- |
322 |
| -##### Browser |
323 |
| -mew-connect-client can be included for use in the browser via webpack or browerfy |
| 4 | +Documentation for MEWconnect-web-client can be found [here](https://myetherwallet.github.io/MEWconnect-Protocol-Documentation/) |
0 commit comments