@@ -10285,7 +10285,9 @@ SafeBuffer.allocUnsafeSlow = function (size) {
10285
10285
var cbor = require('cbor-js');
10286
10286
var CRC = require('crc');
10287
10287
var base58 = require('./crypto/base58');
10288
+ var bech32 = require('./crypto/bech32');
10288
10289
10290
+ var DEFAULT_NETWORK_TYPE = 'prod';
10289
10291
10290
10292
function getDecoded(address) {
10291
10293
try {
@@ -10297,28 +10299,53 @@ function getDecoded(address) {
10297
10299
}
10298
10300
}
10299
10301
10300
- module.exports = {
10301
- isValidAddress: function (address) {
10302
- var decoded = getDecoded(address);
10302
+ function isValidLegacyAddress(address) {
10303
+ var decoded = getDecoded(address);
10303
10304
10304
- if (!decoded || (!Array.isArray(decoded) && decoded.length != 2)) {
10305
- return false;
10306
- }
10305
+ if (!decoded || (!Array.isArray(decoded) && decoded.length != 2)) {
10306
+ return false;
10307
+ }
10307
10308
10308
- var tagged = decoded[0];
10309
- var validCrc = decoded[1];
10310
- if (typeof (validCrc) != 'number') {
10311
- return false;
10312
- }
10309
+ var tagged = decoded[0];
10310
+ var validCrc = decoded[1];
10311
+ if (typeof (validCrc) != 'number') {
10312
+ return false;
10313
+ }
10314
+
10315
+ // get crc of the payload
10316
+ var crc = CRC.crc32(tagged);
10317
+
10318
+ return crc == validCrc;
10319
+ }
10320
+
10321
+ // it is not possible to use bitcoin ./crypto/segwit_addr library, the cardano address is too long for that
10322
+ function isValidBech32Address(address, currency, networkType) {
10323
+ if (!currency.segwitHrp) {
10324
+ return false;
10325
+ }
10326
+ var hrp = currency.segwitHrp[networkType];
10327
+ if (!hrp) {
10328
+ return false;
10329
+ }
10313
10330
10314
- // get crc of the payload
10315
- var crc = CRC.crc32(tagged);
10331
+ var dec = bech32.decode(address, 103);
10316
10332
10317
- return crc == validCrc;
10333
+ if (dec === null || dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16) {
10334
+ return false;
10335
+ }
10336
+
10337
+ return true;
10338
+ }
10339
+
10340
+ module.exports = {
10341
+ isValidAddress: function (address, currency, networkType) {
10342
+ networkType = networkType || DEFAULT_NETWORK_TYPE;
10343
+ return isValidLegacyAddress(address)
10344
+ || isValidBech32Address(address, currency, networkType);
10318
10345
}
10319
10346
};
10320
10347
10321
- },{"./crypto/base58":137,"cbor-js":5,"crc":30}],130:[function(require,module,exports){
10348
+ },{"./crypto/base58":137,"./crypto/bech32":138," cbor-js":5,"crc":30}],130:[function(require,module,exports){
10322
10349
var base58 = require('./crypto/base58')
10323
10350
10324
10351
const ALLOWED_CHARS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
@@ -10734,7 +10761,8 @@ function encode (hrp, data) {
10734
10761
return ret;
10735
10762
}
10736
10763
10737
- function decode (bechString) {
10764
+ function decode (bechString, maxLength) {
10765
+ maxLength=maxLength || 90;
10738
10766
var p;
10739
10767
var has_lower = false;
10740
10768
var has_upper = false;
@@ -10754,7 +10782,7 @@ function decode (bechString) {
10754
10782
}
10755
10783
bechString = bechString.toLowerCase();
10756
10784
var pos = bechString.lastIndexOf('1');
10757
- if (pos < 1 || pos + 7 > bechString.length || bechString.length > 90 ) {
10785
+ if (pos < 1 || pos + 7 > bechString.length || bechString.length > maxLength ) {
10758
10786
return null;
10759
10787
}
10760
10788
var hrp = bechString.substring(0, pos);
@@ -14100,6 +14128,7 @@ var CURRENCIES = [
14100
14128
}, {
14101
14129
name: 'Cardano',
14102
14130
symbol: 'ada',
14131
+ segwitHrp: { prod: 'addr' },
14103
14132
validator: ADAValidator,
14104
14133
}, {
14105
14134
name: 'Monero',
0 commit comments