UNPKG

@sphereon/ssi-sdk.agent-config

Version:

136 lines 5.24 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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.getConfig = void 0; exports.createAgentFromConfig = createAgentFromConfig; exports.createAgent = createAgent; exports.getAgent = getAgent; const objectCreator_1 = require("./objectCreator"); const yaml_1 = __importDefault(require("yaml")); /** * Creates a Veramo agent from a config object containing an `/agent` pointer. * @param config - The configuration object * * @see {@link https://veramo.io/docs/veramo_agent/configuration_internals | Configuration Internals} for details on * the configuration options. * * @beta - This API may change without a major version bump */ function createAgentFromConfig(config) { return __awaiter(this, void 0, void 0, function* () { // @ts-ignore const { agent } = yield (0, objectCreator_1.createObjects)(config, { agent: '/agent' }); return agent; }); } /** * Helper function to create a new instance of the {@link Agent} class with correct type * * @remarks * Use {@link TAgent} to configure agent type (list of available methods) for autocomplete in IDE * * @example * ```typescript * import { createAgent, IResolver, IMessageHandler } from '@veramo/core' * import { AgentRestClient } from '@veramo/remote-client' * import { CredentialIssuer, ICredentialIssuer } from '@veramo/credential-w3c' * const agent = createAgent<IResolver & IMessageHandler & ICredentialIssuer>({ * plugins: [ * new CredentialIssuer(), * new AgentRestClient({ * url: 'http://localhost:3002/agent', * enabledMethods: [ * 'resolveDid', * 'handleMessage', * ], * }), * ], * }) * ``` * @param options - Agent configuration options * @returns configured agent * @public */ function createAgent(options) { return __awaiter(this, void 0, void 0, function* () { //@ts-ignore return new Agent(options); }); } /** * Parses a yaml config file and returns a config object * @param filePath */ const getConfig = (filePath) => __awaiter(void 0, void 0, void 0, function* () { let fileContent; // read file async try { const fs = yield Promise.resolve().then(() => __importStar(require(/* webpackIgnore: true */ 'fs'))); fileContent = yield fs.promises.readFile(filePath, 'utf8'); } catch (e) { console.log('Config file not found: ' + filePath); console.log('Use "veramo config create" to create one'); process.exit(1); } let config; try { config = yaml_1.default.parse(fileContent, { prettyErrors: true }); } catch (e) { console.error(`Unable to parse config file: ${e.message} ${e.linePos}`); process.exit(1); } if ((config === null || config === void 0 ? void 0 : config.version) != 3) { console.error('Unsupported configuration file version:', config.version); process.exit(1); } return config; }); exports.getConfig = getConfig; function getAgent(fileName) { return __awaiter(this, void 0, void 0, function* () { try { return yield createAgentFromConfig(yield (0, exports.getConfig)(fileName)); } catch (e) { console.log('Unable to create agent from ' + fileName + '.', e.message); process.exit(1); } }); } //# sourceMappingURL=agentCreator.js.map