Skip to content

Commit

Permalink
improved error management (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
jleni authored May 16, 2024
1 parent fb65527 commit 4812e73
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ describe('BaseApp', () => {

describe('prepareChunks', () => {
it('should prepare chunks correctly', () => {
// subclassing to expose protected method
class TestBaseApp extends BaseApp {
public prepareChunks(path: string, message: Buffer) {
return super.prepareChunks(path, message)
}
}

const transport = new MockTransport(Buffer.alloc(0))
const app = new BaseApp(transport, params)
const app = new TestBaseApp(transport, params)
const path = "m/44'/0'/0'"
const message = Buffer.from('test message')
const chunks = app.prepareChunks(path, message)
Expand Down
8 changes: 6 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export default class BaseApp {
* @param params - The constructor parameters.
*/
constructor(transport: Transport, params: ConstructorParams) {
if (transport == null) {
throw new Error('Transport has not been defined')
}

this.transport = transport
this.CLA = params.cla
this.INS = params.ins
Expand All @@ -69,7 +73,7 @@ export default class BaseApp {
* @param message - The message to be sent.
* @returns An array of buffers that are ready to be sent.
*/
prepareChunks(path: string, message: Buffer): Buffer[] {
protected prepareChunks(path: string, message: Buffer): Buffer[] {
const chunks = []
const serializedPathBuffer = this.serializePath(path)

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

if (chunkIdx === 1) {
Expand Down
10 changes: 6 additions & 4 deletions src/bip32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ResponseError } from './responseError'
* Serializes a derivation path into a buffer.
* @param path - The derivation path in string format.
* @returns A buffer representing the serialized path.
* @throws {Error} If the path format is incorrect or invalid.
* @throws {ResponseError} If the path format is incorrect or invalid.
*/
export function serializePath(path: string, requiredPathLengths?: number[]): Buffer {
if (typeof path !== 'string') {
Expand Down Expand Up @@ -73,12 +73,15 @@ export function serializePath(path: string, requiredPathLengths?: number[]): Buf
*/
export function numbersToBip32Path(items: number[]): string {
if (items.length === 0) {
throw new Error('The items array cannot be empty.')
throw new ResponseError(LedgerError.GenericError, 'The items array cannot be empty.')
}

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

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

return 'm/' + pathArray.join('/')
}

/**
* Converts a buffer representing a serialized path back into a derivation path string.
* @param buffer - The buffer representing the serialized path.
Expand All @@ -99,7 +101,7 @@ export function numbersToBip32Path(items: number[]): string {
*/
export function bufferToBip32Path(buffer: Buffer): string {
if (buffer.length % 4 !== 0) {
throw new Error('The buffer length must be a multiple of 4.')
throw new ResponseError(LedgerError.GenericError, 'The buffer length must be a multiple of 4.')
}

const items = []
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ export type { default as Transport } from '@ledgerhq/hw-transport'
export * from './common'
export * from './consts'
export * from './types'
export * from './bip32'
export * from './responseError'
export * from './payload'

0 comments on commit 4812e73

Please sign in to comment.