Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #260 from mstuehlinger/ether_address_checksum
Browse files Browse the repository at this point in the history
Verify checksum of Ethereum payout addresses
  • Loading branch information
braydonf authored Sep 27, 2017
2 parents 0c7b150 + 54e27ae commit 00e4435
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const du = require('du'); // Get amount used
const disk = require('diskusage'); // Get amount free
const assert = require('assert');
const bytes = require('bytes');
const web3utils = require('web3-utils');

/**
* Validate the given payout address
Expand Down Expand Up @@ -40,7 +41,7 @@ exports.isValidEthereumAddress = function(address) {
} else if (disallowAddresses.indexOf(address.toLowerCase()) >= 0) {
return false;
}
return /^0x([0-9A-Fa-f]{2}){20}$/.test(address);
return /^0x/.test(address) && web3utils.isAddress(address);
};

/**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
"storj-lib": "^8.0.0",
"strip-json-comments": "^2.0.1",
"tail": "^1.2.1",
"touch": "3.1.0"
"touch": "3.1.0",
"web3-utils": "^1.0.0-beta"
},
"devDependencies": {
"chai": "^2.2.0",
Expand Down
26 changes: 25 additions & 1 deletion test/utils.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,36 @@ describe('module:utils', function() {

describe('#isValidEthereumAddress', function() {

it('should return true for valid mainnet', function() {
it('should return true for checksumed address', function() {
expect(utils.isValidEthereumAddress(
'0xC2D7CF95645D33006175B78989035C7c9061d3F9'
)).to.equal(true);
});

it('should return true for normalized address', function() {
expect(utils.isValidEthereumAddress(
'0xc2d7cf95645d33006175b78989035c7c9061d3f9'
)).to.equal(true);
});

it('should return true for uppercase address', function() {
expect(utils.isValidEthereumAddress(
'0xC2D7CF95645D33006175B78989035C7C9061D3F9'
)).to.equal(true);
});

it('should return false for invalid checksum', function() {
expect(utils.isValidEthereumAddress(
'0xC2D7Cf95645D33006175B78989035C7c9061d3F9'
)).to.equal(false);
});

it('should return false for public key hash digest', function() {
expect(utils.isValidEthereumAddress(
'C2D7CF95645D33006175B78989035C7c9061d3F9'
)).to.equal(false);
});

it('should return false for invalid address', function() {
expect(utils.isValidEthereumAddress(
'1234 Fake Street'
Expand Down

0 comments on commit 00e4435

Please sign in to comment.