Skip to content

Commit ce12481

Browse files
committed
Adding alternative way to specify currency: by symbol. Changing API, by moving testnet as separate param.
1 parent 0a2adc1 commit ce12481

8 files changed

+173
-108
lines changed

Gruntfile.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = function(grunt) {
1111
'bower_components/jssha/src/sha256.js',
1212
'src/base58.js',
1313
'src/crypto_utils.js',
14+
'src/currencies.js',
1415
'src/wallet_address_validator.js'
1516
]
1617
}

README.md

+26-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Simple wallet address validator for validating Bitcoin and other altcoins addres
33

44
Forked from [ryanralph/altcoin-address](https://github.com/ryanralph/altcoin-address).
55

6-
I forked it to remove all Node.js dependencies (crypro, Buffer etc.) to make it usable in the browser as well. I didn't use browserify to achieve smaller footprint, **file size is 3.9 kB (minifed and gzipped)**.
6+
I forked it to remove all Node.js dependencies (crypro, Buffer etc.) to make it usable in the browser as well. I didn't use browserify to achieve smaller footprint, **file size is 4.0 kB (minifed and gzipped)**.
77

88
## Installation
99

@@ -25,37 +25,43 @@ bower install wallet-address-validator
2525

2626
## API
2727

28-
### validate (address [, currency])
28+
##### validate (address [, currency = 'bitcoin'[, networkType = 'prod']])
2929

30-
> returns true if the address (string) is a valid wallet address for the crypto currency specified, see below for supported currencies.
31-
>
32-
> if no options are specified it defaults to bitcoin
30+
###### Parameters
31+
* address - Wallet address to validate.
32+
* currency - Optional. Currency name or symbol, e.g. `'bitcoin'` (default), `'litecoin'` or `'LTC'`
33+
* networkType - Optional. Use `'prod'` (default) to enforce standard address, `'testnet'` to enforce testnet address and `'both'` to enforce nothing.
3334

34-
### getAddressType (address)
35+
> Returns true if the address (string) is a valid wallet address for the crypto currency specified, see below for supported currencies.
3536
36-
> returns address type (as 2 character hex string) if valid base58 address, otherwise null
37+
##### getAddressType (address)
38+
39+
###### Parameters
40+
* address - Wallet address.
41+
42+
> Returns address type (as 2 character hex string) if valid base58 address, otherwise null.
3743
3844
### Supported crypto currencies
3945

40-
* Bitcoin/BTC, `'bitcoin'`
41-
* Litecoin/LTC, `'litecoin'`
42-
* Peercoin/PPCoin/PPC, `'peercoin'`
43-
* Dogecoin/DOGE, `'dogecoin'`
44-
* BeaverCoin/BVC, `'beavercoin'`
45-
* Freicoin/FRC, `'freicoin'`
46-
* Protoshares/PTS, `'protoshares'`
47-
* Megacoin/MEC, `'megacoin'`
48-
* Primecoin/XPM, `'primecoin'`
49-
* Auroracoin/AUR, `'auroracoin'`
50-
* Namecoin/NMC, `'namecoin'`
46+
* Bitcoin/BTC, `'bitcoin'` or `'BTC'`
47+
* Litecoin/LTC, `'litecoin'` or `'LTC'`
48+
* Peercoin/PPCoin/PPC, `'peercoin'` or `'PPC'`
49+
* Dogecoin/DOGE, `'dogecoin'` or `'DOGE'`
50+
* BeaverCoin/BVC, `'beavercoin'` or `'BVC'`
51+
* Freicoin/FRC, `'freicoin'` or `'FRC'`
52+
* Protoshares/PTS, `'protoshares'` or `'PTS'`
53+
* Megacoin/MEC, `'megacoin'` or `'MEC'`
54+
* Primecoin/XPM, `'primecoin'` or `'XPM'`
55+
* Auroracoin/AUR, `'auroracoin'` or `'AUR'`
56+
* Namecoin/NMC, `'namecoin'` or `'NMC'`
5157

5258
### Usage example
5359

5460
#### Node
5561
```javascript
5662
var WAValidator = require('wallet-address-validator');
5763

58-
var valid = WAValidator.validate('1KFzzGtDdnq5hrwxXGjwVnKzRbvf8WVxck', 'bitcoin');
64+
var valid = WAValidator.validate('1KFzzGtDdnq5hrwxXGjwVnKzRbvf8WVxck', 'BTC');
5965
if(valid)
6066
console.log('This is a valid address');
6167
else
@@ -67,7 +73,7 @@ else
6773
```javascript
6874
var WAValidator = require('wallet-address-validator');
6975

70-
var valid = WAValidator.validate('1KFzzGtDdnq5hrwxXGjwVnKzRbvf8WVxck', 'litecoinTestnet');
76+
var valid = WAValidator.validate('1KFzzGtDdnq5hrwxXGjwVnKzRbvf8WVxck', 'litecoin', 'testnet');
7177
if(valid)
7278
console.log('This is a valid address');
7379
else

bower.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
{
22
"name": "wallet-address-validator",
33
"description": "Wallet address validator for Bitcoin and other Altcoins.",
4-
"version": "0.0.1",
4+
"version": "0.1.0",
55
"keywords": [
66
"bitcoin",
77
"litecoin",
88
"dogecoin",
99
"altcoin",
1010
"address",
1111
"wallet",
12-
"validator"
12+
"validator",
13+
"javascript",
14+
"browser",
15+
"nodejs"
1316
],
1417
"license": "MIT",
1518
"homepage": "https://github.com/ognus/wallet-address-validator",

dist/wallet-address-validator.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
"altcoin",
99
"address",
1010
"wallet",
11-
"validator"
11+
"validator",
12+
"javascript",
13+
"browser",
14+
"nodejs"
1215
],
13-
"version": "0.0.1",
16+
"version": "0.1.0",
1417
"author": "Tomek Kolasa <[email protected]>",
1518
"homepage": "https://github.com/ognus/wallet-address-validator",
1619
"license": "MIT",

src/currencies.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
(function (isNode) {
2+
// defines P2PKH and P2SH address types for standard (prod) and testnet networks
3+
var CURRENCIES = [{
4+
name: 'bitcoin',
5+
symbol: 'btc',
6+
addressTypes: {prod: ['00', '05'], testnet: ['6f', 'c4']}
7+
},{
8+
name: 'litecoin',
9+
symbol: 'ltc',
10+
addressTypes: {prod: ['30', '05'], testnet: ['6f', 'c4']}
11+
},{
12+
name: 'peercoin',
13+
symbol: 'ppc',
14+
addressTypes: {prod: ['37', '75'], testnet: ['6f', 'c4']}
15+
},{
16+
name: 'dogecoin',
17+
symbol: 'doge',
18+
addressTypes: {prod: ['1e', '16'], testnet: ['71', 'c4']}
19+
},{
20+
name: 'beavercoin',
21+
symbol: 'bvc',
22+
addressTypes: {prod: ['19', '05'], testnet: ['6f', 'c4']}
23+
},{
24+
name: 'freicoin',
25+
symbol: 'frc',
26+
addressTypes: {prod: ['00', '05'], testnet: ['6f', 'c4']}
27+
},{
28+
name: 'protoshares',
29+
symbol: 'pts',
30+
addressTypes: {prod: ['38', '05'], testnet: ['6f', 'c4']}
31+
},{
32+
name: 'megacoin',
33+
symbol: 'mec',
34+
addressTypes: {prod: ['32', '05'], testnet: ['6f', 'c4']}
35+
},{
36+
name: 'primecoin',
37+
symbol: 'xpm',
38+
addressTypes: {prod: ['17', '53'], testnet: ['6f', 'c4']}
39+
},{
40+
name: 'auroracoin',
41+
symbol: 'aur',
42+
addressTypes: {prod: ['17', '05'], testnet: ['6f', 'c4']}
43+
},{
44+
name: 'namecoin',
45+
symbol: 'nmc',
46+
addressTypes: {prod: ['34'], testnet: []}
47+
}];
48+
49+
50+
var currencies = {
51+
getByNameOrSymbol: function (currencyNameOrSymbol) {
52+
var nameOrSymbol = currencyNameOrSymbol.toLowerCase();
53+
for (var i = 0; i < CURRENCIES.length; i++) {
54+
var currency = CURRENCIES[i];
55+
if(currency.name === nameOrSymbol || currency.symbol === nameOrSymbol) {
56+
return currency;
57+
}
58+
}
59+
return null;
60+
}
61+
};
62+
63+
// export currencies module
64+
if(isNode) {
65+
module.exports = currencies;
66+
} else {
67+
if(typeof window.WAValidator === 'undefined'){
68+
window.WAValidator = {__imports: {}};
69+
}
70+
window.WAValidator.__imports.currencies = currencies;
71+
}
72+
})(typeof module !== 'undefined' && typeof module.exports !== 'undefined');

src/wallet_address_validator.js

+20-56
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,19 @@
11
(function (isNode) {
2-
var base58, cryptoUtils;
2+
var base58, cryptoUtils, currencies;
33

44
if(isNode) {
55
base58 = require('./base58');
66
cryptoUtils = require('./crypto_utils');
7+
currencies = require('./currencies');
78
} else {
89
var imports = window.WAValidator.__imports;
910
base58 = imports.base58;
1011
cryptoUtils = imports.cryptoUtils;
12+
currencies = imports.currencies;
1113
}
1214

13-
var DEFAULT_CURRENCY = 'bitcoin',
14-
ADDRESS_TYPE = {
15-
P2PKH: {
16-
bitcoin: '00', // 0 Decimal 1 prefix
17-
bitcoinTestnet: '6f', //111 Decimal mn prefix
18-
litecoin: '30', // 48 Decimal L prefix
19-
litecoinTestnet: '6f', //111 Decimal mn prefix
20-
peercoin: '37', // 55 Decimal P prefix
21-
peercoinTestnet: '6f', //111 Decimal mn prefix
22-
dogecoin: '1e', // 30 Decimal D prefix
23-
dogecoinTestnet: '71', //113 Decimal n prefix
24-
beavercoin: '19', // 25 Decimal B prefix
25-
beavercoinTestnet: '6f', //111 Decimal mn prefix
26-
freicoin: '00', // 0 Decimal 1 prefix
27-
freicoinTestnet: '6f', //111 Decimal mn prefix
28-
protoshares: '38', // 56 Decimal P prefix
29-
protosharesTestnet: '6f', //111 Decimal mn prefix
30-
megacoin: '32', // 50 Decimal M prefix
31-
megacoinTestnet: '6f', //111 Decimal mn prefix
32-
primecoin: '17', // 23 Decimal A prefix
33-
primecoinTestnet: '6f', //111 Decimal mn prefix
34-
auroracoin: '17', // 23 Decimal A prefix
35-
auroracoinTestnet: '6f', //111 Decimal mn prefix
36-
namecoin: '34'
37-
},
38-
P2SH: {
39-
bitcoin: '05', // 5 Decimal 3 prefix
40-
bitcoinTestnet: 'c4', //196 Decimal 2 prefix
41-
litecoin: '05', // 5 Decimal 3 prefix
42-
litecoinTestnet: 'c4', //196 Decimal 2 prefix
43-
peercoin: '75', //117 Decimal p prefix
44-
peercoinTestnet: 'c4', //196 Decimal 2 prefix
45-
dogecoin: '16', //22 Decimal 9A prefix
46-
dogecoinTestnet: 'c4', //196 Decimal 2 prefix
47-
beavercoin: '05', // 5 Decimal 3 prefix
48-
beavercoinTestnet: 'c4', //196 Decimal 2 prefix
49-
freicoin: '05', // 5 Decimal 3 prefix
50-
freicoinTestnet: 'c4', //196 Decimal 2 prefix
51-
protoshares: '05', // 5 Decimal 3 prefix
52-
protosharesTestnet: 'c4',//196 Decimal 2 prefix
53-
megacoin: '05', // 5 Decimal 3 prefix
54-
megacoinTestnet: 'c4', //196 Decimal 2 prefix
55-
primecoin: '53', // 83 Decimal a prefix
56-
primecoinTestnet: 'c4', //196 Decimal 2 prefix
57-
auroracoin: '05', // 83 Decimal a prefix
58-
auroracoinTestnet: 'c4' //196 Decimal 2 prefix
59-
}
60-
};
15+
var DEFAULT_CURRENCY_NAME = 'bitcoin',
16+
DEFAULT_NETWORK_TYPE = 'prod';
6117

6218
var WAValidator = {
6319
getAddressType: function (address) {
@@ -83,13 +39,21 @@
8339
return checksum === goodChecksum ? cryptoUtils.toHex(decoded.slice(0, 1)) : null;
8440
},
8541

86-
validate: function (address, currency) {
87-
currency = currency || DEFAULT_CURRENCY;
88-
var addressType = this.getAddressType(address);
89-
var correctP2PKH = addressType === ADDRESS_TYPE.P2PKH[currency];
90-
var correctP2SH = addressType === ADDRESS_TYPE.P2SH[currency];
91-
return correctP2PKH || correctP2SH
92-
42+
validate: function (address, currencyNameOrSymbol, networkType) {
43+
currencyNameOrSymbol = currencyNameOrSymbol || DEFAULT_CURRENCY_NAME;
44+
networkType = networkType || DEFAULT_NETWORK_TYPE;
45+
46+
var correctAddressTypes,
47+
currency = currencies.getByNameOrSymbol(currencyNameOrSymbol),
48+
addressType = this.getAddressType(address);
49+
50+
if(networkType === 'prod' || networkType === 'testnet'){
51+
correctAddressTypes = currency.addressTypes[networkType]
52+
} else {
53+
correctAddressTypes = currency.addressTypes.prod.concat(currency.addressTypes.testnet);
54+
}
55+
56+
return correctAddressTypes.indexOf(addressType) >= 0;
9357
}
9458
};
9559

0 commit comments

Comments
 (0)