-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathledger-wrapper.ts
More file actions
643 lines (601 loc) · 21.9 KB
/
ledger-wrapper.ts
File metadata and controls
643 lines (601 loc) · 21.9 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
import TransportWebHID from '@ledgerhq/hw-transport-webhid'
import {
DerivedPublicKey,
KeyParameters,
LedgerDeriveAndDisplayAddressRequest,
LedgerDeviceIdRequest,
LedgerPublicKeyRequest,
LedgerSignChallengeRequest,
LedgerSignSubintentHashRequest,
LedgerSignTransactionRequest,
SignatureOfSigner,
} from 'ledger/schemas'
import { ResultAsync, err, ok, okAsync } from 'neverthrow'
import { bufferToChunks } from 'utils'
import { logger as utilsLogger } from 'utils/logger'
import {
LedgerErrorCode,
LedgerInstructionCode,
LedgerInstructionClass,
} from './constants'
import { encodeDerivationPath } from './encode-derivation-path'
import { getDataLength } from './utils'
import { LedgerSubjects } from './subjects'
import { parseSignAuth } from './parse-sign-auth'
import Transport from '@ledgerhq/hw-transport'
export const ledgerLogger = utilsLogger.getSubLogger({ name: 'ledger' })
export type LedgerOptions = Partial<{
transport: typeof TransportWebHID
ledgerSubjects: ReturnType<typeof LedgerSubjects>
}>
export type AdditionalExchangeParams = {
p1?: string
instructionClass?: LedgerInstructionClass
}
const ledgerModel: Record<string, string> = {
'00': 'nanoS',
'01': 'nanoS+',
'02': 'nanoX',
}
/**
* Send specific hex string to the Ledger device and return the response.
*/
export type ExchangeFn = (
command: LedgerInstructionCode,
data?: string,
additionalParams?: AdditionalExchangeParams,
) => ResultAsync<string, string>
/**
* Radix Wallet supports two types of curves: Curve25519 and secp256k1.
* This function returns the configuration which is dependent on the curve.
*/
const getCurveConfig = ({ curve }: KeyParameters) =>
({
curve25519: {
publicKeyByteCount: 32,
signatureByteCount: 64,
signPreAuth: LedgerInstructionCode.SignPreAuthHashEd25519,
signTx: LedgerInstructionCode.SignTxEd25519,
signAuth: LedgerInstructionCode.SignAuthEd25519,
getPublicKey: LedgerInstructionCode.GetPubKeyEd25519,
deriveAndDisplay: LedgerInstructionCode.DeriveAndDisplayAddressEd25519,
},
secp256k1: {
publicKeyByteCount: 33,
signatureByteCount: 65,
signPreAuth: LedgerInstructionCode.SignPreAuthHashSecp256k1,
signTx: LedgerInstructionCode.SignTxSecp256k1,
signAuth: LedgerInstructionCode.SignAuthSecp256k1,
getPublicKey: LedgerInstructionCode.GetPubKeySecp256k1,
deriveAndDisplay: LedgerInstructionCode.DeriveAndDisplayAddressSecp256k1,
},
})[curve]
/**
* Main module used for interacting with the Ledger device. It initializes and keeps track
* of transport layer and provides highest level methods for interacting with the Ledger device.
*
* There is single communication pipe with Ledger device whereas there might be multiple
* (or duplicated) requests to this module. `lastInteractionId` is always saved when new requests goes in
*/
export const LedgerWrapper = ({
transport = TransportWebHID,
ledgerSubjects = LedgerSubjects(),
}: LedgerOptions) => {
let currentTransport: Transport | undefined
let lastInteractionId: string | undefined
const setProgressMessage = (message: string) =>
ledgerSubjects.onProgressSubject.next(message)
const listLedgerDevices = () =>
ResultAsync.fromPromise(
transport.list(),
() => LedgerErrorCode.FailedToListLedgerDevices,
).andThen((devices) => {
ledgerLogger.debug(
'Found Ledger devices',
devices.map(
({ productName, productId }) => `${productId}, ${productName}`,
),
)
if (devices.length > 1) {
return err(LedgerErrorCode.MultipleLedgerConnected)
}
ledgerSubjects.connectedDeviceIdSubject.next(devices?.[0]?.productId)
return ok(undefined)
})
const createLedgerTransport = (): ResultAsync<
{ closeTransport: () => ResultAsync<void, string>; exchange: ExchangeFn },
string
> => {
if (currentTransport) {
ledgerLogger.debug('📒 closing current transport')
return ResultAsync.fromPromise(
currentTransport.close().then(() => {
currentTransport = undefined
}),
(e) => {
ledgerLogger.error(e)
return 'failedToCloseExistingTransport'
},
).andThen(() => createLedgerTransport())
}
return listLedgerDevices().andThen(() =>
ResultAsync.fromPromise(
transport.create(),
() => LedgerErrorCode.FailedToCreateTransport,
)
.andThen((transport) => {
ledgerLogger.debug('📒 transport layer created')
return listLedgerDevices().map(() => transport)
})
.map((transport) => {
currentTransport = transport
setProgressMessage(' ')
const exchange: ExchangeFn = (
command: LedgerInstructionCode,
data = '',
{
p1 = '00',
instructionClass = LedgerInstructionClass.aa,
}: AdditionalExchangeParams = {},
) => {
const ledgerInput = `${instructionClass}${command}${p1}00${data}`
ledgerLogger.debug('📒 sending', ledgerInput)
return ResultAsync.fromPromise(
transport.exchange(Buffer.from(ledgerInput, 'hex')),
() => LedgerErrorCode.FailedToExchangeData,
).andThen((buffer) => {
const stringifiedResponse = buffer.toString('hex')
ledgerLogger.debug(`📒 received`, stringifiedResponse)
const statusCode = stringifiedResponse.slice(-4)
if (statusCode !== '9000') {
return err(statusCode)
}
return ok(stringifiedResponse.slice(0, -4))
})
}
return {
closeTransport: () => {
setProgressMessage('')
ledgerSubjects.connectedDeviceIdSubject.next(undefined)
currentTransport = undefined
return ResultAsync.fromPromise(
transport.close(),
() => 'failedClosingTransport',
)
},
exchange,
}
}),
)
}
/**
* Wraps the exchange function to ensure that the transport layer is closed after the exchange is done.
* It accepts a function that takes the exchange function as an argument and returns a ResultAsync.
* `exchangeFn` is a function that is capable of data to the Ledger device and returns a ResultAsync.
*/
const wrapDataExchange = (
fn: (exchangeFn: ExchangeFn) => ResultAsync<any, string>,
) =>
createLedgerTransport()
.andThen(({ closeTransport, exchange }) =>
fn(exchange)
.andThen((response) => closeTransport().map(() => response))
.mapErr((error) => {
closeTransport()
return error
}),
)
.mapErr((error) => {
ledgerSubjects.connectedDeviceIdSubject.next(undefined)
setProgressMessage('')
return error
})
/**
* Each time Radix Wallet is asking for ledger data it sends specific ledger identifier with the request.
* This is the reason connector extension asks Ledger Babylon App for public key using `getPublicKeys`
* method before anything else happens
*/
const ensureCorrectDeviceId =
(expectedDeviceId: string) => (ledgerDeviceId: string) => {
if (ledgerDeviceId === expectedDeviceId) {
return ok(undefined)
} else {
return err(LedgerErrorCode.DeviceMismatch)
}
}
const parseGetPublicKeyParams =
(params: Omit<LedgerPublicKeyRequest, 'discriminator' | 'interactionId'>) =>
() =>
ok(
params.keysParameters.map((keyParameter) => {
const { getPublicKey } = getCurveConfig(keyParameter)
const encodedDerivationPath = encodeDerivationPath(
keyParameter.derivationPath,
)
return {
...keyParameter,
getPublicKey,
encodedDerivationPath,
}
}),
)
const parseSignerParams = (signer: KeyParameters) => {
const {
signTx,
signPreAuth,
signatureByteCount,
publicKeyByteCount,
signAuth: signAuthCommand,
} = getCurveConfig(signer)
const encodedDerivationPath = encodeDerivationPath(signer.derivationPath)
return {
command: signTx,
signPreAuth,
signAuthCommand,
encodedDerivationPath,
signatureByteCount,
publicKeyByteCount,
}
}
const parsePreAuthorizationParams = (
params: Omit<
LedgerSignSubintentHashRequest,
'discriminator' | 'interactionId'
>,
) => {
const dataLength = getDataLength(params.subintentHash)
return okAsync({ hash: `${dataLength}${params.subintentHash}` })
}
const parseSignTransactionParams =
(
params: Omit<
LedgerSignTransactionRequest,
'discriminator' | 'interactionId'
>,
) =>
() => {
const compiledTxIntentChunksResult = bufferToChunks(
Buffer.from(params.compiledTransactionIntent, 'hex'),
255,
)
if (compiledTxIntentChunksResult.isErr())
return err('error chunking data')
const apduChunks = compiledTxIntentChunksResult.value.map(
(chunk, index) => {
const stringifiedChunk = chunk.toString('hex')
const chunkLength = getDataLength(stringifiedChunk)
const chunkWithLength = `${chunkLength}${stringifiedChunk}`
return {
chunk: chunkWithLength,
instructionClass:
index === compiledTxIntentChunksResult.value.length - 1
? LedgerInstructionClass.ac
: LedgerInstructionClass.ab,
}
},
)
const p1 = params.displayHash ? '01' : '00'
return ok({ p1, apduChunks })
}
/**
* Gets identification information from Ledger
*
* @returns ResultAsync with `deviceId` and `model` inside object
*/
const getDeviceInfo = (): ResultAsync<
{ deviceId: string; model: string },
string
> =>
wrapDataExchange((exchange) =>
exchange(LedgerInstructionCode.GetDeviceId).andThen((deviceId) =>
exchange(LedgerInstructionCode.GetDeviceModel).map((model) => ({
id: deviceId,
model: ledgerModel[model],
})),
),
)
/**
* Derives public keys for one or more provided derivation paths and curves.
*
* @returns ResultAsync with list of objects. Objects containing `publicKey`,`derivationPath`,`curve`
*/
const getPublicKeys = (
params: Omit<LedgerPublicKeyRequest, 'discriminator' | 'interactionId'>,
): ResultAsync<DerivedPublicKey[], string> =>
wrapDataExchange((exchange) =>
exchange(LedgerInstructionCode.GetDeviceId)
.andThen(ensureCorrectDeviceId(params.ledgerDevice.id))
.andThen(parseGetPublicKeyParams(params))
.andThen((keysParameters) => {
return keysParameters.reduce(
(
acc: ResultAsync<DerivedPublicKey[], string>,
{ getPublicKey, encodedDerivationPath, curve, derivationPath },
) =>
acc.andThen((derivedPublicKeys) =>
exchange(getPublicKey, encodedDerivationPath).map(
(publicKey) => [
...derivedPublicKeys,
{ publicKey, derivationPath, curve },
],
),
),
okAsync([]),
)
}),
)
const deriveAndDisplayAddress = (
params: Omit<
LedgerDeriveAndDisplayAddressRequest,
'discriminator' | 'interactionId'
>,
) =>
wrapDataExchange((exchange) =>
exchange(LedgerInstructionCode.GetDeviceId)
.andThen(ensureCorrectDeviceId(params.ledgerDevice.id))
.andThen(() => {
const keyParameter = params.keyParameters
const { deriveAndDisplay, getPublicKey } =
getCurveConfig(keyParameter)
const encodedDerivationPath = encodeDerivationPath(
keyParameter.derivationPath,
)
return exchange(getPublicKey, encodedDerivationPath).andThen(
(publicKey) =>
exchange(deriveAndDisplay, encodedDerivationPath).map(
(result) => ({
derivedKey: {
...keyParameter,
publicKey,
},
address: Buffer.from(result, 'hex').toString(),
}),
),
)
}),
)
/**
* Signs auth challenge with provided curves and derivation paths
* @returns List of signatures `SignatureOfSigner`
*/
const signAuth = (
params: Omit<LedgerSignChallengeRequest, 'discriminator' | 'interactionId'>,
): ResultAsync<SignatureOfSigner[], string> =>
wrapDataExchange((exchange) =>
exchange(LedgerInstructionCode.GetDeviceId)
.andThen(ensureCorrectDeviceId(params.ledgerDevice.id))
.map(() => parseSignAuth(params))
.andThen(({ challengeData }) =>
params.signers.reduce(
(acc: ResultAsync<SignatureOfSigner[], string>, signer, index) =>
acc.andThen((signatures) => {
const {
signAuthCommand,
signatureByteCount,
publicKeyByteCount,
encodedDerivationPath,
} = parseSignerParams(signer)
return exchange(signAuthCommand, encodedDerivationPath)
.andThen(() =>
exchange(signAuthCommand, challengeData, {
instructionClass: LedgerInstructionClass.ac,
}),
)
.andThen((result) => {
const publicKey = result.slice(
signatureByteCount * 2,
signatureByteCount * 2 + publicKeyByteCount * 2,
)
const signature = result.slice(0, signatureByteCount * 2)
return ok({
signature,
publicKey,
})
})
.map(({ signature, publicKey }) => {
const entry: SignatureOfSigner = {
derivedPublicKey: {
...signer,
publicKey,
},
signature,
}
return [...signatures, entry]
})
}),
okAsync([]),
),
),
)
/**
* Signs subintent (preauthorization) with provided curves and derivation paths
* @returns List of signatures `SignatureOfSigner`
*/
const signSubintent = (
params: Omit<
LedgerSignSubintentHashRequest,
'discriminator' | 'interactionId'
>,
): ResultAsync<SignatureOfSigner[], string> =>
wrapDataExchange((exchange) =>
exchange(LedgerInstructionCode.GetDeviceId)
.andThen(ensureCorrectDeviceId(params.ledgerDevice.id))
.andThen(() => parsePreAuthorizationParams(params))
.andThen(({ hash }) =>
params.signers.reduce(
(acc: ResultAsync<SignatureOfSigner[], string>, signer, index) =>
acc.andThen((signatures) => {
const {
signPreAuth,
signatureByteCount,
publicKeyByteCount,
encodedDerivationPath,
} = parseSignerParams(signer)
return exchange(signPreAuth, encodedDerivationPath)
.andThen(() =>
exchange(signPreAuth, hash, {
instructionClass: LedgerInstructionClass.ac,
}),
)
.andThen((result) => {
const publicKey = result.slice(
signatureByteCount * 2,
signatureByteCount * 2 + publicKeyByteCount * 2,
)
const signature = result.slice(0, signatureByteCount * 2)
return ok({
signature,
publicKey,
})
})
.map(({ signature, publicKey }) => {
const entry: SignatureOfSigner = {
derivedPublicKey: {
...signer,
publicKey,
},
signature,
}
return [...signatures, entry]
})
}),
okAsync([]),
),
),
)
/**
* Gathers all required signatures for a single transaction signing.
* If there are more then X signatures this function will send an event
* through `ledgerSubjects.onProgressSubject` with the current progress.
* @param params
* @returns List of signatures `SignatureOfSigner`
*/
const signTransaction = (
params: Omit<
LedgerSignTransactionRequest,
'discriminator' | 'interactionId'
>,
): ResultAsync<SignatureOfSigner[], string> =>
wrapDataExchange((exchange) =>
exchange(LedgerInstructionCode.GetDeviceId)
.andThen(ensureCorrectDeviceId(params.ledgerDevice.id))
.andThen(parseSignTransactionParams(params))
.andThen(({ p1, apduChunks }) =>
params.signers.reduce(
(signersAcc: ResultAsync<SignatureOfSigner[], string>, signer) => {
const {
command,
signatureByteCount,
publicKeyByteCount,
encodedDerivationPath,
} = parseSignerParams(signer)
const digestLength = 32 * 2
return signersAcc.andThen((previousValue) => {
return exchange(command, encodedDerivationPath, { p1 })
.andThen(() =>
apduChunks.reduce(
(
acc: ResultAsync<string, string>,
{ chunk, instructionClass },
index,
) => {
return acc.andThen(() => {
if (apduChunks.length > 30) {
setProgressMessage(
`Please wait a moment - this is a large transaction... (${Math.round(
((index + 1) / apduChunks.length) * 100,
)} %)`,
)
}
return exchange(command, chunk, {
instructionClass,
p1,
})
})
},
okAsync(''),
),
)
.andThen((result) => {
if (
result.length - digestLength !==
(signatureByteCount + publicKeyByteCount) * 2
) {
ledgerLogger.error(
`Result length is ${result.length} whereas it should be (signature) ${signatureByteCount} bytes * 2 + (publicKey) ${publicKeyByteCount} bytes * 2 + digest length: ${digestLength}`,
)
return err(
'Result containing signature and PublicKey has incorrect length.',
)
}
const signature = result.slice(0, signatureByteCount * 2)
const sigOffset = signatureByteCount * 2
const publicKey = result.slice(
sigOffset,
sigOffset + 2 * publicKeyByteCount,
)
if (signature.length !== signatureByteCount * 2) {
ledgerLogger.error(
`Signature length is ${signature.length} whereas it should be ${signatureByteCount} bytes * 2`,
)
return err('Signature has incorrect length.')
}
if (publicKey.length !== publicKeyByteCount * 2) {
ledgerLogger.error(
`Public Key length is ${publicKey.length} whereas it should be ${publicKeyByteCount} bytes * 2`,
)
return err('PublicKey has incorrect length.')
}
return ok({
signature,
publicKey,
})
})
.map((result) => [
...previousValue,
{
signature: result.signature,
derivedPublicKey: {
publicKey: result.publicKey,
derivationPath: signer.derivationPath,
curve: signer.curve,
},
},
])
})
},
okAsync([]),
),
),
)
return {
signAuth: (params: LedgerSignChallengeRequest) => {
lastInteractionId = params.interactionId
return signAuth(params)
},
getPublicKeys: (params: LedgerPublicKeyRequest) => {
lastInteractionId = params.interactionId
return getPublicKeys(params)
},
getDeviceInfo: (params: LedgerDeviceIdRequest) => {
lastInteractionId = params.interactionId
return getDeviceInfo()
},
signTransaction: (params: LedgerSignTransactionRequest) => {
lastInteractionId = params.interactionId
return signTransaction(params)
},
signSubintent: (params: LedgerSignSubintentHashRequest) => {
lastInteractionId = params.interactionId
return signSubintent(params)
},
deriveAndDisplayAddress: (params: LedgerDeriveAndDisplayAddressRequest) => {
lastInteractionId = params.interactionId
return deriveAndDisplayAddress(params)
},
getLastInteractionId: () => lastInteractionId,
progress$: ledgerSubjects.onProgressSubject.asObservable(),
connectedDeviceId$: ledgerSubjects.connectedDeviceIdSubject.asObservable(),
}
}
export const ledger = LedgerWrapper({})