UNPKG

croak-wallet-sdk

Version:

Guide for installation and usage of Croak's Web3 walllet.

212 lines (211 loc) 10.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.croakWallet = void 0; var react_1 = __importDefault(require("react")); var client_1 = require("react-dom/client"); var Main_1 = __importDefault(require("./components/Main")); var CroakSigner_1 = require("./sub_modules/CroakSigner"); function createLoginModal(skadiWallet) { var root = (0, client_1.createRoot)(document.getElementById("croak-wallet")); root.render(react_1.default.createElement(react_1.default.StrictMode, null, react_1.default.createElement(Main_1.default, { skadiWallet: skadiWallet }))); } var GLIP_WALLET_BASE_URL = "https://glip.gg/wallet-host/"; var GLIP_WALLET_IFRAME_URL = "https://glip.gg/wallet-host/"; var GLIP_WALLET_LOGIN_URL = "https://glip.gg/wallet-login"; var CroakWallet = /** @class */ (function () { function CroakWallet() { var _this = this; this.chain = ''; this.authNetwork = ''; this.clientIdentifier = ''; this.onLoginChange = function () { }; this.iWin = false; this.iframeInitialized = false; this.initCalled = false; this._createIframe(); window.onmessage = function (e) { _this.handleMessage(e); }; // first just set to null // initialize when signer first requested and only // if user is loggedin. this.croakSigner = null; } CroakWallet.prototype.init = function (_a) { var chain = _a.chain, authNetwork = _a.authNetwork, clientIdentifier = _a.clientIdentifier; //init could be called before iframe has full loaded if (this.initCalled) { return this.globalInitPromise; } this.chain = chain; this.authNetwork = authNetwork; this.clientIdentifier = clientIdentifier; this.initCalled = true; var that = this; var initPromise = new Promise(function (resolve, reject) { that.initCB = resolve; that.initRejectCB = reject; that.initInternal(); }); this.globalInitPromise = initPromise; return this.globalInitPromise; }; CroakWallet.prototype.initInternal = function () { if (this.iframeInitialized) { this.iWin.postMessage({ call: 'init', params: { chain: this.chain, authNetwork: this.authNetwork, clientIdentifier: this.clientIdentifier } }, '*'); } }; CroakWallet.prototype._createIframe = function () { var iframe = document.createElement('iframe'); iframe.style.display = "none"; iframe.src = GLIP_WALLET_IFRAME_URL; var that = this; iframe.onload = function () { that.iWin = iframe.contentWindow || iframe; that.iframeInitialized = true; if (that.initCalled) { that.initInternal(); } }; document.body.appendChild(iframe); }; CroakWallet.prototype._checkIfLoaded = function () { var aman = 2 + 2; return aman; }; CroakWallet.prototype.showConnectModal = function () { createLoginModal(this); }; CroakWallet.prototype.login = function (loginType, lastLocation) { window.location = "".concat(GLIP_WALLET_LOGIN_URL, "?loginType=").concat(loginType, "&chain=").concat(this.chain, "&authNetwork=").concat(this.authNetwork, "&lastLocation=").concat(lastLocation, "&clientIdentifier=").concat(this.clientIdentifier); }; CroakWallet.prototype.getUserInfo = function () { var that = this; return new Promise(function (resolve, reject) { that.getUserInfoCB = resolve; that.getUserInfoRejectCB = reject; that.iWin.postMessage({ call: 'getUserInfo', params: {} }, '*'); }); }; CroakWallet.prototype.isConnected = function () { var that = this; return new Promise(function (resolve, reject) { that.isConnectedCB = resolve; that.isConnectedRejectedCB = reject; that.iWin.postMessage({ call: 'isConnected', params: {} }, '*'); }); }; CroakWallet.prototype.getWalletID = function () { var that = this; return new Promise(function (resolve, reject) { that.getWalletIDCB = resolve; that.getWalletIdRejectCB = reject; that.iWin.postMessage({ call: 'getWalletID', params: {} }, '*'); }); }; CroakWallet.prototype.logout = function () { var that = this; return new Promise(function (resolve, reject) { that.logoutCB = resolve; that.logoutRejectedCB = reject; that.iWin.postMessage({ call: 'logout', params: {} }, '*'); }); }; CroakWallet.prototype._loginWithIdToken = function (_a) { var loginType = _a.loginType, userInfo = _a.userInfo, verifier = _a.verifier; var GLIP_WALLET_BASE_URL = 'http://127.0.0.1:8080/'; window.location = "".concat(GLIP_WALLET_BASE_URL, "#loginType=").concat(loginType, "&chain=").concat(this.chain, "&verifier=").concat(verifier, "&id=").concat(userInfo.id, "&idToken=").concat(userInfo.idToken, "&clientIdentifier=").concat(this.clientIdentifier, "&accessToken=").concat(userInfo.accessToken); }; // handles messages from the iFrame CroakWallet.prototype.handleMessage = function (event) { if (event.data.call === "getUserInfo") { if (event.data.errorVal) { this.getUserInfoRejectCB(event.data.errorVal); } this.getUserInfoCB(event.data.retVal); } else if (event.data.call === "init") { this.initCB(event.data.retVal); } else if (event.data.call === "getWalletID") { this.getWalletIDCB(event.data.retVal); } else if (event.data.call === "isConnected") { this.isConnectedCB(event.data.retVal); } else if (event.data.call === "logout") { this.logoutCB(event.data.retVal); } }; CroakWallet.prototype.getSigner = function (provider) { return __awaiter(this, void 0, void 0, function () { var isConnected, userInfo; return __generator(this, function (_a) { switch (_a.label) { case 0: if (this.croakSigner) { return [2 /*return*/, this.croakSigner]; } return [4 /*yield*/, this.isConnected()]; case 1: isConnected = _a.sent(); if (!isConnected) return [3 /*break*/, 3]; return [4 /*yield*/, this.getUserInfo()]; case 2: userInfo = _a.sent(); this.croakSigner = new CroakSigner_1.CroakSigner(userInfo.publicAddress, GLIP_WALLET_BASE_URL, false, provider); return [2 /*return*/, this.croakSigner]; case 3: throw ("Need to be signed in to get signer"); } }); }); }; return CroakWallet; }()); var croakWallet = new CroakWallet(); exports.croakWallet = croakWallet; window.croakWallet = croakWallet;