-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-basic-consent.ts
More file actions
111 lines (94 loc) · 4.34 KB
/
Copy path01-basic-consent.ts
File metadata and controls
111 lines (94 loc) · 4.34 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
101
102
103
104
105
106
107
108
109
110
111
/**
* Example: Basic Consent
* Scenario: Getting Started
* Persona: End Users, Developers
*
* This example demonstrates:
* - Deploying the ConsentReceipt contract
* - Giving consent for a specific purpose
* - Checking consent status
* - Viewing consent details
*
* Prerequisites:
* - Local Hardhat node running (npm run node)
*
* Run with:
* npx hardhat run examples/01-getting-started/01-basic-consent.ts --network localhost
*/
import { ethers } from "hardhat";
async function main() {
console.log("\n========================================");
console.log(" Example: Basic Consent Flow");
console.log("========================================\n");
// === SETUP ===
console.log(">>> Setting up contracts and accounts...\n");
// Get test accounts
const [deployer, user] = await ethers.getSigners();
console.log(`Deployer: ${deployer.address}`);
console.log(`User: ${user.address}`);
// Deploy ConsentReceipt contract
const ConsentReceiptFactory = await ethers.getContractFactory("ConsentReceipt");
const consentReceipt = await ConsentReceiptFactory.deploy();
await consentReceipt.waitForDeployment();
console.log(`\nConsentReceipt deployed at: ${await consentReceipt.getAddress()}`);
// === SCENARIO ===
// Step 1: Check initial consent status
console.log("\n>>> Step 1: Check initial consent status");
const initialStatus = await consentReceipt.getConsentStatus(user.address, "analytics");
console.log(`Has consent for 'analytics': ${initialStatus}`);
// Expected: false (no consent given yet)
// Step 2: User gives consent for analytics
console.log("\n>>> Step 2: User gives consent for 'analytics'");
const tx = await consentReceipt.connect(user)["giveConsent(string)"]("analytics");
const receipt = await tx.wait();
console.log(`Transaction hash: ${receipt?.hash}`);
console.log("Consent given successfully!");
// Step 3: Verify consent status changed
console.log("\n>>> Step 3: Verify consent status");
const newStatus = await consentReceipt.getConsentStatus(user.address, "analytics");
console.log(`Has consent for 'analytics': ${newStatus}`);
// Expected: true
// Step 4: View consent details
console.log("\n>>> Step 4: View consent details");
const consents = await consentReceipt.getUserConsents(user.address);
console.log(`Total consents: ${consents.length}`);
if (consents.length > 0) {
const consent = consents[0];
console.log("\nConsent Record:");
console.log(` User: ${consent.user}`);
console.log(` Purpose: ${consent.purpose}`);
console.log(` Timestamp: ${consent.timestamp} (${new Date(Number(consent.timestamp) * 1000).toISOString()})`);
console.log(` Expiry: ${consent.expiryTime === 0n ? "Never" : consent.expiryTime}`);
console.log(` Is Valid: ${consent.isValid}`);
}
// Step 5: Give consent for another purpose
console.log("\n>>> Step 5: User gives consent for 'marketing'");
await consentReceipt.connect(user)["giveConsent(string)"]("marketing");
console.log("Consent for 'marketing' given!");
// Step 6: Check both consent statuses
console.log("\n>>> Step 6: Verify multiple consents");
const analyticsConsent = await consentReceipt.getConsentStatus(user.address, "analytics");
const marketingConsent = await consentReceipt.getConsentStatus(user.address, "marketing");
const researchConsent = await consentReceipt.getConsentStatus(user.address, "research");
console.log(`Has 'analytics' consent: ${analyticsConsent}`);
console.log(`Has 'marketing' consent: ${marketingConsent}`);
console.log(`Has 'research' consent: ${researchConsent}`);
// Expected: true, true, false
// === VERIFICATION ===
console.log("\n>>> Verification");
const finalCount = await consentReceipt.getUserConsentsCount(user.address);
console.log(`Total consents recorded: ${finalCount}`);
// === SUMMARY ===
console.log("\n========================================");
console.log(" Example completed successfully!");
console.log("========================================");
console.log("\nKey Takeaways:");
console.log("1. Consent is tracked per user and per purpose");
console.log("2. getConsentStatus() returns true/false for quick checks");
console.log("3. getUserConsents() returns full consent history");
console.log("4. Each consent has timestamp and validity tracking\n");
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});