@scatterjs/core
Advanced tools
Comparing version 2.7.27 to 2.7.28
@@ -1,1 +0,1 @@ | ||
"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard"),_interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"SocketService",{enumerable:!0,get:function get(){return _SocketService["default"]}}),Object.defineProperty(exports,"Plugin",{enumerable:!0,get:function get(){return _Plugin["default"]}}),Object.defineProperty(exports,"Blockchains",{enumerable:!0,get:function get(){return _Blockchains.Blockchains}}),Object.defineProperty(exports,"Network",{enumerable:!0,get:function get(){return _Network["default"]}}),Object.defineProperty(exports,"WalletInterface",{enumerable:!0,get:function get(){return _WalletInterface["default"]}}),Object.defineProperty(exports,"WALLET_METHODS",{enumerable:!0,get:function get(){return _WalletInterface.WALLET_METHODS}}),exports.PluginTypes=exports["default"]=exports.EVENTS=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator")),_asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")),_classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")),_createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass")),_PluginRepository=_interopRequireDefault(require("./plugins/PluginRepository")),_SocketService=_interopRequireDefault(require("./services/SocketService")),_Plugin=_interopRequireDefault(require("./plugins/Plugin")),PluginTypes=_interopRequireWildcard(require("./plugins/PluginTypes"));exports.PluginTypes=PluginTypes;var origin,_Blockchains=require("./models/Blockchains"),_Network=_interopRequireDefault(require("./models/Network")),_WalletInterface=_interopRequireWildcard(require("./models/WalletInterface")),_Desktop=_interopRequireDefault(require("./wallets/Desktop")),_Extension=_interopRequireDefault(require("./wallets/Extension")),_Token=_interopRequireDefault(require("./models/Token")),EVENTS={Disconnected:"dced",LoggedOut:"logout"};exports.EVENTS=EVENTS;var socketService=_SocketService["default"],socketSetters=[],holderFns={},Index=/*#__PURE__*/function(){function a(){(0,_classCallCheck2["default"])(this,a),this.identity=null,this.network=null,_PluginRepository["default"].loadPlugin(new _Desktop["default"](this,holderFns)),_PluginRepository["default"].loadPlugin(new _Extension["default"](this,holderFns))}return(0,_createClass2["default"])(a,[{key:"loadPlugin",value:function loadPlugin(a){if(!a.isValid())throw new Error("".concat(a.name," doesn't seem to be a valid ScatterJS plugin."));_PluginRepository["default"].loadPlugin(a),a.type===PluginTypes.BLOCKCHAIN_SUPPORT&&(this[a.name]=a.signatureProvider(function noIdFunc(){if(!holderFns.get().identity)throw new Error("No Identity")},function(){return holderFns.get().identity}),this[a.name+"Hook"]=a.hookProvider,socketSetters.push(a.setSocketService)),a.type===PluginTypes.WALLET_SUPPORT&&a.init(this,holderFns,socketSetters)}},{key:"connect",value:function(){var a=(0,_asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function a(b,c){var d;return _regenerator["default"].wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return c||(c={}),this.network=c.hasOwnProperty("network")?c.network:null,d=_PluginRepository["default"].wallets(),a.next=5,Promise.race(d.map(function(a){return a.connect(b,c).then(/*#__PURE__*/(0,_asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function b(){return _regenerator["default"].wrap(function(b){for(;;)switch(b.prev=b.next){case 0:if("function"!=typeof a.runBeforeInterfacing){b.next=3;break}return b.next=3,a.runBeforeInterfacing();case 3:if(new _WalletInterface["default"](a.name,a.methods(),holderFns.get()),"function"!=typeof a.runAfterInterfacing){b.next=7;break}return b.next=7,a.runAfterInterfacing();case 7:return _WalletInterface["default"].bindBasics(holderFns.get()),b.abrupt("return",!0);case 9:case"end":return b.stop();}},b)})))}).concat(new Promise(function(a){return setTimeout(function(){return a(!1)},c.initTimeout||5e3)})));case 5:return a.abrupt("return",a.sent);case 6:case"end":return a.stop();}},a,this)}));return function connect(){return a.apply(this,arguments)}}()}]),a}(),Holder=/*#__PURE__*/function(){function a(b){(0,_classCallCheck2["default"])(this,a),this.scatter=b}return(0,_createClass2["default"])(a,[{key:"plugins",value:function plugins(){var a=this;if(!this.scatter.isExtension){for(var b=arguments.length,c=Array(b),d=0;d<b;d++)c[d]=arguments[d];c.map(function(b){return a.scatter.loadPlugin(b)})}}},{key:"connect",value:function connect(){var a;return(a=this.scatter).connect.apply(a,arguments)}},{key:"catchAll",value:function catchAll(){}}]),a}(),holder=new Proxy(new Holder(new Index()),{get:function get(a,b){return"undefined"==typeof a[b]?a.scatter[b]:a[b]}});holderFns.set=function(a){return holder.scatter=a},holderFns.get=function(){return holder.scatter},"undefined"!=typeof window&&(window.ScatterJS=holder),holder.Plugin=_Plugin["default"],holder.PluginTypes=PluginTypes,holder.Blockchains=_Blockchains.Blockchains,holder.Network=_Network["default"],holder.Token=_Token["default"],holder.SocketService=_SocketService["default"],holder.EVENTS=EVENTS,holder.WalletInterface=_WalletInterface["default"],holder.WALLET_METHODS=_WalletInterface.WALLET_METHODS;var _default=holder;exports["default"]=_default; | ||
"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard"),_interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"SocketService",{enumerable:!0,get:function get(){return _SocketService["default"]}}),Object.defineProperty(exports,"Plugin",{enumerable:!0,get:function get(){return _Plugin["default"]}}),Object.defineProperty(exports,"Blockchains",{enumerable:!0,get:function get(){return _Blockchains.Blockchains}}),Object.defineProperty(exports,"Network",{enumerable:!0,get:function get(){return _Network["default"]}}),Object.defineProperty(exports,"WalletInterface",{enumerable:!0,get:function get(){return _WalletInterface["default"]}}),Object.defineProperty(exports,"WALLET_METHODS",{enumerable:!0,get:function get(){return _WalletInterface.WALLET_METHODS}}),exports.PluginTypes=exports["default"]=exports.EVENTS=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator")),_asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")),_classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")),_createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass")),_PluginRepository=_interopRequireDefault(require("./plugins/PluginRepository")),_SocketService=_interopRequireDefault(require("./services/SocketService")),_Plugin=_interopRequireDefault(require("./plugins/Plugin")),PluginTypes=_interopRequireWildcard(require("./plugins/PluginTypes"));exports.PluginTypes=PluginTypes;var origin,_Blockchains=require("./models/Blockchains"),_Network=_interopRequireDefault(require("./models/Network")),_WalletInterface=_interopRequireWildcard(require("./models/WalletInterface")),_LocalSocket=_interopRequireDefault(require("./wallets/LocalSocket")),_RelaySocket=_interopRequireDefault(require("./wallets/RelaySocket")),_Injection=_interopRequireDefault(require("./wallets/Injection")),_Token=_interopRequireDefault(require("./models/Token")),EVENTS={Disconnected:"dced",LoggedOut:"logout"};exports.EVENTS=EVENTS;var socketSetters=[],holderFns={},Index=/*#__PURE__*/function(){function a(){(0,_classCallCheck2["default"])(this,a),this.identity=null,this.network=null,_PluginRepository["default"].loadPlugin(new _RelaySocket["default"](this,holderFns)),_PluginRepository["default"].loadPlugin(new _LocalSocket["default"](this,holderFns)),_PluginRepository["default"].loadPlugin(new _Injection["default"](this,holderFns))}return(0,_createClass2["default"])(a,[{key:"loadPlugin",value:function loadPlugin(a){if(!a.isValid())throw new Error("".concat(a.name," doesn't seem to be a valid ScatterJS plugin."));_PluginRepository["default"].loadPlugin(a),a.type===PluginTypes.BLOCKCHAIN_SUPPORT&&(this[a.name]=a.signatureProvider(function noIdFunc(){if(!holderFns.get().identity)throw new Error("No Identity")},function(){return holderFns.get().identity}),this[a.name+"Hook"]=a.hookProvider,socketSetters.push(a.setSocketService)),a.type===PluginTypes.WALLET_SUPPORT&&a.init(this,holderFns,socketSetters)}},{key:"connect",value:function(){var a=(0,_asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function a(b,c){var d;return _regenerator["default"].wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return c||(c={}),this.network=c.hasOwnProperty("network")?c.network:null,d=_PluginRepository["default"].wallets(),a.next=5,Promise.race(d.map(function(a){return a.connect(b,c).then(/*#__PURE__*/function(){var b=(0,_asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function b(c){return _regenerator["default"].wrap(function(b){for(;;)switch(b.prev=b.next){case 0:if(c&&socketSetters.map(function(a){return a(c)}),"function"!=typeof a.runBeforeInterfacing){b.next=4;break}return b.next=4,a.runBeforeInterfacing();case 4:if(new _WalletInterface["default"](a.name,a.methods(),holderFns.get()),"function"!=typeof a.runAfterInterfacing){b.next=8;break}return b.next=8,a.runAfterInterfacing();case 8:return _WalletInterface["default"].bindBasics(holderFns.get()),b.abrupt("return",!0);case 10:case"end":return b.stop();}},b)}));return function(){return b.apply(this,arguments)}}())}).concat(new Promise(function(a){return setTimeout(function(){return a(!1)},c.initTimeout||5e3)})));case 5:return a.abrupt("return",a.sent);case 6:case"end":return a.stop();}},a,this)}));return function connect(){return a.apply(this,arguments)}}()}]),a}(),Holder=/*#__PURE__*/function(){function a(b){(0,_classCallCheck2["default"])(this,a),this.scatter=b}return(0,_createClass2["default"])(a,[{key:"plugins",value:function plugins(){var a=this;if(!this.scatter.isExtension){for(var b=arguments.length,c=Array(b),d=0;d<b;d++)c[d]=arguments[d];c.map(function(b){return a.scatter.loadPlugin(b)})}}},{key:"connect",value:function connect(){var a;return(a=this.scatter).connect.apply(a,arguments)}},{key:"catchAll",value:function catchAll(){}}]),a}(),holder=new Proxy(new Holder(new Index()),{get:function get(a,b){return"undefined"==typeof a[b]?a.scatter[b]:a[b]}});holderFns.set=function(a){return holder.scatter=a},holderFns.get=function(){return holder.scatter},"undefined"!=typeof window&&(window.ScatterJS=holder),holder.Plugin=_Plugin["default"],holder.PluginTypes=PluginTypes,holder.Blockchains=_Blockchains.Blockchains,holder.Network=_Network["default"],holder.Token=_Token["default"],holder.SocketService=_SocketService["default"],holder.EVENTS=EVENTS,holder.WalletInterface=_WalletInterface["default"],holder.WALLET_METHODS=_WalletInterface.WALLET_METHODS;var _default=holder;exports["default"]=_default; |
@@ -1,6 +0,6 @@ | ||
"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),exports["default"]=void 0;var plugin,_regenerator=_interopRequireDefault(require("@babel/runtime/regenerator")),_toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")),_typeof2=_interopRequireDefault(require("@babel/runtime/helpers/typeof")),_slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")),_asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")),_classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")),_createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass")),_StorageService=_interopRequireDefault(require("./StorageService")),_getRandomValues=_interopRequireDefault(require("get-random-values")),_createHash=_interopRequireDefault(require("create-hash")),_isomorphicWs=_interopRequireDefault(require("isomorphic-ws")),suffix="/socket.io/?EIO=3&transport=websocket",socket=null,connected=!1,paired=!1,openRequests=[],sha256=function(a){return(0,_createHash["default"])("sha256").update(a).digest("hex")},random=function(){var a=new Uint8Array(24);return(0,_getRandomValues["default"])(a),a.join("")},_getOrigin=function getOrigin(){var a;return a="undefined"==typeof location?plugin:location.hasOwnProperty("hostname")&&location.hostname.length&&"localhost"!==location.hostname?location.hostname:plugin,"www."===a.substr(0,4)&&(a=a.replace("www.","")),a},appkey=_StorageService["default"].getAppKey();appkey||(appkey="appkey:"+random());var send=function(){var a=0<arguments.length&&arguments[0]!==void 0?arguments[0]:null,b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:null;null===a&&null===b?socket.send("40/scatter"):socket.send("42/scatter,"+JSON.stringify([a,b]))},pairingPromise=null,pair=function(){var a=!!(0<arguments.length&&arguments[0]!==void 0)&&arguments[0];return new Promise(function(b,c){pairingPromise={resolve:b,reject:c},send("pair",{data:{appkey:appkey,origin:_getOrigin(),passthrough:a},plugin:plugin})})},eventHandlers={},SocketService=/*#__PURE__*/function(){function a(){(0,_classCallCheck2["default"])(this,a)}return(0,_createClass2["default"])(a,null,[{key:"init",value:function init(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:6e4;plugin=a,this.timeout=b}},{key:"getOrigin",value:function getOrigin(){return _getOrigin()}},{key:"addEventHandler",value:function addEventHandler(a,b){b||(b="app"),eventHandlers[b]=a}},{key:"removeEventHandler",value:function removeEventHandler(a){a||(a="app"),delete eventHandlers[a]}},{key:"link",value:function link(){var a=this;return Promise.race([new Promise(function(b){return setTimeout(function(){connected||(b(!1),socket&&(socket.close(),socket=null))},a.timeout)}),new Promise(/*#__PURE__*/function(){var a=(0,_asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function a(b){var c,d,e,f,g,h;return _regenerator["default"].wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return c=function(){socket.onmessage=function(e){// Handshaking/Upgrading | ||
if(-1===e.data.indexOf("42/scatter"))return!1;// Real message | ||
var f=JSON.parse(e.data.replace("42/scatter,","")),g=(0,_slicedToArray2["default"])(f,2),h=g[0],i=g[1];return"paired"===h?a(i):"rekey"===h?b():"api"===h?c(i):"event"===h?d(i):void 0};var a=function(a){if(paired=a,paired){var b=_StorageService["default"].getAppKey(),c=-1<appkey.indexOf("appkey:")?sha256(appkey):appkey;b&&b===c||(_StorageService["default"].setAppKey(c),appkey=_StorageService["default"].getAppKey())}pairingPromise.resolve(a)},b=function(){appkey="appkey:"+random(),send("rekeyed",{data:{appkey:appkey,origin:_getOrigin()},plugin:plugin})},c=function(a){var b=openRequests.find(function(b){return b.id===a.id});if(b){openRequests=openRequests.filter(function(b){return b.id!==a.id});var c="object"===(0,_typeof2["default"])(a.result)&&null!==a.result&&a.result.hasOwnProperty("isError");c?b.reject(a.result):b.resolve(a.result)}},d=function(a){var b=a.event,c=a.payload;Object.keys(eventHandlers).length&&Object.keys(eventHandlers).map(function(a){eventHandlers[a](b,c)})}},d=function(a,b){return b?"local.get-scatter.com:".concat(a):"127.0.0.1:".concat(a)},a.next=4,(0,_asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function a(){var b,c,e,f;return _regenerator["default"].wrap(function(a){for(;;)switch(a.prev=a.next){case 0:b=function(a,b){return fetch(a).then(function(a){return a.text()}).then(function(a){return b("scatter"===a)})["catch"](function(){return b(!1)})},c=50005,e=[],(0,_toConsumableArray2["default"])([,,,,,].keys()).map(function(a){var f=c+1500*a;return Promise.all([b("https://"+d(f+1,!0),function(a){return a?e.push(f+1):null}),b("http://"+d(f,!1),function(a){return a?e.push(f):null})])}),f=0;case 5:if(!(50>f)){a.next=13;break}if(!e.length){a.next=8;break}return a.abrupt("break",13);case 8:return a.next=10,new Promise(function(a){return setTimeout(function(){return a(!0)},2)});case 10:f++,a.next=5;break;case 13:return a.abrupt("return",e.length?e.sort(function(c,a){// Always try to use SSL first. | ||
return a%2?c%2?0:-1:1}):/* BACKWARDS COMPAT */[50006,50005]);case 14:case"end":return a.stop();}},a)}))();case 4:e=a.sent,f=function(a){var b,c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null;c||(b=new Promise(function(a){return c=a}));var d=!(a%2),e=d?"local.get-scatter.com:".concat(a):"127.0.0.1:".concat(a),f=d?"wss://":"ws://",g="".concat(f).concat(e).concat(suffix),h=new _isomorphicWs["default"](g);return h.onerror=function(){return c(!1)},h.onopen=function(){return c(h)},b},g=0;case 7:if(!(g<e.length)){a.next=21;break}return a.next=10,f(e[g]);case 10:if(h=a.sent,!h){a.next=18;break}return socket=h,send(),connected=!0,pair(!0).then(function(){return b(!0)}),c(),a.abrupt("break",21);case 18:g++,a.next=7;break;case 21:case"end":return a.stop();}},a)}));return function(){return a.apply(this,arguments)}}())])}},{key:"isConnected",value:function isConnected(){return connected}},{key:"isPaired",value:function isPaired(){return paired}},{key:"disconnect",value:function disconnect(){return console.log("disconnect"),socket&&socket.close(),!0}},{key:"sendApiRequest",value:function sendApiRequest(a){return new Promise(function(b,c){return"identityFromPermissions"!==a.type||paired?void pair().then(function(){if(!paired)return c({code:"not_paired",message:"The user did not allow this app to connect to their Scatter"});// Request ID used for resolving promises | ||
a.id=random(),a.appkey=appkey,a.nonce=_StorageService["default"].getNonce()||0;// Next nonce used to authenticate the next request | ||
var d=random();a.nextNonce=sha256(d),_StorageService["default"].setNonce(d),a.hasOwnProperty("payload")&&!a.payload.hasOwnProperty("origin")&&(a.payload.origin=_getOrigin()),openRequests.push(Object.assign(a,{resolve:b,reject:c})),send("api",{data:a,plugin:plugin})}):b(!1)})}}]),a}();exports["default"]=SocketService; | ||
"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),exports["default"]=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator")),_toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")),_typeof2=_interopRequireDefault(require("@babel/runtime/helpers/typeof")),_slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")),_asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")),_classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")),_createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass")),_StorageService=_interopRequireDefault(require("./StorageService")),_getRandomValues=_interopRequireDefault(require("get-random-values")),_createHash=_interopRequireDefault(require("create-hash")),_isomorphicWs=_interopRequireDefault(require("isomorphic-ws")),_Device=_interopRequireDefault(require("../util/Device")),suffix="/socket.io/?EIO=3&transport=websocket",sha256=function(a){return(0,_createHash["default"])("sha256").update(a).digest("hex")},random=function(){var a=new Uint8Array(24);return(0,_getRandomValues["default"])(a),a.join("")},SocketService=/*#__PURE__*/function(){function a(b,c){(0,_classCallCheck2["default"])(this,a),this.plugin=b,this.timeout=c,this.uuid=null,this.socket=null,this.connected=!1,this.paired=!1,this.openRequests=[],this.pairingPromise=null,this.eventHandlers={},this.appkey=_StorageService["default"].getAppKey(),this.appkey||(this.appkey="appkey:"+random())}return(0,_createClass2["default"])(a,[{key:"getOrigin",value:function getOrigin(){return this.getOrigin()}},{key:"addEventHandler",value:function addEventHandler(a,b){b||(b="app"),this.eventHandlers[b]=a}},{key:"removeEventHandler",value:function removeEventHandler(a){a||(a="app"),delete this.eventHandlers[a]}},{key:"link",value:function link(){var a=this,b=0<arguments.length&&void 0!==arguments[0]?arguments[0]:null,c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null;return this.uuid=b,Promise.race([new Promise(function(b){return setTimeout(function(){a.connected||(b(!1),a.socket&&(a.socket.close(),a.socket=null))},a.timeout)}),new Promise(/*#__PURE__*/function(){var b=(0,_asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function b(d){var e,f,g,h,j,k;return _regenerator["default"].wrap(function(b){for(;;)switch(b.prev=b.next){case 0:return e=function(){a.socket.onmessage=function(a){// Handshaking/Upgrading | ||
if(-1===a.data.indexOf("42/scatter"))return!1;// Real message | ||
var f=JSON.parse(a.data.replace("42/scatter,","")),g=(0,_slicedToArray2["default"])(f,2),h=g[0],i=g[1];return"paired"===h?b(i):"rekey"===h?c():"api"===h?d(i):"event"===h?e(i):void 0};var b=function(b){if(a.paired=b,a.paired){var c=_StorageService["default"].getAppKey(),d=-1<a.appkey.indexOf("appkey:")?sha256(a.appkey):a.appkey;c&&c===d||(_StorageService["default"].setAppKey(d),a.appkey=_StorageService["default"].getAppKey())}a.pairingPromise.resolve(b)},c=function(){a.appkey="appkey:"+random(),a.send("rekeyed",{data:{appkey:a.appkey,origin:a.getOrigin()},plugin:a.plugin})},d=function(b){var c=a.openRequests.find(function(a){return a.id===b.id});if(c){a.openRequests=a.openRequests.filter(function(a){return a.id!==b.id});var d="object"===(0,_typeof2["default"])(b.result)&&null!==b.result&&b.result.hasOwnProperty("isError");d?c.reject(b.result):c.resolve(b.result)}},e=function(b){var c=b.event,d=b.payload;Object.keys(a.eventHandlers).length&&Object.keys(a.eventHandlers).map(function(b){a.eventHandlers[b](c,d)})}},f=function(a,b){return c?c:b?"local.get-scatter.com:".concat(a):"127.0.0.1:".concat(a)},b.next=4,(0,_asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function a(){var b,d,e,g;return _regenerator["default"].wrap(function(a){for(;;)switch(a.prev=a.next){case 0:if(!c){a.next=2;break}return a.abrupt("return",[50005]);case 2:b=function(a,b){return fetch(a).then(function(a){return a.text()}).then(function(a){return b("scatter"===a)})["catch"](function(){return b(!1)})},d=50005,e=[],(0,_toConsumableArray2["default"])([,,,,,].keys()).map(function(a){var c=d+1500*a;return Promise.all([b("https://"+f(c+1,!0),function(a){return a?e.push(c+1):null}),b("http://"+f(c,!1),function(a){return a?e.push(c):null})])}),g=0;case 7:if(!(50>g)){a.next=15;break}if(!e.length){a.next=10;break}return a.abrupt("break",15);case 10:return a.next=12,new Promise(function(a){return setTimeout(function(){return a(!0)},2)});case 12:g++,a.next=7;break;case 15:return a.abrupt("return",e.length?e.sort(function(c,a){// Always try to use SSL first. | ||
return a%2?c%2?0:-1:1}):/* BACKWARDS COMPAT */[50006,50005]);case 16:case"end":return a.stop();}},a)}))();case 4:g=b.sent,h=function(a){var b,c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null;c||(b=new Promise(function(a){return c=a}));var d=!(a%2),e=f(a,d),g=d?"wss://":"ws://",h="".concat(g).concat(e).concat(suffix),i=new _isomorphicWs["default"](h);return i.onerror=function(){return c(!1)},i.onopen=function(){return c(i)},b},j=0;case 7:if(!(j<g.length)){b.next=21;break}return b.next=10,h(g[j]);case 10:if(k=b.sent,!k){b.next=18;break}return a.socket=k,a.send(),a.connected=!0,a.pair(!0).then(function(){return d(!0)}),e(),b.abrupt("break",21);case 18:j++,b.next=7;break;case 21:case"end":return b.stop();}},b)}));return function(){return b.apply(this,arguments)}}())])}},{key:"isConnected",value:function isConnected(){return this.connected}},{key:"isPaired",value:function isPaired(){return this.paired}},{key:"disconnect",value:function disconnect(){return console.log("disconnect"),this.socket&&this.socket.close(),!0}},{key:"sendApiRequest",value:function sendApiRequest(a){var b=this;return new Promise(function(c,d){return"identityFromPermissions"!==a.type||b.paired?void b.pair().then(function(){if(!b.paired)return d({code:"not_paired",message:"The user did not allow this app to connect to their Scatter"});// Request ID used for resolving promises | ||
a.id=random(),a.appkey=b.appkey,a.nonce=_StorageService["default"].getNonce()||0;// Next nonce used to authenticate the next request | ||
var e=random();a.nextNonce=sha256(e),_StorageService["default"].setNonce(e),a.hasOwnProperty("payload")&&!a.payload.hasOwnProperty("origin")&&(a.payload.origin=b.getOrigin()),b.openRequests.push(Object.assign(a,{resolve:c,reject:d})),b.send("api",{data:a,plugin:b.plugin})}):c(!1)})}},{key:"pair",value:function pair(){var a=this,b=!!(0<arguments.length&&void 0!==arguments[0])&&arguments[0];return new Promise(function(c,d){a.pairingPromise={resolve:c,reject:d},a.send("pair",{data:{appkey:a.appkey,origin:a.getOrigin(),passthrough:b},plugin:a.plugin})})}},{key:"send",value:function send(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:null,b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null;null===a&&null===b?this.socket.send("40/scatter"):this.socket.send("42/scatter,"+JSON.stringify([a,Object.assign(b,{device:_Device["default"],uuid:this.uuid})]))}},{key:"getOrigin",value:function getOrigin(){return a.getOriginOrPlugin(this.plugin)}}],[{key:"getOriginOrPlugin",value:function getOriginOrPlugin(a){var b;return b="undefined"==typeof location?a:location.hasOwnProperty("hostname")&&location.hostname.length&&"localhost"!==location.hostname?location.hostname:a,"www."===b.substr(0,4)&&(b=b.replace("www.","")),b}}]),a}();exports["default"]=SocketService; |
{ | ||
"name": "@scatterjs/core", | ||
"version": "2.7.27", | ||
"version": "2.7.28", | ||
"main": "dist/index.js", | ||
@@ -8,2 +8,3 @@ "license": "MIT", | ||
"create-hash": "^1.2.0", | ||
"device-uuid": "^1.0.4", | ||
"es6-promise": "^4.2.4", | ||
@@ -14,3 +15,3 @@ "get-random-values": "^1.2.0", | ||
}, | ||
"gitHead": "53ba6562891c4ae7e580195c15f95c12077b069a", | ||
"gitHead": "d01d3974f836a879801485de010761ad0f5c0c2e", | ||
"publishConfig": { | ||
@@ -17,0 +18,0 @@ "access": "public" |
@@ -8,4 +8,5 @@ import PluginRepository from './plugins/PluginRepository'; | ||
import WalletInterface, {WALLET_METHODS} from './models/WalletInterface'; | ||
import Desktop from "./wallets/Desktop"; | ||
import Extension from "./wallets/Extension"; | ||
import LocalSocket from "./wallets/LocalSocket"; | ||
import RelaySocket from "./wallets/RelaySocket"; | ||
import Injection from "./wallets/Injection"; | ||
import Token from "./models/Token"; | ||
@@ -20,3 +21,2 @@ | ||
let socketService = SocketService; | ||
let socketSetters = []; | ||
@@ -30,4 +30,5 @@ let holderFns = {}; | ||
PluginRepository.loadPlugin(new Desktop(this, holderFns)); | ||
PluginRepository.loadPlugin(new Extension(this, holderFns)); | ||
PluginRepository.loadPlugin(new RelaySocket(this, holderFns)); | ||
PluginRepository.loadPlugin(new LocalSocket(this, holderFns)); | ||
PluginRepository.loadPlugin(new Injection(this, holderFns)); | ||
} | ||
@@ -58,3 +59,4 @@ | ||
return await Promise.race(wallets.map(wallet => { | ||
return wallet.connect(pluginName, options).then(async () => { | ||
return wallet.connect(pluginName, options).then(async socketService => { | ||
if(socketService) socketSetters.map(x => x(socketService)); | ||
if(typeof wallet.runBeforeInterfacing === 'function') await wallet.runBeforeInterfacing(); | ||
@@ -61,0 +63,0 @@ new WalletInterface(wallet.name, wallet.methods(), holderFns.get()); |
@@ -5,2 +5,3 @@ import StorageService from './StorageService' | ||
import WebSocket from 'isomorphic-ws'; | ||
import device from "../util/Device"; | ||
@@ -10,10 +11,2 @@ const suffix = '/socket.io/?EIO=3&transport=websocket'; | ||
let socket = null; | ||
let connected = false; | ||
let paired = false; | ||
let plugin; | ||
let openRequests = []; | ||
const sha256 = data => createHash('sha256').update(data).digest('hex'); | ||
@@ -27,62 +20,45 @@ | ||
const getOrigin = () => { | ||
let origin; | ||
if(typeof location !== 'undefined') | ||
if(location.hasOwnProperty('hostname') && location.hostname.length && location.hostname !== 'localhost') | ||
origin = location.hostname; | ||
else origin = plugin; | ||
else origin = plugin; | ||
if(origin.substr(0, 4) === 'www.') origin = origin.replace('www.',''); | ||
return origin; | ||
} | ||
export default class SocketService { | ||
let appkey = StorageService.getAppKey(); | ||
if(!appkey) appkey = 'appkey:'+random(); | ||
constructor(_plugin, _timeout){ | ||
this.plugin = _plugin; | ||
this.timeout = _timeout; | ||
const send = (type = null, data = null) => { | ||
if(type === null && data === null) socket.send('40/scatter'); | ||
else socket.send('42/scatter,' + JSON.stringify([type, data])); | ||
} | ||
this.uuid = null; | ||
this.socket = null; | ||
this.connected = false; | ||
this.paired = false; | ||
this.openRequests = []; | ||
this.pairingPromise = null; | ||
this.eventHandlers = {}; | ||
let pairingPromise = null; | ||
const pair = (passthrough = false) => { | ||
return new Promise((resolve, reject) => { | ||
pairingPromise = {resolve, reject}; | ||
send('pair', {data:{ appkey, origin:getOrigin(), passthrough }, plugin}) | ||
}) | ||
}; | ||
let eventHandlers = {}; | ||
export default class SocketService { | ||
static init(_plugin, timeout = 60000){ | ||
plugin = _plugin; | ||
this.timeout = timeout; | ||
this.appkey = StorageService.getAppKey(); | ||
if(!this.appkey) this.appkey = 'appkey:'+random(); | ||
} | ||
static getOrigin(){ | ||
return getOrigin(); | ||
getOrigin(){ | ||
return this.getOrigin(); | ||
} | ||
static addEventHandler(handler, key){ | ||
addEventHandler(handler, key){ | ||
if(!key) key = 'app'; | ||
eventHandlers[key] = handler; | ||
this.eventHandlers[key] = handler; | ||
} | ||
static removeEventHandler(key){ | ||
removeEventHandler(key){ | ||
if(!key) key = 'app'; | ||
delete eventHandlers[key]; | ||
delete this.eventHandlers[key]; | ||
} | ||
static link(){ | ||
link(_uuid = null, socketHost = null){ | ||
this.uuid = _uuid; | ||
return Promise.race([ | ||
new Promise((resolve, reject) => setTimeout(() => { | ||
if(connected) return; | ||
if(this.connected) return; | ||
resolve(false); | ||
if(socket) { | ||
socket.close(); | ||
socket = null; | ||
if(this.socket) { | ||
this.socket.close(); | ||
this.socket = null; | ||
} | ||
@@ -93,3 +69,3 @@ }, this.timeout)), | ||
const setupSocket = () => { | ||
socket.onmessage = msg => { | ||
this.socket.onmessage = msg => { | ||
// Handshaking/Upgrading | ||
@@ -112,27 +88,27 @@ if(msg.data.indexOf('42/scatter') === -1) return false; | ||
const msg_paired = result => { | ||
paired = result; | ||
this.paired = result; | ||
if(paired) { | ||
if(this.paired) { | ||
const savedKey = StorageService.getAppKey(); | ||
const hashed = appkey.indexOf('appkey:') > -1 ? sha256(appkey) : appkey; | ||
const hashed = this.appkey.indexOf('appkey:') > -1 ? sha256(this.appkey) : this.appkey; | ||
if (!savedKey || savedKey !== hashed) { | ||
StorageService.setAppKey(hashed); | ||
appkey = StorageService.getAppKey(); | ||
this.appkey = StorageService.getAppKey(); | ||
} | ||
} | ||
pairingPromise.resolve(result); | ||
this.pairingPromise.resolve(result); | ||
}; | ||
const msg_rekey = () => { | ||
appkey = 'appkey:'+random(); | ||
send('rekeyed', {data:{ appkey, origin:getOrigin() }, plugin}); | ||
this.appkey = 'appkey:'+random(); | ||
this.send('rekeyed', {data:{ appkey:this.appkey, origin:this.getOrigin() }, plugin:this.plugin}); | ||
}; | ||
const msg_api = response => { | ||
const openRequest = openRequests.find(x => x.id === response.id); | ||
const openRequest = this.openRequests.find(x => x.id === response.id); | ||
if(!openRequest) return; | ||
openRequests = openRequests.filter(x => x.id !== response.id); | ||
this.openRequests = this.openRequests.filter(x => x.id !== response.id); | ||
@@ -148,4 +124,4 @@ const isErrorResponse = typeof response.result === 'object' | ||
const event_api = ({event, payload}) => { | ||
if(Object.keys(eventHandlers).length) Object.keys(eventHandlers).map(key => { | ||
eventHandlers[key](event, payload); | ||
if(Object.keys(this.eventHandlers).length) Object.keys(this.eventHandlers).map(key => { | ||
this.eventHandlers[key](event, payload); | ||
}); | ||
@@ -155,5 +131,10 @@ }; | ||
const getHostname = (port, ssl) => ssl ? `local.get-scatter.com:${port}` : `127.0.0.1:${port}`; | ||
const getHostname = (port, ssl) => { | ||
if(socketHost) return socketHost; | ||
return ssl ? `local.get-scatter.com:${port}` : `127.0.0.1:${port}`; | ||
} | ||
const ports = await (async () => { | ||
if(socketHost) return [50005]; | ||
const checkPort = (host, cb) => fetch(host).then(r => r.text()).then(r => cb(r === 'scatter')).catch(() => cb(false)); | ||
@@ -189,3 +170,3 @@ | ||
const ssl = !(port % 2); | ||
const hostname = ssl ? `local.get-scatter.com:${port}` : `127.0.0.1:${port}`; | ||
const hostname = getHostname(port, ssl); | ||
const protocol = ssl ? 'wss://' : 'ws://'; | ||
@@ -204,6 +185,6 @@ const host = `${protocol}${hostname}${suffix}`; | ||
if(s){ | ||
socket = s; | ||
send(); | ||
connected = true; | ||
pair(true).then(() => resolve(true)); | ||
this.socket = s; | ||
this.send(); | ||
this.connected = true; | ||
this.pair(true).then(() => resolve(true)); | ||
setupSocket(); | ||
@@ -218,22 +199,22 @@ break; | ||
static isConnected(){ | ||
return connected; | ||
isConnected(){ | ||
return this.connected; | ||
} | ||
static isPaired(){ | ||
return paired; | ||
isPaired(){ | ||
return this.paired; | ||
} | ||
static disconnect(){ | ||
disconnect(){ | ||
console.log('disconnect') | ||
if(socket) socket.close(); | ||
if(this.socket) this.socket.close(); | ||
return true; | ||
} | ||
static sendApiRequest(request){ | ||
sendApiRequest(request){ | ||
return new Promise((resolve, reject) => { | ||
if(request.type === 'identityFromPermissions' && !paired) return resolve(false); | ||
if(request.type === 'identityFromPermissions' && !this.paired) return resolve(false); | ||
pair().then(() => { | ||
if(!paired) return reject({code:'not_paired', message:'The user did not allow this app to connect to their Scatter'}); | ||
this.pair().then(() => { | ||
if(!this.paired) return reject({code:'not_paired', message:'The user did not allow this app to connect to their Scatter'}); | ||
@@ -244,3 +225,3 @@ // Request ID used for resolving promises | ||
// Set Application Key | ||
request.appkey = appkey; | ||
request.appkey = this.appkey; | ||
@@ -255,7 +236,7 @@ // Nonce used to authenticate this request | ||
if(request.hasOwnProperty('payload') && !request.payload.hasOwnProperty('origin')) | ||
request.payload.origin = getOrigin(); | ||
request.payload.origin = this.getOrigin(); | ||
openRequests.push(Object.assign(request, {resolve, reject})); | ||
send('api', {data:request, plugin}) | ||
this.openRequests.push(Object.assign(request, {resolve, reject})); | ||
this.send('api', {data:request, plugin:this.plugin}) | ||
}) | ||
@@ -265,2 +246,41 @@ }); | ||
pair(passthrough = false){ | ||
return new Promise((resolve, reject) => { | ||
this.pairingPromise = {resolve, reject}; | ||
this.send('pair', {data:{ appkey:this.appkey, origin:this.getOrigin(), passthrough }, plugin:this.plugin}) | ||
}) | ||
} | ||
send(type = null, data = null){ | ||
if(type === null && data === null) this.socket.send('40/scatter'); | ||
else this.socket.send('42/scatter,' + JSON.stringify([type, Object.assign(data, {device, uuid:this.uuid})])); | ||
} | ||
getOrigin(){ | ||
return SocketService.getOriginOrPlugin(this.plugin); | ||
} | ||
static getOriginOrPlugin(plugin){ | ||
let origin; | ||
if(typeof location !== 'undefined') | ||
if(location.hasOwnProperty('hostname') && location.hostname.length && location.hostname !== 'localhost') | ||
origin = location.hostname; | ||
else origin = plugin; | ||
else origin = plugin; | ||
if(origin.substr(0, 4) === 'www.') origin = origin.replace('www.',''); | ||
return origin; | ||
} | ||
} |
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 2 instances 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
97406
40
1013
6
16
4
+ Addeddevice-uuid@^1.0.4
+ Addeddevice-uuid@1.0.4(transitive)