|
| 1 | +import { ViemAccountAuthenticator } from '@lit-protocol/auth'; |
| 2 | +import { createLitClient } from '@lit-protocol/lit-client'; |
| 3 | +import { nagaDev } from '@lit-protocol/networks'; |
| 4 | +import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; |
| 5 | +import { Account } from 'viem'; |
| 6 | +import { fundAccount } from '../helper/fundAccount'; |
| 7 | + |
| 8 | +const NETWORK_MODULE = nagaDev; |
| 9 | +const LIVE_MASTER_ACCOUNT = process.env['LIVE_MASTER_ACCOUNT'] as `0x${string}`; |
| 10 | +const LIVE_NETWORK_FUNDING_AMOUNT = '0.01'; |
| 11 | + |
| 12 | +describe('JSS-11: PKP Management and Pagination', () => { |
| 13 | + let masterViemAccount: Account; |
| 14 | + let randomViemAccount: Account; |
| 15 | + let viemAccountAuthData: Awaited< |
| 16 | + ReturnType<typeof ViemAccountAuthenticator.authenticate> |
| 17 | + >; |
| 18 | + let litClient: Awaited<ReturnType<typeof createLitClient>>; |
| 19 | + |
| 20 | + beforeAll(async () => { |
| 21 | + // ---------- Authentication Layer ---------- |
| 22 | + masterViemAccount = privateKeyToAccount(LIVE_MASTER_ACCOUNT); |
| 23 | + randomViemAccount = privateKeyToAccount(generatePrivateKey()); |
| 24 | + |
| 25 | + await fundAccount(randomViemAccount, masterViemAccount, NETWORK_MODULE, { |
| 26 | + ifLessThan: '0', |
| 27 | + thenFundWith: LIVE_NETWORK_FUNDING_AMOUNT, |
| 28 | + }); |
| 29 | + |
| 30 | + viemAccountAuthData = await ViemAccountAuthenticator.authenticate( |
| 31 | + randomViemAccount |
| 32 | + ); |
| 33 | + |
| 34 | + // ---------- Client Setup Layer ---------- |
| 35 | + litClient = await createLitClient({ |
| 36 | + network: NETWORK_MODULE, |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('Initial State Tests', () => { |
| 41 | + it('should return empty array for new account with no PKPs', async () => { |
| 42 | + const result = await litClient.viewPKPsByAuthData({ |
| 43 | + authData: viemAccountAuthData, |
| 44 | + pagination: { limit: 10, offset: 0 }, |
| 45 | + }); |
| 46 | + |
| 47 | + expect(result.pkps.length).toBe(0); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('PKP Creation and Management', () => { |
| 52 | + it('should mint first PKP successfully', async () => { |
| 53 | + // Mint PKP |
| 54 | + await litClient.mintWithAuth({ |
| 55 | + account: randomViemAccount, |
| 56 | + authData: viemAccountAuthData, |
| 57 | + scopes: ['sign-anything'], |
| 58 | + }); |
| 59 | + |
| 60 | + // Verify PKP exists |
| 61 | + const result = await litClient.viewPKPsByAuthData({ |
| 62 | + authData: viemAccountAuthData, |
| 63 | + pagination: { limit: 10, offset: 0 }, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(result.pkps.length).toBe(1); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should mint second PKP and return both PKPs', async () => { |
| 70 | + // Mint second PKP |
| 71 | + await litClient.mintWithAuth({ |
| 72 | + account: randomViemAccount, |
| 73 | + authData: viemAccountAuthData, |
| 74 | + scopes: ['sign-anything'], |
| 75 | + }); |
| 76 | + |
| 77 | + // Verify both PKPs exist |
| 78 | + const result = await litClient.viewPKPsByAuthData({ |
| 79 | + authData: viemAccountAuthData, |
| 80 | + pagination: { limit: 10, offset: 0 }, |
| 81 | + }); |
| 82 | + |
| 83 | + expect(result.pkps.length).toBe(2); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('Pagination Functionality', () => { |
| 88 | + it('should respect limit parameter', async () => { |
| 89 | + const result = await litClient.viewPKPsByAuthData({ |
| 90 | + authData: viemAccountAuthData, |
| 91 | + pagination: { limit: 1, offset: 0 }, |
| 92 | + }); |
| 93 | + |
| 94 | + expect(result.pkps.length).toBe(1); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should respect offset parameter and return different PKPs', async () => { |
| 98 | + // Get first PKP |
| 99 | + const firstResult = await litClient.viewPKPsByAuthData({ |
| 100 | + authData: viemAccountAuthData, |
| 101 | + pagination: { limit: 1, offset: 0 }, |
| 102 | + }); |
| 103 | + |
| 104 | + // Get second PKP |
| 105 | + const secondResult = await litClient.viewPKPsByAuthData({ |
| 106 | + authData: viemAccountAuthData, |
| 107 | + pagination: { limit: 1, offset: 1 }, |
| 108 | + }); |
| 109 | + |
| 110 | + expect(firstResult.pkps.length).toBe(1); |
| 111 | + expect(secondResult.pkps.length).toBe(1); |
| 112 | + expect(firstResult.pkps[0].tokenId).not.toBe( |
| 113 | + secondResult.pkps[0].tokenId |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should handle large limit values correctly', async () => { |
| 118 | + const result = await litClient.viewPKPsByAuthData({ |
| 119 | + authData: viemAccountAuthData, |
| 120 | + pagination: { limit: 100, offset: 0 }, |
| 121 | + }); |
| 122 | + |
| 123 | + // Should return all PKPs (currently 2) |
| 124 | + expect(result.pkps.length).toBe(2); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should handle offset beyond available PKPs', async () => { |
| 128 | + const result = await litClient.viewPKPsByAuthData({ |
| 129 | + authData: viemAccountAuthData, |
| 130 | + pagination: { limit: 10, offset: 10 }, |
| 131 | + }); |
| 132 | + |
| 133 | + expect(result.pkps.length).toBe(0); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('Data Consistency Tests', () => { |
| 138 | + it('should return consistent results across multiple calls', async () => { |
| 139 | + // Get PKPs multiple times |
| 140 | + const firstCall = await litClient.viewPKPsByAuthData({ |
| 141 | + authData: viemAccountAuthData, |
| 142 | + pagination: { limit: 10, offset: 0 }, |
| 143 | + }); |
| 144 | + |
| 145 | + const secondCall = await litClient.viewPKPsByAuthData({ |
| 146 | + authData: viemAccountAuthData, |
| 147 | + pagination: { limit: 10, offset: 0 }, |
| 148 | + }); |
| 149 | + |
| 150 | + expect(secondCall.pkps.length).toBe(firstCall.pkps.length); |
| 151 | + expect(secondCall.pkps.map((p) => p.tokenId).sort()).toEqual( |
| 152 | + firstCall.pkps.map((p) => p.tokenId).sort() |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should maintain correct PKP data structure', async () => { |
| 157 | + const result = await litClient.viewPKPsByAuthData({ |
| 158 | + authData: viemAccountAuthData, |
| 159 | + pagination: { limit: 1, offset: 0 }, |
| 160 | + }); |
| 161 | + |
| 162 | + expect(result.pkps.length).toBe(1); |
| 163 | + const pkp = result.pkps[0]; |
| 164 | + |
| 165 | + // Verify PKP has required properties |
| 166 | + expect(pkp).toHaveProperty('tokenId'); |
| 167 | + expect(pkp).toHaveProperty('publicKey'); |
| 168 | + expect(pkp).toHaveProperty('ethAddress'); |
| 169 | + |
| 170 | + // Verify data types |
| 171 | + expect(typeof pkp.tokenId).toBe('string'); |
| 172 | + expect(typeof pkp.publicKey).toBe('string'); |
| 173 | + expect(typeof pkp.ethAddress).toBe('string'); |
| 174 | + |
| 175 | + // Verify format |
| 176 | + expect(pkp.publicKey).toMatch(/^0x[a-fA-F0-9]+$/); |
| 177 | + expect(pkp.ethAddress).toMatch(/^0x[a-fA-F0-9]{40}$/); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments