@oxyhq/core 1.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/README.md +50 -0
- package/dist/cjs/AuthManager.js +361 -0
- package/dist/cjs/CrossDomainAuth.js +258 -0
- package/dist/cjs/HttpService.js +618 -0
- package/dist/cjs/OxyServices.base.js +263 -0
- package/dist/cjs/OxyServices.errors.js +22 -0
- package/dist/cjs/OxyServices.js +63 -0
- package/dist/cjs/constants/version.js +16 -0
- package/dist/cjs/crypto/index.js +20 -0
- package/dist/cjs/crypto/keyManager.js +887 -0
- package/dist/cjs/crypto/polyfill.js +64 -0
- package/dist/cjs/crypto/recoveryPhrase.js +169 -0
- package/dist/cjs/crypto/signatureService.js +296 -0
- package/dist/cjs/i18n/index.js +73 -0
- package/dist/cjs/i18n/locales/ar-SA.json +120 -0
- package/dist/cjs/i18n/locales/ca-ES.json +120 -0
- package/dist/cjs/i18n/locales/de-DE.json +120 -0
- package/dist/cjs/i18n/locales/en-US.json +956 -0
- package/dist/cjs/i18n/locales/es-ES.json +944 -0
- package/dist/cjs/i18n/locales/fr-FR.json +120 -0
- package/dist/cjs/i18n/locales/it-IT.json +120 -0
- package/dist/cjs/i18n/locales/ja-JP.json +119 -0
- package/dist/cjs/i18n/locales/ko-KR.json +120 -0
- package/dist/cjs/i18n/locales/locales/ar-SA.json +120 -0
- package/dist/cjs/i18n/locales/locales/ca-ES.json +120 -0
- package/dist/cjs/i18n/locales/locales/de-DE.json +120 -0
- package/dist/cjs/i18n/locales/locales/en-US.json +956 -0
- package/dist/cjs/i18n/locales/locales/es-ES.json +944 -0
- package/dist/cjs/i18n/locales/locales/fr-FR.json +120 -0
- package/dist/cjs/i18n/locales/locales/it-IT.json +120 -0
- package/dist/cjs/i18n/locales/locales/ja-JP.json +119 -0
- package/dist/cjs/i18n/locales/locales/ko-KR.json +120 -0
- package/dist/cjs/i18n/locales/locales/pt-PT.json +120 -0
- package/dist/cjs/i18n/locales/locales/zh-CN.json +120 -0
- package/dist/cjs/i18n/locales/pt-PT.json +120 -0
- package/dist/cjs/i18n/locales/zh-CN.json +120 -0
- package/dist/cjs/index.js +153 -0
- package/dist/cjs/mixins/OxyServices.analytics.js +49 -0
- package/dist/cjs/mixins/OxyServices.assets.js +380 -0
- package/dist/cjs/mixins/OxyServices.auth.js +259 -0
- package/dist/cjs/mixins/OxyServices.developer.js +97 -0
- package/dist/cjs/mixins/OxyServices.devices.js +116 -0
- package/dist/cjs/mixins/OxyServices.features.js +309 -0
- package/dist/cjs/mixins/OxyServices.fedcm.js +435 -0
- package/dist/cjs/mixins/OxyServices.karma.js +108 -0
- package/dist/cjs/mixins/OxyServices.language.js +154 -0
- package/dist/cjs/mixins/OxyServices.location.js +43 -0
- package/dist/cjs/mixins/OxyServices.payment.js +158 -0
- package/dist/cjs/mixins/OxyServices.popup.js +371 -0
- package/dist/cjs/mixins/OxyServices.privacy.js +162 -0
- package/dist/cjs/mixins/OxyServices.redirect.js +345 -0
- package/dist/cjs/mixins/OxyServices.security.js +81 -0
- package/dist/cjs/mixins/OxyServices.user.js +355 -0
- package/dist/cjs/mixins/OxyServices.utility.js +156 -0
- package/dist/cjs/mixins/index.js +79 -0
- package/dist/cjs/mixins/mixinHelpers.js +53 -0
- package/dist/cjs/models/interfaces.js +20 -0
- package/dist/cjs/models/session.js +2 -0
- package/dist/cjs/shared/index.js +70 -0
- package/dist/cjs/shared/utils/colorUtils.js +153 -0
- package/dist/cjs/shared/utils/debugUtils.js +73 -0
- package/dist/cjs/shared/utils/errorUtils.js +183 -0
- package/dist/cjs/shared/utils/index.js +49 -0
- package/dist/cjs/shared/utils/networkUtils.js +183 -0
- package/dist/cjs/shared/utils/themeUtils.js +106 -0
- package/dist/cjs/utils/apiUtils.js +61 -0
- package/dist/cjs/utils/asyncUtils.js +194 -0
- package/dist/cjs/utils/cache.js +226 -0
- package/dist/cjs/utils/deviceManager.js +205 -0
- package/dist/cjs/utils/errorUtils.js +154 -0
- package/dist/cjs/utils/index.js +26 -0
- package/dist/cjs/utils/languageUtils.js +165 -0
- package/dist/cjs/utils/loggerUtils.js +126 -0
- package/dist/cjs/utils/platform.js +144 -0
- package/dist/cjs/utils/requestUtils.js +209 -0
- package/dist/cjs/utils/sessionUtils.js +181 -0
- package/dist/cjs/utils/validationUtils.js +173 -0
- package/dist/esm/AuthManager.js +356 -0
- package/dist/esm/CrossDomainAuth.js +253 -0
- package/dist/esm/HttpService.js +614 -0
- package/dist/esm/OxyServices.base.js +259 -0
- package/dist/esm/OxyServices.errors.js +17 -0
- package/dist/esm/OxyServices.js +59 -0
- package/dist/esm/constants/version.js +13 -0
- package/dist/esm/crypto/index.js +13 -0
- package/dist/esm/crypto/keyManager.js +850 -0
- package/dist/esm/crypto/polyfill.js +61 -0
- package/dist/esm/crypto/recoveryPhrase.js +132 -0
- package/dist/esm/crypto/signatureService.js +259 -0
- package/dist/esm/i18n/index.js +69 -0
- package/dist/esm/i18n/locales/ar-SA.json +120 -0
- package/dist/esm/i18n/locales/ca-ES.json +120 -0
- package/dist/esm/i18n/locales/de-DE.json +120 -0
- package/dist/esm/i18n/locales/en-US.json +956 -0
- package/dist/esm/i18n/locales/es-ES.json +944 -0
- package/dist/esm/i18n/locales/fr-FR.json +120 -0
- package/dist/esm/i18n/locales/it-IT.json +120 -0
- package/dist/esm/i18n/locales/ja-JP.json +119 -0
- package/dist/esm/i18n/locales/ko-KR.json +120 -0
- package/dist/esm/i18n/locales/locales/ar-SA.json +120 -0
- package/dist/esm/i18n/locales/locales/ca-ES.json +120 -0
- package/dist/esm/i18n/locales/locales/de-DE.json +120 -0
- package/dist/esm/i18n/locales/locales/en-US.json +956 -0
- package/dist/esm/i18n/locales/locales/es-ES.json +944 -0
- package/dist/esm/i18n/locales/locales/fr-FR.json +120 -0
- package/dist/esm/i18n/locales/locales/it-IT.json +120 -0
- package/dist/esm/i18n/locales/locales/ja-JP.json +119 -0
- package/dist/esm/i18n/locales/locales/ko-KR.json +120 -0
- package/dist/esm/i18n/locales/locales/pt-PT.json +120 -0
- package/dist/esm/i18n/locales/locales/zh-CN.json +120 -0
- package/dist/esm/i18n/locales/pt-PT.json +120 -0
- package/dist/esm/i18n/locales/zh-CN.json +120 -0
- package/dist/esm/index.js +55 -0
- package/dist/esm/mixins/OxyServices.analytics.js +46 -0
- package/dist/esm/mixins/OxyServices.assets.js +377 -0
- package/dist/esm/mixins/OxyServices.auth.js +256 -0
- package/dist/esm/mixins/OxyServices.developer.js +94 -0
- package/dist/esm/mixins/OxyServices.devices.js +113 -0
- package/dist/esm/mixins/OxyServices.features.js +306 -0
- package/dist/esm/mixins/OxyServices.fedcm.js +433 -0
- package/dist/esm/mixins/OxyServices.karma.js +105 -0
- package/dist/esm/mixins/OxyServices.language.js +118 -0
- package/dist/esm/mixins/OxyServices.location.js +40 -0
- package/dist/esm/mixins/OxyServices.payment.js +155 -0
- package/dist/esm/mixins/OxyServices.popup.js +369 -0
- package/dist/esm/mixins/OxyServices.privacy.js +159 -0
- package/dist/esm/mixins/OxyServices.redirect.js +343 -0
- package/dist/esm/mixins/OxyServices.security.js +78 -0
- package/dist/esm/mixins/OxyServices.user.js +352 -0
- package/dist/esm/mixins/OxyServices.utility.js +153 -0
- package/dist/esm/mixins/index.js +76 -0
- package/dist/esm/mixins/mixinHelpers.js +48 -0
- package/dist/esm/models/interfaces.js +17 -0
- package/dist/esm/models/session.js +1 -0
- package/dist/esm/shared/index.js +31 -0
- package/dist/esm/shared/utils/colorUtils.js +143 -0
- package/dist/esm/shared/utils/debugUtils.js +65 -0
- package/dist/esm/shared/utils/errorUtils.js +170 -0
- package/dist/esm/shared/utils/index.js +15 -0
- package/dist/esm/shared/utils/networkUtils.js +173 -0
- package/dist/esm/shared/utils/themeUtils.js +98 -0
- package/dist/esm/utils/apiUtils.js +55 -0
- package/dist/esm/utils/asyncUtils.js +179 -0
- package/dist/esm/utils/cache.js +218 -0
- package/dist/esm/utils/deviceManager.js +168 -0
- package/dist/esm/utils/errorUtils.js +146 -0
- package/dist/esm/utils/index.js +7 -0
- package/dist/esm/utils/languageUtils.js +158 -0
- package/dist/esm/utils/loggerUtils.js +115 -0
- package/dist/esm/utils/platform.js +102 -0
- package/dist/esm/utils/requestUtils.js +203 -0
- package/dist/esm/utils/sessionUtils.js +171 -0
- package/dist/esm/utils/validationUtils.js +153 -0
- package/dist/types/AuthManager.d.ts +143 -0
- package/dist/types/CrossDomainAuth.d.ts +160 -0
- package/dist/types/HttpService.d.ts +163 -0
- package/dist/types/OxyServices.base.d.ts +126 -0
- package/dist/types/OxyServices.d.ts +81 -0
- package/dist/types/OxyServices.errors.d.ts +11 -0
- package/dist/types/constants/version.d.ts +13 -0
- package/dist/types/crypto/index.d.ts +11 -0
- package/dist/types/crypto/keyManager.d.ts +189 -0
- package/dist/types/crypto/polyfill.d.ts +11 -0
- package/dist/types/crypto/recoveryPhrase.d.ts +58 -0
- package/dist/types/crypto/signatureService.d.ts +86 -0
- package/dist/types/i18n/index.d.ts +3 -0
- package/dist/types/index.d.ts +50 -0
- package/dist/types/mixins/OxyServices.analytics.d.ts +66 -0
- package/dist/types/mixins/OxyServices.assets.d.ts +135 -0
- package/dist/types/mixins/OxyServices.auth.d.ts +186 -0
- package/dist/types/mixins/OxyServices.developer.d.ts +99 -0
- package/dist/types/mixins/OxyServices.devices.d.ts +96 -0
- package/dist/types/mixins/OxyServices.features.d.ts +228 -0
- package/dist/types/mixins/OxyServices.fedcm.d.ts +200 -0
- package/dist/types/mixins/OxyServices.karma.d.ts +85 -0
- package/dist/types/mixins/OxyServices.language.d.ts +81 -0
- package/dist/types/mixins/OxyServices.location.d.ts +64 -0
- package/dist/types/mixins/OxyServices.payment.d.ts +111 -0
- package/dist/types/mixins/OxyServices.popup.d.ts +205 -0
- package/dist/types/mixins/OxyServices.privacy.d.ts +122 -0
- package/dist/types/mixins/OxyServices.redirect.d.ts +245 -0
- package/dist/types/mixins/OxyServices.security.d.ts +78 -0
- package/dist/types/mixins/OxyServices.user.d.ts +182 -0
- package/dist/types/mixins/OxyServices.utility.d.ts +93 -0
- package/dist/types/mixins/index.d.ts +30 -0
- package/dist/types/mixins/mixinHelpers.d.ts +31 -0
- package/dist/types/models/interfaces.d.ts +415 -0
- package/dist/types/models/session.d.ts +27 -0
- package/dist/types/shared/index.d.ts +28 -0
- package/dist/types/shared/utils/colorUtils.d.ts +104 -0
- package/dist/types/shared/utils/debugUtils.d.ts +48 -0
- package/dist/types/shared/utils/errorUtils.d.ts +97 -0
- package/dist/types/shared/utils/index.d.ts +13 -0
- package/dist/types/shared/utils/networkUtils.d.ts +139 -0
- package/dist/types/shared/utils/themeUtils.d.ts +90 -0
- package/dist/types/utils/apiUtils.d.ts +53 -0
- package/dist/types/utils/asyncUtils.d.ts +58 -0
- package/dist/types/utils/cache.d.ts +127 -0
- package/dist/types/utils/deviceManager.d.ts +65 -0
- package/dist/types/utils/errorUtils.d.ts +46 -0
- package/dist/types/utils/index.d.ts +6 -0
- package/dist/types/utils/languageUtils.d.ts +37 -0
- package/dist/types/utils/loggerUtils.d.ts +48 -0
- package/dist/types/utils/platform.d.ts +40 -0
- package/dist/types/utils/requestUtils.d.ts +123 -0
- package/dist/types/utils/sessionUtils.d.ts +54 -0
- package/dist/types/utils/validationUtils.d.ts +85 -0
- package/package.json +84 -0
- package/src/AuthManager.ts +436 -0
- package/src/CrossDomainAuth.ts +307 -0
- package/src/HttpService.ts +752 -0
- package/src/OxyServices.base.ts +334 -0
- package/src/OxyServices.errors.ts +26 -0
- package/src/OxyServices.ts +129 -0
- package/src/constants/version.ts +15 -0
- package/src/crypto/index.ts +25 -0
- package/src/crypto/keyManager.ts +962 -0
- package/src/crypto/polyfill.ts +70 -0
- package/src/crypto/recoveryPhrase.ts +166 -0
- package/src/crypto/signatureService.ts +323 -0
- package/src/i18n/index.ts +75 -0
- package/src/i18n/locales/ar-SA.json +120 -0
- package/src/i18n/locales/ca-ES.json +120 -0
- package/src/i18n/locales/de-DE.json +120 -0
- package/src/i18n/locales/en-US.json +956 -0
- package/src/i18n/locales/es-ES.json +944 -0
- package/src/i18n/locales/fr-FR.json +120 -0
- package/src/i18n/locales/it-IT.json +120 -0
- package/src/i18n/locales/ja-JP.json +119 -0
- package/src/i18n/locales/ko-KR.json +120 -0
- package/src/i18n/locales/pt-PT.json +120 -0
- package/src/i18n/locales/zh-CN.json +120 -0
- package/src/index.ts +153 -0
- package/src/mixins/OxyServices.analytics.ts +53 -0
- package/src/mixins/OxyServices.assets.ts +412 -0
- package/src/mixins/OxyServices.auth.ts +358 -0
- package/src/mixins/OxyServices.developer.ts +114 -0
- package/src/mixins/OxyServices.devices.ts +119 -0
- package/src/mixins/OxyServices.features.ts +428 -0
- package/src/mixins/OxyServices.fedcm.ts +494 -0
- package/src/mixins/OxyServices.karma.ts +111 -0
- package/src/mixins/OxyServices.language.ts +127 -0
- package/src/mixins/OxyServices.location.ts +46 -0
- package/src/mixins/OxyServices.payment.ts +163 -0
- package/src/mixins/OxyServices.popup.ts +443 -0
- package/src/mixins/OxyServices.privacy.ts +182 -0
- package/src/mixins/OxyServices.redirect.ts +397 -0
- package/src/mixins/OxyServices.security.ts +103 -0
- package/src/mixins/OxyServices.user.ts +392 -0
- package/src/mixins/OxyServices.utility.ts +191 -0
- package/src/mixins/index.ts +91 -0
- package/src/mixins/mixinHelpers.ts +69 -0
- package/src/models/interfaces.ts +511 -0
- package/src/models/session.ts +30 -0
- package/src/shared/index.ts +82 -0
- package/src/shared/utils/colorUtils.ts +155 -0
- package/src/shared/utils/debugUtils.ts +73 -0
- package/src/shared/utils/errorUtils.ts +181 -0
- package/src/shared/utils/index.ts +59 -0
- package/src/shared/utils/networkUtils.ts +248 -0
- package/src/shared/utils/themeUtils.ts +115 -0
- package/src/types/bip39.d.ts +32 -0
- package/src/types/buffer.d.ts +97 -0
- package/src/types/color.d.ts +20 -0
- package/src/types/elliptic.d.ts +62 -0
- package/src/utils/apiUtils.ts +88 -0
- package/src/utils/asyncUtils.ts +252 -0
- package/src/utils/cache.ts +264 -0
- package/src/utils/deviceManager.ts +198 -0
- package/src/utils/errorUtils.ts +216 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/languageUtils.ts +174 -0
- package/src/utils/loggerUtils.ts +153 -0
- package/src/utils/platform.ts +117 -0
- package/src/utils/requestUtils.ts +237 -0
- package/src/utils/sessionUtils.ts +206 -0
- package/src/utils/validationUtils.ts +174 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Crypto Polyfills
|
|
4
|
+
*
|
|
5
|
+
* Ensures Buffer and crypto.getRandomValues are available
|
|
6
|
+
* across all platforms (Node.js, Browser, React Native).
|
|
7
|
+
*
|
|
8
|
+
* - Browser/Node.js: Uses native crypto
|
|
9
|
+
* - React Native: Falls back to expo-crypto if native crypto unavailable
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Buffer = void 0;
|
|
13
|
+
const buffer_1 = require("buffer");
|
|
14
|
+
Object.defineProperty(exports, "Buffer", { enumerable: true, get: function () { return buffer_1.Buffer; } });
|
|
15
|
+
const getGlobalObject = () => {
|
|
16
|
+
if (typeof globalThis !== 'undefined')
|
|
17
|
+
return globalThis;
|
|
18
|
+
if (typeof global !== 'undefined')
|
|
19
|
+
return global;
|
|
20
|
+
if (typeof window !== 'undefined')
|
|
21
|
+
return window;
|
|
22
|
+
if (typeof self !== 'undefined')
|
|
23
|
+
return self;
|
|
24
|
+
return {};
|
|
25
|
+
};
|
|
26
|
+
const globalObject = getGlobalObject();
|
|
27
|
+
// Make Buffer available globally for libraries that depend on it
|
|
28
|
+
if (!globalObject.Buffer) {
|
|
29
|
+
globalObject.Buffer = buffer_1.Buffer;
|
|
30
|
+
}
|
|
31
|
+
// Cache for expo-crypto module (lazy loaded only in React Native)
|
|
32
|
+
let expoCryptoModule = null;
|
|
33
|
+
let expoCryptoLoadAttempted = false;
|
|
34
|
+
function getRandomBytesSync(byteCount) {
|
|
35
|
+
if (!expoCryptoLoadAttempted) {
|
|
36
|
+
expoCryptoLoadAttempted = true;
|
|
37
|
+
try {
|
|
38
|
+
expoCryptoModule = require('expo-crypto');
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// expo-crypto not available — expected in non-RN environments
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (expoCryptoModule) {
|
|
45
|
+
return expoCryptoModule.getRandomBytes(byteCount);
|
|
46
|
+
}
|
|
47
|
+
throw new Error('No crypto.getRandomValues implementation available. ' +
|
|
48
|
+
'In React Native, install expo-crypto.');
|
|
49
|
+
}
|
|
50
|
+
const cryptoPolyfill = {
|
|
51
|
+
getRandomValues(array) {
|
|
52
|
+
const bytes = getRandomBytesSync(array.byteLength);
|
|
53
|
+
const uint8View = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
54
|
+
uint8View.set(bytes);
|
|
55
|
+
return array;
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
// Only polyfill if crypto or crypto.getRandomValues is not available
|
|
59
|
+
if (typeof globalObject.crypto === 'undefined') {
|
|
60
|
+
globalObject.crypto = cryptoPolyfill;
|
|
61
|
+
}
|
|
62
|
+
else if (typeof globalObject.crypto.getRandomValues !== 'function') {
|
|
63
|
+
globalObject.crypto.getRandomValues = cryptoPolyfill.getRandomValues;
|
|
64
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Recovery Phrase Service - BIP39 Mnemonic Generation
|
|
4
|
+
*
|
|
5
|
+
* Handles generation and restoration of recovery phrases (mnemonic seeds)
|
|
6
|
+
* for backing up and restoring user identities.
|
|
7
|
+
*
|
|
8
|
+
* Note: This module requires the polyfill to be loaded first (done via crypto/index.ts)
|
|
9
|
+
*/
|
|
10
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
13
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
14
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
15
|
+
}
|
|
16
|
+
Object.defineProperty(o, k2, desc);
|
|
17
|
+
}) : (function(o, m, k, k2) {
|
|
18
|
+
if (k2 === undefined) k2 = k;
|
|
19
|
+
o[k2] = m[k];
|
|
20
|
+
}));
|
|
21
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
22
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
23
|
+
}) : function(o, v) {
|
|
24
|
+
o["default"] = v;
|
|
25
|
+
});
|
|
26
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
27
|
+
var ownKeys = function(o) {
|
|
28
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
29
|
+
var ar = [];
|
|
30
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
31
|
+
return ar;
|
|
32
|
+
};
|
|
33
|
+
return ownKeys(o);
|
|
34
|
+
};
|
|
35
|
+
return function (mod) {
|
|
36
|
+
if (mod && mod.__esModule) return mod;
|
|
37
|
+
var result = {};
|
|
38
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
39
|
+
__setModuleDefault(result, mod);
|
|
40
|
+
return result;
|
|
41
|
+
};
|
|
42
|
+
})();
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
exports.RecoveryPhraseService = void 0;
|
|
45
|
+
const bip39 = __importStar(require("bip39"));
|
|
46
|
+
const keyManager_1 = require("./keyManager");
|
|
47
|
+
/**
|
|
48
|
+
* Convert Uint8Array or array-like to hexadecimal string
|
|
49
|
+
* Works in both Node.js and React Native without depending on Buffer
|
|
50
|
+
*/
|
|
51
|
+
function toHex(data) {
|
|
52
|
+
// Convert to array of numbers if needed
|
|
53
|
+
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
54
|
+
return Array.from(bytes)
|
|
55
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
56
|
+
.join('');
|
|
57
|
+
}
|
|
58
|
+
class RecoveryPhraseService {
|
|
59
|
+
/**
|
|
60
|
+
* Generate a new identity with a recovery phrase
|
|
61
|
+
* Returns the mnemonic phrase (should only be shown once to the user)
|
|
62
|
+
*/
|
|
63
|
+
static async generateIdentityWithRecovery() {
|
|
64
|
+
// Generate 128-bit entropy for 12-word mnemonic
|
|
65
|
+
const mnemonic = bip39.generateMnemonic(128);
|
|
66
|
+
// Derive private key from mnemonic
|
|
67
|
+
// Using the seed directly as the private key (simplified approach)
|
|
68
|
+
const seed = await bip39.mnemonicToSeed(mnemonic);
|
|
69
|
+
// Use first 32 bytes of seed as private key
|
|
70
|
+
const seedSlice = seed.subarray ? seed.subarray(0, 32) : seed.slice(0, 32);
|
|
71
|
+
const privateKeyHex = toHex(seedSlice);
|
|
72
|
+
// Import the derived key pair
|
|
73
|
+
const publicKey = await keyManager_1.KeyManager.importKeyPair(privateKeyHex);
|
|
74
|
+
return {
|
|
75
|
+
phrase: mnemonic,
|
|
76
|
+
words: mnemonic.split(' '),
|
|
77
|
+
publicKey,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Generate a 24-word recovery phrase for higher security
|
|
82
|
+
*/
|
|
83
|
+
static async generateIdentityWithRecovery24() {
|
|
84
|
+
// Generate 256-bit entropy for 24-word mnemonic
|
|
85
|
+
const mnemonic = bip39.generateMnemonic(256);
|
|
86
|
+
const seed = await bip39.mnemonicToSeed(mnemonic);
|
|
87
|
+
const seedSlice = seed.subarray ? seed.subarray(0, 32) : seed.slice(0, 32);
|
|
88
|
+
const privateKeyHex = toHex(seedSlice);
|
|
89
|
+
const publicKey = await keyManager_1.KeyManager.importKeyPair(privateKeyHex);
|
|
90
|
+
return {
|
|
91
|
+
phrase: mnemonic,
|
|
92
|
+
words: mnemonic.split(' '),
|
|
93
|
+
publicKey,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Restore an identity from a recovery phrase
|
|
98
|
+
*/
|
|
99
|
+
static async restoreFromPhrase(phrase) {
|
|
100
|
+
// Normalize and validate the phrase
|
|
101
|
+
const normalizedPhrase = phrase.trim().toLowerCase();
|
|
102
|
+
if (!bip39.validateMnemonic(normalizedPhrase)) {
|
|
103
|
+
throw new Error('Invalid recovery phrase. Please check the words and try again.');
|
|
104
|
+
}
|
|
105
|
+
// Derive the same private key from the mnemonic
|
|
106
|
+
const seed = await bip39.mnemonicToSeed(normalizedPhrase);
|
|
107
|
+
const seedSlice = seed.subarray ? seed.subarray(0, 32) : seed.slice(0, 32);
|
|
108
|
+
const privateKeyHex = toHex(seedSlice);
|
|
109
|
+
// Import and store the key pair
|
|
110
|
+
const publicKey = await keyManager_1.KeyManager.importKeyPair(privateKeyHex);
|
|
111
|
+
return publicKey;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Validate a recovery phrase without importing it
|
|
115
|
+
*/
|
|
116
|
+
static validatePhrase(phrase) {
|
|
117
|
+
const normalizedPhrase = phrase.trim().toLowerCase();
|
|
118
|
+
return bip39.validateMnemonic(normalizedPhrase);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get the word list for autocomplete/validation
|
|
122
|
+
*/
|
|
123
|
+
static getWordList() {
|
|
124
|
+
return bip39.wordlists.english;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Check if a word is valid in the BIP39 word list
|
|
128
|
+
*/
|
|
129
|
+
static isValidWord(word) {
|
|
130
|
+
return bip39.wordlists.english.includes(word.toLowerCase());
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Get suggestions for a partial word
|
|
134
|
+
*/
|
|
135
|
+
static getSuggestions(partial, limit = 5) {
|
|
136
|
+
const lowerPartial = partial.toLowerCase();
|
|
137
|
+
return bip39.wordlists.english
|
|
138
|
+
.filter((word) => word.startsWith(lowerPartial))
|
|
139
|
+
.slice(0, limit);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Derive the public key from a phrase without storing
|
|
143
|
+
* Useful for verification before importing
|
|
144
|
+
*/
|
|
145
|
+
static async derivePublicKeyFromPhrase(phrase) {
|
|
146
|
+
const normalizedPhrase = phrase.trim().toLowerCase();
|
|
147
|
+
if (!bip39.validateMnemonic(normalizedPhrase)) {
|
|
148
|
+
throw new Error('Invalid recovery phrase');
|
|
149
|
+
}
|
|
150
|
+
const seed = await bip39.mnemonicToSeed(normalizedPhrase);
|
|
151
|
+
const seedSlice = seed.subarray ? seed.subarray(0, 32) : seed.slice(0, 32);
|
|
152
|
+
const privateKeyHex = toHex(seedSlice);
|
|
153
|
+
return keyManager_1.KeyManager.derivePublicKey(privateKeyHex);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Convert a phrase to its word array
|
|
157
|
+
*/
|
|
158
|
+
static phraseToWords(phrase) {
|
|
159
|
+
return phrase.trim().toLowerCase().split(/\s+/);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Convert a word array to a phrase string
|
|
163
|
+
*/
|
|
164
|
+
static wordsToPhrase(words) {
|
|
165
|
+
return words.map(w => w.toLowerCase().trim()).join(' ');
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
exports.RecoveryPhraseService = RecoveryPhraseService;
|
|
169
|
+
exports.default = RecoveryPhraseService;
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Signature Service - ECDSA Digital Signatures
|
|
4
|
+
*
|
|
5
|
+
* Handles signing and verification of messages using ECDSA secp256k1.
|
|
6
|
+
* Used for authenticating requests and proving identity ownership.
|
|
7
|
+
*/
|
|
8
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
|
+
if (k2 === undefined) k2 = k;
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
15
|
+
}) : (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
o[k2] = m[k];
|
|
18
|
+
}));
|
|
19
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
20
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
21
|
+
}) : function(o, v) {
|
|
22
|
+
o["default"] = v;
|
|
23
|
+
});
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.SignatureService = void 0;
|
|
43
|
+
const elliptic_1 = require("elliptic");
|
|
44
|
+
const keyManager_1 = require("./keyManager");
|
|
45
|
+
// Lazy import for expo-crypto
|
|
46
|
+
let ExpoCrypto = null;
|
|
47
|
+
const ec = new elliptic_1.ec('secp256k1');
|
|
48
|
+
/**
|
|
49
|
+
* Check if we're in a React Native environment
|
|
50
|
+
*/
|
|
51
|
+
function isReactNative() {
|
|
52
|
+
return typeof navigator !== 'undefined' && navigator.product === 'ReactNative';
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Check if we're in a Node.js environment
|
|
56
|
+
*/
|
|
57
|
+
function isNodeJS() {
|
|
58
|
+
return typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Initialize expo-crypto module
|
|
62
|
+
*/
|
|
63
|
+
async function initExpoCrypto() {
|
|
64
|
+
if (!ExpoCrypto) {
|
|
65
|
+
ExpoCrypto = await Promise.resolve().then(() => __importStar(require('expo-crypto')));
|
|
66
|
+
}
|
|
67
|
+
return ExpoCrypto;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Compute SHA-256 hash of a string
|
|
71
|
+
*/
|
|
72
|
+
async function sha256(message) {
|
|
73
|
+
// In React Native, always use expo-crypto
|
|
74
|
+
if (isReactNative() || !isNodeJS()) {
|
|
75
|
+
const Crypto = await initExpoCrypto();
|
|
76
|
+
return Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA256, message);
|
|
77
|
+
}
|
|
78
|
+
// In Node.js, use Node's crypto module
|
|
79
|
+
// Use Function constructor to prevent Metro bundler from statically analyzing this require
|
|
80
|
+
// This ensures the require is only evaluated in Node.js runtime, not during Metro bundling
|
|
81
|
+
try {
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
83
|
+
const getCrypto = new Function('return require("crypto")');
|
|
84
|
+
const crypto = getCrypto();
|
|
85
|
+
return crypto.createHash('sha256').update(message).digest('hex');
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
// Fallback to expo-crypto if Node crypto fails
|
|
89
|
+
const Crypto = await initExpoCrypto();
|
|
90
|
+
return Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA256, message);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
class SignatureService {
|
|
94
|
+
/**
|
|
95
|
+
* Generate a random challenge string (for offline use)
|
|
96
|
+
* Uses expo-crypto in React Native, crypto.randomBytes in Node.js
|
|
97
|
+
*/
|
|
98
|
+
static async generateChallenge() {
|
|
99
|
+
if (isReactNative() || !isNodeJS()) {
|
|
100
|
+
// Use expo-crypto for React Native (expo-random is deprecated)
|
|
101
|
+
const Crypto = await initExpoCrypto();
|
|
102
|
+
const randomBytes = await Crypto.getRandomBytesAsync(32);
|
|
103
|
+
return Array.from(randomBytes)
|
|
104
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
105
|
+
.join('');
|
|
106
|
+
}
|
|
107
|
+
// Node.js fallback
|
|
108
|
+
try {
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
110
|
+
const getCrypto = new Function('return require("crypto")');
|
|
111
|
+
const crypto = getCrypto();
|
|
112
|
+
return crypto.randomBytes(32).toString('hex');
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
// Fallback to expo-crypto if Node crypto fails
|
|
116
|
+
const Crypto = await initExpoCrypto();
|
|
117
|
+
const randomBytes = await Crypto.getRandomBytesAsync(32);
|
|
118
|
+
return Array.from(randomBytes)
|
|
119
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
120
|
+
.join('');
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Hash a message using SHA-256
|
|
125
|
+
*/
|
|
126
|
+
static async hashMessage(message) {
|
|
127
|
+
return sha256(message);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Sign a message using the stored private key
|
|
131
|
+
* Returns the signature in DER format (hex encoded)
|
|
132
|
+
*/
|
|
133
|
+
static async sign(message) {
|
|
134
|
+
const keyPair = await keyManager_1.KeyManager.getKeyPairObject();
|
|
135
|
+
if (!keyPair) {
|
|
136
|
+
throw new Error('No identity found. Please create or import an identity first.');
|
|
137
|
+
}
|
|
138
|
+
const messageHash = await sha256(message);
|
|
139
|
+
const signature = keyPair.sign(messageHash);
|
|
140
|
+
return signature.toDER('hex');
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Sign a message with an explicit private key (without storing)
|
|
144
|
+
* Useful for one-time operations or testing
|
|
145
|
+
*/
|
|
146
|
+
static async signWithKey(message, privateKey) {
|
|
147
|
+
const keyPair = ec.keyFromPrivate(privateKey);
|
|
148
|
+
const messageHash = await sha256(message);
|
|
149
|
+
const signature = keyPair.sign(messageHash);
|
|
150
|
+
return signature.toDER('hex');
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Verify a signature against a message and public key
|
|
154
|
+
*/
|
|
155
|
+
static async verify(message, signature, publicKey) {
|
|
156
|
+
try {
|
|
157
|
+
const key = ec.keyFromPublic(publicKey, 'hex');
|
|
158
|
+
const messageHash = await sha256(message);
|
|
159
|
+
return key.verify(messageHash, signature);
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Synchronous verification (for Node.js backend)
|
|
167
|
+
* Uses crypto module directly for hashing
|
|
168
|
+
* Note: This method should only be used in Node.js environments
|
|
169
|
+
*/
|
|
170
|
+
static verifySync(message, signature, publicKey) {
|
|
171
|
+
try {
|
|
172
|
+
if (!isNodeJS()) {
|
|
173
|
+
// In React Native, use async verify instead
|
|
174
|
+
throw new Error('verifySync should only be used in Node.js. Use verify() in React Native.');
|
|
175
|
+
}
|
|
176
|
+
// Use Function constructor to prevent Metro bundler from statically analyzing this require
|
|
177
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
178
|
+
const getCrypto = new Function('return require("crypto")');
|
|
179
|
+
const crypto = getCrypto();
|
|
180
|
+
const key = ec.keyFromPublic(publicKey, 'hex');
|
|
181
|
+
const messageHash = crypto.createHash('sha256').update(message).digest('hex');
|
|
182
|
+
return key.verify(messageHash, signature);
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Create a signed message object with metadata
|
|
190
|
+
*/
|
|
191
|
+
static async createSignedMessage(message) {
|
|
192
|
+
const publicKey = await keyManager_1.KeyManager.getPublicKey();
|
|
193
|
+
if (!publicKey) {
|
|
194
|
+
throw new Error('No identity found. Please create or import an identity first.');
|
|
195
|
+
}
|
|
196
|
+
const timestamp = Date.now();
|
|
197
|
+
const messageWithTimestamp = `${message}:${timestamp}`;
|
|
198
|
+
const signature = await SignatureService.sign(messageWithTimestamp);
|
|
199
|
+
return {
|
|
200
|
+
message,
|
|
201
|
+
signature,
|
|
202
|
+
publicKey,
|
|
203
|
+
timestamp,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Verify a signed message object
|
|
208
|
+
* Checks both signature validity and timestamp freshness
|
|
209
|
+
*/
|
|
210
|
+
static async verifySignedMessage(signedMessage, maxAgeMs = 5 * 60 * 1000 // 5 minutes default
|
|
211
|
+
) {
|
|
212
|
+
const { message, signature, publicKey, timestamp } = signedMessage;
|
|
213
|
+
// Check timestamp freshness
|
|
214
|
+
const now = Date.now();
|
|
215
|
+
if (now - timestamp > maxAgeMs) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
// Verify signature
|
|
219
|
+
const messageWithTimestamp = `${message}:${timestamp}`;
|
|
220
|
+
return SignatureService.verify(messageWithTimestamp, signature, publicKey);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Create a signed authentication challenge response
|
|
224
|
+
* Used for challenge-response authentication
|
|
225
|
+
*/
|
|
226
|
+
static async signChallenge(challenge) {
|
|
227
|
+
const publicKey = await keyManager_1.KeyManager.getPublicKey();
|
|
228
|
+
if (!publicKey) {
|
|
229
|
+
throw new Error('No identity found. Please create or import an identity first.');
|
|
230
|
+
}
|
|
231
|
+
const timestamp = Date.now();
|
|
232
|
+
const message = `auth:${publicKey}:${challenge}:${timestamp}`;
|
|
233
|
+
const signature = await SignatureService.sign(message);
|
|
234
|
+
return {
|
|
235
|
+
challenge: signature,
|
|
236
|
+
publicKey,
|
|
237
|
+
timestamp,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Verify a challenge response
|
|
242
|
+
*/
|
|
243
|
+
static async verifyChallengeResponse(originalChallenge, response, maxAgeMs = 5 * 60 * 1000) {
|
|
244
|
+
const { challenge: signature, publicKey, timestamp } = response;
|
|
245
|
+
// Check timestamp freshness
|
|
246
|
+
const now = Date.now();
|
|
247
|
+
if (now - timestamp > maxAgeMs) {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
const message = `auth:${publicKey}:${originalChallenge}:${timestamp}`;
|
|
251
|
+
return SignatureService.verify(message, signature, publicKey);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Create a registration signature
|
|
255
|
+
* Used when registering a new identity with the server
|
|
256
|
+
* Format matches server expectation: oxy:register:{publicKey}:{timestamp}
|
|
257
|
+
*/
|
|
258
|
+
static async createRegistrationSignature() {
|
|
259
|
+
const publicKey = await keyManager_1.KeyManager.getPublicKey();
|
|
260
|
+
if (!publicKey) {
|
|
261
|
+
throw new Error('No identity found. Please create or import an identity first.');
|
|
262
|
+
}
|
|
263
|
+
const timestamp = Date.now();
|
|
264
|
+
const message = `oxy:register:${publicKey}:${timestamp}`;
|
|
265
|
+
const signature = await SignatureService.sign(message);
|
|
266
|
+
return {
|
|
267
|
+
signature,
|
|
268
|
+
publicKey,
|
|
269
|
+
timestamp,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Sign arbitrary data for API requests
|
|
274
|
+
* Creates a canonical string representation and signs it
|
|
275
|
+
*/
|
|
276
|
+
static async signRequestData(data) {
|
|
277
|
+
const publicKey = await keyManager_1.KeyManager.getPublicKey();
|
|
278
|
+
if (!publicKey) {
|
|
279
|
+
throw new Error('No identity found. Please create or import an identity first.');
|
|
280
|
+
}
|
|
281
|
+
const timestamp = Date.now();
|
|
282
|
+
// Create canonical string representation
|
|
283
|
+
const sortedKeys = Object.keys(data).sort();
|
|
284
|
+
const canonicalParts = sortedKeys.map(key => `${key}:${JSON.stringify(data[key])}`);
|
|
285
|
+
const canonicalString = canonicalParts.join('|');
|
|
286
|
+
const message = `request:${publicKey}:${timestamp}:${canonicalString}`;
|
|
287
|
+
const signature = await SignatureService.sign(message);
|
|
288
|
+
return {
|
|
289
|
+
signature,
|
|
290
|
+
publicKey,
|
|
291
|
+
timestamp,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
exports.SignatureService = SignatureService;
|
|
296
|
+
exports.default = SignatureService;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.translate = translate;
|
|
4
|
+
exports.hasKey = hasKey;
|
|
5
|
+
// Use JSON locale files (RN Metro supports static requires reliably)
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
7
|
+
const enUS = require('./locales/en-US.json');
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
9
|
+
const esES = require('./locales/es-ES.json');
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
11
|
+
const caES = require('./locales/ca-ES.json');
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
13
|
+
const frFR = require('./locales/fr-FR.json');
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
15
|
+
const deDE = require('./locales/de-DE.json');
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
17
|
+
const itIT = require('./locales/it-IT.json');
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
19
|
+
const ptPT = require('./locales/pt-PT.json');
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
21
|
+
const jaJP = require('./locales/ja-JP.json');
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
23
|
+
const koKR = require('./locales/ko-KR.json');
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
25
|
+
const zhCN = require('./locales/zh-CN.json');
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
27
|
+
const arSA = require('./locales/ar-SA.json');
|
|
28
|
+
const DICTS = {
|
|
29
|
+
'en': enUS,
|
|
30
|
+
'en-US': enUS,
|
|
31
|
+
'es': esES,
|
|
32
|
+
'es-ES': esES,
|
|
33
|
+
'ca': caES,
|
|
34
|
+
'ca-ES': caES,
|
|
35
|
+
'fr': frFR,
|
|
36
|
+
'fr-FR': frFR,
|
|
37
|
+
'de': deDE,
|
|
38
|
+
'de-DE': deDE,
|
|
39
|
+
'it': itIT,
|
|
40
|
+
'it-IT': itIT,
|
|
41
|
+
'pt': ptPT,
|
|
42
|
+
'pt-PT': ptPT,
|
|
43
|
+
'ja': jaJP,
|
|
44
|
+
'ja-JP': jaJP,
|
|
45
|
+
'ko': koKR,
|
|
46
|
+
'ko-KR': koKR,
|
|
47
|
+
'zh': zhCN,
|
|
48
|
+
'zh-CN': zhCN,
|
|
49
|
+
'ar': arSA,
|
|
50
|
+
'ar-SA': arSA,
|
|
51
|
+
};
|
|
52
|
+
const FALLBACK = 'en-US';
|
|
53
|
+
function getNested(obj, path) {
|
|
54
|
+
return path.split('.').reduce((acc, key) => (acc && acc[key] != null ? acc[key] : undefined), obj);
|
|
55
|
+
}
|
|
56
|
+
function translate(locale, key, vars) {
|
|
57
|
+
const lang = locale && DICTS[locale] ? locale : FALLBACK;
|
|
58
|
+
const dict = DICTS[lang] || DICTS[FALLBACK];
|
|
59
|
+
let val = getNested(dict, key);
|
|
60
|
+
if (typeof val !== 'string')
|
|
61
|
+
return key; // fallback to key if missing
|
|
62
|
+
if (vars) {
|
|
63
|
+
Object.keys(vars).forEach(k => {
|
|
64
|
+
const token = `{{${k}}}`;
|
|
65
|
+
val = val.replaceAll(token, String(vars[k]));
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return val;
|
|
69
|
+
}
|
|
70
|
+
function hasKey(locale, key) {
|
|
71
|
+
const lang = locale && DICTS[locale] ? locale : FALLBACK;
|
|
72
|
+
return getNested(DICTS[lang], key) != null || getNested(DICTS[FALLBACK], key) != null;
|
|
73
|
+
}
|