From f7a3152611cac8151dd161121abfe3b94b4bea15 Mon Sep 17 00:00:00 2001 From: Josh Long Date: Fri, 4 Oct 2024 10:17:37 -0400 Subject: [PATCH 1/4] add local storage provider optiion and test for expired sesson signature --- .env.ci | 2 +- local-tests/setup/tinny-config.ts | 4 ++ local-tests/setup/tinny-environment.ts | 13 +++++ local-tests/setup/tinny-operations.ts | 4 +- local-tests/test.ts | 3 ++ ...tPkpSessionSigsIsValidAfterEllapsedTime.ts | 47 +++++++++++++++++++ 6 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts diff --git a/.env.ci b/.env.ci index a07d05981f..9eb4b96846 100644 --- a/.env.ci +++ b/.env.ci @@ -10,7 +10,7 @@ NO_SETUP=false USE_SHIVA=true NETWORK_CONFIG=./networkContext.json TEST_TIMEOUT=45000 - +USE_STORAGE=true #Shiva Client ENV Vars STOP_TESTNET=false TESTNET_MANAGER_URL=http://127.0.0.1:8000 diff --git a/local-tests/setup/tinny-config.ts b/local-tests/setup/tinny-config.ts index 8ebf013197..94e9763140 100644 --- a/local-tests/setup/tinny-config.ts +++ b/local-tests/setup/tinny-config.ts @@ -123,6 +123,10 @@ export interface ProcessEnvs { * this value will be ignored */ NETWORK_CONFIG: string; + + STORAGE_CACHE: string; + + USE_STORAGE: boolean; } /** diff --git a/local-tests/setup/tinny-environment.ts b/local-tests/setup/tinny-environment.ts index 21daf4c6c9..cf2256b5bb 100644 --- a/local-tests/setup/tinny-environment.ts +++ b/local-tests/setup/tinny-environment.ts @@ -20,6 +20,7 @@ import { createSiweMessage, generateAuthSig } from '@lit-protocol/auth-helpers'; import { ShivaClient, TestnetClient } from './shiva-client'; import { toErrorWithMessage } from './tinny-utils'; import { CENTRALISATION_BY_NETWORK } from '@lit-protocol/constants'; +import { LocalStorage } from 'node-localstorage'; console.log('checking env', process.env['DEBUG']); export class TinnyEnvironment { @@ -76,6 +77,9 @@ export class TinnyEnvironment { NO_SETUP: process.env['NO_SETUP'] === 'true', USE_SHIVA: process.env['USE_SHIVA'] === 'true', NETWORK_CONFIG: process.env['NETWORK_CONFIG'] ?? './networkContext.json', + + STORAGE_CACHE: process.env['STORAGE_CACHE'] ?? './storage', + USE_STORAGE: process.env['USE_STORAGE'] === 'true' }; public litNodeClient: LitNodeClient; @@ -241,6 +245,9 @@ export class TinnyEnvironment { this?.testnet?.ContractContext ?? this._contractContext; this.litNodeClient = new LitNodeClient({ litNetwork: 'custom', + storageProvider: { + provider: this.processEnvs.USE_STORAGE ? new LocalStorage(this.processEnvs.STORAGE_CACHE) : undefined + }, rpcUrl: this.rpc, debug: this.processEnvs.DEBUG, checkNodeAttestation: false, // disable node attestation check for local testing @@ -250,12 +257,18 @@ export class TinnyEnvironment { this.litNodeClient = new LitNodeClient({ litNetwork: this.network, checkNodeAttestation: true, + storageProvider: { + provider: this.processEnvs.USE_STORAGE ? new LocalStorage(this.processEnvs.STORAGE_CACHE) : undefined + }, debug: this.processEnvs.DEBUG, }); } else if (centralisation === 'centralised') { this.litNodeClient = new LitNodeClient({ litNetwork: this.network, checkNodeAttestation: false, + storageProvider: { + provider: this.processEnvs.USE_STORAGE ? new LocalStorage(this.processEnvs.STORAGE_CACHE) : undefined + }, debug: this.processEnvs.DEBUG, }); } else { diff --git a/local-tests/setup/tinny-operations.ts b/local-tests/setup/tinny-operations.ts index c0f70aceb9..4105c4b29d 100644 --- a/local-tests/setup/tinny-operations.ts +++ b/local-tests/setup/tinny-operations.ts @@ -85,9 +85,9 @@ export const runInBand = async ({ console.error( `${testName} - Failed after ${maxAttempts} attempts (${timeTaken} ms)` ); - console.error(`Error: ${error}`); + console.error(`Error: ${error.message}\nStack Trace: ${error.stackTrace}`); failedTests.push( - `${testName} (Failed in ${timeTaken} ms) - Error: ${error}` + `${testName} (Failed in ${timeTaken} ms) - Error: ${error.message}` ); } } diff --git a/local-tests/test.ts b/local-tests/test.ts index 1c43482116..e706342866 100644 --- a/local-tests/test.ts +++ b/local-tests/test.ts @@ -105,6 +105,8 @@ import { testExportWrappedKey } from './tests/wrapped-keys/testExportWrappedKey' import { testSignMessageWithSolanaEncryptedKey } from './tests/wrapped-keys/testSignMessageWithSolanaEncryptedKey'; import { testSignTransactionWithSolanaEncryptedKey } from './tests/wrapped-keys/testSignTransactionWithSolanaEncryptedKey'; +import {testPkpSessionSigsIsValidAfterEllapsedTime} from './tests/testPkpSessionSigsIsValidAfterEllapsedTime'; + (async () => { console.log('[𐬺🧪 Tinny𐬺] Running tests...'); const devEnv = new TinnyEnvironment(); @@ -176,6 +178,7 @@ import { testSignTransactionWithSolanaEncryptedKey } from './tests/wrapped-keys/ testUsePkpSessionSigsToEncryptDecryptString, testUsePkpSessionSigsToEncryptDecryptFile, testUsePkpSessionSigsToEncryptDecryptZip, + testPkpSessionSigsIsValidAfterEllapsedTime }; const litActionSessionSigsTests = { diff --git a/local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts b/local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts new file mode 100644 index 0000000000..68dfde9a36 --- /dev/null +++ b/local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts @@ -0,0 +1,47 @@ +import { LIT_ENDPOINT_VERSION } from '@lit-protocol/constants'; +import { log } from '@lit-protocol/misc'; +import { LIT_TESTNET } from 'local-tests/setup/tinny-config'; +import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs'; +import { TinnyEnvironment } from 'local-tests/setup/tinny-environment'; +import { LitAbility, LitActionResource, LitPKPResource } from '@lit-protocol/auth-helpers'; + +/** + * Test Commands: + * ✅ NETWORK=cayenne yarn test:local --filter=testUsePkpSessionSigsToPkpSign + * ✅ NETWORK=manzano yarn test:local --filter=testUsePkpSessionSigsToPkpSign + * ✅ NETWORK=localchain yarn test:local --filter=testUsePkpSessionSigsToPkpSign + */ +export const testPkpSessionSigsIsValidAfterEllapsedTime = async ( + devEnv: TinnyEnvironment +) => { + const alice = await devEnv.createRandomPerson(); + + const pkpSessionSigs = await getPkpSessionSigs(devEnv, alice, + [ + { + resource: new LitPKPResource('*'), + ability: LitAbility.PKPSigning, + }, + { + resource: new LitActionResource('*'), + ability: LitAbility.LitActionExecution, + }, + ], + new Date(Date.now() + 1000).toISOString() + ); + await new Promise((res, rej) => { + setTimeout(res, 2000); + }); + + try { + const res = await devEnv.litNodeClient.pkpSign({ + toSign: alice.loveLetter, + pubKey: alice.authMethodOwnedPkp.publicKey, + sessionSigs: pkpSessionSigs, + }); + }catch (e) { + console.log('✅ Session validation failed as expected: error is ', e.message); + } + + devEnv.releasePrivateKeyFromUser(alice); +}; From 53521754699f45e3b35090c33bebfed5fcb26e61 Mon Sep 17 00:00:00 2001 From: Josh Long Date: Fri, 4 Oct 2024 11:00:35 -0400 Subject: [PATCH 2/4] chore: fmt --- local-tests/setup/tinny-environment.ts | 14 ++-- local-tests/setup/tinny-operations.ts | 4 +- local-tests/test.ts | 4 +- ...tPkpSessionSigsIsValidAfterEllapsedTime.ts | 72 ++++++++++++------- 4 files changed, 60 insertions(+), 34 deletions(-) diff --git a/local-tests/setup/tinny-environment.ts b/local-tests/setup/tinny-environment.ts index cf2256b5bb..d0cf5a7a81 100644 --- a/local-tests/setup/tinny-environment.ts +++ b/local-tests/setup/tinny-environment.ts @@ -79,7 +79,7 @@ export class TinnyEnvironment { NETWORK_CONFIG: process.env['NETWORK_CONFIG'] ?? './networkContext.json', STORAGE_CACHE: process.env['STORAGE_CACHE'] ?? './storage', - USE_STORAGE: process.env['USE_STORAGE'] === 'true' + USE_STORAGE: process.env['USE_STORAGE'] === 'true', }; public litNodeClient: LitNodeClient; @@ -246,7 +246,9 @@ export class TinnyEnvironment { this.litNodeClient = new LitNodeClient({ litNetwork: 'custom', storageProvider: { - provider: this.processEnvs.USE_STORAGE ? new LocalStorage(this.processEnvs.STORAGE_CACHE) : undefined + provider: this.processEnvs.USE_STORAGE + ? new LocalStorage(this.processEnvs.STORAGE_CACHE) + : undefined, }, rpcUrl: this.rpc, debug: this.processEnvs.DEBUG, @@ -258,7 +260,9 @@ export class TinnyEnvironment { litNetwork: this.network, checkNodeAttestation: true, storageProvider: { - provider: this.processEnvs.USE_STORAGE ? new LocalStorage(this.processEnvs.STORAGE_CACHE) : undefined + provider: this.processEnvs.USE_STORAGE + ? new LocalStorage(this.processEnvs.STORAGE_CACHE) + : undefined, }, debug: this.processEnvs.DEBUG, }); @@ -267,7 +271,9 @@ export class TinnyEnvironment { litNetwork: this.network, checkNodeAttestation: false, storageProvider: { - provider: this.processEnvs.USE_STORAGE ? new LocalStorage(this.processEnvs.STORAGE_CACHE) : undefined + provider: this.processEnvs.USE_STORAGE + ? new LocalStorage(this.processEnvs.STORAGE_CACHE) + : undefined, }, debug: this.processEnvs.DEBUG, }); diff --git a/local-tests/setup/tinny-operations.ts b/local-tests/setup/tinny-operations.ts index 4105c4b29d..a5f843210f 100644 --- a/local-tests/setup/tinny-operations.ts +++ b/local-tests/setup/tinny-operations.ts @@ -85,7 +85,9 @@ export const runInBand = async ({ console.error( `${testName} - Failed after ${maxAttempts} attempts (${timeTaken} ms)` ); - console.error(`Error: ${error.message}\nStack Trace: ${error.stackTrace}`); + console.error( + `Error: ${error.message}\nStack Trace: ${error.stackTrace}` + ); failedTests.push( `${testName} (Failed in ${timeTaken} ms) - Error: ${error.message}` ); diff --git a/local-tests/test.ts b/local-tests/test.ts index e706342866..916e5ab75d 100644 --- a/local-tests/test.ts +++ b/local-tests/test.ts @@ -105,7 +105,7 @@ import { testExportWrappedKey } from './tests/wrapped-keys/testExportWrappedKey' import { testSignMessageWithSolanaEncryptedKey } from './tests/wrapped-keys/testSignMessageWithSolanaEncryptedKey'; import { testSignTransactionWithSolanaEncryptedKey } from './tests/wrapped-keys/testSignTransactionWithSolanaEncryptedKey'; -import {testPkpSessionSigsIsValidAfterEllapsedTime} from './tests/testPkpSessionSigsIsValidAfterEllapsedTime'; +import { testPkpSessionSigsIsValidAfterEllapsedTime } from './tests/testPkpSessionSigsIsValidAfterEllapsedTime'; (async () => { console.log('[𐬺🧪 Tinny𐬺] Running tests...'); @@ -178,7 +178,7 @@ import {testPkpSessionSigsIsValidAfterEllapsedTime} from './tests/testPkpSession testUsePkpSessionSigsToEncryptDecryptString, testUsePkpSessionSigsToEncryptDecryptFile, testUsePkpSessionSigsToEncryptDecryptZip, - testPkpSessionSigsIsValidAfterEllapsedTime + testPkpSessionSigsIsValidAfterEllapsedTime, }; const litActionSessionSigsTests = { diff --git a/local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts b/local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts index 68dfde9a36..6fba0a44c4 100644 --- a/local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts +++ b/local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts @@ -1,9 +1,14 @@ import { LIT_ENDPOINT_VERSION } from '@lit-protocol/constants'; -import { log } from '@lit-protocol/misc'; +import { log, validateSessionSigs } from '@lit-protocol/misc'; import { LIT_TESTNET } from 'local-tests/setup/tinny-config'; import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs'; import { TinnyEnvironment } from 'local-tests/setup/tinny-environment'; -import { LitAbility, LitActionResource, LitPKPResource } from '@lit-protocol/auth-helpers'; +import { + LitAbility, + LitActionResource, + LitPKPResource, +} from '@lit-protocol/auth-helpers'; +import { isValid } from 'date-and-time'; /** * Test Commands: @@ -14,34 +19,47 @@ import { LitAbility, LitActionResource, LitPKPResource } from '@lit-protocol/aut export const testPkpSessionSigsIsValidAfterEllapsedTime = async ( devEnv: TinnyEnvironment ) => { - const alice = await devEnv.createRandomPerson(); + const alice = await devEnv.createRandomPerson(); - const pkpSessionSigs = await getPkpSessionSigs(devEnv, alice, - [ - { - resource: new LitPKPResource('*'), - ability: LitAbility.PKPSigning, - }, - { - resource: new LitActionResource('*'), - ability: LitAbility.LitActionExecution, - }, - ], - new Date(Date.now() + 1000).toISOString() + let pkpSessionSigs = await getPkpSessionSigs( + devEnv, + alice, + [ + { + resource: new LitPKPResource('*'), + ability: LitAbility.PKPSigning, + }, + { + resource: new LitActionResource('*'), + ability: LitAbility.LitActionExecution, + }, + ], + new Date(Date.now() + 1000).toISOString() + ); + await new Promise((res, rej) => { + setTimeout(res, 2000); + }); + + let res = validateSessionSigs(pkpSessionSigs); + + if (isValid) { + throw new Error( + 'Session signature validation should fail with expiration ellapsed' ); - await new Promise((res, rej) => { - setTimeout(res, 2000); + } + console.log(res); + try { + const res = await devEnv.litNodeClient.pkpSign({ + toSign: alice.loveLetter, + pubKey: alice.authMethodOwnedPkp.publicKey, + sessionSigs: pkpSessionSigs, }); - - try { - const res = await devEnv.litNodeClient.pkpSign({ - toSign: alice.loveLetter, - pubKey: alice.authMethodOwnedPkp.publicKey, - sessionSigs: pkpSessionSigs, - }); - }catch (e) { - console.log('✅ Session validation failed as expected: error is ', e.message); - } + } catch (e) { + console.log( + '✅ Session validation failed as expected: error is ', + e.message + ); + } devEnv.releasePrivateKeyFromUser(alice); }; From f715594bb3723156819b00160966a3b271d9bfc5 Mon Sep 17 00:00:00 2001 From: Josh Long Date: Fri, 4 Oct 2024 11:10:32 -0400 Subject: [PATCH 3/4] ref: remove import --- .../tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts b/local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts index 6fba0a44c4..b1a26f9948 100644 --- a/local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts +++ b/local-tests/tests/testPkpSessionSigsIsValidAfterEllapsedTime.ts @@ -8,7 +8,6 @@ import { LitActionResource, LitPKPResource, } from '@lit-protocol/auth-helpers'; -import { isValid } from 'date-and-time'; /** * Test Commands: @@ -42,7 +41,7 @@ export const testPkpSessionSigsIsValidAfterEllapsedTime = async ( let res = validateSessionSigs(pkpSessionSigs); - if (isValid) { + if (res.isValid) { throw new Error( 'Session signature validation should fail with expiration ellapsed' ); From 74bdbbe381b58e961894634efa850bbc6a809464 Mon Sep 17 00:00:00 2001 From: Josh Long Date: Fri, 4 Oct 2024 11:29:25 -0400 Subject: [PATCH 4/4] ci: remove local storage in ci --- .env.ci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.ci b/.env.ci index 9eb4b96846..322cb68e0b 100644 --- a/.env.ci +++ b/.env.ci @@ -10,7 +10,7 @@ NO_SETUP=false USE_SHIVA=true NETWORK_CONFIG=./networkContext.json TEST_TIMEOUT=45000 -USE_STORAGE=true +USE_STORAGE=false #Shiva Client ENV Vars STOP_TESTNET=false TESTNET_MANAGER_URL=http://127.0.0.1:8000