-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-code.js
More file actions
100 lines (84 loc) · 3.25 KB
/
test-code.js
File metadata and controls
100 lines (84 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const anchor = require('@coral-xyz/anchor');
const { PublicKey, SystemProgram } = anchor.web3;
const { readFileSync } = require('fs');
// Replace with your IDL file path and wallet keypair
const idl = require('./lib/idl/content_attestation.json');
const keypairFile = './wallet.json';
// Optional: Patch the IDL to ensure the account type info is available for ContentAttestation.
idl.accounts = idl.accounts.map(acc => {
if (acc.name === "ContentAttestation" && !acc.type) {
const typeDef = idl.types.find(t => t.name === "ContentAttestation");
if (typeDef) {
return { ...acc, type: typeDef.type };
}
}
return acc;
});
async function main() {
// Set up connection to devnet
const connection = new anchor.web3.Connection(
anchor.web3.clusterApiUrl('devnet'),
'confirmed'
);
// Load wallet from keypair file
const keypair = anchor.web3.Keypair.fromSecretKey(
Buffer.from(JSON.parse(readFileSync(keypairFile, 'utf-8')))
);
const wallet = new anchor.Wallet(keypair);
// Set up provider
const provider = new anchor.AnchorProvider(connection, wallet, {});
anchor.setProvider(provider);
// Initialize the program using only the IDL and the provider
const program = new anchor.Program(idl, provider);
// Input parameters for the registerContent instruction
const contentCid = 'bafkreiets7d2gtrfmnfwxtefegfwukhhl7wimilvxjxkgwcruxkkp7zy7a';
const metadataCid = 'QmTeMpLqMTrqA77MemR96RNYvAzvUvf9sboyF4oevqP88P';
const contentType = 'image/png'; // This is now an image
const title = 'My Content Title';
const description = 'This is a description of my content';
// Use only the first 5 characters (bytes) of contentCid
const contentCidSliced = contentCid.slice(-5);
const contentCidBuffer = Buffer.from(contentCidSliced, 'utf-8'); // Convert to buffer
console.log(contentCidSliced);
// Derive PDA for the attestation account using the seeds defined in the program:
// - Constant seed: "attestation"
// - Creator's public key
// - Sliced Content CID (first 5 bytes only)
const [attestationPda] = await PublicKey.findProgramAddress(
[
Buffer.from('attestation'),
wallet.publicKey.toBuffer(),
contentCidBuffer, // Use the sliced contentCid here
],
program.programId
);
console.log('Attestation PDA:', attestationPda.toBase58());
// Check if the PDA account already exists
const accountInfo = await connection.getAccountInfo(attestationPda);
if (accountInfo !== null) {
console.log('Attestation account already exists at this PDA:', attestationPda.toBase58());
return; // Skip creating the account if it already exists
}
// Execute the transaction to register the content
try {
const txSignature = await program.methods.registerContent(
contentCid, // Send the full contentCid to the program (if needed)
metadataCid,
contentType,
title,
description
)
.accounts({
attestation: attestationPda,
creator: wallet.publicKey,
systemProgram: SystemProgram.programId,
})
.signers([wallet.payer])
.rpc();
console.log('Transaction signature:', txSignature);
console.log('Content registered successfully!');
} catch (error) {
console.error('Error:', error);
}
}
main().catch(console.error);