Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Offline transaction signing gives a misleading error message (#9093)
Browse files Browse the repository at this point in the history
Fix misleading offline signing error message
  • Loading branch information
bobanm authored Oct 18, 2023
1 parent da03f11 commit 4968383
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
4 changes: 0 additions & 4 deletions commander/src/bootstrapping/commands/transaction/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
getParamsSchema,
} from '../../../utils/transaction';
import { getDefaultPath } from '../../../utils/path';
import { isApplicationRunning } from '../../../utils/application';
import { PromiseResolvedType } from '../../../types';
import { DEFAULT_KEY_DERIVATION_PATH } from '../../../utils/config';
import { deriveKeypair } from '../../../utils/commons';
Expand Down Expand Up @@ -256,9 +255,6 @@ export abstract class SignCommand extends Command {

async finally(error?: Error | string): Promise<void> {
if (error) {
if (this._dataPath && !isApplicationRunning(this._dataPath)) {
throw new Error(`Application at data path ${this._dataPath} is not running.`);
}
this.error(error instanceof Error ? error.message : error);
}
if (this._client) {
Expand Down
8 changes: 4 additions & 4 deletions elements/lisk-cryptography/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const hash = (data: Buffer | string, format?: string): Buffer => {

export const parseKeyDerivationPath = (path: string) => {
if (!path.startsWith('m') || !path.includes('/')) {
throw new Error('Invalid path format');
throw new Error('Invalid key derivation path format');
}

return (
Expand All @@ -58,19 +58,19 @@ export const parseKeyDerivationPath = (path: string) => {
.slice(1)
.map(segment => {
if (!/^[0-9']+$/g.test(segment)) {
throw new Error('Invalid path format');
throw new Error('Invalid key derivation path format');
}

// if segment includes apostrophe add HARDENED_OFFSET
if (segment.includes(`'`)) {
if (parseInt(segment.slice(0, -1), 10) > MAX_UINT32 / 2) {
throw new Error('Invalid path format');
throw new Error('Invalid key derivation path format');
}
return parseInt(segment, 10) + HARDENED_OFFSET;
}

if (parseInt(segment, 10) > MAX_UINT32) {
throw new Error('Invalid path format');
throw new Error('Invalid key derivation path format');
}

return parseInt(segment, 10);
Expand Down
14 changes: 7 additions & 7 deletions elements/lisk-cryptography/test/ed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,19 @@ describe('getPrivateKeyFromPhraseAndPath', () => {

it('should fail for empty string path', async () => {
await expect(getPrivateKeyFromPhraseAndPath(passphrase, '')).rejects.toThrow(
'Invalid path format',
'Invalid key derivation path format',
);
});

it('should fail if path does not start with "m"', async () => {
await expect(getPrivateKeyFromPhraseAndPath(passphrase, `/44'/134'/0'`)).rejects.toThrow(
'Invalid path format',
'Invalid key derivation path format',
);
});

it('should fail if path does not include at least one "/"', async () => {
await expect(getPrivateKeyFromPhraseAndPath(passphrase, 'm441340')).rejects.toThrow(
'Invalid path format',
'Invalid key derivation path format',
);
});

Expand All @@ -190,24 +190,24 @@ describe('getPrivateKeyFromPhraseAndPath', () => {
passphrase,
`m//134'/0'`, // should be number with or without ' between every back slash
),
).rejects.toThrow('Invalid path format');
).rejects.toThrow('Invalid key derivation path format');
});

it('should fail for path with invalid characters', async () => {
await expect(getPrivateKeyFromPhraseAndPath(passphrase, `m/a'/134b'/0'`)).rejects.toThrow(
'Invalid path format',
'Invalid key derivation path format',
);
});

it('should fail for path with non-sanctioned special characters', async () => {
await expect(getPrivateKeyFromPhraseAndPath(passphrase, `m/4a'/#134b'/0'`)).rejects.toThrow(
'Invalid path format',
'Invalid key derivation path format',
);
});

it(`should fail for path with segment greater than ${MAX_UINT32} / 2`, async () => {
await expect(
getPrivateKeyFromPhraseAndPath(passphrase, `m/44'/134'/${MAX_UINT32}'`),
).rejects.toThrow('Invalid path format');
).rejects.toThrow('Invalid key derivation path format');
});
});

0 comments on commit 4968383

Please sign in to comment.