|
| 1 | +import { createAuthManager, storagePlugins } from '@lit-protocol/auth'; |
| 2 | +import { createLitClient } from '@lit-protocol/lit-client'; |
| 3 | +import { nagaDev } from '@lit-protocol/networks'; |
| 4 | +import { privateKeyToAccount } from 'viem/accounts'; |
| 5 | +import { |
| 6 | + createAccBuilder, |
| 7 | + UnifiedAccessControlCondition, |
| 8 | +} from '@lit-protocol/access-control-conditions'; |
| 9 | + |
| 10 | +import { createPublicClient, http } from 'viem'; |
| 11 | +import { baseSepolia } from 'viem/chains'; |
| 12 | + |
| 13 | +const liveMasterAccount = privateKeyToAccount( |
| 14 | + process.env['LIVE_MASTER_ACCOUNT'] as `0x${string}` |
| 15 | +); |
| 16 | + |
| 17 | +const rawAccs = [ |
| 18 | + { |
| 19 | + conditionType: 'evmContract', |
| 20 | + contractAddress: '0x036CbD53842c5426634e7929541eC2318f3dCF7e', |
| 21 | + functionName: 'balanceOf', |
| 22 | + functionParams: [':userAddress'], |
| 23 | + functionAbi: { |
| 24 | + inputs: [{ internalType: 'address', name: 'account', type: 'address' }], |
| 25 | + name: 'balanceOf', |
| 26 | + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], |
| 27 | + stateMutability: 'view', |
| 28 | + type: 'function', |
| 29 | + }, |
| 30 | + chain: 'baseSepolia', |
| 31 | + returnValueTest: { |
| 32 | + key: '', |
| 33 | + comparator: '>=', |
| 34 | + value: '0', |
| 35 | + }, |
| 36 | + }, |
| 37 | +] as UnifiedAccessControlCondition; |
| 38 | + |
| 39 | +describe('jss100 custom contract accs', () => { |
| 40 | + beforeAll(async () => { |
| 41 | + const client = createPublicClient({ |
| 42 | + chain: baseSepolia, |
| 43 | + transport: http(), |
| 44 | + }); |
| 45 | + |
| 46 | + const balance = await client.readContract({ |
| 47 | + address: '0x036CbD53842c5426634e7929541eC2318f3dCF7e', |
| 48 | + abi: [ |
| 49 | + { |
| 50 | + constant: true, |
| 51 | + inputs: [{ name: 'account', type: 'address' }], |
| 52 | + name: 'balanceOf', |
| 53 | + outputs: [{ name: '', type: 'uint256' }], |
| 54 | + payable: false, |
| 55 | + stateMutability: 'view', |
| 56 | + type: 'function', |
| 57 | + }, |
| 58 | + ], |
| 59 | + functionName: 'balanceOf', |
| 60 | + args: [liveMasterAccount.address], |
| 61 | + }); |
| 62 | + |
| 63 | + if (BigInt(balance) <= 0n) { |
| 64 | + throw new Error( |
| 65 | + `Test account ${liveMasterAccount.address} has no balance for token 0x036CbD53842c5426634e7929541eC2318f3dCF7e. Please get some before running the test.` |
| 66 | + ); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + it('should be able to use the custom contract accs api - evmContract conditions', async () => { |
| 71 | + console.log( |
| 72 | + '🔧 [TEST] Testing evmContract conditions (rawAccs):', |
| 73 | + JSON.stringify(rawAccs, null, 2) |
| 74 | + ); |
| 75 | + |
| 76 | + console.log( |
| 77 | + 'Setting up permissions for address:', |
| 78 | + liveMasterAccount.address |
| 79 | + ); |
| 80 | + |
| 81 | + console.log('🔧 [SESSION] Creating fresh litClient and clearing storage'); |
| 82 | + |
| 83 | + const litClient = await createLitClient({ network: nagaDev }); |
| 84 | + |
| 85 | + // Use unique storage path with timestamp to prevent session reuse |
| 86 | + const uniqueStoragePath = `.e2e/jss100-${Date.now()}`; |
| 87 | + console.log('🔧 [SESSION] Using unique storage path:', uniqueStoragePath); |
| 88 | + |
| 89 | + const authManager = createAuthManager({ |
| 90 | + storage: storagePlugins.localStorageNode({ |
| 91 | + appName: 'jss100-custom-contract-accs', |
| 92 | + networkName: 'naga-dev', |
| 93 | + storagePath: uniqueStoragePath, |
| 94 | + }), |
| 95 | + }); |
| 96 | + |
| 97 | + /** |
| 98 | + * ==================================== |
| 99 | + * Create the auth context |
| 100 | + * ==================================== |
| 101 | + */ |
| 102 | + const aliceEoaAuthContext = await authManager.createEoaAuthContext({ |
| 103 | + config: { |
| 104 | + account: liveMasterAccount, |
| 105 | + }, |
| 106 | + authConfig: { |
| 107 | + statement: 'I authorize the Lit Protocol to execute this Lit Action.', |
| 108 | + domain: 'example.com', |
| 109 | + resources: [ |
| 110 | + ['lit-action-execution', '*'], |
| 111 | + ['pkp-signing', '*'], |
| 112 | + ['access-control-condition-decryption', '*'], |
| 113 | + ], |
| 114 | + capabilityAuthSigs: [], |
| 115 | + expiration: new Date(Date.now() + 1000 * 60 * 15).toISOString(), |
| 116 | + }, |
| 117 | + litClient: litClient, |
| 118 | + }); |
| 119 | + |
| 120 | + // Handle both array and single condition formats correctly |
| 121 | + const unifiedAccs = createAccBuilder().unifiedAccs(rawAccs).build(); |
| 122 | + |
| 123 | + console.log('unifiedAccs:', JSON.stringify(unifiedAccs, null, 2)); |
| 124 | + |
| 125 | + const stringData = 'Hello world'; |
| 126 | + const encryptedStringData = await litClient.encrypt({ |
| 127 | + dataToEncrypt: stringData, |
| 128 | + unifiedAccessControlConditions: unifiedAccs, |
| 129 | + }); |
| 130 | + |
| 131 | + console.log(encryptedStringData); |
| 132 | + |
| 133 | + // Use the same account to decrypt the data |
| 134 | + const decryptedStringData = await litClient.decrypt({ |
| 135 | + data: encryptedStringData, |
| 136 | + unifiedAccessControlConditions: unifiedAccs, |
| 137 | + authContext: aliceEoaAuthContext, |
| 138 | + }); |
| 139 | + |
| 140 | + console.log('decryptedStringData:', decryptedStringData); |
| 141 | + |
| 142 | + expect(decryptedStringData.convertedData).toBe(stringData); |
| 143 | + }); |
| 144 | +}); |
0 commit comments