Skip to content

Mingyang-Li/mattr-vii

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ MATTR VII Client

A non-official Node.js client for MATTR VII API platform

πŸ“š Usage:

Install the SDK

yarn add mattr-vii-client
// or
npm install mattr-vii-client

πŸ›’ Import the SDK

import { MattrViiClient, ApiTypes } from 'mattr-vii-client'

πŸ”₯ Initialise the client

const client = new MattrViiClient({
  tenantSubdomain: process.env.MATTR_TENANT_URL,
  authToken: process.env.MATTR_AUTH_TOKEN,
});

β€πŸ« Start using the SDK following these guidelines:

Platform Core:

🐈 Misc: Using the SDK in NestJS

// mattr.service.ts
@Injectable()
export class MattrService extends MattrViiClient {
  super({
    tenantSubdomain: process.env.MATTR_TENANT_URL,
    authToken: process.env.MATTR_AUTH_TOKEN,
  })
}

// user.controller.ts
@Controller('user')
export class UserController {
  constructor(
    private readonly mattrService: MattrService,
    private readonly prismaService: PrismaService,
  ) {};

  @Post('create')
  public async createUser(@Body() args: CreateUserBody) {
    const body: ApiTypes.PlatformCore.DIDs.CreateDidReqBody = {
      method: 'key',
      options: {
        keyType: 'ed25519',
      },
    };
    const did = await this.mattrService.PlatformCore.DIDs.createDid(body);
    return await this.prismaService.user.create({
      ...args,
      did: did.did,
    })
  }
}

⏰ Use-case example - Then VS now

/*
Example use-case:
=== Send a verifiable credential to a Digital Wallet ===

Main steps:
1. Create a DID
2. Create a Credential using the DID created
3. Sign a message using the Credential you just created
4. Encrypt the signed message
5. Send the message to a wallet
*/

// πŸ’© Before πŸ’©
// ❌ Raw API calls
const url = `https://${process.env.MATTR_TENANT}.vii.mattr.global/core/v1`;
const createDid = async () => {
  const res = await fetch(`${url}/dids`, {
    method: "post",
    headers: {
      "Content-type": "application/json",
      Authorization: 'Bearer <YOUR_JWT_HERE>',
    },
    // ❌ You have no idea if the body you passed in is what the endpoint expects
    body: JSON.stringify({
      method: 'key',
      options: {keyType: 'ed25519'},
    }),
  });
  return res.json();
  // ❌ You have no clue how big & nested your response will be...
}


// βœ… Now with our SDK βœ…
/* main.ts */
import { MattrViiClient, ApiTypes } from 'mattr-vii-client';

// βœ… Initialise the SDK once and use it across your backend
// βœ… Minimal environment variables required
const client = new MattrViiClient({
  tenantSubdomain: process.env.MATTR_TENANT_URL,
  authToken: process.env.MATTR_AUTH_TOKEN,
});

export const createDid = async () => {
  // βœ… Tells you exactly the shape of your request body
  const body: ApiTypes.PlatformCore.DIDs.CreateDidReqBody = {
    method: 'key',
    options: {
      keyType: 'ed25519',
    },
  };
  // βœ… Auto-completion helps you figuring out which method to call
  // βœ… Enforcing you to pass in the correct body for each request
  // βœ… Has a response type
  return await client.PlatformCore.DIDs.createDid({ body });
}