Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/crazy-clouds-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit-protocol/networks': major
---

add missing `withdraw` ledge abi method
3 changes: 3 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"brown-houses-matter",
"chatty-poets-grow",
"chilly-rice-stand",
"crazy-clouds-press",
"cuddly-comics-take",
"cuddly-rats-say",
"curvy-trams-fly",
Expand All @@ -48,7 +49,9 @@
"puny-lines-know",
"purple-ants-shake",
"quick-parts-enjoy",
"quick-tigers-fix",
"quiet-bees-exist",
"real-plants-fail",
"rude-chicken-join",
"short-signs-check",
"silent-crabs-take",
Expand Down
5 changes: 5 additions & 0 deletions .changeset/quick-tigers-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit-protocol/networks': major
---

add payment delegation manager
5 changes: 5 additions & 0 deletions .changeset/real-plants-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit-protocol/networks': major
---

add payment delegation apis
8 changes: 4 additions & 4 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "@lit-protocol/js-sdk",
"dependencies": {
"@dotenvx/dotenvx": "^1.6.4",
"@lit-protocol/contracts": "^0.1.34",
"@lit-protocol/contracts": "^0.2.0",
"@lit-protocol/nacl": "7.1.1",
"@lit-protocol/uint8arrays": "7.1.1",
"@metamask/eth-sig-util": "5.0.2",
Expand Down Expand Up @@ -220,9 +220,9 @@
},
"packages/networks": {
"name": "@lit-protocol/networks",
"version": "8.0.0-alpha.22",
"version": "8.0.0-alpha.23",
"dependencies": {
"@lit-protocol/contracts": "^0.1.34",
"@lit-protocol/contracts": "^0.2.0",
"@lit-protocol/nacl": "7.1.1",
"@noble/curves": "^1.8.1",
"@wagmi/core": "^2.17.1",
Expand Down Expand Up @@ -1078,7 +1078,7 @@

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

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

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

Expand Down
3 changes: 3 additions & 0 deletions e2e/src/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
createViewPKPsByAddressTest,
createViewPKPsByAuthDataTest,
createPaymentManagerFlowTest,
createPaymentDelegationFlowTest,
} from './helper/tests';
import { init } from './init';

Expand Down Expand Up @@ -62,6 +63,8 @@ describe('all', () => {
)());
it('paymentManagerFlow', () =>
createPaymentManagerFlowTest(ctx, () => ctx.aliceEoaAuthContext)());
it('paymentDelegationFlow', () =>
createPaymentDelegationFlowTest(ctx, () => ctx.aliceEoaAuthContext)());
});

describe('integrations', () => {
Expand Down
1 change: 1 addition & 0 deletions e2e/src/helper/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { createPkpEncryptDecryptTest } from './pkp-encrypt-decrypt';
export { createEncryptDecryptFlowTest } from './encrypt-decrypt-flow';
export { createPkpPermissionsManagerFlowTest } from './pkp-permissions-manager-flow';
export { createPaymentManagerFlowTest } from './payment-manager-flow';
export { createPaymentDelegationFlowTest } from './payment-delegation-flow';
export { createEoaNativeAuthFlowTest } from './eoa-native-auth-flow';

// Viem integration tests
Expand Down
142 changes: 142 additions & 0 deletions e2e/src/helper/tests/payment-delegation-flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { init } from '../../init';
import { assert } from '../assertions';

export const createPaymentDelegationFlowTest = (
ctx: Awaited<ReturnType<typeof init>>,
_getAuthContext: () => any
) => {
return async () => {
// Get Payment Manager for Alice (the payer/delegator)
const alicePaymentManager = await ctx.litClient.getPaymentManager({
account: ctx.aliceViemAccount,
});

// Get Payment Manager for Bob (the user/delegatee)
const bobPaymentManager = await ctx.litClient.getPaymentManager({
account: ctx.bobViemAccount,
});

assert.toBeDefined(alicePaymentManager);
assert.toBeDefined(bobPaymentManager);

const aliceAddress = ctx.aliceViemAccount.address;
const bobAddress = ctx.bobViemAccount.address;

// Test 1: Get initial payers for Bob
const initialPayers = await bobPaymentManager.getPayers({
userAddress: bobAddress,
});
assert.toBe(Array.isArray(initialPayers), true);
const initialPayersCount = initialPayers.length;
console.log('initialPayers', initialPayers);

// Test 2: Get initial users for Alice
const initialUsers = await alicePaymentManager.getUsers({
payerAddress: aliceAddress,
});
assert.toBe(Array.isArray(initialUsers), true);
const initialUsersCount = initialUsers.length;
console.log('initialUsers', initialUsers);

// Test 3: Alice delegates payment to Bob
const delegateTx = await alicePaymentManager.delegatePayments({
userAddress: bobAddress,
});
assert.toBeDefined(delegateTx.hash);
assert.toBeDefined(delegateTx.receipt);
assert.toBe(delegateTx.receipt.status, 'success');

// Test 4: Verify Bob now has Alice as a payer
const payersAfterDelegate = await bobPaymentManager.getPayers({
userAddress: bobAddress,
});
assert.toBe(payersAfterDelegate.length, initialPayersCount + 1);
assert.toBe(payersAfterDelegate.includes(aliceAddress), true);
console.log('payersAfterDelegate', payersAfterDelegate);

// Test 5: Verify Alice now has Bob as a user
const usersAfterDelegate = await alicePaymentManager.getUsers({
payerAddress: aliceAddress,
});
assert.toBe(usersAfterDelegate.length, initialUsersCount + 1);
assert.toBe(usersAfterDelegate.includes(bobAddress), true);
console.log('usersAfterDelegate', usersAfterDelegate);

// Test 6: Set a restriction for Alice
const setRestrictionTx = await alicePaymentManager.setRestriction({
totalMaxPrice: '1000000000000000000', // 1 ETH
requestsPerPeriod: '100',
periodSeconds: '3600', // 1 hour
});
assert.toBeDefined(setRestrictionTx.hash);
assert.toBe(setRestrictionTx.receipt.status, 'success');
console.log('setRestrictionTx', setRestrictionTx);

// Test 7: Get and verify the restriction
const restriction = await alicePaymentManager.getRestriction({
payerAddress: aliceAddress,
});
assert.toBeDefined(restriction);
assert.toBe(restriction.totalMaxPrice, '1000000000000000000');
assert.toBe(restriction.requestsPerPeriod, '100');
assert.toBe(restriction.periodSeconds, '3600');
console.log('restriction', restriction);
// Test 8: Test batch operations - create test addresses
const testAddresses = [
'0x1234567890123456789012345678901234567890',
'0x2345678901234567890123456789012345678901',
];

// Delegate to multiple users
const batchDelegateTx = await alicePaymentManager.delegatePaymentsBatch({
userAddresses: testAddresses,
});
assert.toBeDefined(batchDelegateTx.hash);
assert.toBe(batchDelegateTx.receipt.status, 'success');
console.log('batchDelegateTx', batchDelegateTx);
// Test 9: Verify batch delegation
const usersAfterBatch = await alicePaymentManager.getUsers({
payerAddress: aliceAddress,
});
assert.toBe(usersAfterBatch.includes(testAddresses[0]), true);
assert.toBe(usersAfterBatch.includes(testAddresses[1]), true);
console.log('usersAfterBatch', usersAfterBatch);
// Test 10: Get payers and restrictions for multiple users
const payersAndRestrictions = await alicePaymentManager.getPayersAndRestrictions({
userAddresses: [bobAddress, testAddresses[0]],
});
assert.toBeDefined(payersAndRestrictions);
assert.toBe(Array.isArray(payersAndRestrictions.payers), true);
assert.toBe(payersAndRestrictions.payers.length, 2);
assert.toBe(Array.isArray(payersAndRestrictions.restrictions), true);
assert.toBe(payersAndRestrictions.restrictions.length, 2);
console.log('payersAndRestrictions', payersAndRestrictions);
// Test 11: Undelegate from batch users
const batchUndelegateTx = await alicePaymentManager.undelegatePaymentsBatch({
userAddresses: testAddresses,
});
assert.toBeDefined(batchUndelegateTx.hash);
assert.toBe(batchUndelegateTx.receipt.status, 'success');
console.log('batchUndelegateTx', batchUndelegateTx);
// Test 12: Alice undelegates payment from Bob
const undelegateTx = await alicePaymentManager.undelegatePayments({
userAddress: bobAddress,
});
assert.toBeDefined(undelegateTx.hash);
assert.toBe(undelegateTx.receipt.status, 'success');
console.log('undelegateTx', undelegateTx);
// Test 13: Verify Bob no longer has Alice as a payer
const finalPayers = await bobPaymentManager.getPayers({
userAddress: bobAddress,
});
assert.toBe(finalPayers.length, initialPayersCount);
assert.toBe(finalPayers.includes(aliceAddress), false);
console.log('finalPayers', finalPayers);
// Test 14: Verify Alice no longer has Bob as a user
const finalUsers = await alicePaymentManager.getUsers({
payerAddress: aliceAddress,
});
assert.toBe(finalUsers.includes(bobAddress), false);
console.log('finalUsers', finalUsers);
};
};
67 changes: 64 additions & 3 deletions e2e/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export const init = async (
aliceViemAccount: any;
aliceViemAccountAuthData: any;
aliceViemAccountPkp: any;
bobViemAccount: any;
bobViemAccountAuthData: any;
bobViemAccountPkp: any;
aliceEoaAuthContext: any;
}> => {
/**
Expand All @@ -47,6 +50,11 @@ export const init = async (
const aliceViemAccountAuthData = await ViemAccountAuthenticator.authenticate(
aliceViemAccount
);

const bobViemAccount = privateKeyToAccount(generatePrivateKey());
const bobViemAccountAuthData = await ViemAccountAuthenticator.authenticate(
bobViemAccount
);

/**
* ====================================
Expand Down Expand Up @@ -84,27 +92,43 @@ export const init = async (
ifLessThan: '0.01',
thenFundWith: '0.01',
});
await fundAccount(bobViemAccount, liveMasterAccount, _networkModule, {
ifLessThan: '0.01',
thenFundWith: '0.01',
});
} else if (_network === 'naga-test') {
const { nagaTest } = await import('@lit-protocol/networks');
_networkModule = nagaTest;
await fundAccount(aliceViemAccount, liveMasterAccount, _networkModule, {
ifLessThan: '0.01',
thenFundWith: '0.01',
});
await fundAccount(bobViemAccount, liveMasterAccount, _networkModule, {
ifLessThan: '0.01',
thenFundWith: '0.01',
});
} else if (_network === 'naga-local') {
const { nagaLocal } = await import('@lit-protocol/networks');
_networkModule = nagaLocal;
await fundAccount(aliceViemAccount, localMasterAccount, _networkModule, {
ifLessThan: '1',
thenFundWith: '1',
});
await fundAccount(bobViemAccount, localMasterAccount, _networkModule, {
ifLessThan: '1',
thenFundWith: '1',
});
} else if (_network === 'naga-staging') {
const { nagaStaging } = await import('@lit-protocol/networks');
_networkModule = nagaStaging;
await fundAccount(aliceViemAccount, liveMasterAccount, _networkModule, {
ifLessThan: '0.0001',
thenFundWith: '0.0001',
});
await fundAccount(bobViemAccount, liveMasterAccount, _networkModule, {
ifLessThan: '0.0001',
thenFundWith: '0.0001',
});
} else {
throw new Error(`❌ Invalid network: ${_network}`);
}
Expand Down Expand Up @@ -133,7 +157,7 @@ export const init = async (

/**
* ====================================
* Select a PKP
* Select PKPs for Alice and Bob
* ====================================
*/
const { pkps: aliceViemAccountPkps } = await litClient.viewPKPsByAuthData({
Expand All @@ -149,9 +173,22 @@ export const init = async (
});
const aliceViemAccountPkp = aliceViemAccountPkps[0];

const { pkps: bobViemAccountPkps } = await litClient.viewPKPsByAuthData({
authData: bobViemAccountAuthData,
pagination: {
limit: 5,
},
storageProvider: storagePlugins.localStorageNode({
appName: 'my-app',
networkName: 'naga-dev',
storagePath: './pkp-tokens-bob',
}),
});
const bobViemAccountPkp = bobViemAccountPkps[0];

/**
* ====================================
* (Local only) Mint a PKP
* (Local only) Mint PKPs for Alice and Bob
* ====================================
*/
if (!aliceViemAccountPkp) {
Expand All @@ -162,9 +199,17 @@ export const init = async (
});
}

if (!bobViemAccountPkp) {
await litClient.mintWithAuth({
authData: bobViemAccountAuthData,
account: bobViemAccount,
scopes: ['sign-anything'],
});
}

/**
* ====================================
* Select a PKP
* Select final PKPs for Alice and Bob
* ====================================
*/
const { pkps: aliceViemAccountPkps2 } = await litClient.viewPKPsByAuthData({
Expand All @@ -180,6 +225,19 @@ export const init = async (
});
const aliceViemAccountPkp2 = aliceViemAccountPkps2[0];

const { pkps: bobViemAccountPkps2 } = await litClient.viewPKPsByAuthData({
authData: bobViemAccountAuthData,
pagination: {
limit: 5,
},
storageProvider: storagePlugins.localStorageNode({
appName: 'my-app',
networkName: 'naga-dev',
storagePath: './pkp-tokens-bob',
}),
});
const bobViemAccountPkp2 = bobViemAccountPkps2[0];

/**
* ====================================
* Create the auth context
Expand Down Expand Up @@ -216,6 +274,9 @@ export const init = async (
aliceViemAccount,
aliceViemAccountAuthData,
aliceViemAccountPkp: aliceViemAccountPkp2,
bobViemAccount,
bobViemAccountAuthData,
bobViemAccountPkp: bobViemAccountPkp2,
aliceEoaAuthContext,
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"private": true,
"dependencies": {
"@dotenvx/dotenvx": "^1.6.4",
"@lit-protocol/contracts": "^0.1.34",
"@lit-protocol/contracts": "^0.2.0",
"@lit-protocol/nacl": "7.1.1",
"@lit-protocol/uint8arrays": "7.1.1",
"@metamask/eth-sig-util": "5.0.2",
Expand Down
Loading
Loading