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

Commit

Permalink
♻️ Update address validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsujutsu committed Nov 12, 2018
1 parent cfdbddf commit 8deff7b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
16 changes: 14 additions & 2 deletions packages/lisk-transactions/src/utils/validation/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const validateKeysgroup = (keysgroup: ReadonlyArray<string>) => {

const MIN_ADDRESS_LENGTH = 2;
const MAX_ADDRESS_LENGTH = 22;
const BASE_TEN = 10;
export const validateAddress = (address: string) => {
if (
address.length < MIN_ADDRESS_LENGTH ||
Expand All @@ -83,14 +84,25 @@ export const validateAddress = (address: string) => {
);
}

const addressAsBignum = new BigNum(address.slice(0, -1));
if (address.indexOf('.') > -1) {
throw new Error(
'Address format does not match requirements. Address includes invalid character: `.`.',
);
}

if (addressAsBignum.cmp(new BigNum(MAX_ADDRESS_NUMBER)) > 0) {
const addressString = address.slice(0, -1);
const addressNumber = new BigNum(addressString);

if (addressNumber.cmp(new BigNum(MAX_ADDRESS_NUMBER)) > 0) {
throw new Error(
'Address format does not match requirements. Address out of maximum range.',
);
}

if (addressString !== addressNumber.toString(BASE_TEN)) {
throw new Error('Address number does not have natural representation.');
}

return true;
};

Expand Down
19 changes: 19 additions & 0 deletions packages/lisk-transactions/test/utils/validation/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ describe('validation', () => {
});
});

describe('Given an address that includes `.`', () => {
const address = '14.15133512790761431L';
const error =
'Address format does not match requirements. Address includes invalid character: `.`.';

it('should throw', () => {
return expect(validateAddress.bind(null, address)).to.throw(error);
});
});

describe('Given an address that is out of range', () => {
const address = '18446744073709551616L';
const error =
Expand All @@ -247,6 +257,15 @@ describe('validation', () => {
return expect(validateAddress.bind(null, address)).to.throw(error);
});
});

describe('Given an address that has leading zeros', () => {
const address = '00015133512790761431L';
const error = 'Address number does not have natural representation.';

it('should throw', () => {
return expect(validateAddress.bind(null, address)).to.throw(error);
});
});
});

describe('#validateAmount', () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/lisk-transactions/test/utils/validation/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ describe('validator', () => {
return expect(validate({ target: '14815133512790761431L' })).to.be.true;
});

it('should validate to true when valid address with leading zeros is provided', () => {
return expect(validate({ target: '00015133512790761431L' })).to.be.true;
it('should validate to false when address with leading zeros is provided', () => {
return expect(validate({ target: '00015133512790761431L' })).to.be.false;
});

it('should validate to false when address including `.` is provided', () => {
return expect(validate({ target: '14.15133512790761431L' })).to.be.false;
});

it('should validate to false when number greater than maximum is provided', () => {
Expand Down

0 comments on commit 8deff7b

Please sign in to comment.