@portis/web3
Version:
Portis Web3 SDK
257 lines • 12.2 kB
JavaScript
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 };
}
};
import { networkAdapter } from './networks';
import { onWindowLoad } from './utils/onWindowLoad';
import { validateSecureOrigin } from './utils/secureOrigin';
import WidgetManager, { windowLoadHandler } from './widget/widgetManager';
import Web3Manager from './web3/web3Manager';
import { isClientSide } from './utils/isClientSide';
import { mockify } from './utils/mockify';
var VERSION = '4.0.7';
var SUPPORTED_SCOPES = ['email', 'reputation'];
onWindowLoad()
.then(windowLoadHandler)
.catch(function () { }); // Prevents unhandledPromiseRejectionWarning, which happens when using React SSR;
var Portis = /** @class */ (function () {
function Portis(dappId, network, options) {
if (options === void 0) { options = {}; }
// If rendered in SSR, return a mock version of the Portis object.
// All methods are callable and all properties exist, but they do nothing.
if (!isClientSide()) {
return mockify(this);
}
validateSecureOrigin();
this._validateParams(dappId, network, options);
this._config = {
dappId: dappId,
network: networkAdapter(network, options.gasRelay),
version: VERSION,
scope: options.scope,
registerPageByDefault: options.registerPageByDefault,
staging: options.staging,
feature: options.feature,
};
this._getWidgetCommunication = this._getWidgetCommunication.bind(this);
this._widgetManagerInstance = new WidgetManager(this.config, this._clearProviderSession);
this._web3ManagerInstance = new Web3Manager(this.config, this._getWidgetCommunication);
this.setDefaultEmail = this.setDefaultEmail.bind(this);
this.changeNetwork = this.changeNetwork.bind(this);
this.getWidget = this.getWidget.bind(this);
this.onLogin = this.onLogin.bind(this);
this.onLogout = this.onLogout.bind(this);
this.onActiveWalletChanged = this.onActiveWalletChanged.bind(this);
this.onError = this.onError.bind(this);
this.showPortis = this.showPortis.bind(this);
this.getCampaignInfo = this.getCampaignInfo.bind(this);
this.claimVoucher = this.claimVoucher.bind(this);
this.getExtendedPublicKey = this.getExtendedPublicKey.bind(this);
this.importWallet = this.importWallet.bind(this);
this.isLoggedIn = this.isLoggedIn.bind(this);
this.signBitcoinTransaction = this.signBitcoinTransaction.bind(this);
this.showBitcoinWallet = this.showBitcoinWallet.bind(this);
// this.purchaseERC20 = this.purchaseERC20.bind(this);
}
Object.defineProperty(Portis.prototype, "_widgetManager", {
get: function () {
return this._widgetManagerInstance;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Portis.prototype, "_web3Manager", {
get: function () {
return this._web3ManagerInstance;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Portis.prototype, "config", {
get: function () {
return this._config;
},
enumerable: true,
configurable: true
});
Portis.prototype._clearProviderSession = function () {
this._web3Manager.setSelectedAddress('');
};
Portis.prototype._getWidgetCommunication = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._widgetManager.getWidget()];
case 1: return [2 /*return*/, (_a.sent()).communication];
}
});
});
};
Object.defineProperty(Portis.prototype, "web3Provider", {
get: function () {
return this._web3Manager.provider;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Portis.prototype, "provider", {
// Todo: deprecate
get: function () {
return this.web3Provider;
},
enumerable: true,
configurable: true
});
Portis.prototype.changeNetwork = function (network, gasRelay) {
this._web3Manager.changeNetwork(network, gasRelay);
};
Portis.prototype.setDefaultEmail = function (email) {
this._widgetManager.setDefaultEmail(email);
};
// async singleton
Portis.prototype.getWidget = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._widgetManager.getWidget()];
});
});
};
// Population by the dev of SDK callbacks that might be invoked by the widget
Portis.prototype.onLogin = function (callback) {
this._widgetManager.setOnLoginCallback(callback);
};
Portis.prototype.onLogout = function (callback) {
this._widgetManager.setOnLogoutCallback(callback);
};
Portis.prototype.onActiveWalletChanged = function (callback) {
this._widgetManager.setOnActiveWalletChangedCallback(callback);
};
Portis.prototype.onError = function (callback) {
this._widgetManager.setOnErrorCallback(callback);
};
// SDK methods that could be invoked by the user and handled by the widget
Portis.prototype.showPortis = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._widgetManager.showPortis()];
});
});
};
Portis.prototype.getCampaignInfo = function (campaignId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._widgetManager.getCampaignInfo(campaignId)];
});
});
};
Portis.prototype.claimVoucher = function (voucherId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._widgetManager.claimVoucher(voucherId)];
});
});
};
Portis.prototype.logout = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._widgetManager.logout()];
});
});
};
Portis.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 () {
return __generator(this, function (_a) {
return [2 /*return*/, this._widgetManager.getExtendedPublicKey(path, coin)];
});
});
};
Portis.prototype.importWallet = function (mnemonicOrPrivateKey) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._widgetManager.importWallet(mnemonicOrPrivateKey)];
});
});
};
Portis.prototype.isLoggedIn = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._widgetManager.isLoggedIn()];
});
});
};
Portis.prototype.signBitcoinTransaction = function (params) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._widgetManager.signBitcoinTransaction(params)];
});
});
};
Portis.prototype.showBitcoinWallet = function (path) {
if (path === void 0) { path = "m/49'/0'/0'/0/0"; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this._widgetManager.showBitcoinWallet(path)];
});
});
};
// async purchaseERC20(tokenAddress: string, amount: string, tokenSymbol: string, logoURL?: string) {
// return this._widgetManager.purchaseERC20({tokenAddress, amount, tokenSymbol, logoURL});
// }
// internal methods
Portis.prototype._validateParams = function (dappId, network, options) {
if (!dappId) {
throw new Error("[Portis] 'dappId' is required. Get your dappId here: https://dashboard.portis.io");
}
if (!network) {
throw new Error("[Portis] 'network' is required. Read more about it here: https://docs.portis.io/#/configuration?id=network");
}
if (options.scope) {
if (!Array.isArray(options.scope)) {
throw new Error("[Portis] 'scope' must be an array. Read more about it here: https://docs.portis.io/#/configuration?id=scope");
}
var unknownScope = options.scope.filter(function (item) { return SUPPORTED_SCOPES.indexOf(item) < 0; });
if (unknownScope.length > 0) {
throw new Error("[Portis] invalid 'scope' parameter. Read more about it here: https://docs.portis.io/#/configuration?id=scope");
}
}
if (options.registerPageByDefault !== undefined && typeof options.registerPageByDefault !== 'boolean') {
throw new Error("[Portis] invalid 'registerPageByDefault' parameter, must be a boolean. Read more about it here: https://docs.portis.io/#/configuration?id=registerPageByDefault");
}
};
return Portis;
}());
export default Portis;
//# sourceMappingURL=index.js.map