@oxyhq/core 1.11.12 → 1.11.14
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/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/CrossDomainAuth.js +3 -1
- package/dist/cjs/HttpService.js +214 -33
- package/dist/cjs/OxyServices.base.js +9 -0
- package/dist/cjs/OxyServices.js +8 -3
- package/dist/cjs/crypto/index.js +3 -1
- package/dist/cjs/crypto/keyManager.js +476 -172
- package/dist/cjs/crypto/polyfill.js +14 -65
- package/dist/cjs/crypto/recoveryPhrase.js +30 -11
- package/dist/cjs/crypto/signatureService.js +25 -60
- package/dist/cjs/i18n/locales/en-US.json +46 -1
- package/dist/cjs/i18n/locales/es-ES.json +46 -1
- package/dist/cjs/i18n/locales/locales/en-US.json +46 -1
- package/dist/cjs/i18n/locales/locales/es-ES.json +46 -1
- package/dist/cjs/index.js +10 -2
- package/dist/cjs/mixins/OxyServices.assets.js +9 -4
- package/dist/cjs/mixins/OxyServices.auth.js +147 -14
- package/dist/cjs/mixins/OxyServices.contacts.js +50 -0
- package/dist/cjs/mixins/OxyServices.features.js +0 -11
- package/dist/cjs/mixins/OxyServices.fedcm.js +4 -3
- package/dist/cjs/mixins/OxyServices.language.js +5 -36
- package/dist/cjs/mixins/OxyServices.redirect.js +6 -2
- package/dist/cjs/mixins/OxyServices.security.js +13 -2
- package/dist/cjs/mixins/OxyServices.user.js +59 -38
- package/dist/cjs/mixins/OxyServices.utility.js +416 -110
- package/dist/cjs/mixins/index.js +11 -3
- package/dist/cjs/utils/accountUtils.js +71 -2
- package/dist/cjs/utils/deviceManager.js +5 -36
- package/dist/cjs/utils/languageUtils.js +22 -0
- package/dist/cjs/utils/platformCrypto.js +165 -0
- package/dist/cjs/utils/platformCrypto.native.js +123 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/CrossDomainAuth.js +3 -1
- package/dist/esm/HttpService.js +215 -34
- package/dist/esm/OxyServices.base.js +9 -0
- package/dist/esm/OxyServices.js +8 -3
- package/dist/esm/crypto/index.js +1 -1
- package/dist/esm/crypto/keyManager.js +473 -138
- package/dist/esm/crypto/polyfill.js +14 -32
- package/dist/esm/crypto/recoveryPhrase.js +30 -11
- package/dist/esm/crypto/signatureService.js +25 -27
- package/dist/esm/i18n/locales/en-US.json +46 -1
- package/dist/esm/i18n/locales/es-ES.json +46 -1
- package/dist/esm/i18n/locales/locales/en-US.json +46 -1
- package/dist/esm/i18n/locales/locales/es-ES.json +46 -1
- package/dist/esm/index.js +4 -3
- package/dist/esm/mixins/OxyServices.assets.js +9 -4
- package/dist/esm/mixins/OxyServices.auth.js +145 -14
- package/dist/esm/mixins/OxyServices.contacts.js +47 -0
- package/dist/esm/mixins/OxyServices.features.js +0 -11
- package/dist/esm/mixins/OxyServices.fedcm.js +4 -3
- package/dist/esm/mixins/OxyServices.language.js +5 -3
- package/dist/esm/mixins/OxyServices.redirect.js +6 -2
- package/dist/esm/mixins/OxyServices.security.js +13 -2
- package/dist/esm/mixins/OxyServices.user.js +59 -38
- package/dist/esm/mixins/OxyServices.utility.js +416 -77
- package/dist/esm/mixins/index.js +11 -3
- package/dist/esm/utils/accountUtils.js +67 -1
- package/dist/esm/utils/deviceManager.js +5 -3
- package/dist/esm/utils/languageUtils.js +21 -0
- package/dist/esm/utils/platformCrypto.js +125 -0
- package/dist/esm/utils/platformCrypto.native.js +80 -0
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/HttpService.d.ts +47 -3
- package/dist/types/OxyServices.base.d.ts +7 -0
- package/dist/types/OxyServices.d.ts +50 -7
- package/dist/types/crypto/index.d.ts +1 -1
- package/dist/types/crypto/keyManager.d.ts +110 -9
- package/dist/types/crypto/polyfill.d.ts +3 -1
- package/dist/types/crypto/recoveryPhrase.d.ts +31 -7
- package/dist/types/crypto/signatureService.d.ts +4 -0
- package/dist/types/index.d.ts +7 -5
- package/dist/types/mixins/OxyServices.analytics.d.ts +1 -0
- package/dist/types/mixins/OxyServices.assets.d.ts +6 -10
- package/dist/types/mixins/OxyServices.auth.d.ts +82 -5
- package/dist/types/mixins/OxyServices.contacts.d.ts +99 -0
- package/dist/types/mixins/OxyServices.developer.d.ts +1 -0
- package/dist/types/mixins/OxyServices.devices.d.ts +1 -0
- package/dist/types/mixins/OxyServices.features.d.ts +2 -7
- package/dist/types/mixins/OxyServices.fedcm.d.ts +1 -0
- package/dist/types/mixins/OxyServices.karma.d.ts +1 -0
- package/dist/types/mixins/OxyServices.language.d.ts +1 -0
- package/dist/types/mixins/OxyServices.location.d.ts +1 -0
- package/dist/types/mixins/OxyServices.managedAccounts.d.ts +1 -0
- package/dist/types/mixins/OxyServices.payment.d.ts +1 -0
- package/dist/types/mixins/OxyServices.popup.d.ts +1 -0
- package/dist/types/mixins/OxyServices.privacy.d.ts +1 -0
- package/dist/types/mixins/OxyServices.redirect.d.ts +1 -0
- package/dist/types/mixins/OxyServices.security.d.ts +1 -0
- package/dist/types/mixins/OxyServices.topics.d.ts +1 -0
- package/dist/types/mixins/OxyServices.user.d.ts +28 -11
- package/dist/types/mixins/OxyServices.utility.d.ts +145 -10
- package/dist/types/mixins/index.d.ts +52 -4
- package/dist/types/models/interfaces.d.ts +62 -3
- package/dist/types/utils/accountUtils.d.ts +41 -1
- package/dist/types/utils/languageUtils.d.ts +1 -0
- package/dist/types/utils/platformCrypto.d.ts +87 -0
- package/dist/types/utils/platformCrypto.native.d.ts +54 -0
- package/package.json +45 -2
- package/src/CrossDomainAuth.ts +12 -10
- package/src/HttpService.ts +251 -40
- package/src/OxyServices.base.ts +10 -0
- package/src/OxyServices.ts +26 -7
- package/src/crypto/__tests__/keyManager.test.ts +336 -0
- package/src/crypto/index.ts +6 -1
- package/src/crypto/keyManager.ts +529 -151
- package/src/crypto/polyfill.ts +14 -34
- package/src/crypto/recoveryPhrase.ts +56 -17
- package/src/crypto/signatureService.ts +25 -30
- package/src/i18n/locales/en-US.json +46 -1
- package/src/i18n/locales/es-ES.json +46 -1
- package/src/index.ts +19 -4
- package/src/mixins/OxyServices.assets.ts +15 -11
- package/src/mixins/OxyServices.auth.ts +175 -15
- package/src/mixins/OxyServices.contacts.ts +73 -0
- package/src/mixins/OxyServices.features.ts +2 -12
- package/src/mixins/OxyServices.fedcm.ts +4 -3
- package/src/mixins/OxyServices.language.ts +6 -4
- package/src/mixins/OxyServices.redirect.ts +6 -2
- package/src/mixins/OxyServices.security.ts +18 -8
- package/src/mixins/OxyServices.user.ts +72 -49
- package/src/mixins/OxyServices.utility.ts +562 -89
- package/src/mixins/__tests__/serviceAuth.test.ts +623 -0
- package/src/mixins/index.ts +58 -7
- package/src/models/interfaces.ts +65 -3
- package/src/utils/accountUtils.ts +82 -2
- package/src/utils/deviceManager.ts +7 -4
- package/src/utils/languageUtils.ts +23 -2
- package/src/utils/platformCrypto.native.ts +101 -0
- package/src/utils/platformCrypto.ts +145 -0
package/dist/esm/mixins/index.js
CHANGED
|
@@ -24,6 +24,7 @@ import { OxyServicesUtilityMixin } from './OxyServices.utility.js';
|
|
|
24
24
|
import { OxyServicesFeaturesMixin } from './OxyServices.features.js';
|
|
25
25
|
import { OxyServicesTopicsMixin } from './OxyServices.topics.js';
|
|
26
26
|
import { OxyServicesManagedAccountsMixin } from './OxyServices.managedAccounts.js';
|
|
27
|
+
import { OxyServicesContactsMixin } from './OxyServices.contacts.js';
|
|
27
28
|
/**
|
|
28
29
|
* Mixin pipeline - applied in order from first to last.
|
|
29
30
|
*
|
|
@@ -62,6 +63,7 @@ const MIXIN_PIPELINE = [
|
|
|
62
63
|
OxyServicesFeaturesMixin,
|
|
63
64
|
OxyServicesTopicsMixin,
|
|
64
65
|
OxyServicesManagedAccountsMixin,
|
|
66
|
+
OxyServicesContactsMixin,
|
|
65
67
|
// Utility (last, can use all above)
|
|
66
68
|
OxyServicesUtilityMixin,
|
|
67
69
|
];
|
|
@@ -69,12 +71,18 @@ const MIXIN_PIPELINE = [
|
|
|
69
71
|
* Composes all OxyServices mixins using a pipeline pattern.
|
|
70
72
|
*
|
|
71
73
|
* This is equivalent to the nested calls but more readable and maintainable.
|
|
72
|
-
* Adding a new mixin:
|
|
74
|
+
* Adding a new mixin: add it to MIXIN_PIPELINE at the appropriate position
|
|
75
|
+
* AND extend `AllMixinInstances` so its methods are visible to consumers.
|
|
73
76
|
*
|
|
74
|
-
*
|
|
77
|
+
* The cast through `unknown` carries the runtime augmentation chain into the
|
|
78
|
+
* static type system. `Array.reduce` cannot track each mixin's generic
|
|
79
|
+
* refinement, so we assert the final shape exposed by all mixins together.
|
|
80
|
+
*
|
|
81
|
+
* @returns The fully composed OxyServices constructor with all mixins applied
|
|
75
82
|
*/
|
|
76
83
|
export function composeOxyServices() {
|
|
77
|
-
|
|
84
|
+
const composed = MIXIN_PIPELINE.reduce((Base, mixin) => mixin(Base), OxyServicesBase);
|
|
85
|
+
return composed;
|
|
78
86
|
}
|
|
79
87
|
// Export the pipeline for testing/debugging
|
|
80
88
|
export { MIXIN_PIPELINE };
|
|
@@ -2,6 +2,72 @@
|
|
|
2
2
|
* Shared account types and pure helper functions.
|
|
3
3
|
* Used by both @oxyhq/services (React Native) and @oxyhq/auth (Web) account stores.
|
|
4
4
|
*/
|
|
5
|
+
import { translate } from '../i18n/index.js';
|
|
6
|
+
/**
|
|
7
|
+
* Truncate a long public key for display, e.g. `0x12345678…`.
|
|
8
|
+
* Falls back to the raw key if it's too short to truncate.
|
|
9
|
+
*/
|
|
10
|
+
export const formatPublicKeyHandle = (publicKey) => {
|
|
11
|
+
const cleaned = publicKey.startsWith('0x') ? publicKey.slice(2) : publicKey;
|
|
12
|
+
if (cleaned.length <= 8)
|
|
13
|
+
return `0x${cleaned}`;
|
|
14
|
+
return `0x${cleaned.slice(0, 8)}…`;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Resolve a friendly display name for a user.
|
|
18
|
+
*
|
|
19
|
+
* Order of preference:
|
|
20
|
+
* 1. `name.full`, or composed `name.first name.last`
|
|
21
|
+
* 2. `name` (when stored as a plain string)
|
|
22
|
+
* 3. `username`
|
|
23
|
+
* 4. `Account 0x12345678…` (derived from publicKey, when present)
|
|
24
|
+
* 5. Translated fallback (e.g. "Unnamed")
|
|
25
|
+
*
|
|
26
|
+
* The translation key `common.unnamed` is used for the final fallback. If the
|
|
27
|
+
* caller does not pass a locale, the default English translation is used.
|
|
28
|
+
*/
|
|
29
|
+
export const getAccountDisplayName = (user, locale) => {
|
|
30
|
+
if (!user)
|
|
31
|
+
return translate(locale, 'common.unnamed');
|
|
32
|
+
const { name, username, publicKey } = user;
|
|
33
|
+
if (name && typeof name === 'object') {
|
|
34
|
+
if (typeof name.full === 'string' && name.full.trim())
|
|
35
|
+
return name.full.trim();
|
|
36
|
+
const first = typeof name.first === 'string' ? name.first.trim() : '';
|
|
37
|
+
const last = typeof name.last === 'string' ? name.last.trim() : '';
|
|
38
|
+
const composed = [first, last].filter(Boolean).join(' ').trim();
|
|
39
|
+
if (composed)
|
|
40
|
+
return composed;
|
|
41
|
+
}
|
|
42
|
+
else if (typeof name === 'string' && name.trim()) {
|
|
43
|
+
return name.trim();
|
|
44
|
+
}
|
|
45
|
+
if (typeof username === 'string' && username.trim())
|
|
46
|
+
return username.trim();
|
|
47
|
+
if (typeof publicKey === 'string' && publicKey.length > 0) {
|
|
48
|
+
return translate(locale, 'common.accountFallback', {
|
|
49
|
+
handle: formatPublicKeyHandle(publicKey),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return translate(locale, 'common.unnamed');
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Resolve a `@handle` style identifier for a user.
|
|
56
|
+
*
|
|
57
|
+
* Returns the bare username when present (without the `@`), otherwise a
|
|
58
|
+
* truncated public-key handle (`0x12345678…`), or `undefined` when neither is
|
|
59
|
+
* available — callers can decide whether to hide the line entirely.
|
|
60
|
+
*/
|
|
61
|
+
export const getAccountFallbackHandle = (user) => {
|
|
62
|
+
if (!user)
|
|
63
|
+
return undefined;
|
|
64
|
+
if (typeof user.username === 'string' && user.username.trim())
|
|
65
|
+
return user.username.trim();
|
|
66
|
+
if (typeof user.publicKey === 'string' && user.publicKey.length > 0) {
|
|
67
|
+
return formatPublicKeyHandle(user.publicKey);
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
};
|
|
5
71
|
/**
|
|
6
72
|
* Build an ordered array of QuickAccounts from a map and order list.
|
|
7
73
|
*/
|
|
@@ -23,7 +89,7 @@ export const buildAccountsArray = (accounts, order) => {
|
|
|
23
89
|
* @param getFileDownloadUrl - Function to generate avatar download URL from file ID
|
|
24
90
|
*/
|
|
25
91
|
export const createQuickAccount = (sessionId, userData, existingAccount, getFileDownloadUrl) => {
|
|
26
|
-
const displayName = userData
|
|
92
|
+
const displayName = getAccountDisplayName(userData);
|
|
27
93
|
const userId = userData.id || (typeof userData._id === 'string' ? userData._id : userData._id?.toString());
|
|
28
94
|
// Preserve existing avatarUrl if avatar hasn't changed (prevents image reload)
|
|
29
95
|
let avatarUrl;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { loadAsyncStorage } from './platformCrypto.js';
|
|
1
2
|
/**
|
|
2
3
|
* Client-side device management utility
|
|
3
4
|
* Handles persistent device identification across app sessions
|
|
@@ -15,9 +16,10 @@ export class DeviceManager {
|
|
|
15
16
|
static async getStorage() {
|
|
16
17
|
if (this.isReactNative()) {
|
|
17
18
|
try {
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
// `loadAsyncStorage` is per-platform: the RN variant statically imports
|
|
20
|
+
// @react-native-async-storage/async-storage, the default variant throws
|
|
21
|
+
// (never called outside RN because of the `isReactNative()` gate above).
|
|
22
|
+
const asyncStorageModule = await loadAsyncStorage();
|
|
21
23
|
const storage = asyncStorageModule.default;
|
|
22
24
|
return {
|
|
23
25
|
getItem: storage.getItem.bind(storage),
|
|
@@ -156,3 +156,24 @@ export function normalizeLanguageCode(lang) {
|
|
|
156
156
|
};
|
|
157
157
|
return map[lang] || lang;
|
|
158
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* RTL language detection.
|
|
161
|
+
*
|
|
162
|
+
* Returns `true` when the given BCP-47 tag or bare language code is one
|
|
163
|
+
* of the right-to-left scripts we ship UI for. Apps use this to drive
|
|
164
|
+
* `I18nManager.allowRTL(true)` / `forceRTL(...)` on React Native and the
|
|
165
|
+
* `<html dir="rtl">` attribute on web.
|
|
166
|
+
*
|
|
167
|
+
* Includes Arabic (`ar`), Hebrew (`he` / legacy `iw`), Persian (`fa`),
|
|
168
|
+
* Urdu (`ur`) plus their common region variants. Unknown tags are
|
|
169
|
+
* treated as LTR.
|
|
170
|
+
*/
|
|
171
|
+
const RTL_LANGUAGE_BASES = new Set(['ar', 'he', 'iw', 'fa', 'ur']);
|
|
172
|
+
export function isRTLLocale(locale) {
|
|
173
|
+
if (!locale)
|
|
174
|
+
return false;
|
|
175
|
+
const base = locale.toLowerCase().split('-')[0];
|
|
176
|
+
if (!base)
|
|
177
|
+
return false;
|
|
178
|
+
return RTL_LANGUAGE_BASES.has(base);
|
|
179
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform Crypto / Storage — Default Variant (Node.js, Browser, generic bundlers)
|
|
3
|
+
*
|
|
4
|
+
* Provides lazy access to platform-specific crypto and storage modules.
|
|
5
|
+
*
|
|
6
|
+
* # Variants
|
|
7
|
+
*
|
|
8
|
+
* This module ships in two physical variants on disk, selected per consumer
|
|
9
|
+
* by the bundler / runtime:
|
|
10
|
+
*
|
|
11
|
+
* - `platformCrypto.js` — this file. Used by Node.js, Vite, webpack,
|
|
12
|
+
* Rollup, esbuild, and anything that does
|
|
13
|
+
* not match Metro's `*.native.js`
|
|
14
|
+
* source-extension preference.
|
|
15
|
+
* - `platformCrypto.native.js` — sibling file. Picked up automatically by
|
|
16
|
+
* Metro's resolver (which prefers
|
|
17
|
+
* `*.<platform>.js` and `*.native.js` over
|
|
18
|
+
* plain `*.js` when `preferNativePlatform`
|
|
19
|
+
* is true — Expo sets this for all non-web
|
|
20
|
+
* builds).
|
|
21
|
+
*
|
|
22
|
+
* The `package.json#exports` map also declares a `"react-native"` condition
|
|
23
|
+
* pointing at the same `dist/esm/index.js` entry — that entry transitively
|
|
24
|
+
* imports `./platformCrypto`, and Metro's per-file source-extension lookup
|
|
25
|
+
* substitutes the `.native.js` sibling automatically inside `dist/`. This
|
|
26
|
+
* means consumers never have to add resolver shims; Metro Just Works.
|
|
27
|
+
*
|
|
28
|
+
* Both variants expose the EXACT same public API; importers don't need to know
|
|
29
|
+
* which one they got. The variant difference is purely about which underlying
|
|
30
|
+
* native modules each one references:
|
|
31
|
+
*
|
|
32
|
+
* ┌──────────────────┬───────────────────────┬───────────────────────────────┐
|
|
33
|
+
* │ Function │ Default variant │ React Native variant │
|
|
34
|
+
* ├──────────────────┼───────────────────────┼───────────────────────────────┤
|
|
35
|
+
* │ loadNodeCrypto │ `await import('crypto')` (Node built-in) │
|
|
36
|
+
* │ │ │ throws — Node crypto is not │
|
|
37
|
+
* │ │ │ available on Hermes/RN │
|
|
38
|
+
* ├──────────────────┼───────────────────────┼───────────────────────────────┤
|
|
39
|
+
* │ loadExpoCrypto │ throws — expo-crypto │ static `import 'expo-crypto'` │
|
|
40
|
+
* │ │ is not part of a │ │
|
|
41
|
+
* │ │ Node/Vite bundle │ │
|
|
42
|
+
* ├──────────────────┼───────────────────────┼───────────────────────────────┤
|
|
43
|
+
* │ loadSecureStore │ throws (web/Node have │ static `import 'expo-secure-` │
|
|
44
|
+
* │ │ their own storage) │ store' │
|
|
45
|
+
* ├──────────────────┼───────────────────────┼───────────────────────────────┤
|
|
46
|
+
* │ loadAsyncStorage │ throws (web/Node have │ static `import '@react- │
|
|
47
|
+
* │ │ their own storage) │ native-async-storage/...' │
|
|
48
|
+
* ├──────────────────┼───────────────────────┼───────────────────────────────┤
|
|
49
|
+
* │ getRandomBytesRN │ throws (RN-only) │ direct call into expo-crypto │
|
|
50
|
+
* └──────────────────┴───────────────────────┴───────────────────────────────┘
|
|
51
|
+
*
|
|
52
|
+
* Crucially, the default variant references ONLY Node's `'crypto'`. It never
|
|
53
|
+
* mentions `expo-*` or `@react-native-async-storage/*` — so Vite, webpack,
|
|
54
|
+
* esbuild, Rollup, and Node itself can bundle / require it without ever
|
|
55
|
+
* attempting to resolve those RN-only packages.
|
|
56
|
+
*
|
|
57
|
+
* The React Native variant references ONLY the RN packages. It never
|
|
58
|
+
* mentions `'crypto'` — so Metro and Hermes have nothing to choke on.
|
|
59
|
+
*
|
|
60
|
+
* # Why not a single file with dynamic import?
|
|
61
|
+
*
|
|
62
|
+
* A previous iteration used a "bundler-opaque" `new Function('s', 'return
|
|
63
|
+
* import(s)')` trick so a single file could service every platform. It
|
|
64
|
+
* bundled cleanly on Metro but Hermes refused to PARSE the resulting
|
|
65
|
+
* `import()` expression inside a Function-constructor body
|
|
66
|
+
* (`SyntaxError: Invalid expression encountered` at the `(` of `import(`).
|
|
67
|
+
* The platform-extension split is the only approach that lets each runtime
|
|
68
|
+
* see a file containing only specifiers it can understand — no tricks, no
|
|
69
|
+
* runtime parsing risks.
|
|
70
|
+
*/
|
|
71
|
+
import { isReactNative } from './platform.js';
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
// Node `crypto` — Node built-in
|
|
74
|
+
//
|
|
75
|
+
// `await import('crypto')` here is a real, static-from-tsc's-perspective
|
|
76
|
+
// dynamic import. Node ESM, Vite, webpack, and esbuild all resolve it fine.
|
|
77
|
+
// Metro never sees this file because the `.react-native.js` sibling shadows
|
|
78
|
+
// it, so Metro never tries to resolve `'crypto'`.
|
|
79
|
+
// ---------------------------------------------------------------------------
|
|
80
|
+
let cachedNodeCrypto = null;
|
|
81
|
+
export async function loadNodeCrypto() {
|
|
82
|
+
if (cachedNodeCrypto) {
|
|
83
|
+
return cachedNodeCrypto;
|
|
84
|
+
}
|
|
85
|
+
cachedNodeCrypto = await import('node:crypto');
|
|
86
|
+
return cachedNodeCrypto;
|
|
87
|
+
}
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
// RN-only modules — never called from this variant.
|
|
90
|
+
//
|
|
91
|
+
// These throw a clear error if anything ever reaches them outside RN. In
|
|
92
|
+
// practice every caller gates with `isReactNative()` before calling, so
|
|
93
|
+
// these are belt-and-braces.
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
function notReactNativeError(module) {
|
|
96
|
+
return new Error(`[oxy.platformCrypto] Tried to load '${module}' outside React Native. This module is only available in a React Native runtime; bundling routed this consumer to the default (Node/web) variant. This indicates a missing platform gate (\`isReactNative()\`) in the calling code.`);
|
|
97
|
+
}
|
|
98
|
+
export async function loadExpoCrypto() {
|
|
99
|
+
if (isReactNative()) {
|
|
100
|
+
// Should be unreachable: when running on RN, Metro / the `react-native`
|
|
101
|
+
// exports condition serves the sibling variant. If we got here, the
|
|
102
|
+
// package-exports map is misconfigured for this host. Throw with a
|
|
103
|
+
// helpful diagnostic rather than fall back to a broken dynamic import.
|
|
104
|
+
throw new Error('[oxy.platformCrypto] React Native runtime resolved the default ' +
|
|
105
|
+
'(non-RN) variant of @oxyhq/core/utils/platformCrypto. Check the ' +
|
|
106
|
+
"consumer's bundler resolution — Metro should pick the sibling " +
|
|
107
|
+
'.react-native.js file via package exports.');
|
|
108
|
+
}
|
|
109
|
+
throw notReactNativeError('expo-crypto');
|
|
110
|
+
}
|
|
111
|
+
export async function loadSecureStore() {
|
|
112
|
+
throw notReactNativeError('expo-secure-store');
|
|
113
|
+
}
|
|
114
|
+
export async function loadAsyncStorage() {
|
|
115
|
+
throw notReactNativeError('@react-native-async-storage/async-storage');
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Synchronous random-bytes via `expo-crypto.getRandomBytes`. Only available
|
|
119
|
+
* in the React Native variant. The default variant throws because Node and
|
|
120
|
+
* browsers have their own native CSPRNGs (`crypto.randomBytes` and
|
|
121
|
+
* `crypto.getRandomValues` respectively) — callers should use those.
|
|
122
|
+
*/
|
|
123
|
+
export function getRandomBytesRN(_byteCount) {
|
|
124
|
+
throw notReactNativeError('expo-crypto.getRandomBytes (sync)');
|
|
125
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform Crypto / Storage — React Native Variant
|
|
3
|
+
*
|
|
4
|
+
* Companion to `./platformCrypto.ts`. See the doc-comment at the top of that
|
|
5
|
+
* file for the full design.
|
|
6
|
+
*
|
|
7
|
+
* Metro auto-selects this file in any non-web build (`preferNativePlatform`
|
|
8
|
+
* is `true` for iOS / Android, so `*.native.js` shadows `*.js` during
|
|
9
|
+
* source-extension resolution inside `node_modules/@oxyhq/core/dist/`). On
|
|
10
|
+
* iOS / Android `<base>.ios.js` / `<base>.android.js` would shadow this file
|
|
11
|
+
* if they existed, but they don't — `.native.js` is the shared RN variant.
|
|
12
|
+
*
|
|
13
|
+
* - The default variant references Node's `'crypto'` and would crash Metro
|
|
14
|
+
* if bundled into an RN app.
|
|
15
|
+
* - This variant references the RN-only modules (`expo-crypto`,
|
|
16
|
+
* `expo-secure-store`, `@react-native-async-storage/async-storage`)
|
|
17
|
+
* as static imports, so Metro and Hermes both resolve and parse them
|
|
18
|
+
* cleanly.
|
|
19
|
+
*
|
|
20
|
+
* Both variants expose the same surface; importers don't care which one
|
|
21
|
+
* they got.
|
|
22
|
+
*
|
|
23
|
+
* # Why static imports?
|
|
24
|
+
*
|
|
25
|
+
* Every RN consumer of `@oxyhq/core` already lists or transitively pulls
|
|
26
|
+
* in `expo-crypto`, `expo-secure-store`, and
|
|
27
|
+
* `@react-native-async-storage/async-storage` (they're stable Expo modules
|
|
28
|
+
* present in `services`, `accounts`, `inbox`, and `test-app`). A static
|
|
29
|
+
* import is what Metro wants to see anyway, and Hermes parses it like any
|
|
30
|
+
* other ES module — no `Function`-constructor parser exotic-mode involved.
|
|
31
|
+
*
|
|
32
|
+
* This is also clearer to debug: Metro fails up-front with a normal
|
|
33
|
+
* unresolved-module error if a consumer is missing a peer dep, instead of
|
|
34
|
+
* a confusing runtime throw the first time a code path that needs the
|
|
35
|
+
* module is exercised.
|
|
36
|
+
*/
|
|
37
|
+
import * as ExpoCrypto from 'expo-crypto';
|
|
38
|
+
import * as SecureStore from 'expo-secure-store';
|
|
39
|
+
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// Node `crypto` — never available in RN.
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
export async function loadNodeCrypto() {
|
|
44
|
+
// Unreachable in practice: every caller gates with `isNodeJS()` before
|
|
45
|
+
// invoking this. If it somehow does fire, throw immediately with a clear
|
|
46
|
+
// diagnostic rather than letting Metro / Hermes attempt to find a
|
|
47
|
+
// non-existent module at runtime.
|
|
48
|
+
throw new Error("[oxy.platformCrypto] Node's built-in 'crypto' module is not available " +
|
|
49
|
+
'in a React Native runtime. Use the RN-specific helpers ' +
|
|
50
|
+
'(loadExpoCrypto, getRandomBytesRN) or the Web Crypto API (`globalThis.crypto`).');
|
|
51
|
+
}
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
// expo-crypto — RN cryptographic primitives.
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
export async function loadExpoCrypto() {
|
|
56
|
+
return ExpoCrypto;
|
|
57
|
+
}
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
// expo-secure-store — RN keychain / keystore.
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
export async function loadSecureStore() {
|
|
62
|
+
return SecureStore;
|
|
63
|
+
}
|
|
64
|
+
export async function loadAsyncStorage() {
|
|
65
|
+
// Mirror the shape callers historically used (`module.default.<method>`)
|
|
66
|
+
// so the call sites don't have to know whether the underlying module
|
|
67
|
+
// ships ESM or CJS-with-default.
|
|
68
|
+
const storage = AsyncStorage;
|
|
69
|
+
return {
|
|
70
|
+
default: storage,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Synchronous random-bytes via `expo-crypto.getRandomBytes`. Available
|
|
75
|
+
* synchronously because `expo-crypto` is statically imported by this file
|
|
76
|
+
* — no async initialization race.
|
|
77
|
+
*/
|
|
78
|
+
export function getRandomBytesRN(byteCount) {
|
|
79
|
+
return ExpoCrypto.getRandomBytes(byteCount);
|
|
80
|
+
}
|