Skip to content

Commit

Permalink
add Nimiq support
Browse files Browse the repository at this point in the history
  • Loading branch information
Developer committed Aug 1, 2019
1 parent bcd0c8f commit d350b0c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ npm install wallet-address-validator
* NEO/NEO, `'NEO'` or `'NEO'`
* NeoGas/GAS, `'neogas'` or `'GAS'`

* Nimiq/NIM, `'nimiq'` or `'NIM'`

* Peercoin/PPCoin/PPC, `'peercoin'` or `'PPC'`
* Primecoin/XPM, `'primecoin'` or `'XPM'`
* Protoshares/PTS, `'protoshares'` or `'PTS'`
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"validator",
"vertcoin",
"nano",
"nimiq",
"raiblocks",
"javascript",
"browser",
Expand Down
7 changes: 7 additions & 0 deletions src/currencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var ETHValidator = require('./ethereum_validator');
var BTCValidator = require('./bitcoin_validator');
var XMRValidator = require('./monero_validator');
var NANOValidator = require('./nano_validator');
var IBANValidator = require('./iban_validator');

// defines P2PKH and P2SH address types for standard (prod) and testnet networks
var CURRENCIES = [{
Expand Down Expand Up @@ -209,6 +210,12 @@ var CURRENCIES = [{
name: 'raiblocks',
symbol: 'xrb',
validator: NANOValidator,
},{
name: 'nimiq',
symbol: 'nim',
countryCode: 'NQ',
length: 36,
validator: IBANValidator,
}];


Expand Down
32 changes: 32 additions & 0 deletions src/iban_validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function ibanCheck(address) {
const num = address.split('').map(function(c) {
const code = c.toUpperCase().charCodeAt(0);
return code >= 48 && code <= 57 ? c : (code - 55).toString();
}).join('');
let tmp = '';

for (let i = 0; i < Math.ceil(num.length / 6); i++) {
tmp = (parseInt(tmp + num.substr(i * 6, 6)) % 97).toString();
}

return parseInt(tmp);
}

module.exports = {
isValidAddress: function (address, currency) {
currency = currency || {};
address = address.replace(/ /g, '');

if (address.substr(0, 2).toUpperCase() !== currency.countryCode) {
return false;
}
if (address.length !== currency.length) {
return false;
}
if (ibanCheck(address.substr(4) + address.substr(0, 4)) !== 1) {
return false;
}

return true;
}
};
21 changes: 21 additions & 0 deletions test/wallet_address_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,17 @@ describe('WAValidator.validate()', function () {
valid('xrb_1q79ahdr36uqn38p5tp5sqwkn73rnpj1k8obtuetdbjcx37d5gahhd1u9cuh', 'nano');
valid('nano_1q79ahdr36uqn38p5tp5sqwkn73rnpj1k8obtuetdbjcx37d5gahhd1u9cuh', 'nano');
});

it('should return true for correct nimiq addresses', function () {
valid('NQ09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNT', 'nimiq');
valid('nq09 qcg8 hg0t nebp 4djn 81xv cju2 9ly8 bvnt', 'nimiq');
valid('NQ09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNT', 'nimiq');
valid('nq09qcg8hg0tnebp4djn81xvcju29ly8bvnt', 'nimiq');
valid('NQ09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNT', 'NIM');
valid('nq09 qcg8 hg0t nebp 4djn 81xv cju2 9ly8 bvnt', 'NIM');
valid('NQ09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNT', 'NIM');
valid('nq09qcg8hg0tnebp4djn81xvcju29ly8bvnt', 'NIM');
});
});

describe('invalid results', function () {
Expand Down Expand Up @@ -595,5 +606,15 @@ describe('WAValidator.validate()', function () {
invalid('xrb_1111111112111111111111111111111111111111111111111111hifc8npp', 'nano');
invalid('nano_111111111111111111111111111111111111111111111111111hifc8npp', 'nano');
});

it('should return false for incorrect nimiq addresses', function () {
commonTests('nimiq');
invalid('NQ09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNX', 'nimiq');
invalid('NQ09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNX', 'nimiq');
invalid('09 QCG8 HG0T NEBP 4DJN 81XV CJU2 9LY8 BVNT', 'nimiq');
invalid('09 qcg8 hg0t nebp 4djn 81xv cju2 9ly8 bvnt', 'nimiq');
invalid('09QCG8HG0TNEBP4DJN81XVCJU29LY8BVNT', 'nimiq');
invalid('09qcg8hg0tnebp4djn81xvcju29ly8bvnt', 'nimiq');
});
});
});

0 comments on commit d350b0c

Please sign in to comment.