This repository contains the backend service for the Masumi Registry Service.
The goal of the Masumi Registry Service is to provide an easy to use and still performant service that can query the Cardano blockchain for registered agents and nodes. It currently supports a REST full API that provides filtering options for the registry and capabilities.
Furthermore it supports a API key system with different permissions and a crediting system to access the registry (enables registry as a service) and multiple blockchain networks and sources.
Registry entries (and de-registrations) are cached in a database and periodically updated in the background to increase performance and redundant queries.
Masumi Payment Service. Used to register and deregister agents and handle payments.
This section provides an overview of the key architectural patterns and technologies employed in this service:
- Onion Architecture: Employed as the core architectural design pattern
- Express-Zod-Api: Utilized as the framework for implementing our RESTful API with Swagger UI. The library uses:
- Prisma: Implemented as the ORM to interact with our PostgreSQL database
- DOTENV: Incorporated to securely load environment variables from
.env
files - Zod-to-OpenAPI: Used to generate the OpenAPI schema from the Zod schemas
- Blockfrost: Used to interact with the Cardano blockchain
- Jest: Used as the testing framework
- Docker: Used to containerize the application for production
- Install Node.js
- Clone this repository, and using a terminal navigate to its directory.
- Run
yarn
ornpm install
to install the dependencies. - Configure the environment variables by copying the
.env.example
file to.env
or.env.local
and setup the variables- DATABASE_URL: Please provide the endpoint for a PostgreSQL database to be used
- PORT: The port to run the server on (default is 3001)
- UPDATE_CARDANO_REGISTRY_INTERVAL: The interval to update the cardano registry as a cron string
- UPDATE_CARDANO_DEREGISTER_INTERVAL: The interval to update the cardano deregistered entries as a cron string
- In case you need to apply migrations to the database run
yarn prisma:migrate
ornpm prisma:migrate
otherwise runyarn prisma:generate
ornpm prisma:generate
to generate the prisma client (only works after installing the dependencies via step 3) - In case the database is not yet seeded (and or migrated) please also setup the following variables:
- BLOCKFROST_API_KEY: An API Key from https://blockfrost.io/ for the correct blockchain network, you can create this fro free
- REGISTRY_SOURCE_NETWORK: PREPROD Currently only supports the PREPROD Cardano network
- REGISTRY_SOURCE_IDENTIFIER_CARDANO: The identifier of the cardano registry source, this is the name of the policy used
- ADMIN_KEY: The key of the admin user, this key will have all permissions and can create new api_keys
- In case you want to seed the database now run
yarn prisma:seed
ornpm prisma:seed
- Copy the contents of the
.env.example
file to a.env
next to it, and edit it with your values. - Run
yarn build
ornpm build
to build the files. - Run
yarn start
ornpm start
to start the application.
- You can run
yarn dev
ornpm dev
to combine the 2 steps above, while listening to changes and restarting automatically.
To verify that the application is working correctly, point your browser to http://localhost:3001/api/health - you should see a response with one books in JSON format.
You can see a OpenAPI (Swagger) definition of the REST API at http://localhost:3001/docs/. This interface also allows you to interact with the API.
-
Please ensure to follow the setup if you have not migrated and/or seeded the database, also ensure to setup the environment variables, described in the previous sections (by default it tries to copy the
.env
file and use it) -
Build:
docker build -t masumi-registry-service .
-
Run:
docker run -d -p 3001:3001 masumi-registry-service
Replacing
masumi-registry-service
with the image name, and3001:3001
with thehost:container
ports configured in the environment variables. -
Congratulations, you have now a running production instance of the Masumi Registry Service!
Currently you can configure various parameters via the swagger playground.
- Installing the Eslint (
dbaeumer.vscode-eslint
) and Prettier - Code formatter (esbenp.prettier-vscode
) extensions is recommended.
- Run
yarn lint
ornpm lint
to lint the code. - Run
yarn format
ornpm format
to format the code.
This project uses Jest as the testing framework. Here's how you can run tests:
- Run
yarn test
ornpm test
to execute all tests. - Run
yarn test:watch
ornpm test:watch
to run tests in watch mode, which will re-run tests on file changes. - Run
yarn test:coverage
ornpm test:coverage
to see the test coverage report.
Tests are located in the src
directory, alongside the files they are testing. Test files should follow the naming convention of *.spec.ts
or *.test.ts
.
/src
/routes
/services
/repositories
/utils
The source folder contains sub-folders that arrange the application into logical layers
-
routes:
This is the adapter layer of architecture. It defines the HTTP requests structures from and to the external world and the services layers. -
services
: The service layer coordinates high-level activities such as creation of domain objects and asking them to perform tasks requested by the external world. It interacts with the repository layer to save and restore objects. -
repositories
: The repository layer is responsible for persisting domain objects and performing CRUD operations on them. We use SQL to persist the changes -
The
utils
folder contains useful utilities and helpers.
- REST API to query and filter the registry
- REST API to query available capabilities in the registry
- Swagger UI as API documentation
- API Key system with different permissions and crediting system to access the registry (enables registry as a service)
- Support for multiple blockchain networks and registry sources
- Caching of registry entries
- Periodic updating of the registry entries
- Admin UI to manage the registry