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