Skip to content

fix: update IPFS tasks to use environment variables from .env files #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
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
31 changes: 7 additions & 24 deletions tasks/ipfs-cid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,18 @@ import { create } from 'ipfs-http-client';
task('ipfs-cid', 'Gets a CID on IPFS for a path')
.addParam<string>('ipfspath', 'the path')
.setAction(async ({ ipfspath }: { ipfspath: string }) => {
const env = await fetch(
`${process.env.BTP_CLUSTER_MANAGER_URL}/ide/foundry/${process.env.BTP_SCS_ID}/env`,
{
headers: {
'x-auth-token': process.env.BTP_SERVICE_TOKEN!,
},
}
);
// Use SettleMint IPFS API endpoint directly from .env file
const btpIpfs = process.env.SETTLEMINT_IPFS_API_ENDPOINT;

const envText = await env.text();

const envVars = envText.split('\n').map((line) => line.trim());
for (const envVar of envVars) {
const [key, value] = envVar.split('=');
process.env[key] = value;
}

const btpIpfs = process.env.BTP_IPFS;

if (btpIpfs?.includes('api.thegraph.com') || !btpIpfs) {
throw new Error(`No IPFS node found or configured wrong.`);
if (!btpIpfs) {
throw new Error(`No IPFS node configured. Please run 'settlemint connect' to set up your environment.`);
}
const lastSlashIndex = btpIpfs.lastIndexOf('/');
const baseUrl = btpIpfs.substring(0, lastSlashIndex);

// Create IPFS client with the API endpoint
const ipfsClient = create({
url: baseUrl,
url: btpIpfs,
headers: {
'x-auth-token': process.env.BTP_SERVICE_TOKEN!,
'x-auth-token': process.env.SETTLEMINT_ACCESS_TOKEN!,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (review_instructions): Using the non-null assertion operator (!) could lead to runtime errors if the token is missing.

The code assumes SETTLEMINT_ACCESS_TOKEN will always be defined. If this environment variable is missing, this will cause a runtime error. Consider adding a check for this token's existence before creating the IPFS client, similar to how you check for the IPFS endpoint.

Review instructions:

Path patterns: **/*.ts

Instructions:
Always write correct, up to date, bug free, fully functional and working, secure, performant and efficient code.

},
});

Expand Down
35 changes: 10 additions & 25 deletions tasks/ipfs-upload-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { readFileSync } from 'fs';
import { task } from 'hardhat/config';
import { create } from 'ipfs-http-client';
import { Blob, NFTStorage } from 'nft.storage';
import { nftStorageToken } from '../hardhat.config';

// NFT Storage token - ideally this would be in config or .env
const nftStorageToken = process.env.NFT_STORAGE_TOKEN || '';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (review_instructions): Using an empty string as a fallback for the NFT storage token could lead to silent failures.

If the token is missing, the code will continue execution with an empty string and only fail later when trying to use it. Consider throwing an error immediately if the environment variable is not set to prevent potential issues downstream.

Review instructions:

Path patterns: **/*.ts

Instructions:
Always write correct, up to date, bug free, fully functional and working, secure, performant and efficient code.


async function pinToNFTStorage(blob: Blob) {
if (!nftStorageToken || nftStorageToken === '') {
Expand All @@ -20,35 +22,18 @@ async function ipfsUpload(
content: Buffer | Record<string, any>,
pin?: true
) {
const env = await fetch(
`${process.env.BTP_CLUSTER_MANAGER_URL}/ide/foundry/${process.env.BTP_SCS_ID}/env`,
{
headers: {
'x-auth-token': process.env.BTP_SERVICE_TOKEN!,
},
}
);

const envText = await env.text();

const envVars = envText.split('\n').map((line) => line.trim());
for (const envVar of envVars) {
const [key, value] = envVar.split('=');
process.env[key] = value;
}

const btpIpfs = process.env.BTP_IPFS;
// Use SettleMint IPFS API endpoint directly from .env file
const btpIpfs = process.env.SETTLEMINT_IPFS_API_ENDPOINT;

if (btpIpfs?.includes('network.thegraph.com') || !btpIpfs) {
throw new Error(`No IPFS node found or configured wrong.`);
if (!btpIpfs) {
throw new Error(`No IPFS node configured. Please run 'settlemint connect' to set up your environment.`);
}
const lastSlashIndex = btpIpfs.lastIndexOf('/');
const baseUrl = btpIpfs.substring(0, lastSlashIndex);

// Create IPFS client with the API endpoint
const ipfsClient = create({
url: baseUrl,
url: btpIpfs,
headers: {
'x-auth-token': process.env.BTP_SERVICE_TOKEN!,
'x-auth-token': process.env.SETTLEMINT_ACCESS_TOKEN!,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (review_instructions): Using the non-null assertion operator (!) could lead to runtime errors if the token is missing.

The code assumes SETTLEMINT_ACCESS_TOKEN will always be defined. If this environment variable is missing, this will cause a runtime error. Consider adding a check for this token's existence before creating the IPFS client, similar to how you check for the IPFS endpoint.

Review instructions:

Path patterns: **/*.ts

Instructions:
Always write correct, up to date, bug free, fully functional and working, secure, performant and efficient code.

},
});
Comment on lines +32 to 38
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Validate the presence of SETTLEMINT_ACCESS_TOKEN

Since this token is critical for creating a valid IPFS client, consider adding a check to ensure that SETTLEMINT_ACCESS_TOKEN is set to prevent potential runtime issues.

Suggested change
// Create IPFS client with the API endpoint
const ipfsClient = create({
url: baseUrl,
url: btpIpfs,
headers: {
'x-auth-token': process.env.BTP_SERVICE_TOKEN!,
'x-auth-token': process.env.SETTLEMINT_ACCESS_TOKEN!,
},
});
// Validate that SETTLEMINT_ACCESS_TOKEN environment variable is set
const settlemintAccessToken = process.env.SETTLEMINT_ACCESS_TOKEN;
if (!settlemintAccessToken) {
throw new Error("Environment variable SETTLEMINT_ACCESS_TOKEN is not set.");
}
// Create IPFS client with the API endpoint
const ipfsClient = create({
url: btpIpfs,
headers: {
'x-auth-token': settlemintAccessToken,
},
});

const contentToStore = Buffer.isBuffer(content)
Expand Down
Loading