UNPKG

@planq-network/connect

Version:

Light Toolkit for connecting with the Planq network

328 lines 15.5 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 (g && (g = 0, op[0] && (_ = 0)), _) 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.PlanqProvider = exports.assertIsPlanqProvider = void 0; var lock_1 = require("@planq-network/base/lib/lock"); var debug_1 = __importDefault(require("debug")); var provider_utils_1 = require("./utils/provider-utils"); var rpc_caller_1 = require("./utils/rpc-caller"); var debug = (0, debug_1.default)("provider:connection"); var debugPayload = (0, debug_1.default)("provider:payload"); var debugTxToSend = (0, debug_1.default)("provider:tx-to-send"); var debugEncodedTx = (0, debug_1.default)("provider:encoded-tx"); var debugResponse = (0, debug_1.default)("provider:response"); var InterceptedMethods; (function (InterceptedMethods) { InterceptedMethods["accounts"] = "eth_accounts"; InterceptedMethods["sendTransaction"] = "eth_sendTransaction"; InterceptedMethods["signTransaction"] = "eth_signTransaction"; InterceptedMethods["sign"] = "eth_sign"; InterceptedMethods["personalSign"] = "personal_sign"; InterceptedMethods["signTypedData"] = "eth_signTypedData"; InterceptedMethods["signTypedDataV1"] = "eth_signTypedData_v1"; InterceptedMethods["signTypedDataV3"] = "eth_signTypedData_v3"; InterceptedMethods["signTypedDataV4"] = "eth_signTypedData_v4"; InterceptedMethods["signTypedDataV5"] = "eth_signTypedData_v5"; })(InterceptedMethods || (InterceptedMethods = {})); function assertIsPlanqProvider(provider) { if (!(provider instanceof PlanqProvider)) { throw new Error("A different Provider was manually added to the kit. The kit should have a PlanqProvider"); } } exports.assertIsPlanqProvider = assertIsPlanqProvider; /* * PlanqProvider wraps a web3.js provider for use with Planq */ var PlanqProvider = /** @class */ (function () { function PlanqProvider(existingProvider, connection) { this.existingProvider = existingProvider; this.connection = connection; this.alreadyStopped = false; // Transaction nonce is calculated as the max of an account's nonce on-chain, and any pending transactions in a node's // transaction pool. As a result, once a nonce is used, the transaction must be sent to the node before the nonce can // be calculated for another transaction. In particular the sign and send operation must be completed atomically with // relation to other sign and send operations. this.nonceLock = new lock_1.Lock(); this.addProviderDelegatedFunctions(); } // @deprecated Use the `addAccount` from the Connection PlanqProvider.prototype.addAccount = function (privateKey) { this.connection.addAccount(privateKey); }; // @deprecated Use the `removeAccount` from the Connection PlanqProvider.prototype.removeAccount = function (address) { this.connection.removeAccount(address); }; // @deprecated Use the `getAccounts` from the Connection PlanqProvider.prototype.getAccounts = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.connection.getAccounts()]; }); }); }; PlanqProvider.prototype.isLocalAccount = function (address) { return (this.connection.wallet != null && this.connection.wallet.hasAccount(address)); }; /** * Send method as expected by web3.js */ PlanqProvider.prototype.send = function (payload, callback) { var txParams; var address; debugPayload("%O", payload); var decoratedCallback = function (error, result) { debugResponse("%O", result); callback(error, result); }; if (this.alreadyStopped) { throw Error("PlanqProvider already stopped"); } switch (payload.method) { case InterceptedMethods.accounts: { (0, rpc_caller_1.rpcCallHandler)(payload, this.handleAccounts.bind(this), decoratedCallback); return; } case InterceptedMethods.sendTransaction: { this.checkPayloadWithAtLeastNParams(payload, 1); txParams = payload.params[0]; if (this.connection.isLocalAccount(txParams.from)) { (0, rpc_caller_1.rpcCallHandler)(payload, this.handleSendTransaction.bind(this), decoratedCallback); } else { this.forwardSend(payload, callback); } return; } case InterceptedMethods.signTransaction: { this.checkPayloadWithAtLeastNParams(payload, 1); txParams = payload.params[0]; if (this.connection.isLocalAccount(txParams.from)) { (0, rpc_caller_1.rpcCallHandler)(payload, this.handleSignTransaction.bind(this), decoratedCallback); } else { this.forwardSend(payload, callback); } return; } case InterceptedMethods.sign: case InterceptedMethods.personalSign: { this.checkPayloadWithAtLeastNParams(payload, 2); address = payload.method === InterceptedMethods.sign ? payload.params[0] : payload.params[1]; if (this.connection.isLocalAccount(address)) { (0, rpc_caller_1.rpcCallHandler)(payload, this.handleSignPersonalMessage.bind(this), decoratedCallback); } else { this.forwardSend(payload, callback); } return; } case InterceptedMethods.signTypedData: case InterceptedMethods.signTypedDataV1: case InterceptedMethods.signTypedDataV3: case InterceptedMethods.signTypedDataV4: case InterceptedMethods.signTypedDataV5: { this.checkPayloadWithAtLeastNParams(payload, 1); address = payload.params[0]; if (this.connection.isLocalAccount(address)) { (0, rpc_caller_1.rpcCallHandler)(payload, this.handleSignTypedData.bind(this), decoratedCallback); } else { this.forwardSend(payload, callback); } return; } default: { this.forwardSend(payload, callback); return; } } }; PlanqProvider.prototype.stop = function () { if (this.alreadyStopped) { return; } try { (0, provider_utils_1.stopProvider)(this.existingProvider); this.alreadyStopped = true; } catch (error) { debug("Failed to close the connection: ".concat(error)); } }; PlanqProvider.prototype.handleAccounts = function (_payload) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.connection.getAccounts()]; }); }); }; PlanqProvider.prototype.handleSignTypedData = function (payload) { return __awaiter(this, void 0, void 0, function () { var _a, address, typedData, signature; return __generator(this, function (_b) { _a = payload.params, address = _a[0], typedData = _a[1]; signature = this.connection.wallet.signTypedData(address, typedData); return [2 /*return*/, signature]; }); }); }; PlanqProvider.prototype.handleSignPersonalMessage = function (payload) { return __awaiter(this, void 0, void 0, function () { var address, data, ecSignatureHex; return __generator(this, function (_a) { address = payload.method === "eth_sign" ? payload.params[0] : payload.params[1]; data = payload.method === "eth_sign" ? payload.params[1] : payload.params[0]; ecSignatureHex = this.connection.wallet.signPersonalMessage(address, data); return [2 /*return*/, ecSignatureHex]; }); }); }; PlanqProvider.prototype.handleSignTransaction = function (payload) { return __awaiter(this, void 0, void 0, function () { var txParams, filledParams, signedTx; return __generator(this, function (_a) { switch (_a.label) { case 0: txParams = payload.params[0]; return [4 /*yield*/, this.connection.paramsPopulator.populate(txParams)]; case 1: filledParams = _a.sent(); debugTxToSend("%O", filledParams); return [4 /*yield*/, this.connection.wallet.signTransaction(filledParams)]; case 2: signedTx = _a.sent(); debugEncodedTx("%O", signedTx); return [2 /*return*/, signedTx]; } }); }); }; PlanqProvider.prototype.handleSendTransaction = function (payload) { return __awaiter(this, void 0, void 0, function () { var signedTx, response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.nonceLock.acquire()]; case 1: _a.sent(); _a.label = 2; case 2: _a.trys.push([2, , 5, 6]); return [4 /*yield*/, this.handleSignTransaction(payload)]; case 3: signedTx = _a.sent(); return [4 /*yield*/, this.connection.rpcCaller.call("eth_sendRawTransaction", [signedTx.raw])]; case 4: response = _a.sent(); return [2 /*return*/, response.result]; case 5: this.nonceLock.release(); return [7 /*endfinally*/]; case 6: return [2 /*return*/]; } }); }); }; PlanqProvider.prototype.forwardSend = function (payload, callback) { this.connection.rpcCaller.send(payload, callback); }; PlanqProvider.prototype.checkPayloadWithAtLeastNParams = function (payload, n) { if (!payload.params || payload.params.length < n) { throw Error("Invalid params"); } }; // Functions required to act as a delefator for the existingProvider PlanqProvider.prototype.addProviderDelegatedFunctions = function () { if ((0, provider_utils_1.hasProperty)(this.existingProvider, "on")) { // @ts-ignore this.on = this.defaultOn; } if ((0, provider_utils_1.hasProperty)(this.existingProvider, "once")) { // @ts-ignore this.once = this.defaultOnce; } if ((0, provider_utils_1.hasProperty)(this.existingProvider, "removeListener")) { // @ts-ignore this.removeListener = this.defaultRemoveListener; } if ((0, provider_utils_1.hasProperty)(this.existingProvider, "removeAllListener")) { // @ts-ignore this.removeAllListener = this.defaultRemoveAllListeners; } if ((0, provider_utils_1.hasProperty)(this.existingProvider, "reset")) { // @ts-ignore this.reset = this.defaultReset; } }; Object.defineProperty(PlanqProvider.prototype, "connected", { get: function () { return this.existingProvider.connected; }, enumerable: false, configurable: true }); PlanqProvider.prototype.supportsSubscriptions = function () { return this.existingProvider.supportsSubscriptions(); }; PlanqProvider.prototype.defaultOn = function (type, callback) { this.existingProvider.on(type, callback); }; PlanqProvider.prototype.defaultOnce = function (type, callback) { this.existingProvider.once(type, callback); }; PlanqProvider.prototype.defaultRemoveListener = function (type, callback) { this.existingProvider.removeListener(type, callback); }; PlanqProvider.prototype.defaultRemoveAllListeners = function (type) { this.existingProvider.removeAllListeners(type); }; PlanqProvider.prototype.defaultReset = function () { this.existingProvider.reset(); }; return PlanqProvider; }()); exports.PlanqProvider = PlanqProvider; //# sourceMappingURL=planq-provider.js.map