1
1
import { onboardingProfile } from '@contexts/onboarding/stores' ;
2
2
import { api } from '@core/api' ;
3
- import { SecretManager } from '@iota/sdk' ;
4
- import { Readable , derived , get } from 'svelte/store' ;
5
-
6
- const onboardingProfileSecretManagerOptions = derived ( onboardingProfile , ( $onboardingProfile ) => {
7
- return $onboardingProfile ?. secretManagerOptions
8
- } ) ;
9
-
10
- export const onboardingProfileSecretManager : Readable < SecretManager | null > = derived ( onboardingProfileSecretManagerOptions , ( $onboardingProfileSecretManagerOptions , set ) => {
11
- if ( $onboardingProfileSecretManagerOptions ) {
12
- api . createSecretManager ( $onboardingProfileSecretManagerOptions )
13
- . then ( ( secretManager ) => {
14
- set ( secretManager )
15
- } )
3
+ import { SecretManager , SecretManagerType } from '@iota/sdk' ;
4
+ import { USE_LEDGER_SIMULATOR } from 'shared/lib/core/ledger' ;
5
+ import { getStorageDirectoryOfProfile , ProfileType } from 'shared/lib/core/profile' ;
6
+ import { get , writable , Writable } from 'svelte/store' ;
7
+
8
+ export const onboardingProfileSecretManager : Writable < SecretManager | null > = writable ( null ) ;
9
+
10
+ export async function buildOnboardingSecretManager ( ) {
11
+ const profile = get ( onboardingProfile ) ;
12
+ if ( profile ) {
13
+ const { id, type, strongholdPassword } = profile ;
14
+
15
+ const storagePath = await getStorageDirectoryOfProfile ( id )
16
+ const secretManagerOptions = getSecretManagerFromProfileType ( type , {
17
+ storagePath,
18
+ strongholdPassword
19
+ } )
20
+
21
+ console . log ( "options" , secretManagerOptions )
22
+
23
+ const secretManager = await api . createSecretManager ( secretManagerOptions ) ;
24
+
25
+ onboardingProfileSecretManager . set ( secretManager )
16
26
} else {
17
- set ( null )
27
+ onboardingProfileSecretManager . set ( null )
18
28
}
19
- } )
29
+ }
20
30
21
31
export function isOnboardingSecretManagerInitialized ( ) : boolean {
22
32
return ! ! get ( onboardingProfileSecretManager )
23
33
}
34
+
35
+ export function getSecretManagerFromProfileType ( type ?: ProfileType , { storagePath, strongholdPassword } : {
36
+ storagePath ?: string , strongholdPassword ?: string
37
+ } = { } ) : SecretManagerType {
38
+ const strongholdSecretManager = {
39
+ stronghold : { snapshotPath : `${ storagePath } /wallet.stronghold` , password : strongholdPassword } ,
40
+ }
41
+ const ledgerSecretManager = {
42
+ ledgerNano : USE_LEDGER_SIMULATOR ,
43
+ }
44
+
45
+ switch ( type ) {
46
+ case ProfileType . Ledger :
47
+ return ledgerSecretManager
48
+ case ProfileType . Software :
49
+ default :
50
+ return strongholdSecretManager
51
+ }
52
+ }
0 commit comments