Skip to content

Commit 475d495

Browse files
committed
feat(PKPPermissionsManager): implement addPermittedAuthMethod and removePermittedAuthMethodScope methods with corresponding handlers and tests
1 parent e18c696 commit 475d495

File tree

8 files changed

+530
-11
lines changed

8 files changed

+530
-11
lines changed

e2e/src/helper/tests/pkp-permissions-manager-flow.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,92 @@ export const createPkpPermissionsManagerFlowTest = (
109109

110110
assert.toBe(finalAddressPermitted, initialAddressPermitted);
111111
assert.toBe(finalActionPermitted, initialActionPermitted);
112+
113+
// Test 8: Verify new addPermittedAuthMethod method exists and is callable
114+
assert.toBeDefined(pkpPermissionsManager.addPermittedAuthMethod);
115+
assert.toBe(typeof pkpPermissionsManager.addPermittedAuthMethod, 'function');
116+
117+
// Test 9: Verify new removePermittedAuthMethodScope method exists and is callable
118+
assert.toBeDefined(pkpPermissionsManager.removePermittedAuthMethodScope);
119+
assert.toBe(typeof pkpPermissionsManager.removePermittedAuthMethodScope, 'function');
120+
121+
// Test 10: Actually test addPermittedAuthMethod functionality
122+
const testAuthMethodParams = {
123+
authMethodType: 1, // EthWallet
124+
authMethodId: '0x1234567890abcdef1234567890abcdef12345678',
125+
userPubkey: '0x04abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
126+
scopes: ['sign-anything'] as const,
127+
};
128+
129+
// Get initial auth methods count
130+
const initialAuthMethods = await pkpPermissionsManager.getPermittedAuthMethods();
131+
const initialAuthMethodsCount = initialAuthMethods.length;
132+
133+
console.log('🧪 Adding test auth method...');
134+
// Add the test auth method
135+
const addAuthMethodTx = await pkpPermissionsManager.addPermittedAuthMethod(testAuthMethodParams);
136+
assert.toBeDefined(addAuthMethodTx.hash);
137+
assert.toBeDefined(addAuthMethodTx.receipt);
138+
assert.toBe(addAuthMethodTx.receipt.status, 'success');
139+
140+
// Verify the auth method was added
141+
const authMethodsAfterAdd = await pkpPermissionsManager.getPermittedAuthMethods();
142+
assert.toBe(authMethodsAfterAdd.length, initialAuthMethodsCount + 1);
143+
144+
// Find our added auth method
145+
const addedAuthMethod = authMethodsAfterAdd.find(
146+
(am) =>
147+
am.id === testAuthMethodParams.authMethodId &&
148+
Number(am.authMethodType) === testAuthMethodParams.authMethodType
149+
);
150+
assert.toBeDefined(addedAuthMethod);
151+
console.log('✅ Test auth method successfully added');
152+
153+
// Test 11: Test removePermittedAuthMethodScope functionality
154+
const testScopeParams = {
155+
authMethodType: testAuthMethodParams.authMethodType,
156+
authMethodId: testAuthMethodParams.authMethodId,
157+
scopeId: 1, // SignAnything scope
158+
};
159+
160+
console.log('🧪 Removing scope from test auth method...');
161+
// Remove a scope from the auth method
162+
const removeScopeTx = await pkpPermissionsManager.removePermittedAuthMethodScope(testScopeParams);
163+
assert.toBeDefined(removeScopeTx.hash);
164+
assert.toBeDefined(removeScopeTx.receipt);
165+
assert.toBe(removeScopeTx.receipt.status, 'success');
166+
167+
// Verify the scope was removed by checking auth method scopes
168+
const authMethodScopes = await pkpPermissionsManager.getPermittedAuthMethodScopes({
169+
authMethodType: testAuthMethodParams.authMethodType,
170+
authMethodId: testAuthMethodParams.authMethodId,
171+
scopeId: 1,
172+
});
173+
// After removing scope 1, it should return false for that specific scope
174+
assert.toBe(authMethodScopes[0], false);
175+
console.log('✅ Scope successfully removed from test auth method');
176+
177+
// Test 12: Cleanup - Remove the test auth method entirely
178+
console.log('🧹 Cleaning up test auth method...');
179+
const removeAuthMethodTx = await pkpPermissionsManager.removePermittedAuthMethod({
180+
authMethodType: testAuthMethodParams.authMethodType,
181+
authMethodId: testAuthMethodParams.authMethodId,
182+
});
183+
assert.toBeDefined(removeAuthMethodTx.hash);
184+
assert.toBeDefined(removeAuthMethodTx.receipt);
185+
assert.toBe(removeAuthMethodTx.receipt.status, 'success');
186+
187+
// Verify the auth method was removed
188+
const finalAuthMethods = await pkpPermissionsManager.getPermittedAuthMethods();
189+
assert.toBe(finalAuthMethods.length, initialAuthMethodsCount);
190+
191+
// Ensure our test auth method is no longer in the list
192+
const removedAuthMethod = finalAuthMethods.find(
193+
(am) =>
194+
am.id === testAuthMethodParams.authMethodId &&
195+
Number(am.authMethodType) === testAuthMethodParams.authMethodType
196+
);
197+
assert.toBe(removedAuthMethod, undefined);
198+
console.log('✅ Test auth method successfully cleaned up');
112199
};
113200
};

packages/networks/src/networks/vNaga/LitChainClient/apis/highLevelApis/PKPPermissionsManager/PKPPermissionsManager.ts

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { PkpIdentifierRaw } from '../../rawContractApis/permissions/utils/resolv
3131
// Import all handler functions
3232
import { addPermittedActionByIdentifier } from './handlers/addPermittedActionByIdentifier';
3333
import { addPermittedAddressByIdentifier } from './handlers/addPermittedAddressByIdentifier';
34+
import { addPermittedAuthMethodByIdentifier } from './handlers/addPermittedAuthMethodByIdentifier';
3435
import { addPermittedAuthMethodScopeByIdentifier } from './handlers/addPermittedAuthMethodScopeByIdentifier';
3536
import {
3637
getPermissionsContext,
@@ -50,6 +51,7 @@ import { isPermittedAddressByIdentifier } from './handlers/isPermittedAddressByI
5051
import { removePermittedActionByIdentifier } from './handlers/removePermittedActionByIdentifier';
5152
import { removePermittedAddressByIdentifier } from './handlers/removePermittedAddressByIdentifier';
5253
import { removePermittedAuthMethodByIdentifier } from './handlers/removePermittedAuthMethodByIdentifier';
54+
import { removePermittedAuthMethodScopeByIdentifier } from './handlers/removePermittedAuthMethodScopeByIdentifier';
5355

5456
import type { PKPStorageProvider } from '../../../../../../storage/types';
5557
import { logger } from '../../../../../shared/logger';
@@ -137,6 +139,31 @@ export class PKPPermissionsManager {
137139
);
138140
}
139141

142+
/**
143+
* Adds a permitted authentication method to the PKP
144+
*
145+
* @param params - Parameters containing authMethodType, authMethodId, userPubkey, and scopes
146+
* @returns Promise resolving to transaction details
147+
*/
148+
async addPermittedAuthMethod(params: {
149+
authMethodType: string | number | bigint;
150+
authMethodId: string;
151+
userPubkey: string;
152+
scopes: ScopeString[];
153+
}): Promise<LitTxVoid> {
154+
return addPermittedAuthMethodByIdentifier(
155+
{
156+
authMethodType: params.authMethodType,
157+
authMethodId: params.authMethodId,
158+
userPubkey: params.userPubkey,
159+
scopes: params.scopes,
160+
...this.getIdentifierParams(),
161+
},
162+
this.networkContext,
163+
this.accountOrWalletClient
164+
);
165+
}
166+
140167
/**
141168
* Adds a permitted authentication method scope to the PKP
142169
*
@@ -217,6 +244,29 @@ export class PKPPermissionsManager {
217244
);
218245
}
219246

247+
/**
248+
* Removes a specific scope from a permitted authentication method for the PKP
249+
*
250+
* @param params - Parameters containing authMethodType, authMethodId, and scopeId
251+
* @returns Promise resolving to transaction details
252+
*/
253+
async removePermittedAuthMethodScope(params: {
254+
authMethodType: string | number | bigint;
255+
authMethodId: string;
256+
scopeId: string | number | bigint;
257+
}): Promise<LitTxVoid> {
258+
return removePermittedAuthMethodScopeByIdentifier(
259+
{
260+
authMethodType: params.authMethodType,
261+
authMethodId: params.authMethodId,
262+
scopeId: params.scopeId,
263+
...this.getIdentifierParams(),
264+
},
265+
this.networkContext,
266+
this.accountOrWalletClient
267+
);
268+
}
269+
220270
/**
221271
* Checks if a LitAction is permitted for the PKP
222272
*
@@ -387,18 +437,31 @@ export class PKPPermissionsManager {
387437
| { type: 'addAction'; ipfsId: string; scopes: ScopeString[] }
388438
| { type: 'addAddress'; address: string; scopes: ScopeString[] }
389439
| {
390-
type: 'addAuthMethodScope';
391-
authMethodType: string | number | bigint;
392-
authMethodId: string;
393-
scopeId: string | number | bigint;
394-
}
440+
type: 'addAuthMethod';
441+
authMethodType: string | number | bigint;
442+
authMethodId: string;
443+
userPubkey: string;
444+
scopes: ScopeString[];
445+
}
446+
| {
447+
type: 'addAuthMethodScope';
448+
authMethodType: string | number | bigint;
449+
authMethodId: string;
450+
scopeId: string | number | bigint;
451+
}
395452
| { type: 'removeAction'; ipfsId: string }
396453
| { type: 'removeAddress'; address: string }
397454
| {
398-
type: 'removeAuthMethod';
399-
authMethodType: string | number | bigint;
400-
authMethodId: string;
401-
}
455+
type: 'removeAuthMethod';
456+
authMethodType: string | number | bigint;
457+
authMethodId: string;
458+
}
459+
| {
460+
type: 'removeAuthMethodScope';
461+
authMethodType: string | number | bigint;
462+
authMethodId: string;
463+
scopeId: string | number | bigint;
464+
}
402465
>
403466
): Promise<void> {
404467
// Process operations sequentially to avoid transaction conflicts
@@ -416,6 +479,14 @@ export class PKPPermissionsManager {
416479
scopes: op.scopes,
417480
});
418481
break;
482+
case 'addAuthMethod':
483+
await this.addPermittedAuthMethod({
484+
authMethodType: op.authMethodType,
485+
authMethodId: op.authMethodId,
486+
userPubkey: op.userPubkey,
487+
scopes: op.scopes,
488+
});
489+
break;
419490
case 'addAuthMethodScope':
420491
await this.addPermittedAuthMethodScope({
421492
authMethodType: op.authMethodType,
@@ -439,6 +510,13 @@ export class PKPPermissionsManager {
439510
authMethodId: op.authMethodId,
440511
});
441512
break;
513+
case 'removeAuthMethodScope':
514+
await this.removePermittedAuthMethodScope({
515+
authMethodType: op.authMethodType,
516+
authMethodId: op.authMethodId,
517+
scopeId: op.scopeId,
518+
});
519+
break;
442520
}
443521
}
444522
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { z } from 'zod';
2+
import { DefaultNetworkConfig } from '../../../../../interfaces/NetworkContext';
3+
import { ScopeStringSchema } from '../../../../schemas/shared/ScopeSchema';
4+
import {
5+
PkpIdentifierRaw,
6+
resolvePkpTokenId,
7+
} from '../../../rawContractApis/permissions/utils/resolvePkpTokenId';
8+
import { addPermittedAuthMethod } from '../../../rawContractApis/permissions/write/addPermittedAuthMethod';
9+
import { LitTxVoid } from '../../../types';
10+
import { ExpectedAccountOrWalletClient } from '../../../../contract-manager/createContractsManager';
11+
12+
// Schema for the request
13+
const addPermittedAuthMethodByIdentifierSchema = z.intersection(
14+
z.object({
15+
authMethodType: z.string().or(z.number()).or(z.bigint()),
16+
authMethodId: z.string(),
17+
userPubkey: z.string(),
18+
scopes: z.array(ScopeStringSchema),
19+
}),
20+
z.union([
21+
z.object({ tokenId: z.string().or(z.number()).or(z.bigint()) }),
22+
z.object({ pubkey: z.string() }),
23+
z.object({ address: z.string() }),
24+
])
25+
);
26+
27+
type AddPermittedAuthMethodByIdentifierRequest = z.infer<
28+
typeof addPermittedAuthMethodByIdentifierSchema
29+
>;
30+
31+
/**
32+
* Adds a permitted authentication method to a PKP token using various identifier types
33+
* @param request - Object containing either tokenId/address/pubkey, authMethodType, authMethodId, userPubkey, and scopes
34+
* @param networkCtx - Network context for contract interactions
35+
* @param accountOrWalletClient - Account or wallet client for signing
36+
* @returns Promise resolving to transaction details
37+
*/
38+
export async function addPermittedAuthMethodByIdentifier(
39+
request: AddPermittedAuthMethodByIdentifierRequest,
40+
networkCtx: DefaultNetworkConfig,
41+
accountOrWalletClient: ExpectedAccountOrWalletClient
42+
): Promise<LitTxVoid> {
43+
const { authMethodType, authMethodId, userPubkey, scopes, ...identifier } = request;
44+
const pkpTokenId = await resolvePkpTokenId(
45+
identifier as PkpIdentifierRaw,
46+
networkCtx
47+
);
48+
49+
return addPermittedAuthMethod(
50+
{
51+
tokenId: pkpTokenId.toString(),
52+
authMethodType: authMethodType.toString(),
53+
id: authMethodId,
54+
userPubkey,
55+
scopes,
56+
},
57+
networkCtx,
58+
accountOrWalletClient
59+
);
60+
}
61+
62+
// Example usage
63+
// if (import.meta.main) {
64+
// const networkCtx = networkContext;
65+
66+
// const res = await addPermittedAuthMethodByIdentifier(
67+
// {
68+
// tokenId:
69+
// "76136736151863037541847315168980811654782785653773679312890341037699996601290",
70+
// authMethodType: 1, // AuthMethodType.EthWallet
71+
// authMethodId: "0x1234567890abcdef1234567890abcdef12345678",
72+
// userPubkey: "0x04abcdef...",
73+
// scopes: ["sign-anything"],
74+
// },
75+
// networkCtx,
76+
// accountOrWalletClient
77+
// );
78+
79+
// console.log("res", res);
80+
// }

0 commit comments

Comments
 (0)