Skip to content

Commit 1de9673

Browse files
committed
chore: add node address in policy server request
1 parent 6a3e228 commit 1de9673

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

docs/env.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Environmental variables are also tracked in `ENVIRONMENT_VARIABLES` within `src/
112112
## Policy Server
113113

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

116117
## Additional Nodes (Test Environments)
117118

src/components/policyServer/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import { DDO } from '@oceanprotocol/ddo-js'
22
import { PolicyServerResult } from '../../@types/policyServer.js'
33
import { isDefined } from '../../utils/util.js'
44
import { BaseFileObject } from '../../@types/fileObject.js'
5+
import { OceanNode } from '../../OceanNode.js'
6+
7+
export function attachNodeAddress(
8+
command: Record<string, any>,
9+
nodeAddress: string
10+
): Record<string, any> {
11+
return {
12+
...command,
13+
nodeAddress
14+
}
15+
}
516

617
export class PolicyServer {
718
serverUrl: string
@@ -12,9 +23,18 @@ export class PolicyServer {
1223
this.apikey = process.env.POLICY_SERVER_API_KEY
1324
}
1425

26+
private attachNodeAddress(command: Record<string, any>): Record<string, any> {
27+
const node = OceanNode.getInstance()
28+
const keyManager = node.getKeyManager()
29+
const nodeAddress = keyManager.getEthWallet().address
30+
return attachNodeAddress(command, nodeAddress)
31+
}
32+
1533
private async askServer(command: any): Promise<PolicyServerResult> {
1634
if (!this.serverUrl) return { success: true, message: '', httpStatus: 404 }
1735
let response
36+
const commandWithNodeAddress = this.attachNodeAddress(command)
37+
1838
const headers: Record<string, string> = {
1939
'Content-Type': 'application/json'
2040
}
@@ -25,7 +45,7 @@ export class PolicyServer {
2545
response = await fetch(this.serverUrl, {
2646
headers,
2747
method: 'POST',
28-
body: JSON.stringify(command)
48+
body: JSON.stringify(commandWithNodeAddress)
2949
})
3050
} catch (e) {
3151
const errorText =

0 commit comments

Comments
 (0)