Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isEthereumAddress #1117

Merged
merged 3 commits into from
Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Validator | Description
**isByteLength(str [, options])** | check if the string's length (in UTF-8 bytes) falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`.
**isCreditCard(str)** | check if the string is a credit card.
**isCurrency(str [, options])** | check if the string is a valid currency amount.<br/><br/>`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.<br/>**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3].
**isEthereumAddress(str)** | check if the string is an [Ethereum](https://ethereum.org/) address using basic regex. Does not validate address checksums.
**isBtcAddress(str)** | check if the string is a valid BTC address.
**isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs).
**isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.<br/><br/>`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`<br/><br/>`locale` determine the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'hu-HU', 'it-IT', 'ku-IQ', nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`.<br/>**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'.
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ var _isISSN = _interopRequireDefault(require("./lib/isISSN"));

var _isMobilePhone = _interopRequireWildcard(require("./lib/isMobilePhone"));

var _isEthereumAddress = _interopRequireDefault(require("./lib/isEthereumAddress"));

var _isCurrency = _interopRequireDefault(require("./lib/isCurrency"));

var _isBtcAddress = _interopRequireDefault(require("./lib/isBtcAddress"));
Expand Down Expand Up @@ -241,6 +243,7 @@ var validator = {
isMobilePhoneLocales: _isMobilePhone.locales,
isPostalCode: _isPostalCode.default,
isPostalCodeLocales: _isPostalCode.locales,
isEthereumAddress: _isEthereumAddress.default,
isCurrency: _isCurrency.default,
isBtcAddress: _isBtcAddress.default,
isISO8601: _isISO.default,
Expand Down
20 changes: 20 additions & 0 deletions lib/isEthereumAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isEthereumAddress;

var _assertString = _interopRequireDefault(require("./util/assertString"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var eth = /^(0x)[0-9a-f]{40}$/i;

function isEthereumAddress(str) {
(0, _assertString.default)(str);
return eth.test(str);
}

module.exports = exports.default;
module.exports.default = exports.default;
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ import isISSN from './lib/isISSN';

import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone';

import isEthereumAddress from './lib/isEthereumAddress';

import isCurrency from './lib/isCurrency';

import isBtcAddress from './lib/isBtcAddress';
Expand Down Expand Up @@ -175,6 +177,7 @@ const validator = {
isMobilePhoneLocales,
isPostalCode,
isPostalCodeLocales,
isEthereumAddress,
isCurrency,
isBtcAddress,
isISO8601,
Expand Down
8 changes: 8 additions & 0 deletions src/lib/isEthereumAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import assertString from './util/assertString';

const eth = /^(0x)[0-9a-f]{40}$/i;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just asking, don't we have any capitalize/uppercase for Ethereum Address?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ethereum addresses can be either all-caps or all-lower, which is why I added the /i flag to mark the regex as case insensitive. Addresses with mixed casing (e.g. 0xdAC17F958D2ee523a2206206994597C13D831ec7) contain checksum information to make sure it's correct, but they require additional dependencies to compute and check.


export default function isEthereumAddress(str) {
assertString(str);
return eth.test(str);
}
21 changes: 21 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6657,6 +6657,27 @@ describe('Validators', () => {
});
});

it('should validate Ethereum addresses', () => {
test({
validator: 'isEthereumAddress',
valid: [
'0x0000000000000000000000000000000000000001',
'0x683E07492fBDfDA84457C16546ac3f433BFaa128',
'0x88dA6B6a8D3590e88E0FcadD5CEC56A7C9478319',
'0x8a718a84ee7B1621E63E680371e0C03C417cCaF6',
'0xFCb5AFB808b5679b4911230Aa41FfCD0cd335b42',
],
invalid: [
'0xGHIJK05pwm37asdf5555QWERZCXV2345AoEuIdHt',
'0xFCb5AFB808b5679b4911230Aa41FfCD0cd335b422222',
'0xFCb5AFB808b5679b4911230Aa41FfCD0cd33',
'0b0110100001100101011011000110110001101111',
'683E07492fBDfDA84457C16546ac3f433BFaa128',
'1C6o5CDkLxjsVpnLSuqRs1UBFozXLEwYvU',
],
});
});

it('should validate Bitcoin addresses', () => {
test({
validator: 'isBtcAddress',
Expand Down
7 changes: 7 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,12 @@ function isMobilePhone(str, locale, options) {
}
var locales$3 = Object.keys(phones);

var eth = /^(0x)[0-9a-f]{40}$/i;
function isEthereumAddress(str) {
assertString(str);
return eth.test(str);
}

function currencyRegex(options) {
var decimal_digits = "\\d{".concat(options.digits_after_decimal[0], "}");
options.digits_after_decimal.forEach(function (digit, index) {
Expand Down Expand Up @@ -2430,6 +2436,7 @@ var validator = {
isMobilePhoneLocales: locales$3,
isPostalCode: isPostalCode,
isPostalCodeLocales: locales$4,
isEthereumAddress: isEthereumAddress,
isCurrency: isCurrency,
isBtcAddress: isBtcAddress,
isISO8601: isISO8601,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.