Skip to content
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
1 change: 1 addition & 0 deletions src/@types/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface GetP2PNetworkStatsCommand extends Command {}
export interface AdminCommand extends Command {
expiryTimestamp: number
signature: string
address?: string
}

export interface AdminCollectFeesHandlerResponse {
Expand Down
3 changes: 2 additions & 1 deletion src/components/core/admin/adminHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export abstract class AdminCommandHandler
}
const signatureValidation: CommonValidation = await validateAdminSignature(
command.expiryTimestamp,
command.signature
command.signature,
command.address
)
if (!signatureValidation.valid) {
return buildInvalidRequestMessage(
Expand Down
2 changes: 1 addition & 1 deletion src/components/core/utils/nonceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ async function validateNonceAndSignature(
}

// Smart account validation
async function isERC1271Valid(
export async function isERC1271Valid(
address: string,
hash: string | Uint8Array,
signature: string,
Expand Down
45 changes: 38 additions & 7 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,39 @@ import { getAccountsFromAccessList } from '../utils/credentials.js'
import { OceanNodeConfig } from '../@types/OceanNode.js'
import { LOG_LEVELS_STR } from './logging/Logger.js'
import { CommonValidation } from './validators.js'
import { isERC1271Valid } from '../components/core/utils/nonceHandler.js'

export async function validateAdminSignature(
expiryTimestamp: number,
signature: string
signature: string,
address?: string
): Promise<CommonValidation> {
const message = expiryTimestamp.toString()
const signerAddress = ethers.verifyMessage(message, signature)?.toLowerCase()
CORE_LOGGER.logMessage(`Resolved signer address: ${signerAddress}`)
let signerAddress

try {
const allowedAdmins: string[] = await getAdminAddresses()
console.log(`Allowed admins: ${allowedAdmins}`)
const config = await getConfiguration()
if (address) {
const hexMessage = ethers.hashMessage(message)
const firstChainId = Object.keys(config?.supportedNetworks || {})[0]
if (firstChainId) {
const provider = new ethers.JsonRpcProvider(
config.supportedNetworks[firstChainId].rpc
)

if (!(await isERC1271Valid(address, hexMessage, signature, provider))) {
return { valid: false, error: 'Invalid ERC1271 signature' }
}
signerAddress = address
} else {
return { valid: false, error: 'No network configured in node config' }
}
} else {
signerAddress = ethers.verifyMessage(message, signature)?.toLowerCase()
CORE_LOGGER.logMessage(`Resolved signer address: ${signerAddress}`)
}

const allowedAdmins: string[] = await getAdminAddresses(config)

if (allowedAdmins.length === 0) {
const errorMsg = "Allowed admins list is empty. Please add admins' addresses."
Expand Down Expand Up @@ -48,8 +71,16 @@ export async function validateAdminSignature(
}
}

export async function getAdminAddresses(): Promise<string[]> {
const config: OceanNodeConfig = await getConfiguration()
export async function getAdminAddresses(
existingConfig?: OceanNodeConfig
): Promise<string[]> {
let config: OceanNodeConfig
if (!existingConfig) {
config = await getConfiguration()
} else {
config = existingConfig
}

const validAddresses: string[] = []
if (config.allowedAdmins && config.allowedAdmins.length > 0) {
for (const admin of config.allowedAdmins) {
Expand Down
Loading