@gvnrdao/dh-lit-ops 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +197 -0
  2. package/dist/constants/chunks/pkp-data.d.ts +28 -0
  3. package/dist/constants/chunks/pkp-data.d.ts.map +1 -0
  4. package/dist/constants/chunks/pkp-data.js +86 -0
  5. package/dist/constants/chunks/pkp-data.js.map +1 -0
  6. package/dist/constants/index.d.ts +8 -0
  7. package/dist/constants/index.d.ts.map +1 -0
  8. package/dist/constants/index.js +25 -0
  9. package/dist/constants/index.js.map +1 -0
  10. package/dist/index.d.ts +30 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +63 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/interfaces/chunks/authentication.d.ts +29 -0
  15. package/dist/interfaces/chunks/authentication.d.ts.map +1 -0
  16. package/dist/interfaces/chunks/authentication.js +7 -0
  17. package/dist/interfaces/chunks/authentication.js.map +1 -0
  18. package/dist/interfaces/chunks/config.d.ts +52 -0
  19. package/dist/interfaces/chunks/config.d.ts.map +1 -0
  20. package/dist/interfaces/chunks/config.js +7 -0
  21. package/dist/interfaces/chunks/config.js.map +1 -0
  22. package/dist/interfaces/chunks/lit-action-execution.d.ts +27 -0
  23. package/dist/interfaces/chunks/lit-action-execution.d.ts.map +1 -0
  24. package/dist/interfaces/chunks/lit-action-execution.js +7 -0
  25. package/dist/interfaces/chunks/lit-action-execution.js.map +1 -0
  26. package/dist/interfaces/chunks/pkp-operations.d.ts +143 -0
  27. package/dist/interfaces/chunks/pkp-operations.d.ts.map +1 -0
  28. package/dist/interfaces/chunks/pkp-operations.js +3 -0
  29. package/dist/interfaces/chunks/pkp-operations.js.map +1 -0
  30. package/dist/interfaces/index.d.ts +11 -0
  31. package/dist/interfaces/index.d.ts.map +1 -0
  32. package/dist/interfaces/index.js +31 -0
  33. package/dist/interfaces/index.js.map +1 -0
  34. package/dist/modules/action-executor.module.d.ts +34 -0
  35. package/dist/modules/action-executor.module.d.ts.map +1 -0
  36. package/dist/modules/action-executor.module.js +231 -0
  37. package/dist/modules/action-executor.module.js.map +1 -0
  38. package/dist/modules/auth-manager.module.d.ts +24 -0
  39. package/dist/modules/auth-manager.module.d.ts.map +1 -0
  40. package/dist/modules/auth-manager.module.js +103 -0
  41. package/dist/modules/auth-manager.module.js.map +1 -0
  42. package/dist/modules/client-manager.module.d.ts +64 -0
  43. package/dist/modules/client-manager.module.d.ts.map +1 -0
  44. package/dist/modules/client-manager.module.js +213 -0
  45. package/dist/modules/client-manager.module.js.map +1 -0
  46. package/dist/modules/lit-ops.module.d.ts +261 -0
  47. package/dist/modules/lit-ops.module.d.ts.map +1 -0
  48. package/dist/modules/lit-ops.module.js +792 -0
  49. package/dist/modules/lit-ops.module.js.map +1 -0
  50. package/dist/modules/pkp-authorizer.module.d.ts +72 -0
  51. package/dist/modules/pkp-authorizer.module.d.ts.map +1 -0
  52. package/dist/modules/pkp-authorizer.module.js +126 -0
  53. package/dist/modules/pkp-authorizer.module.js.map +1 -0
  54. package/dist/modules/pkp-macros.module.d.ts +188 -0
  55. package/dist/modules/pkp-macros.module.d.ts.map +1 -0
  56. package/dist/modules/pkp-macros.module.js +656 -0
  57. package/dist/modules/pkp-macros.module.js.map +1 -0
  58. package/dist/modules/pkp-manager.module.d.ts +56 -0
  59. package/dist/modules/pkp-manager.module.d.ts.map +1 -0
  60. package/dist/modules/pkp-manager.module.js +304 -0
  61. package/dist/modules/pkp-manager.module.js.map +1 -0
  62. package/dist/modules/pkp-minter.module.d.ts +66 -0
  63. package/dist/modules/pkp-minter.module.d.ts.map +1 -0
  64. package/dist/modules/pkp-minter.module.js +185 -0
  65. package/dist/modules/pkp-minter.module.js.map +1 -0
  66. package/dist/modules/pkp-signer.module.d.ts +56 -0
  67. package/dist/modules/pkp-signer.module.d.ts.map +1 -0
  68. package/dist/modules/pkp-signer.module.js +148 -0
  69. package/dist/modules/pkp-signer.module.js.map +1 -0
  70. package/dist/modules/session-signature-manager.module.d.ts +32 -0
  71. package/dist/modules/session-signature-manager.module.d.ts.map +1 -0
  72. package/dist/modules/session-signature-manager.module.js +123 -0
  73. package/dist/modules/session-signature-manager.module.js.map +1 -0
  74. package/package.json +61 -0
@@ -0,0 +1,656 @@
1
+ "use strict";
2
+ /**
3
+ * PKP Macros - High-Level PKP Operations
4
+ *
5
+ * Provides simple macro functions for common PKP workflows:
6
+ * 1. Create PKP + Authorize + Burn (Test Workflow)
7
+ * 2. Create PKP + Authorize (Production Workflow)
8
+ * 3. Individual operations with smart defaults
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.PKPMacroUtils = exports.PKPMacros = exports.DEFAULT_RPC_ENDPOINT = exports.DEFAULT_PKP_NFT_CONTRACT = exports.AUTHORIZATION_DUMMY_CID = void 0;
12
+ const dh_lit_actions_1 = require("@gvnrdao/dh-lit-actions");
13
+ const ethers_1 = require("ethers");
14
+ const action_executor_module_1 = require("./action-executor.module");
15
+ const client_manager_module_1 = require("./client-manager.module");
16
+ const pkp_authorizer_module_1 = require("./pkp-authorizer.module");
17
+ const pkp_minter_module_1 = require("./pkp-minter.module");
18
+ // Import the working CID from lit-actions package
19
+ exports.AUTHORIZATION_DUMMY_CID = dh_lit_actions_1.DH_LIT_ACTIONS.authorizationDummy.cid;
20
+ // Default PKP NFT contract (datil-test)
21
+ exports.DEFAULT_PKP_NFT_CONTRACT = "0x6a0f439f064B7167A8Ea6B22AcC07ae5360ee0d1";
22
+ // Default RPC endpoint
23
+ exports.DEFAULT_RPC_ENDPOINT = "https://yellowstone-rpc.litprotocol.com";
24
+ class PKPMacros {
25
+ constructor(config, sharedDependencies) {
26
+ // Require network to be explicitly specified - no default to prevent multi-instance issues
27
+ if (!config.network) {
28
+ throw new Error("Network must be specified in PKPMacroConfig - no default network allowed");
29
+ }
30
+ this.config = {
31
+ pkpNftContract: exports.DEFAULT_PKP_NFT_CONTRACT,
32
+ debug: false,
33
+ ...config,
34
+ };
35
+ this.provider = new ethers_1.ethers.providers.JsonRpcProvider(exports.DEFAULT_RPC_ENDPOINT);
36
+ // Use shared client manager if provided, otherwise create new one
37
+ this.clientManager =
38
+ sharedDependencies?.clientManager ||
39
+ new client_manager_module_1.LitClientManager({
40
+ litNetwork: config.network,
41
+ debug: config.debug || false,
42
+ alertWhenUnauthorized: false,
43
+ minNodeCount: 2,
44
+ });
45
+ // Ensure all dependencies exist - create if not provided via shared dependencies
46
+ this.pkpMinter =
47
+ sharedDependencies?.pkpMinter ||
48
+ new pkp_minter_module_1.PKPMinter({
49
+ signer: this.config.signer,
50
+ network: this.config.network,
51
+ debug: this.config.debug,
52
+ clientManager: this.clientManager,
53
+ });
54
+ this.pkpAuthorizer =
55
+ sharedDependencies?.pkpAuthorizer ||
56
+ new pkp_authorizer_module_1.PKPAuthorizer({
57
+ signer: this.config.signer,
58
+ network: this.config.network,
59
+ debug: this.config.debug,
60
+ clientManager: this.clientManager,
61
+ });
62
+ this.actionExecutor =
63
+ sharedDependencies?.actionExecutor ||
64
+ new action_executor_module_1.LitActionExecutor("standalone", this.config.debug, sharedDependencies?.authManager);
65
+ this.authManager = sharedDependencies?.authManager;
66
+ if (config.debug) {
67
+ console.log("šŸ”§ PKPMacros initialized with shared dependencies:", {
68
+ clientManager: !!sharedDependencies?.clientManager,
69
+ pkpMinter: !!sharedDependencies?.pkpMinter,
70
+ pkpAuthorizer: !!sharedDependencies?.pkpAuthorizer,
71
+ actionExecutor: !!sharedDependencies?.actionExecutor,
72
+ authManager: !!sharedDependencies?.authManager,
73
+ });
74
+ }
75
+ }
76
+ /**
77
+ * MACRO 1: Complete PKP Test Workflow
78
+ * Creates PKP -> Authorizes dummy action -> Burns PKP
79
+ * Perfect for testing and validation
80
+ */
81
+ async createAuthorizeAndBurn(litActionCid) {
82
+ const startTime = Date.now();
83
+ const cid = litActionCid || exports.AUTHORIZATION_DUMMY_CID;
84
+ if (this.config.debug) {
85
+ console.log("\nšŸš€ MACRO: Create + Authorize + Burn PKP");
86
+ console.log("=========================================");
87
+ console.log(` Network: ${this.config.network}`);
88
+ console.log(` LIT Action CID: ${cid}`);
89
+ console.log(` Signer: ${this.config.signer.address}`);
90
+ }
91
+ try {
92
+ // Step 1: Create PKP
93
+ if (this.config.debug) {
94
+ console.log("\n⚔ Step 1: Creating PKP...");
95
+ }
96
+ const minter = this.pkpMinter;
97
+ const pkpResult = await minter.mintAndVerifyPKP(this.config.pkpNftContract, this.provider);
98
+ if (this.config.debug) {
99
+ console.log(` āœ… PKP Created: ${pkpResult.tokenId}`);
100
+ console.log(` šŸ“ Address: ${pkpResult.ethAddress}`);
101
+ }
102
+ // Step 2: Authorize LIT Action
103
+ if (this.config.debug) {
104
+ console.log("\n⚔ Step 2: Authorizing LIT Action...");
105
+ }
106
+ const authorizer = this.pkpAuthorizer;
107
+ const authResult = await authorizer.authorizeAndVerify(pkpResult.tokenId, cid);
108
+ if (this.config.debug) {
109
+ console.log(` āœ… Authorization Complete: ${authResult.isPermitted}`);
110
+ console.log(` šŸ“‹ Permitted Actions: ${authResult.permittedActions.length}`);
111
+ }
112
+ // Step 3: Burn PKP
113
+ if (this.config.debug) {
114
+ console.log("\n⚔ Step 3: Burning PKP...");
115
+ }
116
+ const burnResult = await this.burnPKP(pkpResult.tokenId);
117
+ if (this.config.debug) {
118
+ console.log(` āœ… PKP Burned: ${burnResult.burned}`);
119
+ const duration = Date.now() - startTime;
120
+ console.log(`\nšŸŽÆ COMPLETE WORKFLOW FINISHED in ${duration}ms`);
121
+ console.log(" āœ… Create PKP");
122
+ console.log(" āœ… Authorize LIT Action");
123
+ console.log(" āœ… Burn PKP");
124
+ console.log(" šŸ”„ PKP lifecycle completed successfully!");
125
+ }
126
+ return {
127
+ // PKP Creation
128
+ tokenId: pkpResult.tokenId,
129
+ publicKey: pkpResult.publicKey,
130
+ ethAddress: pkpResult.ethAddress,
131
+ mintTransactionHash: pkpResult.transactionHash,
132
+ verified: pkpResult.verified,
133
+ owner: pkpResult.owner,
134
+ // Authorization
135
+ authTransactionHash: authResult.transactionHash,
136
+ isPermitted: authResult.isPermitted,
137
+ permittedActions: authResult.permittedActions,
138
+ // Burn
139
+ burnTransactionHash: burnResult.transactionHash,
140
+ burned: burnResult.burned,
141
+ // Metadata
142
+ workflow: "create-authorize-burn",
143
+ timestamp: startTime,
144
+ duration: Date.now() - startTime,
145
+ };
146
+ }
147
+ catch (error) {
148
+ if (this.config.debug) {
149
+ console.error("āŒ MACRO FAILED:", error);
150
+ }
151
+ throw error;
152
+ }
153
+ }
154
+ /**
155
+ * MACRO 2: Create, Authorize, and Burn PKP (Production Workflow)
156
+ * Creates PKP -> Authorizes Diamond Hands action -> Burns PKP for immutability
157
+ * This makes the PKP production-ready: only the authorized LIT action can sign
158
+ */
159
+ async createAuthorizeDhActionAndBurn(litActionCid) {
160
+ const startTime = Date.now();
161
+ const cid = litActionCid || exports.AUTHORIZATION_DUMMY_CID;
162
+ if (this.config.debug) {
163
+ console.log("\nšŸš€ MACRO: Create + Authorize + Burn PKP (Production Ready)");
164
+ console.log("=========================================================");
165
+ console.log(` Network: ${this.config.network}`);
166
+ console.log(` Diamond Hands Action CID: ${cid}`);
167
+ console.log(` Signer: ${this.config.signer.address}`);
168
+ console.log(" šŸ”’ Burn = Production Security (makes PKP immutable)");
169
+ }
170
+ try {
171
+ // Step 1: Create PKP
172
+ if (this.config.debug) {
173
+ console.log("\n⚔ Step 1: Creating PKP...");
174
+ }
175
+ const minter = this.pkpMinter;
176
+ const pkpResult = await minter.mintAndVerifyPKP(this.config.pkpNftContract, this.provider);
177
+ if (this.config.debug) {
178
+ console.log(` āœ… PKP Created: ${pkpResult.tokenId}`);
179
+ console.log(` šŸ“ Address: ${pkpResult.ethAddress}`);
180
+ }
181
+ // Step 2: Authorize Diamond Hands LIT Action
182
+ if (this.config.debug) {
183
+ console.log("\n⚔ Step 2: Authorizing Diamond Hands Action...");
184
+ }
185
+ const authorizer = this.pkpAuthorizer;
186
+ const authResult = await authorizer.authorizeAndVerify(pkpResult.tokenId, cid);
187
+ if (this.config.debug) {
188
+ console.log(` āœ… Authorization Complete: ${authResult.isPermitted}`);
189
+ console.log(` šŸ“‹ Permitted Actions: ${authResult.permittedActions.length}`);
190
+ }
191
+ // Step 3: Make PKP Immutable (Transfer to Burn Address)
192
+ if (this.config.debug) {
193
+ console.log("\n⚔ Step 3: Making PKP Immutable (Transfer to Burn Address)...");
194
+ console.log(" šŸ”’ This makes the PKP immutable and production-ready");
195
+ console.log(" šŸ”’ Only the authorized LIT action can sign");
196
+ console.log(" šŸ”’ Creator can no longer modify or add signers");
197
+ console.log(" šŸŽÆ PKP will exist on-chain but be permanently locked");
198
+ }
199
+ const burnResult = await this.burnPKP(pkpResult.tokenId);
200
+ // Step 4: Wait for network propagation before verification
201
+ if (this.config.debug) {
202
+ console.log("\n⚔ Step 4: Waiting for network propagation before verification...");
203
+ console.log(` ā³ Delay: ${this.config.pkpImmutabilityVerificationDelayMs || 5000}ms`);
204
+ }
205
+ await new Promise((resolve) => setTimeout(resolve, this.config.pkpImmutabilityVerificationDelayMs || 5000));
206
+ // Step 5: VERIFY the PKP is immutable (transferred to burn address)
207
+ if (this.config.debug) {
208
+ console.log("\n⚔ Step 5: VERIFYING PKP is actually immutable...");
209
+ }
210
+ const burnVerified = await this.verifyPKPBurned(pkpResult.tokenId);
211
+ if (!burnVerified) {
212
+ throw new Error(`IMMUTABILITY VERIFICATION FAILED: PKP ${pkpResult.tokenId} is not immutable!`);
213
+ }
214
+ if (this.config.debug) {
215
+ console.log(` āœ… PKP IMMUTABILITY VERIFIED: ${burnResult.burned} (on-chain confirmed)`);
216
+ const duration = Date.now() - startTime;
217
+ console.log(`\nšŸŽÆ PRODUCTION WORKFLOW FINISHED in ${duration}ms`);
218
+ console.log(" āœ… Create PKP");
219
+ console.log(" āœ… Authorize Diamond Hands Action");
220
+ console.log(" āœ… Make PKP Immutable (Transfer to Burn Address)");
221
+ console.log(" āœ… VERIFY PKP Immutability (On-chain Confirmation)");
222
+ console.log(` šŸ”’ PKP ${pkpResult.tokenId} is now PRODUCTION READY and IMMUTABLE!`);
223
+ console.log(" šŸŽÆ PKP exists on-chain and can be validated by address");
224
+ console.log(" šŸ“ Ready to be added to smart contract for validation");
225
+ }
226
+ return {
227
+ // PKP Creation
228
+ tokenId: pkpResult.tokenId,
229
+ publicKey: pkpResult.publicKey,
230
+ ethAddress: pkpResult.ethAddress,
231
+ mintTransactionHash: pkpResult.transactionHash,
232
+ verified: pkpResult.verified,
233
+ owner: pkpResult.owner,
234
+ // Authorization
235
+ authTransactionHash: authResult.transactionHash,
236
+ isPermitted: authResult.isPermitted,
237
+ permittedActions: authResult.permittedActions,
238
+ // Production Security Burn
239
+ burnTransactionHash: burnResult.transactionHash,
240
+ burned: burnResult.burned,
241
+ // Metadata
242
+ workflow: "create-authorize-burn",
243
+ timestamp: startTime,
244
+ duration: Date.now() - startTime,
245
+ };
246
+ }
247
+ catch (error) {
248
+ if (this.config.debug) {
249
+ console.error("āŒ PRODUCTION MACRO FAILED:", error);
250
+ }
251
+ throw error;
252
+ }
253
+ }
254
+ /**
255
+ * MACRO 3: Quick PKP Creation
256
+ * Just creates a PKP with verification
257
+ */
258
+ async quickCreatePKP() {
259
+ if (this.config.debug) {
260
+ console.log("\n⚔ MACRO: Quick Create PKP");
261
+ }
262
+ const minter = this.pkpMinter;
263
+ const result = await minter.mintAndVerifyPKP(this.config.pkpNftContract, this.provider);
264
+ if (this.config.debug) {
265
+ console.log(` āœ… Quick PKP Created: ${result.tokenId}`);
266
+ }
267
+ return result;
268
+ }
269
+ /**
270
+ * MACRO 4: Quick Authorization
271
+ * Just authorizes a LIT Action for an existing PKP
272
+ */
273
+ async quickAuthorize(tokenId, litActionCid) {
274
+ const cid = litActionCid || exports.AUTHORIZATION_DUMMY_CID;
275
+ if (this.config.debug) {
276
+ console.log(`\n⚔ MACRO: Quick Authorize PKP ${tokenId}`);
277
+ }
278
+ const authorizer = this.pkpAuthorizer;
279
+ const result = await authorizer.authorizeAndVerify(tokenId, cid);
280
+ if (this.config.debug) {
281
+ console.log(` āœ… Quick Authorization: ${result.isPermitted}`);
282
+ }
283
+ return result;
284
+ }
285
+ /**
286
+ * MACRO 5: PKP Immutability via Transfer
287
+ * Makes PKP immutable by transferring to burn address (instead of burning)
288
+ * This achieves the same security goal: PKP exists but cannot be modified
289
+ */
290
+ async burnPKP(tokenId) {
291
+ if (this.config.debug) {
292
+ console.log(`\nļæ½ MACRO: Making PKP Immutable ${tokenId}`);
293
+ console.log(" šŸ”’ TRANSFER TO BURN ADDRESS - PKP exists but locked forever");
294
+ console.log(" šŸŽÆ This achieves same security as burn: PKP cannot be modified");
295
+ }
296
+ try {
297
+ // Initialize contracts for transfer operation (OPTIMIZED)
298
+ const contractClient = await this.clientManager.getContracts(this.config.signer, {
299
+ litNetwork: this.config.network,
300
+ debug: this.config.debug,
301
+ });
302
+ // Use the PKP NFT contract for transfer
303
+ const pkpNftContract = contractClient.pkpNftContract.write;
304
+ // Get current owner
305
+ const currentOwner = await pkpNftContract.ownerOf(tokenId);
306
+ // Burn address - no one can control this address
307
+ const BURN_ADDRESS = "0x000000000000000000000000000000000000dEaD";
308
+ if (this.config.debug) {
309
+ console.log(` šŸ“ Current owner: ${currentOwner}`);
310
+ console.log(` šŸŽÆ Transferring to burn address: ${BURN_ADDRESS}`);
311
+ console.log(" šŸ”’ After transfer, PKP will be immutable forever");
312
+ }
313
+ // Transfer PKP to burn address (makes it immutable)
314
+ const transferTransaction = await pkpNftContract.transferFrom(currentOwner, BURN_ADDRESS, tokenId);
315
+ if (this.config.debug) {
316
+ console.log(` šŸ“¤ Transfer transaction sent: ${transferTransaction.hash}`);
317
+ console.log(" ā³ Waiting for transaction confirmation...");
318
+ }
319
+ // Wait for confirmation
320
+ const receipt = await transferTransaction.wait();
321
+ // Verify the transfer worked
322
+ const newOwner = await pkpNftContract.ownerOf(tokenId);
323
+ const isImmutable = newOwner.toLowerCase() === BURN_ADDRESS.toLowerCase();
324
+ if (this.config.debug) {
325
+ console.log(` āœ… PKP NOW IMMUTABLE: ${tokenId}`);
326
+ console.log(` šŸ”’ New owner: ${newOwner}`);
327
+ console.log(` šŸŽÆ Is immutable: ${isImmutable}`);
328
+ console.log(` ļæ½ Transaction confirmed: ${transferTransaction.hash}`);
329
+ console.log(` ⛽ Gas used: ${receipt.gasUsed.toString()}`);
330
+ console.log(" šŸ”’ PKP exists on-chain but can never be modified!");
331
+ }
332
+ return {
333
+ burned: isImmutable, // "burned" means immutable in our context
334
+ transactionHash: transferTransaction.hash,
335
+ };
336
+ }
337
+ catch (error) {
338
+ if (this.config.debug) {
339
+ console.error(` āŒ Transfer to burn address failed: ${error}`);
340
+ }
341
+ throw new Error(`Failed to make PKP immutable ${tokenId}: ${error instanceof Error ? error.message : String(error)}`);
342
+ }
343
+ }
344
+ /**
345
+ * UTILITY: Get PKP information and verify if it exists
346
+ */
347
+ async getPKPInfo(tokenId) {
348
+ const PKP_NFT_ABI = [
349
+ "function ownerOf(uint256 tokenId) view returns (address owner)",
350
+ "function exists(uint256 tokenId) view returns (bool)",
351
+ ];
352
+ // Get contract address dynamically from the LIT SDK
353
+ const contractClient = await this.clientManager.getContracts(this.config.signer, {
354
+ litNetwork: this.config.network,
355
+ debug: this.config.debug,
356
+ });
357
+ // Use the correct contract address for the current network
358
+ const pkpNftContractAddress = contractClient.pkpNftContract.read.address;
359
+ const contract = new ethers_1.ethers.Contract(pkpNftContractAddress, PKP_NFT_ABI, this.provider);
360
+ try {
361
+ if (this.config.debug) {
362
+ console.log(` šŸ” Querying PKP info for token ${tokenId}`);
363
+ console.log(` šŸ“‹ Contract address: ${pkpNftContractAddress}`);
364
+ console.log(` 🌐 Network: ${this.config.network}`);
365
+ }
366
+ const owner = await contract.ownerOf(tokenId);
367
+ if (this.config.debug) {
368
+ console.log(` āœ… PKP owner query successful: ${owner}`);
369
+ }
370
+ return {
371
+ tokenId,
372
+ owner,
373
+ exists: owner !== "0x0000000000000000000000000000000000000000",
374
+ };
375
+ }
376
+ catch (error) {
377
+ if (this.config.debug) {
378
+ console.log(` āŒ PKP owner query failed: ${error.message}`);
379
+ console.log(` šŸ“‹ Error code: ${error.code}`);
380
+ console.log(` šŸ“‹ Error reason: ${error.reason}`);
381
+ }
382
+ // If error contains "invalid token ID" or "ERC721: owner query for nonexistent token"
383
+ // then the PKP has been burned or never existed
384
+ if (error.message?.includes("invalid token") ||
385
+ error.message?.includes("nonexistent token") ||
386
+ error.message?.includes("ERC721")) {
387
+ return {
388
+ tokenId,
389
+ owner: null,
390
+ exists: false,
391
+ burned: true, // PKP was burned
392
+ };
393
+ }
394
+ throw error;
395
+ }
396
+ }
397
+ /**
398
+ * UTILITY: Verify PKP is immutable (transferred to burn address)
399
+ */
400
+ async verifyPKPBurned(tokenId) {
401
+ try {
402
+ const info = await this.getPKPInfo(tokenId);
403
+ // For immutable PKPs, check if owner is burn address
404
+ const BURN_ADDRESS = "0x000000000000000000000000000000000000dEaD";
405
+ const isImmutable = info.exists &&
406
+ info.owner &&
407
+ info.owner.toLowerCase() === BURN_ADDRESS.toLowerCase();
408
+ if (this.config.debug) {
409
+ console.log(` šŸ” Immutability verification: ${isImmutable ? "CONFIRMED" : "FAILED"}`);
410
+ console.log(` šŸ“‹ PKP ${tokenId} exists: ${info.exists}`);
411
+ console.log(` šŸ‘¤ Owner: ${info.owner}`);
412
+ console.log(` šŸ”’ Is owned by burn address: ${isImmutable}`);
413
+ console.log(` šŸŽÆ PKP is ${isImmutable ? "IMMUTABLE" : "still modifiable"}`);
414
+ }
415
+ return isImmutable;
416
+ }
417
+ catch (error) {
418
+ if (this.config.debug) {
419
+ console.error(` āŒ Immutability verification error: ${error}`);
420
+ }
421
+ // Don't hide errors - let caller handle them
422
+ throw new Error(`Failed to verify PKP immutability for ${tokenId}: ${error instanceof Error ? error.message : String(error)}`);
423
+ }
424
+ }
425
+ }
426
+ exports.PKPMacros = PKPMacros;
427
+ /**
428
+ * PKP Macro Utils - Proper Instantiated Class with Shared Dependencies
429
+ * No more instantiating new dependencies every function call!
430
+ */
431
+ class PKPMacroUtils {
432
+ constructor(network, debug = false, sharedDependencies, pkpImmutabilityVerificationDelayMs = 5000) {
433
+ this.network = network;
434
+ this.debug = debug;
435
+ this.pkpImmutabilityVerificationDelayMs = pkpImmutabilityVerificationDelayMs;
436
+ // Use shared dependencies if provided, otherwise create new ones
437
+ this.clientManager =
438
+ sharedDependencies?.clientManager ||
439
+ new client_manager_module_1.LitClientManager({
440
+ litNetwork: network,
441
+ debug,
442
+ alertWhenUnauthorized: false,
443
+ minNodeCount: 2,
444
+ });
445
+ this.actionExecutor =
446
+ sharedDependencies?.actionExecutor ||
447
+ new action_executor_module_1.LitActionExecutor("standalone", debug, sharedDependencies?.authManager);
448
+ // Store shared PKPMinter if provided
449
+ this.pkpMinter = sharedDependencies?.pkpMinter;
450
+ // Store shared PKPAuthorizer if provided
451
+ this.pkpAuthorizer = sharedDependencies?.pkpAuthorizer;
452
+ if (debug) {
453
+ console.log("šŸ”§ PKPMacroUtils initialized with shared dependencies");
454
+ }
455
+ }
456
+ /**
457
+ * Quick test workflow: Create + Authorize + Burn
458
+ */
459
+ async quickTestWorkflow(signer, options = {}) {
460
+ const macros = new PKPMacros({
461
+ signer,
462
+ network: this.network,
463
+ debug: this.debug,
464
+ }, {
465
+ clientManager: this.clientManager,
466
+ actionExecutor: this.actionExecutor,
467
+ });
468
+ return await macros.createAuthorizeAndBurn(options.litActionCid);
469
+ }
470
+ /**
471
+ * Quick production workflow: Create + Authorize + Burn (Production Ready)
472
+ */
473
+ async quickCreatePkpAuthorizeDhActionAndBurn(signer, options = {}) {
474
+ const macros = new PKPMacros({
475
+ signer,
476
+ network: this.network,
477
+ debug: this.debug,
478
+ pkpImmutabilityVerificationDelayMs: this.pkpImmutabilityVerificationDelayMs,
479
+ }, {
480
+ clientManager: this.clientManager,
481
+ actionExecutor: this.actionExecutor,
482
+ });
483
+ return await macros.createAuthorizeDhActionAndBurn(options.litActionCid);
484
+ }
485
+ /**
486
+ * Quick burn (cleanup utility)
487
+ */
488
+ static async quickBurn(signer, tokenId, options = {}) {
489
+ const macros = new PKPMacros({
490
+ signer,
491
+ network: options.network,
492
+ debug: options.debug,
493
+ });
494
+ return await macros.burnPKP(tokenId);
495
+ }
496
+ /**
497
+ * PKP Validator Macro - Validate PKP Security via LIT Action
498
+ *
499
+ * This is the optimized macro that validates a PKP is created, granted, and burned
500
+ * correctly against a specific CID by executing the pkp-validator LIT Action and
501
+ * verifying the signature returned.
502
+ *
503
+ * @param targetPkpTokenId - The PKP token ID to validate
504
+ * @param expectedCid - The expected CID in hex format that the PKP should be authorized for
505
+ * @param signer - The wallet to use for session signatures
506
+ * @param options - Configuration options
507
+ * @returns PKP validation result with signature verification
508
+ */
509
+ static async validatePKPSecurity(targetPkpTokenId, expectedCid, signer, options = {}) {
510
+ const debug = options.debug || false;
511
+ const network = options.network;
512
+ if (!network) {
513
+ throw new Error("Network must be specified - no default network allowed");
514
+ }
515
+ if (debug) {
516
+ console.log("\nšŸ” PKP Security Validation Macro");
517
+ console.log(` Target PKP: ${targetPkpTokenId}`);
518
+ console.log(` Expected CID: ${expectedCid}`);
519
+ console.log(` Network: ${network}`);
520
+ }
521
+ try {
522
+ // Initialize LitActionExecutor with configuration
523
+ const actionExecutor = new action_executor_module_1.LitActionExecutor("standalone", debug);
524
+ // Create a client manager for this static method
525
+ const clientManager = new client_manager_module_1.LitClientManager({
526
+ litNetwork: network,
527
+ debug: debug || false,
528
+ alertWhenUnauthorized: false,
529
+ minNodeCount: 2,
530
+ });
531
+ // Get LIT client
532
+ const litClient = await clientManager.getClient({
533
+ litNetwork: network,
534
+ debug: debug,
535
+ });
536
+ // Get PKP Validator CID and signer PKP
537
+ const pkpValidatorCid = dh_lit_actions_1.DH_LIT_ACTIONS.pkpValidator.cid;
538
+ const signerPkp = options.signerPkp || {
539
+ tokenId: "0x1234", // Default fallback
540
+ publicKey: "041a4c73c35215b50fb3708e4ad8cbd0ebf67231fb25e4e095f9f1dcb70f408ab24d28a8460ef436786e66c967d7ebf1d3f3e6441fd661afd9dc7d4f2c1d416f59",
541
+ ethAddress: "0x5449b0C085Fbd9D1A5F02d30b02aA042016A8756",
542
+ };
543
+ if (debug) {
544
+ console.log(` PKP Validator CID: ${pkpValidatorCid}`);
545
+ console.log(` Signer PKP: ${signerPkp.tokenId}`);
546
+ }
547
+ // Execute PKP Validator LIT Action
548
+ const litResult = await actionExecutor.executeAction({
549
+ cid: pkpValidatorCid,
550
+ pkpPublicKey: signerPkp.publicKey,
551
+ params: {
552
+ targetPkpTokenId,
553
+ expectedCid,
554
+ message: `PKP validation via macro: ${Date.now()}`,
555
+ },
556
+ signer,
557
+ }, litClient);
558
+ if (debug) {
559
+ console.log(` LIT Action Success: ${litResult.success}`);
560
+ console.log(` Response: ${litResult.response || "NONE"}`);
561
+ console.log(` Error: ${litResult.error || "NONE"}`);
562
+ }
563
+ // Parse response and verify signature
564
+ if (litResult.success && litResult.response) {
565
+ try {
566
+ const responseData = JSON.parse(litResult.response);
567
+ // Verify signature if present
568
+ let signatureValid = false;
569
+ let pkpAddress = "";
570
+ let recoveredAddress = "";
571
+ if (responseData.signature) {
572
+ if (debug) {
573
+ console.log(" šŸ” Verifying signature authenticity...");
574
+ }
575
+ try {
576
+ // Compute PKP address from public key
577
+ pkpAddress = ethers_1.ethers.utils.computeAddress(`0x${signerPkp.publicKey}`);
578
+ // Recover address from signature
579
+ const signedMessage = responseData.signedMessage;
580
+ if (signedMessage) {
581
+ recoveredAddress = ethers_1.ethers.utils.verifyMessage(signedMessage, responseData.signature);
582
+ signatureValid =
583
+ pkpAddress.toLowerCase() === recoveredAddress.toLowerCase();
584
+ if (debug) {
585
+ console.log(` PKP Address: ${pkpAddress}`);
586
+ console.log(` Recovered Address: ${recoveredAddress}`);
587
+ console.log(` Signature Valid: ${signatureValid ? "āœ…" : "āŒ"}`);
588
+ }
589
+ }
590
+ else {
591
+ throw new Error("No signed message found for verification");
592
+ }
593
+ }
594
+ catch (verifyError) {
595
+ if (debug) {
596
+ console.log(` āŒ Signature verification failed: ${verifyError}`);
597
+ }
598
+ throw new Error(`Signature verification failed: ${verifyError instanceof Error
599
+ ? verifyError.message
600
+ : verifyError}`);
601
+ }
602
+ }
603
+ // Return parsed result
604
+ return {
605
+ success: responseData.success || false,
606
+ signature: responseData.signature,
607
+ error: responseData.error,
608
+ signatureValid,
609
+ pkpAddress,
610
+ recoveredAddress,
611
+ validationResult: responseData.validated
612
+ ? {
613
+ exists: responseData.validated.exists || false,
614
+ isImmutable: responseData.validated.immutable || false,
615
+ hasOnlyOneAction: responseData.validated.singleAction || false,
616
+ matchesExpectedCID: responseData.validated.correctCid || false,
617
+ allValid: responseData.validated.exists &&
618
+ responseData.validated.immutable &&
619
+ responseData.validated.singleAction &&
620
+ responseData.validated.correctCid,
621
+ }
622
+ : undefined,
623
+ };
624
+ }
625
+ catch (parseError) {
626
+ if (debug) {
627
+ console.log(" Response is not JSON, treating as execution error");
628
+ }
629
+ return {
630
+ success: false,
631
+ error: "Failed to parse LIT Action response",
632
+ signatureValid: false,
633
+ };
634
+ }
635
+ }
636
+ // LIT Action execution failed
637
+ return {
638
+ success: false,
639
+ error: litResult.error || "LIT Action execution failed",
640
+ signatureValid: false,
641
+ };
642
+ }
643
+ catch (error) {
644
+ if (debug) {
645
+ console.error(" āŒ PKP validation macro failed:", error);
646
+ }
647
+ return {
648
+ success: false,
649
+ error: error instanceof Error ? error.message : "Unknown error",
650
+ signatureValid: false,
651
+ };
652
+ }
653
+ }
654
+ }
655
+ exports.PKPMacroUtils = PKPMacroUtils;
656
+ //# sourceMappingURL=pkp-macros.module.js.map