Skip to content

Commit 5e0b9a2

Browse files
committed
use .env
1 parent ffbda1a commit 5e0b9a2

File tree

3 files changed

+33
-49
lines changed

3 files changed

+33
-49
lines changed

scenario.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ echo "TOKENID=2" >> .env
4343
source .env
4444

4545
npx hardhat run scripts/verify-metadata-proof.ts --network sepolia
46-
46+
source .env
4747
npx hardhat run scripts/claim-metadata-update.ts --network opSepolia

scripts/claim-metadata-update.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,45 @@
11
import { ethers } from "hardhat"
22
import { NFT__factory } from "../typechain-types/factories/contracts/variants/crosschain/NFT__factory"
3-
import * as fs from "fs"
4-
import * as path from "path"
3+
import * as dotenv from "dotenv"
54

65
async function main() {
7-
const ALICE_PRIVATE_KEY = process.env.ALICE
8-
if (!ALICE_PRIVATE_KEY) {
6+
// Load environment variables
7+
dotenv.config()
8+
9+
if (!process.env.ALICE) {
910
throw new Error("Please set ALICE private key in your .env file")
1011
}
1112

13+
if (!process.env.PROOF) {
14+
throw new Error(
15+
"No proof found in .env file. Please run verify-metadata-proof.ts first"
16+
)
17+
}
18+
19+
if (!process.env.TOKENID && process.env.TOKENID !== "0") {
20+
throw new Error("No token ID specified in .env file")
21+
}
22+
1223
// Load contract addresses from deployment files
1324
const deploymentsNFT = require("../deployments/sepolia/CrosschainNFT.json")
1425
const NFT_ADDRESS = deploymentsNFT.address
1526

1627
const provider = new ethers.JsonRpcProvider(
1728
process.env.OP_SEPOLIA_RPC_ENDPOINT_URL
1829
)
19-
const aliceSigner = new ethers.Wallet(ALICE_PRIVATE_KEY, provider)
30+
const aliceSigner = new ethers.Wallet(process.env.ALICE, provider)
2031
const nft = NFT__factory.connect(NFT_ADDRESS, aliceSigner)
2132

22-
// Load proofs from file
23-
const proofsPath = path.resolve(__dirname, "../proofs.json")
24-
if (!fs.existsSync(proofsPath)) {
25-
throw new Error(
26-
"proofs.json not found. Please run verify-metadata-proof.ts first"
27-
)
28-
}
29-
const proofs = JSON.parse(fs.readFileSync(proofsPath, "utf8"))
30-
31-
if (!Array.isArray(proofs) || proofs.length === 0) {
32-
throw new Error("No proofs found in proofs.json")
33-
}
34-
35-
// Use the proof from the first entry
36-
const proofData = proofs[0]
33+
const TOKEN_ID = parseInt(process.env.TOKENID)
3734

3835
try {
3936
console.log("\nSimulating metadata update claim...")
40-
console.log("Using proof for token:", proofData.tokenId)
41-
await nft.claimOperation.staticCall(proofData.proof)
37+
console.log("Using proof for token:", TOKEN_ID)
38+
await nft.claimOperation.staticCall(process.env.PROOF)
4239
console.log("✅ Simulation successful")
4340

4441
console.log("\nSubmitting metadata update claim...")
45-
const tx = await nft.claimOperation(proofData.proof, {
42+
const tx = await nft.claimOperation(process.env.PROOF, {
4643
gasLimit: 500000
4744
})
4845

@@ -52,7 +49,7 @@ async function main() {
5249
if (receipt?.status === 1) {
5350
console.log("\nMetadata updated successfully! 🎉")
5451
try {
55-
const tokenURI = await nft.tokenURI(proofData.tokenId)
52+
const tokenURI = await nft.tokenURI(TOKEN_ID)
5653
console.log("New token URI:", tokenURI)
5754
} catch (e) {
5855
console.log("Could not fetch new token URI")

scripts/verify-metadata-proof.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,20 @@ async function main() {
1010
dotenv.config()
1111

1212
// Load contract addresses from deployment files
13-
const deploymentsGov = require("../deployments/sepolia/CrosschainGov.json")
14-
const GOV_ADDRESS = deploymentsGov.address
1513
const deploymentsNFT = require("../deployments/sepolia/CrosschainNFT.json")
1614
const NFT_ADDRESS = deploymentsNFT.address
17-
const PROOF_HANDLER_ADDRESS = deploymentsGov.libraries.ProofHandler
15+
const PROOF_HANDLER_ADDRESS = deploymentsNFT.libraries.ProofHandler
1816

1917
// Get token info
2018
if (!process.env.TOKENID && process.env.TOKENID !== "0") {
2119
throw new Error("No token ID specified in .env")
2220
}
2321
const TOKEN_ID = parseInt(process.env.TOKENID)
2422

25-
// Log the environment variable for debugging
26-
console.log("TOKENID environment variable:", process.env.TOKENID)
27-
2823
const NEW_URI =
2924
"https://bafkreifnnreoxxgkhty7v2w3qwiie6cfxpv3vcco2xldekfvbiem3nm6dm.ipfs.w3s.link/"
3025

31-
console.log("Generating metadata update proof...")
32-
console.log("Gov Address:", GOV_ADDRESS)
26+
console.log("\nGenerating metadata update proof...")
3327
console.log("NFT Address:", NFT_ADDRESS)
3428
console.log("ProofHandler Address:", PROOF_HANDLER_ADDRESS)
3529
console.log("Token ID:", TOKEN_ID)
@@ -55,46 +49,39 @@ async function main() {
5549
[TOKEN_ID, NEW_URI]
5650
)
5751

58-
// Generate the operation proof
59-
// Operation type 2 is SET_METADATA
52+
// Generate the operation proof (Operation type 2 is SET_METADATA)
6053
const proof = await nft.generateOperationProof(2, encodedParams)
6154

6255
console.log("\nProof generated successfully!")
6356
console.log("\nProof:", proof)
6457

65-
// Create the proof object in the expected format
66-
const proofData = {
67-
tokenId: TOKEN_ID,
68-
proof: proof,
69-
owner: undefined, // Not needed for metadata update
70-
nonce: 1 // Start with nonce 1 for metadata updates
71-
}
72-
73-
// Add the new token ID to .env file
58+
// Update .env file with the proof
7459
const envPath = path.resolve(__dirname, "../.env")
7560
let envContent = ""
61+
7662
try {
7763
// Read existing .env content if file exists
7864
if (fs.existsSync(envPath)) {
7965
envContent = fs.readFileSync(envPath, "utf8")
8066
}
8167

82-
// Remove existing TOKENID line if it exists
83-
envContent = envContent.replace(/^TOKENID=.*$/m, "")
68+
// Remove existing PROOF line if it exists
69+
envContent = envContent.replace(/^PROOF=.*$/m, "")
8470

8571
// Add new line if content doesn't end with one
8672
if (envContent.length > 0 && !envContent.endsWith("\n")) {
8773
envContent += "\n"
8874
}
8975

90-
// Add the new TOKENID
91-
envContent += `TOKENID=${TOKEN_ID}\n`
76+
// Add the new PROOF
77+
envContent += `PROOF=${proof}\n`
9278

9379
// Write back to .env file
9480
fs.writeFileSync(envPath, envContent)
95-
console.log("\nToken ID has been written to .env file")
81+
console.log("\nProof has been saved to .env file")
9682
} catch (error) {
9783
console.error("Error updating .env file:", error)
84+
throw error
9885
}
9986
} catch (error: any) {
10087
console.error("\nError generating proof:", error)

0 commit comments

Comments
 (0)