Skip to content

Commit 1c55095

Browse files
committed
Add loading accounts module for ledger
1 parent 8a343a3 commit 1c55095

File tree

11 files changed

+126
-72
lines changed

11 files changed

+126
-72
lines changed

src/components/Wallets.svelte

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
<script lang="ts">
2+
import { onDestroy } from 'svelte'
23
import { fade } from 'svelte/transition'
34
import Button from '../elements/Button.svelte'
45
import IconButton from '../elements/IconButton.svelte'
5-
import { WalletSelectModalData, WalletModule } from '../interfaces'
6+
import { wallet } from '../stores'
7+
import {
8+
WalletSelectModalData,
9+
WalletModule,
10+
WritableStore
11+
} from '../interfaces'
612
export let modalData: WalletSelectModalData
713
export let handleWalletSelect: (wallet: WalletModule) => void
814
export let loadingWallet: string | undefined
915
1016
let showingAllWalletModules: boolean = false
17+
let selectedWallet: WritableStore
18+
19+
const unsubscribe = wallet.subscribe(wallet => (selectedWallet = wallet))
20+
21+
onDestroy(() => unsubscribe())
1122
</script>
1223

1324
<style>
@@ -65,6 +76,7 @@
6576
iconSrcSet={wallet.iconSrcSet}
6677
svg={wallet.svg}
6778
text={wallet.name}
79+
currentlySelected={wallet.name === selectedWallet.name}
6880
{loadingWallet} />
6981
</li>
7082
{/each}
@@ -86,6 +98,7 @@
8698
iconSrcSet={wallet.iconSrcSet}
8799
svg={wallet.svg}
88100
text={wallet.name}
101+
currentlySelected={wallet.name === selectedWallet.name}
89102
{loadingWallet} />
90103
</li>
91104
{/each}

src/elements/IconButton.svelte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
export let onclick: () => void = () => {}
88
export let text: string
99
export let loadingWallet: string | undefined
10+
export let currentlySelected: boolean = false
1011
</script>
1112

1213
<style>
@@ -61,6 +62,11 @@
6162
font-family: inherit;
6263
}
6364
65+
i {
66+
font-size: 1.6rem;
67+
margin-left: 0.5rem;
68+
}
69+
6470
@media only screen and (max-width: 450px) {
6571
button {
6672
width: 100%;
@@ -82,4 +88,7 @@
8288
{/if}
8389
</div>
8490
<span>{text}</span>
91+
{#if currentlySelected}
92+
<i>*</i>
93+
{/if}
8594
</button>

src/interfaces.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface WalletCheckModule {
2525
| WalletCheckModal
2626
| undefined
2727
| Promise<WalletCheckModal | undefined>
28+
id?: string
2829
}
2930

3031
export interface WalletCheckModal {
@@ -37,9 +38,7 @@ export interface WalletCheckModal {
3738
}
3839
eventCode: string
3940
action?: () => Promise<{ message: string } | undefined>
40-
loading?: Promise<undefined>
4141
icon?: string
42-
hint?: string
4342
}
4443

4544
export interface WalletSelectModalData {
@@ -109,7 +108,6 @@ export interface WalletInterface {
109108
name: string | undefined
110109
connect?: () => Promise<{ message: string } | undefined>
111110
disconnect?: () => void
112-
loading?: Promise<undefined>
113111
address: StateSyncer
114112
network: StateSyncer
115113
balance: StateSyncer
@@ -126,7 +124,6 @@ export interface Wallet {
126124
type: 'hardware' | 'injected' | 'sdk'
127125
instance?: any
128126
connect?: () => Promise<{ message: string } | undefined>
129-
loading?: Promise<undefined>
130127
}
131128

132129
export interface CommonWalletOptions {

src/modules/check/connect.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,12 @@ function connect() {
99
description: `This dapp requires access to your wallet, please login and authorize access to your ${wallet.name} accounts to continue.`,
1010
eventCode: 'loginFail',
1111
action: wallet.connect,
12-
loading: wallet.loading,
1312
icon: `
1413
<svg height="14" viewBox="0 0 18 14" width="18" xmlns="http://www.w3.org/2000/svg"><g fill="currentColor"><path d="m10.29375 4.05351563c0-.04921875 0-.09140625 0-.13007813 0-1.0546875 0-2.109375 0-3.1640625 0-.43945312.3480469-.76992188.7804688-.7453125.2003906.01054688.3585937.10546875.4992187.24609375.5800781.58359375 1.1566406 1.16367188 1.7367187 1.74023438 1.4695313 1.46953125 2.9390625 2.93906249 4.4050782 4.40859375.1335937.13359375.2425781.27421875.2707031.46757812.0351562.20742188-.0246094.421875-.1652344.58007813-.0246094.028125-.0492187.05273437-.0738281.08085937-2.0601563 2.06367188-4.1203125 4.1238281-6.1804688 6.1875-.2109375.2109375-.4570312.3023438-.7453125.2179688-.2707031-.0808594-.4464843-.2707032-.5132812-.5484375-.0140625-.0738282-.0175781-.1441407-.0140625-.2179688 0-1.0335937 0-2.0707031 0-3.1042969 0-.0386719 0-.08085935 0-.13359372h-5.06953125c-.49570313 0-.80507813-.309375-.80507813-.80859375 0-1.42382813 0-2.84414063 0-4.26796875 0-.49570313.30585938-.8015625.8015625-.8015625h4.93593748z"/><path d="m5.69882812 13.978125h-4.01132812c-.928125 0-1.6875-.8753906-1.6875-1.9511719v-10.06171872c0-1.07578125.75585938-1.95117188 1.6875-1.95117188h4.01132812c.34101563 0 .61523438.31992188.61523438.71015625 0 .39023438-.27421875.71015625-.61523438.71015625h-4.01132812c-.253125 0-.45703125.23554688-.45703125.52734375v10.06171875c0 .2917969.20390625.5273437.45703125.5273437h4.01132812c.34101563 0 .61523438.3199219.61523438.7101563s-.27773438.7171875-.61523438.7171875z"/></g></svg>
15-
`,
16-
hint: connectHint(wallet.name)
14+
`
1715
}
1816
}
1917
}
2018
}
2119

22-
function connectHint(walletName: string) {
23-
switch (walletName) {
24-
case 'Ledger':
25-
return 'Make sure your Ledger wallet is plugged in, unlocked and has the Ethereum app open and then click connect.'
26-
default:
27-
return undefined
28-
}
29-
}
30-
3120
export default connect

src/modules/check/loading-accounts.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { WalletCheckModal, StateAndHelpers } from '../../interfaces'
2+
3+
function loadingAccounts() {
4+
// need to assign an id so that it can be checked at to whether it has been included in modules array
5+
return Object.assign(module, { id: 'loadingAccounts' })
6+
}
7+
8+
function module(
9+
stateAndHelpers: StateAndHelpers
10+
): WalletCheckModal | undefined {
11+
const { wallet, address } = stateAndHelpers
12+
if (!address && wallet && wallet.name) {
13+
return {
14+
heading: 'Loading Accounts',
15+
description: getDescription(wallet.name),
16+
eventCode: 'loadingAccounts',
17+
action: wallet.connect,
18+
icon: `
19+
<svg height="14" viewBox="0 0 18 14" width="18" xmlns="http://www.w3.org/2000/svg"><g fill="currentColor"><path d="m10.29375 4.05351563c0-.04921875 0-.09140625 0-.13007813 0-1.0546875 0-2.109375 0-3.1640625 0-.43945312.3480469-.76992188.7804688-.7453125.2003906.01054688.3585937.10546875.4992187.24609375.5800781.58359375 1.1566406 1.16367188 1.7367187 1.74023438 1.4695313 1.46953125 2.9390625 2.93906249 4.4050782 4.40859375.1335937.13359375.2425781.27421875.2707031.46757812.0351562.20742188-.0246094.421875-.1652344.58007813-.0246094.028125-.0492187.05273437-.0738281.08085937-2.0601563 2.06367188-4.1203125 4.1238281-6.1804688 6.1875-.2109375.2109375-.4570312.3023438-.7453125.2179688-.2707031-.0808594-.4464843-.2707032-.5132812-.5484375-.0140625-.0738282-.0175781-.1441407-.0140625-.2179688 0-1.0335937 0-2.0707031 0-3.1042969 0-.0386719 0-.08085935 0-.13359372h-5.06953125c-.49570313 0-.80507813-.309375-.80507813-.80859375 0-1.42382813 0-2.84414063 0-4.26796875 0-.49570313.30585938-.8015625.8015625-.8015625h4.93593748z"/><path d="m5.69882812 13.978125h-4.01132812c-.928125 0-1.6875-.8753906-1.6875-1.9511719v-10.06171872c0-1.07578125.75585938-1.95117188 1.6875-1.95117188h4.01132812c.34101563 0 .61523438.31992188.61523438.71015625 0 .39023438-.27421875.71015625-.61523438.71015625h-4.01132812c-.253125 0-.45703125.23554688-.45703125.52734375v10.06171875c0 .2917969.20390625.5273437.45703125.5273437h4.01132812c.34101563 0 .61523438.3199219.61523438.7101563s-.27773438.7171875-.61523438.7171875z"/></g></svg>
20+
`
21+
}
22+
}
23+
}
24+
25+
function getDescription(walletName: string) {
26+
switch (walletName) {
27+
case 'Ledger':
28+
return 'Your Ledger accounts are currently being loaded, make sure your Ledger wallet: <ol><li>Is plugged in</li><li>Is Unlocked</li><li>Has the Ethereum app open</li></ol> For more info on how to connect, <a style="color: #4a90e2; font-size: 0.889rem; font-family: inherit;" href="https://www.ledger.com/start/" target="_blank" rel="noopener noreferrer">check out the Ledger documentation </a>'
29+
default:
30+
return ''
31+
}
32+
}
33+
34+
export default loadingAccounts

src/modules/select/wallets/authereum.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
import authereumIcon from '../wallet-icons/icon-authereum.png'
22
import { networkName } from '../../../utilities'
3-
import { WalletModule, AuthereumOptions, CommonWalletOptions } from '../../../interfaces'
3+
import {
4+
WalletModule,
5+
AuthereumOptions,
6+
CommonWalletOptions
7+
} from '../../../interfaces'
48

5-
function authereum(options: AuthereumOptions & CommonWalletOptions): WalletModule {
6-
const { networkId, preferred, label, iconSrc, svg, disableNotifications } = options
9+
function authereum(
10+
options: AuthereumOptions & CommonWalletOptions
11+
): WalletModule {
12+
const {
13+
networkId,
14+
preferred,
15+
label,
16+
iconSrc,
17+
svg,
18+
disableNotifications
19+
} = options
720

821
return {
922
name: label || 'Authereum',
@@ -25,9 +38,6 @@ function authereum(options: AuthereumOptions & CommonWalletOptions): WalletModul
2538
name: 'Authereum',
2639
connect: () => provider.enable(),
2740
disconnect: () => instance.logout(),
28-
loading: new Promise((resolve: () => void) => {
29-
instance.on('openPopup', resolve)
30-
}),
3141
address: {
3242
get: () => instance.getAccountAddress()
3343
},
@@ -46,7 +56,9 @@ function authereum(options: AuthereumOptions & CommonWalletOptions): WalletModul
4656
type: 'sdk',
4757
desktop: true,
4858
mobile: true,
49-
url: `https://${networkId !== 1 ? `${networkName(networkId)}.` : ''}authereum.com/`,
59+
url: `https://${
60+
networkId !== 1 ? `${networkName(networkId)}.` : ''
61+
}authereum.com/`,
5062
preferred
5163
}
5264
}

src/modules/select/wallets/ledger.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,23 @@ async function ledgerProvider(options: {
196196
paths.push(`${basePath}/0'/0`)
197197
}
198198

199-
const transport = await TransportU2F.create()
200-
const eth = new Eth(transport)
199+
let transport
200+
let eth
201+
202+
try {
203+
transport = await TransportU2F.create()
204+
eth = new Eth(transport)
205+
} catch (error) {
206+
reject({ message: 'Error connecting to Ledger wallet' })
207+
}
201208

202209
for (const path of paths) {
203210
try {
204211
const { address } = await eth.getAddress(path)
205212
addressToPath.set(address.toLowerCase(), path)
206213
} catch (err) {
207214
return reject({
208-
message: 'An error occurred when trying to get account information'
215+
message: 'There was a problem trying to connect to your Ledger.'
209216
})
210217
}
211218
}
@@ -239,8 +246,15 @@ async function ledgerProvider(options: {
239246

240247
async function signTransaction(transactionData: any) {
241248
const path = [...addressToPath.values()][0]
242-
const transport = await TransportU2F.create()
243-
const eth = new Eth(transport)
249+
let transport
250+
let eth
251+
252+
try {
253+
transport = await TransportU2F.create()
254+
eth = new Eth(transport)
255+
} catch (error) {
256+
throw new Error('Error connecting to Ledger wallet')
257+
}
244258

245259
try {
246260
const transaction = new EthereumTx(transactionData, {
@@ -262,7 +276,7 @@ async function ledgerProvider(options: {
262276

263277
return `0x${transaction.serialize().toString('hex')}`
264278
} catch (error) {
265-
throw new Error(error)
279+
throw new Error('Error signing transaction')
266280
} finally {
267281
transport.close()
268282
}

src/stores.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export const wallet: WritableStore = writable({
5151
connect: null,
5252
instance: null,
5353
url: null,
54-
loading: null,
5554
type: null
5655
})
5756

@@ -116,8 +115,7 @@ export function resetWalletState(options?: {
116115
provider: undefined,
117116
connect: undefined,
118117
instance: undefined,
119-
url: undefined,
120-
loading: undefined
118+
url: undefined
121119
}))
122120

123121
currentInterface.disconnect && currentInterface.disconnect()
@@ -134,8 +132,7 @@ export function resetWalletState(options?: {
134132
provider: undefined,
135133
connect: undefined,
136134
instance: undefined,
137-
url: undefined,
138-
loading: undefined
135+
url: undefined
139136
}))
140137

141138
disconnected &&

src/validation.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -357,25 +357,13 @@ export function validateModal(modal: WalletCheckModal): never | void {
357357
eventCode,
358358
action,
359359
icon,
360-
loading,
361360
html,
362-
hint,
363361
...otherParams
364362
} = modal
365363

366364
invalidParams(
367365
otherParams,
368-
[
369-
'heading',
370-
'description',
371-
'button',
372-
'eventCode',
373-
'action',
374-
'icon',
375-
'loading',
376-
'html',
377-
'hint'
378-
],
366+
['heading', 'description', 'button', 'eventCode', 'action', 'icon', 'html'],
379367
'modal'
380368
)
381369

@@ -389,13 +377,6 @@ export function validateModal(modal: WalletCheckModal): never | void {
389377
optional: true
390378
})
391379

392-
validateType({
393-
name: 'loading',
394-
value: loading,
395-
type: 'object',
396-
optional: true
397-
})
398-
399380
validateType({
400381
name: 'button',
401382
value: button,
@@ -410,13 +391,6 @@ export function validateModal(modal: WalletCheckModal): never | void {
410391
optional: true
411392
})
412393

413-
validateType({
414-
name: 'hint',
415-
value: hint,
416-
type: 'string',
417-
optional: true
418-
})
419-
420394
if (button) {
421395
const { onclick, text, ...restParams } = button
422396
invalidParams(restParams, ['onclick', 'text'], 'button')

0 commit comments

Comments
 (0)