UNPKG

3.73 kBMarkdownView Raw
1<!--##### Note This Repository Nees Some Cleaning-->
2
3### Getting Started
4
5
6### Get the code
7The example requires both MEWconnect-web-client (this repo) and MEWconnect-Signal-Server (MEWconnect-hanshake-server)
8```
9git clone https://github.com/MyEtherWallet/MEWconnect-web-client.git
10```
11Install the dependencies:
12
13```
14npm install
15```
16
17Start the server serving the example initiator and receiver:
18```
19npm start
20```
21
22###### Get the signaling server
23Clone the repo:
24
25```
26git clone https://github.com/MyEtherWallet/MEWconnect-hanshake-server.git
27```
28Install the dependencies:
29
30```
31npm install
32```
33
34Start the signaling server:
35```
36npm start
37```
38
39### Usage
40Two Peers are needed with one designated as the Initiator and the other as the Receiver.
41
42Require the MEWconnect client
43```javascript
44let mewConnect = require('@myetherwallet/mewconnect-web-client').Client;
45```
46
47Initiate the client
48```javascript
49let mewConnectClient = mewConnect.init();
50```
51
52MEWconnect Client functions as an event emitter.
53The connection details are passed along with the 'codeDisplay' event
54```javascript
55mewConnectClient.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
62Now call the initiatorStart method to create the connection details:
63```javascript
64mewConnectClient.initiatorStart('https://signal-server-url')
65```
66
67Once a p2p connection is established the client will emit a 'rtcConnected' event
68```javascript
69mewConnectClient.on('rtcConnected', () =>{
70 alert('congrats you are connected to mew connect!')
71})
72```
73
74Once a connection is extablished call the 'sendRtcMessage' method to interact with the app
75```javascript
76mewConnectClient.sendRtcMessage('address', {})
77```
78
79The 'sendRtcMessage' method takes two parameters (message type, message data)
80
81To get the response listen for an event matching the sent message type
82```javascript
83mewConnectClient.on('address', address => {
84 alert('got address: ' + address)
85})
86```
87
88Currently the app supports two other message types: 'signMessage', and 'signTx'
89
90exists you can get the address or send a transaction or message to the mobile app for signing.
91
92The 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
116If the p2p connection fails to be established the client can attempt to use an intermediate TURN server to facilitate the connection.
117To signal a failed p2p attempt the client can call the 'useFallback' method on the client
118```javascript
119mewConnectClient.useFallback()
120```
121
122Additional 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
152mew-connect-client can be included for use in the browser via webpack or browerfy
153
154
155<!-- ##### API -->
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