UNPKG

@celo/connect

Version:

Light Toolkit for connecting with the Celo network

119 lines 4.61 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpRpcCaller = exports.getRandomId = exports.rpcCallHandler = void 0; const debug_1 = __importDefault(require("debug")); const debugRpcPayload = (0, debug_1.default)('rpc:payload'); const debugRpcResponse = (0, debug_1.default)('rpc:response'); const debugRpcCallback = (0, debug_1.default)('rpc:callback:exception'); function rpcCallHandler(payload, handler, callback) { try { handler(payload) .then((result) => { callback(null, toRPCResponse(payload, result)); }, // Called if the Promise of the 'handler' fails (error) => { callback(error, toRPCResponse(payload, null, error)); }) .catch((error) => { // Called if the 'callback' fails debugRpcCallback('Callback for handling the JsonRpcResponse fails'); debugRpcCallback('%O', error); }); } catch (error) { // Called if the handler fails before making the promise callback(error instanceof Error ? error : null); } } exports.rpcCallHandler = rpcCallHandler; // Ported from: https://github.com/MetaMask/provider-engine/blob/master/util/random-id.js function getRandomId() { const extraDigits = 3; const baseTen = 10; // 13 time digits const datePart = new Date().getTime() * Math.pow(baseTen, extraDigits); // 3 random digits const extraPart = Math.floor(Math.random() * Math.pow(baseTen, extraDigits)); // 16 digits return datePart + extraPart; } exports.getRandomId = getRandomId; function toRPCResponse(payload, result, error) { const response = { id: Number(payload.id), jsonrpc: payload.jsonrpc, result, }; if (error != null) { response.error = { message: error.message || error.toString(), code: -32000, }; } return response; } class HttpRpcCaller { constructor(httpProvider, jsonrpcVersion = '2.0') { this.httpProvider = httpProvider; this.jsonrpcVersion = jsonrpcVersion; } call(method, params) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const payload = { id: getRandomId(), jsonrpc: this.jsonrpcVersion, method, params, }; this.send(payload, (err, response) => { if (err != null || !response) { reject(err); } else { resolve(response); } }); }); }); } send(payload, callback) { debugRpcPayload('%O', payload); const decoratedCallback = (error, result) => { let err = null; // error could be false if (error) { err = error; } debugRpcResponse('%O', result); // The provider send call will not provide an error to the callback if // the result itself specifies an error. Here, we extract the error in the // result. if (result && result.error != null && typeof result.error !== 'string' && result.error.message != null) { err = new Error(result.error.message); } callback(err, result); }; if (this.httpProvider && typeof this.httpProvider !== 'string') { this.httpProvider.send(payload, decoratedCallback); } } } exports.HttpRpcCaller = HttpRpcCaller; //# sourceMappingURL=rpc-caller.js.map