Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eas sdk #10

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npx lint-staged
npx lint - staged
4 changes: 2 additions & 2 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
npm test
npx lint-staged
npm test
npx lint - staged
6 changes: 3 additions & 3 deletions src/auth-siwe/dto/verify-siwe.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsString, IsNotEmpty, IsNumber, IsIn } from 'class-validator'
import { SignMessageReturnType, Hex } from 'viem'
import { SUPPORTED_CHAIN_IDS } from '../../shared/constants/chain.constants'
import { SUPPORTED_CHAINS } from '../../shared/constants/chain.constants'

export class VerifySiweDto {
@ApiProperty({
Expand All @@ -24,10 +24,10 @@ export class VerifySiweDto {
description: 'Chain Id',
example: '1',
required: true,
enum: SUPPORTED_CHAIN_IDS,
enum: SUPPORTED_CHAINS,
})
@IsNumber()
@IsNotEmpty()
@IsIn(SUPPORTED_CHAIN_IDS)
@IsIn(SUPPORTED_CHAINS)
readonly chainId: number
}
4 changes: 1 addition & 3 deletions src/auth-siwe/siwe.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ describe('SiweService', () => {
let publicClientMock: { verifySiweMessage: jest.Mock }
let loggerMock: PinoLogger

beforeEach(async () => {
beforeAll(async () => {
publicClientMock = {
verifySiweMessage: jest.fn(),
}

loggerMock = { error: jest.fn() } as unknown as PinoLogger

const module: TestingModule = await Test.createTestingModule({
imports: [LoggerModule.forRoot()],
providers: [
Expand Down
7 changes: 1 addition & 6 deletions src/auth-siwe/siwe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ export class SiweService {
throw new Error()
}
} catch (error) {
this.logger.error(
{
error,
},
`Siwe Verification Failed`
)
this.logger.error(error, `Siwe Verification Failed`)
throw new HttpException(
`${AUTH_PROVIDERS.SIWE} verification Failed`,
HttpStatus.BAD_REQUEST
Expand Down
36 changes: 21 additions & 15 deletions src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,35 @@ import { ConfigService } from '@nestjs/config'
import * as moment from 'moment'
import { JwtPayload } from './types/jwt-payload.type'
import * as jwt from 'jsonwebtoken'
import { PinoLogger, LoggerModule } from 'nestjs-pino'
import { BadRequestException } from '@nestjs/common'

const mockPublicKey =
'0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef'
const mockJwtSecret = 'jwtSecret'
const mockJwtExpiresIn = '60'

const mockConfigService = {
get: jest.fn((key: string) => {
if (key === 'wallet.publicKey') return mockPublicKey
if (key === 'jwt.secret') return mockJwtSecret
if (key === 'jwt.expiresIn') return mockJwtExpiresIn
}),
}

describe('AuthService', () => {
let service: AuthService
const mockPublicKey =
'0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef'
const mockJwtSecret = 'jwtSecret'
const mockJwtExpiresIn = '60'
let loggerMock: PinoLogger

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [LoggerModule.forRoot()],
providers: [
AuthService,
JwtService,
{
provide: ConfigService,
useValue: {
get: jest.fn((key: string) => {
if (key === 'wallet.publicKey') return mockPublicKey
if (key === 'jwt.secret') return mockJwtSecret
if (key === 'jwt.expiresIn') return mockJwtExpiresIn
}),
},
},
{ provide: ConfigService, useValue: mockConfigService },
{ provide: ConfigService, useValue: mockConfigService },
{ provide: PinoLogger, useValue: loggerMock },
],
}).compile()

Expand Down Expand Up @@ -76,7 +82,7 @@ describe('AuthService', () => {
it('should return null if token is invalid', async () => {
await expect(
service.validateToken('invalid.token.here')
).rejects.toThrow(jwt.JsonWebTokenError)
).rejects.toThrow(BadRequestException)
})
})
})
24 changes: 19 additions & 5 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Injectable } from '@nestjs/common'
import { Injectable, BadRequestException } from '@nestjs/common'
import * as jwt from 'jsonwebtoken'
import { JwtPayload } from './types/jwt-payload.type'
import { ConfigService } from '@nestjs/config'
import { PinoLogger, InjectPinoLogger } from 'nestjs-pino'
import * as moment from 'moment'

@Injectable()
export class AuthService {
constructor(private readonly configService: ConfigService) {}
constructor(
private readonly configService: ConfigService,
@InjectPinoLogger(AuthService.name)
private readonly logger: PinoLogger
) {}

async signPayload(payload: JwtPayload): Promise<string> {
return jwt.sign(payload, this.configService.get<string>('jwt.secret'), {
Expand All @@ -15,9 +20,18 @@ export class AuthService {
}

async validateToken(token: string): Promise<JwtPayload> {
return jwt.verify(token, this.configService.get<string>('jwt.secret'), {
algorithms: ['HS256'],
}) as JwtPayload
try {
return jwt.verify(
token,
this.configService.get<string>('jwt.secret'),
{
algorithms: ['HS256'],
}
) as JwtPayload
} catch (error) {
this.logger.error(error, `Failed to validtae token`)
throw new BadRequestException(error.message)
}
}

async generateJwt(identifier: string, provider: string): Promise<string> {
Expand Down
14 changes: 2 additions & 12 deletions src/auth/oAuth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ export class OAuthService {
return response.data
} catch (error) {
this.logger.error(
{
message: error.message,
name: error.name,
stack: error.stack,
response: error.response?.data,
},
error,
`Failed to exchange ${provider} code for token`
)
throw new BadRequestException(
Expand All @@ -112,12 +107,7 @@ export class OAuthService {
return response.data
} catch (error) {
this.logger.error(
{
message: error.message,
name: error.name,
stack: error.stack,
response: error.response?.data,
},
error,
`Failed to retrieve user information from ${provider}`
)
throw new BadRequestException(
Expand Down
4 changes: 1 addition & 3 deletions src/eas/constants/attestation.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,4 @@ export const REVOKE_TYPE = [
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint64' },
]
export const SCHEMA_TYPES = parseAbiParameters(
'bytes32 key, string provider, string secret'
)
export const SCHEMA_TYPES = 'bytes32 key, string provider, string secret'
27 changes: 15 additions & 12 deletions src/eas/eas.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { Test, TestingModule } from '@nestjs/testing'
import { EasService } from './eas.service'
import { ViemUtilsService } from '../utils/viem.utils.service'
import { ConfigService } from '@nestjs/config'
import { generatePrivateKey } from 'viem/accounts'
// import { EAS_SEPOLIA_CONTRACT_ADDRESS } from './constants/sepolia'
// import { sepolia } from 'viem/chains'
import { PinoLogger, LoggerModule } from 'nestjs-pino'
import { EthersUtilsService } from '../utils/ethers.utils.service'
const mockPrivateKey = generatePrivateKey()

const mockConfigService = {
get: jest.fn((key: string) => {
if (key === 'wallet.privateKey') return mockPrivateKey
}),
}

describe('EasService', () => {
let service: EasService
const mockPrivateKey = generatePrivateKey()
let loggerMock: PinoLogger

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [LoggerModule.forRoot()],
providers: [
EasService,
ViemUtilsService,
{
provide: ConfigService,
useValue: {
get: jest.fn((key: string) => {
if (key === 'wallet.privateKey')
return mockPrivateKey
}),
},
},
EthersUtilsService,
{ provide: ConfigService, useValue: mockConfigService },
{ provide: PinoLogger, useValue: loggerMock },
],
}).compile()

Expand Down
Loading
Loading