@oxyhq/services 5.16.18 → 5.16.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -8
- package/lib/commonjs/crypto/index.js +0 -7
- package/lib/commonjs/crypto/index.js.map +1 -1
- package/lib/commonjs/crypto/keyManager.js +2 -2
- package/lib/commonjs/crypto/polyfill.js +4 -4
- package/lib/commonjs/i18n/locales/en-US.json +1 -1
- package/lib/commonjs/index.js +0 -7
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/ui/context/OxyContext.js +17 -3
- package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
- package/lib/commonjs/ui/context/hooks/useAuthOperations.js +56 -15
- package/lib/commonjs/ui/context/hooks/useAuthOperations.js.map +1 -1
- package/lib/commonjs/ui/hooks/useIdentityMutations.js +4 -4
- package/lib/commonjs/ui/hooks/useIdentityMutations.js.map +1 -1
- package/lib/module/crypto/index.js +2 -3
- package/lib/module/crypto/index.js.map +1 -1
- package/lib/module/crypto/keyManager.js +2 -2
- package/lib/module/crypto/polyfill.js +4 -4
- package/lib/module/i18n/locales/en-US.json +1 -1
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/ui/context/OxyContext.js +17 -3
- package/lib/module/ui/context/OxyContext.js.map +1 -1
- package/lib/module/ui/context/hooks/useAuthOperations.js +56 -16
- package/lib/module/ui/context/hooks/useAuthOperations.js.map +1 -1
- package/lib/module/ui/hooks/useIdentityMutations.js +4 -4
- package/lib/module/ui/hooks/useIdentityMutations.js.map +1 -1
- package/lib/typescript/crypto/index.d.ts +1 -2
- package/lib/typescript/crypto/index.d.ts.map +1 -1
- package/lib/typescript/crypto/keyManager.d.ts +2 -2
- package/lib/typescript/crypto/polyfill.d.ts +2 -2
- package/lib/typescript/index.d.ts +2 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/ui/context/OxyContext.d.ts +6 -2
- package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
- package/lib/typescript/ui/context/hooks/useAuthOperations.d.ts +7 -3
- package/lib/typescript/ui/context/hooks/useAuthOperations.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/useIdentityMutations.d.ts +15 -3
- package/lib/typescript/ui/hooks/useIdentityMutations.d.ts.map +1 -1
- package/package.json +1 -2
- package/src/crypto/index.ts +2 -6
- package/src/crypto/keyManager.ts +2 -2
- package/src/crypto/polyfill.ts +4 -4
- package/src/i18n/locales/en-US.json +1 -1
- package/src/index.ts +2 -4
- package/src/ui/context/OxyContext.tsx +23 -5
- package/src/ui/context/hooks/useAuthOperations.ts +67 -17
- package/src/ui/hooks/useIdentityMutations.ts +6 -6
- package/lib/commonjs/crypto/recoveryPhrase.js +0 -152
- package/lib/commonjs/crypto/recoveryPhrase.js.map +0 -1
- package/lib/module/crypto/recoveryPhrase.js +0 -147
- package/lib/module/crypto/recoveryPhrase.js.map +0 -1
- package/lib/typescript/crypto/recoveryPhrase.d.ts +0 -59
- package/lib/typescript/crypto/recoveryPhrase.d.ts.map +0 -1
- package/src/crypto/recoveryPhrase.ts +0 -166
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Recovery Phrase Service - BIP39 Mnemonic Generation
|
|
5
|
-
*
|
|
6
|
-
* Handles generation and restoration of recovery phrases (mnemonic seeds)
|
|
7
|
-
* for backing up and restoring user identities.
|
|
8
|
-
*
|
|
9
|
-
* Note: This module requires the polyfill to be loaded first (done via crypto/index.ts)
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import * as bip39 from 'bip39';
|
|
13
|
-
import { KeyManager } from './keyManager';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Convert Uint8Array or array-like to hexadecimal string
|
|
17
|
-
* Works in both Node.js and React Native without depending on Buffer
|
|
18
|
-
*/
|
|
19
|
-
function toHex(data) {
|
|
20
|
-
// Convert to array of numbers if needed
|
|
21
|
-
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
22
|
-
return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
|
|
23
|
-
}
|
|
24
|
-
export class RecoveryPhraseService {
|
|
25
|
-
/**
|
|
26
|
-
* Generate a new identity with a recovery phrase
|
|
27
|
-
* Returns the mnemonic phrase (should only be shown once to the user)
|
|
28
|
-
*/
|
|
29
|
-
static async generateIdentityWithRecovery() {
|
|
30
|
-
// Generate 128-bit entropy for 12-word mnemonic
|
|
31
|
-
const mnemonic = bip39.generateMnemonic(128);
|
|
32
|
-
|
|
33
|
-
// Derive private key from mnemonic
|
|
34
|
-
// Using the seed directly as the private key (simplified approach)
|
|
35
|
-
const seed = await bip39.mnemonicToSeed(mnemonic);
|
|
36
|
-
|
|
37
|
-
// Use first 32 bytes of seed as private key
|
|
38
|
-
const seedSlice = seed.subarray ? seed.subarray(0, 32) : seed.slice(0, 32);
|
|
39
|
-
const privateKeyHex = toHex(seedSlice);
|
|
40
|
-
|
|
41
|
-
// Import the derived key pair
|
|
42
|
-
const publicKey = await KeyManager.importKeyPair(privateKeyHex);
|
|
43
|
-
return {
|
|
44
|
-
phrase: mnemonic,
|
|
45
|
-
words: mnemonic.split(' '),
|
|
46
|
-
publicKey
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Generate a 24-word recovery phrase for higher security
|
|
52
|
-
*/
|
|
53
|
-
static async generateIdentityWithRecovery24() {
|
|
54
|
-
// Generate 256-bit entropy for 24-word mnemonic
|
|
55
|
-
const mnemonic = bip39.generateMnemonic(256);
|
|
56
|
-
const seed = await bip39.mnemonicToSeed(mnemonic);
|
|
57
|
-
const seedSlice = seed.subarray ? seed.subarray(0, 32) : seed.slice(0, 32);
|
|
58
|
-
const privateKeyHex = toHex(seedSlice);
|
|
59
|
-
const publicKey = await KeyManager.importKeyPair(privateKeyHex);
|
|
60
|
-
return {
|
|
61
|
-
phrase: mnemonic,
|
|
62
|
-
words: mnemonic.split(' '),
|
|
63
|
-
publicKey
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Restore an identity from a recovery phrase
|
|
69
|
-
*/
|
|
70
|
-
static async restoreFromPhrase(phrase) {
|
|
71
|
-
// Normalize and validate the phrase
|
|
72
|
-
const normalizedPhrase = phrase.trim().toLowerCase();
|
|
73
|
-
if (!bip39.validateMnemonic(normalizedPhrase)) {
|
|
74
|
-
throw new Error('Invalid recovery phrase. Please check the words and try again.');
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Derive the same private key from the mnemonic
|
|
78
|
-
const seed = await bip39.mnemonicToSeed(normalizedPhrase);
|
|
79
|
-
const seedSlice = seed.subarray ? seed.subarray(0, 32) : seed.slice(0, 32);
|
|
80
|
-
const privateKeyHex = toHex(seedSlice);
|
|
81
|
-
|
|
82
|
-
// Import and store the key pair
|
|
83
|
-
const publicKey = await KeyManager.importKeyPair(privateKeyHex);
|
|
84
|
-
return publicKey;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Validate a recovery phrase without importing it
|
|
89
|
-
*/
|
|
90
|
-
static validatePhrase(phrase) {
|
|
91
|
-
const normalizedPhrase = phrase.trim().toLowerCase();
|
|
92
|
-
return bip39.validateMnemonic(normalizedPhrase);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Get the word list for autocomplete/validation
|
|
97
|
-
*/
|
|
98
|
-
static getWordList() {
|
|
99
|
-
return bip39.wordlists.english;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Check if a word is valid in the BIP39 word list
|
|
104
|
-
*/
|
|
105
|
-
static isValidWord(word) {
|
|
106
|
-
return bip39.wordlists.english.includes(word.toLowerCase());
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Get suggestions for a partial word
|
|
111
|
-
*/
|
|
112
|
-
static getSuggestions(partial, limit = 5) {
|
|
113
|
-
const lowerPartial = partial.toLowerCase();
|
|
114
|
-
return bip39.wordlists.english.filter(word => word.startsWith(lowerPartial)).slice(0, limit);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Derive the public key from a phrase without storing
|
|
119
|
-
* Useful for verification before importing
|
|
120
|
-
*/
|
|
121
|
-
static async derivePublicKeyFromPhrase(phrase) {
|
|
122
|
-
const normalizedPhrase = phrase.trim().toLowerCase();
|
|
123
|
-
if (!bip39.validateMnemonic(normalizedPhrase)) {
|
|
124
|
-
throw new Error('Invalid recovery phrase');
|
|
125
|
-
}
|
|
126
|
-
const seed = await bip39.mnemonicToSeed(normalizedPhrase);
|
|
127
|
-
const seedSlice = seed.subarray ? seed.subarray(0, 32) : seed.slice(0, 32);
|
|
128
|
-
const privateKeyHex = toHex(seedSlice);
|
|
129
|
-
return KeyManager.derivePublicKey(privateKeyHex);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Convert a phrase to its word array
|
|
134
|
-
*/
|
|
135
|
-
static phraseToWords(phrase) {
|
|
136
|
-
return phrase.trim().toLowerCase().split(/\s+/);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Convert a word array to a phrase string
|
|
141
|
-
*/
|
|
142
|
-
static wordsToPhrase(words) {
|
|
143
|
-
return words.map(w => w.toLowerCase().trim()).join(' ');
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
export default RecoveryPhraseService;
|
|
147
|
-
//# sourceMappingURL=recoveryPhrase.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["bip39","KeyManager","toHex","data","bytes","Uint8Array","Array","from","map","b","toString","padStart","join","RecoveryPhraseService","generateIdentityWithRecovery","mnemonic","generateMnemonic","seed","mnemonicToSeed","seedSlice","subarray","slice","privateKeyHex","publicKey","importKeyPair","phrase","words","split","generateIdentityWithRecovery24","restoreFromPhrase","normalizedPhrase","trim","toLowerCase","validateMnemonic","Error","validatePhrase","getWordList","wordlists","english","isValidWord","word","includes","getSuggestions","partial","limit","lowerPartial","filter","startsWith","derivePublicKeyFromPhrase","derivePublicKey","phraseToWords","wordsToPhrase","w"],"sourceRoot":"../../../src","sources":["crypto/recoveryPhrase.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAC9B,SAASC,UAAU,QAAQ,cAAc;;AAEzC;AACA;AACA;AACA;AACA,SAASC,KAAKA,CAACC,IAAoC,EAAU;EAC3D;EACA,MAAMC,KAAK,GAAGD,IAAI,YAAYE,UAAU,GAAGF,IAAI,GAAG,IAAIE,UAAU,CAACF,IAAI,CAAC;EACtE,OAAOG,KAAK,CAACC,IAAI,CAACH,KAAK,CAAC,CACrBI,GAAG,CAACC,CAAC,IAAIA,CAAC,CAACC,QAAQ,CAAC,EAAE,CAAC,CAACC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CACzCC,IAAI,CAAC,EAAE,CAAC;AACb;AAQA,OAAO,MAAMC,qBAAqB,CAAC;EACjC;AACF;AACA;AACA;EACE,aAAaC,4BAA4BA,CAAA,EAAkC;IACzE;IACA,MAAMC,QAAQ,GAAGf,KAAK,CAACgB,gBAAgB,CAAC,GAAG,CAAC;;IAE5C;IACA;IACA,MAAMC,IAAI,GAAG,MAAMjB,KAAK,CAACkB,cAAc,CAACH,QAAQ,CAAC;;IAEjD;IACA,MAAMI,SAAS,GAAGF,IAAI,CAACG,QAAQ,GAAGH,IAAI,CAACG,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,GAAGH,IAAI,CAACI,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC;IAC1E,MAAMC,aAAa,GAAGpB,KAAK,CAACiB,SAAS,CAAC;;IAEtC;IACA,MAAMI,SAAS,GAAG,MAAMtB,UAAU,CA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5C;IAEA,MAAMjB,IAAI,GAAG,MAAMjB,KAAK,CAACkB,cAAc,CAACY,gBAAgB,CAAC;IACzD,MAAMX,SAAS,GAAGF,IAAI,CAACG,QAAQ,GAAGH,IAAI,CAACG,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,GAAGH,IAAI,CAACI,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC;IAC1E,MAAMC,aAAa,GAAGpB,KAAK,CAACiB,SAAS,CAAC;IAEtC,OAAOlB,UAAU,CAACgD,eAAe,CAAC3B,aAAa,CAAC;EAClD;;EAEA;AACF;AACA;EACE,OAAO4B,aAAaA,CAACzB,MAAc,EAAY;IAC7C,OAAOA,MAAM,CAACM,IAAI,CAAC,CAAC,CAACC,WAAW,CAAC,CAAC,CAACL,KAAK,CAAC,KAAK,CAAC;EACjD;;EAEA;AACF;AACA;EACE,OAAOwB,aAAaA,CAACzB,KAAe,EAAU;IAC5C,OAAOA,KAAK,CAAClB,GAAG,CAAC4C,CAAC,IAAIA,CAAC,CAACpB,WAAW,CAAC,CAAC,CAACD,IAAI,CAAC,CAAC,CAAC,CAACnB,IAAI,CAAC,GAAG,CAAC;EACzD;AACF;AAEA,eAAeC,qBAAqB","ignoreList":[]}
|
|
@@ -1,59 +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
|
-
export interface RecoveryPhraseResult {
|
|
10
|
-
phrase: string;
|
|
11
|
-
words: string[];
|
|
12
|
-
publicKey: string;
|
|
13
|
-
}
|
|
14
|
-
export declare class RecoveryPhraseService {
|
|
15
|
-
/**
|
|
16
|
-
* Generate a new identity with a recovery phrase
|
|
17
|
-
* Returns the mnemonic phrase (should only be shown once to the user)
|
|
18
|
-
*/
|
|
19
|
-
static generateIdentityWithRecovery(): Promise<RecoveryPhraseResult>;
|
|
20
|
-
/**
|
|
21
|
-
* Generate a 24-word recovery phrase for higher security
|
|
22
|
-
*/
|
|
23
|
-
static generateIdentityWithRecovery24(): Promise<RecoveryPhraseResult>;
|
|
24
|
-
/**
|
|
25
|
-
* Restore an identity from a recovery phrase
|
|
26
|
-
*/
|
|
27
|
-
static restoreFromPhrase(phrase: string): Promise<string>;
|
|
28
|
-
/**
|
|
29
|
-
* Validate a recovery phrase without importing it
|
|
30
|
-
*/
|
|
31
|
-
static validatePhrase(phrase: string): boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Get the word list for autocomplete/validation
|
|
34
|
-
*/
|
|
35
|
-
static getWordList(): string[];
|
|
36
|
-
/**
|
|
37
|
-
* Check if a word is valid in the BIP39 word list
|
|
38
|
-
*/
|
|
39
|
-
static isValidWord(word: string): boolean;
|
|
40
|
-
/**
|
|
41
|
-
* Get suggestions for a partial word
|
|
42
|
-
*/
|
|
43
|
-
static getSuggestions(partial: string, limit?: number): string[];
|
|
44
|
-
/**
|
|
45
|
-
* Derive the public key from a phrase without storing
|
|
46
|
-
* Useful for verification before importing
|
|
47
|
-
*/
|
|
48
|
-
static derivePublicKeyFromPhrase(phrase: string): Promise<string>;
|
|
49
|
-
/**
|
|
50
|
-
* Convert a phrase to its word array
|
|
51
|
-
*/
|
|
52
|
-
static phraseToWords(phrase: string): string[];
|
|
53
|
-
/**
|
|
54
|
-
* Convert a word array to a phrase string
|
|
55
|
-
*/
|
|
56
|
-
static wordsToPhrase(words: string[]): string;
|
|
57
|
-
}
|
|
58
|
-
export default RecoveryPhraseService;
|
|
59
|
-
//# sourceMappingURL=recoveryPhrase.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"recoveryPhrase.d.ts","sourceRoot":"","sources":["../../../src/crypto/recoveryPhrase.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAiBH,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,qBAAa,qBAAqB;IAChC;;;OAGG;WACU,4BAA4B,IAAI,OAAO,CAAC,oBAAoB,CAAC;IAsB1E;;OAEG;WACU,8BAA8B,IAAI,OAAO,CAAC,oBAAoB,CAAC;IAgB5E;;OAEG;WACU,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAmB/D;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAK9C;;OAEG;IACH,MAAM,CAAC,WAAW,IAAI,MAAM,EAAE;IAI9B;;OAEG;IACH,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIzC;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,GAAE,MAAU,GAAG,MAAM,EAAE;IAOnE;;;OAGG;WACU,yBAAyB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAcvE;;OAEG;IACH,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE;IAI9C;;OAEG;IACH,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM;CAG9C;AAED,eAAe,qBAAqB,CAAC"}
|
|
@@ -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
|
-
|