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 support for validating Stellar addresses #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -81,6 +81,7 @@ npm install wallet-address-validator
* Ripple/XRP, `'ripple'` or `'XRP'`

* Snowgem/SNG, `'snowgem'` or `'SNG'`
* Stellar/XLM, `'stellar'` or `'XLM'`

* Vertcoin/VTC, `'vertcoin'` or `'VTC'`

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
"dependencies": {
"base-x": "^3.0.4",
"crc": "^3.8.0",
"jssha": "2.3.1"
},
"devDependencies": {
Expand Down
5 changes: 5 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 XLMValidator = require('./stellar_validator');

// defines P2PKH and P2SH address types for standard (prod) and testnet networks
var CURRENCIES = [{
Expand Down Expand Up @@ -209,6 +210,10 @@ var CURRENCIES = [{
name: 'raiblocks',
symbol: 'xrb',
validator: NANOValidator,
}, {
name: 'stellar',
symbol: 'xlm',
validator: XLMValidator,
}];


Expand Down
46 changes: 46 additions & 0 deletions src/stellar_validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var baseX = require('base-x');
var crc = require('crc');
var cryptoUtils = require('./crypto/utils');

var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';

var base32 = baseX(ALPHABET);
var regexp = new RegExp('^[' + ALPHABET + ']{56}$');
var ed25519PublicKeyVersionByte = (6 << 3);

function swap16(number) {
var lower = number & 0xFF;
var upper = (number >> 8) & 0xFF;
return (lower << 8) | upper;
}

function numberToHex(number) {
var hex = number.toString(16);
if(hex.length % 2 === 1) {
hex = '0' + hex;
}
return hex;
}

module.exports = {
isValidAddress: function (address) {
if (regexp.test(address)) {
return this.verifyChecksum(address);
}

return false;
},

verifyChecksum: function (address) {
// based on https://github.com/stellar/js-stellar-base/blob/master/src/strkey.js#L126
var bytes = base32.decode(address);
if (bytes[0] !== ed25519PublicKeyVersionByte) {
return false;
}

var computedChecksum = numberToHex(swap16(crc.crc16xmodem(bytes.slice(0, -2))));
var checksum = cryptoUtils.toHex(bytes.slice(-2));

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

it('should return true for correct stellar addresses', function () {
valid('GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB', 'stellar');
valid('GB7KKHHVYLDIZEKYJPAJUOTBE5E3NJAXPSDZK7O6O44WR3EBRO5HRPVT', 'stellar');
valid('GD6WVYRVID442Y4JVWFWKWCZKB45UGHJAABBJRS22TUSTWGJYXIUR7N2', 'stellar');
valid('GBCG42WTVWPO4Q6OZCYI3D6ZSTFSJIXIS6INCIUF23L6VN3ADE4337AP', 'stellar');
valid('GDFX463YPLCO2EY7NGFMI7SXWWDQAMASGYZXCG2LATOF3PP5NQIUKBPT', 'stellar');
valid('GBXEODUMM3SJ3QSX2VYUWFU3NRP7BQRC2ERWS7E2LZXDJXL2N66ZQ5PT', 'stellar');
valid('GAJHORKJKDDEPYCD6URDFODV7CVLJ5AAOJKR6PG2VQOLWFQOF3X7XLOG', 'stellar');
valid('GACXQEAXYBEZLBMQ2XETOBRO4P66FZAJENDHOQRYPUIXZIIXLKMZEXBJ', 'stellar');
valid('GDD3XRXU3G4DXHVRUDH7LJM4CD4PDZTVP4QHOO4Q6DELKXUATR657OZV', 'stellar');
valid('GDTYVCTAUQVPKEDZIBWEJGKBQHB4UGGXI2SXXUEW7LXMD4B7MK37CWLJ', 'stellar');
});
});

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

it('should return false for incorrect stellar addresses', function () {
commonTests('stellar');
invalid('SBGWKM3CD4IL47QN6X54N6Y33T3JDNVI6AIJ6CD5IM47HG3IG4O36XCU', 'stellar');
invalid('GBPXX0A5N4JYPESHAADMQKBPWZWQDQ64ZV6ZL2S3LAGW4SY7NTCMWIVL', 'stellar');
invalid('GCFZB6L25D26RQFDWSSBDEYQ32JHLRMTT44ZYE3DZQUTYOL7WY43PLBG++', 'stellar');
invalid('GADE5QJ2TY7S5ZB65Q43DFGWYWCPHIYDJ2326KZGAGBN7AE5UY6JVDRRA', 'stellar');
invalid('GB6OWYST45X57HCJY5XWOHDEBULB6XUROWPIKW77L5DSNANBEQGUPADT2', 'stellar');
invalid('GB6OWYST45X57HCJY5XWOHDEBULB6XUROWPIKW77L5DSNANBEQGUPADT2T', 'stellar');
invalid('GDXIIZTKTLVYCBHURXL2UPMTYXOVNI7BRAEFQCP6EZCY4JLKY4VKFNLT', 'stellar');
invalid('SAB5556L5AN5KSR5WF7UOEFDCIODEWEO7H2UR4S5R62DFTQOGLKOVZDY', 'stellar');
invalid('gWRYUerEKuz53tstxEuR3NCkiQDcV4wzFHmvLnZmj7PUqxW2wt', 'stellar');
invalid('g4VPBPrHZkfE8CsjuG2S4yBQNd455UWmk', 'stellar');
});
});
});