@oxyhq/services 5.17.8 → 5.17.10
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/lib/commonjs/crypto/index.js +0 -23
- package/lib/commonjs/crypto/index.js.map +1 -1
- package/lib/commonjs/index.js +0 -15
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/ui/components/Icon.js.map +1 -1
- package/lib/commonjs/ui/components/IconButton/utils.js.map +1 -1
- package/lib/commonjs/ui/components/TextField/Adornment/utils.js.map +1 -1
- package/lib/commonjs/ui/components/TextField/helpers.js.map +1 -1
- package/lib/commonjs/ui/components/TouchableRipple/utils.js.map +1 -1
- package/lib/commonjs/ui/components/Typography/AnimatedText.js.map +1 -1
- package/lib/commonjs/ui/context/OxyContext.js +20 -35
- package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
- package/lib/commonjs/ui/context/hooks/useAuthOperations.js +41 -118
- package/lib/commonjs/ui/context/hooks/useAuthOperations.js.map +1 -1
- package/lib/commonjs/ui/hooks/useSessionSocket.js +2 -26
- package/lib/commonjs/ui/hooks/useSessionSocket.js.map +1 -1
- package/lib/commonjs/ui/screens/OxyAuthScreen.js +0 -1
- package/lib/commonjs/ui/screens/OxyAuthScreen.js.map +1 -1
- package/lib/commonjs/ui/stores/authStore.js +33 -13
- package/lib/commonjs/ui/stores/authStore.js.map +1 -1
- package/lib/commonjs/ui/utils/avatarUtils.js +2 -32
- package/lib/commonjs/ui/utils/avatarUtils.js.map +1 -1
- package/lib/module/crypto/index.js +4 -6
- package/lib/module/crypto/index.js.map +1 -1
- package/lib/module/index.js +6 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/ui/components/Icon.js.map +1 -1
- package/lib/module/ui/components/IconButton/utils.js.map +1 -1
- package/lib/module/ui/components/TextField/Adornment/utils.js.map +1 -1
- package/lib/module/ui/components/TextField/helpers.js.map +1 -1
- package/lib/module/ui/components/TouchableRipple/utils.js.map +1 -1
- package/lib/module/ui/components/Typography/AnimatedText.js.map +1 -1
- package/lib/module/ui/context/OxyContext.js +20 -36
- package/lib/module/ui/context/OxyContext.js.map +1 -1
- package/lib/module/ui/context/hooks/useAuthOperations.js +41 -118
- package/lib/module/ui/context/hooks/useAuthOperations.js.map +1 -1
- package/lib/module/ui/hooks/useSessionSocket.js +2 -26
- package/lib/module/ui/hooks/useSessionSocket.js.map +1 -1
- package/lib/module/ui/screens/OxyAuthScreen.js +0 -1
- package/lib/module/ui/screens/OxyAuthScreen.js.map +1 -1
- package/lib/module/ui/stores/authStore.js +33 -13
- package/lib/module/ui/stores/authStore.js.map +1 -1
- package/lib/module/ui/utils/avatarUtils.js +2 -32
- package/lib/module/ui/utils/avatarUtils.js.map +1 -1
- package/lib/typescript/crypto/index.d.ts +2 -5
- package/lib/typescript/crypto/index.d.ts.map +1 -1
- package/lib/typescript/crypto/types.d.ts +2 -2
- package/lib/typescript/index.d.ts +4 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/ui/components/IconButton/utils.d.ts +1 -1
- package/lib/typescript/ui/components/TextField/Adornment/utils.d.ts +1 -1
- package/lib/typescript/ui/components/TextField/Adornment/utils.d.ts.map +1 -1
- package/lib/typescript/ui/components/TextField/helpers.d.ts +6 -6
- package/lib/typescript/ui/components/types.d.ts +0 -4
- package/lib/typescript/ui/components/types.d.ts.map +1 -1
- package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
- package/lib/typescript/ui/context/OxyContextBase.d.ts +2 -2
- package/lib/typescript/ui/context/OxyContextBase.d.ts.map +1 -1
- package/lib/typescript/ui/context/hooks/useAuthOperations.d.ts +2 -9
- package/lib/typescript/ui/context/hooks/useAuthOperations.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/useSessionSocket.d.ts.map +1 -1
- package/lib/typescript/ui/stores/authStore.d.ts +5 -3
- package/lib/typescript/ui/stores/authStore.d.ts.map +1 -1
- package/lib/typescript/ui/utils/avatarUtils.d.ts +0 -2
- package/lib/typescript/ui/utils/avatarUtils.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/crypto/index.ts +3 -11
- package/src/crypto/types.ts +2 -2
- package/src/index.ts +6 -11
- package/src/ui/components/Icon.tsx +1 -1
- package/src/ui/components/IconButton/utils.ts +1 -1
- package/src/ui/components/TextField/Adornment/utils.ts +2 -2
- package/src/ui/components/TextField/helpers.tsx +8 -8
- package/src/ui/components/TouchableRipple/utils.ts +2 -2
- package/src/ui/components/Typography/AnimatedText.tsx +2 -2
- package/src/ui/components/types.tsx +0 -6
- package/src/ui/context/OxyContext.tsx +22 -27
- package/src/ui/context/OxyContextBase.tsx +4 -4
- package/src/ui/context/hooks/useAuthOperations.ts +61 -140
- package/src/ui/hooks/useSessionSocket.ts +3 -21
- package/src/ui/screens/OxyAuthScreen.tsx +1 -1
- package/src/ui/stores/authStore.ts +39 -18
- package/src/ui/utils/avatarUtils.ts +4 -36
- package/lib/commonjs/crypto/keyManager.js +0 -356
- package/lib/commonjs/crypto/keyManager.js.map +0 -1
- package/lib/commonjs/crypto/signatureService.js +0 -269
- package/lib/commonjs/crypto/signatureService.js.map +0 -1
- package/lib/module/crypto/keyManager.js +0 -353
- package/lib/module/crypto/keyManager.js.map +0 -1
- package/lib/module/crypto/signatureService.js +0 -266
- package/lib/module/crypto/signatureService.js.map +0 -1
- package/lib/typescript/crypto/keyManager.d.ts +0 -80
- package/lib/typescript/crypto/keyManager.d.ts.map +0 -1
- package/lib/typescript/crypto/signatureService.d.ts +0 -77
- package/lib/typescript/crypto/signatureService.d.ts.map +0 -1
- package/src/crypto/keyManager.ts +0 -379
- package/src/crypto/signatureService.ts +0 -301
package/src/crypto/keyManager.ts
DELETED
|
@@ -1,379 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Key Manager - ECDSA secp256k1 Key Generation and Storage
|
|
3
|
-
*
|
|
4
|
-
* Handles secure generation, storage, and retrieval of cryptographic keys.
|
|
5
|
-
* Private keys are stored securely using expo-secure-store and never leave the device.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { ec as EC } from 'elliptic';
|
|
9
|
-
import type { ECKeyPair } from 'elliptic';
|
|
10
|
-
import { Platform } from 'react-native';
|
|
11
|
-
|
|
12
|
-
// Lazy imports for React Native specific modules
|
|
13
|
-
let SecureStore: typeof import('expo-secure-store') | null = null;
|
|
14
|
-
let ExpoCrypto: typeof import('expo-crypto') | null = null;
|
|
15
|
-
|
|
16
|
-
const ec = new EC('secp256k1');
|
|
17
|
-
|
|
18
|
-
const STORAGE_KEYS = {
|
|
19
|
-
PRIVATE_KEY: 'oxy_identity_private_key',
|
|
20
|
-
PUBLIC_KEY: 'oxy_identity_public_key',
|
|
21
|
-
} as const;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Initialize React Native specific modules
|
|
25
|
-
* This allows the module to work in both Node.js and React Native environments
|
|
26
|
-
*/
|
|
27
|
-
async function initSecureStore(): Promise<typeof import('expo-secure-store')> {
|
|
28
|
-
if (!SecureStore) {
|
|
29
|
-
try {
|
|
30
|
-
SecureStore = await import('expo-secure-store');
|
|
31
|
-
} catch (error) {
|
|
32
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
33
|
-
throw new Error(`Failed to load expo-secure-store: ${errorMessage}. Make sure expo-secure-store is installed and properly configured.`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (!SecureStore) {
|
|
37
|
-
throw new Error('expo-secure-store module is not available');
|
|
38
|
-
}
|
|
39
|
-
return SecureStore;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Check if we're in a React Native environment
|
|
44
|
-
*/
|
|
45
|
-
function isReactNative(): boolean {
|
|
46
|
-
return typeof navigator !== 'undefined' && navigator.product === 'ReactNative';
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Check if we're in a Node.js environment
|
|
51
|
-
*/
|
|
52
|
-
function isNodeJS(): boolean {
|
|
53
|
-
return typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Check if we're on web platform
|
|
58
|
-
* Identity storage is only available on native platforms (iOS/Android)
|
|
59
|
-
*/
|
|
60
|
-
function isWebPlatform(): boolean {
|
|
61
|
-
try {
|
|
62
|
-
return Platform.OS === 'web';
|
|
63
|
-
} catch {
|
|
64
|
-
// Fallback if Platform is not available
|
|
65
|
-
return typeof window !== 'undefined' && typeof navigator !== 'undefined' && navigator.product !== 'ReactNative';
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async function initExpoCrypto(): Promise<typeof import('expo-crypto')> {
|
|
70
|
-
if (!ExpoCrypto) {
|
|
71
|
-
ExpoCrypto = await import('expo-crypto');
|
|
72
|
-
}
|
|
73
|
-
return ExpoCrypto;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Convert Uint8Array to hexadecimal string
|
|
78
|
-
* Works in both Node.js and React Native
|
|
79
|
-
*/
|
|
80
|
-
function uint8ArrayToHex(bytes: Uint8Array): string {
|
|
81
|
-
return Array.from(bytes)
|
|
82
|
-
.map(b => b.toString(16).padStart(2, '0'))
|
|
83
|
-
.join('');
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Generate cryptographically secure random bytes
|
|
88
|
-
*/
|
|
89
|
-
async function getSecureRandomBytes(length: number): Promise<Uint8Array> {
|
|
90
|
-
// In React Native, always use expo-crypto
|
|
91
|
-
if (isReactNative() || !isNodeJS()) {
|
|
92
|
-
const Crypto = await initExpoCrypto();
|
|
93
|
-
return Crypto.getRandomBytes(length);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// In Node.js, use Node's crypto module
|
|
97
|
-
// Use Function constructor to prevent Metro bundler from statically analyzing this require
|
|
98
|
-
// This ensures the require is only evaluated in Node.js runtime, not during Metro bundling
|
|
99
|
-
try {
|
|
100
|
-
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
101
|
-
const getCrypto = new Function('return require("crypto")');
|
|
102
|
-
const crypto = getCrypto();
|
|
103
|
-
return new Uint8Array(crypto.randomBytes(length));
|
|
104
|
-
} catch (error) {
|
|
105
|
-
// Fallback to expo-crypto if Node crypto fails
|
|
106
|
-
const Crypto = await initExpoCrypto();
|
|
107
|
-
return Crypto.getRandomBytes(length);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export interface KeyPair {
|
|
112
|
-
publicKey: string;
|
|
113
|
-
privateKey: string;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export class KeyManager {
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Generate a new ECDSA secp256k1 key pair
|
|
120
|
-
* Returns the keys in hexadecimal format
|
|
121
|
-
*/
|
|
122
|
-
static generateKeyPairSync(): KeyPair {
|
|
123
|
-
const keyPair = ec.genKeyPair();
|
|
124
|
-
return {
|
|
125
|
-
privateKey: keyPair.getPrivate('hex'),
|
|
126
|
-
publicKey: keyPair.getPublic('hex'),
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Generate a new key pair using secure random bytes
|
|
132
|
-
*/
|
|
133
|
-
static async generateKeyPair(): Promise<KeyPair> {
|
|
134
|
-
const randomBytes = await getSecureRandomBytes(32);
|
|
135
|
-
const privateKeyHex = uint8ArrayToHex(randomBytes);
|
|
136
|
-
const keyPair = ec.keyFromPrivate(privateKeyHex);
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
privateKey: keyPair.getPrivate('hex'),
|
|
140
|
-
publicKey: keyPair.getPublic('hex'),
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Generate and securely store a new key pair on the device
|
|
146
|
-
* Returns only the public key (private key is stored securely)
|
|
147
|
-
*/
|
|
148
|
-
static async createIdentity(): Promise<string> {
|
|
149
|
-
if (isWebPlatform()) {
|
|
150
|
-
throw new Error('Identity creation is only available on native platforms (iOS/Android). Please use the native app to create your identity.');
|
|
151
|
-
}
|
|
152
|
-
const store = await initSecureStore();
|
|
153
|
-
const { privateKey, publicKey } = await KeyManager.generateKeyPair();
|
|
154
|
-
|
|
155
|
-
await store.setItemAsync(STORAGE_KEYS.PRIVATE_KEY, privateKey, {
|
|
156
|
-
keychainAccessible: store.WHEN_UNLOCKED_THIS_DEVICE_ONLY,
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
await store.setItemAsync(STORAGE_KEYS.PUBLIC_KEY, publicKey);
|
|
160
|
-
|
|
161
|
-
return publicKey;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Import an existing key pair (e.g., from backup file)
|
|
166
|
-
*/
|
|
167
|
-
static async importKeyPair(privateKey: string): Promise<string> {
|
|
168
|
-
if (isWebPlatform()) {
|
|
169
|
-
throw new Error('Identity import is only available on native platforms (iOS/Android). Please use the native app to import your identity.');
|
|
170
|
-
}
|
|
171
|
-
const store = await initSecureStore();
|
|
172
|
-
|
|
173
|
-
const keyPair = ec.keyFromPrivate(privateKey);
|
|
174
|
-
const publicKey = keyPair.getPublic('hex');
|
|
175
|
-
|
|
176
|
-
await store.setItemAsync(STORAGE_KEYS.PRIVATE_KEY, privateKey, {
|
|
177
|
-
keychainAccessible: store.WHEN_UNLOCKED_THIS_DEVICE_ONLY,
|
|
178
|
-
});
|
|
179
|
-
await store.setItemAsync(STORAGE_KEYS.PUBLIC_KEY, publicKey);
|
|
180
|
-
|
|
181
|
-
return publicKey;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Get the stored private key
|
|
186
|
-
* WARNING: Only use this for signing operations within the app
|
|
187
|
-
*/
|
|
188
|
-
static async getPrivateKey(): Promise<string | null> {
|
|
189
|
-
if (isWebPlatform()) {
|
|
190
|
-
return null; // Identity storage is only available on native platforms
|
|
191
|
-
}
|
|
192
|
-
try {
|
|
193
|
-
const store = await initSecureStore();
|
|
194
|
-
return await store.getItemAsync(STORAGE_KEYS.PRIVATE_KEY);
|
|
195
|
-
} catch (error) {
|
|
196
|
-
// If secure store is not available, return null (no identity)
|
|
197
|
-
// This allows the app to continue functioning even if secure store fails to load
|
|
198
|
-
if (__DEV__) {
|
|
199
|
-
console.warn('[KeyManager] Failed to access secure store:', error);
|
|
200
|
-
}
|
|
201
|
-
return null;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Get the stored public key
|
|
207
|
-
*/
|
|
208
|
-
static async getPublicKey(): Promise<string | null> {
|
|
209
|
-
if (isWebPlatform()) {
|
|
210
|
-
return null; // Identity storage is only available on native platforms
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
try {
|
|
214
|
-
const store = await initSecureStore();
|
|
215
|
-
return await store.getItemAsync(STORAGE_KEYS.PUBLIC_KEY);
|
|
216
|
-
} catch (error) {
|
|
217
|
-
if (__DEV__) {
|
|
218
|
-
console.warn('[KeyManager] Failed to access secure store:', error);
|
|
219
|
-
}
|
|
220
|
-
return null;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Check if an identity (key pair) exists on this device
|
|
226
|
-
*/
|
|
227
|
-
static async hasIdentity(): Promise<boolean> {
|
|
228
|
-
if (isWebPlatform()) {
|
|
229
|
-
return false; // Identity storage is only available on native platforms
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
try {
|
|
233
|
-
const privateKey = await KeyManager.getPrivateKey();
|
|
234
|
-
return privateKey !== null;
|
|
235
|
-
} catch (error) {
|
|
236
|
-
if (__DEV__) {
|
|
237
|
-
console.warn('[KeyManager] Failed to check identity:', error);
|
|
238
|
-
}
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Delete the stored identity (both keys)
|
|
245
|
-
* Use with EXTREME caution - this is irreversible without a backup file
|
|
246
|
-
* This should ONLY be called when explicitly requested by the user
|
|
247
|
-
* @param force - If true, skip confirmation checks (default: false)
|
|
248
|
-
* @param userConfirmed - If true, user has explicitly confirmed deletion (default: false)
|
|
249
|
-
*/
|
|
250
|
-
static async deleteIdentity(
|
|
251
|
-
force: boolean = false,
|
|
252
|
-
userConfirmed: boolean = false
|
|
253
|
-
): Promise<void> {
|
|
254
|
-
if (isWebPlatform()) {
|
|
255
|
-
return; // Identity storage is only available on native platforms, nothing to delete
|
|
256
|
-
}
|
|
257
|
-
// CRITICAL SAFEGUARD: Require explicit user confirmation unless force is true
|
|
258
|
-
if (!force && !userConfirmed) {
|
|
259
|
-
throw new Error('Identity deletion requires explicit user confirmation. This is a safety measure to prevent accidental data loss.');
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
if (!force) {
|
|
263
|
-
const hasIdentity = await KeyManager.hasIdentity();
|
|
264
|
-
if (!hasIdentity) {
|
|
265
|
-
return; // Nothing to delete
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
const store = await initSecureStore();
|
|
270
|
-
await store.deleteItemAsync(STORAGE_KEYS.PRIVATE_KEY);
|
|
271
|
-
await store.deleteItemAsync(STORAGE_KEYS.PUBLIC_KEY);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Verify identity integrity - checks if keys are valid and accessible
|
|
276
|
-
*/
|
|
277
|
-
static async verifyIdentityIntegrity(): Promise<boolean> {
|
|
278
|
-
if (isWebPlatform()) {
|
|
279
|
-
return false; // Identity storage is only available on native platforms
|
|
280
|
-
}
|
|
281
|
-
try {
|
|
282
|
-
const privateKey = await KeyManager.getPrivateKey();
|
|
283
|
-
const publicKey = await KeyManager.getPublicKey();
|
|
284
|
-
|
|
285
|
-
if (!privateKey || !publicKey) {
|
|
286
|
-
return false;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// Validate private key format
|
|
290
|
-
if (!KeyManager.isValidPrivateKey(privateKey)) {
|
|
291
|
-
return false;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// Validate public key format
|
|
295
|
-
if (!KeyManager.isValidPublicKey(publicKey)) {
|
|
296
|
-
return false;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
// Verify public key can be derived from private key
|
|
300
|
-
const derivedPublicKey = KeyManager.derivePublicKey(privateKey);
|
|
301
|
-
if (derivedPublicKey !== publicKey) {
|
|
302
|
-
return false; // Keys don't match
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// Verify we can create a key pair object (tests elliptic curve operations)
|
|
306
|
-
const keyPair = await KeyManager.getKeyPairObject();
|
|
307
|
-
if (!keyPair) {
|
|
308
|
-
return false;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
return true;
|
|
312
|
-
} catch (error) {
|
|
313
|
-
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
314
|
-
console.error('[KeyManager] Identity integrity check failed:', error);
|
|
315
|
-
}
|
|
316
|
-
return false;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* Get the elliptic curve key object from the stored private key
|
|
322
|
-
* Used internally for signing operations
|
|
323
|
-
*/
|
|
324
|
-
static async getKeyPairObject(): Promise<ECKeyPair | null> {
|
|
325
|
-
if (isWebPlatform()) {
|
|
326
|
-
return null; // Identity storage is only available on native platforms
|
|
327
|
-
}
|
|
328
|
-
const privateKey = await KeyManager.getPrivateKey();
|
|
329
|
-
if (!privateKey) return null;
|
|
330
|
-
return ec.keyFromPrivate(privateKey);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Derive public key from a private key (without storing)
|
|
335
|
-
*/
|
|
336
|
-
static derivePublicKey(privateKey: string): string {
|
|
337
|
-
const keyPair = ec.keyFromPrivate(privateKey);
|
|
338
|
-
return keyPair.getPublic('hex');
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Validate that a string is a valid public key
|
|
343
|
-
*/
|
|
344
|
-
static isValidPublicKey(publicKey: string): boolean {
|
|
345
|
-
try {
|
|
346
|
-
ec.keyFromPublic(publicKey, 'hex');
|
|
347
|
-
return true;
|
|
348
|
-
} catch {
|
|
349
|
-
return false;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Validate that a string is a valid private key
|
|
355
|
-
*/
|
|
356
|
-
static isValidPrivateKey(privateKey: string): boolean {
|
|
357
|
-
try {
|
|
358
|
-
const keyPair = ec.keyFromPrivate(privateKey);
|
|
359
|
-
// Verify it can derive a public key
|
|
360
|
-
keyPair.getPublic('hex');
|
|
361
|
-
return true;
|
|
362
|
-
} catch {
|
|
363
|
-
return false;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Get a shortened version of the public key for display
|
|
369
|
-
* Format: first 8 chars...last 8 chars
|
|
370
|
-
*/
|
|
371
|
-
static shortenPublicKey(publicKey: string): string {
|
|
372
|
-
if (publicKey.length <= 20) return publicKey;
|
|
373
|
-
return `${publicKey.slice(0, 8)}...${publicKey.slice(-8)}`;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
export default KeyManager;
|
|
378
|
-
|
|
379
|
-
|
|
@@ -1,301 +0,0 @@
|
|
|
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
|
-
* Sign arbitrary data for API requests
|
|
269
|
-
* Creates a canonical string representation and signs it
|
|
270
|
-
*/
|
|
271
|
-
static async signRequestData(data: Record<string, unknown>): Promise<{
|
|
272
|
-
signature: string;
|
|
273
|
-
publicKey: string;
|
|
274
|
-
timestamp: number;
|
|
275
|
-
}> {
|
|
276
|
-
const publicKey = await KeyManager.getPublicKey();
|
|
277
|
-
if (!publicKey) {
|
|
278
|
-
throw new Error('No identity found. Please create or import an identity first.');
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const timestamp = Date.now();
|
|
282
|
-
|
|
283
|
-
// Create canonical string representation
|
|
284
|
-
const sortedKeys = Object.keys(data).sort();
|
|
285
|
-
const canonicalParts = sortedKeys.map(key => `${key}:${JSON.stringify(data[key])}`);
|
|
286
|
-
const canonicalString = canonicalParts.join('|');
|
|
287
|
-
|
|
288
|
-
const message = `request:${publicKey}:${timestamp}:${canonicalString}`;
|
|
289
|
-
const signature = await SignatureService.sign(message);
|
|
290
|
-
|
|
291
|
-
return {
|
|
292
|
-
signature,
|
|
293
|
-
publicKey,
|
|
294
|
-
timestamp,
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
export default SignatureService;
|
|
300
|
-
|
|
301
|
-
|