-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlitAction-deadManSwitch.js
More file actions
74 lines (63 loc) · 2.24 KB
/
litAction-deadManSwitch.js
File metadata and controls
74 lines (63 loc) · 2.24 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
/**
* Lit Action for Dead Man Switch Expiration Check
*
* This Lit Action checks if a Solana Dead Man Switch has expired
* by fetching current account data and comparing last_ping + ping_interval
* against current time.
*
* Upload this file to IPFS and use the CID in access control conditions.
*/
const checkExpiry = async (switchPDA) => {
try {
// Fetch account data from Solana
const rpcUrl = "https://api.devnet.solana.com";
const response = await fetch(rpcUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "getAccountInfo",
params: [
switchPDA,
{
encoding: "base64",
commitment: "confirmed"
}
]
})
});
const result = await response.json();
if (!result.result || !result.result.value) {
return false; // Account not found - deny access
}
// Parse the account data
const accountDataBase64 = result.result.value.data[0];
const accountDataBuffer = Uint8Array.from(atob(accountDataBase64), c => c.charCodeAt(0));
// Parse according to DeadManSwitch struct layout:
// discriminator: 8 bytes (0-7)
// owner: Pubkey (32 bytes) (8-39)
// last_ping: i64 (8 bytes) (40-47)
// ping_interval: i64 (8 bytes) (48-55)
let offset = 8; // Skip Anchor discriminator
offset += 32; // Skip owner (32 bytes) - now at offset 40
// Parse last_ping (8 bytes, little-endian)
const lastPing = Number(
new DataView(accountDataBuffer.buffer, accountDataBuffer.byteOffset + offset, 8).getBigInt64(0, true)
);
offset += 8; // now at offset 48
// Parse ping_interval (8 bytes, little-endian)
const pingInterval = Number(
new DataView(accountDataBuffer.buffer, accountDataBuffer.byteOffset + offset, 8).getBigInt64(0, true)
);
// Calculate expiration: last_ping + ping_interval
const currentTime = Math.floor(Date.now() / 1000);
const expirationTime = lastPing + pingInterval;
const isExpired = currentTime >= expirationTime;
return isExpired;
} catch (error) {
return false; // Deny access on any error
}
};