Skip to content

Commit 499bdbc

Browse files
committed
Updates
1 parent 7094f0c commit 499bdbc

File tree

4 files changed

+114
-46
lines changed

4 files changed

+114
-46
lines changed

src/interfaces.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export interface WalletCheckModule {
2828
id?: string
2929
}
3030

31+
export interface WalletCheckModule {
32+
reset?: () => void
33+
}
34+
3135
export interface WalletCheckModal {
3236
heading: string
3337
description: string

src/modules/check/derivation-path.ts

Lines changed: 107 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,120 @@ interface DerivationPaths {
77
const derivationPaths: DerivationPaths = {
88
Ledger: [
99
{ 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' }
1211
],
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' }]
1713
}
1814

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+
1945
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)" />
2152
`
2253

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>`
3486
}
35-
)}
36-
</select>
37-
`
38-
}
87+
</div>
88+
`
89+
}
3990

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+
}
4496

45-
return (stateAndHelpers: StateAndHelpers): WalletCheckModal | undefined => {
97+
function checkModule(
98+
stateAndHelpers: StateAndHelpers
99+
): WalletCheckModal | undefined {
46100
const { wallet, address } = stateAndHelpers
47101

48-
if (!address && wallet && wallet.type === 'hardware' && !completed) {
102+
if (!address && wallet && wallet.type === 'hardware' && !state.completed) {
49103
const handleCustomInput = () => {
50104
const input = <HTMLInputElement>(
51105
document.getElementById('custom-derivation-input')
52106
)
53-
path = input && input.value
54-
}
55107

56-
const handleDerivationSelect = () => {
57-
const pathIndex = (document as any).getElementById('derivation-select')
58-
.selectedIndex
108+
state.dPath = input && input.value
109+
}
59110

60-
const selectedPath = derivationPaths[wallet.name][pathIndex].path
111+
const handleDerivationClick = (button: any) => {
112+
const selectedPath = button.dataset.path
61113

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)
64121
} else {
65-
path = selectedPath
122+
state.showCustomInput = false
123+
state.dPath = selectedPath
66124
}
67125
}
68126

@@ -71,25 +129,25 @@ function derivationPath() {
71129
delete (window as any).handleDerivationSelect
72130
}
73131
;(window as any).handleCustomInput = handleCustomInput
74-
;(window as any).handleDerivationSelect = handleDerivationSelect
132+
;(window as any).handleDerivationClick = handleDerivationClick
75133

76134
return {
77135
heading: 'Hardware Wallet Connect',
78136
description: `Please select a derivation path to connect your ${wallet.name} accounts, or select custom to input a custom path:`,
79137
eventCode: 'derivationPath',
80-
html: showCustomInput
81-
? customInputHtmlString
82-
: derivationSelectHtmlString(wallet.name),
138+
html: derivationSelectHtmlString(wallet.name, state.showCustomInput),
83139
button: {
84140
text: 'Connect',
85141
onclick: () => {
142+
wallet.provider.setPath(
143+
state.dPath || derivationPaths[wallet.name][0].path
144+
)
86145
wallet.connect &&
87146
wallet.connect().then(() => {
88-
wallet.provider.setPath(
89-
path || derivationPaths[wallet.name][0].path
90-
)
147+
// @TODO add path to local store
148+
91149
deleteWindowProperties()
92-
completed = true
150+
state.completed = true
93151
})
94152
}
95153
},
@@ -100,6 +158,10 @@ function derivationPath() {
100158
}
101159
}
102160
}
161+
162+
checkModule.reset = resetState
163+
164+
return checkModule
103165
}
104166

105167
export default derivationPath

src/modules/select/wallets/trezor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ async function trezorProvider(options: {
120120
provider.send = provider.sendAsync
121121

122122
function setPath(path: string) {
123+
console.log({ path })
123124
dPath = path
124125
}
125126

src/views/WalletCheck.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
activeModal = result && result.modal ? result.modal : activeModal
112112
}
113113
}
114-
}, 500)
114+
}, 100)
115115
}
116116
)
117117
}
@@ -179,6 +179,7 @@
179179
}
180180
181181
loadingModal = false
182+
modules.forEach(m => m.reset && m.reset())
182183
return resolve({ modal: undefined, module: undefined })
183184
}
184185
)

0 commit comments

Comments
 (0)