-
Notifications
You must be signed in to change notification settings - Fork 15
/
HardwareWalletModal.vue
executable file
·402 lines (366 loc) · 10.2 KB
/
HardwareWalletModal.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<template>
<b-modal
id="hd-wallet-modal"
v-model="visible"
title="Hardware wallet"
hide-footer
centered
no-close-on-backdrop
>
<b-form style="display: flex; flex-direction: column;">
<b-alert
show
variant="warning">
{{ $t('components.modals.hardware_wallet_modal.this_option_deprecated') }}</b-alert>
<b-alert
variant="info"
:show="ledgerLocked === true"
>{{ $t('components.modals.hardware_wallet_modal.ledger_locked') }}</b-alert>
<b-alert :show="!!errorMsg">{{errorMsg}}</b-alert>
<div v-if="!hdWallet && ledgerLocked === false">
<b-spinner label="Loading" />{{ $t('components.modals.hardware_wallet_modal.initializing') }}
</div>
<b-form-select class="mb-2" :value="null" :options="paths" v-model="selectedPath">
<option slot="first" :value="null">{{ $t('components.modals.hardware_wallet_modal.select_path') }}</option>
</b-form-select>
<b-list-group class="account-list" v-if="selectedPath">
<b-list-group-item
v-for="option in accounts"
:key="option.address"
:active="option === account"
@click="account = option"
>
<address>{{formatAddress(option.address)}}</address>
<div class="balance">{{option.balance | tokenAmount}} ETH</div>
</b-list-group-item>
<b-list-group-item class="load-more" @click="loadMore" v-if="!loadingAccounts">{{ $t('components.modals.hardware_wallet_modal.load_more') }}</b-list-group-item>
<b-list-group-item class="loading" v-else>
<b-spinner label="Loading" />
</b-list-group-item>
</b-list-group>
<footer slot="modal-footer" class="mt-3">
<b-button
class="btn proceed-btn"
:disabled="account === null"
variant="primary"
@click="connect(account)"
>{{ $t('components.modals.hardware_wallet_modal.use_account') }}</b-button>
</footer>
</b-form>
</b-modal>
</template>
<script lang="ts">
import Vue from "vue"
import { Component, Watch } from "vue-property-decorator"
import { pathsArr as hdPaths } from "@/services/ledger/paths"
import { DashboardState } from "@/types"
import Web3 from "web3"
import { ethereumModule } from "@/store/ethereum"
import { feedbackModule as feedback } from "@/feedback/store"
import TransportU2F from "@ledgerhq/hw-transport-u2f"
import createLedgerSubprovider from "@ledgerhq/web3-subprovider"
import ProviderEngine from "web3-provider-engine"
import FetchSubprovider from "web3-provider-engine/subproviders/fetch"
import { createWallet, CustomLedgerWallet } from "@/services/ledger/ledgerWallet"
import { of, from } from "rxjs"
import { map, tap, flatMap, concatMap } from "rxjs/operators"
interface LedgerAccount {
address: string
balance: any
path: string
}
@Component
export default class HardwareWalletModal extends Vue {
_transport!: TransportU2F
// Pagination
// rows = 100
// perPage = 10
// currentPage = 1
ledgerLocked = false
hdWallet: CustomLedgerWallet | null = null
readonly maxAddresses = 100
errorMsg: any = null
accounts: any[] = []
path = ""
account: LedgerAccount | null = null
derivationPath = ""
readonly paths = hdPaths.paths.map((item) => ({ value: item.path, text: item.label }))
selectedPath: string = ""
web3: Web3 | null = null
infura!: Web3
ledger: any = null
loading = false
loadingAccounts: boolean = false
get visible(): boolean {
const state = this.state.ethereum
return state.walletType === "ledger" && state.signer === null
}
set visible(val) {
if (val === false) {
// disconnect ledger?
ethereumModule.clearWalletType()
}
}
get state(): DashboardState {
return this.$store.state
}
async transport() {
if (!this._transport) {
try {
this._transport = await TransportU2F.create(3000, 10000)
} catch (e) {
this.errorMsg = "Unable to connect to Ledger"
console.error("unable to create TransportU2F")
console.error(e)
}
}
return this._transport
}
calculatePath(path, offset) {
const derivationPath = path.replace("m/", "")
if (derivationPath === "44'/60'") {
// Ethereum addresses (Ledger live)
return `44'/60'/${offset}'/0/0`
} else if (derivationPath === "44'/60'/0'") {
// Ethereum addresses (legacy)
return `${derivationPath}/${offset}`
}
throw new Error("Don't know how to handle path " + path)
}
async connect(account: LedgerAccount) {
const selectedAddress = account.address
const path = this.calculatePath(this.selectedPath, this.accounts.indexOf(account))
const networkId = Number(this.state.ethereum.networkId)
const rpcUrl = this.state.ethereum.endpoint
const engine = new ProviderEngine()
const transport = await this.transport()
const ledger = createLedgerSubprovider(
() => transport, {
networkId,
accountsLength: 1,
path,
})
ledger.signMessage = ledger.signPersonalMessage
engine.addProvider(ledger)
engine.addProvider(
// new RpcSubprovider({ rpcUrl }),
new FetchSubprovider({
rpcUrl,
}),
)
engine.start()
// @ts-ignore
// const web3account = (await .web3!.eth.getAccounts())[0]
// console.assert(web3account === selectedAddress,
// `Expected web3 to be initialized with ${selectedAddress} but got ${web3account}`)
// @ts-ignore
ethereumModule.setProvider(engine)
}
@Watch("selectedPath")
onPathChange() {
this.loadMore()
}
@Watch("selectedPath")
async loadAccounts() {
this.accounts = []
}
async loadMore() {
const transport = await this.transport()
const getTransport = () => transport
const path = this.selectedPath
const networkId = Number(this.state.ethereum.networkId)
const accountsLength = 4
const offset = this.accounts.length
// stop the loop when selectedPath has chaned or max accounts reached
if (offset + accountsLength >= 100) {
return
}
// console.log("loading,", path, offset, networkId, accountsLength)
this.loadingAccounts = true
const ledger = createLedgerSubprovider(
getTransport
, {
networkId,
accountsLength,
path: this.calculatePath(path, offset),
})
const t = Date.now()
// console.log("getAccounts")
ledger.getAccounts((error, accounts: string[]) => {
console.log("getAccounts", (Date.now() - t) / 1000)
if (error) {
// show error "please unlock your ledger wallet and go to the etherum app"
console.log(error)
} else {
from(accounts)
.pipe(
map((address) => ({ address, balance: -1 })),
concatMap(this.loadBalance),
).subscribe(
(account) => this.accounts.push(account),
console.error,
() => this.loadingAccounts = false,
)
}
})
}
async loadBalance(account) {
account.balance = await this.infura.eth.getBalance(account.address)
return account
}
formatAddress(address) {
const cap = 10
return address.slice(0, cap) + "..." + address.slice(-cap, address.length)
}
@Watch("visible")
async onToggle(visible) {
if (visible === false) return
const endpoint = ethereumModule.state.endpoint
const web3Provider = /^ws/.test(endpoint) ?
new Web3.providers.WebsocketProvider(endpoint) :
new Web3.providers.HttpProvider(endpoint)
console.log("visible")
// await this.setWeb3Instance()
try {
this.loading = true
// this.infura = new Web3(new Web3.providers.HttpProvider(this.state.ethereum.endpoint))
this.infura = new Web3(web3Provider)
this.hdWallet = await createWallet()
} catch (err) {
console.error(err)
this.ledgerLocked = true
// feedback.showInfo("Please unlock your ledger and go to ethereum app")
// this.$root.$emit("bv::show::modal", "unlock-ledger-modal")
this.loading = false
return
}
this.loading = false
}
async init() {
// await this.setWeb3Instance()
this.ledgerLocked = false
try {
this.loading = true
this.hdWallet = await createWallet()
this.loading = false
} catch (err) {
this.ledgerLocked = true
// feedback.showInfo("Please unlock your ledger and go to ethereum app")
// this.$root.$emit("bv::show::modal", "unlock-ledger-modal")
this.loading = false
return
}
}
}
</script>
<style lang="scss">
.account-list {
counter-reset: address;
max-height: calc(100vh - 300px);
overflow-y: scroll;
.list-group-item {
display: flex;
> address {
margin: 0;
font-family: Monaco, "Lucida Console", monospace;
width: 260px;
font-size: 14px;
}
&::before {
display: block;
text-align: center;
padding-right: 10px;
font-size: 12px;
font-family: inherit;
counter-increment: address;
content: counter(address);
width: 36px;
}
&.load-more::before,
&.loading::before {
content: "";
}
.balance {
flex: 1;
text-align: right;
}
}
}
label {
color: gray;
}
#confirm-seed-modal .modal-dialog {
width: 500px;
max-width: 500px;
.modal-header {
margin-left: 10px;
margin-right: 10px;
padding-left: 5px;
padding-right: 5px;
h5 {
color: gray;
}
}
.modal-body {
.col-sm-3,
.col-sm-9 {
padding: 0;
}
.text-error {
color: red;
}
.btn {
width: 150px;
}
}
}
.pagination-container {
display: flex;
align-items: center;
justify-content: center;
padding: 24px;
.pagination {
margin: 0;
}
}
.address-table {
max-height: 280px;
overflow-y: scroll;
}
.wallet-config-container {
width: 100%;
.card-body {
padding: 0px;
max-height: 250px;
overflow: scroll;
}
.custom-control-label::before {
position: relative;
}
table {
margin-bottom: 0px;
td {
white-space: no-wrap;
padding: 0.5rem;
}
tr {
width: 100%;
display: inline-table;
}
}
.form-group {
margin-bottom: 0px;
}
}
.proceed-btn {
margin-left: auto;
}
.dropdown-container {
width: 100%;
input {
width: 100%;
border: 2px solid #f2f1f3;
padding: 4px 12px;
}
}
</style>