@metamask/profile-sync-controller 7.0.1 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -1
- package/dist/controllers/user-storage/UserStorageController.cjs +3 -0
- package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.d.cts +6 -11
- package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.d.mts +6 -11
- package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.mjs +3 -0
- package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/mockMessenger.d.cts +2 -2
- package/dist/controllers/user-storage/__fixtures__/mockMessenger.d.mts +2 -2
- package/dist/controllers/user-storage/account-syncing/__fixtures__/test-utils.d.cts +1 -1
- package/dist/controllers/user-storage/account-syncing/__fixtures__/test-utils.d.mts +1 -1
- package/dist/controllers/user-storage/account-syncing/controller-integration.cjs +1 -1
- package/dist/controllers/user-storage/account-syncing/controller-integration.cjs.map +1 -1
- package/dist/controllers/user-storage/account-syncing/controller-integration.mjs +1 -1
- package/dist/controllers/user-storage/account-syncing/controller-integration.mjs.map +1 -1
- package/dist/sdk/user-storage.cjs +9 -3
- package/dist/sdk/user-storage.cjs.map +1 -1
- package/dist/sdk/user-storage.d.cts +8 -8
- package/dist/sdk/user-storage.d.cts.map +1 -1
- package/dist/sdk/user-storage.d.mts +8 -8
- package/dist/sdk/user-storage.d.mts.map +1 -1
- package/dist/sdk/user-storage.mjs +9 -3
- package/dist/sdk/user-storage.mjs.map +1 -1
- package/dist/shared/storage-schema.cjs +18 -11
- package/dist/shared/storage-schema.cjs.map +1 -1
- package/dist/shared/storage-schema.d.cts +24 -2
- package/dist/shared/storage-schema.d.cts.map +1 -1
- package/dist/shared/storage-schema.d.mts +24 -2
- package/dist/shared/storage-schema.d.mts.map +1 -1
- package/dist/shared/storage-schema.mjs +18 -11
- package/dist/shared/storage-schema.mjs.map +1 -1
- package/package.json +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user-storage.d.mts","sourceRoot":"","sources":["../../src/sdk/user-storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,8CAA0C;AAInE,OAAO,KAAK,EAAE,GAAG,EAAE,0BAAsB;AAEzC,OAAO,KAAK,EACV,
|
|
1
|
+
{"version":3,"file":"user-storage.d.mts","sourceRoot":"","sources":["../../src/sdk/user-storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,8CAA0C;AAInE,OAAO,KAAK,EAAE,GAAG,EAAE,0BAAsB;AAEzC,OAAO,KAAK,EACV,4BAA4B,EAC5B,6BAA6B,EAC7B,uCAAuC,EAExC,qCAAiC;AAGlC,eAAO,MAAM,WAAW,QAAS,GAAG,iBAAiB,MAAM,WACiB,CAAC;AAE7E,MAAM,MAAM,iBAAiB,GAAG;IAC9B,GAAG,EAAE,GAAG,CAAC;IACT,IAAI,EAAE,IAAI,CAAC,SAAS,EAAE,gBAAgB,GAAG,gBAAgB,GAAG,aAAa,CAAC,CAAC;CAC5E,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,aAAa,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAC5C,aAAa,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/C,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,CAAC,EAAE,cAAc,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,uCAAuC,GAAG;IACpD,SAAS,EAAE,MAAM,CAAC;IAElB,IAAI,EAAE,MAAM,CAAC;CACd,EAAE,CAAC;AAOJ,qBAAa,WAAW;;IACtB,SAAS,CAAC,MAAM,EAAE,iBAAiB,CAAC;IAEpC,SAAS,CAAC,OAAO,EAAE,kBAAkB,CAAC;IAEtC,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC;gBAEP,MAAM,EAAE,iBAAiB,EAAE,OAAO,EAAE,kBAAkB;IAM5D,OAAO,CACX,IAAI,EAAE,uCAAuC,EAC7C,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC;IAIV,aAAa,CACjB,IAAI,EAAE,6BAA6B,EACnC,MAAM,EAAE,CAAC,4BAA4B,EAAE,MAAM,CAAC,EAAE;IAK5C,OAAO,CACX,IAAI,EAAE,uCAAuC,GAC5C,OAAO,CAAC,MAAM,CAAC;IAIZ,kBAAkB,CACtB,IAAI,EAAE,6BAA6B,GAClC,OAAO,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC;IAIrB,UAAU,CACd,IAAI,EAAE,uCAAuC,GAC5C,OAAO,CAAC,IAAI,CAAC;IAIV,qBAAqB,CACzB,IAAI,EAAE,6BAA6B,GAClC,OAAO,CAAC,IAAI,CAAC;IAIV,gBAAgB,CACpB,IAAI,EAAE,6BAA6B,EACnC,MAAM,EAAE,4BAA4B,EAAE;IAKlC,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;CAsbvC"}
|
|
@@ -55,7 +55,9 @@ _UserStorage_instances = new WeakSet(), _UserStorage_upsertUserStorage = async f
|
|
|
55
55
|
const headers = await __classPrivateFieldGet(this, _UserStorage_instances, "m", _UserStorage_getAuthorizationHeader).call(this);
|
|
56
56
|
const storageKey = await this.getStorageKey();
|
|
57
57
|
const encryptedData = await encryption.encryptString(data, storageKey);
|
|
58
|
-
const encryptedPath = createEntryPath(path, storageKey
|
|
58
|
+
const encryptedPath = createEntryPath(path, storageKey, {
|
|
59
|
+
validateAgainstSchema: false,
|
|
60
|
+
});
|
|
59
61
|
const url = new URL(STORAGE_URL(this.env, encryptedPath));
|
|
60
62
|
const response = await fetch(url.toString(), {
|
|
61
63
|
method: 'PUT',
|
|
@@ -145,7 +147,9 @@ _UserStorage_instances = new WeakSet(), _UserStorage_upsertUserStorage = async f
|
|
|
145
147
|
try {
|
|
146
148
|
const headers = await __classPrivateFieldGet(this, _UserStorage_instances, "m", _UserStorage_getAuthorizationHeader).call(this);
|
|
147
149
|
const storageKey = await this.getStorageKey();
|
|
148
|
-
const encryptedPath = createEntryPath(path, storageKey
|
|
150
|
+
const encryptedPath = createEntryPath(path, storageKey, {
|
|
151
|
+
validateAgainstSchema: false,
|
|
152
|
+
});
|
|
149
153
|
const url = new URL(STORAGE_URL(this.env, encryptedPath));
|
|
150
154
|
const response = await fetch(url.toString(), {
|
|
151
155
|
headers: {
|
|
@@ -239,7 +243,9 @@ _UserStorage_instances = new WeakSet(), _UserStorage_upsertUserStorage = async f
|
|
|
239
243
|
try {
|
|
240
244
|
const headers = await __classPrivateFieldGet(this, _UserStorage_instances, "m", _UserStorage_getAuthorizationHeader).call(this);
|
|
241
245
|
const storageKey = await this.getStorageKey();
|
|
242
|
-
const encryptedPath = createEntryPath(path, storageKey
|
|
246
|
+
const encryptedPath = createEntryPath(path, storageKey, {
|
|
247
|
+
validateAgainstSchema: false,
|
|
248
|
+
});
|
|
243
249
|
const url = new URL(STORAGE_URL(this.env, encryptedPath));
|
|
244
250
|
const response = await fetch(url.toString(), {
|
|
245
251
|
method: 'DELETE',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user-storage.mjs","sourceRoot":"","sources":["../../src/sdk/user-storage.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,qBAAiB;AAC3D,OAAO,UAAU,EAAE,EAAE,gBAAgB,EAAE,uCAA6B;AACpE,OAAO,EAAE,WAAW,EAAE,2CAAuC;AAE7D,OAAO,EAAE,UAAU,EAAE,0BAAsB;AAO3C,OAAO,EAAE,eAAe,EAAE,qCAAiC;AAE3D,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,GAAQ,EAAE,aAAqB,EAAE,E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AAM,WAAW,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;IAC5D,OAAO,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE,CAAC;AACpD,CAAC","sourcesContent":["import type { IBaseAuth } from './authentication-jwt-bearer/types';\nimport { NotFoundError, UserStorageError } from './errors';\nimport encryption, { createSHA256Hash } from '../shared/encryption';\nimport { SHARED_SALT } from '../shared/encryption/constants';\nimport type { Env } from '../shared/env';\nimport { getEnvUrls } from '../shared/env';\nimport type {\n UserStorageFeatureKeys,\n UserStorageFeatureNames,\n UserStoragePathWithFeatureAndKey,\n UserStoragePathWithFeatureOnly,\n} from '../shared/storage-schema';\nimport { createEntryPath } from '../shared/storage-schema';\n\nexport const STORAGE_URL = (env: Env, encryptedPath: string) =>\n `${getEnvUrls(env).userStorageApiUrl}/api/v1/userstorage/${encryptedPath}`;\n\nexport type UserStorageConfig = {\n env: Env;\n auth: Pick<IBaseAuth, 'getAccessToken' | 'getUserProfile' | 'signMessage'>;\n};\n\nexport type StorageOptions = {\n getStorageKey: () => Promise<string | null>;\n setStorageKey: (val: string) => Promise<void>;\n};\n\nexport type UserStorageOptions = {\n storage?: StorageOptions;\n};\n\nexport type GetUserStorageAllFeatureEntriesResponse = {\n HashedKey: string;\n\n Data: string;\n}[];\n\ntype ErrorMessage = {\n message: string;\n error: string;\n};\n\nexport class UserStorage {\n protected config: UserStorageConfig;\n\n protected options: UserStorageOptions;\n\n protected env: Env;\n\n constructor(config: UserStorageConfig, options: UserStorageOptions) {\n this.env = config.env;\n this.config = config;\n this.options = options;\n }\n\n async setItem(\n path: UserStoragePathWithFeatureAndKey,\n value: string,\n ): Promise<void> {\n await this.#upsertUserStorage(path, value);\n }\n\n async batchSetItems<FeatureName extends UserStorageFeatureNames>(\n path: FeatureName,\n values: [UserStorageFeatureKeys<FeatureName>, string][],\n ) {\n await this.#batchUpsertUserStorage(path, values);\n }\n\n async getItem(path: UserStoragePathWithFeatureAndKey): Promise<string> {\n return this.#getUserStorage(path);\n }\n\n async getAllFeatureItems(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<string[] | null> {\n return this.#getUserStorageAllFeatureEntries(path);\n }\n\n async deleteItem(path: UserStoragePathWithFeatureAndKey): Promise<void> {\n return this.#deleteUserStorage(path);\n }\n\n async deleteAllFeatureItems(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<void> {\n return this.#deleteUserStorageAllFeatureEntries(path);\n }\n\n async batchDeleteItems(\n path: UserStoragePathWithFeatureOnly,\n values: string[],\n ) {\n return this.#batchDeleteUserStorage(path, values);\n }\n\n async getStorageKey(): Promise<string> {\n const storageKey = await this.options.storage?.getStorageKey();\n if (storageKey) {\n return storageKey;\n }\n\n const userProfile = await this.config.auth.getUserProfile();\n const storageKeySignature = await this.config.auth.signMessage(\n `metamask:${userProfile.profileId}`,\n );\n const hashedStorageKeySignature = createSHA256Hash(storageKeySignature);\n await this.options.storage?.setStorageKey(hashedStorageKeySignature);\n return hashedStorageKeySignature;\n }\n\n async #upsertUserStorage(\n path: UserStoragePathWithFeatureAndKey,\n data: string,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedData = await encryption.encryptString(data, storageKey);\n const encryptedPath = createEntryPath(path, storageKey);\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: encryptedData }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #batchUpsertUserStorage(\n path: UserStoragePathWithFeatureOnly,\n data: [string, string][],\n ): Promise<void> {\n try {\n if (!data.length) {\n return;\n }\n\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const encryptedData = await Promise.all(\n data.map(async (d) => {\n return [\n this.#createEntryKey(d[0], storageKey),\n await encryption.encryptString(d[1], storageKey),\n ];\n }),\n );\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: Object.fromEntries(encryptedData) }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to batch upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries(\n path: UserStoragePathWithFeatureOnly,\n encryptedData: [string, string][],\n ): Promise<void> {\n try {\n if (!encryptedData.length) {\n return;\n }\n\n const headers = await this.#getAuthorizationHeader();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: Object.fromEntries(encryptedData) }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to batch upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorage(\n path: UserStoragePathWithFeatureAndKey,\n ): Promise<string> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedPath = createEntryPath(path, storageKey);\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(\n `feature/key set not found for path '${path}'.`,\n );\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const { Data: encryptedData } = await response.json();\n const decryptedData = await encryption.decryptString(\n encryptedData,\n storageKey,\n );\n\n // Re-encrypt the entry if it was encrypted with a random salt\n const salt = encryption.getSalt(encryptedData);\n if (salt.toString() !== SHARED_SALT.toString()) {\n await this.#upsertUserStorage(path, decryptedData);\n }\n\n return decryptedData;\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorageAllFeatureEntries(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<string[] | null> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(`feature not found for path '${path}'.`);\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const userStorage: GetUserStorageAllFeatureEntriesResponse | null =\n await response.json();\n\n if (!Array.isArray(userStorage)) {\n return null;\n }\n\n const decryptedData: string[] = [];\n const reEncryptedEntries: [string, string][] = [];\n\n for (const entry of userStorage) {\n if (!entry.Data) {\n continue;\n }\n\n try {\n const data = await encryption.decryptString(entry.Data, storageKey);\n decryptedData.push(data);\n\n // Re-encrypt the entry was encrypted with a random salt\n const salt = encryption.getSalt(entry.Data);\n if (salt.toString() !== SHARED_SALT.toString()) {\n reEncryptedEntries.push([\n entry.HashedKey,\n await encryption.encryptString(data, storageKey),\n ]);\n }\n } catch {\n // do nothing\n }\n }\n\n // Re-upload the re-encrypted entries\n if (reEncryptedEntries.length) {\n await this.#batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries(\n path,\n reEncryptedEntries,\n );\n }\n\n return decryptedData;\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #deleteUserStorage(\n path: UserStoragePathWithFeatureAndKey,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedPath = createEntryPath(path, storageKey);\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n method: 'DELETE',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(\n `feature/key set not found for path '${path}'.`,\n );\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to delete user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #deleteUserStorageAllFeatureEntries(\n path: UserStoragePathWithFeatureOnly,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'DELETE',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(`feature not found for path '${path}'.`);\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to delete user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #batchDeleteUserStorage(\n path: UserStoragePathWithFeatureOnly,\n data: string[],\n ): Promise<void> {\n try {\n if (!data.length) {\n return;\n }\n\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const encryptedData = await Promise.all(\n data.map(async (d) => this.#createEntryKey(d, storageKey)),\n );\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n\n body: JSON.stringify({ batch_delete: encryptedData }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to batch delete user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n #createEntryKey(key: string, storageKey: string): string {\n const hashedKey = createSHA256Hash(key + storageKey);\n return hashedKey;\n }\n\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n\n async #getAuthorizationHeader(): Promise<{ Authorization: string }> {\n const accessToken = await this.config.auth.getAccessToken();\n return { Authorization: `Bearer ${accessToken}` };\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"user-storage.mjs","sourceRoot":"","sources":["../../src/sdk/user-storage.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,qBAAiB;AAC3D,OAAO,UAAU,EAAE,EAAE,gBAAgB,EAAE,uCAA6B;AACpE,OAAO,EAAE,WAAW,EAAE,2CAAuC;AAE7D,OAAO,EAAE,UAAU,EAAE,0BAAsB;AAO3C,OAAO,EAAE,eAAe,EAAE,qCAAiC;AAE3D,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,GAAQ,EAAE,aAAqB,EAAE,EAAE,CAC7D,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,iBAAiB,uBAAuB,aAAa,EAAE,CAAC;AA2B7E,MAAM,OAAO,WAAW;IAOtB,YAAY,MAAyB,EAAE,OAA2B;;QAChE,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;QACtB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,OAAO,CACX,IAA6C,EAC7C,KAAa;QAEb,MAAM,uBAAA,IAAI,8DAAmB,MAAvB,IAAI,EAAoB,IAAI,EAAE,KAAK,CAAC,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,aAAa,CACjB,IAAmC,EACnC,MAAgD;QAEhD,MAAM,uBAAA,IAAI,mEAAwB,MAA5B,IAAI,EAAyB,IAAI,EAAE,MAAM,CAAC,CAAC;IACnD,CAAC;IAED,KAAK,CAAC,OAAO,CACX,IAA6C;QAE7C,OAAO,uBAAA,IAAI,2DAAgB,MAApB,IAAI,EAAiB,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,KAAK,CAAC,kBAAkB,CACtB,IAA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AE,SAAS;gBAClB,KAAK,EAAE,SAAS;aACjB,CAAC,CAAC,CAAC;YACJ,MAAM,IAAI,KAAK,CACb,uBAAuB,YAAY,CAAC,OAAO,YAAY,YAAY,CAAC,KAAK,EAAE,CAC5E,CAAC;SACH;KACF;IAAC,OAAO,CAAC,EAAE;QACV,0BAA0B;QAC1B,MAAM,YAAY,GAChB,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QAC3D,MAAM,IAAI,gBAAgB,CACxB,iDAAiD,IAAI,MAAM,YAAY,EAAE,CAC1E,CAAC;KACH;AACH,CAAC,qEAEe,GAAW,EAAE,UAAkB;IAC7C,MAAM,SAAS,GAAG,gBAAgB,CAAC,GAAG,GAAG,UAAU,CAAC,CAAC;IACrD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,gFAAgF;AAEhF,KAAK;IACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;IAC5D,OAAO,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE,CAAC;AACpD,CAAC","sourcesContent":["import type { IBaseAuth } from './authentication-jwt-bearer/types';\nimport { NotFoundError, UserStorageError } from './errors';\nimport encryption, { createSHA256Hash } from '../shared/encryption';\nimport { SHARED_SALT } from '../shared/encryption/constants';\nimport type { Env } from '../shared/env';\nimport { getEnvUrls } from '../shared/env';\nimport type {\n UserStorageGenericFeatureKey,\n UserStorageGenericFeatureName,\n UserStorageGenericPathWithFeatureAndKey,\n UserStorageGenericPathWithFeatureOnly,\n} from '../shared/storage-schema';\nimport { createEntryPath } from '../shared/storage-schema';\n\nexport const STORAGE_URL = (env: Env, encryptedPath: string) =>\n `${getEnvUrls(env).userStorageApiUrl}/api/v1/userstorage/${encryptedPath}`;\n\nexport type UserStorageConfig = {\n env: Env;\n auth: Pick<IBaseAuth, 'getAccessToken' | 'getUserProfile' | 'signMessage'>;\n};\n\nexport type StorageOptions = {\n getStorageKey: () => Promise<string | null>;\n setStorageKey: (val: string) => Promise<void>;\n};\n\nexport type UserStorageOptions = {\n storage?: StorageOptions;\n};\n\nexport type GetUserStorageAllFeatureEntriesResponse = {\n HashedKey: string;\n\n Data: string;\n}[];\n\ntype ErrorMessage = {\n message: string;\n error: string;\n};\n\nexport class UserStorage {\n protected config: UserStorageConfig;\n\n protected options: UserStorageOptions;\n\n protected env: Env;\n\n constructor(config: UserStorageConfig, options: UserStorageOptions) {\n this.env = config.env;\n this.config = config;\n this.options = options;\n }\n\n async setItem(\n path: UserStorageGenericPathWithFeatureAndKey,\n value: string,\n ): Promise<void> {\n await this.#upsertUserStorage(path, value);\n }\n\n async batchSetItems(\n path: UserStorageGenericFeatureName,\n values: [UserStorageGenericFeatureKey, string][],\n ) {\n await this.#batchUpsertUserStorage(path, values);\n }\n\n async getItem(\n path: UserStorageGenericPathWithFeatureAndKey,\n ): Promise<string> {\n return this.#getUserStorage(path);\n }\n\n async getAllFeatureItems(\n path: UserStorageGenericFeatureName,\n ): Promise<string[] | null> {\n return this.#getUserStorageAllFeatureEntries(path);\n }\n\n async deleteItem(\n path: UserStorageGenericPathWithFeatureAndKey,\n ): Promise<void> {\n return this.#deleteUserStorage(path);\n }\n\n async deleteAllFeatureItems(\n path: UserStorageGenericFeatureName,\n ): Promise<void> {\n return this.#deleteUserStorageAllFeatureEntries(path);\n }\n\n async batchDeleteItems(\n path: UserStorageGenericFeatureName,\n values: UserStorageGenericFeatureKey[],\n ) {\n return this.#batchDeleteUserStorage(path, values);\n }\n\n async getStorageKey(): Promise<string> {\n const storageKey = await this.options.storage?.getStorageKey();\n if (storageKey) {\n return storageKey;\n }\n\n const userProfile = await this.config.auth.getUserProfile();\n const storageKeySignature = await this.config.auth.signMessage(\n `metamask:${userProfile.profileId}`,\n );\n const hashedStorageKeySignature = createSHA256Hash(storageKeySignature);\n await this.options.storage?.setStorageKey(hashedStorageKeySignature);\n return hashedStorageKeySignature;\n }\n\n async #upsertUserStorage(\n path: UserStorageGenericPathWithFeatureAndKey,\n data: string,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedData = await encryption.encryptString(data, storageKey);\n const encryptedPath = createEntryPath(path, storageKey, {\n validateAgainstSchema: false,\n });\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: encryptedData }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #batchUpsertUserStorage(\n path: UserStorageGenericPathWithFeatureOnly,\n data: [string, string][],\n ): Promise<void> {\n try {\n if (!data.length) {\n return;\n }\n\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const encryptedData = await Promise.all(\n data.map(async (d) => {\n return [\n this.#createEntryKey(d[0], storageKey),\n await encryption.encryptString(d[1], storageKey),\n ];\n }),\n );\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: Object.fromEntries(encryptedData) }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to batch upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries(\n path: UserStorageGenericPathWithFeatureOnly,\n encryptedData: [string, string][],\n ): Promise<void> {\n try {\n if (!encryptedData.length) {\n return;\n }\n\n const headers = await this.#getAuthorizationHeader();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({ data: Object.fromEntries(encryptedData) }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to batch upsert user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorage(\n path: UserStorageGenericPathWithFeatureAndKey,\n ): Promise<string> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedPath = createEntryPath(path, storageKey, {\n validateAgainstSchema: false,\n });\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(\n `feature/key set not found for path '${path}'.`,\n );\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const { Data: encryptedData } = await response.json();\n const decryptedData = await encryption.decryptString(\n encryptedData,\n storageKey,\n );\n\n // Re-encrypt the entry if it was encrypted with a random salt\n const salt = encryption.getSalt(encryptedData);\n if (salt.toString() !== SHARED_SALT.toString()) {\n await this.#upsertUserStorage(path, decryptedData);\n }\n\n return decryptedData;\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #getUserStorageAllFeatureEntries(\n path: UserStorageGenericPathWithFeatureOnly,\n ): Promise<string[] | null> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(`feature not found for path '${path}'.`);\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n\n const userStorage: GetUserStorageAllFeatureEntriesResponse | null =\n await response.json();\n\n if (!Array.isArray(userStorage)) {\n return null;\n }\n\n const decryptedData: string[] = [];\n const reEncryptedEntries: [string, string][] = [];\n\n for (const entry of userStorage) {\n if (!entry.Data) {\n continue;\n }\n\n try {\n const data = await encryption.decryptString(entry.Data, storageKey);\n decryptedData.push(data);\n\n // Re-encrypt the entry was encrypted with a random salt\n const salt = encryption.getSalt(entry.Data);\n if (salt.toString() !== SHARED_SALT.toString()) {\n reEncryptedEntries.push([\n entry.HashedKey,\n await encryption.encryptString(data, storageKey),\n ]);\n }\n } catch {\n // do nothing\n }\n }\n\n // Re-upload the re-encrypted entries\n if (reEncryptedEntries.length) {\n await this.#batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries(\n path,\n reEncryptedEntries,\n );\n }\n\n return decryptedData;\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to get user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #deleteUserStorage(\n path: UserStorageGenericPathWithFeatureAndKey,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n const encryptedPath = createEntryPath(path, storageKey, {\n validateAgainstSchema: false,\n });\n\n const url = new URL(STORAGE_URL(this.env, encryptedPath));\n\n const response = await fetch(url.toString(), {\n method: 'DELETE',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(\n `feature/key set not found for path '${path}'.`,\n );\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to delete user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #deleteUserStorageAllFeatureEntries(\n path: UserStorageGenericPathWithFeatureOnly,\n ): Promise<void> {\n try {\n const headers = await this.#getAuthorizationHeader();\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'DELETE',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n });\n\n if (response.status === 404) {\n throw new NotFoundError(`feature not found for path '${path}'.`);\n }\n\n if (!response.ok) {\n const responseBody = (await response.json()) as ErrorMessage;\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n if (e instanceof NotFoundError) {\n throw e;\n }\n\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n\n throw new UserStorageError(\n `failed to delete user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n async #batchDeleteUserStorage(\n path: UserStorageGenericPathWithFeatureOnly,\n data: string[],\n ): Promise<void> {\n try {\n if (!data.length) {\n return;\n }\n\n const headers = await this.#getAuthorizationHeader();\n const storageKey = await this.getStorageKey();\n\n const encryptedData = await Promise.all(\n data.map(async (d) => this.#createEntryKey(d, storageKey)),\n );\n\n const url = new URL(STORAGE_URL(this.env, path));\n\n const response = await fetch(url.toString(), {\n method: 'PUT',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n\n body: JSON.stringify({ batch_delete: encryptedData }),\n });\n\n if (!response.ok) {\n const responseBody: ErrorMessage = await response.json().catch(() => ({\n message: 'unknown',\n error: 'unknown',\n }));\n throw new Error(\n `HTTP error message: ${responseBody.message}, error: ${responseBody.error}`,\n );\n }\n } catch (e) {\n /* istanbul ignore next */\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? '');\n throw new UserStorageError(\n `failed to batch delete user storage for path '${path}'. ${errorMessage}`,\n );\n }\n }\n\n #createEntryKey(key: string, storageKey: string): string {\n const hashedKey = createSHA256Hash(key + storageKey);\n return hashedKey;\n }\n\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n\n async #getAuthorizationHeader(): Promise<{ Authorization: string }> {\n const accessToken = await this.config.auth.getAccessToken();\n return { Authorization: `Bearer ${accessToken}` };\n }\n}\n"]}
|
|
@@ -20,20 +20,24 @@ exports.USER_STORAGE_SCHEMA = {
|
|
|
20
20
|
[exports.USER_STORAGE_FEATURE_NAMES.accounts]: [ALLOW_ARBITRARY_KEYS],
|
|
21
21
|
[exports.USER_STORAGE_FEATURE_NAMES.networks]: [ALLOW_ARBITRARY_KEYS], // keyed by chains/networks
|
|
22
22
|
};
|
|
23
|
-
const getFeatureAndKeyFromPath = (path) => {
|
|
23
|
+
const getFeatureAndKeyFromPath = (path, options = { validateAgainstSchema: true }) => {
|
|
24
24
|
const pathRegex = /^\w+\.\w+$/u;
|
|
25
25
|
if (!pathRegex.test(path)) {
|
|
26
26
|
throw new Error(`user-storage - path is not in the correct format. Correct format: 'feature.key'`);
|
|
27
27
|
}
|
|
28
28
|
const [feature, key] = path.split('.');
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
29
|
+
if (options.validateAgainstSchema) {
|
|
30
|
+
const featureToValidate = feature;
|
|
31
|
+
const keyToValidate = key;
|
|
32
|
+
if (!(featureToValidate in exports.USER_STORAGE_SCHEMA)) {
|
|
33
|
+
throw new Error(`user-storage - invalid feature provided: ${featureToValidate}. Valid features: ${Object.keys(exports.USER_STORAGE_SCHEMA).join(', ')}`);
|
|
34
|
+
}
|
|
35
|
+
const validFeature = exports.USER_STORAGE_SCHEMA[featureToValidate];
|
|
36
|
+
if (!validFeature.includes(keyToValidate) &&
|
|
37
|
+
!validFeature.includes(ALLOW_ARBITRARY_KEYS)) {
|
|
38
|
+
const validKeys = exports.USER_STORAGE_SCHEMA[featureToValidate].join(', ');
|
|
39
|
+
throw new Error(`user-storage - invalid key provided for this feature: ${keyToValidate}. Valid keys: ${validKeys}`);
|
|
40
|
+
}
|
|
37
41
|
}
|
|
38
42
|
return { feature, key };
|
|
39
43
|
};
|
|
@@ -50,10 +54,13 @@ exports.isPathWithFeatureAndKey = isPathWithFeatureAndKey;
|
|
|
50
54
|
*
|
|
51
55
|
* @param path - string in the form of `${feature}.${key}` that matches schema
|
|
52
56
|
* @param storageKey - users storage key
|
|
57
|
+
* @param options - options object
|
|
58
|
+
* @param options.validateAgainstSchema - whether to validate the path against the schema.
|
|
59
|
+
* This defaults to true, and should only be set to false when using the SDK with arbitrary feature names and keys.
|
|
53
60
|
* @returns path to store entry
|
|
54
61
|
*/
|
|
55
|
-
function createEntryPath(path, storageKey) {
|
|
56
|
-
const { feature, key } = (0, exports.getFeatureAndKeyFromPath)(path);
|
|
62
|
+
function createEntryPath(path, storageKey, options = { validateAgainstSchema: true }) {
|
|
63
|
+
const { feature, key } = (0, exports.getFeatureAndKeyFromPath)(path, options);
|
|
57
64
|
const hashedKey = (0, encryption_1.createSHA256Hash)(key + storageKey);
|
|
58
65
|
return `${feature}/${hashedKey}`;
|
|
59
66
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage-schema.cjs","sourceRoot":"","sources":["../../src/shared/storage-schema.ts"],"names":[],"mappings":";;;AAAA,uDAAgD;AAEhD;;;;;;GAMG;AACH,MAAM,oBAAoB,GAAG,sBAA+B,CAAC;AAEhD,QAAA,0BAA0B,GAAG;IACxC,aAAa,EAAE,eAAe;IAC9B,QAAQ,EAAE,aAAa;IACvB,QAAQ,EAAE,UAAU;CACZ,CAAC;AAKE,QAAA,mBAAmB,GAAG;IACjC,CAAC,kCAA0B,CAAC,aAAa,CAAC,EAAE,CAAC,uBAAuB,CAAC;IACrE,CAAC,kCAA0B,CAAC,QAAQ,CAAC,EAAE,CAAC,oBAAoB,CAAC;IAC7D,CAAC,kCAA0B,CAAC,QAAQ,CAAC,EAAE,CAAC,oBAAoB,CAAC,EAAE,2BAA2B;CAClF,CAAC;
|
|
1
|
+
{"version":3,"file":"storage-schema.cjs","sourceRoot":"","sources":["../../src/shared/storage-schema.ts"],"names":[],"mappings":";;;AAAA,uDAAgD;AAEhD;;;;;;GAMG;AACH,MAAM,oBAAoB,GAAG,sBAA+B,CAAC;AAEhD,QAAA,0BAA0B,GAAG;IACxC,aAAa,EAAE,eAAe;IAC9B,QAAQ,EAAE,aAAa;IACvB,QAAQ,EAAE,UAAU;CACZ,CAAC;AAKE,QAAA,mBAAmB,GAAG;IACjC,CAAC,kCAA0B,CAAC,aAAa,CAAC,EAAE,CAAC,uBAAuB,CAAC;IACrE,CAAC,kCAA0B,CAAC,QAAQ,CAAC,EAAE,CAAC,oBAAoB,CAAC;IAC7D,CAAC,kCAA0B,CAAC,QAAQ,CAAC,EAAE,CAAC,oBAAoB,CAAC,EAAE,2BAA2B;CAClF,CAAC;AAuCJ,M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CAAC;AAC9B,CAAC,CAAC;AANW,QAAA,uBAAuB,2BAMlC;AAEF;;;;;;;;;;;GAWG;AACH,SAAgB,eAAe,CAC7B,IAE2C,EAC3C,UAAkB,EAClB,UAEI,EAAE,qBAAqB,EAAE,IAAS,EAAE;IAExC,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,IAAA,gCAAwB,EAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACjE,MAAM,SAAS,GAAG,IAAA,6BAAgB,EAAC,GAAG,GAAG,UAAU,CAAC,CAAC;IAErD,OAAO,GAAG,OAAO,IAAI,SAAS,EAAE,CAAC;AACnC,CAAC;AAbD,0CAaC","sourcesContent":["import { createSHA256Hash } from './encryption';\n\n/**\n * The User Storage Endpoint requires a feature name and a namespace key.\n * Developers can provide additional features and keys by extending these types below.\n *\n * Adding ALLOW_ARBITRARY_KEYS as the first key in the array allows for any key to be used for this feature.\n * This can be useful for features where keys are not deterministic (eg. accounts addresses).\n */\nconst ALLOW_ARBITRARY_KEYS = 'ALLOW_ARBITRARY_KEYS' as const;\n\nexport const USER_STORAGE_FEATURE_NAMES = {\n notifications: 'notifications',\n accounts: 'accounts_v2',\n networks: 'networks',\n} as const;\n\nexport type UserStorageFeatureNames =\n (typeof USER_STORAGE_FEATURE_NAMES)[keyof typeof USER_STORAGE_FEATURE_NAMES];\n\nexport const USER_STORAGE_SCHEMA = {\n [USER_STORAGE_FEATURE_NAMES.notifications]: ['notification_settings'],\n [USER_STORAGE_FEATURE_NAMES.accounts]: [ALLOW_ARBITRARY_KEYS], // keyed by account addresses\n [USER_STORAGE_FEATURE_NAMES.networks]: [ALLOW_ARBITRARY_KEYS], // keyed by chains/networks\n} as const;\n\ntype UserStorageSchema = typeof USER_STORAGE_SCHEMA;\n\nexport type UserStorageFeatureKeys<Feature extends UserStorageFeatureNames> =\n UserStorageSchema[Feature][0] extends typeof ALLOW_ARBITRARY_KEYS\n ? string\n : UserStorageSchema[Feature][number];\n\ntype UserStorageFeatureAndKey = {\n feature: UserStorageFeatureNames;\n key: UserStorageFeatureKeys<UserStorageFeatureNames>;\n};\n\nexport type UserStoragePathWithFeatureOnly = UserStorageFeatureNames;\nexport type UserStoragePathWithFeatureAndKey = {\n [K in UserStorageFeatureNames]: `${K}.${UserStorageFeatureKeys<K>}`;\n}[UserStoragePathWithFeatureOnly];\n\n/**\n * The below types are mainly used for the SDK.\n * These exist so that the SDK can be used with arbitrary feature names and keys.\n *\n * We only type enforce feature names and keys when using UserStorageController.\n * This is done so we don't end up with magic strings within the applications.\n */\n\nexport type UserStorageGenericFeatureName = string;\nexport type UserStorageGenericFeatureKey = string;\nexport type UserStorageGenericPathWithFeatureAndKey =\n `${UserStorageGenericFeatureName}.${UserStorageGenericFeatureKey}`;\nexport type UserStorageGenericPathWithFeatureOnly =\n UserStorageGenericFeatureName;\n\ntype UserStorageGenericFeatureAndKey = {\n feature: UserStorageGenericFeatureName;\n key: UserStorageGenericFeatureKey;\n};\n\nexport const getFeatureAndKeyFromPath = <T extends boolean>(\n path: T extends true\n ? UserStoragePathWithFeatureAndKey\n : UserStorageGenericPathWithFeatureAndKey,\n options: {\n validateAgainstSchema: T;\n } = { validateAgainstSchema: true as T },\n): T extends true\n ? UserStorageFeatureAndKey\n : UserStorageGenericFeatureAndKey => {\n const pathRegex = /^\\w+\\.\\w+$/u;\n\n if (!pathRegex.test(path)) {\n throw new Error(\n `user-storage - path is not in the correct format. Correct format: 'feature.key'`,\n );\n }\n\n const [feature, key] = path.split('.');\n\n if (options.validateAgainstSchema) {\n const featureToValidate = feature as UserStorageFeatureNames;\n const keyToValidate = key as UserStorageFeatureKeys<\n typeof featureToValidate\n >;\n\n if (!(featureToValidate in USER_STORAGE_SCHEMA)) {\n throw new Error(\n `user-storage - invalid feature provided: ${featureToValidate}. Valid features: ${Object.keys(\n USER_STORAGE_SCHEMA,\n ).join(', ')}`,\n );\n }\n\n const validFeature = USER_STORAGE_SCHEMA[\n featureToValidate\n ] as readonly string[];\n\n if (\n !validFeature.includes(keyToValidate) &&\n !validFeature.includes(ALLOW_ARBITRARY_KEYS)\n ) {\n const validKeys = USER_STORAGE_SCHEMA[featureToValidate].join(', ');\n\n throw new Error(\n `user-storage - invalid key provided for this feature: ${keyToValidate}. Valid keys: ${validKeys}`,\n );\n }\n }\n\n return { feature, key } as T extends true\n ? UserStorageFeatureAndKey\n : UserStorageGenericFeatureAndKey;\n};\n\nexport const isPathWithFeatureAndKey = (\n path: string,\n): path is UserStoragePathWithFeatureAndKey => {\n const pathRegex = /^\\w+\\.\\w+$/u;\n\n return pathRegex.test(path);\n};\n\n/**\n * Constructs a unique entry path for a user.\n * This can be done due to the uniqueness of the storage key (no users will share the same storage key).\n * The users entry is a unique hash that cannot be reversed.\n *\n * @param path - string in the form of `${feature}.${key}` that matches schema\n * @param storageKey - users storage key\n * @param options - options object\n * @param options.validateAgainstSchema - whether to validate the path against the schema.\n * This defaults to true, and should only be set to false when using the SDK with arbitrary feature names and keys.\n * @returns path to store entry\n */\nexport function createEntryPath<T extends boolean>(\n path: T extends true\n ? UserStoragePathWithFeatureAndKey\n : UserStorageGenericPathWithFeatureAndKey,\n storageKey: string,\n options: {\n validateAgainstSchema: T;\n } = { validateAgainstSchema: true as T },\n): string {\n const { feature, key } = getFeatureAndKeyFromPath(path, options);\n const hashedKey = createSHA256Hash(key + storageKey);\n\n return `${feature}/${hashedKey}`;\n}\n"]}
|
|
@@ -27,7 +27,24 @@ export type UserStoragePathWithFeatureOnly = UserStorageFeatureNames;
|
|
|
27
27
|
export type UserStoragePathWithFeatureAndKey = {
|
|
28
28
|
[K in UserStorageFeatureNames]: `${K}.${UserStorageFeatureKeys<K>}`;
|
|
29
29
|
}[UserStoragePathWithFeatureOnly];
|
|
30
|
-
|
|
30
|
+
/**
|
|
31
|
+
* The below types are mainly used for the SDK.
|
|
32
|
+
* These exist so that the SDK can be used with arbitrary feature names and keys.
|
|
33
|
+
*
|
|
34
|
+
* We only type enforce feature names and keys when using UserStorageController.
|
|
35
|
+
* This is done so we don't end up with magic strings within the applications.
|
|
36
|
+
*/
|
|
37
|
+
export type UserStorageGenericFeatureName = string;
|
|
38
|
+
export type UserStorageGenericFeatureKey = string;
|
|
39
|
+
export type UserStorageGenericPathWithFeatureAndKey = `${UserStorageGenericFeatureName}.${UserStorageGenericFeatureKey}`;
|
|
40
|
+
export type UserStorageGenericPathWithFeatureOnly = UserStorageGenericFeatureName;
|
|
41
|
+
type UserStorageGenericFeatureAndKey = {
|
|
42
|
+
feature: UserStorageGenericFeatureName;
|
|
43
|
+
key: UserStorageGenericFeatureKey;
|
|
44
|
+
};
|
|
45
|
+
export declare const getFeatureAndKeyFromPath: <T extends boolean>(path: T extends true ? UserStoragePathWithFeatureAndKey : `${string}.${string}`, options?: {
|
|
46
|
+
validateAgainstSchema: T;
|
|
47
|
+
}) => T extends true ? UserStorageFeatureAndKey : UserStorageGenericFeatureAndKey;
|
|
31
48
|
export declare const isPathWithFeatureAndKey: (path: string) => path is UserStoragePathWithFeatureAndKey;
|
|
32
49
|
/**
|
|
33
50
|
* Constructs a unique entry path for a user.
|
|
@@ -36,8 +53,13 @@ export declare const isPathWithFeatureAndKey: (path: string) => path is UserStor
|
|
|
36
53
|
*
|
|
37
54
|
* @param path - string in the form of `${feature}.${key}` that matches schema
|
|
38
55
|
* @param storageKey - users storage key
|
|
56
|
+
* @param options - options object
|
|
57
|
+
* @param options.validateAgainstSchema - whether to validate the path against the schema.
|
|
58
|
+
* This defaults to true, and should only be set to false when using the SDK with arbitrary feature names and keys.
|
|
39
59
|
* @returns path to store entry
|
|
40
60
|
*/
|
|
41
|
-
export declare function createEntryPath(path: UserStoragePathWithFeatureAndKey, storageKey: string
|
|
61
|
+
export declare function createEntryPath<T extends boolean>(path: T extends true ? UserStoragePathWithFeatureAndKey : UserStorageGenericPathWithFeatureAndKey, storageKey: string, options?: {
|
|
62
|
+
validateAgainstSchema: T;
|
|
63
|
+
}): string;
|
|
42
64
|
export {};
|
|
43
65
|
//# sourceMappingURL=storage-schema.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage-schema.d.cts","sourceRoot":"","sources":["../../src/shared/storage-schema.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,QAAA,MAAM,oBAAoB,wBAAkC,CAAC;AAE7D,eAAO,MAAM,0BAA0B;;;;CAI7B,CAAC;AAEX,MAAM,MAAM,uBAAuB,GACjC,CAAC,OAAO,0BAA0B,CAAC,CAAC,MAAM,OAAO,0BAA0B,CAAC,CAAC;AAE/E,eAAO,MAAM,mBAAmB;;;;CAItB,CAAC;AAEX,KAAK,iBAAiB,GAAG,OAAO,mBAAmB,CAAC;AAEpD,MAAM,MAAM,sBAAsB,CAAC,OAAO,SAAS,uBAAuB,IACxE,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,OAAO,oBAAoB,GAC7D,MAAM,GACN,iBAAiB,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC;AAEzC,KAAK,wBAAwB,GAAG;IAC9B,OAAO,EAAE,uBAAuB,CAAC;IACjC,GAAG,EAAE,sBAAsB,CAAC,uBAAuB,CAAC,CAAC;CACtD,CAAC;AAEF,MAAM,MAAM,8BAA8B,GAAG,uBAAuB,CAAC;AACrE,MAAM,MAAM,gCAAgC,GAAG;KAC5C,CAAC,IAAI,uBAAuB,GAAG,GAAG,CAAC,IAAI,sBAAsB,CAAC,CAAC,CAAC,EAAE;CACpE,CAAC,8BAA8B,CAAC,CAAC;AAElC,
|
|
1
|
+
{"version":3,"file":"storage-schema.d.cts","sourceRoot":"","sources":["../../src/shared/storage-schema.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,QAAA,MAAM,oBAAoB,wBAAkC,CAAC;AAE7D,eAAO,MAAM,0BAA0B;;;;CAI7B,CAAC;AAEX,MAAM,MAAM,uBAAuB,GACjC,CAAC,OAAO,0BAA0B,CAAC,CAAC,MAAM,OAAO,0BAA0B,CAAC,CAAC;AAE/E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CAAC;CACa,GACvC,MAAM,CAKR"}
|
|
@@ -27,7 +27,24 @@ export type UserStoragePathWithFeatureOnly = UserStorageFeatureNames;
|
|
|
27
27
|
export type UserStoragePathWithFeatureAndKey = {
|
|
28
28
|
[K in UserStorageFeatureNames]: `${K}.${UserStorageFeatureKeys<K>}`;
|
|
29
29
|
}[UserStoragePathWithFeatureOnly];
|
|
30
|
-
|
|
30
|
+
/**
|
|
31
|
+
* The below types are mainly used for the SDK.
|
|
32
|
+
* These exist so that the SDK can be used with arbitrary feature names and keys.
|
|
33
|
+
*
|
|
34
|
+
* We only type enforce feature names and keys when using UserStorageController.
|
|
35
|
+
* This is done so we don't end up with magic strings within the applications.
|
|
36
|
+
*/
|
|
37
|
+
export type UserStorageGenericFeatureName = string;
|
|
38
|
+
export type UserStorageGenericFeatureKey = string;
|
|
39
|
+
export type UserStorageGenericPathWithFeatureAndKey = `${UserStorageGenericFeatureName}.${UserStorageGenericFeatureKey}`;
|
|
40
|
+
export type UserStorageGenericPathWithFeatureOnly = UserStorageGenericFeatureName;
|
|
41
|
+
type UserStorageGenericFeatureAndKey = {
|
|
42
|
+
feature: UserStorageGenericFeatureName;
|
|
43
|
+
key: UserStorageGenericFeatureKey;
|
|
44
|
+
};
|
|
45
|
+
export declare const getFeatureAndKeyFromPath: <T extends boolean>(path: T extends true ? UserStoragePathWithFeatureAndKey : `${string}.${string}`, options?: {
|
|
46
|
+
validateAgainstSchema: T;
|
|
47
|
+
}) => T extends true ? UserStorageFeatureAndKey : UserStorageGenericFeatureAndKey;
|
|
31
48
|
export declare const isPathWithFeatureAndKey: (path: string) => path is UserStoragePathWithFeatureAndKey;
|
|
32
49
|
/**
|
|
33
50
|
* Constructs a unique entry path for a user.
|
|
@@ -36,8 +53,13 @@ export declare const isPathWithFeatureAndKey: (path: string) => path is UserStor
|
|
|
36
53
|
*
|
|
37
54
|
* @param path - string in the form of `${feature}.${key}` that matches schema
|
|
38
55
|
* @param storageKey - users storage key
|
|
56
|
+
* @param options - options object
|
|
57
|
+
* @param options.validateAgainstSchema - whether to validate the path against the schema.
|
|
58
|
+
* This defaults to true, and should only be set to false when using the SDK with arbitrary feature names and keys.
|
|
39
59
|
* @returns path to store entry
|
|
40
60
|
*/
|
|
41
|
-
export declare function createEntryPath(path: UserStoragePathWithFeatureAndKey, storageKey: string
|
|
61
|
+
export declare function createEntryPath<T extends boolean>(path: T extends true ? UserStoragePathWithFeatureAndKey : UserStorageGenericPathWithFeatureAndKey, storageKey: string, options?: {
|
|
62
|
+
validateAgainstSchema: T;
|
|
63
|
+
}): string;
|
|
42
64
|
export {};
|
|
43
65
|
//# sourceMappingURL=storage-schema.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage-schema.d.mts","sourceRoot":"","sources":["../../src/shared/storage-schema.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,QAAA,MAAM,oBAAoB,wBAAkC,CAAC;AAE7D,eAAO,MAAM,0BAA0B;;;;CAI7B,CAAC;AAEX,MAAM,MAAM,uBAAuB,GACjC,CAAC,OAAO,0BAA0B,CAAC,CAAC,MAAM,OAAO,0BAA0B,CAAC,CAAC;AAE/E,eAAO,MAAM,mBAAmB;;;;CAItB,CAAC;AAEX,KAAK,iBAAiB,GAAG,OAAO,mBAAmB,CAAC;AAEpD,MAAM,MAAM,sBAAsB,CAAC,OAAO,SAAS,uBAAuB,IACxE,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,OAAO,oBAAoB,GAC7D,MAAM,GACN,iBAAiB,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC;AAEzC,KAAK,wBAAwB,GAAG;IAC9B,OAAO,EAAE,uBAAuB,CAAC;IACjC,GAAG,EAAE,sBAAsB,CAAC,uBAAuB,CAAC,CAAC;CACtD,CAAC;AAEF,MAAM,MAAM,8BAA8B,GAAG,uBAAuB,CAAC;AACrE,MAAM,MAAM,gCAAgC,GAAG;KAC5C,CAAC,IAAI,u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KAAK,+BAA+B,GAAG;IACrC,OAAO,EAAE,6BAA6B,CAAC;IACvC,GAAG,EAAE,4BAA4B,CAAC;CACnC,CAAC;AAEF,eAAO,MAAM,wBAAwB;;iFAqDpC,CAAC;AAEF,eAAO,MAAM,uBAAuB,SAC5B,MAAM,6CAKb,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,OAAO,EAC/C,IAAI,EAAE,CAAC,SAAS,IAAI,GAChB,gCAAgC,GAChC,uCAAuC,EAC3C,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE;IACP,qBAAqB,EAAE,CAAC,CAAC;CACa,GACvC,MAAM,CAKR"}
|
|
@@ -17,20 +17,24 @@ export const USER_STORAGE_SCHEMA = {
|
|
|
17
17
|
[USER_STORAGE_FEATURE_NAMES.accounts]: [ALLOW_ARBITRARY_KEYS],
|
|
18
18
|
[USER_STORAGE_FEATURE_NAMES.networks]: [ALLOW_ARBITRARY_KEYS], // keyed by chains/networks
|
|
19
19
|
};
|
|
20
|
-
export const getFeatureAndKeyFromPath = (path) => {
|
|
20
|
+
export const getFeatureAndKeyFromPath = (path, options = { validateAgainstSchema: true }) => {
|
|
21
21
|
const pathRegex = /^\w+\.\w+$/u;
|
|
22
22
|
if (!pathRegex.test(path)) {
|
|
23
23
|
throw new Error(`user-storage - path is not in the correct format. Correct format: 'feature.key'`);
|
|
24
24
|
}
|
|
25
25
|
const [feature, key] = path.split('.');
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
26
|
+
if (options.validateAgainstSchema) {
|
|
27
|
+
const featureToValidate = feature;
|
|
28
|
+
const keyToValidate = key;
|
|
29
|
+
if (!(featureToValidate in USER_STORAGE_SCHEMA)) {
|
|
30
|
+
throw new Error(`user-storage - invalid feature provided: ${featureToValidate}. Valid features: ${Object.keys(USER_STORAGE_SCHEMA).join(', ')}`);
|
|
31
|
+
}
|
|
32
|
+
const validFeature = USER_STORAGE_SCHEMA[featureToValidate];
|
|
33
|
+
if (!validFeature.includes(keyToValidate) &&
|
|
34
|
+
!validFeature.includes(ALLOW_ARBITRARY_KEYS)) {
|
|
35
|
+
const validKeys = USER_STORAGE_SCHEMA[featureToValidate].join(', ');
|
|
36
|
+
throw new Error(`user-storage - invalid key provided for this feature: ${keyToValidate}. Valid keys: ${validKeys}`);
|
|
37
|
+
}
|
|
34
38
|
}
|
|
35
39
|
return { feature, key };
|
|
36
40
|
};
|
|
@@ -45,10 +49,13 @@ export const isPathWithFeatureAndKey = (path) => {
|
|
|
45
49
|
*
|
|
46
50
|
* @param path - string in the form of `${feature}.${key}` that matches schema
|
|
47
51
|
* @param storageKey - users storage key
|
|
52
|
+
* @param options - options object
|
|
53
|
+
* @param options.validateAgainstSchema - whether to validate the path against the schema.
|
|
54
|
+
* This defaults to true, and should only be set to false when using the SDK with arbitrary feature names and keys.
|
|
48
55
|
* @returns path to store entry
|
|
49
56
|
*/
|
|
50
|
-
export function createEntryPath(path, storageKey) {
|
|
51
|
-
const { feature, key } = getFeatureAndKeyFromPath(path);
|
|
57
|
+
export function createEntryPath(path, storageKey, options = { validateAgainstSchema: true }) {
|
|
58
|
+
const { feature, key } = getFeatureAndKeyFromPath(path, options);
|
|
52
59
|
const hashedKey = createSHA256Hash(key + storageKey);
|
|
53
60
|
return `${feature}/${hashedKey}`;
|
|
54
61
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage-schema.mjs","sourceRoot":"","sources":["../../src/shared/storage-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,+BAAqB;AAEhD;;;;;;GAMG;AACH,MAAM,oBAAoB,GAAG,sBAA+B,CAAC;AAE7D,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,aAAa,EAAE,eAAe;IAC9B,QAAQ,EAAE,aAAa;IACvB,QAAQ,EAAE,UAAU;CACZ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SHA256Hash } from './encryption';\n\n/**\n * The User Storage Endpoint requires a feature name and a namespace key.\n * Developers can provide additional features and keys by extending these types below.\n *\n * Adding ALLOW_ARBITRARY_KEYS as the first key in the array allows for any key to be used for this feature.\n * This can be useful for features where keys are not deterministic (eg. accounts addresses).\n */\nconst ALLOW_ARBITRARY_KEYS = 'ALLOW_ARBITRARY_KEYS' as const;\n\nexport const USER_STORAGE_FEATURE_NAMES = {\n notifications: 'notifications',\n accounts: 'accounts_v2',\n networks: 'networks',\n} as const;\n\nexport type UserStorageFeatureNames =\n (typeof USER_STORAGE_FEATURE_NAMES)[keyof typeof USER_STORAGE_FEATURE_NAMES];\n\nexport const USER_STORAGE_SCHEMA = {\n [USER_STORAGE_FEATURE_NAMES.notifications]: ['notification_settings'],\n [USER_STORAGE_FEATURE_NAMES.accounts]: [ALLOW_ARBITRARY_KEYS], // keyed by account addresses\n [USER_STORAGE_FEATURE_NAMES.networks]: [ALLOW_ARBITRARY_KEYS], // keyed by chains/networks\n} as const;\n\ntype UserStorageSchema = typeof USER_STORAGE_SCHEMA;\n\nexport type UserStorageFeatureKeys<Feature extends UserStorageFeatureNames> =\n UserStorageSchema[Feature][0] extends typeof ALLOW_ARBITRARY_KEYS\n ? string\n : UserStorageSchema[Feature][number];\n\ntype UserStorageFeatureAndKey = {\n feature: UserStorageFeatureNames;\n key: UserStorageFeatureKeys<UserStorageFeatureNames>;\n};\n\nexport type UserStoragePathWithFeatureOnly = UserStorageFeatureNames;\nexport type UserStoragePathWithFeatureAndKey = {\n [K in UserStorageFeatureNames]: `${K}.${UserStorageFeatureKeys<K>}`;\n}[UserStoragePathWithFeatureOnly];\n\n/**\n * The below types are mainly used for the SDK.\n * These exist so that the SDK can be used with arbitrary feature names and keys.\n *\n * We only type enforce feature names and keys when using UserStorageController.\n * This is done so we don't end up with magic strings within the applications.\n */\n\nexport type UserStorageGenericFeatureName = string;\nexport type UserStorageGenericFeatureKey = string;\nexport type UserStorageGenericPathWithFeatureAndKey =\n `${UserStorageGenericFeatureName}.${UserStorageGenericFeatureKey}`;\nexport type UserStorageGenericPathWithFeatureOnly =\n UserStorageGenericFeatureName;\n\ntype UserStorageGenericFeatureAndKey = {\n feature: UserStorageGenericFeatureName;\n key: UserStorageGenericFeatureKey;\n};\n\nexport const getFeatureAndKeyFromPath = <T extends boolean>(\n path: T extends true\n ? UserStoragePathWithFeatureAndKey\n : UserStorageGenericPathWithFeatureAndKey,\n options: {\n validateAgainstSchema: T;\n } = { validateAgainstSchema: true as T },\n): T extends true\n ? UserStorageFeatureAndKey\n : UserStorageGenericFeatureAndKey => {\n const pathRegex = /^\\w+\\.\\w+$/u;\n\n if (!pathRegex.test(path)) {\n throw new Error(\n `user-storage - path is not in the correct format. Correct format: 'feature.key'`,\n );\n }\n\n const [feature, key] = path.split('.');\n\n if (options.validateAgainstSchema) {\n const featureToValidate = feature as UserStorageFeatureNames;\n const keyToValidate = key as UserStorageFeatureKeys<\n typeof featureToValidate\n >;\n\n if (!(featureToValidate in USER_STORAGE_SCHEMA)) {\n throw new Error(\n `user-storage - invalid feature provided: ${featureToValidate}. Valid features: ${Object.keys(\n USER_STORAGE_SCHEMA,\n ).join(', ')}`,\n );\n }\n\n const validFeature = USER_STORAGE_SCHEMA[\n featureToValidate\n ] as readonly string[];\n\n if (\n !validFeature.includes(keyToValidate) &&\n !validFeature.includes(ALLOW_ARBITRARY_KEYS)\n ) {\n const validKeys = USER_STORAGE_SCHEMA[featureToValidate].join(', ');\n\n throw new Error(\n `user-storage - invalid key provided for this feature: ${keyToValidate}. Valid keys: ${validKeys}`,\n );\n }\n }\n\n return { feature, key } as T extends true\n ? UserStorageFeatureAndKey\n : UserStorageGenericFeatureAndKey;\n};\n\nexport const isPathWithFeatureAndKey = (\n path: string,\n): path is UserStoragePathWithFeatureAndKey => {\n const pathRegex = /^\\w+\\.\\w+$/u;\n\n return pathRegex.test(path);\n};\n\n/**\n * Constructs a unique entry path for a user.\n * This can be done due to the uniqueness of the storage key (no users will share the same storage key).\n * The users entry is a unique hash that cannot be reversed.\n *\n * @param path - string in the form of `${feature}.${key}` that matches schema\n * @param storageKey - users storage key\n * @param options - options object\n * @param options.validateAgainstSchema - whether to validate the path against the schema.\n * This defaults to true, and should only be set to false when using the SDK with arbitrary feature names and keys.\n * @returns path to store entry\n */\nexport function createEntryPath<T extends boolean>(\n path: T extends true\n ? UserStoragePathWithFeatureAndKey\n : UserStorageGenericPathWithFeatureAndKey,\n storageKey: string,\n options: {\n validateAgainstSchema: T;\n } = { validateAgainstSchema: true as T },\n): string {\n const { feature, key } = getFeatureAndKeyFromPath(path, options);\n const hashedKey = createSHA256Hash(key + storageKey);\n\n return `${feature}/${hashedKey}`;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/profile-sync-controller",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"description": "The profile sync helps developers synchronize data across multiple clients and devices in a privacy-preserving way. All data saved in the user storage database is encrypted client-side to preserve privacy. The user storage provides a modular design, giving developers the flexibility to construct and manage their storage spaces in a way that best suits their needs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
"dependencies": {
|
|
103
103
|
"@metamask/base-controller": "^8.0.0",
|
|
104
104
|
"@metamask/keyring-api": "^17.0.0",
|
|
105
|
-
"@metamask/keyring-controller": "^19.0
|
|
105
|
+
"@metamask/keyring-controller": "^19.1.0",
|
|
106
106
|
"@metamask/network-controller": "^22.2.1",
|
|
107
107
|
"@metamask/snaps-sdk": "^6.17.1",
|
|
108
108
|
"@metamask/snaps-utils": "^8.10.0",
|
|
@@ -115,7 +115,7 @@
|
|
|
115
115
|
"devDependencies": {
|
|
116
116
|
"@lavamoat/allow-scripts": "^3.0.4",
|
|
117
117
|
"@lavamoat/preinstall-always-fail": "^2.1.0",
|
|
118
|
-
"@metamask/accounts-controller": "^
|
|
118
|
+
"@metamask/accounts-controller": "^24.0.0",
|
|
119
119
|
"@metamask/auto-changelog": "^3.4.4",
|
|
120
120
|
"@metamask/keyring-internal-api": "^4.0.1",
|
|
121
121
|
"@metamask/providers": "^18.1.1",
|
|
@@ -133,7 +133,7 @@
|
|
|
133
133
|
"webextension-polyfill": "^0.12.0"
|
|
134
134
|
},
|
|
135
135
|
"peerDependencies": {
|
|
136
|
-
"@metamask/accounts-controller": "^
|
|
136
|
+
"@metamask/accounts-controller": "^24.0.0",
|
|
137
137
|
"@metamask/keyring-controller": "^19.0.0",
|
|
138
138
|
"@metamask/network-controller": "^22.0.0",
|
|
139
139
|
"@metamask/providers": "^18.1.0",
|