Skip to content

Commit d350b0c

Browse files
author
Developer
committed
add Nimiq support
1 parent bcd0c8f commit d350b0c

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ npm install wallet-address-validator
7171
* NEO/NEO, `'NEO'` or `'NEO'`
7272
* NeoGas/GAS, `'neogas'` or `'GAS'`
7373

74+
* Nimiq/NIM, `'nimiq'` or `'NIM'`
75+
7476
* Peercoin/PPCoin/PPC, `'peercoin'` or `'PPC'`
7577
* Primecoin/XPM, `'primecoin'` or `'XPM'`
7678
* Protoshares/PTS, `'protoshares'` or `'PTS'`

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"validator",
2727
"vertcoin",
2828
"nano",
29+
"nimiq",
2930
"raiblocks",
3031
"javascript",
3132
"browser",

src/currencies.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var ETHValidator = require('./ethereum_validator');
33
var BTCValidator = require('./bitcoin_validator');
44
var XMRValidator = require('./monero_validator');
55
var NANOValidator = require('./nano_validator');
6+
var IBANValidator = require('./iban_validator');
67

78
// defines P2PKH and P2SH address types for standard (prod) and testnet networks
89
var CURRENCIES = [{
@@ -209,6 +210,12 @@ var CURRENCIES = [{
209210
name: 'raiblocks',
210211
symbol: 'xrb',
211212
validator: NANOValidator,
213+
},{
214+
name: 'nimiq',
215+
symbol: 'nim',
216+
countryCode: 'NQ',
217+
length: 36,
218+
validator: IBANValidator,
212219
}];
213220

214221

src/iban_validator.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function ibanCheck(address) {
2+
const num = address.split('').map(function(c) {
3+
const code = c.toUpperCase().charCodeAt(0);
4+
return code >= 48 && code <= 57 ? c : (code - 55).toString();
5+
}).join('');
6+
let tmp = '';
7+
8+
for (let i = 0; i < Math.ceil(num.length / 6); i++) {
9+
tmp = (parseInt(tmp + num.substr(i * 6, 6)) % 97).toString();
10+
}
11+
12+
return parseInt(tmp);
13+
}
14+
15+
module.exports = {
16+
isValidAddress: function (address, currency) {
17+
currency = currency || {};
18+
address = address.replace(/ /g, '');
19+
20+
if (address.substr(0, 2).toUpperCase() !== currency.countryCode) {
21+
return false;
22+
}
23+
if (address.length !== currency.length) {
24+
return false;
25+
}
26+
if (ibanCheck(address.substr(4) + address.substr(0, 4)) !== 1) {
27+
return false;
28+
}
29+
30+
return true;
31+
}
32+
};

test/wallet_address_validator.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,17 @@ describe('WAValidator.validate()', function () {
382382
valid('xrb_1q79ahdr36uqn38p5tp5sqwkn73rnpj1k8obtuetdbjcx37d5gahhd1u9cuh', 'nano');
383383
valid('nano_1q79ahdr36uqn38p5tp5sqwkn73rnpj1k8obtuetdbjcx37d5gahhd1u9cuh', 'nano');
384384
});
385+
386+
it('should return true for correct nimiq addresses', function () {
387+
valid('NQ09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNT', 'nimiq');
388+
valid('nq09 qcg8 hg0t nebp 4djn 81xv cju2 9ly8 bvnt', 'nimiq');
389+
valid('NQ09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNT', 'nimiq');
390+
valid('nq09qcg8hg0tnebp4djn81xvcju29ly8bvnt', 'nimiq');
391+
valid('NQ09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNT', 'NIM');
392+
valid('nq09 qcg8 hg0t nebp 4djn 81xv cju2 9ly8 bvnt', 'NIM');
393+
valid('NQ09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNT', 'NIM');
394+
valid('nq09qcg8hg0tnebp4djn81xvcju29ly8bvnt', 'NIM');
395+
});
385396
});
386397

387398
describe('invalid results', function () {
@@ -595,5 +606,15 @@ describe('WAValidator.validate()', function () {
595606
invalid('xrb_1111111112111111111111111111111111111111111111111111hifc8npp', 'nano');
596607
invalid('nano_111111111111111111111111111111111111111111111111111hifc8npp', 'nano');
597608
});
609+
610+
it('should return false for incorrect nimiq addresses', function () {
611+
commonTests('nimiq');
612+
invalid('NQ09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNX', 'nimiq');
613+
invalid('NQ09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNX', 'nimiq');
614+
invalid('09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNT', 'nimiq');
615+
invalid('09 qcg8 hg0t nebp 4djn 81xv cju2 9ly8 bvnt', 'nimiq');
616+
invalid('09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNT', 'nimiq');
617+
invalid('09qcg8hg0tnebp4djn81xvcju29ly8bvnt', 'nimiq');
618+
});
598619
});
599620
});

0 commit comments

Comments
 (0)