Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a flag to differntiate V2 & V3 eth wallet storage key #200

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
25 changes: 2 additions & 23 deletions packages/getlit-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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');
}

Expand Down
11 changes: 9 additions & 2 deletions packages/getlit-sdk/src/lib/lit-options-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions packages/getlit-sdk/src/lib/lit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ ${LitMessages.persistentStorageExample}`;
res?.decryptedData as Uint8Array
);

log.info('msg:', msg);

if (!res?.decryptedData) {
log.throw('Could not decrypt data');
}
Expand Down
11 changes: 10 additions & 1 deletion packages/getlit-sdk/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any[]> = [];

Expand Down Expand Up @@ -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
========================================================================`,
};

/**
Expand Down
14 changes: 14 additions & 0 deletions packages/lit-auth-client/src/lib/lit-auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-<address>`
*
* By default, @getlit/sdk would go for V3, but the primitive would stay with V2.
*/
private version: 'V2' | 'V3' = 'V2';

/**
* Create a LitAuthClient instance
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -114,6 +127,7 @@ export class LitAuthClient {
relay: this.relay,
litNodeClient: this.litNodeClient,
storageProvider: this.storageProvider,
version: this.version,
};

let provider: T;
Expand Down
9 changes: 9 additions & 0 deletions packages/lit-auth-client/src/lib/providers/BaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-<address>`
* 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';
}

/**
Expand Down
13 changes: 8 additions & 5 deletions packages/lit-auth-client/src/lib/providers/EthWalletProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-<address>` 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-<address>` cache key

if (isBrowser()) {
setNewExpiration = false;

Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 19 additions & 3 deletions packages/types/src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-<address>`
* By default, @getlit/sdk would go for V3, but the primitive would stay with V2.
*/
version?: 'V2' | 'V3';
}

export interface OtpSessionResult {
Expand Down Expand Up @@ -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-<address>`
* By default, @getlit/sdk would go for V3, but the primitive would stay with V2.
*/
version?: 'V2' | 'V3';
}

export interface OAuthProviderOptions {
Expand Down Expand Up @@ -1455,8 +1471,9 @@ export interface OtpAuthenticateOptions {
code: string;
}


export interface EthWalletAuthenticateOptions extends BaseAuthenticateOptions, ExpirableOptions {
export interface EthWalletAuthenticateOptions
extends BaseAuthenticateOptions,
ExpirableOptions {
/**
* Ethereum wallet address
*/
Expand Down Expand Up @@ -1503,7 +1520,6 @@ export interface ExpirableOptions {
expirationUnit?: 'seconds' | 'minutes' | 'hours' | 'days';

expirationLength?: number;

}
export interface StytchOtpAuthenticateOptions extends BaseAuthenticateOptions {
/*
Expand Down
Loading