Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 docs/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Environmental variables are also tracked in `ENVIRONMENT_VARIABLES` within `src/
## Policy Server

- `POLICY_SERVER_URL`: URI definition of PolicyServer, if any. See [the policy server documentation for more details](docs/PolicyServer.md).
- `POLICY_SERVER_API_KEY`: Optional API key sent by Ocean Node as `X-API-Key` when calling Policy Server.

## Additional Nodes (Test Environments)

Expand Down
22 changes: 21 additions & 1 deletion src/components/policyServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import { DDO } from '@oceanprotocol/ddo-js'
import { PolicyServerResult } from '../../@types/policyServer.js'
import { isDefined } from '../../utils/util.js'
import { BaseFileObject } from '../../@types/fileObject.js'
import { OceanNode } from '../../OceanNode.js'

export function attachNodeAddress(
command: Record<string, any>,
nodeAddress: string
): Record<string, any> {
return {
...command,
nodeAddress
}
}

export class PolicyServer {
serverUrl: string
Expand All @@ -12,9 +23,18 @@ export class PolicyServer {
this.apikey = process.env.POLICY_SERVER_API_KEY
}

private attachNodeAddress(command: Record<string, any>): Record<string, any> {
const node = OceanNode.getInstance()
const keyManager = node.getKeyManager()
const nodeAddress = keyManager.getEthWallet().address
return attachNodeAddress(command, nodeAddress)
}

private async askServer(command: any): Promise<PolicyServerResult> {
if (!this.serverUrl) return { success: true, message: '', httpStatus: 404 }
let response
const commandWithNodeAddress = this.attachNodeAddress(command)

const headers: Record<string, string> = {
'Content-Type': 'application/json'
}
Expand All @@ -25,7 +45,7 @@ export class PolicyServer {
response = await fetch(this.serverUrl, {
headers,
method: 'POST',
body: JSON.stringify(command)
body: JSON.stringify(commandWithNodeAddress)
})
} catch (e) {
const errorText =
Expand Down
Loading