-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathaccessListHandler.ts
More file actions
84 lines (80 loc) · 2.72 KB
/
accessListHandler.ts
File metadata and controls
84 lines (80 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { CommandHandler } from './handler.js'
import { P2PCommandResponse } from '../../../@types/OceanNode.js'
import {
GetAccessListCommand,
SearchAccessListCommand
} from '../../../@types/commands.js'
import { Readable } from 'stream'
import { isAddress } from 'ethers'
import {
ValidateParams,
validateCommandParameters
} from '../../httpRoutes/validateCommands.js'
import { CORE_LOGGER } from '../../../utils/logging/common.js'
export class GetAccessListHandler extends CommandHandler {
validate(command: GetAccessListCommand): ValidateParams {
return validateCommandParameters(command, ['chainId', 'contractAddress'])
}
async handle(task: GetAccessListCommand): Promise<P2PCommandResponse> {
const checks = await this.verifyParamsAndRateLimits(task)
if (checks.status.httpStatus !== 200 || checks.status.error !== null) {
return checks
}
try {
const db = await this.getOceanNode().getDatabase()
const doc = await db.accessList.retrieve(
Number(task.chainId),
String(task.contractAddress)
)
if (!doc) {
return {
stream: null,
status: { httpStatus: 404, error: 'AccessList not found' }
}
}
return {
stream: Readable.from(JSON.stringify(doc)),
status: { httpStatus: 200 }
}
} catch (error) {
CORE_LOGGER.error(`GetAccessListHandler error: ${error.message}`)
return {
stream: null,
status: { httpStatus: 500, error: 'Unknown error: ' + error.message }
}
}
}
}
export class SearchAccessListHandler extends CommandHandler {
validate(command: SearchAccessListCommand): ValidateParams {
return validateCommandParameters(command, ['wallet'])
}
async handle(task: SearchAccessListCommand): Promise<P2PCommandResponse> {
const checks = await this.verifyParamsAndRateLimits(task)
if (checks.status.httpStatus !== 200 || checks.status.error !== null) {
return checks
}
try {
const walletString = String(task.wallet)
if (!isAddress(walletString)) {
return {
stream: null,
status: { httpStatus: 400, error: 'Invalid wallet address' }
}
}
const db = await this.getOceanNode().getDatabase()
const chainId = task.chainId !== undefined ? Number(task.chainId) : undefined
const docs = await db.accessList.searchByWallet(walletString, chainId)
return {
stream: Readable.from(JSON.stringify(docs ?? [])),
status: { httpStatus: 200 }
}
} catch (error) {
CORE_LOGGER.error(`SearchAccessListHandler error: ${error.message}`)
return {
stream: null,
status: { httpStatus: 500, error: 'Unknown error: ' + error.message }
}
}
}
}