@sphereon/ssi-sdk.ebsi-support
Version:
80 lines • 3.86 kB
JavaScript
;
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.callRpcMethod = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const functions_1 = require("../../functions");
const index_1 = require("../../index");
const functions_2 = require("../functions");
const types_1 = require("../types");
/**
* Allows to call 5 api methods of the EBSI RPC api
* - insertDidDocument
* - updateBaseDocument
* - addVerificationMethod
* - addVerificationMethodRelationship
* - sendSignedTransaction
* @function callRpcMethod
* @param {{ params: RPCParams[]; id: number; token: string; method: EbsiRpcMethod; apiOpts? ApiOpts }} args
*/
const callRpcMethod = (args) => __awaiter(void 0, void 0, void 0, function* () {
return callRpcMethodImpl(Object.assign(Object.assign({}, args), { retries: 10 }));
});
exports.callRpcMethod = callRpcMethod;
const callRpcMethodImpl = (args) => __awaiter(void 0, void 0, void 0, function* () {
const { params, rpcId, accessToken, rpcMethod, apiOpts, doNotThrowErrors = false, retries } = args;
const options = buildFetchOptions({ accessToken: accessToken, params, rpcId, rpcMethod });
index_1.logger.debug(`RPC call:\r\n ${JSON.stringify(options, null, 2)}`);
const rpcResponse = yield (yield (0, cross_fetch_1.default)((0, functions_2.ebsiGetRegistryAPIUrls)(Object.assign({}, apiOpts)).mutate, options)).json();
let result = rpcResponse.result;
index_1.logger.debug(`RPC RESPONSE:\r\n${JSON.stringify(result !== null && result !== void 0 ? result : rpcResponse.error)}`);
if (rpcResponse.error !== undefined && !doNotThrowErrors) {
index_1.logger.error(`RPC ERROR RESPONSE:`, rpcResponse);
if (rpcResponse.error.message.includes(`replacement fee too low`)) {
args.rpcId = (0, functions_2.randomRpcId)();
if (retries <= 0) {
throw Error(rpcResponse.error.message);
}
index_1.logger.warning(`Replacement fee too low error. Waiting 1 sec. Retries: ${retries}`);
yield (0, functions_1.wait)(1000);
return callRpcMethodImpl(Object.assign(Object.assign({}, args), { retries: retries - 1 }));
}
throw Error(rpcResponse.error.message);
}
return rpcResponse;
});
/**
* Builds the request body of the http request to EBSI RPC api
* @function buildFetchOptions
* @param {{ params: RPCParams[]; id: number; token: string; method: EbsiRpcMethod }} args
*/
const buildFetchOptions = (args) => {
const { params, rpcId, accessToken, rpcMethod } = args;
const fetchReq = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
jsonrpc: types_1.JSON_RPC_VERSION,
method: rpcMethod,
params: params,
id: rpcId,
}),
};
index_1.logger.debug(fetchReq);
return fetchReq;
};
//# sourceMappingURL=EbsiRPCService.js.map