UNPKG

@unilogin/sdk

Version:

SDK is a JS library, that communicates with relayer. SDK allows managing contract, by creating basic contract-calling messages.

273 lines 14.5 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); var commons_1 = require("@unilogin/commons"); var ethers_1 = require("ethers"); var AbstractWallet_1 = require("./AbstractWallet"); var BackupCodesWithExecution_1 = require("./BackupCodesWithExecution"); var errors_1 = require("../../core/utils/errors"); var validation_1 = require("../../core/utils/validation"); var propertyFromSubscription_1 = require("../../core/utils/propertyFromSubscription"); var DeployedWallet = /** @class */ (function (_super) { __extends(DeployedWallet, _super); function DeployedWallet(contractAddress, name, privateKey, sdk) { var _this = _super.call(this, contractAddress, name, privateKey) || this; _this.sdk = sdk; _this.authorizations = propertyFromSubscription_1.propertyFromSubscription(_this.subscribeAuthorisations.bind(_this), []); return _this; } Object.defineProperty(DeployedWallet.prototype, "asApplicationWallet", { get: function () { return { contractAddress: this.contractAddress, name: this.name, privateKey: this.privateKey, }; }, enumerable: true, configurable: true }); DeployedWallet.prototype.addKey = function (publicKey, executionOptions) { return this.selfExecute('addKey', [publicKey], __assign({ gasLimit: commons_1.DEFAULT_GAS_LIMIT }, executionOptions)); }; DeployedWallet.prototype.addKeys = function (publicKeys, executionOptions) { return this.selfExecute('addKeys', [publicKeys], __assign({ gasLimit: commons_1.DEFAULT_GAS_LIMIT }, executionOptions)); }; DeployedWallet.prototype.removeKey = function (key, executionOptions) { return this.selfExecute('removeKey', [key], __assign({ gasLimit: commons_1.DEFAULT_GAS_LIMIT }, executionOptions)); }; DeployedWallet.prototype.removeCurrentKey = function (executionOptions) { var ownKey = ethers_1.utils.computeAddress(this.privateKey); return this.removeKey(ownKey, executionOptions); }; DeployedWallet.prototype.denyRequests = function () { return __awaiter(this, void 0, void 0, function () { var authorisationRequest; return __generator(this, function (_a) { switch (_a.label) { case 0: authorisationRequest = { contractAddress: this.contractAddress }; return [4 /*yield*/, this.sdk.walletContractService.signRelayerRequest(this.privateKey, authorisationRequest)]; case 1: _a.sent(); return [4 /*yield*/, this.sdk.relayerApi.denyConnection(authorisationRequest)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }; DeployedWallet.prototype.setRequiredSignatures = function (requiredSignatures, executionOptions) { return this.selfExecute('setRequiredSignatures', [requiredSignatures], __assign({ gasLimit: commons_1.DEFAULT_GAS_LIMIT }, executionOptions)); }; DeployedWallet.prototype.execute = function (message) { return __awaiter(this, void 0, void 0, function () { var relayerConfig, nonce, _a, partialMessage, signedMessage; return __generator(this, function (_b) { switch (_b.label) { case 0: relayerConfig = this.sdk.getRelayerConfig(); _a = message.nonce; if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, this.getNonce()]; case 1: _a = (_b.sent()); _b.label = 2; case 2: nonce = _a; partialMessage = __assign(__assign({ gasToken: commons_1.ETHER_NATIVE_TOKEN.address }, message), { nonce: nonce, refundReceiver: relayerConfig.relayerAddress, operationType: commons_1.OperationType.call, from: this.contractAddress, gasPrice: this.sdk.isRefundPaid() ? '0' : message.gasPrice }); commons_1.ensure(partialMessage.gasLimit <= relayerConfig.maxGasLimit, errors_1.InvalidGasLimit, partialMessage.gasLimit + " provided, when relayer's max gas limit is " + relayerConfig.maxGasLimit); return [4 /*yield*/, this.sdk.messageConverter.messageToSignedMessage(partialMessage, this.privateKey)]; case 3: signedMessage = _b.sent(); return [4 /*yield*/, this.sdk.sufficientBalanceValidator.validate(signedMessage)]; case 4: _b.sent(); validation_1.ensureSufficientGas(signedMessage); return [2 /*return*/, this.sdk.executionFactory.createExecution(signedMessage)]; } }); }); }; DeployedWallet.prototype.keyExist = function (key) { return this.sdk.walletContractService.keyExist(this.contractAddress, key); }; DeployedWallet.prototype.getNonce = function () { return this.sdk.walletContractService.lastNonce(this.contractAddress); }; DeployedWallet.prototype.getConnectedDevices = function () { return this.sdk.getConnectedDevices(this.contractAddress, this.privateKey); }; DeployedWallet.prototype.getKeys = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getConnectedDevices()]; case 1: return [2 /*return*/, (_a.sent()) .map(function (device) { return device.publicKey; })]; } }); }); }; DeployedWallet.prototype.getRequiredSignatures = function () { return this.sdk.walletContractService.requiredSignatures(this.contractAddress); }; DeployedWallet.prototype.getGasModes = function () { return this.sdk.getGasModes(); }; DeployedWallet.prototype.selfExecute = function (method, args, executionOptions) { return __awaiter(this, void 0, void 0, function () { var data, message; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.sdk.walletContractService.encodeFunction(this.contractAddress, method, args)]; case 1: data = _a.sent(); message = __assign(__assign({}, executionOptions), { to: this.contractAddress, from: this.contractAddress, data: data }); return [2 /*return*/, this.execute(message)]; } }); }); }; DeployedWallet.prototype.generateBackupCodes = function (executionOptions) { return __awaiter(this, void 0, void 0, function () { var codes, addresses, codes_1, codes_1_1, code, address, e_1_1, execution; var e_1, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: codes = [commons_1.generateBackupCode()]; addresses = []; _b.label = 1; case 1: _b.trys.push([1, 6, 7, 8]); codes_1 = __values(codes), codes_1_1 = codes_1.next(); _b.label = 2; case 2: if (!!codes_1_1.done) return [3 /*break*/, 5]; code = codes_1_1.value; return [4 /*yield*/, commons_1.walletFromBrain(this.name, code)]; case 3: address = (_b.sent()).address; addresses.push(address); _b.label = 4; case 4: codes_1_1 = codes_1.next(); return [3 /*break*/, 2]; case 5: return [3 /*break*/, 8]; case 6: e_1_1 = _b.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 8]; case 7: try { if (codes_1_1 && !codes_1_1.done && (_a = codes_1.return)) _a.call(codes_1); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 8: return [4 /*yield*/, this.addKey(addresses[0], __assign({ gasLimit: commons_1.DEFAULT_GAS_LIMIT }, executionOptions))]; case 9: execution = _b.sent(); return [2 /*return*/, new BackupCodesWithExecution_1.BackupCodesWithExecution(execution, codes)]; } }); }); }; DeployedWallet.prototype.signMessage = function (bytes) { return this.sdk.walletContractService.signMessage(this.contractAddress, this.privateKey, bytes); }; DeployedWallet.prototype.subscribeAuthorisations = function (callback) { return this.sdk.subscribeAuthorisations(this.contractAddress, this.privateKey, callback); }; DeployedWallet.prototype.subscribeToBalances = function (callback) { return this.sdk.subscribeToBalances(this.contractAddress, callback); }; DeployedWallet.prototype.subscribeDisconnected = function (onDisconnected) { return __awaiter(this, void 0, void 0, function () { var eventName, unsubscribe; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.sdk.walletContractService.getEventNameFor(this.contractAddress, 'KeyRemoved')]; case 1: eventName = _a.sent(); unsubscribe = this.sdk.subscribe(eventName, { contractAddress: this.contractAddress, key: this.publicKey }, function () { return onDisconnected(); }); return [2 /*return*/, function () { return unsubscribe(); }]; } }); }); }; return DeployedWallet; }(AbstractWallet_1.AbstractWallet)); exports.DeployedWallet = DeployedWallet; //# sourceMappingURL=DeployedWallet.js.map