This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
forked from MetaMask/eth-ledger-bridge-keyring
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathledger-bridge.js
197 lines (175 loc) · 5.29 KB
/
ledger-bridge.js
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
const TransportWebHID = require('@ledgerhq/hw-transport-webhid').default
const LedgerEth = require('@ledgerhq/hw-app-eth').default
const WebSocketTransport = require('@ledgerhq/hw-transport-http/lib/WebSocketTransport').default
'use strict'
require('buffer')
// URL which triggers Ledger Live app to open and handle communication
const BRIDGE_URL = 'ws://localhost:8435'
// Number of seconds to poll for Ledger Live and Ethereum app opening
const TRANSPORT_CHECK_DELAY = 1000
const TRANSPORT_CHECK_LIMIT = 120
module.exports = class LedgerBridge {
constructor () {
this.useLedgerLive = false
}
request (data, cb) {
const { action, params } = data
switch (action) {
case 'ledger-unlock':
this.unlock(cb, params.hdPath)
break
case 'ledger-sign-transaction':
this.signTransaction(cb, params.hdPath, params.tx)
break
case 'ledger-sign-personal-message':
this.signPersonalMessage(cb, params.hdPath, params.message)
break
case 'ledger-close-bridge':
this.cleanUp(cb)
break
case 'ledger-update-transport':
this.updateLedgerLivePreference(cb, params.useLedgerLive)
break
case 'ledger-sign-typed-data':
this.signTypedData(cb, params.hdPath, params.domainSeparatorHex, params.hashStructMessageHex)
break
default:
break
}
}
delay (ms) {
return new Promise((success) => setTimeout(success, ms))
}
checkTransportLoop (i) {
const iterator = i || 0
return WebSocketTransport.check(BRIDGE_URL).catch(async () => {
await this.delay(TRANSPORT_CHECK_DELAY)
if (iterator < TRANSPORT_CHECK_LIMIT) {
return this.checkTransportLoop(iterator + 1)
}
throw new Error('Ledger transport check timeout')
})
}
async makeApp () {
try {
if (this.useLedgerLive) {
let reestablish = false
try {
await WebSocketTransport.check(BRIDGE_URL)
} catch (_err) {
window.open('ledgerlive://bridge?appName=Ethereum')
await this.checkTransportLoop()
reestablish = true
}
if (!this.app || reestablish) {
this.transport = await WebSocketTransport.open(BRIDGE_URL)
this.app = new LedgerEth(this.transport)
}
} else {
window.TransportWebHID = TransportWebHID
window.LedgerEth = LedgerEth
this.transport = await TransportWebHID.create()
this.app = new LedgerEth(this.transport)
}
} catch (e) {
console.log('LEDGER:::CREATE APP ERROR', e)
throw e
}
}
updateLedgerLivePreference (cb, useLedgerLive) {
this.useLedgerLive = useLedgerLive
this.cleanUp()
cb(true)
}
cleanUp (cb) {
this.app = null
if (this.transport) {
this.transport.close()
}
if (cb) {
cb(true)
}
}
async unlock (cb, hdPath) {
try {
await this.makeApp()
const res = await this.app.getAddress(hdPath, false, true)
cb(true, res)
} catch (err) {
const e = this.ledgerErrToMessage(err)
cb(false, { error: e.toString() })
} finally {
if (!this.useLedgerLive) {
this.cleanUp()
}
}
}
async signTransaction (cb, hdPath, tx) {
try {
await this.makeApp()
const res = await this.app.signTransaction(hdPath, tx)
cb(true, res)
} catch (err) {
const e = this.ledgerErrToMessage(err)
cb(false, { error: e.toString() })
} finally {
if (!this.useLedgerLive) {
this.cleanUp()
}
}
}
async signPersonalMessage (cb, hdPath, message) {
try {
await this.makeApp()
const res = await this.app.signPersonalMessage(hdPath, message)
cb(true, res)
} catch (err) {
const e = this.ledgerErrToMessage(err)
cb(false, { error: e.toString() })
} finally {
if (!this.useLedgerLive) {
this.cleanUp()
}
}
}
async signTypedData (cb, hdPath, domainSeparatorHex, hashStructMessageHex) {
try {
await this.makeApp()
const res = await this.app.signEIP712HashedMessage(hdPath, domainSeparatorHex, hashStructMessageHex)
cb(true, res)
} catch (err) {
const e = this.ledgerErrToMessage(err)
cb(false, { error: e.toString() })
} finally {
this.cleanUp()
}
}
ledgerErrToMessage (err) {
const isU2FError = (err) => Boolean(err) && Boolean((err).metaData)
const isStringError = (err) => typeof err === 'string'
const isErrorWithId = (err) => err.hasOwnProperty('id') && err.hasOwnProperty('message')
const isWrongAppError = (err) => String(err.message || err).includes('6804')
const isLedgerLockedError = (err) => err.message && err.message.includes('OpenFailed')
// https://developers.yubico.com/U2F/Libraries/Client_error_codes.html
if (isU2FError(err)) {
if (err.metaData.code === 5) {
return 'LEDGER_TIMEOUT'
}
return err.metaData.type
}
if (isWrongAppError(err)) {
return 'LEDGER_WRONG_APP'
}
if (isLedgerLockedError(err) || (isStringError(err) && err.includes('6801'))) {
return 'LEDGER_LOCKED'
}
if (isErrorWithId(err)) {
// Browser doesn't support U2F
if (err.message.includes('U2F not supported')) {
return 'U2F_NOT_SUPPORTED'
}
}
// Other
return err.toString()
}
}