16
16
import type Transport from '@ledgerhq/hw-transport'
17
17
18
18
import { errorCodeToString , processErrorResponse } from './common'
19
- import { LedgerError } from './consts'
19
+ import { HARDENED , LedgerError , PAYLOAD_TYPE } from './consts'
20
20
import {
21
21
type ConstructorParams ,
22
22
type INSGeneric ,
@@ -50,7 +50,9 @@ export default class BaseApp {
50
50
* @throws {Error } If the path format is incorrect or invalid.
51
51
*/
52
52
serializePath ( path : string ) : Buffer {
53
- const HARDENED = 0x80000000
53
+ if ( typeof path !== 'string' ) {
54
+ throw new Error ( "Path should be a string (e.g \"m/44'/461'/5'/0/3\")" )
55
+ }
54
56
55
57
if ( ! path . startsWith ( 'm/' ) ) {
56
58
throw new Error ( 'Path should start with "m/" (e.g "m/44\'/5757\'/5\'/0/3")' )
@@ -67,15 +69,18 @@ export default class BaseApp {
67
69
68
70
pathArray . forEach ( ( child , i ) => {
69
71
let value = 0
72
+
70
73
if ( child . endsWith ( "'" ) ) {
71
74
value += HARDENED
72
75
child = child . slice ( 0 , - 1 )
73
76
}
77
+
74
78
const numChild = Number ( child )
75
79
76
80
if ( Number . isNaN ( numChild ) ) {
77
81
throw new Error ( `Invalid path : ${ child } is not a number. (e.g "m/44'/461'/5'/0/3")` )
78
82
}
83
+
79
84
if ( numChild >= HARDENED ) {
80
85
throw new Error ( 'Incorrect child value (bigger or equal to 0x80000000)' )
81
86
}
@@ -110,6 +115,31 @@ export default class BaseApp {
110
115
return chunks
111
116
}
112
117
118
+ async signSendChunk ( ins : number , chunkIdx : number , chunkNum : number , chunk : Buffer ) {
119
+ let payloadType = PAYLOAD_TYPE . ADD
120
+
121
+ if ( chunkIdx === 1 ) {
122
+ payloadType = PAYLOAD_TYPE . INIT
123
+ }
124
+
125
+ if ( chunkIdx === chunkNum ) {
126
+ payloadType = PAYLOAD_TYPE . LAST
127
+ }
128
+
129
+ return this . transport . send ( this . CLA , ins , payloadType , 0 , chunk , [ 0x9000 , 0x6984 , 0x6a80 ] ) . then ( response => {
130
+ const errorCodeData = response . subarray ( - 2 )
131
+ const returnCode = errorCodeData [ 0 ] * 256 + errorCodeData [ 1 ]
132
+
133
+ let errorMessage = errorCodeToString ( returnCode )
134
+
135
+ if ( returnCode === LedgerError . BadKeyHandle || returnCode === LedgerError . DataIsInvalid ) {
136
+ errorMessage = `${ errorMessage } : ${ response . subarray ( 0 , response . length - 2 ) . toString ( 'ascii' ) } `
137
+ }
138
+
139
+ return response
140
+ } , processErrorResponse )
141
+ }
142
+
113
143
/**
114
144
* Retrieves the version information from the device.
115
145
* @returns A promise that resolves to the version information.
0 commit comments