Skip to content

Commit 4812e73

Browse files
authored
improved error management (#24)
1 parent fb65527 commit 4812e73

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

src/app.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ describe('BaseApp', () => {
3030

3131
describe('prepareChunks', () => {
3232
it('should prepare chunks correctly', () => {
33+
// subclassing to expose protected method
34+
class TestBaseApp extends BaseApp {
35+
public prepareChunks(path: string, message: Buffer) {
36+
return super.prepareChunks(path, message)
37+
}
38+
}
39+
3340
const transport = new MockTransport(Buffer.alloc(0))
34-
const app = new BaseApp(transport, params)
41+
const app = new TestBaseApp(transport, params)
3542
const path = "m/44'/0'/0'"
3643
const message = Buffer.from('test message')
3744
const chunks = app.prepareChunks(path, message)

src/app.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export default class BaseApp {
4646
* @param params - The constructor parameters.
4747
*/
4848
constructor(transport: Transport, params: ConstructorParams) {
49+
if (transport == null) {
50+
throw new Error('Transport has not been defined')
51+
}
52+
4953
this.transport = transport
5054
this.CLA = params.cla
5155
this.INS = params.ins
@@ -69,7 +73,7 @@ export default class BaseApp {
6973
* @param message - The message to be sent.
7074
* @returns An array of buffers that are ready to be sent.
7175
*/
72-
prepareChunks(path: string, message: Buffer): Buffer[] {
76+
protected prepareChunks(path: string, message: Buffer): Buffer[] {
7377
const chunks = []
7478
const serializedPathBuffer = this.serializePath(path)
7579

@@ -98,7 +102,7 @@ export default class BaseApp {
98102
* @returns A promise that resolves to the processed response from the device.
99103
* @throws {ResponseError} If the response from the device indicates an error.
100104
*/
101-
async signSendChunk(ins: number, chunkIdx: number, chunkNum: number, chunk: Buffer): Promise<ResponsePayload> {
105+
protected async signSendChunk(ins: number, chunkIdx: number, chunkNum: number, chunk: Buffer): Promise<ResponsePayload> {
102106
let payloadType = PAYLOAD_TYPE.ADD
103107

104108
if (chunkIdx === 1) {

src/bip32.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { ResponseError } from './responseError'
2020
* Serializes a derivation path into a buffer.
2121
* @param path - The derivation path in string format.
2222
* @returns A buffer representing the serialized path.
23-
* @throws {Error} If the path format is incorrect or invalid.
23+
* @throws {ResponseError} If the path format is incorrect or invalid.
2424
*/
2525
export function serializePath(path: string, requiredPathLengths?: number[]): Buffer {
2626
if (typeof path !== 'string') {
@@ -73,12 +73,15 @@ export function serializePath(path: string, requiredPathLengths?: number[]): Buf
7373
*/
7474
export function numbersToBip32Path(items: number[]): string {
7575
if (items.length === 0) {
76-
throw new Error('The items array cannot be empty.')
76+
throw new ResponseError(LedgerError.GenericError, 'The items array cannot be empty.')
7777
}
7878

7979
const pathArray = []
8080
for (let i = 0; i < items.length; i++) {
8181
let value = items[i]
82+
if (!Number.isInteger(value) || value < 0) {
83+
throw new ResponseError(LedgerError.GenericError, 'Each item must be a positive integer.')
84+
}
8285
let child = value & ~HARDENED
8386

8487
if (value >= HARDENED) {
@@ -90,7 +93,6 @@ export function numbersToBip32Path(items: number[]): string {
9093

9194
return 'm/' + pathArray.join('/')
9295
}
93-
9496
/**
9597
* Converts a buffer representing a serialized path back into a derivation path string.
9698
* @param buffer - The buffer representing the serialized path.
@@ -99,7 +101,7 @@ export function numbersToBip32Path(items: number[]): string {
99101
*/
100102
export function bufferToBip32Path(buffer: Buffer): string {
101103
if (buffer.length % 4 !== 0) {
102-
throw new Error('The buffer length must be a multiple of 4.')
104+
throw new ResponseError(LedgerError.GenericError, 'The buffer length must be a multiple of 4.')
103105
}
104106

105107
const items = []

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ export type { default as Transport } from '@ledgerhq/hw-transport'
2222
export * from './common'
2323
export * from './consts'
2424
export * from './types'
25+
export * from './bip32'
26+
export * from './responseError'
27+
export * from './payload'

0 commit comments

Comments
 (0)