1+ // Run this command for this demo:
2+ // LOG_LEVEL=silent NETWORK=naga-dev bun run ./e2e/src/demo/add-permitted-address-demo.ts
3+
4+ //
5+ // This test if a PKP EOA Auth Method could add a permitted address via the PKPViemAccount
6+ //
7+ import { generatePrivateKey , privateKeyToAccount } from 'viem/accounts' ;
8+ import { nonceManager } from 'viem' ;
9+ import { fundAccount } from '../helper/fundAccount' ;
10+ import { createLitClient } from '@lit-protocol/lit-client' ;
11+ import {
12+ createAuthManager ,
13+ storagePlugins ,
14+ ViemAccountAuthenticator ,
15+ } from '@lit-protocol/auth' ;
16+
17+ // -- Configurations
18+ const { nagaLocal } = await import ( '@lit-protocol/networks' ) ;
19+ const LOCAL_NETWORK_FUNDING_AMOUNT = '1' ;
20+
21+ // -- Master account to fund the alice(test) account
22+ const localMasterAccount = privateKeyToAccount (
23+ process . env [ 'LOCAL_MASTER_ACCOUNT' ] as `0x${string } `,
24+ {
25+ nonceManager : nonceManager ,
26+ }
27+ ) ;
28+
29+ // -- EOA Test account via Viem
30+ const aliceViemAccount = privateKeyToAccount ( generatePrivateKey ( ) ) ;
31+
32+ // -- Using the authenticator to get the Auth Data
33+ const aliceViemAccountAuthData = await ViemAccountAuthenticator . authenticate (
34+ aliceViemAccount
35+ ) ;
36+
37+ console . log ( "✅ aliceViemAccountAuthData:" , aliceViemAccountAuthData ) ;
38+
39+ try {
40+ await fundAccount ( aliceViemAccount , localMasterAccount , nagaLocal , {
41+ ifLessThan : LOCAL_NETWORK_FUNDING_AMOUNT ,
42+ thenFundWith : LOCAL_NETWORK_FUNDING_AMOUNT ,
43+ } ) ;
44+ console . log ( "✅ Account Funded." )
45+ } catch ( e ) {
46+ throw new Error ( "❌ Failed to fund account." )
47+ }
48+
49+ /**
50+ * ====================================
51+ * Initialise the LitClient
52+ * ====================================
53+ */
54+ const litClient = await createLitClient ( { network : nagaLocal } ) ;
55+ console . log ( "✅ Created Lit Client" )
56+
57+ /**
58+ * ====================================
59+ * Initialise the AuthManager
60+ * ====================================
61+ */
62+ const authManager = createAuthManager ( {
63+ storage : storagePlugins . localStorageNode ( {
64+ appName : 'my-local-testing-app' ,
65+ networkName : 'local-test' ,
66+ storagePath : './lit-auth-local' ,
67+ } ) ,
68+ } ) ;
69+ console . log ( "✅ Created Auth Manager" )
70+
71+ // Minting a new PKP
72+ const tx = await litClient . mintWithAuth ( {
73+ account : aliceViemAccount ,
74+ authData : aliceViemAccountAuthData ,
75+ scopes : [ 'sign-anything' ] ,
76+ } ) ;
77+ console . log ( "✅ TX 1 done" ) ;
78+ console . log ( "ℹ️ tx:" , tx )
79+
80+ const pkpInfo = tx . data ;
81+ console . log ( "✅ pkpInfo:" , pkpInfo ) ;
82+
83+ const pkpPermissionsManagerForAliceViemAccount = await litClient . getPKPPermissionsManager ( {
84+ pkpIdentifier : {
85+ tokenId : pkpInfo . tokenId ,
86+ } ,
87+ account : aliceViemAccount ,
88+ } ) ;
89+
90+ console . log ( "✅ pkpPermissionsManagerForAliceViemAccount:" , await pkpPermissionsManagerForAliceViemAccount . getPermissionsContext ( ) ) ;
91+
92+ // check is address permitted
93+ const aliceViemAccountIsPermitted = await pkpPermissionsManagerForAliceViemAccount . isPermittedAddress ( {
94+ address : aliceViemAccount . address ,
95+ } ) ;
96+
97+ console . log ( `❗️ ${ aliceViemAccount . address } is ${ aliceViemAccountIsPermitted ? 'permitted' : 'NOT permitted' } ` ) ;
98+
99+ // check if pkp address is permitted
100+ const pkpIsPermitted = await pkpPermissionsManagerForAliceViemAccount . isPermittedAddress ( {
101+ address : pkpInfo . ethAddress ,
102+ } ) ;
103+
104+ console . log ( `❗️ ${ pkpInfo . ethAddress } is ${ pkpIsPermitted ? 'permitted' : 'NOT permitted' } ` ) ;
105+
106+
107+ const authContext = await authManager . createPkpAuthContext ( {
108+ authData : aliceViemAccountAuthData ,
109+ pkpPublicKey : pkpInfo . pubkey ,
110+ authConfig : {
111+ capabilityAuthSigs : [ ] ,
112+ expiration : new Date ( Date . now ( ) + 1000 * 60 * 60 * 24 ) . toISOString ( ) ,
113+ statement : "" ,
114+ domain : "" ,
115+ resources : [
116+ [ "pkp-signing" , "*" ] ,
117+ [ "lit-action-execution" , "*" ] ,
118+ ] ,
119+ } ,
120+ litClient,
121+ } ) ;
122+
123+ console . log ( "authContext:" , authContext ) ;
124+
125+ const pkpViemAccount = await litClient . getPkpViemAccount ( {
126+ pkpPublicKey : pkpInfo . pubkey ,
127+ authContext : authContext ,
128+ chainConfig : nagaLocal . getChainConfig ( ) ,
129+ } ) ;
130+
131+ await fundAccount ( pkpViemAccount , localMasterAccount , nagaLocal , {
132+ ifLessThan : LOCAL_NETWORK_FUNDING_AMOUNT ,
133+ thenFundWith : LOCAL_NETWORK_FUNDING_AMOUNT ,
134+ } ) ;
135+
136+ const pkpViemAccountPermissionsManager = await litClient . getPKPPermissionsManager ( {
137+ pkpIdentifier : {
138+ tokenId : pkpInfo . tokenId ,
139+ } ,
140+ account : pkpViemAccount ,
141+ } ) ;
142+
143+ try {
144+ const tx2 = await pkpViemAccountPermissionsManager . addPermittedAddress ( {
145+ address : "0x1234567890123456789012345678901234567890" ,
146+ scopes : [ "sign-anything" ] ,
147+ } ) ;
148+ console . log ( 'tx2:' , tx2 )
149+ } catch ( e ) {
150+ throw new Error ( e ) ;
151+ }
152+
153+ console . log ( "✅ pkpViemAccountPermissionsManager:" , await pkpViemAccountPermissionsManager . getPermissionsContext ( ) ) ;
154+
155+ process . exit ( ) ;
0 commit comments