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

Commit

Permalink
Add length check to getLisk32AddressFromPublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Phanco committed Oct 26, 2023
1 parent 185e3fa commit 31a1073
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion elements/lisk-cryptography/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,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 !== 32) {
throw new Error('publicKey length must be 32.');
}
return `${prefix}${addressToLisk32(getAddressFromPublicKey(publicKey))}`;
};

export const validateLisk32Address = (
address: string,
Expand Down
6 changes: 6 additions & 0 deletions elements/lisk-cryptography/test/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ describe('address', () => {
});

describe('#getLisk32AddressFromPublicKey', () => {
it('should reject when publicKey length is not 32', () => {
expect(() => getLisk32AddressFromPublicKey(Buffer.alloc(31), 'lsk')).toThrow(
'publicKey length must be 32.',
);
});

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

Expand Down

0 comments on commit 31a1073

Please sign in to comment.