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

Add length check to getLisk32AddressFromPublicKey #9124

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion elements/lisk-cryptography/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
BINARY_ADDRESS_LENGTH,
DEFAULT_LISK32_ADDRESS_PREFIX,
LISK32_ADDRESS_LENGTH,
ED25519_PUBLIC_KEY_LENGTH,
} from './constants';
import { getPublicKey } from './nacl';
import { hash } from './utils';
Expand Down Expand Up @@ -118,7 +119,12 @@ const addressToLisk32 = (address: Buffer): string => {
export const getLisk32AddressFromPublicKey = (
publicKey: Buffer,
prefix = DEFAULT_LISK32_ADDRESS_PREFIX,
): string => `${prefix}${addressToLisk32(getAddressFromPublicKey(publicKey))}`;
): string => {
if (publicKey.length !== ED25519_PUBLIC_KEY_LENGTH) {
throw new Error(`publicKey length must be ${ED25519_PUBLIC_KEY_LENGTH}.`);
}
return `${prefix}${addressToLisk32(getAddressFromPublicKey(publicKey))}`;
};

export const validateLisk32Address = (
address: string,
Expand Down
2 changes: 2 additions & 0 deletions elements/lisk-cryptography/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ export const SHA256 = 'sha256';
export const LISK32_CHARSET = 'zxvcpmbn3465o978uyrtkqew2adsjhfg';
export const LISK32_ADDRESS_LENGTH = 41;
export const MESSAGE_TAG_NON_PROTOCOL_MESSAGE = 'LSK_NPM_';

Phanco marked this conversation as resolved.
Show resolved Hide resolved
export const ED25519_PUBLIC_KEY_LENGTH = 32;
7 changes: 7 additions & 0 deletions elements/lisk-cryptography/test/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
LISK32_CHARSET,
DEFAULT_LISK32_ADDRESS_PREFIX,
LISK32_ADDRESS_LENGTH,
ED25519_PUBLIC_KEY_LENGTH,
} from '../src/constants';
import * as utils from '../src/utils';

Expand Down Expand Up @@ -57,6 +58,12 @@ describe('address', () => {
});

describe('#getLisk32AddressFromPublicKey', () => {
it('should reject when publicKey length not equal to ED25519_PUBLIC_KEY_LENGTH', () => {
expect(() =>
getLisk32AddressFromPublicKey(Buffer.alloc(ED25519_PUBLIC_KEY_LENGTH - 1), 'lsk'),
Phanco marked this conversation as resolved.
Show resolved Hide resolved
).toThrow(`publicKey length must be ${ED25519_PUBLIC_KEY_LENGTH}.`);
});

it('should generate lisk32 address from publicKey', () => {
const address = getLisk32AddressFromPublicKey(defaultPublicKey, 'lsk');

Expand Down