@portis/web3
Version:
Portis Web3 SDK
341 lines • 16.7 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(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 };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var penpal_1 = require("penpal");
var onWindowLoad_1 = require("../utils/onWindowLoad");
var styles_1 = require("../styles");
var secureOrigin_1 = require("../utils/secureOrigin");
// Create a .env file to override the default WIDGET_URL when running locally
var WIDGET_URL = process.env.PORTIS_WIDGET_URL || 'https://widget.portis.io';
var STAGING_WIDGET_URL = 'https://widget-staging.portis.io';
var FEATURE_WIDGET_URL = 'https://widget-feature.portis.io';
var PORTIS_IFRAME_CLASS = 'por_portis-widget-frame';
var PORTIS_CONTAINER_CLASS = 'por_portis-container';
function windowLoadHandler() {
if (document.getElementsByClassName(PORTIS_IFRAME_CLASS).length) {
console.warn('Portis script was already loaded. This might cause unexpected behavior. If loading with a <script> tag, please make sure that you only load it once.');
}
}
exports.windowLoadHandler = windowLoadHandler;
var WidgetManager = /** @class */ (function () {
function WidgetManager(_widgetConfig, _clearProviderSession) {
this._widgetConfig = _widgetConfig;
this._clearProviderSession = _clearProviderSession;
this._widgetUrl = WIDGET_URL;
this._onLoginCallback = function () { };
this._onLogoutCallback = function () { };
this._onErrorCallback = function () { };
secureOrigin_1.validateSecureOrigin();
if (_widgetConfig.staging) {
console.warn('Please note: you are using the Portis STAGING environment.');
this._widgetUrl = STAGING_WIDGET_URL;
}
else if (_widgetConfig.feature) {
console.warn('Please note: you are using the Portis FEATURE environment.');
this._widgetUrl = FEATURE_WIDGET_URL;
}
WidgetManager._checkIfWidgetAlreadyInitialized();
}
// async singleton
WidgetManager.prototype.getWidget = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!!this.widgetInstance) return [3 /*break*/, 2];
if (!this.widgetPromise) {
this.widgetPromise = this._initWidget();
}
_a = this;
return [4 /*yield*/, this.widgetPromise];
case 1:
_a.widgetInstance = _b.sent();
_b.label = 2;
case 2: return [2 /*return*/, this.widgetInstance];
}
});
});
};
WidgetManager.prototype.setDefaultEmail = function (email) {
this._widgetConfig.defaultEmail = email;
};
// Population by the dev of SDK callbacks that might be invoked by the widget
WidgetManager.prototype.setOnLoginCallback = function (callback) {
this._onLoginCallback = callback;
};
WidgetManager.prototype.setOnLogoutCallback = function (callback) {
this._onLogoutCallback = callback;
};
WidgetManager.prototype.setOnActiveWalletChangedCallback = function (callback) {
this._onActiveWalletChangedCallback = callback;
};
WidgetManager.prototype.setOnErrorCallback = function (callback) {
this._onErrorCallback = callback;
};
// SDK methods that could be invoked by the user and handled by the widget
WidgetManager.prototype.showPortis = function () {
return __awaiter(this, void 0, void 0, function () {
var widgetCommunication;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWidget()];
case 1:
widgetCommunication = (_a.sent()).communication;
return [2 /*return*/, widgetCommunication.showPortis(this._widgetConfig)];
}
});
});
};
WidgetManager.prototype.getCampaignInfo = function (campaignId) {
return __awaiter(this, void 0, void 0, function () {
var widgetCommunication;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWidget()];
case 1:
widgetCommunication = (_a.sent()).communication;
return [2 /*return*/, widgetCommunication.getCampaignInfo(campaignId)];
}
});
});
};
WidgetManager.prototype.claimVoucher = function (voucherId) {
return __awaiter(this, void 0, void 0, function () {
var widgetCommunication;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWidget()];
case 1:
widgetCommunication = (_a.sent()).communication;
return [2 /*return*/, widgetCommunication.claimVoucher(voucherId, this._widgetConfig)];
}
});
});
};
WidgetManager.prototype.logout = function () {
return __awaiter(this, void 0, void 0, function () {
var widgetCommunication;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWidget()];
case 1:
widgetCommunication = (_a.sent()).communication;
return [2 /*return*/, widgetCommunication.logout()];
}
});
});
};
WidgetManager.prototype.getExtendedPublicKey = function (path, coin) {
if (path === void 0) { path = "m/44'/60'/0'/0/0"; }
if (coin === void 0) { coin = 'Ethereum'; }
return __awaiter(this, void 0, void 0, function () {
var widgetCommunication;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWidget()];
case 1:
widgetCommunication = (_a.sent()).communication;
return [2 /*return*/, widgetCommunication.getExtendedPublicKey(path, coin, this._widgetConfig)];
}
});
});
};
WidgetManager.prototype.importWallet = function (mnemonicOrPrivateKey) {
return __awaiter(this, void 0, void 0, function () {
var widgetCommunication;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWidget()];
case 1:
widgetCommunication = (_a.sent()).communication;
return [2 /*return*/, widgetCommunication.importWallet(mnemonicOrPrivateKey, this._widgetConfig)];
}
});
});
};
WidgetManager.prototype.isLoggedIn = function () {
return __awaiter(this, void 0, void 0, function () {
var widgetCommunication;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWidget()];
case 1:
widgetCommunication = (_a.sent()).communication;
return [2 /*return*/, widgetCommunication.isLoggedIn()];
}
});
});
};
WidgetManager.prototype.signBitcoinTransaction = function (params) {
return __awaiter(this, void 0, void 0, function () {
var widgetCommunication;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWidget()];
case 1:
widgetCommunication = (_a.sent()).communication;
return [2 /*return*/, widgetCommunication.signBitcoinTransaction(params, this._widgetConfig)];
}
});
});
};
WidgetManager.prototype.showBitcoinWallet = function (path) {
if (path === void 0) { path = "m/49'/0'/0'/0/0"; }
return __awaiter(this, void 0, void 0, function () {
var widgetCommunication;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWidget()];
case 1:
widgetCommunication = (_a.sent()).communication;
return [2 /*return*/, widgetCommunication.showBitcoinWallet(path, this._widgetConfig)];
}
});
});
};
WidgetManager.prototype.purchaseERC20 = function (params) {
return __awaiter(this, void 0, void 0, function () {
var widgetCommunication;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWidget()];
case 1:
widgetCommunication = (_a.sent()).communication;
return [2 /*return*/, widgetCommunication.purchaseERC20(params, this._widgetConfig)];
}
});
});
};
// internal methods
WidgetManager._checkIfWidgetAlreadyInitialized = function () {
if (document.getElementsByClassName(PORTIS_CONTAINER_CLASS).length) {
console.warn('An instance of Portis was already initialized. This is probably a mistake. Make sure that you use the same Portis instance throughout your app.');
}
};
WidgetManager.prototype._initWidget = function () {
return __awaiter(this, void 0, void 0, function () {
var style, container, widgetFrame, connection, communication;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, onWindowLoad_1.onWindowLoad()];
case 1:
_a.sent();
style = document.createElement('style');
style.innerHTML = styles_1.styles;
container = document.createElement('div');
container.className = PORTIS_CONTAINER_CLASS;
widgetFrame = document.createElement('div');
widgetFrame.id = "portis-container-" + Date.now();
widgetFrame.className = PORTIS_IFRAME_CLASS;
container.appendChild(widgetFrame);
document.body.appendChild(container);
document.head.appendChild(style);
connection = penpal_1.default.connectToChild({
url: this._widgetUrl,
appendTo: document.getElementById(widgetFrame.id),
methods: {
setHeight: this._setHeight.bind(this),
getWindowSize: WidgetManager._getWindowSize.bind(this),
onLogin: this._onLogin.bind(this),
onLogout: this._onLogout.bind(this),
onActiveWalletChanged: this._onActiveWalletChanged.bind(this),
hasOnActiveWalletChanged: this.hasOnActiveWalletChanged.bind(this),
onError: this._onError.bind(this),
},
});
return [4 /*yield*/, connection.promise];
case 2:
communication = _a.sent();
communication.setSdkConfig(this._widgetConfig);
connection.iframe.style.position = 'absolute';
connection.iframe.style.height = '100%';
connection.iframe.style.width = '100%';
connection.iframe.style.border = '0 transparent';
return [2 /*return*/, { communication: communication, widgetFrame: widgetFrame }];
}
});
});
};
WidgetManager.prototype._setHeight = function (height) {
return __awaiter(this, void 0, void 0, function () {
var widgetFrame;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getWidget()];
case 1:
widgetFrame = (_a.sent()).widgetFrame;
widgetFrame.style.height = height + "px";
return [2 /*return*/];
}
});
});
};
WidgetManager._getWindowSize = function () {
var body = document.getElementsByTagName('body')[0];
var width = window.innerWidth || document.documentElement.clientWidth || body.clientWidth;
var height = window.innerHeight || document.documentElement.clientHeight || body.clientHeight;
return { width: width, height: height };
};
WidgetManager.prototype._onLogin = function (walletAddress, email, reputation) {
if (this._onLoginCallback) {
this._onLoginCallback(walletAddress, email, reputation);
}
};
WidgetManager.prototype._onLogout = function () {
this._clearProviderSession();
if (this._onLogoutCallback) {
this._onLogoutCallback();
}
};
WidgetManager.prototype._onActiveWalletChanged = function (walletAddress) {
if (this._onActiveWalletChangedCallback) {
this._onActiveWalletChangedCallback(walletAddress);
}
};
WidgetManager.prototype.hasOnActiveWalletChanged = function () {
return !!this._onActiveWalletChangedCallback;
};
WidgetManager.prototype._onError = function (error) {
if (this._onErrorCallback) {
this._onErrorCallback(error);
}
};
return WidgetManager;
}());
exports.default = WidgetManager;
//# sourceMappingURL=widgetManager.js.map