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

Commit

Permalink
🌱 Add validation to cryptography toAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
tosch110 committed Mar 16, 2018
1 parent bef963e commit 2d98226
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/cryptography/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ export const getFirstEightBytesReversed = publicKeyBytes =>
.slice(0, 8)
.reverse();

export const toAddress = buffer => `${bufferToBigNumberString(buffer)}L`;
export const toAddress = buffer => {
if (
!Buffer.from(buffer)
.slice(0, 8)
.equals(buffer)
)
throw new Error('The buffer for Lisk addresses must not have than 8 bytes');
return `${bufferToBigNumberString(buffer)}L`;
};

export const getAddressFromPublicKey = publicKey => {
const publicKeyHash = hash(publicKey, 'hex');
Expand Down
9 changes: 9 additions & 0 deletions test/cryptography/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ describe('convert', () => {
const address = toAddress(bufferInit);
return expect(address).to.be.eql(defaultAddressFromBuffer);
});

it('should throw on more than 8 bytes as input', () => {
const bufferExceedError =
'The buffer for Lisk addresses must not have than 8 bytes';
const bufferInit = Buffer.from(defaultStringWithMoreThanEightCharacters);
return expect(toAddress.bind(null, bufferInit)).to.throw(
bufferExceedError,
);
});
});

describe('#getAddressFromPublicKey', () => {
Expand Down

0 comments on commit 2d98226

Please sign in to comment.