-
Notifications
You must be signed in to change notification settings - Fork 312
/
neonLedger.js
272 lines (252 loc) · 7.76 KB
/
neonLedger.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
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
// @flow
import { tx, wallet, u } from 'neon-js'
import type { Transaction } from 'neon-js'
import LedgerNode from '@ledgerhq/hw-transport-node-hid'
import asyncWrap from '../core/asyncHelper'
const VALID_STATUS = 0x9000
const MSG_TOO_BIG = 0x6d08
const APP_CLOSED = 0x6e00
const TX_DENIED = 0x6985
const TX_PARSE_ERR = 0x6d07
/**
* Evaluates Transport Error thrown and rewrite the error message to be more user friendly.
* @param {Error} err
* @return {Error}
*/
const evalTransportError = err => {
switch (err.statusCode) {
case APP_CLOSED:
err.message = 'Your NEO app is closed! Please login.'
break
case MSG_TOO_BIG:
err.message = 'Your transaction is too big for the ledger to sign!'
break
case TX_DENIED:
err.message = 'You have denied the transaction on your ledger.'
break
case TX_PARSE_ERR:
err.message = 'Error parsing transaction. Make sure your NEO app version is up to date.'
break
}
return err
}
const BIP44 = (acct = 0) => {
const acctNumber = acct.toString(16)
return (
'8000002C' +
'80000378' +
'80000000' +
'00000000' +
'0'.repeat(8 - acctNumber.length) +
acctNumber
)
}
export default class NeonLedger {
path: string;
device: any;
constructor (path: string) {
this.path = path
}
/**
* Initialises by listing devices and trying to find a ledger device connected. Throws an error if no ledgers detected or unable to connect.
* @return {Promise<NeonLedger>}
*/
static async init () {
const supported = await LedgerNode.isSupported()
if (!supported) { throw new Error(`Your computer does not support the ledger!`) }
const paths = await NeonLedger.list()
if (paths.length === 0) throw new Error('USB Error: No device found.')
const ledger = new NeonLedger(paths[0])
return ledger.open()
}
static async list (): Promise<string[]> {
return LedgerNode.list()
}
/**
* Opens an connection with the selected ledger.
* @return {Promise<NeonLedger>}this
*/
async open (): Promise<NeonLedger> {
try {
this.device = await LedgerNode.open(this.path)
return this
} catch (err) {
throw evalTransportError(err)
}
}
/**
* Closes the connection between the Ledger and the wallet.
* @return {Promise<void>}}
*/
close (): Promise<void> {
if (this.device) return this.device.close()
return Promise.resolve()
}
/**
* Retrieves the public key of an account from the Ledger.
* @param {number} [acct] - Account that you want to retrieve the public key from.
* @return {string} Public Key (Unencoded)
*/
async getPublicKey (acct: number = 0): Promise<string> {
const res = await this.send('80040000', BIP44(acct), [VALID_STATUS])
return res.toString('hex').substring(0, 130)
}
getDeviceInfo () {
try {
return this.device.device.getDeviceInfo()
} catch (err) {
throw evalTransportError(err)
}
}
/**
* Sends an message with params over to the Ledger.
* @param {string} params - params as a hexstring
* @param {string} msg - Message as a hexstring
* @param {number[]} statusList - Statuses to return
* @return {Promise<Buffer>} return value decoded to ASCII string
*/
async send (
params: string,
msg: string,
statusList: number[]
): Promise<Buffer> {
if (params.length !== 8) throw new Error(`params requires 4 bytes`)
// $FlowFixMe
const [cla, ins, p1, p2] = params
.match(/.{1,2}/g)
.map(i => parseInt(i, 16))
try {
return await this.device.send(
cla,
ins,
p1,
p2,
Buffer.from(msg, 'hex'),
statusList
)
} catch (err) {
throw evalTransportError(err)
}
}
/**
* Gets the ECDH signature of the data from Ledger using acct
* @param {string} data
* @param {number} [acct]
* @return {Promise<string>}
*/
async getSignature (data: string, acct: number = 0): Promise<string> {
data += BIP44(acct)
let response = null
const chunks = data.match(/.{1,510}/g) || []
if (!chunks.length) throw new Error(`Invalid data provided: ${data}`)
for (let i = 0; i < chunks.length; i++) {
const p = i === chunks.length - 1 ? '80' : '00'
// $FlowFixMe
const chunk = chunks[i]
const params = `8002${p}00`
let [err, res] = await asyncWrap(
this.send(params, chunk, [VALID_STATUS])
)
if (err) throw evalTransportError(err)
response = res
}
if (response === 0x9000) {
throw new Error(`No more data but Ledger did not return signature!`)
}
// $FlowFixMe
return assembleSignature(response.toString('hex'))
}
}
/**
* The signature is returned from the ledger in a DER format
* @param {string} response - Signature in DER format
*/
const assembleSignature = (response: string): string => {
let ss = new u.StringStream(response)
// The first byte is format. It is usually 0x30 (SEQ) or 0x31 (SET)
// The second byte represents the total length of the DER module.
ss.read(2)
// Now we read each field off
// Each field is encoded with a type byte, length byte followed by the data itself
ss.read(1) // Read and drop the type
const r = ss.readVarBytes()
ss.read(1)
const s = ss.readVarBytes()
// We will need to ensure both integers are 32 bytes long
const integers = [r, s].map(i => {
if (i.length < 64) {
i = i.padStart(64, '0')
}
if (i.length > 64) {
i = i.substr(-64)
}
return i
})
return integers.join('')
}
export const getPublicKey = async (acct: number = 0): Promise<string> => {
const ledger = await NeonLedger.init()
try {
return await ledger.getPublicKey(acct)
} finally {
await ledger.close()
}
}
export const getDeviceInfo = async () => {
const ledger = await NeonLedger.init()
try {
return await ledger.getDeviceInfo()
} finally {
await ledger.close()
}
}
/**
* Signs a transaction with Ledger. Returns the whole transaction string
* @param {Transaction|string} unsignedTx - hexstring or Transaction object
* @param {number} acct - The account to sign with.
* @return {string} Transaction as a hexstring.
*/
export const signWithLedger = async (
unsignedTx: Transaction | string,
acct: number = 0
): Promise<string> => {
const ledger = await NeonLedger.init()
try {
const data =
typeof unsignedTx !== 'string'
? tx.serializeTransaction(unsignedTx, false)
: unsignedTx
const publicKey = await ledger.getPublicKey(acct)
const invocationScript = '40' + (await ledger.getSignature(data, acct))
const verificationScript = wallet.getVerificationScriptFromPublicKey(
publicKey
)
const txObj = tx.deserializeTransaction(data)
txObj.scripts.push({ invocationScript, verificationScript })
return tx.serializeTransaction(txObj)
} finally {
await ledger.close()
}
}
export const legacySignWithLedger = async (
unsignedTx: Transaction | string,
publicKeyEncoded: string,
acct: number = 0
): Promise<string> => {
const ledger = await NeonLedger.init()
try {
const data =
typeof unsignedTx !== 'string'
? tx.serializeTransaction(unsignedTx, false)
: unsignedTx
const invocationScript = '40' + (await ledger.getSignature(data, acct))
const verificationScript = wallet.getVerificationScriptFromPublicKey(
publicKeyEncoded
)
const txObj = tx.deserializeTransaction(data)
txObj.scripts.push({ invocationScript, verificationScript })
return tx.serializeTransaction(txObj)
} finally {
await ledger.close()
}
}