@@ -7,62 +7,120 @@ interface DerivationPaths {
7
7
const derivationPaths : DerivationPaths = {
8
8
Ledger : [
9
9
{ path : `m/44'/60'` , label : 'Ethereum' } ,
10
- { path : `m/44'/60'/0'` , label : 'Ethereum Legacy' } ,
11
- { label : 'Custom Path' }
10
+ { path : `m/44'/60'/0'` , label : 'Ethereum Legacy' }
12
11
] ,
13
- Trezor : [
14
- { path : `m/44'/60'/0'/0` , label : 'Ethereum' } ,
15
- { label : 'Custom Path' }
16
- ]
12
+ Trezor : [ { path : `m/44'/60'/0'/0` , label : 'Ethereum' } ]
17
13
}
18
14
15
+ const styles = `
16
+ display: flex;
17
+ flex-direction: column;
18
+ `
19
+
20
+ // color: #4a90e2;
21
+
22
+ const baseStyles = `
23
+ background: inherit;
24
+ font-size: 0.889em;
25
+ font-family: inherit;
26
+ border: 1px solid #282828;
27
+ border-radius: 40px;
28
+ margin-top: 0.5rem;
29
+ padding: 0.55em 1.4em;
30
+ cursor: pointer;
31
+ color: #282828;
32
+ font-family: inherit;
33
+ transition: background 150ms ease-in-out;
34
+ line-height: 1.15;
35
+ `
36
+
37
+ const buttonStyles = `
38
+ cursor: pointer;
39
+ `
40
+
41
+ const selectedStyles = `
42
+ border: 1px solid #4a90e2;
43
+ `
44
+
19
45
const customInputHtmlString = `
20
- <input id="custom-derivation-input" type="text" placeholder="custom derivation path" onchange="window.handleCustomInput()" />
46
+ <input
47
+ id="custom-derivation-input"
48
+ style="${ baseStyles + selectedStyles } "
49
+ type="text"
50
+ placeholder="custom derivation path"
51
+ onchange="window.handleCustomInput(this.value)" />
21
52
`
22
53
23
- function derivationSelectHtmlString ( walletName : string ) {
24
- return `
25
- <select id="derivation-select" onchange="window.handleDerivationSelect()">
26
- ${ derivationPaths [ walletName ] . map (
27
- ( derivation : { path ?: string ; label : string } ) => {
28
- const { path, label } = derivation
29
- return path
30
- ? `
31
- <option>${ label } - ${ path } </option>
32
- `
33
- : `<option>${ label } </option>`
54
+ function derivationPath ( ) {
55
+ let state = {
56
+ completed : false ,
57
+ showCustomInput : false ,
58
+ dPath : ''
59
+ }
60
+
61
+ function derivationSelectHtmlString (
62
+ walletName : string ,
63
+ showCustomInput : boolean
64
+ ) {
65
+ return `
66
+ <div id="derivation-select" style="${ styles } ">
67
+ ${ derivationPaths [ walletName ]
68
+ . map ( ( derivation : { path ?: string ; label : string } ) => {
69
+ const { path, label } = derivation
70
+ return `
71
+ <button style="${ baseStyles +
72
+ buttonStyles +
73
+ ( state . dPath === path
74
+ ? selectedStyles
75
+ : '' ) } " onclick="window.handleDerivationClick(this)" data-path="${ path } ">
76
+ ${ label } - ${ path }
77
+ </button>
78
+ `
79
+ } )
80
+ . join ( ' ' ) }
81
+ ${
82
+ showCustomInput
83
+ ? customInputHtmlString
84
+ : `<button style="${ baseStyles +
85
+ buttonStyles } " onclick="window.handleDerivationClick(this)" data-path="custom">Custom Path</button>`
34
86
}
35
- ) }
36
- </select>
37
- `
38
- }
87
+ </div>
88
+ `
89
+ }
39
90
40
- function derivationPath ( ) {
41
- let completed = false
42
- let showCustomInput = false
43
- let path : string | undefined
91
+ function resetState ( ) {
92
+ state . completed = false
93
+ state . showCustomInput = false
94
+ state . dPath = ''
95
+ }
44
96
45
- return ( stateAndHelpers : StateAndHelpers ) : WalletCheckModal | undefined => {
97
+ function checkModule (
98
+ stateAndHelpers : StateAndHelpers
99
+ ) : WalletCheckModal | undefined {
46
100
const { wallet, address } = stateAndHelpers
47
101
48
- if ( ! address && wallet && wallet . type === 'hardware' && ! completed ) {
102
+ if ( ! address && wallet && wallet . type === 'hardware' && ! state . completed ) {
49
103
const handleCustomInput = ( ) => {
50
104
const input = < HTMLInputElement > (
51
105
document . getElementById ( 'custom-derivation-input' )
52
106
)
53
- path = input && input . value
54
- }
55
107
56
- const handleDerivationSelect = ( ) => {
57
- const pathIndex = ( document as any ) . getElementById ( 'derivation-select' )
58
- . selectedIndex
108
+ state . dPath = input && input . value
109
+ }
59
110
60
- const selectedPath = derivationPaths [ wallet . name ] [ pathIndex ] . path
111
+ const handleDerivationClick = ( button : any ) => {
112
+ const selectedPath = button . dataset . path
61
113
62
- if ( ! selectedPath ) {
63
- showCustomInput = true
114
+ if ( selectedPath === 'custom' ) {
115
+ state . showCustomInput = true
116
+ state . dPath = ''
117
+ setTimeout ( ( ) => {
118
+ const input = document . getElementById ( 'custom-derivation-input' )
119
+ input && input . focus ( )
120
+ } , 100 )
64
121
} else {
65
- path = selectedPath
122
+ state . showCustomInput = false
123
+ state . dPath = selectedPath
66
124
}
67
125
}
68
126
@@ -71,25 +129,25 @@ function derivationPath() {
71
129
delete ( window as any ) . handleDerivationSelect
72
130
}
73
131
; ( window as any ) . handleCustomInput = handleCustomInput
74
- ; ( window as any ) . handleDerivationSelect = handleDerivationSelect
132
+ ; ( window as any ) . handleDerivationClick = handleDerivationClick
75
133
76
134
return {
77
135
heading : 'Hardware Wallet Connect' ,
78
136
description : `Please select a derivation path to connect your ${ wallet . name } accounts, or select custom to input a custom path:` ,
79
137
eventCode : 'derivationPath' ,
80
- html : showCustomInput
81
- ? customInputHtmlString
82
- : derivationSelectHtmlString ( wallet . name ) ,
138
+ html : derivationSelectHtmlString ( wallet . name , state . showCustomInput ) ,
83
139
button : {
84
140
text : 'Connect' ,
85
141
onclick : ( ) => {
142
+ wallet . provider . setPath (
143
+ state . dPath || derivationPaths [ wallet . name ] [ 0 ] . path
144
+ )
86
145
wallet . connect &&
87
146
wallet . connect ( ) . then ( ( ) => {
88
- wallet . provider . setPath (
89
- path || derivationPaths [ wallet . name ] [ 0 ] . path
90
- )
147
+ // @TODO add path to local store
148
+
91
149
deleteWindowProperties ( )
92
- completed = true
150
+ state . completed = true
93
151
} )
94
152
}
95
153
} ,
@@ -100,6 +158,10 @@ function derivationPath() {
100
158
}
101
159
}
102
160
}
161
+
162
+ checkModule . reset = resetState
163
+
164
+ return checkModule
103
165
}
104
166
105
167
export default derivationPath
0 commit comments