UNPKG

@sphereon/ssi-sdk-web3.headless-provider

Version:

99 lines 5.05 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createRpcServer = createRpcServer; exports.createServiceMethod = createServiceMethod; exports.createService = createService; const ssi_express_support_1 = require("@sphereon/ssi-express-support"); const express_1 = require("express"); const types_1 = require("./types"); function createRpcServer(provider, expressSupport, opts) { var _a, _b, _c; const express = expressSupport.express; const router = (0, express_1.Router)(); // const app = expressSupport.express // app.post(opts?.basePath ?? "/web3/rpc", (req, res, next) => {console.log(`${JSON.stringify(req.body, null,2)}`); next()} , rpcHandler(createService(provider))); const path = (_a = opts === null || opts === void 0 ? void 0 : opts.path) !== null && _a !== void 0 ? _a : '/web3/rpc'; console.log(`RPC server will use basePath ${(_b = opts === null || opts === void 0 ? void 0 : opts.basePath) !== null && _b !== void 0 ? _b : '/'} and path ${path}`); router.post(path, (req, res, next) => { var _a; console.log(`REQ ${(_a = req.body) === null || _a === void 0 ? void 0 : _a.method}:\r\n${JSON.stringify(req.body, null, 2)}\r\n===`); next(); }, (req, res, next) => __awaiter(this, void 0, void 0, function* () { var _a; try { const method = req.body.method; const params = req.body.params; const id = req.body.id; // todo: A Notification is a Request object without an "id" member. // A Request object that is a Notification signifies the Client's lack of interest in the corresponding Response object, // and as such no Response object needs to be returned to the client. The Server MUST NOT reply to a Notification, including those that are within a batch request. if (req.body.jsonrpc !== '2.0') { console.log('No valid JSON RPC call received', JSON.stringify(req.body)); return (0, ssi_express_support_1.sendErrorResponse)(res, 200, { id: req.body.id, jsonrpc: '2.0', error: 'No valid JSON RPC call received. No jsonrp version supplied', code: -32600, }); } else if (!id || !method) { console.log('No valid JSON RPC call received', JSON.stringify(req.body)); return (0, ssi_express_support_1.sendErrorResponse)(res, 200, { id: req.body.id, jsonrpc: '2.0', error: 'No valid JSON RPC call received', code: -32600, }); } const result = provider.request({ method, params }); provider.authorizeAll(); const respBody = { id, jsonrpc: '2.0', result: yield result }; res.json(respBody); console.log(`RESPONSE for ${method}:\r\n${JSON.stringify(respBody, null, 2)}`); } catch (error) { console.log(error.message); let msg = error.message; if (`body` in error) { msg = error.body; return (0, ssi_express_support_1.sendErrorResponse)(res, 200, msg); // res.json(error.body) } else { return (0, ssi_express_support_1.sendErrorResponse)(res, 200, { id: req.body.id, jsonrpc: '2.0', error: msg, code: (_a = error.code) !== null && _a !== void 0 ? _a : -32000, }); } } return next(); })); express.use((_c = opts === null || opts === void 0 ? void 0 : opts.basePath) !== null && _c !== void 0 ? _c : '', router); } function createServiceMethod(method, service, provider) { service[method] = (params) => __awaiter(this, void 0, void 0, function* () { // @ts-ignore const result = provider.request({ method, params }); provider.authorizeAll(); return yield result; }); } function createService(provider) { const service = {}; for (const method of Object.values(types_1.Web3Method)) { createServiceMethod(method, service, provider); } return service; } //# sourceMappingURL=rpc-server.js.map