@oxyhq/services 5.16.18 → 5.16.20

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 (71) hide show
  1. package/README.md +7 -8
  2. package/lib/commonjs/crypto/index.js +0 -7
  3. package/lib/commonjs/crypto/index.js.map +1 -1
  4. package/lib/commonjs/crypto/keyManager.js +2 -2
  5. package/lib/commonjs/crypto/polyfill.js +4 -4
  6. package/lib/commonjs/crypto/types.js +2 -0
  7. package/lib/commonjs/crypto/types.js.map +1 -0
  8. package/lib/commonjs/i18n/locales/en-US.json +1 -1
  9. package/lib/commonjs/index.js +0 -7
  10. package/lib/commonjs/index.js.map +1 -1
  11. package/lib/commonjs/ui/context/OxyContext.js +54 -11
  12. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  13. package/lib/commonjs/ui/context/hooks/useAuthOperations.js +56 -15
  14. package/lib/commonjs/ui/context/hooks/useAuthOperations.js.map +1 -1
  15. package/lib/commonjs/ui/hooks/useSessionSocket.js +52 -2
  16. package/lib/commonjs/ui/hooks/useSessionSocket.js.map +1 -1
  17. package/lib/module/crypto/index.js +2 -4
  18. package/lib/module/crypto/index.js.map +1 -1
  19. package/lib/module/crypto/keyManager.js +2 -2
  20. package/lib/module/crypto/polyfill.js +4 -4
  21. package/lib/module/crypto/types.js +2 -0
  22. package/lib/module/crypto/types.js.map +1 -0
  23. package/lib/module/i18n/locales/en-US.json +1 -1
  24. package/lib/module/index.js +1 -1
  25. package/lib/module/index.js.map +1 -1
  26. package/lib/module/ui/context/OxyContext.js +49 -6
  27. package/lib/module/ui/context/OxyContext.js.map +1 -1
  28. package/lib/module/ui/context/hooks/useAuthOperations.js +56 -16
  29. package/lib/module/ui/context/hooks/useAuthOperations.js.map +1 -1
  30. package/lib/module/ui/hooks/useSessionSocket.js +52 -2
  31. package/lib/module/ui/hooks/useSessionSocket.js.map +1 -1
  32. package/lib/typescript/crypto/index.d.ts +2 -2
  33. package/lib/typescript/crypto/index.d.ts.map +1 -1
  34. package/lib/typescript/crypto/keyManager.d.ts +2 -2
  35. package/lib/typescript/crypto/polyfill.d.ts +2 -2
  36. package/lib/typescript/crypto/types.d.ts +18 -0
  37. package/lib/typescript/crypto/types.d.ts.map +1 -0
  38. package/lib/typescript/index.d.ts +2 -2
  39. package/lib/typescript/index.d.ts.map +1 -1
  40. package/lib/typescript/ui/context/OxyContext.d.ts +2 -2
  41. package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
  42. package/lib/typescript/ui/context/hooks/useAuthOperations.d.ts +3 -3
  43. package/lib/typescript/ui/context/hooks/useAuthOperations.d.ts.map +1 -1
  44. package/lib/typescript/ui/hooks/useSessionSocket.d.ts +7 -1
  45. package/lib/typescript/ui/hooks/useSessionSocket.d.ts.map +1 -1
  46. package/package.json +1 -2
  47. package/src/crypto/index.ts +3 -6
  48. package/src/crypto/keyManager.ts +2 -2
  49. package/src/crypto/polyfill.ts +4 -4
  50. package/src/crypto/types.ts +19 -0
  51. package/src/i18n/locales/en-US.json +1 -1
  52. package/src/index.ts +2 -4
  53. package/src/ui/context/OxyContext.tsx +63 -7
  54. package/src/ui/context/hooks/useAuthOperations.ts +67 -17
  55. package/src/ui/hooks/useSessionSocket.ts +62 -2
  56. package/lib/commonjs/crypto/recoveryPhrase.js +0 -152
  57. package/lib/commonjs/crypto/recoveryPhrase.js.map +0 -1
  58. package/lib/commonjs/ui/hooks/useIdentityMutations.js +0 -111
  59. package/lib/commonjs/ui/hooks/useIdentityMutations.js.map +0 -1
  60. package/lib/module/crypto/recoveryPhrase.js +0 -147
  61. package/lib/module/crypto/recoveryPhrase.js.map +0 -1
  62. package/lib/module/ui/hooks/useIdentityMutations.js +0 -105
  63. package/lib/module/ui/hooks/useIdentityMutations.js.map +0 -1
  64. package/lib/typescript/crypto/recoveryPhrase.d.ts +0 -59
  65. package/lib/typescript/crypto/recoveryPhrase.d.ts.map +0 -1
  66. package/lib/typescript/types/bip39.d.ts +0 -32
  67. package/lib/typescript/ui/hooks/useIdentityMutations.d.ts +0 -29
  68. package/lib/typescript/ui/hooks/useIdentityMutations.d.ts.map +0 -1
  69. package/src/crypto/recoveryPhrase.ts +0 -166
  70. package/src/types/bip39.d.ts +0 -32
  71. package/src/ui/hooks/useIdentityMutations.ts +0 -115
@@ -1,166 +0,0 @@
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
-
@@ -1,32 +0,0 @@
1
- declare module 'bip39' {
2
- export interface Wordlist {
3
- [index: number]: string;
4
- length: number;
5
- getWord(index: number): string;
6
- getWordIndex(word: string): number;
7
- }
8
-
9
- export const wordlists: {
10
- english: string[];
11
- chinese_simplified: string[];
12
- chinese_traditional: string[];
13
- french: string[];
14
- italian: string[];
15
- japanese: string[];
16
- korean: string[];
17
- spanish: string[];
18
- };
19
-
20
- // Use Uint8Array instead of Buffer for React Native compatibility
21
- // In Node.js, Buffer extends Uint8Array so this is compatible
22
- export function generateMnemonic(strength?: number, rng?: (size: number) => Uint8Array, wordlist?: string[]): string;
23
- export function mnemonicToSeed(mnemonic: string, passphrase?: string): Promise<Uint8Array>;
24
- export function mnemonicToSeedSync(mnemonic: string, passphrase?: string): Uint8Array;
25
- export function mnemonicToEntropy(mnemonic: string, wordlist?: string[]): string;
26
- export function entropyToMnemonic(entropy: string, wordlist?: string[]): string;
27
- export function validateMnemonic(mnemonic: string, wordlist?: string[]): boolean;
28
- export function mnemonicToSeedHex(mnemonic: string, passphrase?: string): Promise<string>;
29
- export function mnemonicToSeedHexSync(mnemonic: string, passphrase?: string): string;
30
- }
31
-
32
-
@@ -1,115 +0,0 @@
1
- /**
2
- * TanStack Query mutations for identity operations
3
- * Provides offline-first mutations for identity creation, import, and sync
4
- * Never deletes identity on errors - preserves user data
5
- */
6
-
7
- import { useMutation, useQueryClient } from '@tanstack/react-query';
8
- import type { User } from '../../models/interfaces';
9
-
10
- export interface CreateIdentityResult {
11
- recoveryPhrase: string[];
12
- synced: boolean;
13
- }
14
-
15
- export interface ImportIdentityResult {
16
- synced: boolean;
17
- }
18
-
19
- /**
20
- * Hook for creating a new identity with offline support
21
- * Never deletes identity on error - preserves user data
22
- */
23
- export function useCreateIdentity(
24
- createIdentityFn: () => Promise<CreateIdentityResult>
25
- ) {
26
- const queryClient = useQueryClient();
27
-
28
- return useMutation({
29
- mutationFn: createIdentityFn,
30
- onSuccess: (data) => {
31
- // Invalidate user queries to refetch after identity creation
32
- queryClient.invalidateQueries({ queryKey: ['user'] });
33
- queryClient.invalidateQueries({ queryKey: ['identity'] });
34
- },
35
- onError: (error) => {
36
- // Never delete identity on error - just log it
37
- // User can recover using recovery phrase
38
- if (__DEV__) {
39
- console.warn('[useCreateIdentity] Identity creation error (identity may still exist):', error);
40
- }
41
- },
42
- retry: false, // Don't retry identity creation
43
- networkMode: 'offlineFirst',
44
- });
45
- }
46
-
47
- /**
48
- * Hook for importing an identity from recovery phrase
49
- * Never deletes identity on error - preserves user data
50
- */
51
- export function useImportIdentity(
52
- importIdentityFn: (phrase: string) => Promise<ImportIdentityResult>
53
- ) {
54
- const queryClient = useQueryClient();
55
-
56
- return useMutation({
57
- mutationFn: importIdentityFn,
58
- onSuccess: (data) => {
59
- queryClient.invalidateQueries({ queryKey: ['user'] });
60
- queryClient.invalidateQueries({ queryKey: ['identity'] });
61
- },
62
- onError: (error) => {
63
- // Never delete identity on error - just log it
64
- if (__DEV__) {
65
- console.warn('[useImportIdentity] Identity import error (identity may still exist):', error);
66
- }
67
- },
68
- retry: false,
69
- networkMode: 'offlineFirst',
70
- });
71
- }
72
-
73
- /**
74
- * Hook for syncing identity with server
75
- * Never deletes identity on error - only logs and allows retry
76
- */
77
- export function useSyncIdentity(
78
- syncIdentityFn: () => Promise<User>
79
- ) {
80
- const queryClient = useQueryClient();
81
-
82
- return useMutation({
83
- mutationFn: syncIdentityFn,
84
- onSuccess: (user) => {
85
- // Update user cache
86
- queryClient.setQueryData(['user', 'current'], user);
87
- queryClient.invalidateQueries({ queryKey: ['user'] });
88
- queryClient.invalidateQueries({ queryKey: ['identity'] });
89
- },
90
- onError: (error) => {
91
- // Never delete identity on error - just log it
92
- // User can retry sync later or use recovery phrase
93
- if (__DEV__) {
94
- console.warn('[useSyncIdentity] Sync failed, but identity is preserved:', error);
95
- }
96
- },
97
- retry: (failureCount, error: any) => {
98
- // Retry up to 2 times for sync operations
99
- // Don't retry if it's a network error - user can retry when online
100
- const errorMessage = error instanceof Error ? error.message : String(error);
101
- const isNetworkError =
102
- errorMessage.includes('Network') ||
103
- errorMessage.includes('Failed to fetch') ||
104
- error?.code === 'NETWORK_ERROR';
105
-
106
- if (isNetworkError) {
107
- return false; // Don't retry network errors - user will retry when online
108
- }
109
-
110
- return failureCount < 2;
111
- },
112
- networkMode: 'offlineFirst',
113
- });
114
- }
115
-