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

Fix AVAX, include AVAXC #71

Open
wants to merge 3 commits 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
bower_components
.idea
.DS_Store
*.swp
12 changes: 12 additions & 0 deletions src/avax_validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var bip173 = require('./bip173_validator.js')

// Checks validity in Avalanche X- & P- chains
module.exports = {
isValidAddress: function (address, currency, opts = {}) {
// AVAX addresses can have an ID at the beginning
let [id, addr] = address.split('-')
if (!addr) addr = id

return bip173.isValidAddress(addr, currency, opts)
}
}
11 changes: 11 additions & 0 deletions src/avaxc_validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var ethereum = require('./ethereum_validator')

// Validate C-Chain addresses (AVAXC)
module.exports = {
isValidAddress: function (address) {
// While rarely used, C-Chain addresses can include a chain ID
let [id, addr] = address.split('-')
if (!addr) addr = id
return ethereum.isValidAddress(addr)
}
}
10 changes: 9 additions & 1 deletion src/currencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var AlgoValidator = require('./algo_validator');
var DotValidator = require('./dot_validator');
var BIP173Validator = require('./bip173_validator')
var Base58Validator = require('./base58_validator')
var AVAXValidator = require('./avax_validator')
var AVAXCValidator = require('./avaxc_validator')

// defines P2PKH and P2SH address types for standard (prod) and testnet networks
var CURRENCIES = [{
Expand Down Expand Up @@ -597,7 +599,13 @@ var CURRENCIES = [{
{
name: 'Avalanche',
symbol: 'avax',
validator: ETHValidator,
bech32Hrp: { prod: ['avax'], testnet: ['cascade', 'denali', 'everest', 'fuji', 'local', 'custom'] },
validator: AVAXValidator
},
{
name: 'Avalanche C-Chain',
symbol: 'avaxc',
validator: AVAXCValidator
},
];

Expand Down
18 changes: 18 additions & 0 deletions test/wallet_address_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,24 @@ describe('WAValidator.validate()', function () {
valid('G4qGCGF4vWGPzYi2pxc2Djvgv3j8NiWaHQMgTVebCX6W', 'sol');
});

it('should return true for correct X- & P- Avalanche chain addreses', function () {
valid('P-avax1ks5kfds2mk8hxwdfdg6dya2v3pggdwf6enj9lt', 'avax');
valid('X-avax1ks5kfds2mk8hxwdfdg6dya2v3pggdwf6enj9lt', 'avax');
valid('avax1ks5kfds2mk8hxwdfdg6dya2v3pggdwf6enj9lt', 'avax');
valid('P-avax1ks5kfds2mk8hxwdfdg6dya2v3pggdwf6enj9lt', 'avalanche');
valid('X-avax1ks5kfds2mk8hxwdfdg6dya2v3pggdwf6enj9lt', 'avalanche');
valid('avax1ks5kfds2mk8hxwdfdg6dya2v3pggdwf6enj9lt', 'avalanche');

valid('X-fuji1xpmx0ljrpvqexrvrj26fnggvr0ax9wm32gaxmx', 'avalanche', 'testnet');
valid('fuji1xpmx0ljrpvqexrvrj26fnggvr0ax9wm32gaxmx', 'avalanche', 'testnet');
});

it('should return true for correct Avalanche C-chain address (AVAXC)', function () {
valid('C-0x572f4D80f10f663B5049F789546f25f70Bb62a7F', 'avaxc');
valid('0x572f4D80f10f663B5049F789546f25f70Bb62a7F', 'avaxc');
valid('C-0x572f4D80f10f663B5049F789546f25f70Bb62a7F', 'avalanche c-chain');
valid('0x572f4D80f10f663B5049F789546f25f70Bb62a7F', 'avalanche c-chain');
});
});

describe('invalid results', function () {
Expand Down