Skip to content

Commit 8c882f0

Browse files
Merge pull request #10 from TogetherCrew/eas-sdk
Eas sdk
2 parents a7d300b + cbe708a commit 8c882f0

24 files changed

+586
-489
lines changed

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
npx lint-staged
1+
npx lint - staged

.husky/pre-push

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
npm test
2-
npx lint-staged
1+
npm test
2+
npx lint - staged

src/auth-siwe/dto/verify-siwe.dto.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ApiProperty } from '@nestjs/swagger'
22
import { IsString, IsNotEmpty, IsNumber, IsIn } from 'class-validator'
33
import { SignMessageReturnType, Hex } from 'viem'
4-
import { SUPPORTED_CHAIN_IDS } from '../../shared/constants/chain.constants'
4+
import { SUPPORTED_CHAINS } from '../../shared/constants/chain.constants'
55

66
export class VerifySiweDto {
77
@ApiProperty({
@@ -24,10 +24,10 @@ export class VerifySiweDto {
2424
description: 'Chain Id',
2525
example: '1',
2626
required: true,
27-
enum: SUPPORTED_CHAIN_IDS,
27+
enum: SUPPORTED_CHAINS,
2828
})
2929
@IsNumber()
3030
@IsNotEmpty()
31-
@IsIn(SUPPORTED_CHAIN_IDS)
31+
@IsIn(SUPPORTED_CHAINS)
3232
readonly chainId: number
3333
}

src/auth-siwe/siwe.service.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ describe('SiweService', () => {
99
let publicClientMock: { verifySiweMessage: jest.Mock }
1010
let loggerMock: PinoLogger
1111

12-
beforeEach(async () => {
12+
beforeAll(async () => {
1313
publicClientMock = {
1414
verifySiweMessage: jest.fn(),
1515
}
1616

17-
loggerMock = { error: jest.fn() } as unknown as PinoLogger
18-
1917
const module: TestingModule = await Test.createTestingModule({
2018
imports: [LoggerModule.forRoot()],
2119
providers: [

src/auth-siwe/siwe.service.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ export class SiweService {
2828
throw new Error()
2929
}
3030
} catch (error) {
31-
this.logger.error(
32-
{
33-
error,
34-
},
35-
`Siwe Verification Failed`
36-
)
31+
this.logger.error(error, `Siwe Verification Failed`)
3732
throw new HttpException(
3833
`${AUTH_PROVIDERS.SIWE} verification Failed`,
3934
HttpStatus.BAD_REQUEST

src/auth/auth.service.spec.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,35 @@ import { ConfigService } from '@nestjs/config'
66
import * as moment from 'moment'
77
import { JwtPayload } from './types/jwt-payload.type'
88
import * as jwt from 'jsonwebtoken'
9+
import { PinoLogger, LoggerModule } from 'nestjs-pino'
10+
import { BadRequestException } from '@nestjs/common'
11+
12+
const mockPublicKey =
13+
'0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef'
14+
const mockJwtSecret = 'jwtSecret'
15+
const mockJwtExpiresIn = '60'
16+
17+
const mockConfigService = {
18+
get: jest.fn((key: string) => {
19+
if (key === 'wallet.publicKey') return mockPublicKey
20+
if (key === 'jwt.secret') return mockJwtSecret
21+
if (key === 'jwt.expiresIn') return mockJwtExpiresIn
22+
}),
23+
}
924

1025
describe('AuthService', () => {
1126
let service: AuthService
12-
const mockPublicKey =
13-
'0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef'
14-
const mockJwtSecret = 'jwtSecret'
15-
const mockJwtExpiresIn = '60'
27+
let loggerMock: PinoLogger
1628

1729
beforeAll(async () => {
1830
const module: TestingModule = await Test.createTestingModule({
31+
imports: [LoggerModule.forRoot()],
1932
providers: [
2033
AuthService,
2134
JwtService,
22-
{
23-
provide: ConfigService,
24-
useValue: {
25-
get: jest.fn((key: string) => {
26-
if (key === 'wallet.publicKey') return mockPublicKey
27-
if (key === 'jwt.secret') return mockJwtSecret
28-
if (key === 'jwt.expiresIn') return mockJwtExpiresIn
29-
}),
30-
},
31-
},
35+
{ provide: ConfigService, useValue: mockConfigService },
36+
{ provide: ConfigService, useValue: mockConfigService },
37+
{ provide: PinoLogger, useValue: loggerMock },
3238
],
3339
}).compile()
3440

@@ -76,7 +82,7 @@ describe('AuthService', () => {
7682
it('should return null if token is invalid', async () => {
7783
await expect(
7884
service.validateToken('invalid.token.here')
79-
).rejects.toThrow(jwt.JsonWebTokenError)
85+
).rejects.toThrow(BadRequestException)
8086
})
8187
})
8288
})

src/auth/auth.service.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import { Injectable } from '@nestjs/common'
1+
import { Injectable, BadRequestException } from '@nestjs/common'
22
import * as jwt from 'jsonwebtoken'
33
import { JwtPayload } from './types/jwt-payload.type'
44
import { ConfigService } from '@nestjs/config'
5+
import { PinoLogger, InjectPinoLogger } from 'nestjs-pino'
56
import * as moment from 'moment'
67

78
@Injectable()
89
export class AuthService {
9-
constructor(private readonly configService: ConfigService) {}
10+
constructor(
11+
private readonly configService: ConfigService,
12+
@InjectPinoLogger(AuthService.name)
13+
private readonly logger: PinoLogger
14+
) {}
1015

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

1722
async validateToken(token: string): Promise<JwtPayload> {
18-
return jwt.verify(token, this.configService.get<string>('jwt.secret'), {
19-
algorithms: ['HS256'],
20-
}) as JwtPayload
23+
try {
24+
return jwt.verify(
25+
token,
26+
this.configService.get<string>('jwt.secret'),
27+
{
28+
algorithms: ['HS256'],
29+
}
30+
) as JwtPayload
31+
} catch (error) {
32+
this.logger.error(error, `Failed to validtae token`)
33+
throw new BadRequestException(error.message)
34+
}
2135
}
2236

2337
async generateJwt(identifier: string, provider: string): Promise<string> {

src/auth/oAuth.service.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,7 @@ export class OAuthService {
8383
return response.data
8484
} catch (error) {
8585
this.logger.error(
86-
{
87-
message: error.message,
88-
name: error.name,
89-
stack: error.stack,
90-
response: error.response?.data,
91-
},
86+
error,
9287
`Failed to exchange ${provider} code for token`
9388
)
9489
throw new BadRequestException(
@@ -112,12 +107,7 @@ export class OAuthService {
112107
return response.data
113108
} catch (error) {
114109
this.logger.error(
115-
{
116-
message: error.message,
117-
name: error.name,
118-
stack: error.stack,
119-
response: error.response?.data,
120-
},
110+
error,
121111
`Failed to retrieve user information from ${provider}`
122112
)
123113
throw new BadRequestException(

src/eas/constants/attestation.constants.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,4 @@ export const REVOKE_TYPE = [
2626
{ name: 'nonce', type: 'uint256' },
2727
{ name: 'deadline', type: 'uint64' },
2828
]
29-
export const SCHEMA_TYPES = parseAbiParameters(
30-
'bytes32 key, string provider, string secret'
31-
)
29+
export const SCHEMA_TYPES = 'bytes32 key, string provider, string secret'

src/eas/eas.service.spec.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import { Test, TestingModule } from '@nestjs/testing'
22
import { EasService } from './eas.service'
3-
import { ViemUtilsService } from '../utils/viem.utils.service'
43
import { ConfigService } from '@nestjs/config'
54
import { generatePrivateKey } from 'viem/accounts'
65
// import { EAS_SEPOLIA_CONTRACT_ADDRESS } from './constants/sepolia'
76
// import { sepolia } from 'viem/chains'
7+
import { PinoLogger, LoggerModule } from 'nestjs-pino'
8+
import { EthersUtilsService } from '../utils/ethers.utils.service'
9+
const mockPrivateKey = generatePrivateKey()
10+
11+
const mockConfigService = {
12+
get: jest.fn((key: string) => {
13+
if (key === 'wallet.privateKey') return mockPrivateKey
14+
}),
15+
}
816

917
describe('EasService', () => {
1018
let service: EasService
11-
const mockPrivateKey = generatePrivateKey()
19+
let loggerMock: PinoLogger
20+
1221
beforeAll(async () => {
1322
const module: TestingModule = await Test.createTestingModule({
23+
imports: [LoggerModule.forRoot()],
1424
providers: [
1525
EasService,
16-
ViemUtilsService,
17-
{
18-
provide: ConfigService,
19-
useValue: {
20-
get: jest.fn((key: string) => {
21-
if (key === 'wallet.privateKey')
22-
return mockPrivateKey
23-
}),
24-
},
25-
},
26+
EthersUtilsService,
27+
{ provide: ConfigService, useValue: mockConfigService },
28+
{ provide: PinoLogger, useValue: loggerMock },
2629
],
2730
}).compile()
2831

0 commit comments

Comments
 (0)