@walletconnect/core
Advanced tools
Comparing version 1.0.0-beta.4 to 1.0.0-beta.5
@@ -80,2 +80,3 @@ import { ICryptoLib, ISessionStatus, ISessionError, IPartialRpcResponse, IPartialRpcRequest, ITxData, IClientMeta, IWalletConnectOptions } from '@walletconnect/types'; | ||
private _socketOpen; | ||
private _toggleSocketPing; | ||
private _socketSend; | ||
@@ -82,0 +83,0 @@ private _socketReceive; |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("@walletconnect/core",[],e):"object"==typeof exports?exports["@walletconnect/core"]=e():t["@walletconnect/core"]=e()}(window,function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var r=n(1),o=n(2);function i(t){return"result"in t}function s(t){return"error"in t}var u=null;"undefined"!=typeof window&&void 0!==window.localStorage&&(u=window.localStorage);var a=function(){function t(t,e,n,r){if(this.cryptoLib=t,this.protocol="wc",this.version=1,this._bridge="",this._key=null,this._nextKey=null,this._clientId="",this._clientMeta=null,this._peerId="",this._peerMeta=null,this._handshakeId=0,this._handshakeTopic="",this._accounts=[],this._chainId=0,this._socket=null,this._queue=[],this._eventEmitters=[],this._connected=!1,this._browser=n,this._pingInterval=null,r&&(this.clientMeta=r),!e.bridge&&!e.uri&&!e.session)throw new Error("Missing one of two required parameters: bridge / uri / session");e.bridge&&(this.bridge=e.bridge),e.uri&&(this.uri=e.uri,this._subscribeToSessionRequest());var o=e.session||this._getStorageSession();o&&(this.session=o,this._browser&&this._exchangeKey()),this.handshakeId&&this._subscribeToSessionResponse(this.handshakeId,"Session request rejected"),this._subscribeToInternalEvents(),this._socketOpen()}return Object.defineProperty(t.prototype,"bridge",{get:function(){return this._bridge},set:function(t){t&&(this._bridge=t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"key",{get:function(){return this._key?o.convertArrayBufferToHex(this._key):""},set:function(t){if(t){var e=o.convertHexToArrayBuffer(t);this._key=e}},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"nextKey",{get:function(){return this._nextKey?o.convertArrayBufferToHex(this._nextKey):""},set:function(t){if(t){var e=o.convertHexToArrayBuffer(t);this._nextKey=e}},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"clientId",{get:function(){var t=this._clientId;return t||(t=this._clientId=o.uuid()),this._clientId},set:function(t){t&&(this._clientId=t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"peerId",{get:function(){return this._peerId},set:function(t){t&&(this._peerId=t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"clientMeta",{get:function(){var t=this._clientMeta;return t||(t=this._clientMeta=o.getMeta()),t},set:function(t){},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"peerMeta",{get:function(){return this._peerMeta},set:function(t){this._peerMeta=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"handshakeTopic",{get:function(){return this._handshakeTopic},set:function(t){t&&(this._handshakeTopic=t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"handshakeId",{get:function(){return this._handshakeId},set:function(t){t&&(this._handshakeId=t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"uri",{get:function(){return this._formatUri()},set:function(t){if(t){var e=this._parseUri(t),n=e.handshakeTopic,r=e.bridge,o=e.key;this.handshakeTopic=n,this.bridge=r,this.key=o}},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"chainId",{get:function(){return this._chainId},set:function(t){this._chainId=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"accounts",{get:function(){return this._accounts},set:function(t){this._accounts=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"connected",{get:function(){return this._connected},set:function(t){},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"pending",{get:function(){return!!this._handshakeTopic},set:function(t){},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"session",{get:function(){return{connected:this.connected,accounts:this.accounts,chainId:this.chainId,bridge:this.bridge,key:this.key,clientId:this.clientId,clientMeta:this.clientMeta,peerId:this.peerId,peerMeta:this.peerMeta,handshakeId:this.handshakeId,handshakeTopic:this.handshakeTopic}},set:function(t){t&&(this._connected=t.connected,this.accounts=t.accounts,this.chainId=t.chainId,this.bridge=t.bridge,this.key=t.key,this.clientId=t.clientId,this.clientMeta=t.clientMeta,this.peerId=t.peerId,this.peerMeta=t.peerMeta,this.handshakeId=t.handshakeId,this.handshakeTopic=t.handshakeTopic)},enumerable:!0,configurable:!0}),t.prototype.on=function(t,e){var n={event:t,callback:e};this._eventEmitters.push(n)},t.prototype.createSession=function(){return r.__awaiter(this,void 0,void 0,function(){var t,e;return r.__generator(this,function(n){switch(n.label){case 0:if(this._connected)throw new Error("Session currently connected");return this.pending?[2]:(t=this,[4,this._generateKey()]);case 1:return t._key=n.sent(),e=this._formatRequest({method:"wc_sessionRequest",params:[{peerId:this.clientId,peerMeta:this.clientMeta}]}),this.handshakeId=e.id,this.handshakeTopic=o.uuid(),this._sendSessionRequest(e,"Session update rejected",this.handshakeTopic),this._setStorageSession(),[2]}})})},t.prototype.approveSession=function(t){if(this._connected)throw new Error("Session currently connected");this.chainId=t.chainId,this.accounts=t.accounts;var e={approved:!0,chainId:this.chainId,accounts:this.accounts},n={id:this.handshakeId,jsonrpc:"2.0",result:e};this._sendResponse(n),this._connected=!0,this._triggerEvents({event:"connect",params:[{peerId:this.peerId,peerMeta:this.peerMeta,chainId:this.chainId,accounts:this.accounts}]}),this._setStorageSession()},t.prototype.rejectSession=function(t){if(this._connected)throw new Error("Session currently connected");var e=t&&t.message?t.message:"Session Rejected",n=this._formatResponse({id:this.handshakeId,error:{message:e}});this._sendResponse(n),this._connected=!1,this._triggerEvents({event:"disconnect",params:[{message:e}]}),this._removeStorageSession()},t.prototype.updateSession=function(t){if(!this._connected)throw new Error("Session currently disconnected");this.chainId=t.chainId,this.accounts=t.accounts;var e={approved:!0,chainId:this.chainId,accounts:this.accounts},n=this._formatRequest({method:"wc_sessionUpdate",params:[e]});this._sendSessionRequest(n,"Session update rejected"),this._triggerEvents({event:"session_update",params:[{chainId:this.chainId,accounts:this.accounts}]}),this._setStorageSession()},t.prototype.killSession=function(t){var e=t?t.message:"Session Disconnected",n=this._formatRequest({method:"wc_sessionUpdate",params:[{approved:!1,chainId:null,accounts:null}]});this._sendSessionRequest(n,"Failed to kill Session"),this._handleSessionDisconnect(e)},t.prototype.sendTransaction=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:if(!this._connected)throw new Error("Session currently disconnected");e=this._formatRequest({method:"eth_sendTransaction",params:[t]}),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this._sendCallRequest(e)];case 2:return[2,n.sent()];case 3:throw n.sent();case 4:return[2]}})})},t.prototype.signMessage=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:if(!this._connected)throw new Error("Session currently disconnected");e=this._formatRequest({method:"eth_sign",params:t}),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this._sendCallRequest(e)];case 2:return[2,n.sent()];case 3:throw n.sent();case 4:return[2]}})})},t.prototype.signTypedData=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:if(!this._connected)throw new Error("Session currently disconnected");e=this._formatRequest({method:"eth_signTypedData",params:t}),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this._sendCallRequest(e)];case 2:return[2,n.sent()];case 3:throw n.sent();case 4:return[2]}})})},t.prototype.sendCustomRequest=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:if(!this._connected)throw new Error("Session currently disconnected");e=this._formatRequest(t),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this._sendCallRequest(e)];case 2:return[2,n.sent()];case 3:throw n.sent();case 4:return[2]}})})},t.prototype.approveRequest=function(t){if(!i(t))throw new Error('JSON-RPC success response must include "result" field');var e=this._formatResponse(t);this._sendResponse(e)},t.prototype.rejectRequest=function(t){if(!s(t))throw new Error('JSON-RPC error response must include "error" field');var e=this._formatResponse(t);this._sendResponse(e)},t.prototype._sendRequest=function(t,e){return r.__awaiter(this,void 0,void 0,function(){var n,o,i,s,u;return r.__generator(this,function(r){switch(r.label){case 0:return n=this._formatRequest(t),[4,this._encrypt(n)];case 1:return o=r.sent(),i=e||this.peerId,s=JSON.stringify(o),u={topic:i,type:"pub",payload:s},this._socket?this._socketSend(u):this._setToQueue(u),[2]}})})},t.prototype._sendResponse=function(t){return r.__awaiter(this,void 0,void 0,function(){var e,n,o,i;return r.__generator(this,function(r){switch(r.label){case 0:return[4,this._encrypt(t)];case 1:return e=r.sent(),n=this.peerId,o=JSON.stringify(e),i={topic:n,type:"pub",payload:o},this._socket?this._socketSend(i):this._setToQueue(i),[2]}})})},t.prototype._sendSessionRequest=function(t,e,n){return r.__awaiter(this,void 0,void 0,function(){return r.__generator(this,function(r){return this._sendRequest(t,n),this._subscribeToSessionResponse(t.id,e),[2]})})},t.prototype._sendCallRequest=function(t){return this._sendRequest(t),this._subscribeToCallResponse(t.id)},t.prototype._formatRequest=function(t){return r.__assign({id:o.payloadId(),jsonrpc:"2.0"},t)},t.prototype._formatResponse=function(t){if(s(t)){var e=o.formatRpcError(t.error);return r.__assign({jsonrpc:"2.0"},t,{error:e})}return r.__assign({jsonrpc:"2.0",result:null},t)},t.prototype._handleSessionDisconnect=function(t){var e=t||"Session Disconnected";this._connected=!1,this._triggerEvents({event:"disconnect",params:[{message:e}]}),console.error(e),this._removeStorageSession(),clearInterval(this._pingInterval)},t.prototype._handleSessionResponse=function(t,e){e&&e.approved?(this._connected?(e.chainId&&(this.chainId=e.chainId),e.accounts&&(this.accounts=e.accounts),this._triggerEvents({event:"session_update",params:[{chainId:this.chainId,accounts:this.accounts}]})):(this._connected=!0,e.chainId&&(this.chainId=e.chainId),e.accounts&&(this.accounts=e.accounts),this._triggerEvents({event:"connect",params:[{peerId:this.peerId,peerMeta:this.peerMeta,chainId:this.chainId,accounts:this.accounts}]})),this._setStorageSession()):this._handleSessionDisconnect(t)},t.prototype._subscribeToSessionRequest=function(){this._setToQueue({topic:""+this.handshakeTopic,type:"sub",payload:""})},t.prototype._subscribeToSessionResponse=function(t,e){var n=this;this.on("response:"+t,function(t,r){t&&n._handleSessionResponse(t.message),n._handleSessionResponse(e,r.result)})},t.prototype._subscribeToCallResponse=function(t){var e=this;return new Promise(function(n,r){e.on("response:"+t,function(t,e){t&&r(t),e.result?n(e.result):r(new Error("Invalid JSON RPC response format received"))})})},t.prototype._subscribeToInternalEvents=function(){var t=this;this.on("wc_sessionRequest",function(e,n){e&&console.error(e),t.handshakeId=n.id,t.peerId=n.params[0].peerId,t.peerMeta=n.params[0].peerMeta,t._exchangeKey();var o=r.__assign({},n,{method:"session_request"});t._triggerEvents(o)}),this.on("wc_sessionUpdate",function(e,n){e&&t._handleSessionResponse(e.message),t._handleSessionResponse("Session disconnected",n.params[0])}),this.on("wc_exchangeKey",function(e,n){e&&console.error(e),t._handleExchangeKeyRequest(n)})},t.prototype._triggerEvents=function(t){var e,n=[];(e="method"in t?t.method:i(t)||s(t)?"response:"+t.id:function(t){return"event"in t}(t)?t.event:"")&&(n=this._eventEmitters.filter(function(t){return t.event===e})),n&&n.length||(n=this._eventEmitters.filter(function(t){return"call_request"===t.event})),n.forEach(function(e){if(s(t)){var n=new Error(t.error.message);e.callback(n,null)}else e.callback(null,t)})},t.prototype._exchangeKey=function(){return r.__awaiter(this,void 0,void 0,function(){var t,e;return r.__generator(this,function(n){switch(n.label){case 0:return t=this,[4,this._generateKey()];case 1:t._nextKey=n.sent(),e=this._formatRequest({method:"wc_exchangeKey",params:[{peerId:this.clientId,peerMeta:this.clientMeta,nextKey:this.nextKey}]}),n.label=2;case 2:return n.trys.push([2,4,,5]),[4,this._sendCallRequest(e)];case 3:return n.sent(),this._swapKey(),[3,5];case 4:throw n.sent();case 5:return[2]}})})},t.prototype._handleExchangeKeyRequest=function(t){return r.__awaiter(this,void 0,void 0,function(){var e,n,o,i,s;return r.__generator(this,function(r){switch(r.label){case 0:return e=t.params[0],n=e.peerId,o=e.peerMeta,i=e.nextKey,this.peerId=n,this.peerMeta=o,this.nextKey=i,s={id:t.id,jsonrpc:"2.0",result:!0},[4,this._sendResponse(s)];case 1:return r.sent(),this._swapKey(),[2]}})})},t.prototype._swapKey=function(){this._key=this._nextKey,this._nextKey=null,this._setStorageSession()},t.prototype._socketOpen=function(){var t=this,e=this.bridge,n=e.startsWith("https")?e.replace("https","wss"):e.startsWith("http")?e.replace("http","ws"):e,r=new WebSocket(n);r.onmessage=function(e){return t._socketReceive(e)},r.onopen=function(){t._socket=r,t._setToQueue({topic:""+t.clientId,type:"sub",payload:""}),t._dispatchQueue(),t._pingInterval=setInterval(function(){r.send("ping")},500)}},t.prototype._socketSend=function(t){var e=this._socket;if(!e)throw new Error("Missing socket: required for sending message");var n=JSON.stringify(t);e.send(n)},t.prototype._socketReceive=function(t){return r.__awaiter(this,void 0,void 0,function(){var e,n,o;return r.__generator(this,function(r){switch(r.label){case 0:try{e=JSON.parse(t.data)}catch(t){throw t}if(![this.clientId,this.handshakeTopic].includes(e.topic))return[2];try{n=JSON.parse(e.payload)}catch(t){throw t}return[4,this._decrypt(n)];case 1:return(o=r.sent())&&this._triggerEvents(o),[2]}})})},t.prototype._setToQueue=function(t){this._queue.push(t)},t.prototype._dispatchQueue=function(){var t=this;this._queue.forEach(function(e){return t._socketSend(e)}),this._queue=[]},t.prototype._formatUri=function(){return this.protocol+":"+this.handshakeTopic+"@"+this.version+"?bridge="+encodeURIComponent(this.bridge)+"&key="+this.key},t.prototype._parseUri=function(t){var e=o.parseWalletConnectUri(t);if(e.protocol===this.protocol){if(!e.handshakeTopic)throw Error("Invalid or missing handshakeTopic parameter value");var n=e.handshakeTopic;if(!e.bridge)throw Error("Invalid or missing bridge url parameter value");var r=decodeURIComponent(e.bridge);if(!e.key)throw Error("Invalid or missing kkey parameter value");return{handshakeTopic:n,bridge:r,key:e.key}}throw new Error("URI format doesn't follow Connector protocol")},t.prototype._generateKey=function(){return r.__awaiter(this,void 0,void 0,function(){return r.__generator(this,function(t){switch(t.label){case 0:return this.cryptoLib?[4,this.cryptoLib.generateKey()]:[3,2];case 1:return[2,t.sent()];case 2:return[2,null]}})})},t.prototype._encrypt=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:return e=this._key,this.cryptoLib&&e?[4,this.cryptoLib.encrypt(t,e)]:[3,2];case 1:return[2,n.sent()];case 2:return[2,null]}})})},t.prototype._decrypt=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:return e=this._key,this.cryptoLib&&e?[4,this.cryptoLib.decrypt(t,e)]:[3,2];case 1:return[2,n.sent()];case 2:return[2,null]}})})},t.prototype._getStorageSession=function(){var t=null,e=null;if(u&&(e=u.getItem("walletconnect")),e&&"string"==typeof e)try{var n=JSON.parse(e);"bridge"in n&&(t=n)}catch(t){throw t}return t},t.prototype._setStorageSession=function(){var t=this.session,e=JSON.stringify(t);return u&&u.setItem("walletconnect",e),t},t.prototype._removeStorageSession=function(){u&&u.removeItem("walletconnect")},t}();e.default=a},function(t,e,n){"use strict";n.r(e),n.d(e,"__extends",function(){return o}),n.d(e,"__assign",function(){return i}),n.d(e,"__rest",function(){return s}),n.d(e,"__decorate",function(){return u}),n.d(e,"__param",function(){return a}),n.d(e,"__metadata",function(){return c}),n.d(e,"__awaiter",function(){return f}),n.d(e,"__generator",function(){return h}),n.d(e,"__exportStar",function(){return l}),n.d(e,"__values",function(){return p}),n.d(e,"__read",function(){return d}),n.d(e,"__spread",function(){return y}),n.d(e,"__await",function(){return _}),n.d(e,"__asyncGenerator",function(){return g}),n.d(e,"__asyncDelegator",function(){return v}),n.d(e,"__asyncValues",function(){return w}),n.d(e,"__makeTemplateObject",function(){return b}),n.d(e,"__importStar",function(){return m}),n.d(e,"__importDefault",function(){return S}); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("@walletconnect/core",[],e):"object"==typeof exports?exports["@walletconnect/core"]=e():t["@walletconnect/core"]=e()}(window,function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var r=n(1),o=n(2);function i(t){return"result"in t}function s(t){return"error"in t}var u=null;"undefined"!=typeof window&&void 0!==window.localStorage&&(u=window.localStorage);var a=function(){function t(t,e,n,r){if(this.cryptoLib=t,this.protocol="wc",this.version=1,this._bridge="",this._key=null,this._nextKey=null,this._clientId="",this._clientMeta=null,this._peerId="",this._peerMeta=null,this._handshakeId=0,this._handshakeTopic="",this._accounts=[],this._chainId=0,this._socket=null,this._queue=[],this._eventEmitters=[],this._connected=!1,this._browser=n,this._pingInterval=null,r&&(this.clientMeta=r),!e.bridge&&!e.uri&&!e.session)throw new Error("Missing one of two required parameters: bridge / uri / session");e.bridge&&(this.bridge=e.bridge),e.uri&&(this.uri=e.uri,this._subscribeToSessionRequest());var o=e.session||this._getStorageSession();o&&(this.session=o,this._browser&&this._exchangeKey()),this.handshakeId&&this._subscribeToSessionResponse(this.handshakeId,"Session request rejected"),this._subscribeToInternalEvents(),this._socketOpen()}return Object.defineProperty(t.prototype,"bridge",{get:function(){return this._bridge},set:function(t){t&&(this._bridge=t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"key",{get:function(){return this._key?o.convertArrayBufferToHex(this._key):""},set:function(t){if(t){var e=o.convertHexToArrayBuffer(t);this._key=e}},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"nextKey",{get:function(){return this._nextKey?o.convertArrayBufferToHex(this._nextKey):""},set:function(t){if(t){var e=o.convertHexToArrayBuffer(t);this._nextKey=e}},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"clientId",{get:function(){var t=this._clientId;return t||(t=this._clientId=o.uuid()),this._clientId},set:function(t){t&&(this._clientId=t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"peerId",{get:function(){return this._peerId},set:function(t){t&&(this._peerId=t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"clientMeta",{get:function(){var t=this._clientMeta;return t||(t=this._clientMeta=o.getMeta()),t},set:function(t){},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"peerMeta",{get:function(){return this._peerMeta},set:function(t){this._peerMeta=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"handshakeTopic",{get:function(){return this._handshakeTopic},set:function(t){t&&(this._handshakeTopic=t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"handshakeId",{get:function(){return this._handshakeId},set:function(t){t&&(this._handshakeId=t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"uri",{get:function(){return this._formatUri()},set:function(t){if(t){var e=this._parseUri(t),n=e.handshakeTopic,r=e.bridge,o=e.key;this.handshakeTopic=n,this.bridge=r,this.key=o}},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"chainId",{get:function(){return this._chainId},set:function(t){this._chainId=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"accounts",{get:function(){return this._accounts},set:function(t){this._accounts=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"connected",{get:function(){return this._connected},set:function(t){},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"pending",{get:function(){return!!this._handshakeTopic},set:function(t){},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"session",{get:function(){return{connected:this.connected,accounts:this.accounts,chainId:this.chainId,bridge:this.bridge,key:this.key,clientId:this.clientId,clientMeta:this.clientMeta,peerId:this.peerId,peerMeta:this.peerMeta,handshakeId:this.handshakeId,handshakeTopic:this.handshakeTopic}},set:function(t){t&&(this._connected=t.connected,this.accounts=t.accounts,this.chainId=t.chainId,this.bridge=t.bridge,this.key=t.key,this.clientId=t.clientId,this.clientMeta=t.clientMeta,this.peerId=t.peerId,this.peerMeta=t.peerMeta,this.handshakeId=t.handshakeId,this.handshakeTopic=t.handshakeTopic)},enumerable:!0,configurable:!0}),t.prototype.on=function(t,e){var n={event:t,callback:e};this._eventEmitters.push(n)},t.prototype.createSession=function(){return r.__awaiter(this,void 0,void 0,function(){var t,e;return r.__generator(this,function(n){switch(n.label){case 0:if(this._connected)throw new Error("Session currently connected");return this.pending?[2]:(t=this,[4,this._generateKey()]);case 1:return t._key=n.sent(),e=this._formatRequest({method:"wc_sessionRequest",params:[{peerId:this.clientId,peerMeta:this.clientMeta}]}),this.handshakeId=e.id,this.handshakeTopic=o.uuid(),this._sendSessionRequest(e,"Session update rejected",this.handshakeTopic),this._setStorageSession(),[2]}})})},t.prototype.approveSession=function(t){if(this._connected)throw new Error("Session currently connected");this.chainId=t.chainId,this.accounts=t.accounts;var e={approved:!0,chainId:this.chainId,accounts:this.accounts},n={id:this.handshakeId,jsonrpc:"2.0",result:e};this._sendResponse(n),this._connected=!0,this._triggerEvents({event:"connect",params:[{peerId:this.peerId,peerMeta:this.peerMeta,chainId:this.chainId,accounts:this.accounts}]}),this._setStorageSession()},t.prototype.rejectSession=function(t){if(this._connected)throw new Error("Session currently connected");var e=t&&t.message?t.message:"Session Rejected",n=this._formatResponse({id:this.handshakeId,error:{message:e}});this._sendResponse(n),this._connected=!1,this._triggerEvents({event:"disconnect",params:[{message:e}]}),this._removeStorageSession()},t.prototype.updateSession=function(t){if(!this._connected)throw new Error("Session currently disconnected");this.chainId=t.chainId,this.accounts=t.accounts;var e={approved:!0,chainId:this.chainId,accounts:this.accounts},n=this._formatRequest({method:"wc_sessionUpdate",params:[e]});this._sendSessionRequest(n,"Session update rejected"),this._triggerEvents({event:"session_update",params:[{chainId:this.chainId,accounts:this.accounts}]}),this._setStorageSession()},t.prototype.killSession=function(t){var e=t?t.message:"Session Disconnected",n=this._formatRequest({method:"wc_sessionUpdate",params:[{approved:!1,chainId:null,accounts:null}]});this._sendSessionRequest(n,"Failed to kill Session"),this._handleSessionDisconnect(e)},t.prototype.sendTransaction=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:if(!this._connected)throw new Error("Session currently disconnected");e=this._formatRequest({method:"eth_sendTransaction",params:[t]}),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this._sendCallRequest(e)];case 2:return[2,n.sent()];case 3:throw n.sent();case 4:return[2]}})})},t.prototype.signMessage=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:if(!this._connected)throw new Error("Session currently disconnected");e=this._formatRequest({method:"eth_sign",params:t}),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this._sendCallRequest(e)];case 2:return[2,n.sent()];case 3:throw n.sent();case 4:return[2]}})})},t.prototype.signTypedData=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:if(!this._connected)throw new Error("Session currently disconnected");e=this._formatRequest({method:"eth_signTypedData",params:t}),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this._sendCallRequest(e)];case 2:return[2,n.sent()];case 3:throw n.sent();case 4:return[2]}})})},t.prototype.sendCustomRequest=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:if(!this._connected)throw new Error("Session currently disconnected");e=this._formatRequest(t),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this._sendCallRequest(e)];case 2:return[2,n.sent()];case 3:throw n.sent();case 4:return[2]}})})},t.prototype.approveRequest=function(t){if(!i(t))throw new Error('JSON-RPC success response must include "result" field');var e=this._formatResponse(t);this._sendResponse(e)},t.prototype.rejectRequest=function(t){if(!s(t))throw new Error('JSON-RPC error response must include "error" field');var e=this._formatResponse(t);this._sendResponse(e)},t.prototype._sendRequest=function(t,e){return r.__awaiter(this,void 0,void 0,function(){var n,o,i,s,u;return r.__generator(this,function(r){switch(r.label){case 0:return n=this._formatRequest(t),[4,this._encrypt(n)];case 1:return o=r.sent(),i=e||this.peerId,s=JSON.stringify(o),u={topic:i,type:"pub",payload:s},this._socket&&1===this._socket.readyState?this._socketSend(u):this._setToQueue(u),[2]}})})},t.prototype._sendResponse=function(t){return r.__awaiter(this,void 0,void 0,function(){var e,n,o,i;return r.__generator(this,function(r){switch(r.label){case 0:return[4,this._encrypt(t)];case 1:return e=r.sent(),n=this.peerId,o=JSON.stringify(e),i={topic:n,type:"pub",payload:o},this._socket&&1===this._socket.readyState?this._socketSend(i):this._setToQueue(i),[2]}})})},t.prototype._sendSessionRequest=function(t,e,n){return r.__awaiter(this,void 0,void 0,function(){return r.__generator(this,function(r){return this._sendRequest(t,n),this._subscribeToSessionResponse(t.id,e),[2]})})},t.prototype._sendCallRequest=function(t){return this._sendRequest(t),this._subscribeToCallResponse(t.id)},t.prototype._formatRequest=function(t){return r.__assign({id:o.payloadId(),jsonrpc:"2.0"},t)},t.prototype._formatResponse=function(t){if(s(t)){var e=o.formatRpcError(t.error);return r.__assign({jsonrpc:"2.0"},t,{error:e})}return r.__assign({jsonrpc:"2.0",result:null},t)},t.prototype._handleSessionDisconnect=function(t){var e=t||"Session Disconnected";this._connected&&(this._connected=!1,this._triggerEvents({event:"disconnect",params:[{message:e}]}),console.error(e)),this._removeStorageSession(),this._toggleSocketPing()},t.prototype._handleSessionResponse=function(t,e){e&&e.approved?(this._connected?(e.chainId&&(this.chainId=e.chainId),e.accounts&&(this.accounts=e.accounts),this._triggerEvents({event:"session_update",params:[{chainId:this.chainId,accounts:this.accounts}]})):(this._connected=!0,e.chainId&&(this.chainId=e.chainId),e.accounts&&(this.accounts=e.accounts),this._triggerEvents({event:"connect",params:[{peerId:this.peerId,peerMeta:this.peerMeta,chainId:this.chainId,accounts:this.accounts}]})),this._setStorageSession()):this._handleSessionDisconnect(t)},t.prototype._subscribeToSessionRequest=function(){this._setToQueue({topic:""+this.handshakeTopic,type:"sub",payload:""})},t.prototype._subscribeToSessionResponse=function(t,e){var n=this;this.on("response:"+t,function(t,r){t&&n._handleSessionResponse(t.message),n._handleSessionResponse(e,r.result)})},t.prototype._subscribeToCallResponse=function(t){var e=this;return new Promise(function(n,r){e.on("response:"+t,function(t,e){t&&r(t),e.result?n(e.result):e.error&&e.error.message?r(new Error(e.error.message)):r(new Error("Invalid JSON RPC response format received"))})})},t.prototype._subscribeToInternalEvents=function(){var t=this;this.on("wc_sessionRequest",function(e,n){e&&console.error(e),t.handshakeId=n.id,t.peerId=n.params[0].peerId,t.peerMeta=n.params[0].peerMeta,t._exchangeKey();var o=r.__assign({},n,{method:"session_request"});t._triggerEvents(o)}),this.on("wc_sessionUpdate",function(e,n){e&&t._handleSessionResponse(e.message),t._handleSessionResponse("Session disconnected",n.params[0])}),this.on("wc_exchangeKey",function(e,n){e&&console.error(e),t._handleExchangeKeyRequest(n)})},t.prototype._triggerEvents=function(t){var e,n=[];(e="method"in t?t.method:i(t)||s(t)?"response:"+t.id:function(t){return"event"in t}(t)?t.event:"")&&(n=this._eventEmitters.filter(function(t){return t.event===e})),n&&n.length||(n=this._eventEmitters.filter(function(t){return"call_request"===t.event})),n.forEach(function(e){if(s(t)){var n=new Error(t.error.message);e.callback(n,null)}else e.callback(null,t)})},t.prototype._exchangeKey=function(){return r.__awaiter(this,void 0,void 0,function(){var t,e;return r.__generator(this,function(n){switch(n.label){case 0:return t=this,[4,this._generateKey()];case 1:t._nextKey=n.sent(),e=this._formatRequest({method:"wc_exchangeKey",params:[{peerId:this.clientId,peerMeta:this.clientMeta,nextKey:this.nextKey}]}),n.label=2;case 2:return n.trys.push([2,4,,5]),[4,this._sendCallRequest(e)];case 3:return n.sent(),this._swapKey(),[3,5];case 4:throw n.sent();case 5:return[2]}})})},t.prototype._handleExchangeKeyRequest=function(t){return r.__awaiter(this,void 0,void 0,function(){var e,n,o,i,s;return r.__generator(this,function(r){switch(r.label){case 0:return e=t.params[0],n=e.peerId,o=e.peerMeta,i=e.nextKey,this.peerId=n,this.peerMeta=o,this.nextKey=i,s={id:t.id,jsonrpc:"2.0",result:!0},[4,this._sendResponse(s)];case 1:return r.sent(),this._swapKey(),[2]}})})},t.prototype._swapKey=function(){this._key=this._nextKey,this._nextKey=null,this._setStorageSession()},t.prototype._socketOpen=function(){var t=this,e=this.bridge,n=e.startsWith("https")?e.replace("https","wss"):e.startsWith("http")?e.replace("http","ws"):e,r=new WebSocket(n);r.onmessage=function(e){return t._socketReceive(e)},r.onopen=function(){t._socket=r,t._setToQueue({topic:""+t.clientId,type:"sub",payload:""}),t._dispatchQueue(),t._toggleSocketPing()}},t.prototype._toggleSocketPing=function(){var t=this;this._socket&&1===this._socket.readyState?this._pingInterval=setInterval(function(){t._socket&&t._socket.send("ping")},1e4):clearInterval(this._pingInterval)},t.prototype._socketSend=function(t){if(!this._socket)throw new Error("Missing socket: required for sending message");var e=JSON.stringify(t);this._socket&&1===this._socket.readyState?this._socket.send(e):this._connected&&(this._setToQueue(t),this._socketOpen())},t.prototype._socketReceive=function(t){return r.__awaiter(this,void 0,void 0,function(){var e,n,o;return r.__generator(this,function(r){switch(r.label){case 0:try{e=JSON.parse(t.data)}catch(t){throw t}if(![this.clientId,this.handshakeTopic].includes(e.topic))return[2];try{n=JSON.parse(e.payload)}catch(t){throw t}return[4,this._decrypt(n)];case 1:return(o=r.sent())&&this._triggerEvents(o),[2]}})})},t.prototype._setToQueue=function(t){this._queue.push(t)},t.prototype._dispatchQueue=function(){var t=this;this._queue.forEach(function(e){return t._socketSend(e)}),this._queue=[]},t.prototype._formatUri=function(){return this.protocol+":"+this.handshakeTopic+"@"+this.version+"?bridge="+encodeURIComponent(this.bridge)+"&key="+this.key},t.prototype._parseUri=function(t){var e=o.parseWalletConnectUri(t);if(e.protocol===this.protocol){if(!e.handshakeTopic)throw Error("Invalid or missing handshakeTopic parameter value");var n=e.handshakeTopic;if(!e.bridge)throw Error("Invalid or missing bridge url parameter value");var r=decodeURIComponent(e.bridge);if(!e.key)throw Error("Invalid or missing kkey parameter value");return{handshakeTopic:n,bridge:r,key:e.key}}throw new Error("URI format doesn't follow Connector protocol")},t.prototype._generateKey=function(){return r.__awaiter(this,void 0,void 0,function(){return r.__generator(this,function(t){switch(t.label){case 0:return this.cryptoLib?[4,this.cryptoLib.generateKey()]:[3,2];case 1:return[2,t.sent()];case 2:return[2,null]}})})},t.prototype._encrypt=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:return e=this._key,this.cryptoLib&&e?[4,this.cryptoLib.encrypt(t,e)]:[3,2];case 1:return[2,n.sent()];case 2:return[2,null]}})})},t.prototype._decrypt=function(t){return r.__awaiter(this,void 0,void 0,function(){var e;return r.__generator(this,function(n){switch(n.label){case 0:return e=this._key,this.cryptoLib&&e?[4,this.cryptoLib.decrypt(t,e)]:[3,2];case 1:return[2,n.sent()];case 2:return[2,null]}})})},t.prototype._getStorageSession=function(){var t=null,e=null;if(u&&(e=u.getItem("walletconnect")),e&&"string"==typeof e)try{var n=JSON.parse(e);"bridge"in n&&(t=n)}catch(t){throw t}return t},t.prototype._setStorageSession=function(){var t=this.session,e=JSON.stringify(t);return u&&u.setItem("walletconnect",e),t},t.prototype._removeStorageSession=function(){u&&u.removeItem("walletconnect")},t}();e.default=a},function(t,e,n){"use strict";n.r(e),n.d(e,"__extends",function(){return o}),n.d(e,"__assign",function(){return i}),n.d(e,"__rest",function(){return s}),n.d(e,"__decorate",function(){return u}),n.d(e,"__param",function(){return a}),n.d(e,"__metadata",function(){return c}),n.d(e,"__awaiter",function(){return f}),n.d(e,"__generator",function(){return h}),n.d(e,"__exportStar",function(){return l}),n.d(e,"__values",function(){return p}),n.d(e,"__read",function(){return d}),n.d(e,"__spread",function(){return y}),n.d(e,"__await",function(){return _}),n.d(e,"__asyncGenerator",function(){return g}),n.d(e,"__asyncDelegator",function(){return v}),n.d(e,"__asyncValues",function(){return w}),n.d(e,"__makeTemplateObject",function(){return b}),n.d(e,"__importStar",function(){return m}),n.d(e,"__importDefault",function(){return S}); | ||
/*! ***************************************************************************** | ||
@@ -3,0 +3,0 @@ Copyright (c) Microsoft Corporation. All rights reserved. |
{ | ||
"name": "@walletconnect/core", | ||
"version": "1.0.0-beta.4", | ||
"version": "1.0.0-beta.5", | ||
"description": "Core Library for WalletConnect", | ||
@@ -56,6 +56,6 @@ "scripts": { | ||
"dependencies": { | ||
"@walletconnect/types": "^1.0.0-beta.4", | ||
"@walletconnect/utils": "^1.0.0-beta.4" | ||
"@walletconnect/types": "^1.0.0-beta.5", | ||
"@walletconnect/utils": "^1.0.0-beta.5" | ||
}, | ||
"gitHead": "165f7993c2acc907c653c02847fb02721052c6e7" | ||
} |
@@ -617,3 +617,3 @@ import { | ||
if (this._socket) { | ||
if (this._socket && this._socket.readyState === 1) { | ||
this._socketSend(socketMessage) | ||
@@ -641,3 +641,3 @@ } else { | ||
if (this._socket) { | ||
if (this._socket && this._socket.readyState === 1) { | ||
this._socketSend(socketMessage) | ||
@@ -697,10 +697,12 @@ } else { | ||
const message = errorMsg || 'Session Disconnected' | ||
this._connected = false | ||
this._triggerEvents({ | ||
event: 'disconnect', | ||
params: [{ message }] | ||
}) | ||
console.error(message) // tslint:disable-line | ||
if (this._connected) { | ||
this._connected = false | ||
this._triggerEvents({ | ||
event: 'disconnect', | ||
params: [{ message }] | ||
}) | ||
console.error(message) // tslint:disable-line | ||
} | ||
this._removeStorageSession() | ||
clearInterval(this._pingInterval) | ||
this._toggleSocketPing() | ||
} | ||
@@ -787,2 +789,4 @@ | ||
resolve(payload.result) | ||
} else if (payload.error && payload.error.message) { | ||
reject(new Error(payload.error.message)) | ||
} else { | ||
@@ -939,6 +943,18 @@ reject(new Error('Invalid JSON RPC response format received')) | ||
this._dispatchQueue() | ||
this._toggleSocketPing() | ||
} | ||
} | ||
this._pingInterval = setInterval(() => { | ||
socket.send('ping') | ||
}, 500) | ||
private _toggleSocketPing () { | ||
if (this._socket && this._socket.readyState === 1) { | ||
this._pingInterval = setInterval( | ||
() => { | ||
if (this._socket) { | ||
this._socket.send('ping') | ||
} | ||
}, | ||
10000 // 10 seconds | ||
) | ||
} else { | ||
clearInterval(this._pingInterval) | ||
} | ||
@@ -948,10 +964,16 @@ } | ||
private _socketSend (socketMessage: ISocketMessage) { | ||
const socket: WebSocket | null = this._socket | ||
if (!socket) { | ||
if (!this._socket) { | ||
throw new Error('Missing socket: required for sending message') | ||
} | ||
const message: string = JSON.stringify(socketMessage) | ||
socket.send(message) | ||
if (this._socket && this._socket.readyState === 1) { | ||
this._socket.send(message) | ||
} else { | ||
if (this._connected) { | ||
this._setToQueue(socketMessage) | ||
this._socketOpen() | ||
} | ||
} | ||
} | ||
@@ -958,0 +980,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
235839
1293