UNPKG

@mobile-wallet-protocol/client

Version:
67 lines (66 loc) 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchRPCRequest = fetchRPCRequest; exports.appendMWPResponsePath = appendMWPResponsePath; exports.checkErrorForInvalidRequestArgs = checkErrorForInvalidRequestArgs; const version_1 = require("../../version"); const constants_1 = require("../constants"); const error_1 = require("../error"); async function fetchRPCRequest(request, rpcUrl) { const requestBody = Object.assign(Object.assign({}, request), { jsonrpc: '2.0', id: crypto.randomUUID() }); const res = await fetch(rpcUrl, { method: 'POST', body: JSON.stringify(requestBody), mode: 'cors', headers: { 'Content-Type': 'application/json', 'X-Cbw-Sdk-Version': version_1.LIB_VERSION, 'X-Cbw-Sdk-Platform': '@mobile-wallet-protocol/client', }, }); const { result, error } = await res.json(); if (error) throw error; return result; } function appendMWPResponsePath(urlString) { const url = new URL(urlString); url.pathname += url.pathname.endsWith('/') ? constants_1.MWP_RESPONSE_PATH : `/${constants_1.MWP_RESPONSE_PATH}`; return url.toString(); } /** * Validates the arguments for an invalid request and returns an error if any validation fails. * Valid request args are defined here: https://eips.ethereum.org/EIPS/eip-1193#request * @param args The request arguments to validate. * @returns An error object if the arguments are invalid, otherwise undefined. */ function checkErrorForInvalidRequestArgs(args) { if (!args || typeof args !== 'object' || Array.isArray(args)) { throw error_1.standardErrors.rpc.invalidParams({ message: 'Expected a single, non-array, object argument.', data: args, }); } const { method, params } = args; if (typeof method !== 'string' || method.length === 0) { throw error_1.standardErrors.rpc.invalidParams({ message: "'args.method' must be a non-empty string.", data: args, }); } if (params !== undefined && !Array.isArray(params) && (typeof params !== 'object' || params === null)) { throw error_1.standardErrors.rpc.invalidParams({ message: "'args.params' must be an object or array if provided.", data: args, }); } switch (method) { case 'eth_sign': case 'eth_signTypedData_v2': case 'eth_subscribe': case 'eth_unsubscribe': throw error_1.standardErrors.provider.unsupportedMethod(); } }