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 all 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
1 change: 1 addition & 0 deletions elements/lisk-cryptography/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export const SHA256 = 'sha256';
export const LISK32_CHARSET = 'zxvcpmbn3465o978uyrtkqew2adsjhfg';
export const LISK32_ADDRESS_LENGTH = 41;
export const MESSAGE_TAG_NON_PROTOCOL_MESSAGE = 'LSK_NPM_';
export const ED25519_PUBLIC_KEY_LENGTH = 32;
15 changes: 14 additions & 1 deletion 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,8 +58,20 @@ 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),
DEFAULT_LISK32_ADDRESS_PREFIX,
),
).toThrow(`publicKey length must be ${ED25519_PUBLIC_KEY_LENGTH}.`);
});

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

expect(address).toBe(getLisk32AddressFromAddress(defaultAddress));
});
Expand Down