From 3c46962c28aa9e59571cbf5bfc5299ff3580691f Mon Sep 17 00:00:00 2001 From: Ansonhkg Date: Wed, 23 Aug 2023 16:17:13 +0100 Subject: [PATCH] Squashed commit of the following: commit 43599efcbddf7ae7ad703907c60970bf273835f8 Author: Ansonhkg Date: Wed Aug 23 16:15:08 2023 +0100 feat: add a flag to differntiate V2 & V3 eth wallet storage key --- packages/getlit-sdk/src/index.ts | 25 ++----------------- .../getlit-sdk/src/lib/lit-options-builder.ts | 11 ++++++-- packages/getlit-sdk/src/lib/lit.ts | 2 ++ packages/getlit-sdk/src/lib/utils.ts | 11 +++++++- .../src/lib/lit-auth-client.ts | 14 +++++++++++ .../src/lib/providers/BaseProvider.ts | 9 +++++++ .../src/lib/providers/EthWalletProvider.ts | 13 ++++++---- packages/types/src/lib/interfaces.ts | 22 +++++++++++++--- 8 files changed, 73 insertions(+), 34 deletions(-) diff --git a/packages/getlit-sdk/src/index.ts b/packages/getlit-sdk/src/index.ts index 3681b919a1..60459960b3 100644 --- a/packages/getlit-sdk/src/index.ts +++ b/packages/getlit-sdk/src/index.ts @@ -1,7 +1,7 @@ import './global'; import { LitAnalytics } from './lib/analytics'; import { LitOptionsBuilder } from './lib/lit-options-builder'; -import { log, waitForLit } from './lib/utils'; +import { LitMessages, log, waitForLit } from './lib/utils'; /** * This is a trick to make the instance behave like a function where you can async/await, but still be an instance where you can chain methods like .withPersistentStorage() .withAuthOptions() etc. @@ -39,28 +39,7 @@ const loadLit = async ( globalThis.Lit.collectAnalytics = collectAnalytics ?? true; if (globalThis.Lit.collectAnalytics) { - console.log( - '========================================================================' - ); - console.log( - "NOTICE: We're collecting anonymous usage data to help improve our product." - ); - console.log( - 'Your privacy is important to us. We only collect data that helps us understand how our product is being used.' - ); - console.log( - 'None of the collected data can be used to identify you, and we do not share the data with any third parties.' - ); - console.log( - "If you'd like to opt out of data collection, you can do so by setting the 'collectAnalytics' parameter to 'false' when calling the 'loadLit' function." - ); - console.log( - 'For example: loadLit({ debug: true, collectAnalytics: false })' - ); - console.log('Thank you for helping us improve our product!'); - console.log( - '========================================================================' - ); + console.log(LitMessages.usageAnalyticsNotice); LitAnalytics.collect('loadLit'); } diff --git a/packages/getlit-sdk/src/lib/lit-options-builder.ts b/packages/getlit-sdk/src/lib/lit-options-builder.ts index cc13870144..606e93c30e 100644 --- a/packages/getlit-sdk/src/lib/lit-options-builder.ts +++ b/packages/getlit-sdk/src/lib/lit-options-builder.ts @@ -116,8 +116,14 @@ export class LitOptionsBuilder { public withPersistentStorage({ provider, options }: PersistentStorageConfig) { log.info('------ withPersistentStorage ------'); this.custom.persistentStorage = true; - console.log("this.custom.persistentStorage:", this.custom.persistentStorage) - console.log("globalThis.Lit.builder?.custom.persistentStorage:", globalThis.Lit.builder?.custom.persistentStorage) + console.log( + 'this.custom.persistentStorage:', + this.custom.persistentStorage + ); + console.log( + 'globalThis.Lit.builder?.custom.persistentStorage:', + globalThis.Lit.builder?.custom.persistentStorage + ); this.initialiseIPFSProvider({ provider, @@ -185,6 +191,7 @@ export class LitOptionsBuilder { ...(this._authOptions && { ...this._authOptions }), litNodeClient: globalThis.Lit.nodeClient, storageProvider: globalThis.Lit.storage, + version: 'V3', }); if (!globalThis.Lit.authClient) { diff --git a/packages/getlit-sdk/src/lib/lit.ts b/packages/getlit-sdk/src/lib/lit.ts index 95bed132ba..db699bd7da 100644 --- a/packages/getlit-sdk/src/lib/lit.ts +++ b/packages/getlit-sdk/src/lib/lit.ts @@ -327,6 +327,8 @@ ${LitMessages.persistentStorageExample}`; res?.decryptedData as Uint8Array ); + log.info('msg:', msg); + if (!res?.decryptedData) { log.throw('Could not decrypt data'); } diff --git a/packages/getlit-sdk/src/lib/utils.ts b/packages/getlit-sdk/src/lib/utils.ts index 7d3b8a151d..8e35e99504 100644 --- a/packages/getlit-sdk/src/lib/utils.ts +++ b/packages/getlit-sdk/src/lib/utils.ts @@ -24,7 +24,7 @@ import { BaseAuthenticateOptions, } from '@lit-protocol/types'; -const version = '0.0.706'; +const version = '0.0.714'; const PREFIX = 'GetLit SDK'; const logBuffer: Array = []; @@ -700,6 +700,15 @@ loadLit.withPersistentStorage({ }, }, });`, +usageAnalyticsNotice: ` +========================================================================\n +NOTICE: We're collecting anonymous usage data to help improve our product.\n +Your privacy is important to us. We only collect data that helps us understand how our product is being used.\n +None of the collected data can be used to identify you, and we do not share the data with any third parties.\n +If you'd like to opt out of data collection, you can do so by setting the 'collectAnalytics' parameter to 'false' when calling the 'loadLit' function.\n +For example: loadLit({ debug: true, collectAnalytics: false })\n +Thank you for helping us improve our product!\n +========================================================================`, }; /** diff --git a/packages/lit-auth-client/src/lib/lit-auth-client.ts b/packages/lit-auth-client/src/lib/lit-auth-client.ts index 9150dddd94..c8c9545414 100644 --- a/packages/lit-auth-client/src/lib/lit-auth-client.ts +++ b/packages/lit-auth-client/src/lib/lit-auth-client.ts @@ -47,6 +47,15 @@ export class LitAuthClient { private storageProvider: LitStorage | undefined; + /** + * Handle both V2 and V3 versions of the access token, using different storage keys for each: + * - V2: `lit-auth-signature` + * - V3: `lit-ethwallet-token-
` + * + * By default, @getlit/sdk would go for V3, but the primitive would stay with V2. + */ + private version: 'V2' | 'V3' = 'V2'; + /** * Create a LitAuthClient instance * @@ -78,6 +87,10 @@ export class LitAuthClient { if (options?.storageProvider) { this.storageProvider = options?.storageProvider; } + + if (options?.version) { + this.version = options.version; + } } // Check if Lit node client is provided @@ -114,6 +127,7 @@ export class LitAuthClient { relay: this.relay, litNodeClient: this.litNodeClient, storageProvider: this.storageProvider, + version: this.version, }; let provider: T; diff --git a/packages/lit-auth-client/src/lib/providers/BaseProvider.ts b/packages/lit-auth-client/src/lib/providers/BaseProvider.ts index b725240148..f86698cfd9 100644 --- a/packages/lit-auth-client/src/lib/providers/BaseProvider.ts +++ b/packages/lit-auth-client/src/lib/providers/BaseProvider.ts @@ -32,11 +32,20 @@ export abstract class BaseProvider { public storageProvider: LitStorage; + /** + * Handle both V2 and V3 versions of the access token, using different storage keys for each: + * - V2: `lit-auth-signature` + * - V3: `lit-ethwallet-token-
` + * By default, @getlit/sdk would go for V3, but the primitive would stay with V2. + */ + public version?: 'V2' | 'V3'; + constructor(options: BaseProviderOptions) { this.rpcUrl = options.rpcUrl; this.relay = options.relay; this.litNodeClient = options.litNodeClient; this.storageProvider = options.storageProvider; + this.version = options.version ?? 'V2'; } /** diff --git a/packages/lit-auth-client/src/lib/providers/EthWalletProvider.ts b/packages/lit-auth-client/src/lib/providers/EthWalletProvider.ts index 1330623bed..0860656b56 100644 --- a/packages/lit-auth-client/src/lib/providers/EthWalletProvider.ts +++ b/packages/lit-auth-client/src/lib/providers/EthWalletProvider.ts @@ -83,18 +83,18 @@ export default class EthWalletProvider extends BaseProvider { const _options = { cache: true, - version: 'V3', + version: this?.version ?? 'V2', ...options, }; let setNewExpiration = false; + // ==================== VERSION 3 ==================== // Please refer to this document for the expected behaviour // https://www.notion.so/litprotocol/ETH-Wallet-Expected-Behaviour-1194ddeae22d4ff6a1a7bacf17ba5885?pvs=4 + // NOTE: we do not want to use the default `lit-auth-signature` when cache is enabled, + // instead we want to use the `lit-ethwallet-token-
` cache key if (_options.version === 'V3') { - // -- we do not want to use the default `lit-auth-signature` when cache is enabled, - // instead we want to use the `lit-ethwallet-token-
` cache key - if (isBrowser()) { setNewExpiration = false; @@ -239,7 +239,10 @@ export default class EthWalletProvider extends BaseProvider { _options?.expirationUnit ?? 'hours' ); } - } else { + } + // ==================== VERSION 2 ==================== + // Use `lit-auth-signature` as the cache key + else { if ((address && signMessage) || _options?.signer) { if (_options?.signer) { if (!address) { diff --git a/packages/types/src/lib/interfaces.ts b/packages/types/src/lib/interfaces.ts index 5cb382e949..90c8174a2c 100644 --- a/packages/types/src/lib/interfaces.ts +++ b/packages/types/src/lib/interfaces.ts @@ -1119,6 +1119,14 @@ export interface LitAuthClientOptions { litOtpConfig?: OtpProviderOptions; storageProvider?: any; + + /** + * Handle both V2 and V3 versions of the access token, using different storage keys for each: + * - V2: `lit-auth-signature` + * - V3: `lit-ethwallet-token-
` + * By default, @getlit/sdk would go for V3, but the primitive would stay with V2. + */ + version?: 'V2' | 'V3'; } export interface OtpSessionResult { @@ -1320,6 +1328,14 @@ export interface BaseProviderOptions { litNodeClient: any; storageProvider?: any; + + /** + * Handle both V2 and V3 versions of the access token, using different storage keys for each: + * - V2: `lit-auth-signature` + * - V3: `lit-ethwallet-token-
` + * By default, @getlit/sdk would go for V3, but the primitive would stay with V2. + */ + version?: 'V2' | 'V3'; } export interface OAuthProviderOptions { @@ -1455,8 +1471,9 @@ export interface OtpAuthenticateOptions { code: string; } - -export interface EthWalletAuthenticateOptions extends BaseAuthenticateOptions, ExpirableOptions { +export interface EthWalletAuthenticateOptions + extends BaseAuthenticateOptions, + ExpirableOptions { /** * Ethereum wallet address */ @@ -1503,7 +1520,6 @@ export interface ExpirableOptions { expirationUnit?: 'seconds' | 'minutes' | 'hours' | 'days'; expirationLength?: number; - } export interface StytchOtpAuthenticateOptions extends BaseAuthenticateOptions { /*