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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ lit-cache
lit-auth-local
artillery-state.json
artillery-pkp-tokens
lit-auth-artillery
lit-auth-artillery
alice-auth-manager-data
61 changes: 39 additions & 22 deletions e2e/src/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ describe('all', () => {

// Auth contexts for testing
let alicePkpAuthContext: any;
let aliceCustomAuthContext: any;
let eveCustomAuthContext: any;

beforeAll(async () => {
try {
ctx = await init();

// Create PKP and custom auth contexts using helper functions
// alicePkpAuthContext = await createPkpAuthContext(ctx);
aliceCustomAuthContext = await createCustomAuthContext(ctx);
eveCustomAuthContext = await createCustomAuthContext(ctx);
} catch (e) {
console.error(e);
process.exit(1);
Expand Down Expand Up @@ -124,34 +124,51 @@ describe('all', () => {

describe('endpoints', () => {
it('pkpSign', () =>
createPkpSignTest(ctx, () => aliceCustomAuthContext)());
createPkpSignTest(
ctx,
() => eveCustomAuthContext,
ctx.eveViemAccountPkp.pubkey
)());
it('executeJs', () =>
createExecuteJsTest(ctx, () => aliceCustomAuthContext)());
it('viewPKPsByAddress', () =>
createViewPKPsByAddressTest(ctx, () => aliceCustomAuthContext)());
createExecuteJsTest(
ctx,
() => eveCustomAuthContext,
ctx.eveViemAccountPkp.pubkey
)());
it('viewPKPsByAddress', () => createViewPKPsByAddressTest(ctx)());
it('viewPKPsByAuthData', () =>
createViewPKPsByAuthDataTest(ctx, () => aliceCustomAuthContext)());
createViewPKPsByAuthDataTest(ctx, ctx.eveCustomAuthData)());
it('pkpEncryptDecrypt', () =>
createPkpEncryptDecryptTest(ctx, () => aliceCustomAuthContext)());
createPkpEncryptDecryptTest(
ctx,
() => eveCustomAuthContext,
ctx.eveViemAccountPkp.ethAddress
)());
it('encryptDecryptFlow', () =>
createEncryptDecryptFlowTest(ctx, () => aliceCustomAuthContext)());
it('pkpPermissionsManagerFlow', () =>
createPkpPermissionsManagerFlowTest(
createEncryptDecryptFlowTest(
ctx,
() => aliceCustomAuthContext
() => eveCustomAuthContext,
ctx.eveViemAccountPkp.pubkey
)());
});

describe('integrations', () => {
describe('pkp viem account', () => {
it('sign message', () =>
createViemSignMessageTest(ctx, () => aliceCustomAuthContext)());
it('sign transaction', () =>
createViemSignTransactionTest(ctx, () => aliceCustomAuthContext)());
it('sign typed data', () =>
createViemSignTypedDataTest(ctx, () => aliceCustomAuthContext)());
});
// Disable for now because it requires a different flow
// it('pkpPermissionsManagerFlow', () =>
// createPkpPermissionsManagerFlowTest(
// ctx,
// () => eveCustomAuthContext, ctx.eveViemAccountPkp.pubkey
// )());
});

// describe('integrations', () => {
// describe('pkp viem account', () => {
// it('sign message', () =>
// createViemSignMessageTest(ctx, () => eveCustomAuthContext, ctx.eveViemAccountPkp.pubkey)());
// it('sign transaction', () =>
// createViemSignTransactionTest(ctx, () => eveCustomAuthContext, ctx.eveViemAccountPkp.pubkey)());
// it('sign typed data', () =>
// createViemSignTypedDataTest(ctx, () => eveCustomAuthContext, ctx.eveViemAccountPkp.pubkey)());
// });
// });
});

describe('EOA Native', () => {
Expand Down
20 changes: 7 additions & 13 deletions e2e/src/helper/auth-contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,12 @@ export const createCustomAuthContext = async (
ctx: Awaited<ReturnType<typeof init>>
) => {
console.log('🔁 Creating Custom Auth Context');

try {
// Set up custom auth method type and validation IPFS CID (from custom-auth-flow example)
const uniqueDappName = 'e2e-test-dapp';
const uniqueAuthMethodType = hexToBigInt(
keccak256(toBytes(uniqueDappName))
);
const uniqueUserId = `${uniqueDappName}-alice`;
const authMethodId = keccak256(toBytes(uniqueUserId));
const validationIpfsCid = 'QmYLeVmwJPVs7Uebk85YdVPivMyrvoeKR6X37kyVRZUXW4';

const customAuthContext = await ctx.authManager.createCustomAuthContext({
pkpPublicKey: ctx.aliceViemAccountPkp.publicKey,
pkpPublicKey: ctx.eveViemAccountPkp.pubkey,
authConfig: {
resources: [
['pkp-signing', '*'],
Expand All @@ -61,17 +55,17 @@ export const createCustomAuthContext = async (
},
litClient: ctx.litClient,
customAuthParams: {
litActionIpfsId: validationIpfsCid,
litActionIpfsId: ctx.eveValidationIpfsCid,
jsParams: {
pkpPublicKey: ctx.aliceViemAccountPkp.publicKey,
username: 'alice',
pkpPublicKey: ctx.eveViemAccountPkp.pubkey,
username: 'eve',
password: 'lit',
authMethodId: authMethodId,
authMethodId: ctx.eveCustomAuthData.authMethodId,
},
},
});

console.log('✅ Custom Auth Context created');
console.log('✅ Custom Auth Context created', customAuthContext);
return customAuthContext;
} catch (e) {
console.error('❌ Error creating Custom Auth Context', e);
Expand Down
5 changes: 3 additions & 2 deletions e2e/src/helper/tests/encrypt-decrypt-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { assert } from '../assertions';

export const createEncryptDecryptFlowTest = (
ctx: Awaited<ReturnType<typeof init>>,
getAuthContext: () => any
getAuthContext: () => any,
address?: string
) => {
return async () => {
const { createAccBuilder } = await import(
Expand All @@ -17,7 +18,7 @@ export const createEncryptDecryptFlowTest = (
if (authContext === ctx.aliceEoaAuthContext) {
aliceAddress = ctx.aliceViemAccount.address;
} else {
aliceAddress = ctx.aliceViemAccountPkp.ethAddress;
aliceAddress = address || ctx.aliceViemAccountPkp.ethAddress;
}

// Set up access control conditions requiring Bob's wallet ownership
Expand Down
5 changes: 3 additions & 2 deletions e2e/src/helper/tests/execute-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { assert } from '../assertions';

export const createExecuteJsTest = (
ctx: Awaited<ReturnType<typeof init>>,
getAuthContext: () => any
getAuthContext: () => any,
pubkey?: string
) => {
return async () => {
const litActionCode = `
Expand All @@ -29,7 +30,7 @@ export const createExecuteJsTest = (
message: 'Test message from e2e executeJs',
sigName: 'e2e-test-sig',
toSign: 'Test message from e2e executeJs',
publicKey: ctx.aliceViemAccountPkp.publicKey,
publicKey: pubkey || ctx.aliceViemAccountPkp.publicKey,
},
});

Expand Down
5 changes: 3 additions & 2 deletions e2e/src/helper/tests/pkp-encrypt-decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { assert } from '../assertions';

export const createPkpEncryptDecryptTest = (
ctx: Awaited<ReturnType<typeof init>>,
getAuthContext: () => any
getAuthContext: () => any,
address?: string
) => {
return async () => {
const { createAccBuilder } = await import(
Expand All @@ -19,7 +20,7 @@ export const createPkpEncryptDecryptTest = (
addressToUse = ctx.aliceViemAccount.address;
} else {
// PKP or Custom auth contexts
addressToUse = ctx.aliceViemAccountPkp.ethAddress;
addressToUse = address || ctx.aliceViemAccountPkp.ethAddress;
}

// Set up access control conditions requiring wallet ownership
Expand Down
5 changes: 3 additions & 2 deletions e2e/src/helper/tests/pkp-sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { assert } from '../assertions';

export const createPkpSignTest = (
ctx: Awaited<ReturnType<typeof init>>,
getAuthContext: () => any
getAuthContext: () => any,
pubkey?: string
) => {
return async () => {
const res = await ctx.litClient.chain.ethereum.pkpSign({
authContext: getAuthContext(),
pubKey: ctx.aliceViemAccountPkp.publicKey,
pubKey: pubkey || ctx.aliceViemAccountPkp.publicKey,
toSign: 'Hello, world!',
});

Expand Down
3 changes: 1 addition & 2 deletions e2e/src/helper/tests/view-pkps-by-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { init } from '../../init';
import { assert } from '../assertions';

export const createViewPKPsByAddressTest = (
ctx: Awaited<ReturnType<typeof init>>,
getAuthContext: () => any
ctx: Awaited<ReturnType<typeof init>>
) => {
return async () => {
const pkps = await ctx.litClient.viewPKPsByAddress({
Expand Down
12 changes: 6 additions & 6 deletions e2e/src/helper/tests/view-pkps-by-auth-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { assert } from '../assertions';

export const createViewPKPsByAuthDataTest = (
ctx: Awaited<ReturnType<typeof init>>,
getAuthContext: () => any
authData?: any
) => {
return async () => {
const { ViemAccountAuthenticator } = await import('@lit-protocol/auth');
const authData = await ViemAccountAuthenticator.authenticate(
ctx.aliceViemAccount
);
const _authData =
authData ||
(await ViemAccountAuthenticator.authenticate(ctx.aliceViemAccount));

const pkps = await ctx.litClient.viewPKPsByAuthData({
authData: {
authMethodType: authData.authMethodType,
authMethodId: authData.authMethodId,
authMethodType: _authData.authMethodType,
authMethodId: _authData.authMethodId,
},
pagination: {
limit: 10,
Expand Down
50 changes: 48 additions & 2 deletions e2e/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {
storagePlugins,
ViemAccountAuthenticator,
} from '@lit-protocol/auth';
import { createLitClient } from '@lit-protocol/lit-client';
import { Account, PrivateKeyAccount } from 'viem';
import { createLitClient, utils as litUtils } from '@lit-protocol/lit-client';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { z } from 'zod';
import { fundAccount } from './helper/fundAccount';
import { getOrCreatePkp } from './helper/pkp-utils';

// import { createPkpAuthContext } from './helper/auth-contexts';

const SupportedNetworkSchema = z.enum([
Expand All @@ -17,6 +17,7 @@ const SupportedNetworkSchema = z.enum([
'naga-local',
'naga-staging',
]);

type SupportedNetwork = z.infer<typeof SupportedNetworkSchema>;

const LogLevelSchema = z.enum(['silent', 'info', 'debug']);
Expand All @@ -27,6 +28,9 @@ const LIVE_NETWORK_FUNDING_AMOUNT = '0.01';
const LOCAL_NETWORK_FUNDING_AMOUNT = '1';
const LIVE_NETWORK_LEDGER_DEPOSIT_AMOUNT = '2';

const EVE_VALIDATION_IPFS_CID =
'QmcxWmo3jefFsPUnskJXYBwsJYtiFuMAH1nDQEs99AwzDe';

export const init = async (
network?: SupportedNetwork,
logLevel?: LogLevel
Expand All @@ -42,6 +46,10 @@ export const init = async (
bobViemAccountPkp: any;
aliceEoaAuthContext: any;
alicePkpAuthContext: any;
eveViemAccount: any;
eveCustomAuthData: Awaited<ReturnType<typeof litUtils.generateAuthData>>;
eveViemAccountPkp: Awaited<ReturnType<typeof litClient.mintWithCustomAuth>>['pkpData']['data'];
eveValidationIpfsCid: string;
masterDepositForUser: (userAddress: string) => Promise<void>;
// alicePkpViemAccountPermissionsManager: any,
}> => {
Expand All @@ -66,6 +74,8 @@ export const init = async (
bobViemAccount
);

const eveViemAccount = privateKeyToAccount(generatePrivateKey());

/**
* ====================================
* Environment settings
Expand Down Expand Up @@ -129,6 +139,11 @@ export const init = async (
thenFundWith: fundingAmount,
});

await fundAccount(eveViemAccount, masterAccount, _networkModule, {
ifLessThan: fundingAmount,
thenFundWith: fundingAmount,
});

/**
* ====================================
* Initialise the LitClient
Expand Down Expand Up @@ -184,6 +199,27 @@ export const init = async (
_network
),
]);
// Use custom auth to create a PKP for Eve
const uniqueDappName = 'e2e-test-dapp';

const authMethodConfig = litUtils.generateUniqueAuthMethodType({
uniqueDappName: uniqueDappName,
});

const eveCustomAuthData = litUtils.generateAuthData({
uniqueDappName: uniqueDappName,
uniqueAuthMethodType: authMethodConfig.bigint,
userId: 'eve',
});

const { pkpData } = await litClient.mintWithCustomAuth({
account: eveViemAccount,
authData: eveCustomAuthData,
scope: 'sign-anything',
validationIpfsCid: EVE_VALIDATION_IPFS_CID,
});

const eveViemAccountPkp = pkpData.data;

/**
* ====================================
Expand Down Expand Up @@ -270,6 +306,12 @@ export const init = async (
// Deposit to the Bob PKP Ledger
await masterDepositForUser(bobViemAccountPkp.ethAddress);

// Deposit to the Eve EOA Ledger
await masterDepositForUser(eveViemAccount.address);

// Deposit to the Eve PKP Ledger
await masterDepositForUser(eveViemAccountPkp.ethAddress);

// const alicePkpViemAccountPermissionsManager = await litClient.getPKPPermissionsManager({
// pkpIdentifier: {
// tokenId: aliceViemAccountPkp.tokenId,
Expand All @@ -292,6 +334,10 @@ export const init = async (
bobViemAccount,
bobViemAccountAuthData,
bobViemAccountPkp,
eveViemAccount,
eveCustomAuthData,
eveViemAccountPkp,
eveValidationIpfsCid: EVE_VALIDATION_IPFS_CID,
aliceEoaAuthContext,
alicePkpAuthContext,
masterDepositForUser,
Expand Down
Loading
Loading