From 36a1df9ffe467f11006edf25eeca539bc45701ef Mon Sep 17 00:00:00 2001 From: Franco NG Date: Tue, 31 Oct 2023 11:04:08 +0100 Subject: [PATCH] Add length check to getLisk32AddressFromPublicKey (#9124) * Add length check to getLisk32AddressFromPublicKey * Set ED25519_PUBLIC_KEY_LENGTH be a constant in lisk-cryptography * Update test cases * Update formats on getLisk32AddressFromPublicKey --- elements/lisk-cryptography/src/address.ts | 8 +++++++- elements/lisk-cryptography/src/constants.ts | 1 + elements/lisk-cryptography/test/address.spec.ts | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/elements/lisk-cryptography/src/address.ts b/elements/lisk-cryptography/src/address.ts index b02170dd5c0..3abaa752138 100644 --- a/elements/lisk-cryptography/src/address.ts +++ b/elements/lisk-cryptography/src/address.ts @@ -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'; @@ -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, diff --git a/elements/lisk-cryptography/src/constants.ts b/elements/lisk-cryptography/src/constants.ts index bc57b3538d7..790b9e391c0 100644 --- a/elements/lisk-cryptography/src/constants.ts +++ b/elements/lisk-cryptography/src/constants.ts @@ -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; diff --git a/elements/lisk-cryptography/test/address.spec.ts b/elements/lisk-cryptography/test/address.spec.ts index 3cf77fe6b31..22acad9534b 100644 --- a/elements/lisk-cryptography/test/address.spec.ts +++ b/elements/lisk-cryptography/test/address.spec.ts @@ -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'; @@ -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)); });