Created
March 17, 2023 16:45
-
-
Save bradleySuira/6964927a260aa0d4dbb39fb44b8b1da0 to your computer and use it in GitHub Desktop.
Liquality SDK sample using Vanilla js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <title>Liquality SDK</title> | |
| </head> | |
| <body> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { AuthService, tryRegisterSW } from "@liquality/wallet-sdk"; | |
| async function callToSdk() { | |
| await tryRegisterSW("/serviceworker/sw.js"); | |
| const directParams = { | |
| baseUrl: `http://localhost:3005/serviceworker`, | |
| enableLogging: true, | |
| networkUrl: "https://goerli.infura.io/v3/a8684b771e9e4997a567bbd7189e0b27", | |
| network: "testnet" | |
| }; | |
| const tKey = await AuthService.init(directParams); | |
| const verifierMap = { | |
| google: { | |
| name: "Google", | |
| typeOfLogin: "google", | |
| clientId: | |
| "852640103435-0qhvrgpkm66c9hu0co6edkhao3hrjlv3.apps.googleusercontent.com", | |
| verifier: "liquality-google-testnet", | |
| } | |
| }; | |
| const wallet = await AuthService.createWallet(tKey, verifierMap); | |
| console.log('wallet', wallet) | |
| let response = await AuthService.generateNewShareWithPassword( | |
| wallet.tKey, | |
| '12345678_1Add' | |
| ); | |
| console.log('response', response); | |
| } | |
| const btn = document.createElement('button'); | |
| btn.innerHTML = 'Start usign Liquality Auth SDK!'; | |
| btn.onclick = callToSdk; | |
| document.body.appendChild(btn); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "scripts": { | |
| "build": "webpack", | |
| "watch": "webpack --watch", | |
| "start": "webpack serve" | |
| }, | |
| "dependencies": { | |
| "@liquality/wallet-sdk": "^2.2.0" | |
| }, | |
| "devDependencies": { | |
| "@babel/core": "^7.20.12", | |
| "@babel/preset-env": "^7.20.2", | |
| "assert": "^2.0.0", | |
| "babel-loader": "^9.1.2", | |
| "buffer": "^6.0.3", | |
| "crypto-browserify": "^3.12.0", | |
| "html-webpack-plugin": "^5.5.0", | |
| "https-browserify": "^1.0.0", | |
| "os-browserify": "^0.3.0", | |
| "process": "^0.11.10", | |
| "stream-browserify": "^3.0.0", | |
| "stream-http": "^3.2.0", | |
| "url": "^0.11.0", | |
| "webpack": "^5.75.0", | |
| "webpack-cli": "^5.0.1", | |
| "webpack-dev-server": "^4.11.1" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* eslint-disable */ | |
| function getScope() { | |
| return self.registration.scope; | |
| } | |
| self.addEventListener("message", function (event) { | |
| if (event.data && event.data.type === "SKIP_WAITING") { | |
| self.skipWaiting(); | |
| } | |
| }); | |
| self.addEventListener("fetch", function (event) { | |
| try { | |
| const url = new URL(event.request.url); | |
| if (url.pathname.includes("redirect") && url.href.includes(getScope())) { | |
| event.respondWith( | |
| new Response( | |
| new Blob( | |
| [ | |
| ` | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
| <meta name="viewport" content="width=device-width,initial-scale=1.0" /> | |
| <title>Redirect</title> | |
| <style> | |
| * { | |
| box-sizing: border-box; | |
| } | |
| html, | |
| body { | |
| background: #fcfcfc; | |
| height: 100%; | |
| padding: 0; | |
| margin: 0; | |
| } | |
| .container { | |
| width: 100%; | |
| height: 100%; | |
| display: flex; | |
| justify-content: center; | |
| align-items: center; | |
| flex-direction: column; | |
| } | |
| h1.title { | |
| font-size: 14px; | |
| color: #0f1222; | |
| font-family: "Roboto", sans-serif !important; | |
| margin: 0; | |
| text-align: center; | |
| } | |
| .spinner .beat { | |
| background-color: #0364ff; | |
| height: 12px; | |
| width: 12px; | |
| margin: 24px 2px 10px; | |
| border-radius: 100%; | |
| -webkit-animation: beatStretchDelay 0.7s infinite linear; | |
| animation: beatStretchDelay 0.7s infinite linear; | |
| -webkit-animation-fill-mode: both; | |
| animation-fill-mode: both; | |
| display: inline-block; | |
| } | |
| .spinner .beat-odd { | |
| animation-delay: 0s; | |
| } | |
| .spinner .beat-even { | |
| animation-delay: 0.35s; | |
| } | |
| @-webkit-keyframes beatStretchDelay { | |
| 50% { | |
| -webkit-transform: scale(0.75); | |
| transform: scale(0.75); | |
| -webkit-opacity: 0.2; | |
| opacity: 0.2; | |
| } | |
| 100% { | |
| -webkit-transform: scale(1); | |
| transform: scale(1); | |
| -webkit-opacity: 1; | |
| opacity: 1; | |
| } | |
| } | |
| @keyframes beatStretchDelay { | |
| 50% { | |
| -webkit-transform: scale(0.75); | |
| transform: scale(0.75); | |
| -webkit-opacity: 0.2; | |
| opacity: 0.2; | |
| } | |
| 100% { | |
| -webkit-transform: scale(1); | |
| transform: scale(1); | |
| -webkit-opacity: 1; | |
| opacity: 1; | |
| } | |
| } | |
| @media (min-width: 768px) { | |
| h1.title { | |
| font-size: 14px; | |
| } | |
| p.info { | |
| font-size: 28px; | |
| } | |
| .spinner .beat { | |
| height: 12px; | |
| width: 12px; | |
| } | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="message" class="container"> | |
| <div class="spinner content" id="spinner"> | |
| <div class="beat beat-odd"></div> | |
| <div class="beat beat-even"></div> | |
| <div class="beat beat-odd"></div> | |
| </div> | |
| <h1 class="title content" id="closeText" style="display: none;">You can close this window now</h1> | |
| </div> | |
| <script | |
| src="https://scripts.toruswallet.io/broadcastChannel_5_0_2.js" | |
| integrity="Bu0bRAeSlh2jpBuUxKk5ivkdotaHH37cQ2XiV20EmFJmghb41D0f8xME/M1WZxFC" | |
| ></script> | |
| <script> | |
| function storageAvailable(type) { | |
| var storage; | |
| try { | |
| storage = window[type]; | |
| var x = "__storage_test__"; | |
| storage.setItem(x, x); | |
| storage.removeItem(x); | |
| return true; | |
| } catch (e) { | |
| return ( | |
| e && | |
| // everything except Firefox | |
| (e.code === 22 || | |
| // Firefox | |
| e.code === 1014 || | |
| // test name field too, because code might not be present | |
| // everything except Firefox | |
| e.name === "QuotaExceededError" || | |
| // Firefox | |
| e.name === "NS_ERROR_DOM_QUOTA_REACHED") && | |
| // acknowledge QuotaExceededError only if there's something already stored | |
| storage && | |
| storage.length !== 0 | |
| ); | |
| } | |
| } | |
| function showCloseText() { | |
| var closeText = document.getElementById("closeText"); | |
| var spinner = document.getElementById("spinner"); | |
| if (closeText) { | |
| closeText.style.display = "block"; | |
| } | |
| if (spinner) { | |
| spinner.style.display = "none"; | |
| } | |
| } | |
| var isLocalStorageAvailable = storageAvailable("localStorage"); | |
| var isSessionStorageAvailable = storageAvailable("sessionStorage"); | |
| // set theme | |
| let theme = "light"; | |
| if (isLocalStorageAvailable) { | |
| var torusTheme = localStorage.getItem("torus-theme"); | |
| if (torusTheme) { | |
| theme = torusTheme.split("-")[0]; | |
| } | |
| } | |
| if (theme === "dark") { | |
| document.querySelector("body").style.backgroundColor = "#24252A"; | |
| } | |
| var bc; | |
| var broadcastChannelOptions = { | |
| // type: 'localstorage', // (optional) enforce a type, oneOf['native', 'idb', 'localstorage', 'node' | |
| webWorkerSupport: false, // (optional) set this to false if you know that your channel will never be used in a WebWorker (increase performance) | |
| }; | |
| var instanceParams = {}; | |
| var preopenInstanceId = new URL(window.location.href).searchParams.get("preopenInstanceId"); | |
| if (!preopenInstanceId) { | |
| document.getElementById("message").style.visibility = "visible"; | |
| // in general oauth redirect | |
| try { | |
| var url = new URL(location.href); | |
| var hash = url.hash.substr(1); | |
| var hashParams = {}; | |
| if (hash) { | |
| hashParams = hash.split("&").reduce(function (result, item) { | |
| var parts = item.split("="); | |
| result[parts[0]] = parts[1]; | |
| return result; | |
| }, {}); | |
| } | |
| var queryParams = {}; | |
| for (var key of url.searchParams.keys()) { | |
| queryParams[key] = url.searchParams.get(key); | |
| } | |
| var error = ""; | |
| try { | |
| if (Object.keys(hashParams).length > 0 && hashParams.state) { | |
| instanceParams = JSON.parse(window.atob(decodeURIComponent(decodeURIComponent(hashParams.state)))) || {}; | |
| if (hashParams.error) error = hashParams.error; | |
| } else if (Object.keys(queryParams).length > 0 && queryParams.state) { | |
| instanceParams = JSON.parse(window.atob(decodeURIComponent(decodeURIComponent(queryParams.state)))) || {}; | |
| if (queryParams.error) error = queryParams.error; | |
| } | |
| } catch (e) { | |
| console.error(e); | |
| } | |
| if (instanceParams.redirectToOpener) { | |
| // communicate to window.opener | |
| window.opener.postMessage( | |
| { | |
| channel: "redirect_channel_" + instanceParams.instanceId, | |
| data: { | |
| instanceParams: instanceParams, | |
| hashParams: hashParams, | |
| queryParams: queryParams, | |
| }, | |
| error: error, | |
| }, | |
| "http://localhost:9000" | |
| ); | |
| } else { | |
| // communicate via broadcast channel | |
| bc = new broadcastChannelLib.BroadcastChannel("redirect_channel_" + instanceParams.instanceId, broadcastChannelOptions); | |
| bc.addEventListener("message", function (ev) { | |
| if (ev.success) { | |
| bc.close(); | |
| console.log("posted", { | |
| queryParams, | |
| instanceParams, | |
| hashParams, | |
| }); | |
| } else { | |
| window.close(); | |
| showCloseText(); | |
| } | |
| }); | |
| bc.postMessage({ | |
| data: { | |
| instanceParams: instanceParams, | |
| hashParams: hashParams, | |
| queryParams: queryParams, | |
| }, | |
| error: error, | |
| }).then(function () { | |
| setTimeout(function () { | |
| window.location.href = url.origin + location.search + location.hash; | |
| }, 5000); | |
| }); | |
| } | |
| } catch (err) { | |
| console.error(err, "service worker error in redirect"); | |
| bc && bc.close(); | |
| window.close(); | |
| showCloseText(); | |
| } | |
| } else { | |
| // in preopen, awaiting redirect | |
| try { | |
| bc = new broadcastChannelLib.BroadcastChannel("preopen_channel_" + preopenInstanceId, broadcastChannelOptions); | |
| bc.onmessage = function (ev) { | |
| var { preopenInstanceId: oldId, payload, message } = ev.data; | |
| if (oldId === preopenInstanceId && payload && payload.url) { | |
| window.location.href = payload.url; | |
| } else if (oldId === preopenInstanceId && message === "setup_complete") { | |
| bc.postMessage({ | |
| data: { | |
| preopenInstanceId: preopenInstanceId, | |
| message: "popup_loaded", | |
| }, | |
| }); | |
| } | |
| if (ev.error && ev.error !== "") { | |
| console.error(ev.error); | |
| bc.close(); | |
| } | |
| }; | |
| } catch (err) { | |
| console.error(err, "service worker error in preopen"); | |
| bc && bc.close(); | |
| window.close(); | |
| showCloseText(); | |
| } | |
| } | |
| </script> | |
| </body> | |
| </html> | |
| ${""} | |
| `, | |
| ], | |
| { type: "text/html" } | |
| ) | |
| ) | |
| ); | |
| } | |
| } catch (error) { | |
| console.log("Error caught in sw.js", error); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const path = require('path'); | |
| const webpack = require('webpack'); | |
| const HtmlWebpackPlugin = require('html-webpack-plugin'); | |
| module.exports = { | |
| mode: 'development', | |
| entry: { | |
| index: './src/index.js', | |
| 'serviceworker/sw': './src/service-worker.js' | |
| }, | |
| devtool: 'inline-source-map', | |
| devServer: { | |
| hot: false, | |
| client: { | |
| progress: true, | |
| }, | |
| port: 3005, | |
| liveReload: true, | |
| open: { | |
| app: { | |
| name: 'google-chrome', | |
| }, | |
| }, | |
| }, | |
| plugins: [ | |
| new HtmlWebpackPlugin({ | |
| title: 'Development', | |
| template: 'src/index.html' | |
| }), | |
| new webpack.ProvidePlugin({ | |
| Buffer: ['buffer', 'Buffer'], | |
| process: "process/browser", | |
| }), | |
| ], | |
| output: { | |
| filename: '[name].js', | |
| path: path.resolve(__dirname, 'dist'), | |
| clean: true, | |
| }, | |
| resolve: { | |
| fallback: { | |
| crypto: require.resolve("crypto-browserify"), | |
| stream: require.resolve("stream-browserify"), | |
| assert: require.resolve("assert"), | |
| buffer: require.resolve('buffer/'), | |
| process: require.resolve("process"), | |
| http: require.resolve("stream-http"), | |
| https: require.resolve("https-browserify"), | |
| os: require.resolve("os-browserify"), | |
| url: require.resolve("url"), | |
| }, | |
| }, | |
| module: { | |
| rules: [ | |
| { | |
| test: /\.m?js$/, | |
| exclude: /node_modules/, | |
| use: { | |
| loader: 'babel-loader', | |
| options: { | |
| presets: [ | |
| ['@babel/preset-env', { targets: "defaults" }] | |
| ] | |
| } | |
| } | |
| } | |
| ] | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment