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 (#9124)
Browse files Browse the repository at this point in the history
* Add length check to getLisk32AddressFromPublicKey

* Set ED25519_PUBLIC_KEY_LENGTH be a constant in lisk-cryptography

* Update test cases

* Update formats on getLisk32AddressFromPublicKey
  • Loading branch information
Phanco authored Oct 31, 2023
1 parent 185e3fa commit 36a1df9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
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

0 comments on commit 36a1df9

Please sign in to comment.