From 31a1073886b86f42af56907ec64ed772ed369eeb Mon Sep 17 00:00:00 2001 From: Franco NG Date: Thu, 26 Oct 2023 10:21:14 +0200 Subject: [PATCH] Add length check to getLisk32AddressFromPublicKey --- elements/lisk-cryptography/src/address.ts | 7 ++++++- elements/lisk-cryptography/test/address.spec.ts | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/elements/lisk-cryptography/src/address.ts b/elements/lisk-cryptography/src/address.ts index b02170dd5c..72e73f7246 100644 --- a/elements/lisk-cryptography/src/address.ts +++ b/elements/lisk-cryptography/src/address.ts @@ -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, diff --git a/elements/lisk-cryptography/test/address.spec.ts b/elements/lisk-cryptography/test/address.spec.ts index 3cf77fe6b3..ef4bc8456d 100644 --- a/elements/lisk-cryptography/test/address.spec.ts +++ b/elements/lisk-cryptography/test/address.spec.ts @@ -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');