-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 || ''; | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: Instructions: |
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
async function pinToNFTStorage(blob: Blob) { | ||||||||||||||||||||||||||||||||||||||||||||||
if (!nftStorageToken || nftStorageToken === '') { | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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!, | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: Instructions: |
||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+32
to
38
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||||||||||||||||||||||||||||||
const contentToStore = Buffer.isBuffer(content) | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
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.