Skip to content

Commit 113eaf7

Browse files
authored
Merge pull request #836 from LIT-Protocol/feature/node-4571-payment-manager-doesnt-include-paymentdelegationfacet
Feature/node 4571 payment manager doesnt include paymentdelegationfacet
2 parents d6e7137 + bf201e8 commit 113eaf7

File tree

23 files changed

+885
-10
lines changed

23 files changed

+885
-10
lines changed

.changeset/crazy-clouds-press.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lit-protocol/networks': major
3+
---
4+
5+
add missing `withdraw` ledge abi method

.changeset/pre.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"brown-houses-matter",
2727
"chatty-poets-grow",
2828
"chilly-rice-stand",
29+
"crazy-clouds-press",
2930
"cuddly-comics-take",
3031
"cuddly-rats-say",
3132
"curvy-trams-fly",
@@ -48,7 +49,9 @@
4849
"puny-lines-know",
4950
"purple-ants-shake",
5051
"quick-parts-enjoy",
52+
"quick-tigers-fix",
5153
"quiet-bees-exist",
54+
"real-plants-fail",
5255
"rude-chicken-join",
5356
"short-signs-check",
5457
"silent-crabs-take",

.changeset/quick-tigers-fix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lit-protocol/networks': major
3+
---
4+
5+
add payment delegation manager

.changeset/real-plants-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lit-protocol/networks': major
3+
---
4+
5+
add payment delegation apis

bun.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "@lit-protocol/js-sdk",
66
"dependencies": {
77
"@dotenvx/dotenvx": "^1.6.4",
8-
"@lit-protocol/contracts": "^0.1.34",
8+
"@lit-protocol/contracts": "^0.2.0",
99
"@lit-protocol/nacl": "7.1.1",
1010
"@lit-protocol/uint8arrays": "7.1.1",
1111
"@metamask/eth-sig-util": "5.0.2",
@@ -220,9 +220,9 @@
220220
},
221221
"packages/networks": {
222222
"name": "@lit-protocol/networks",
223-
"version": "8.0.0-alpha.22",
223+
"version": "8.0.0-alpha.23",
224224
"dependencies": {
225-
"@lit-protocol/contracts": "^0.1.34",
225+
"@lit-protocol/contracts": "^0.2.0",
226226
"@lit-protocol/nacl": "7.1.1",
227227
"@noble/curves": "^1.8.1",
228228
"@wagmi/core": "^2.17.1",
@@ -1078,7 +1078,7 @@
10781078

10791079
"@lit-protocol/constants": ["@lit-protocol/constants@workspace:packages/constants"],
10801080

1081-
"@lit-protocol/contracts": ["@lit-protocol/contracts@0.1.34", "", { "peerDependencies": { "typescript": "^5.0.0" } }, "sha512-Cg+3SrHjnGAxNLB85n+QlmkcgCX99I0VQQ1KVrk3nix/IXoMzG4Mm/najb9EDPegi76WKWKrt2nhqhAkmGj9LQ=="],
1081+
"@lit-protocol/contracts": ["@lit-protocol/contracts@0.2.0", "", { "peerDependencies": { "typescript": "^5.0.0" } }, "sha512-AoSTZbrGwdnPJjC68rN6vnapmzMCKdvqGyi7OM+dCBRI/pgm7e3b3NXdtAOz5ynzAtnL3gcF9RxqoQthcbBIKw=="],
10821082

10831083
"@lit-protocol/crypto": ["@lit-protocol/crypto@workspace:packages/crypto"],
10841084

e2e/src/e2e.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
createViewPKPsByAddressTest,
1616
createViewPKPsByAuthDataTest,
1717
createPaymentManagerFlowTest,
18+
createPaymentDelegationFlowTest,
1819
} from './helper/tests';
1920
import { init } from './init';
2021

@@ -62,6 +63,8 @@ describe('all', () => {
6263
)());
6364
it('paymentManagerFlow', () =>
6465
createPaymentManagerFlowTest(ctx, () => ctx.aliceEoaAuthContext)());
66+
it('paymentDelegationFlow', () =>
67+
createPaymentDelegationFlowTest(ctx, () => ctx.aliceEoaAuthContext)());
6568
});
6669

6770
describe('integrations', () => {

e2e/src/helper/tests/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export { createPkpEncryptDecryptTest } from './pkp-encrypt-decrypt';
77
export { createEncryptDecryptFlowTest } from './encrypt-decrypt-flow';
88
export { createPkpPermissionsManagerFlowTest } from './pkp-permissions-manager-flow';
99
export { createPaymentManagerFlowTest } from './payment-manager-flow';
10+
export { createPaymentDelegationFlowTest } from './payment-delegation-flow';
1011
export { createEoaNativeAuthFlowTest } from './eoa-native-auth-flow';
1112

1213
// Viem integration tests
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { init } from '../../init';
2+
import { assert } from '../assertions';
3+
4+
export const createPaymentDelegationFlowTest = (
5+
ctx: Awaited<ReturnType<typeof init>>,
6+
_getAuthContext: () => any
7+
) => {
8+
return async () => {
9+
// Get Payment Manager for Alice (the payer/delegator)
10+
const alicePaymentManager = await ctx.litClient.getPaymentManager({
11+
account: ctx.aliceViemAccount,
12+
});
13+
14+
// Get Payment Manager for Bob (the user/delegatee)
15+
const bobPaymentManager = await ctx.litClient.getPaymentManager({
16+
account: ctx.bobViemAccount,
17+
});
18+
19+
assert.toBeDefined(alicePaymentManager);
20+
assert.toBeDefined(bobPaymentManager);
21+
22+
const aliceAddress = ctx.aliceViemAccount.address;
23+
const bobAddress = ctx.bobViemAccount.address;
24+
25+
// Test 1: Get initial payers for Bob
26+
const initialPayers = await bobPaymentManager.getPayers({
27+
userAddress: bobAddress,
28+
});
29+
assert.toBe(Array.isArray(initialPayers), true);
30+
const initialPayersCount = initialPayers.length;
31+
console.log('initialPayers', initialPayers);
32+
33+
// Test 2: Get initial users for Alice
34+
const initialUsers = await alicePaymentManager.getUsers({
35+
payerAddress: aliceAddress,
36+
});
37+
assert.toBe(Array.isArray(initialUsers), true);
38+
const initialUsersCount = initialUsers.length;
39+
console.log('initialUsers', initialUsers);
40+
41+
// Test 3: Alice delegates payment to Bob
42+
const delegateTx = await alicePaymentManager.delegatePayments({
43+
userAddress: bobAddress,
44+
});
45+
assert.toBeDefined(delegateTx.hash);
46+
assert.toBeDefined(delegateTx.receipt);
47+
assert.toBe(delegateTx.receipt.status, 'success');
48+
49+
// Test 4: Verify Bob now has Alice as a payer
50+
const payersAfterDelegate = await bobPaymentManager.getPayers({
51+
userAddress: bobAddress,
52+
});
53+
assert.toBe(payersAfterDelegate.length, initialPayersCount + 1);
54+
assert.toBe(payersAfterDelegate.includes(aliceAddress), true);
55+
console.log('payersAfterDelegate', payersAfterDelegate);
56+
57+
// Test 5: Verify Alice now has Bob as a user
58+
const usersAfterDelegate = await alicePaymentManager.getUsers({
59+
payerAddress: aliceAddress,
60+
});
61+
assert.toBe(usersAfterDelegate.length, initialUsersCount + 1);
62+
assert.toBe(usersAfterDelegate.includes(bobAddress), true);
63+
console.log('usersAfterDelegate', usersAfterDelegate);
64+
65+
// Test 6: Set a restriction for Alice
66+
const setRestrictionTx = await alicePaymentManager.setRestriction({
67+
totalMaxPrice: '1000000000000000000', // 1 ETH
68+
requestsPerPeriod: '100',
69+
periodSeconds: '3600', // 1 hour
70+
});
71+
assert.toBeDefined(setRestrictionTx.hash);
72+
assert.toBe(setRestrictionTx.receipt.status, 'success');
73+
console.log('setRestrictionTx', setRestrictionTx);
74+
75+
// Test 7: Get and verify the restriction
76+
const restriction = await alicePaymentManager.getRestriction({
77+
payerAddress: aliceAddress,
78+
});
79+
assert.toBeDefined(restriction);
80+
assert.toBe(restriction.totalMaxPrice, '1000000000000000000');
81+
assert.toBe(restriction.requestsPerPeriod, '100');
82+
assert.toBe(restriction.periodSeconds, '3600');
83+
console.log('restriction', restriction);
84+
// Test 8: Test batch operations - create test addresses
85+
const testAddresses = [
86+
'0x1234567890123456789012345678901234567890',
87+
'0x2345678901234567890123456789012345678901',
88+
];
89+
90+
// Delegate to multiple users
91+
const batchDelegateTx = await alicePaymentManager.delegatePaymentsBatch({
92+
userAddresses: testAddresses,
93+
});
94+
assert.toBeDefined(batchDelegateTx.hash);
95+
assert.toBe(batchDelegateTx.receipt.status, 'success');
96+
console.log('batchDelegateTx', batchDelegateTx);
97+
// Test 9: Verify batch delegation
98+
const usersAfterBatch = await alicePaymentManager.getUsers({
99+
payerAddress: aliceAddress,
100+
});
101+
assert.toBe(usersAfterBatch.includes(testAddresses[0]), true);
102+
assert.toBe(usersAfterBatch.includes(testAddresses[1]), true);
103+
console.log('usersAfterBatch', usersAfterBatch);
104+
// Test 10: Get payers and restrictions for multiple users
105+
const payersAndRestrictions = await alicePaymentManager.getPayersAndRestrictions({
106+
userAddresses: [bobAddress, testAddresses[0]],
107+
});
108+
assert.toBeDefined(payersAndRestrictions);
109+
assert.toBe(Array.isArray(payersAndRestrictions.payers), true);
110+
assert.toBe(payersAndRestrictions.payers.length, 2);
111+
assert.toBe(Array.isArray(payersAndRestrictions.restrictions), true);
112+
assert.toBe(payersAndRestrictions.restrictions.length, 2);
113+
console.log('payersAndRestrictions', payersAndRestrictions);
114+
// Test 11: Undelegate from batch users
115+
const batchUndelegateTx = await alicePaymentManager.undelegatePaymentsBatch({
116+
userAddresses: testAddresses,
117+
});
118+
assert.toBeDefined(batchUndelegateTx.hash);
119+
assert.toBe(batchUndelegateTx.receipt.status, 'success');
120+
console.log('batchUndelegateTx', batchUndelegateTx);
121+
// Test 12: Alice undelegates payment from Bob
122+
const undelegateTx = await alicePaymentManager.undelegatePayments({
123+
userAddress: bobAddress,
124+
});
125+
assert.toBeDefined(undelegateTx.hash);
126+
assert.toBe(undelegateTx.receipt.status, 'success');
127+
console.log('undelegateTx', undelegateTx);
128+
// Test 13: Verify Bob no longer has Alice as a payer
129+
const finalPayers = await bobPaymentManager.getPayers({
130+
userAddress: bobAddress,
131+
});
132+
assert.toBe(finalPayers.length, initialPayersCount);
133+
assert.toBe(finalPayers.includes(aliceAddress), false);
134+
console.log('finalPayers', finalPayers);
135+
// Test 14: Verify Alice no longer has Bob as a user
136+
const finalUsers = await alicePaymentManager.getUsers({
137+
payerAddress: aliceAddress,
138+
});
139+
assert.toBe(finalUsers.includes(bobAddress), false);
140+
console.log('finalUsers', finalUsers);
141+
};
142+
};

e2e/src/init.ts

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export const init = async (
3030
aliceViemAccount: any;
3131
aliceViemAccountAuthData: any;
3232
aliceViemAccountPkp: any;
33+
bobViemAccount: any;
34+
bobViemAccountAuthData: any;
35+
bobViemAccountPkp: any;
3336
aliceEoaAuthContext: any;
3437
}> => {
3538
/**
@@ -47,6 +50,11 @@ export const init = async (
4750
const aliceViemAccountAuthData = await ViemAccountAuthenticator.authenticate(
4851
aliceViemAccount
4952
);
53+
54+
const bobViemAccount = privateKeyToAccount(generatePrivateKey());
55+
const bobViemAccountAuthData = await ViemAccountAuthenticator.authenticate(
56+
bobViemAccount
57+
);
5058

5159
/**
5260
* ====================================
@@ -84,27 +92,43 @@ export const init = async (
8492
ifLessThan: '0.01',
8593
thenFundWith: '0.01',
8694
});
95+
await fundAccount(bobViemAccount, liveMasterAccount, _networkModule, {
96+
ifLessThan: '0.01',
97+
thenFundWith: '0.01',
98+
});
8799
} else if (_network === 'naga-test') {
88100
const { nagaTest } = await import('@lit-protocol/networks');
89101
_networkModule = nagaTest;
90102
await fundAccount(aliceViemAccount, liveMasterAccount, _networkModule, {
91103
ifLessThan: '0.01',
92104
thenFundWith: '0.01',
93105
});
106+
await fundAccount(bobViemAccount, liveMasterAccount, _networkModule, {
107+
ifLessThan: '0.01',
108+
thenFundWith: '0.01',
109+
});
94110
} else if (_network === 'naga-local') {
95111
const { nagaLocal } = await import('@lit-protocol/networks');
96112
_networkModule = nagaLocal;
97113
await fundAccount(aliceViemAccount, localMasterAccount, _networkModule, {
98114
ifLessThan: '1',
99115
thenFundWith: '1',
100116
});
117+
await fundAccount(bobViemAccount, localMasterAccount, _networkModule, {
118+
ifLessThan: '1',
119+
thenFundWith: '1',
120+
});
101121
} else if (_network === 'naga-staging') {
102122
const { nagaStaging } = await import('@lit-protocol/networks');
103123
_networkModule = nagaStaging;
104124
await fundAccount(aliceViemAccount, liveMasterAccount, _networkModule, {
105125
ifLessThan: '0.0001',
106126
thenFundWith: '0.0001',
107127
});
128+
await fundAccount(bobViemAccount, liveMasterAccount, _networkModule, {
129+
ifLessThan: '0.0001',
130+
thenFundWith: '0.0001',
131+
});
108132
} else {
109133
throw new Error(`❌ Invalid network: ${_network}`);
110134
}
@@ -133,7 +157,7 @@ export const init = async (
133157

134158
/**
135159
* ====================================
136-
* Select a PKP
160+
* Select PKPs for Alice and Bob
137161
* ====================================
138162
*/
139163
const { pkps: aliceViemAccountPkps } = await litClient.viewPKPsByAuthData({
@@ -149,9 +173,22 @@ export const init = async (
149173
});
150174
const aliceViemAccountPkp = aliceViemAccountPkps[0];
151175

176+
const { pkps: bobViemAccountPkps } = await litClient.viewPKPsByAuthData({
177+
authData: bobViemAccountAuthData,
178+
pagination: {
179+
limit: 5,
180+
},
181+
storageProvider: storagePlugins.localStorageNode({
182+
appName: 'my-app',
183+
networkName: 'naga-dev',
184+
storagePath: './pkp-tokens-bob',
185+
}),
186+
});
187+
const bobViemAccountPkp = bobViemAccountPkps[0];
188+
152189
/**
153190
* ====================================
154-
* (Local only) Mint a PKP
191+
* (Local only) Mint PKPs for Alice and Bob
155192
* ====================================
156193
*/
157194
if (!aliceViemAccountPkp) {
@@ -162,9 +199,17 @@ export const init = async (
162199
});
163200
}
164201

202+
if (!bobViemAccountPkp) {
203+
await litClient.mintWithAuth({
204+
authData: bobViemAccountAuthData,
205+
account: bobViemAccount,
206+
scopes: ['sign-anything'],
207+
});
208+
}
209+
165210
/**
166211
* ====================================
167-
* Select a PKP
212+
* Select final PKPs for Alice and Bob
168213
* ====================================
169214
*/
170215
const { pkps: aliceViemAccountPkps2 } = await litClient.viewPKPsByAuthData({
@@ -180,6 +225,19 @@ export const init = async (
180225
});
181226
const aliceViemAccountPkp2 = aliceViemAccountPkps2[0];
182227

228+
const { pkps: bobViemAccountPkps2 } = await litClient.viewPKPsByAuthData({
229+
authData: bobViemAccountAuthData,
230+
pagination: {
231+
limit: 5,
232+
},
233+
storageProvider: storagePlugins.localStorageNode({
234+
appName: 'my-app',
235+
networkName: 'naga-dev',
236+
storagePath: './pkp-tokens-bob',
237+
}),
238+
});
239+
const bobViemAccountPkp2 = bobViemAccountPkps2[0];
240+
183241
/**
184242
* ====================================
185243
* Create the auth context
@@ -216,6 +274,9 @@ export const init = async (
216274
aliceViemAccount,
217275
aliceViemAccountAuthData,
218276
aliceViemAccountPkp: aliceViemAccountPkp2,
277+
bobViemAccount,
278+
bobViemAccountAuthData,
279+
bobViemAccountPkp: bobViemAccountPkp2,
219280
aliceEoaAuthContext,
220281
};
221282
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"private": true,
3535
"dependencies": {
3636
"@dotenvx/dotenvx": "^1.6.4",
37-
"@lit-protocol/contracts": "^0.1.34",
37+
"@lit-protocol/contracts": "^0.2.0",
3838
"@lit-protocol/nacl": "7.1.1",
3939
"@lit-protocol/uint8arrays": "7.1.1",
4040
"@metamask/eth-sig-util": "5.0.2",

0 commit comments

Comments
 (0)