-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdata-set.ts
More file actions
112 lines (106 loc) · 4.35 KB
/
data-set.ts
File metadata and controls
112 lines (106 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* LocalStorage helpers for managing data set IDs scoped per wallet address.
*
* Why scope to wallet address?
* ----------------------------
* Data sets in Synapse are wallet-specific - each data set is created by and
* belongs to a specific wallet address. By scoping data set IDs to wallet addresses,
* we ensure:
*
* 1. **Multi-wallet support**: Users can switch between different wallets, and each
* wallet will have its own data set ID stored separately.
*
* 2. **Data integrity**: We prevent accidentally using a data set that was created
* with a different wallet, which would cause authorization errors or failed uploads.
*
* 3. **User isolation**: In scenarios where multiple users share the same browser
* (e.g., shared computers, testing), each wallet maintains its own data set.
*
* Storage format: `filecoin-pin-data-set-id-{version}-{walletAddress}` → `{dataSetId}`
*/
const DATA_SET_ID_KEY = 'filecoin-pin-data-set-id-v2'
/**
* Get the storage key for a given wallet and optional provider.
*
* @param walletAddress - The wallet address
* @param providerId - Optional provider ID for provider-specific keys
* @returns The localStorage key string
*/
const getDataSetStorageKey = (walletAddress: string, providerId?: number): string => {
if (providerId) {
return `${DATA_SET_ID_KEY}-${walletAddress}-provider-${providerId}`
}
return `${DATA_SET_ID_KEY}-${walletAddress}`
}
/**
* Get the stored data set ID for a specific wallet address.
*
* @param walletAddress - The wallet address to look up the data set ID for
* @returns The data set ID if found, null otherwise
*/
export const getStoredDataSetId = (walletAddress: string): number | null => {
try {
const key = getDataSetStorageKey(walletAddress)
const storedId = localStorage.getItem(key)
console.debug('[DataSetStorage] Reading from key:', key, '→', storedId || 'not found')
return storedId ? Number.parseInt(storedId, 10) : null
} catch (error) {
console.warn('[DataSetStorage] Failed to read data set ID from localStorage:', error)
return null
}
}
/**
* Store the data set ID for a specific wallet address.
*
* @param walletAddress - The wallet address to associate with this data set ID
* @param dataSetId - The data set ID to store
*/
export const storeDataSetId = (walletAddress: string, dataSetId: number): void => {
try {
const key = getDataSetStorageKey(walletAddress)
localStorage.setItem(key, dataSetId.toString())
console.debug('[DataSetStorage] Stored dataSetId:', dataSetId, 'to key:', key)
} catch (error) {
console.warn('[DataSetStorage] Failed to store data set ID in localStorage:', error)
}
}
/**
* Get the stored data set ID for a specific wallet address and provider ID.
*
* This allows testing/debugging with different providers by storing separate
* data sets per wallet+provider combination.
*
* @param walletAddress - The wallet address to look up the data set ID for
* @param providerId - The provider ID to scope the data set to
* @returns The data set ID if found, null otherwise
*/
export const getStoredDataSetIdForProvider = (walletAddress: string, providerId: number): number | null => {
try {
const key = getDataSetStorageKey(walletAddress, providerId)
const storedId = localStorage.getItem(key)
console.debug('[DataSetStorage] Reading from key:', key, '→', storedId || 'not found')
return storedId ? Number.parseInt(storedId, 10) : null
} catch (error) {
console.warn('[DataSetStorage] Failed to read data set ID from localStorage:', error)
return null
}
}
/**
* Store the data set ID for a specific wallet address and provider ID.
*
* This allows testing/debugging with different providers by storing separate
* data sets per wallet+provider combination.
*
* @param walletAddress - The wallet address to associate with this data set ID
* @param providerId - The provider ID to scope the data set to
* @param dataSetId - The data set ID to store
*/
export const storeDataSetIdForProvider = (walletAddress: string, providerId: number, dataSetId: number): void => {
try {
const key = getDataSetStorageKey(walletAddress, providerId)
localStorage.setItem(key, dataSetId.toString())
console.debug('[DataSetStorage] Stored dataSetId:', dataSetId, 'to key:', key)
} catch (error) {
console.warn('[DataSetStorage] Failed to store data set ID in localStorage:', error)
}
}