|
| 1 | +import { createAccBuilder } from '@lit-protocol/access-control-conditions'; |
| 2 | +import { ViemAccountAuthenticator } from '@lit-protocol/auth'; |
| 3 | +import { createSiweMessage } from '@lit-protocol/auth-helpers'; |
| 4 | +import { createEnvVars } from '../../helper/createEnvVars'; |
| 5 | +import { createTestAccount } from '../../helper/createTestAccount'; |
| 6 | +import { createTestEnv } from '../../helper/createTestEnv'; |
| 7 | + |
| 8 | +const CHECK_CONDITIONS_LIT_ACTION = ` |
| 9 | +(async () => { |
| 10 | + const { conditions, authSig } = jsParams; |
| 11 | + const isAuthorized = await Lit.Actions.checkConditions({ |
| 12 | + conditions, |
| 13 | + authSig, |
| 14 | + chain: 'ethereum', |
| 15 | + }); |
| 16 | +
|
| 17 | + Lit.Actions.setResponse({ response: isAuthorized ? 'true' : 'false' }); |
| 18 | +})(); |
| 19 | +`; |
| 20 | + |
| 21 | +describe('PKP AuthSig Access Control', () => { |
| 22 | + let testEnv: Awaited<ReturnType<typeof createTestEnv>>; |
| 23 | + |
| 24 | + beforeAll(async () => { |
| 25 | + const envVars = createEnvVars(); |
| 26 | + testEnv = await createTestEnv(envVars); |
| 27 | + }); |
| 28 | + |
| 29 | + it('allows a PKP to satisfy wallet-ownership ACCs via a PKP-generated authSig', async () => { |
| 30 | + const pkpOwner = await createTestAccount(testEnv, { |
| 31 | + label: 'PKP ACC Owner', |
| 32 | + fundAccount: true, |
| 33 | + fundLedger: true, |
| 34 | + hasEoaAuthContext: true, |
| 35 | + hasPKP: true, |
| 36 | + fundPKP: true, |
| 37 | + hasPKPAuthContext: true, |
| 38 | + fundPKPLedger: true, |
| 39 | + }); |
| 40 | + |
| 41 | + const { pkp, pkpAuthContext, pkpViemAccount } = pkpOwner; |
| 42 | + |
| 43 | + if (!pkp || !pkp.ethAddress) { |
| 44 | + throw new Error( |
| 45 | + 'PKP data with ethereum address is required for this test' |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + if (!pkpAuthContext) { |
| 50 | + throw new Error('PKP auth context was not created'); |
| 51 | + } |
| 52 | + |
| 53 | + if (!pkpViemAccount) { |
| 54 | + throw new Error('PKP viem account was not initialized'); |
| 55 | + } |
| 56 | + |
| 57 | + // Ensure the PKP ledger has enough balance to pay for executeJs |
| 58 | + await testEnv.masterPaymentManager.depositForUser({ |
| 59 | + userAddress: pkp.ethAddress as `0x${string}`, |
| 60 | + amountInEth: '0.2', |
| 61 | + }); |
| 62 | + |
| 63 | + const accessControlConditions = createAccBuilder() |
| 64 | + .requireWalletOwnership(pkp.ethAddress) |
| 65 | + .on('ethereum') |
| 66 | + .build(); |
| 67 | + |
| 68 | + const siweMessage = await createSiweMessage({ |
| 69 | + walletAddress: pkpViemAccount.address, |
| 70 | + nonce: (await testEnv.litClient.getContext()).latestBlockhash, |
| 71 | + }); |
| 72 | + |
| 73 | + const pkpAuthSig = await ViemAccountAuthenticator.createAuthSig( |
| 74 | + pkpViemAccount, |
| 75 | + siweMessage |
| 76 | + ); |
| 77 | + |
| 78 | + expect(pkpAuthSig.address?.toLowerCase()).toBe( |
| 79 | + pkp.ethAddress.toLowerCase() |
| 80 | + ); |
| 81 | + |
| 82 | + const executionResult = await testEnv.litClient.executeJs({ |
| 83 | + code: CHECK_CONDITIONS_LIT_ACTION, |
| 84 | + authContext: pkpAuthContext, |
| 85 | + jsParams: { |
| 86 | + conditions: accessControlConditions, |
| 87 | + // authSig: pkpAuthSig, |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(executionResult.response).toBe('true'); |
| 92 | + }); |
| 93 | +}); |
0 commit comments