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');