Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
add address validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rovak committed Apr 9, 2018
1 parent 3e80420 commit 0b629a2
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 5 deletions.
48 changes: 47 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"license": "GPL-3.0",
"dependencies": {
"axios": "^0.18.0",
"chai": "^4.1.2",
"crypto-js": "^3.1.9-1",
"elliptic": "^6.4.0",
"google-protobuf": "^3.5.0",
Expand Down
7 changes: 3 additions & 4 deletions src/utils/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const byteArray2hexStr = require("./bytes").byteArray2hexStr;
const base64EncodeToString = require("../lib/code").base64EncodeToString;
const {genPriKey, getAddressFromPriKey} = require("./crypto");

/**
* Generate a new account
*/
function generateAccount() {
let priKeyBytes = genPriKey();
let addressBytes = getAddressFromPriKey(priKeyBytes);
Expand All @@ -15,7 +18,3 @@ function generateAccount() {
password,
}
}

module.exports = {
generateAccount,
};
24 changes: 24 additions & 0 deletions src/utils/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const ADDRESS_PREFIX = "a0";
const ADDRESS_SIZE = 42;


function isAddressValid(address) {

if (!address || address.length === 0) {
return false;
}

if (address.length !== ADDRESS_SIZE) {
return false;
}

if (address.substr(0, 2).toUpperCase() !== ADDRESS_PREFIX.toUpperCase()) {
return false;
}

return true;
}

module.exports = {
isAddressValid,
};
34 changes: 34 additions & 0 deletions test/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { assert } = require('chai');
const {isAddressValid} = require("../src/utils/address");

describe('address', () => {

it('accept valid address', async () => {
assert.isTrue(isAddressValid("A00D0CF9099287F92B98BCDF28D40684A1A70C1E83"));
assert.isTrue(isAddressValid("A030975C3437C94118C924241ADF2A937010889B6D"));
assert.isTrue(isAddressValid("A08B5B1D533EFA3D098D34D3CA4FD908130856DA4B"));
});

it('reject addresses of invalid length', async () => {
assert.isFalse(isAddressValid("A00D0CF9099287F92B98BCDF28DA0684A11E83"));
assert.isFalse(isAddressValid("A00D0CF9099287F92B98BCDF28DA01E83"));
assert.isFalse(isAddressValid("A00D0CF9099287F92B98BCDF1E83"));
assert.isFalse(isAddressValid("A00D0CF9099287F92B91E83"));
assert.isFalse(isAddressValid("A00D0CF9099287F1E83"));
assert.isFalse(isAddressValid("A00D0CF90991E83"));
assert.isFalse(isAddressValid("A00D0C1E83"));
assert.isFalse(isAddressValid(""));
});

it('reject invalid addresses', async () => {
assert.isFalse(isAddressValid(""));
assert.isFalse(isAddressValid());
assert.isFalse(isAddressValid(null));
assert.isFalse(isAddressValid(undefined));
assert.isFalse(isAddressValid(false));
assert.isFalse(isAddressValid(true));
assert.isFalse(isAddressValid(1));
assert.isFalse(isAddressValid(0));
});

});

0 comments on commit 0b629a2

Please sign in to comment.