Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
feat: ipfs helper
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Jun 27, 2024
1 parent e2eb9e0 commit 343d9de
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 114 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ Just clone this repository and run the following commands:
# Install all the dependencies
$ yarn

# Start the server
$ yarn start
# Configure the database where we are gonna store the tasks and steps info
$ yarn database:run-migrations

# Start the api server
$ yarn start:api

# Start the backend transactions processor
$ yarn start:proc
```

By default the server will start at `http://localhost:4100`. You can change the port by setting the `API_PORT` environment variable.
Expand All @@ -59,7 +65,7 @@ The agent can be configured using environment variables. The following variables

The template is designed to be as simple as possible to allow you to focus on the AI logic of your agent. The main files you need to modify are:

- `agent.service.ts`: This file contains the main logic of your agent. You can implement your AI logic here.
- `processor.controller.ts`: This file contains the main logic of your agent. You can implement your AI logic here.

## License

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "agent-template-js",
"version": "0.0.1",
"version": "0.0.2",
"description": "",
"private": true,
"author": "Nevermined <root@nevermined.io>",
Expand Down Expand Up @@ -44,6 +44,7 @@
"rimraf": "^5.0.7",
"rxjs": "^7.2.0",
"typeorm": "^0.3.20",
"ipfs-http-client-lite": "^0.3.0",
"uuid": "^10.0.0"
},
"devDependencies": {
Expand Down
65 changes: 65 additions & 0 deletions src/common/utils/ipfs-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import IpfsHttpClientLite from 'ipfs-http-client-lite'

export interface IpfsConnectionParameters {
gatewayUrl: string
projectId?: string
projectSecret?: string
authToken?: string
}

export default class IpfsHelper {
private ipfsGateway: string
private authToken: string

constructor(ipfsOptions: IpfsConnectionParameters) {
this.ipfsGateway = ipfsOptions.gatewayUrl
this.authToken = this.getAuthToken(ipfsOptions)
}

private getAuthToken(ipfsOptions: IpfsConnectionParameters): string {
if (this.authToken) return this.authToken
if (ipfsOptions.authToken) return ipfsOptions.authToken
if (!ipfsOptions.projectId || !ipfsOptions.projectSecret) {
return undefined
} else {
return Buffer.from(
`${ipfsOptions.projectId}:${ipfsOptions.projectSecret}`
).toString('base64')
}
}

async add(content: any): Promise<string> {
const ipfs = IpfsHttpClientLite({
apiUrl: this.ipfsGateway,
...(this.authToken && {
headers: { Authorization: `Basic ${this.authToken}` }
})
})
const addResult = await ipfs.add(content)
return addResult[0].hash
}

async get(cid: string): Promise<string> {
const url =
this.ipfsGateway + '/api/v0/cat?arg=' + cid.replace('cid://', '')
const options = {
method: 'POST',
...(this.authToken && {
headers: { Authorization: `Basic ${this.authToken}` }
})
}

return fetch(url, options)
.then(async (res) => {
if (!res.ok) {
throw new Error(
`${res.status}: ${res.statusText} - ${await res.text()}`
)
}
return res.text()
})
.catch((err) => {
throw err
})
}
}
5 changes: 4 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export const appConfig = {
schena: process.env.DATABASE_SCHEMA
},
processor: {
sleepDuration: Number(process.env.AGENT_SLEEP_DURATION) || 3000 // In milliseconds
sleepDuration: Number(process.env.AGENT_SLEEP_DURATION) || 3000, // In milliseconds
ipfsGateway: process.env.IPFS_GATEWAY || 'https://ipfs.infura.io:5001',
ipfsProjectId: process.env.IPFS_PROJECT_ID,
ipfsProjectSecret: process.env.IPFS_PROJECT_SECRET
}
}
export default () => appConfig
Loading

0 comments on commit 343d9de

Please sign in to comment.