From 4b39ccd524a9c5e933cb7a44580dcf2fc8cf637f Mon Sep 17 00:00:00 2001 From: SteveM Date: Thu, 11 Mar 2021 16:38:09 -0800 Subject: [PATCH] working on getting close and disconnect events propagating --- example/app/src/App.vue | 5 +- jest.config.js | 6 +-- package-lock.json | 2 +- package.json | 3 +- src/connectClient/WebRtcCommunication.js | 7 +++ src/connectClient/constants/signals.js | 3 +- .../initiator/MewConnectInitiator.js | 5 +- .../initiator/MewConnectInitiatorV2.js | 14 +++++- src/connectClient/websocketWrapper.js | 4 +- src/connectProvider/index.js | 22 +++++++- .../web3-provider/providers/http-provider.js | 31 ++++++++++++ .../providers/ws-web3-provider.js | 50 +++++++++++++++++++ src/connectWindow/popUpCreator.js | 21 ++++++-- test/clients/receiverV1.js | 2 +- ....pairing.test.js => basic.pairing.spec.js} | 6 +++ tests/helpers/MewConnectReceiver.js | 5 +- tests/specs/MewConnectInitiator.spec.js | 7 +-- 17 files changed, 169 insertions(+), 24 deletions(-) rename test/integration/{basic.pairing.test.js => basic.pairing.spec.js} (98%) diff --git a/example/app/src/App.vue b/example/app/src/App.vue index bb3d7dec..1aa736aa 100644 --- a/example/app/src/App.vue +++ b/example/app/src/App.vue @@ -4,7 +4,7 @@

{{ userAddress }}

- +
@@ -468,6 +468,9 @@ export default { // }) }, methods: { + doThing(){ + + }, showThing() { console.log('CONSOLE CHECK'); // todo remove dev item diff --git a/jest.config.js b/jest.config.js index 7cf418cc..abbb0fb8 100644 --- a/jest.config.js +++ b/jest.config.js @@ -4,10 +4,10 @@ module.exports = { collectCoverage: true, setTimeout: 10000, coverageDirectory: 'coverage',*/ - // testEnvironment: 'jsdom', - testMatch: ['**/__tests__/**/*.js?(x)', '**/test/**/?(*.)+(spec|test).js?(x)'], + testEnvironment: 'jsdom', + testMatch: ['**/__tests__/**/*.js?(x)', '**/tests/**/?(*.)+(spec|test).js?(x)', '**/test/**/?(*.)+(spec|test).js?(x)'], testPathIgnorePatterns: ['/node_modules/'], - "testEnvironment": "node", + // "testEnvironment": "node", /* transform: { "^.+\\.(js) ? $": "babel-jest" },*/ diff --git a/package-lock.json b/package-lock.json index 14bb280e..731814ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@myetherwallet/mewconnect-web-client", - "version": "2.1.23-beta.5", + "version": "2.1.23-beta.7", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 50eb8166..ba3d1e7d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@myetherwallet/mewconnect-web-client", "homepage": "https://github.com/myetherwallet/MEWconnect-web-client", - "version": "2.1.23-beta.5", + "version": "2.1.23-beta.7", "main": "./dist/index.js", "module": "./src/index.js", "scripts": { @@ -17,6 +17,7 @@ "lint": "npx eslint --fix 'src/**.js'", "update:lists": "node src/connectProvider/fetchLists/index.js", "prepublishOnly": "npm install && npm run update:lists && npm run build", + "prepack": "npm run build", "ci": "npm install && npm run update:lists && npm run build" }, "gitHooks": { diff --git a/src/connectClient/WebRtcCommunication.js b/src/connectClient/WebRtcCommunication.js index 04821d61..a589e8aa 100644 --- a/src/connectClient/WebRtcCommunication.js +++ b/src/connectClient/WebRtcCommunication.js @@ -365,8 +365,10 @@ export default class WebRtcCommunication extends MewConnectCommon { } onClose(peerID, data) { + console.log('1'); // todo remove dev item debugStages('WRTC onClose event'); debug('peerID', peerID); + console.log('onClose', this.connected); // todo remove dev item if (!this.isAlive()) { debugStages('WRTC CLOSE', data); if (this.connected) { @@ -377,6 +379,11 @@ export default class WebRtcCommunication extends MewConnectCommon { this.connected = false; } } + if (this.connected) { + this.emit(this.lifeCycle.RtcClosedEvent); + this.uiCommunicator(this.lifeCycle.disconnected); + this.connected = false; + } } onError(peerID, err) { diff --git a/src/connectClient/constants/signals.js b/src/connectClient/constants/signals.js index bb2d8e46..c6e08cfb 100644 --- a/src/connectClient/constants/signals.js +++ b/src/connectClient/constants/signals.js @@ -41,7 +41,8 @@ const signalV2 = { initiated: 'initiated', socketTimeout: 'socketTimeout', receivedSignal: 'receivedSignal', - error: 'error' + error: 'error', + disconnected: 'disconnected' }; export { signalV1, signalV2 }; diff --git a/src/connectClient/initiator/MewConnectInitiator.js b/src/connectClient/initiator/MewConnectInitiator.js index dc8769b3..1c13c722 100644 --- a/src/connectClient/initiator/MewConnectInitiator.js +++ b/src/connectClient/initiator/MewConnectInitiator.js @@ -258,7 +258,7 @@ export default class MewConnectInitiator extends MewConnectCommon { } } else { this.uiCommunicator(this.lifeCycle.codeDisplay, qrCodeString); - this.uiCommunicator(this.lifeCycle.checkNumber, privateKey); + // this.uiCommunicator(this.lifeCycle.checkNumber, privateKey); this.uiCommunicator(this.lifeCycle.ConnectionId, this.connId); } @@ -394,9 +394,8 @@ Keys this.webRtcCommunication.once( this.jsonDetails.lifeCycle.RtcConnectedEvent, (peerid) => { - console.log(peerid); // todo remove dev item this.webRtcCommunication.removeAllListeners(this.jsonDetails.lifeCycle.RtcConnectedEvent) - console.log('RTC CONNECTED ENVIRONMENT SETUP'); // todo remove dev item + debug('RTC CONNECTED ENVIRONMENT SETUP'); // todo remove dev item this.emit(this.lifeCycle.RtcConnectedEvent); this.webRtcCommunication.on('appData', this.dataReceived.bind(this)); this.connected = true; diff --git a/src/connectClient/initiator/MewConnectInitiatorV2.js b/src/connectClient/initiator/MewConnectInitiatorV2.js index d69e6880..b4b35e92 100644 --- a/src/connectClient/initiator/MewConnectInitiatorV2.js +++ b/src/connectClient/initiator/MewConnectInitiatorV2.js @@ -236,10 +236,22 @@ export default class MewConnectInitiatorV2 extends MewConnectCommon { this.socketConnected = true; }); + this.socket.on('onClose', () => { + debugStages('SOCKET DISCONNECTED'); + this.socketConnected = false; + if (!this.connected) { + // show new QRcode + } + }); + this.socketOn(this.signals.initiated, this.initiated.bind(this)); // response + this.socketOn(this.signals.disconnected, data => { + // this.beginRtcSequence(stunServers); + }); // response + this.socketOn(this.signals.confirmation, data => { this.beginRtcSequence(stunServers); - }); // response + }); // this.signals.answer this.socketOn('answer', this.recieveAnswer.bind(this)); this.socketOn( diff --git a/src/connectClient/websocketWrapper.js b/src/connectClient/websocketWrapper.js index 0379f696..c1dc5e60 100644 --- a/src/connectClient/websocketWrapper.js +++ b/src/connectClient/websocketWrapper.js @@ -132,7 +132,6 @@ export default class WebsocketConnection { } if(parsedMessage.signal === 'ping' || parsedMessage.signal === 'pong') { - console.log(parsedMessage); // todo remove dev item return; } const signal = parsedMessage.signal; @@ -156,6 +155,9 @@ export default class WebsocketConnection { onClose() { debug(`websocket onClose = ${this.getSocketState()}`); + if(this.listeners['onClose']){ + this.listeners['onClose'].call(this); + } } /** diff --git a/src/connectProvider/index.js b/src/connectProvider/index.js index d79c268e..25dabf54 100644 --- a/src/connectProvider/index.js +++ b/src/connectProvider/index.js @@ -293,7 +293,7 @@ export default class Integration extends EventEmitter { web3Provider.close = this.disconnect.bind(this); web3Provider.disconnect = this.disconnect.bind(this); state.web3Provider = web3Provider; - + console.log(state.web3Provider); // todo remove dev item state.web3 = new Web3(web3Provider); if (!this.runningInApp) { state.web3.currentProvider.sendAsync = state.web3.currentProvider.send; @@ -324,12 +324,19 @@ export default class Integration extends EventEmitter { () => { this.popUpHandler.showNotice(messageConstants.disconnect); MEWconnectWallet.setConnectionState(connection.lifeCycle.disconnected); + console.log('DISCONNECT NOTIFIER', state.web3Provider); // todo remove dev item if (state.wallet !== null && state.web3Provider.disconnectCallback) { state.web3Provider.disconnectCallback(); - this.emit('disconnect') + // this.emit('disconnect') + } + if (state.wallet !== null && state.web3Provider.closeCallback) { + state.web3Provider.closeCallback(); + // this.emit('close') } state.wallet = null; this.emit(DISCONNECTED); + this.emit('close') + this.emit('disconnect') } ); @@ -338,12 +345,23 @@ export default class Integration extends EventEmitter { () => { this.popUpHandler.showNotice(messageConstants.disconnect); MEWconnectWallet.setConnectionState(connection.lifeCycle.disconnected); + console.log('DISCONNECT NOTIFIER', state.web3Provider); // todo remove dev item if (state.wallet !== null && state.web3Provider.disconnectCallback) { + console.log('disconnectCallback', typeof state.web3Provider.closeCallback); // todo remove dev item state.web3Provider.disconnectCallback(); + state.web3Provider.emit('disconnect') this.emit('disconnect') } + if (state.wallet !== null && state.web3Provider.closeCallback) { + state.web3Provider.closeCallback(); + this.emit('close') + state.web3Provider.emit('close') + } + state.wallet = null; this.emit(connection.lifeCycle.disconnected); + this.emit('close') + this.emit('disconnect') } ); } diff --git a/src/connectProvider/web3Provider/web3-provider/providers/http-provider.js b/src/connectProvider/web3Provider/web3-provider/providers/http-provider.js index 41c1ad25..ab311650 100644 --- a/src/connectProvider/web3Provider/web3-provider/providers/http-provider.js +++ b/src/connectProvider/web3Provider/web3-provider/providers/http-provider.js @@ -48,6 +48,8 @@ class HttpProvider { }); }, notificationCallbacks: [], + disconnectCallbacks: [], + closeCallbacks: [], createSubscriptions: (subscription, ) => { requestManager.addSubscription() }, @@ -81,8 +83,37 @@ class HttpProvider { this.httpProvider.disconnectedCallback = callback; break; case 'disconnect': + this.httpProvider.disconnectCallbacks.push(callback) this.httpProvider.disconnectCallback = callback; break; + case 'close': + this.httpProvider.closeCallbacks.push(callback) + this.httpProvider.closeCallback = callback; + break; + } + }, + emit:(type, data) => { + if (typeof type !== 'string') + throw new Error('The first parameter type must be a function.'); + + switch (type) { + + // case 'accountsChanged': + // this.accountsChanged = callback; + // break; + // case 'disconnected': + // this.disconnectedCallback = callback; + // break; + case 'disconnect': + this.httpProvider.disconnectCallbacks.forEach(function(callback) { + if (typeof callback === 'function') callback(data); + }); + break; + case 'close': + this.httpProvider.closeCallbacks.forEach(function(callback) { + if (typeof callback === 'function') callback(data); + }); + break; } } }; diff --git a/src/connectProvider/web3Provider/web3-provider/providers/ws-web3-provider.js b/src/connectProvider/web3Provider/web3-provider/providers/ws-web3-provider.js index 5048c3c5..6aec74d0 100644 --- a/src/connectProvider/web3Provider/web3-provider/providers/ws-web3-provider.js +++ b/src/connectProvider/web3Provider/web3-provider/providers/ws-web3-provider.js @@ -19,6 +19,8 @@ const WebsocketProvider = function WebsocketProvider(url, options) { const _this = this; this.responseCallbacks = {}; this.notificationCallbacks = []; + this.closeCallbacks = []; + this.disconnectCallbacks = [] options = options || {}; this._customTimeout = options.timeout; @@ -191,6 +193,7 @@ WebsocketProvider.prototype.send = function(payload, callback) { WebsocketProvider.prototype.on = function(type, callback) { if (typeof callback !== 'function') throw new Error('The second parameter callback must be a function.'); + console.log('set callback'); // todo remove dev item switch (type) { case 'data': @@ -216,8 +219,39 @@ WebsocketProvider.prototype.on = function(type, callback) { this.disconnectedCallback = callback; break; case 'disconnect': + this.disconnectCallbacks.push(callback) this.disconnectCallback = callback; break; + case 'close': + this.closeCallbacks.push(callback) + console.log('set close callback'); // todo remove dev item + this.closeCallback = callback; + break; + } +}; + +WebsocketProvider.prototype.emit = function(type, data) { + if (typeof type !== 'string') + throw new Error('The first parameter type must be a function.'); + + switch (type) { + + // case 'accountsChanged': + // this.accountsChanged = callback; + // break; + // case 'disconnected': + // this.disconnectedCallback = callback; + // break; + case 'disconnect': + this.disconnectCallbacks.forEach(function(callback) { + if (_.isFunction(callback)) callback(data); + }); + break; + case 'close': + this.closeCallbacks.forEach(function(callback) { + if (_.isFunction(callback)) callback(data); + }); + break; } }; @@ -230,6 +264,22 @@ WebsocketProvider.prototype.removeListener = function(type, callback) { if (cb === callback) _this.notificationCallbacks.splice(index, 1); }); break; + // case 'accountsChanged': + // this.accountsChanged = callback; + // break; + // case 'disconnected': + // this.disconnectedCallback = callback; + // break; + case 'disconnect': + this.disconnectCallbacks.forEach(function(cb, index) { + if (cb === callback) _this.disconnectCallbacks.splice(index, 1); + }); + break; + case 'close': + this.closeCallbacks.forEach(function(cb, index) { + if (cb === callback) _this.closeCallbacks.splice(index, 1); + }); + break; } }; WebsocketProvider.prototype.removeAllListeners = function(type) { diff --git a/src/connectWindow/popUpCreator.js b/src/connectWindow/popUpCreator.js index f20358dd..f17e74c4 100644 --- a/src/connectWindow/popUpCreator.js +++ b/src/connectWindow/popUpCreator.js @@ -293,13 +293,26 @@ export default class PopUpCreator extends EventEmitter{ } closePopupWindow() { - // this.hideDialog(); - this.container.dispatchEvent(new Event('mewModalClosed')); - this.container.replaceChildren(); - this.popupWindowOpen = null; + try { // this.hideDialog(); + this.popupWindowOpen = null; + document.querySelector('#Attach-Mew-Wallet-Modal').dispatchEvent(new Event('mewModalClosed')); + document.querySelector('#Attach-Mew-Wallet-Modal').replaceChildren(); + + // document.querySelector('#Attach-Mew-Wallet-Modal').classList.add('hidden'); + } catch (e) { + this.popupWindowOpen = null; + document.querySelector('#Attach-Mew-Wallet-Modal').innerHTML = ''; + } } handleBeforeUnload() { // this.closePopupWindow(); } + resetSetup() { + this.popupWindowOpen = null; + this.hideNotifier(); + this.closePopupWindow(); + // document.getElementById('Attach-Mew-Wallet-Modal'); + + } } diff --git a/test/clients/receiverV1.js b/test/clients/receiverV1.js index 40f3d06b..44e2c936 100644 --- a/test/clients/receiverV1.js +++ b/test/clients/receiverV1.js @@ -3,7 +3,7 @@ import CryptoUtils from '../utils/crypto-utils'; const debugLogger = require('debug'); const io = require('socket.io-client'); const EventEmitter = require('events').EventEmitter; -const MewConnectCrypto = require('../../dist/index.js').Crypto; +const MewConnectCrypto = require('../../src/connectClient/MewConnectCrypto').default; const SimplePeer = require('simple-peer'); const wrtc = require('wrtc'); diff --git a/test/integration/basic.pairing.test.js b/test/integration/basic.pairing.spec.js similarity index 98% rename from test/integration/basic.pairing.test.js rename to test/integration/basic.pairing.spec.js index eeba4666..7e9bd1e8 100644 --- a/test/integration/basic.pairing.test.js +++ b/test/integration/basic.pairing.spec.js @@ -193,6 +193,12 @@ describe('Pairing', () => { // initiator.rtcSend({ type: 'address', data: '' }); }); + initiator.on('displayCode', (data) => { + console.log(data); // todo remove dev item + if (!silent) console.log('address', data); // todo remove dev item + // initiator.rtcSend({ type: 'address', data: '' }); + }); + await receiver.setKeys( initiator.publicKey, initiator.privateKey, diff --git a/tests/helpers/MewConnectReceiver.js b/tests/helpers/MewConnectReceiver.js index 6825b874..ed960788 100644 --- a/tests/helpers/MewConnectReceiver.js +++ b/tests/helpers/MewConnectReceiver.js @@ -1,7 +1,7 @@ const debugLogger = require('debug'); const io = require('socket.io-client'); const EventEmitter = require('events').EventEmitter; -const MewConnectCrypto = require('../../dist/index.js').Crypto; +const MewConnectCrypto = require('../../src/connectClient/MewConnectCrypto'); const SimplePeer = require('simple-peer'); const { @@ -66,7 +66,8 @@ export default class MewConnectReceiver extends EventEmitter { this.io = io; this.Peer = SimplePeer; - this.mewCrypto = options.cryptoImpl || MewConnectCrypto.create(); + console.log(MewConnectCrypto); // todo remove dev item + this.mewCrypto = options.cryptoImpl || MewConnectCrypto.default.create(); this.signals = this.jsonDetails.signals; this.rtcEvents = this.jsonDetails.rtc; diff --git a/tests/specs/MewConnectInitiator.spec.js b/tests/specs/MewConnectInitiator.spec.js index 8e38dc74..b6c92a66 100644 --- a/tests/specs/MewConnectInitiator.spec.js +++ b/tests/specs/MewConnectInitiator.spec.js @@ -1,13 +1,13 @@ import 'regenerator-runtime/runtime'; import { expect as chaiExpect } from 'chai'; import debug from 'debug'; -import * as MewConnectSrc from '../../src'; +import * as MewConnectSrc from '../../src/connectClient/index'; import MewConnectReceiver from '../helpers/MewConnectReceiver'; const connectLogger = debug('test:Connect'); const fallbackLogger = debug('test:Fallback'); -const signalUrl = typeof signalServer !== 'undefined' ? signalServer : 'https://connect.mewapi.io'; +const signalUrl = typeof signalServer !== 'undefined' ? signalServer : 'wss://connect2.mewapi.io/staging'; @@ -54,8 +54,9 @@ describe('Check Base Connection Operation', () => { const recSignals = []; const intSignals = []; const completed = []; + console.log(MewConnectSrc); // todo remove dev item const MewConnect = MewConnectSrc.default.Initiator; - const mewConnectClient = MewConnect.init(); + const mewConnectClient = new MewConnect(); mewConnectClient.initiatorStart(signalUrl);