diff --git a/package.json b/package.json index 94cdbb2..b211d3a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "jest", - "dev": "ts-node ./index.ts", + "dev": "ts-node src/index.ts", "lint": "eslint .", "lint:fix": "eslint . --fix", "format": "prettier --write ." diff --git a/src/Gateway.ts b/src/Gateway.ts index ef0b6d8..4755570 100644 --- a/src/Gateway.ts +++ b/src/Gateway.ts @@ -1,5 +1,6 @@ import { getMeshSDK, Sdk } from '../.mesh'; import { Organization } from './organization/organization'; +import { Auth } from './auth/auth'; import { PDA } from './pda/pda'; import { DataRequestTemplate } from './dataRequestsTemplate/dataRequestsTemplate'; @@ -8,6 +9,7 @@ export class Gateway { public pda: PDA; public dataRequestTemplate: DataRequestTemplate; public organization: Organization; + public auth: Auth; constructor({ apiKey, token }: { apiKey: string; token: string }) { if (!apiKey && !token) throw new Error('No token found'); @@ -18,5 +20,6 @@ export class Gateway { this.pda = new PDA(this.sdk); this.dataRequestTemplate = new DataRequestTemplate(this.sdk); this.organization = new Organization(this.sdk); + this.auth = new Auth(this.sdk); } } diff --git a/src/auth/auth.ts b/src/auth/auth.ts new file mode 100644 index 0000000..d4b7f16 --- /dev/null +++ b/src/auth/auth.ts @@ -0,0 +1,189 @@ +import { AddWalletConfirmationInput, Sdk } from '../../.mesh'; +import { AuthType, Chain } from '../types'; +import { errorHandler } from '../utils/errorHandler'; + +export class Auth { + private sdk: Sdk; + + constructor(sdk: Sdk) { + this.sdk = sdk; + } + + /** + * The function checks the availability of a username by making a async query to the SDK. + * @param {string} username - The `username` parameter is a string that represents the username that + * needs to be checked for availability. + * @returns the result of the `checkUsernameAvailability` method, is a boolean + */ + async checkUsernameAvailability(username: string) { + try { + return (await this.sdk.checkUsernameAvailability_query({ username })) + .checkUsernameAvailability; + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + /** + * The function `addEmail` is an asynchronous function that adds an email to a mutation using the + * SDK. + * @param {string} email - The `email` parameter is a string that represents the email address that + * needs to be added. + * @returns The addEmail function is returning the result code and email + */ + async addEmail(email: string) { + try { + return (await this.sdk.addEmail_mutation({ input: { email } })).addEmail; + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + /** + * The function `addEmailConfirmation` takes an email and code as input, and calls a mutation to add + * email confirmation. + * @param {string} email: a string representing the email address to be confirmed + * @param {number} code : a 6 digit code representing that was sent + * @returns the result of the `addEmailConfirmation` method call, is the logged in user. + */ + async addEmailConfirmation(email: string, code: number) { + try { + return ( + await this.sdk.addEmailConfirmation_mutation({ + input: { code, email }, + }) + ).addEmailConfirmation; + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + /** + * The function `addWallet` takes an wallet and optional chain as input. + * @param {string} wallet: a string representing the wallet + * @param {Chain} chain : a chain optional of type Chain + * @returns the result of the `addWallet` method call, is a message which will be used to confirm wallet. + */ + async addWallet(wallet: string, chain?: Chain) { + try { + return (await this.sdk.addWallet_mutation({ input: { wallet, chain } })) + .addWallet; + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + /** + * The function `addWalletConfirmation` takes an walletConfirmationInput input, and calls a mutation to add + * wallet confirmation. + * @param {AddWalletConfirmationInput} walletConfirmationInput: walletConfirmationInput of type AddWalletConfirmationInput + * @returns the result of the `addWalletConfirmation` method call, is the logged in user. + */ + async addWalletConfirmation( + walletConfirmationInput: AddWalletConfirmationInput, + ) { + try { + return await this.sdk.addWalletConfirmation_mutation({ + input: walletConfirmationInput, + }); + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + /** + * The function `createWalletNounce` takes an wallet and optional chain as input + * @param {string} wallet: a string representing the wallet + * @param {Chain} chain : a chain optional of type Chain + * @returns the result of the `createWalletNounce` method call, is a message which will be used to confirm wallet. + */ + async createWalletNonce(wallet: string, chain?: Chain) { + try { + return ( + await this.sdk.createWalletNonce_mutation({ input: { wallet, chain } }) + ).createWalletNonce; + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + /** + * The function `deleteAccount` takes an id to soft delete the account + * @param {string} id: a string representing the user id + * @returns the result of the `deleteAccount` method call,returning the boolean | undefined if user not found + */ + async deleteAccount(id: string) { + try { + return (await this.sdk.deleteAccount_mutation({ id })).deleteAccount; + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + /** + * The function `loginEmail` takes an email and code as input verifies it and gives user details + * @param {string} email: a string representing the email + * @param {number} code: a number representing the verification code + * @returns the result of the `loginEmail` method call,returning the user if code is correct + */ + async loginEmail(email: string, code: number) { + try { + return (await this.sdk.loginEmail_mutation({ input: { email, code } })) + .loginEmail; + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + /** + * The function `loginWallet` takes an wallet and signature as input verifies it and gives user details + * @param {string} wallet: a string representing the email + * @param {string} signature: a string representing the signature generated + * @returns the result of the `loginWallet` method call,returning the user if signature is correct + */ + async loginWallet(wallet: string, signature: string) { + try { + return ( + await this.sdk.loginWallet_mutation({ + input: { wallet, signature }, + }) + ).loginWallet; + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + /** + * The function `refreshToken` takes an existing refresh token to create a new one + * @param {string} existingRefreshToken: a string representing the existing refresh token + * @returns the result of the `refreshToken` method call,returning the new refresh token and user + */ + async refreshToken(existingRefreshToken: string) { + try { + return ( + await this.sdk.refreshToken_mutation({ + input: { refresh_token: existingRefreshToken }, + }) + ).refreshToken; + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + /** + * The function unregisterAuthMethod is an asynchronous function that takes in a JSON object and an + * AuthType, and attempts to unregister an authentication method using the SDK. + * @param {string} data: a string representing either email or wallet + * @param {AuthType} type: a AuthType representing the type like Wallet, Email and so on + * @returns the result of the `this.sdk.unregisterAuthMethod_mutation` method, which is awaited. + */ + async unregisterAuthMethod(data: string, type: AuthType) { + try { + return await this.sdk.unregisterAuthMethod_mutation({ + input: { data, type }, + }); + } catch (error) { + throw new Error(errorHandler(error)); + } + } +} diff --git a/src/types.ts b/src/types.ts index bc021e3..610d2e2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -32,3 +32,15 @@ export enum OrganizationRole { Member = 'Member', Owner = 'Owner', } + +export enum AuthType { + 'EMAIL' = 'EMAIL', + 'GOOGLE' = 'GOOGLE', + 'HOT_WALLET' = 'HOT_WALLET', + 'WALLET' = 'WALLET', +} + +export enum Chain { + EVM = 'EVM', + SOL = 'SOL', +} diff --git a/test/auth.test.ts b/test/auth.test.ts new file mode 100644 index 0000000..989c8d0 --- /dev/null +++ b/test/auth.test.ts @@ -0,0 +1,56 @@ +import dotenv from 'dotenv'; +import { Gateway } from '../src/Gateway'; + +dotenv.config(); +let api: Gateway; +const DEFAULT_TIMEOUT = 100000; + +beforeAll(() => { + api = new Gateway({ + apiKey: process.env.API_KEY!, + token: process.env.BEARER_TOKEN!, + }); +}); + +describe('auth test', () => { + it( + 'check username availability', + async () => { + const result = await api.auth.checkUsernameAvailability('sid'); + expect(result).toEqual(false); + }, + DEFAULT_TIMEOUT, + ); + + it( + 'add email', + async () => { + const { email, code } = await api.auth.addEmail('sid@test.com'); + expect(email).toBe('sid@test.com'); + expect(code).toBeDefined(); + }, + DEFAULT_TIMEOUT, + ); + + it( + 'add wallet', + async () => { + const { message } = await api.auth.addWallet( + '0xD73e46DeBD3958F2706903A6D3644C570285EC1F', + ); + expect(message.length).toBeDefined(); + }, + DEFAULT_TIMEOUT, + ); + + it( + 'create wallet nounce', + async () => { + const { message } = await api.auth.createWalletNonce( + '0xCf084430Fc2CfAd8E81716aEdeBBE4458866D239', + ); + expect(message.length).toBeDefined(); + }, + DEFAULT_TIMEOUT, + ); +});