@oxyhq/services 5.16.35 → 5.16.36

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 (228) hide show
  1. package/README.md +8 -26
  2. package/lib/commonjs/core/OxyServices.base.js.map +1 -1
  3. package/lib/commonjs/core/mixins/OxyServices.user.js.map +1 -1
  4. package/lib/commonjs/core/mixins/OxyServices.utility.js.map +1 -1
  5. package/lib/commonjs/crypto/README.md +142 -0
  6. package/lib/commonjs/crypto/core.js +147 -0
  7. package/lib/commonjs/crypto/core.js.map +1 -0
  8. package/lib/commonjs/crypto/index.js +16 -0
  9. package/lib/commonjs/crypto/index.js.map +1 -1
  10. package/lib/commonjs/crypto/keyManager.js +19 -22
  11. package/lib/commonjs/crypto/keyManager.js.map +1 -1
  12. package/lib/commonjs/crypto/signatureService.js +116 -28
  13. package/lib/commonjs/crypto/signatureService.js.map +1 -1
  14. package/lib/commonjs/index.js +0 -12
  15. package/lib/commonjs/index.js.map +1 -1
  16. package/lib/commonjs/models/interfaces.js +10 -11
  17. package/lib/commonjs/models/interfaces.js.map +1 -1
  18. package/lib/commonjs/node/index.js +10 -1
  19. package/lib/commonjs/node/index.js.map +1 -1
  20. package/lib/commonjs/node/signatureService.js +107 -0
  21. package/lib/commonjs/node/signatureService.js.map +1 -0
  22. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  23. package/lib/commonjs/ui/context/hooks/useAuthOperations.js.map +1 -1
  24. package/lib/commonjs/ui/context/hooks/useLanguageManagement.js.map +1 -1
  25. package/lib/commonjs/ui/hooks/useLanguageManagement.js.map +1 -1
  26. package/lib/commonjs/ui/hooks/useSessionManagement.js.map +1 -1
  27. package/lib/commonjs/ui/index.js +0 -2
  28. package/lib/commonjs/ui/index.js.map +1 -1
  29. package/lib/commonjs/ui/screens/AccountSettingsScreen.js.map +1 -1
  30. package/lib/commonjs/ui/screens/OxyAuthScreen.js +11 -2
  31. package/lib/commonjs/ui/screens/OxyAuthScreen.js.map +1 -1
  32. package/lib/module/core/OxyServices.base.js.map +1 -1
  33. package/lib/module/core/mixins/OxyServices.user.js.map +1 -1
  34. package/lib/module/core/mixins/OxyServices.utility.js.map +1 -1
  35. package/lib/module/crypto/README.md +142 -0
  36. package/lib/module/crypto/core.js +133 -0
  37. package/lib/module/crypto/core.js.map +1 -0
  38. package/lib/module/crypto/index.js +3 -9
  39. package/lib/module/crypto/index.js.map +1 -1
  40. package/lib/module/crypto/keyManager.js +19 -22
  41. package/lib/module/crypto/keyManager.js.map +1 -1
  42. package/lib/module/crypto/signatureService.js +113 -23
  43. package/lib/module/crypto/signatureService.js.map +1 -1
  44. package/lib/module/index.js +0 -2
  45. package/lib/module/index.js.map +1 -1
  46. package/lib/module/models/interfaces.js +10 -11
  47. package/lib/module/models/interfaces.js.map +1 -1
  48. package/lib/module/node/index.js +3 -0
  49. package/lib/module/node/index.js.map +1 -1
  50. package/lib/module/node/signatureService.js +101 -0
  51. package/lib/module/node/signatureService.js.map +1 -0
  52. package/lib/module/ui/context/OxyContext.js.map +1 -1
  53. package/lib/module/ui/context/hooks/useAuthOperations.js.map +1 -1
  54. package/lib/module/ui/context/hooks/useLanguageManagement.js.map +1 -1
  55. package/lib/module/ui/hooks/useLanguageManagement.js.map +1 -1
  56. package/lib/module/ui/hooks/useSessionManagement.js.map +1 -1
  57. package/lib/module/ui/index.js +0 -1
  58. package/lib/module/ui/index.js.map +1 -1
  59. package/lib/module/ui/screens/AccountSettingsScreen.js.map +1 -1
  60. package/lib/module/ui/screens/OxyAuthScreen.js +11 -2
  61. package/lib/module/ui/screens/OxyAuthScreen.js.map +1 -1
  62. package/lib/typescript/core/OxyServices.base.d.ts.map +1 -1
  63. package/lib/typescript/core/mixins/OxyServices.analytics.d.ts.map +1 -1
  64. package/lib/typescript/core/mixins/OxyServices.assets.d.ts.map +1 -1
  65. package/lib/typescript/core/mixins/OxyServices.auth.d.ts +1 -1
  66. package/lib/typescript/core/mixins/OxyServices.auth.d.ts.map +1 -1
  67. package/lib/typescript/core/mixins/OxyServices.developer.d.ts.map +1 -1
  68. package/lib/typescript/core/mixins/OxyServices.devices.d.ts.map +1 -1
  69. package/lib/typescript/core/mixins/OxyServices.karma.d.ts.map +1 -1
  70. package/lib/typescript/core/mixins/OxyServices.language.d.ts.map +1 -1
  71. package/lib/typescript/core/mixins/OxyServices.location.d.ts.map +1 -1
  72. package/lib/typescript/core/mixins/OxyServices.payment.d.ts.map +1 -1
  73. package/lib/typescript/core/mixins/OxyServices.privacy.d.ts.map +1 -1
  74. package/lib/typescript/core/mixins/OxyServices.security.d.ts.map +1 -1
  75. package/lib/typescript/core/mixins/OxyServices.user.d.ts +1 -2
  76. package/lib/typescript/core/mixins/OxyServices.user.d.ts.map +1 -1
  77. package/lib/typescript/core/mixins/OxyServices.utility.d.ts.map +1 -1
  78. package/lib/typescript/core/mixins/index.d.ts +13 -13
  79. package/lib/typescript/core/mixins/index.d.ts.map +1 -1
  80. package/lib/typescript/core/services/SessionService.d.ts +1 -1
  81. package/lib/typescript/core/services/SessionService.d.ts.map +1 -1
  82. package/lib/typescript/crypto/core.d.ts +56 -0
  83. package/lib/typescript/crypto/core.d.ts.map +1 -0
  84. package/lib/typescript/crypto/index.d.ts +1 -9
  85. package/lib/typescript/crypto/index.d.ts.map +1 -1
  86. package/lib/typescript/crypto/keyManager.d.ts +13 -1
  87. package/lib/typescript/crypto/keyManager.d.ts.map +1 -1
  88. package/lib/typescript/crypto/signatureService.d.ts +15 -9
  89. package/lib/typescript/crypto/signatureService.d.ts.map +1 -1
  90. package/lib/typescript/index.d.ts +1 -2
  91. package/lib/typescript/index.d.ts.map +1 -1
  92. package/lib/typescript/models/interfaces.d.ts +68 -15
  93. package/lib/typescript/models/interfaces.d.ts.map +1 -1
  94. package/lib/typescript/node/index.d.ts +1 -0
  95. package/lib/typescript/node/index.d.ts.map +1 -1
  96. package/lib/typescript/node/signatureService.d.ts +55 -0
  97. package/lib/typescript/node/signatureService.d.ts.map +1 -0
  98. package/lib/typescript/ui/context/OxyContext.d.ts +1 -2
  99. package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
  100. package/lib/typescript/ui/context/hooks/useAuthOperations.d.ts +1 -2
  101. package/lib/typescript/ui/context/hooks/useAuthOperations.d.ts.map +1 -1
  102. package/lib/typescript/ui/context/hooks/useLanguageManagement.d.ts +1 -2
  103. package/lib/typescript/ui/context/hooks/useLanguageManagement.d.ts.map +1 -1
  104. package/lib/typescript/ui/hooks/mutations/useAccountMutations.d.ts +1 -1
  105. package/lib/typescript/ui/hooks/mutations/useAccountMutations.d.ts.map +1 -1
  106. package/lib/typescript/ui/hooks/queries/useAccountQueries.d.ts +1 -1
  107. package/lib/typescript/ui/hooks/queries/useAccountQueries.d.ts.map +1 -1
  108. package/lib/typescript/ui/hooks/useLanguageManagement.d.ts +1 -2
  109. package/lib/typescript/ui/hooks/useLanguageManagement.d.ts.map +1 -1
  110. package/lib/typescript/ui/hooks/useSessionManagement.d.ts +1 -2
  111. package/lib/typescript/ui/hooks/useSessionManagement.d.ts.map +1 -1
  112. package/lib/typescript/ui/index.d.ts +1 -1
  113. package/lib/typescript/ui/index.d.ts.map +1 -1
  114. package/lib/typescript/ui/screens/OxyAuthScreen.d.ts.map +1 -1
  115. package/lib/typescript/ui/stores/authStore.d.ts +1 -1
  116. package/lib/typescript/ui/stores/authStore.d.ts.map +1 -1
  117. package/lib/typescript/ui/utils/avatarUtils.d.ts +1 -1
  118. package/lib/typescript/ui/utils/avatarUtils.d.ts.map +1 -1
  119. package/package.json +6 -1
  120. package/src/core/OxyServices.base.ts +1 -2
  121. package/src/core/mixins/OxyServices.auth.ts +1 -1
  122. package/src/core/mixins/OxyServices.user.ts +1 -2
  123. package/src/core/mixins/OxyServices.utility.ts +1 -2
  124. package/src/core/services/SessionService.ts +1 -1
  125. package/src/crypto/README.md +142 -0
  126. package/src/crypto/__tests__/core.test.ts +203 -0
  127. package/src/crypto/core.ts +142 -0
  128. package/src/crypto/index.ts +3 -10
  129. package/src/crypto/keyManager.ts +25 -21
  130. package/src/crypto/signatureService.ts +137 -36
  131. package/src/index.ts +2 -3
  132. package/src/models/interfaces.ts +73 -21
  133. package/src/node/index.ts +3 -0
  134. package/src/node/signatureService.ts +126 -0
  135. package/src/ui/context/OxyContext.tsx +1 -2
  136. package/src/ui/context/hooks/useAuthOperations.ts +1 -2
  137. package/src/ui/context/hooks/useLanguageManagement.ts +1 -2
  138. package/src/ui/hooks/auth/index.ts +2 -0
  139. package/src/ui/hooks/mutations/useAccountMutations.ts +1 -1
  140. package/src/ui/hooks/mutations/useServicesMutations.ts +1 -1
  141. package/src/ui/hooks/queries/useAccountQueries.ts +1 -1
  142. package/src/ui/hooks/useLanguageManagement.ts +1 -2
  143. package/src/ui/hooks/useSessionManagement.ts +1 -2
  144. package/src/ui/index.ts +1 -2
  145. package/src/ui/screens/AccountSettingsScreen.tsx +6 -6
  146. package/src/ui/screens/AccountSwitcherScreen.tsx +1 -1
  147. package/src/ui/screens/OxyAuthScreen.tsx +11 -2
  148. package/src/ui/screens/ProfileScreen.tsx +1 -1
  149. package/src/ui/stores/authStore.ts +1 -1
  150. package/src/ui/types/navigation.ts +1 -1
  151. package/src/ui/utils/avatarUtils.ts +1 -1
  152. package/lib/commonjs/core/services/AuthService.js +0 -156
  153. package/lib/commonjs/core/services/AuthService.js.map +0 -1
  154. package/lib/commonjs/core/services/SessionTransportService.js +0 -64
  155. package/lib/commonjs/core/services/SessionTransportService.js.map +0 -1
  156. package/lib/commonjs/core/services/UserService.js +0 -123
  157. package/lib/commonjs/core/services/UserService.js.map +0 -1
  158. package/lib/commonjs/core/services/index.js +0 -34
  159. package/lib/commonjs/core/services/index.js.map +0 -1
  160. package/lib/commonjs/shared/crypto/messageBuilders.js +0 -79
  161. package/lib/commonjs/shared/crypto/messageBuilders.js.map +0 -1
  162. package/lib/commonjs/shared/crypto/platform.js +0 -118
  163. package/lib/commonjs/shared/crypto/platform.js.map +0 -1
  164. package/lib/commonjs/shared/crypto/signature.js +0 -191
  165. package/lib/commonjs/shared/crypto/signature.js.map +0 -1
  166. package/lib/commonjs/shared/index.js +0 -94
  167. package/lib/commonjs/shared/index.js.map +0 -1
  168. package/lib/commonjs/shared/models/index.js +0 -2
  169. package/lib/commonjs/shared/models/index.js.map +0 -1
  170. package/lib/commonjs/shared/transport/index.js +0 -260
  171. package/lib/commonjs/shared/transport/index.js.map +0 -1
  172. package/lib/commonjs/shared/utils/index.js +0 -82
  173. package/lib/commonjs/shared/utils/index.js.map +0 -1
  174. package/lib/module/core/services/AuthService.js +0 -151
  175. package/lib/module/core/services/AuthService.js.map +0 -1
  176. package/lib/module/core/services/SessionTransportService.js +0 -59
  177. package/lib/module/core/services/SessionTransportService.js.map +0 -1
  178. package/lib/module/core/services/UserService.js +0 -118
  179. package/lib/module/core/services/UserService.js.map +0 -1
  180. package/lib/module/core/services/index.js +0 -16
  181. package/lib/module/core/services/index.js.map +0 -1
  182. package/lib/module/shared/crypto/messageBuilders.js +0 -70
  183. package/lib/module/shared/crypto/messageBuilders.js.map +0 -1
  184. package/lib/module/shared/crypto/platform.js +0 -112
  185. package/lib/module/shared/crypto/platform.js.map +0 -1
  186. package/lib/module/shared/crypto/signature.js +0 -186
  187. package/lib/module/shared/crypto/signature.js.map +0 -1
  188. package/lib/module/shared/index.js +0 -30
  189. package/lib/module/shared/index.js.map +0 -1
  190. package/lib/module/shared/models/index.js +0 -2
  191. package/lib/module/shared/models/index.js.map +0 -1
  192. package/lib/module/shared/transport/index.js +0 -254
  193. package/lib/module/shared/transport/index.js.map +0 -1
  194. package/lib/module/shared/utils/index.js +0 -74
  195. package/lib/module/shared/utils/index.js.map +0 -1
  196. package/lib/typescript/core/services/AuthService.d.ts +0 -50
  197. package/lib/typescript/core/services/AuthService.d.ts.map +0 -1
  198. package/lib/typescript/core/services/SessionTransportService.d.ts +0 -31
  199. package/lib/typescript/core/services/SessionTransportService.d.ts.map +0 -1
  200. package/lib/typescript/core/services/UserService.d.ts +0 -39
  201. package/lib/typescript/core/services/UserService.d.ts.map +0 -1
  202. package/lib/typescript/core/services/index.d.ts +0 -13
  203. package/lib/typescript/core/services/index.d.ts.map +0 -1
  204. package/lib/typescript/shared/crypto/messageBuilders.d.ts +0 -38
  205. package/lib/typescript/shared/crypto/messageBuilders.d.ts.map +0 -1
  206. package/lib/typescript/shared/crypto/platform.d.ts +0 -54
  207. package/lib/typescript/shared/crypto/platform.d.ts.map +0 -1
  208. package/lib/typescript/shared/crypto/signature.d.ts +0 -72
  209. package/lib/typescript/shared/crypto/signature.d.ts.map +0 -1
  210. package/lib/typescript/shared/index.d.ts +0 -20
  211. package/lib/typescript/shared/index.d.ts.map +0 -1
  212. package/lib/typescript/shared/models/index.d.ts +0 -163
  213. package/lib/typescript/shared/models/index.d.ts.map +0 -1
  214. package/lib/typescript/shared/transport/index.d.ts +0 -73
  215. package/lib/typescript/shared/transport/index.d.ts.map +0 -1
  216. package/lib/typescript/shared/utils/index.d.ts +0 -28
  217. package/lib/typescript/shared/utils/index.d.ts.map +0 -1
  218. package/src/core/services/AuthService.ts +0 -153
  219. package/src/core/services/SessionTransportService.ts +0 -69
  220. package/src/core/services/UserService.ts +0 -125
  221. package/src/core/services/index.ts +0 -14
  222. package/src/shared/crypto/messageBuilders.ts +0 -89
  223. package/src/shared/crypto/platform.ts +0 -140
  224. package/src/shared/crypto/signature.ts +0 -235
  225. package/src/shared/index.ts +0 -28
  226. package/src/shared/models/index.ts +0 -173
  227. package/src/shared/transport/index.ts +0 -349
  228. package/src/shared/utils/index.ts +0 -73
@@ -0,0 +1,142 @@
1
+ # Oxy Crypto Module
2
+
3
+ This module provides cryptographic operations for the Oxy ecosystem, supporting both React Native and Node.js environments.
4
+
5
+ ## Architecture
6
+
7
+ The crypto module is organized into several layers:
8
+
9
+ ### Core Layer (`core.ts`)
10
+ Platform-agnostic cryptographic functions that work everywhere:
11
+ - Signature verification using elliptic curve cryptography
12
+ - Public/private key validation
13
+ - Message formatting (auth, registration, requests)
14
+ - Utility functions (shortenPublicKey, derivePublicKey, etc.)
15
+
16
+ ### Platform-Specific Implementations
17
+
18
+ #### React Native (`signatureService.ts`, `keyManager.ts`)
19
+ - **KeyManager**: Manages ECDSA key pairs with secure storage via Expo SecureStore
20
+ - ⚠️ **For Oxy Accounts app only** - Not intended for third-party apps
21
+ - Handles key generation, import, backup, and secure retrieval
22
+ - Private keys never leave the device
23
+
24
+ - **SignatureService**: Provides async signing and verification
25
+ - Uses `expo-crypto` for hashing in React Native
26
+ - Falls back to Node.js crypto when available
27
+ - Suitable for both React Native and Node.js environments
28
+
29
+ #### Node.js (`node/signatureService.ts`)
30
+ - Optimized synchronous signature operations for backend
31
+ - Uses Node's `crypto` module directly for better performance
32
+ - Exports same API as React Native version for consistency
33
+
34
+ ## Usage
35
+
36
+ ### In React Native / Accounts App
37
+
38
+ ```typescript
39
+ import { KeyManager, SignatureService } from '@oxyhq/services/crypto';
40
+
41
+ // Create identity (Accounts app only)
42
+ const publicKey = await KeyManager.createIdentity();
43
+
44
+ // Sign a challenge
45
+ const signature = await SignatureService.sign(message);
46
+
47
+ // Verify a signature
48
+ const isValid = await SignatureService.verify(message, signature, publicKey);
49
+ ```
50
+
51
+ ### In Node.js Backend
52
+
53
+ ```typescript
54
+ import { SignatureService } from '@oxyhq/services/node';
55
+
56
+ // Generate challenge
57
+ const challenge = SignatureService.generateChallenge();
58
+
59
+ // Verify signature (synchronous)
60
+ const isValid = SignatureService.verifyChallengeResponse(
61
+ publicKey,
62
+ challenge,
63
+ signature,
64
+ timestamp
65
+ );
66
+ ```
67
+
68
+ ### In Third-Party Apps (Services SDK)
69
+
70
+ Third-party apps should **not** use KeyManager directly. Instead, use the authentication flows provided by the OxyServices class:
71
+
72
+ ```typescript
73
+ import { OxyServices } from '@oxyhq/services';
74
+
75
+ const oxy = new OxyServices({ baseURL: 'https://api.oxy.so' });
76
+
77
+ // Request authentication (shows QR code / deep link)
78
+ // User approves in Oxy Accounts app
79
+ // Returns session when approved
80
+ const session = await oxy.requestAuth({ appId: 'my-app' });
81
+ ```
82
+
83
+ ## Security Considerations
84
+
85
+ ### Message Formats
86
+ All signed messages follow specific formats to prevent replay attacks:
87
+
88
+ - **Authentication**: `auth:{publicKey}:{challenge}:{timestamp}`
89
+ - **Registration**: `oxy:register:{publicKey}:{timestamp}`
90
+ - **API Requests**: `request:{publicKey}:{timestamp}:{canonicalData}`
91
+
92
+ Timestamps must be within 5 minutes to be valid.
93
+
94
+ ### Key Storage
95
+ - Private keys are stored in device secure storage (iOS Keychain, Android Keystore)
96
+ - Never transmitted or exposed outside the device
97
+ - Only the Oxy Accounts app has access to private keys
98
+
99
+ ### Backup Encryption
100
+ ⚠️ **Current Implementation**: The backup encryption currently uses a custom XOR scheme with key stretching. This is functional but not optimal.
101
+
102
+ 📋 **Planned Improvement**: Migrate to standard AES-256-GCM encryption for better security and interoperability.
103
+
104
+ ## Separation of Concerns
105
+
106
+ ### Oxy Accounts App
107
+ - Owns and manages the user's private key
108
+ - Handles identity creation, backup, and recovery
109
+ - Signs authentication challenges
110
+ - Uses `KeyManager` and `SignatureService`
111
+
112
+ ### Services SDK / Third-Party Apps
113
+ - Does NOT generate or store private keys
114
+ - Displays QR codes and deep links for authentication
115
+ - Polls server for authentication status
116
+ - Uses `OxyServices` class for API communication
117
+
118
+ ### API Backend
119
+ - Generates challenges
120
+ - Verifies signatures using public keys
121
+ - Manages sessions and user data
122
+ - Uses `SignatureService` from `@oxyhq/services/node`
123
+
124
+ ## Migration from Old Signature Service
125
+
126
+ The API previously had a duplicate `signature.service.ts` file. This has been replaced with a re-export from `@oxyhq/services/node` to ensure consistency and eliminate duplication.
127
+
128
+ If you're maintaining code that imports from the old location, it will continue to work as the file now re-exports from the shared module.
129
+
130
+ ## Testing
131
+
132
+ When making changes to crypto code:
133
+ 1. Test in both React Native and Node.js environments
134
+ 2. Verify signatures created in one environment can be verified in another
135
+ 3. Check that timestamps are properly validated
136
+ 4. Ensure message formats match exactly between client and server
137
+
138
+ ## Further Reading
139
+
140
+ - [Public Key Authentication Guide](../docs/PUBLIC_KEY_AUTHENTICATION.md)
141
+ - [ECDSA on secp256k1](https://en.bitcoin.it/wiki/Secp256k1)
142
+ - [Expo SecureStore Documentation](https://docs.expo.dev/versions/latest/sdk/securestore/)
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Tests for the shared crypto core module
3
+ * These tests verify that signature verification is consistent across platforms
4
+ */
5
+
6
+ import {
7
+ verifySignatureCore,
8
+ isValidPublicKey,
9
+ isValidPrivateKey,
10
+ isTimestampFresh,
11
+ buildAuthMessage,
12
+ buildRegistrationMessage,
13
+ buildRequestMessage,
14
+ shortenPublicKey,
15
+ derivePublicKey,
16
+ getEllipticCurve,
17
+ CHALLENGE_TTL_MS,
18
+ MAX_SIGNATURE_AGE_MS,
19
+ } from '../core';
20
+
21
+ describe('Crypto Core Module', () => {
22
+ // Test key pair (for testing only - never use in production)
23
+ const testPrivateKey = '1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef';
24
+ // Public key derived from the private key above
25
+ const testPublicKey = '04bb50e2d89a4ed70663d080659fe0ad4b9bc3e06c17a227433966cb59ceee020decddbf6e00192011648d13b1c00af770c0c1bb609d4d3a5c98a43772e0e18ef4';
26
+
27
+ describe('Public/Private Key Validation', () => {
28
+ it('should validate correct public keys', () => {
29
+ expect(isValidPublicKey(testPublicKey)).toBe(true);
30
+ });
31
+
32
+ it('should reject invalid public keys', () => {
33
+ expect(isValidPublicKey('invalid')).toBe(false);
34
+ expect(isValidPublicKey('')).toBe(false);
35
+ expect(isValidPublicKey('1234')).toBe(false);
36
+ });
37
+
38
+ it('should validate correct private keys', () => {
39
+ expect(isValidPrivateKey(testPrivateKey)).toBe(true);
40
+ });
41
+
42
+ it('should reject invalid private keys', () => {
43
+ expect(isValidPrivateKey('invalid')).toBe(false);
44
+ expect(isValidPrivateKey('')).toBe(false);
45
+ expect(isValidPrivateKey('1234')).toBe(false);
46
+ });
47
+
48
+ it('should derive public key from private key', () => {
49
+ const derived = derivePublicKey(testPrivateKey);
50
+ expect(derived).toBe(testPublicKey);
51
+ });
52
+ });
53
+
54
+ describe('Utility Functions', () => {
55
+ it('should shorten public keys correctly', () => {
56
+ const shortened = shortenPublicKey(testPublicKey);
57
+ expect(shortened).toBe('04bb50e2...e0e18ef4');
58
+ expect(shortened.length).toBeLessThan(testPublicKey.length);
59
+ });
60
+
61
+ it('should not shorten already short keys', () => {
62
+ const shortKey = '1234567890';
63
+ expect(shortenPublicKey(shortKey)).toBe(shortKey);
64
+ });
65
+ });
66
+
67
+ describe('Timestamp Validation', () => {
68
+ it('should accept fresh timestamps', () => {
69
+ const now = Date.now();
70
+ expect(isTimestampFresh(now)).toBe(true);
71
+ expect(isTimestampFresh(now - 1000)).toBe(true); // 1 second ago
72
+ });
73
+
74
+ it('should reject old timestamps', () => {
75
+ const old = Date.now() - (MAX_SIGNATURE_AGE_MS + 1000);
76
+ expect(isTimestampFresh(old)).toBe(false);
77
+ });
78
+
79
+ it('should respect custom max age', () => {
80
+ const timestamp = Date.now() - 10000; // 10 seconds ago
81
+ expect(isTimestampFresh(timestamp, 5000)).toBe(false); // max 5 seconds
82
+ expect(isTimestampFresh(timestamp, 15000)).toBe(true); // max 15 seconds
83
+ });
84
+ });
85
+
86
+ describe('Message Building', () => {
87
+ it('should build auth messages correctly', () => {
88
+ const publicKey = 'abc123';
89
+ const challenge = 'challenge456';
90
+ const timestamp = 1234567890;
91
+
92
+ const message = buildAuthMessage(publicKey, challenge, timestamp);
93
+ expect(message).toBe('auth:abc123:challenge456:1234567890');
94
+ });
95
+
96
+ it('should build registration messages correctly', () => {
97
+ const publicKey = 'abc123';
98
+ const timestamp = 1234567890;
99
+
100
+ const message = buildRegistrationMessage(publicKey, timestamp);
101
+ expect(message).toBe('oxy:register:abc123:1234567890');
102
+ });
103
+
104
+ it('should build request messages with canonical data', () => {
105
+ const publicKey = 'abc123';
106
+ const timestamp = 1234567890;
107
+ const data = {
108
+ username: 'testuser',
109
+ action: 'update',
110
+ id: 42,
111
+ };
112
+
113
+ const message = buildRequestMessage(publicKey, timestamp, data);
114
+
115
+ // Keys should be sorted alphabetically
116
+ expect(message).toContain('action:"update"');
117
+ expect(message).toContain('id:42');
118
+ expect(message).toContain('username:"testuser"');
119
+ expect(message).toContain('request:abc123:1234567890:');
120
+ });
121
+
122
+ it('should produce consistent canonical strings', () => {
123
+ const publicKey = 'key';
124
+ const timestamp = 1000;
125
+
126
+ // Same data, different order
127
+ const data1 = { b: 2, a: 1, c: 3 };
128
+ const data2 = { c: 3, a: 1, b: 2 };
129
+
130
+ const message1 = buildRequestMessage(publicKey, timestamp, data1);
131
+ const message2 = buildRequestMessage(publicKey, timestamp, data2);
132
+
133
+ expect(message1).toBe(message2);
134
+ });
135
+ });
136
+
137
+ describe('Signature Verification Core', () => {
138
+ it('should verify valid signatures', () => {
139
+ const ec = getEllipticCurve();
140
+ const keyPair = ec.keyFromPrivate(testPrivateKey);
141
+
142
+ // Create a test message hash
143
+ const messageHash = 'abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890';
144
+
145
+ // Sign it
146
+ const signature = keyPair.sign(messageHash);
147
+ const signatureHex = signature.toDER('hex');
148
+
149
+ // Verify it
150
+ const isValid = verifySignatureCore(messageHash, signatureHex, testPublicKey);
151
+ expect(isValid).toBe(true);
152
+ });
153
+
154
+ it('should reject invalid signatures', () => {
155
+ const messageHash = 'abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890';
156
+ const invalidSignature = '1234567890abcdef';
157
+
158
+ const isValid = verifySignatureCore(messageHash, invalidSignature, testPublicKey);
159
+ expect(isValid).toBe(false);
160
+ });
161
+
162
+ it('should reject signatures with wrong public key', () => {
163
+ const ec = getEllipticCurve();
164
+ const keyPair = ec.keyFromPrivate(testPrivateKey);
165
+
166
+ const messageHash = 'abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890';
167
+ const signature = keyPair.sign(messageHash);
168
+ const signatureHex = signature.toDER('hex');
169
+
170
+ // Use a different public key
171
+ const wrongPublicKey = '04' + 'a'.repeat(128);
172
+
173
+ const isValid = verifySignatureCore(messageHash, signatureHex, wrongPublicKey);
174
+ expect(isValid).toBe(false);
175
+ });
176
+ });
177
+
178
+ describe('Constants', () => {
179
+ it('should export correct TTL constants', () => {
180
+ expect(CHALLENGE_TTL_MS).toBe(5 * 60 * 1000); // 5 minutes
181
+ expect(MAX_SIGNATURE_AGE_MS).toBe(5 * 60 * 1000); // 5 minutes
182
+ });
183
+ });
184
+
185
+ describe('Elliptic Curve', () => {
186
+ it('should provide secp256k1 curve', () => {
187
+ const ec = getEllipticCurve();
188
+ expect(ec).toBeDefined();
189
+ expect(ec.curve.type).toBe('short');
190
+ });
191
+
192
+ it('should generate valid key pairs', () => {
193
+ const ec = getEllipticCurve();
194
+ const keyPair = ec.genKeyPair();
195
+
196
+ const privateKey = keyPair.getPrivate('hex');
197
+ const publicKey = keyPair.getPublic('hex');
198
+
199
+ expect(isValidPrivateKey(privateKey)).toBe(true);
200
+ expect(isValidPublicKey(publicKey)).toBe(true);
201
+ });
202
+ });
203
+ });
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Core Cryptographic Functions - Platform Agnostic
3
+ *
4
+ * This module contains the core signature verification logic
5
+ * that is shared between all platforms (React Native, Node.js, Web).
6
+ * Platform-specific implementations (hashing, random generation) are injected.
7
+ */
8
+
9
+ import { ec as EC } from 'elliptic';
10
+ import type { EC as ECType } from 'elliptic';
11
+
12
+ const ec = new EC('secp256k1');
13
+
14
+ // Constants for signature validation
15
+ export const CHALLENGE_TTL_MS = 5 * 60 * 1000; // 5 minutes
16
+ export const MAX_SIGNATURE_AGE_MS = 5 * 60 * 1000; // 5 minutes
17
+
18
+ /**
19
+ * Core signature verification using elliptic curve
20
+ * This is platform-agnostic and works everywhere
21
+ */
22
+ export function verifySignatureCore(
23
+ messageHash: string,
24
+ signature: string,
25
+ publicKey: string
26
+ ): boolean {
27
+ try {
28
+ const key = ec.keyFromPublic(publicKey, 'hex');
29
+ return key.verify(messageHash, signature);
30
+ } catch {
31
+ return false;
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Validate that a string is a valid public key
37
+ */
38
+ export function isValidPublicKey(publicKey: string): boolean {
39
+ // Reject empty strings
40
+ if (!publicKey || publicKey.trim().length === 0) {
41
+ return false;
42
+ }
43
+
44
+ try {
45
+ ec.keyFromPublic(publicKey, 'hex');
46
+ return true;
47
+ } catch {
48
+ return false;
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Validate that a string is a valid private key
54
+ */
55
+ export function isValidPrivateKey(privateKey: string): boolean {
56
+ // Reject empty strings
57
+ if (!privateKey || privateKey.trim().length === 0) {
58
+ return false;
59
+ }
60
+
61
+ // Private keys must be 64 hex characters (32 bytes)
62
+ if (!/^[0-9a-fA-F]{64}$/.test(privateKey)) {
63
+ return false;
64
+ }
65
+
66
+ try {
67
+ const keyPair = ec.keyFromPrivate(privateKey);
68
+ // Verify it can derive a public key and the key is valid
69
+ keyPair.getPublic('hex');
70
+ // Check that the private key is not zero (which would be invalid)
71
+ const priv = keyPair.getPrivate();
72
+ if (!priv || priv.isZero()) {
73
+ return false;
74
+ }
75
+ return true;
76
+ } catch {
77
+ return false;
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Get a shortened display version of a public key
83
+ * Format: first 8 chars...last 8 chars
84
+ */
85
+ export function shortenPublicKey(publicKey: string): string {
86
+ if (publicKey.length <= 20) return publicKey;
87
+ return `${publicKey.slice(0, 8)}...${publicKey.slice(-8)}`;
88
+ }
89
+
90
+ /**
91
+ * Derive public key from a private key (without storing)
92
+ */
93
+ export function derivePublicKey(privateKey: string): string {
94
+ const keyPair = ec.keyFromPrivate(privateKey);
95
+ return keyPair.getPublic('hex');
96
+ }
97
+
98
+ /**
99
+ * Check timestamp freshness
100
+ */
101
+ export function isTimestampFresh(timestamp: number, maxAgeMs: number = MAX_SIGNATURE_AGE_MS): boolean {
102
+ const now = Date.now();
103
+ return (now - timestamp) <= maxAgeMs;
104
+ }
105
+
106
+ /**
107
+ * Build authentication challenge message
108
+ * Format: auth:{publicKey}:{challenge}:{timestamp}
109
+ */
110
+ export function buildAuthMessage(publicKey: string, challenge: string, timestamp: number): string {
111
+ return `auth:${publicKey}:${challenge}:${timestamp}`;
112
+ }
113
+
114
+ /**
115
+ * Build registration message
116
+ * Format: oxy:register:{publicKey}:{timestamp}
117
+ */
118
+ export function buildRegistrationMessage(publicKey: string, timestamp: number): string {
119
+ return `oxy:register:${publicKey}:${timestamp}`;
120
+ }
121
+
122
+ /**
123
+ * Build request signature message
124
+ * Format: request:{publicKey}:{timestamp}:{canonicalString}
125
+ */
126
+ export function buildRequestMessage(
127
+ publicKey: string,
128
+ timestamp: number,
129
+ data: Record<string, unknown>
130
+ ): string {
131
+ const sortedKeys = Object.keys(data).sort();
132
+ const canonicalParts = sortedKeys.map(key => `${key}:${JSON.stringify(data[key])}`);
133
+ const canonicalString = canonicalParts.join('|');
134
+ return `request:${publicKey}:${timestamp}:${canonicalString}`;
135
+ }
136
+
137
+ /**
138
+ * Get the elliptic curve instance (for key generation)
139
+ */
140
+ export function getEllipticCurve(): ECType {
141
+ return ec;
142
+ }
@@ -3,15 +3,6 @@
3
3
  *
4
4
  * Provides cryptographic identity management for the Oxy ecosystem.
5
5
  * Handles key generation, secure storage, and digital signatures.
6
- *
7
- * ⚠️ IMPORTANT: KeyManager is primarily intended for use in the Oxy Accounts app.
8
- * While it's exported here for convenience, third-party applications should NOT
9
- * generate or store user identities. The Accounts app is the identity wallet.
10
- *
11
- * For third-party apps:
12
- * - Use SignatureService for signing operations (but keys come from Accounts app)
13
- * - Do NOT call KeyManager.createIdentity() or KeyManager.importKeyPair()
14
- * - The Oxy Accounts app handles all identity generation and key storage
15
6
  */
16
7
 
17
8
  // Import polyfills first - this ensures Buffer is available for crypto libraries
@@ -25,7 +16,9 @@ export {
25
16
  } from './signatureService';
26
17
  export { type BackupData } from './types';
27
18
 
19
+ // Export core crypto utilities (shared across platforms)
20
+ export * from './core';
21
+
28
22
  // Re-export for convenience
29
23
  export { KeyManager as default } from './keyManager';
30
24
 
31
-
@@ -1,20 +1,38 @@
1
1
  /**
2
2
  * Key Manager - ECDSA secp256k1 Key Generation and Storage
3
3
  *
4
- * Handles secure generation, storage, and retrieval of cryptographic keys.
4
+ * ⚠️ **FOR OXY ACCOUNTS APP ONLY**
5
+ *
6
+ * This module handles secure generation, storage, and retrieval of cryptographic keys.
5
7
  * Private keys are stored securely using expo-secure-store and never leave the device.
8
+ *
9
+ * **IMPORTANT**: Third-party apps should NOT use KeyManager directly.
10
+ * Instead, use the OxyServices authentication flows which communicate with the
11
+ * Oxy Accounts app via deep links/QR codes to obtain user authorization.
12
+ *
13
+ * The Oxy Accounts app is the sole owner of the user's private key and identity.
14
+ * Other apps request authentication from the Accounts app, which signs challenges
15
+ * and returns authorization to the requesting app via the API.
16
+ *
17
+ * @see {@link https://github.com/OxyHQ/OxyHQServices/blob/main/packages/services/src/crypto/README.md|Crypto Module Documentation}
6
18
  */
7
19
 
8
20
  import { ec as EC } from 'elliptic';
9
21
  import type { ECKeyPair } from 'elliptic';
10
22
  import { Platform } from 'react-native';
11
- import { shortenPublicKey as sharedShortenPublicKey } from '../shared';
23
+ import {
24
+ isValidPublicKey as validatePublicKey,
25
+ isValidPrivateKey as validatePrivateKey,
26
+ derivePublicKey as derivePublicKeyFromPrivate,
27
+ shortenPublicKey as shortenKey,
28
+ getEllipticCurve,
29
+ } from './core';
12
30
 
13
31
  // Lazy imports for React Native specific modules
14
32
  let SecureStore: typeof import('expo-secure-store') | null = null;
15
33
  let ExpoCrypto: typeof import('expo-crypto') | null = null;
16
34
 
17
- const ec = new EC('secp256k1');
35
+ const ec = getEllipticCurve();
18
36
 
19
37
  const STORAGE_KEYS = {
20
38
  PRIVATE_KEY: 'oxy_identity_private_key',
@@ -505,34 +523,21 @@ export class KeyManager {
505
523
  * Derive public key from a private key (without storing)
506
524
  */
507
525
  static derivePublicKey(privateKey: string): string {
508
- const keyPair = ec.keyFromPrivate(privateKey);
509
- return keyPair.getPublic('hex');
526
+ return derivePublicKeyFromPrivate(privateKey);
510
527
  }
511
528
 
512
529
  /**
513
530
  * Validate that a string is a valid public key
514
531
  */
515
532
  static isValidPublicKey(publicKey: string): boolean {
516
- try {
517
- ec.keyFromPublic(publicKey, 'hex');
518
- return true;
519
- } catch {
520
- return false;
521
- }
533
+ return validatePublicKey(publicKey);
522
534
  }
523
535
 
524
536
  /**
525
537
  * Validate that a string is a valid private key
526
538
  */
527
539
  static isValidPrivateKey(privateKey: string): boolean {
528
- try {
529
- const keyPair = ec.keyFromPrivate(privateKey);
530
- // Verify it can derive a public key
531
- keyPair.getPublic('hex');
532
- return true;
533
- } catch {
534
- return false;
535
- }
540
+ return validatePrivateKey(privateKey);
536
541
  }
537
542
 
538
543
  /**
@@ -540,8 +545,7 @@ export class KeyManager {
540
545
  * Format: first 8 chars...last 8 chars
541
546
  */
542
547
  static shortenPublicKey(publicKey: string): string {
543
- // Use shared utility
544
- return sharedShortenPublicKey(publicKey);
548
+ return shortenKey(publicKey);
545
549
  }
546
550
  }
547
551