Skip to content

Commit

Permalink
fixed call.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariacarmina committed Mar 8, 2024
1 parent 92bbb4a commit 40b2302
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { sha256, toUtf8Bytes, verifyMessage } from 'ethers'
import { HTTP_LOGGER } from './logging/common.js'
import { existsEnvironmentVariable } from './config.js'
import { PROTOCOL_COMMANDS } from './constants.js'
import { StatusHandler } from '../components/core/statusHandler.js'
import { streamToObject } from './util.js'
import { Readable } from 'stream'
import { OceanNode } from '../OceanNode.js'

export function validateSignature(expiryTimestamp: number, signature: string): boolean {
export async function validateSignature(
expiryTimestamp: number,
signature: string,
oceanNode: OceanNode
): Promise<boolean> {
try {
const message = sha256(toUtf8Bytes(expiryTimestamp.toString()))

const signerAddress = verifyMessage(message, signature).toLowerCase()
HTTP_LOGGER.logMessage(`Resolved signer address: ${signerAddress}`)

if (!existsEnvironmentVariable('ALLOWED_ADMINS')) {
HTTP_LOGGER.logMessage(`Missing env var for ALLOWED_ADMINS`)
const statusCommand = {
command: PROTOCOL_COMMANDS.STATUS
}
const response = await new StatusHandler(oceanNode).handle(statusCommand)
const status = await streamToObject(response.stream as Readable)
const currentTimestamp = new Date().getTime()
for (const address of JSON.parse(process.env.ALLOWED_ADMINS)) {
for (const address of status.allowedAdmins) {
if (address.lowercase() === signerAddress && currentTimestamp < expiryTimestamp) {
return true
}
Expand Down

0 comments on commit 40b2302

Please sign in to comment.