Skip to content

Commit

Permalink
added cardano shelly support
Browse files Browse the repository at this point in the history
  • Loading branch information
christsim committed Oct 6, 2020
1 parent fecf7c1 commit 1af6f6b
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 273 deletions.
128 changes: 74 additions & 54 deletions dist/wallet-address-validator.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/wallet-address-validator.min.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
"ZenCash",
"zen"
],
"version": "0.4.7",
"version": "0.4.8",
"author": "Chris <[email protected]>",
"homepage": "https://github.com/christsim/multicoin-address-validator",
"license": "MIT",
Expand All @@ -243,18 +243,18 @@
"bundle": "^2.1.0",
"cbor-js": "^0.1.0",
"crc": "^3.8.0",
"jssha": "^2.4.2",
"jssha": "^3.1.2",
"lodash.isequal": "^4.5.0",
"minify": "^5.1.1"
"minify": "^6.0.0"
},
"devDependencies": {
"browserify": "^16.5.1",
"browserify": "^16.5.2",
"chai": "^4.2.0",
"karma": "^5.1.0",
"karma": "^5.2.3",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^3.1.0",
"karma-mocha": "^2.0.1",
"mocha": "^8.0.1",
"mocha": "^8.1.3",
"uglify-es": "^3.3.9"
},
"standard": {
Expand Down
56 changes: 41 additions & 15 deletions src/ada_validator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var cbor = require('cbor-js');
var CRC = require('crc');
var base58 = require('./crypto/base58');

var bech32 = require('./crypto/bech32');

function getDecoded(address) {
try {
Expand All @@ -13,23 +13,49 @@ function getDecoded(address) {
}
}

module.exports = {
isValidAddress: function (address) {
var decoded = getDecoded(address);
function isValidAddressV1(address) {
var decoded = getDecoded(address);

if (!decoded || (!Array.isArray(decoded) && decoded.length != 2)) {
return false;
}

if (!decoded || (!Array.isArray(decoded) && decoded.length != 2)) {
return false;
}
var tagged = decoded[0];
var validCrc = decoded[1];
if (typeof (validCrc) != 'number') {
return false;
}

// get crc of the payload
var crc = CRC.crc32(tagged);

var tagged = decoded[0];
var validCrc = decoded[1];
if (typeof (validCrc) != 'number') {
return false;
}
return crc == validCrc;
}

// get crc of the payload
var crc = CRC.crc32(tagged);
function isValidAddressShelley(address, currency, opts) {
const {networkType = 'prod'} = opts;
const decoded = bech32.decode(address);
if(!decoded) {
return false;
}

return crc == validCrc;
const bech32Hrp = decoded.hrp;
let correctBech32Hrps;
if (networkType === 'prod' || networkType === 'testnet') {
correctBech32Hrps = currency.bech32Hrp[networkType];
} else {
correctBech32Hrps = currency.bech32Hrp.prod.concat(currency.bech32Hrp.testnet)
}

if (correctBech32Hrps.indexOf(bech32Hrp) === -1) {
return false;
}

return true;
}

module.exports = {
isValidAddress: function (address, currency, opts = {}) {
return isValidAddressV1(address) || isValidAddressShelley(address, currency, opts);
}
};
6 changes: 3 additions & 3 deletions src/bitcoin_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function getAddressType(address, currency) {
}

function isValidP2PKHandP2SHAddress(address, currency, opts) {
var networkType = opts ? (opts.networkType || DEFAULT_NETWORK_TYPE) : DEFAULT_NETWORK_TYPE
const { networkType = DEFAULT_NETWORK_TYPE} = opts;

var correctAddressTypes;
var addressType = getAddressType(address, currency);
Expand All @@ -80,7 +80,7 @@ function isValidP2PKHandP2SHAddress(address, currency, opts) {
}

module.exports = {
isValidAddress: function (address, currency, networkType) {
return isValidP2PKHandP2SHAddress(address, currency, networkType) || segwit.isValidAddress(address, currency, networkType);
isValidAddress: function (address, currency, opts = {}) {
return isValidP2PKHandP2SHAddress(address, currency, opts) || segwit.isValidAddress(address, currency, opts);
}
};
2 changes: 1 addition & 1 deletion src/crypto/bech32.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function decode (bechString) {
}
bechString = bechString.toLowerCase();
var pos = bechString.lastIndexOf('1');
if (pos < 1 || pos + 7 > bechString.length || bechString.length > 90) {
if (pos < 1 || pos + 7 > bechString.length || bechString.length > 110) {
return null;
}
var hrp = bechString.substring(0, pos);
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/segwit_addr.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function encode (hrp, version, program) {
}

function isValidAddress(address, currency, opts) {
var networkType = opts ? (opts.networkType || DEFAULT_NETWORK_TYPE) : DEFAULT_NETWORK_TYPE
const { networkType = DEFAULT_NETWORK_TYPE} = opts;
var ret = decode(address);

if(ret === null) {
Expand All @@ -90,7 +90,7 @@ function isValidAddress(address, currency, opts) {
} else {
correctBech32Hrps = currency.bech32Hrp.prod.concat(currency.bech32Hrp.testnet)
}

if (correctBech32Hrps.indexOf(bech32Hrp) === -1) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var jsSHA = require('jssha/src/sha256');
var jsSHA = require('jssha/dist/sha256');
var Blake256 = require('./blake256');
var keccak256 = require('./sha3')['keccak256'];
var Blake2B = require('./blake2b');
Expand Down
1 change: 1 addition & 0 deletions src/currencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ var CURRENCIES = [{
}, {
name: 'Cardano',
symbol: 'ada',
bech32Hrp: { prod: ['addr'], testnet: ['addr']},
validator: ADAValidator
}, {
name: 'Monero',
Expand Down
4 changes: 2 additions & 2 deletions src/monero_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ function hextobin(hex) {
}

module.exports = {
isValidAddress: function(address, currency, opts) {
var networkType = opts ? (opts.networkType || DEFAULT_NETWORK_TYPE) : DEFAULT_NETWORK_TYPE
isValidAddress: function(address, currency, opts = {}) {
const { networkType = DEFAULT_NETWORK_TYPE} = opts;
var addressType = 'standard'
if (!addressRegTest.test(address)) {
if (integratedAddressRegTest.test(address)) {
Expand Down
8 changes: 8 additions & 0 deletions test/wallet_address_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ describe('WAValidator.validate()', function () {
valid('DdzFFzCqrhsfdzUZxvuBkhV8Lpm9p43p9ubh79GCTkxJikAjKh51qhtCFMqUniC5tv5ZExyvSmAte2Du2tGimavSo6qSgXbjiy8qZRTg', 'ada');
valid('Ae2tdPwUPEZKmwoy3AU3cXb5Chnasj6mvVNxV1H11997q3VW5ihbSfQwGpm', 'ada');
valid('4swhHtxKapQbj3TZEipgtp7NQzcRWDYqCxXYoPQWjGyHmhxS1w1TjUEszCQT1sQucGwmPQMYdv1FYs3d51KgoubviPBf', 'cardano');

valid('addr1qxy3w62dupy9pzmpdfzxz4k240w5vawyagl5m9djqquyymrtm3grn7gpnjh7rwh2dy62hk8639lt6kzn32yxq960usnq9pexvt', 'cardano');
valid('addr1skemppwfevyk0lshu2w8j34707s3t3t58a04xcx5ccevrcmvpmxg2qt4pk0', 'cardano', 'testnet');
});

it('should return true for correct monero addresses', function () {
Expand Down Expand Up @@ -812,6 +815,11 @@ describe('WAValidator.validate()', function () {
invalid('DdzFFzCqrhsfdzUZxvuBkhV8Lpm9p43p9ubh79GCTkxJikAjKh51qhtCFMqUniC5tv5ZExyvSmAte2Du2tGimavSo6qSgXbjiy8qZRTg1', 'cardano');
invalid('DdzFFzCqrhsfdzUZxvuBkhV8Lpm9p43p9ubh79GCTkxJikAjKh51qhtCFMqUniC5tv5ZExyvSmAte2Du2tGimavSo6qSgXbjiy8qZRT', 'ada');
//invalid('t2YNzUUx8mWBCRYPRezvA363EYXyEpHokyi', 'komodo', 'testnet');

invalid('adrr1qxy3w62dupy9pzmpdfzxz4k240w5vawyagl5m9djqquyymrtm3grn7gpnjh7rwh2dy62hk8639lt6kzn32yxq960usnq9pexvt', 'cardano');
invalid('addr2qxy3w62dupy9pzmpdfzxz4k240w5vawyagl5m9djqquyymrtm3grn7gpnjh7rwh2dy62hk8639lt6kzn32yxq960usnq9pexvt', 'cardano', 'prod');
invalid('addr1skemppmfevyk0lshu2w8j34707s3t3t58a04xcx5ccevrcmvpmxg2qt4pk0', 'cardano', 'testnet');

});

it('should return false for incorrect monero addresses', function () {
Expand Down
Loading

0 comments on commit 1af6f6b

Please sign in to comment.