multichain-address-validator 0.7.2 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/README.md +1 -1
  2. package/package-lock.json +479 -70
  3. package/package.json +15 -3
  4. package/src/chain-validators.ts +3 -1
  5. package/src/crypto/cnBase58.js +1 -1
  6. package/src/crypto/segwit_addr.js +1 -1
  7. package/src/{multichain-address-validator.ts → index.ts} +6 -1
  8. package/test/addresses/ltc-testnet.json +3 -1
  9. package/test/addresses/ltc.json +0 -2
  10. package/test/multichain-address-validator.test.ts +2 -3
  11. package/tsconfig.cjs.json +7 -0
  12. package/tsconfig.esm.json +7 -0
  13. package/tsconfig.json +1 -0
  14. package/dist/chain-validators.d.ts +0 -2
  15. package/dist/chain-validators.js +0 -119
  16. package/dist/crypto/base32.d.ts +0 -5
  17. package/dist/crypto/base32.js +0 -64
  18. package/dist/crypto/base58.d.ts +0 -4
  19. package/dist/crypto/base58.js +0 -42
  20. package/dist/crypto/bech32.d.ts +0 -17
  21. package/dist/crypto/bech32.js +0 -124
  22. package/dist/crypto/biginteger.d.ts +0 -57
  23. package/dist/crypto/biginteger.js +0 -1311
  24. package/dist/crypto/blake256.d.ts +0 -22
  25. package/dist/crypto/blake256.js +0 -169
  26. package/dist/crypto/blake2b.d.ts +0 -13
  27. package/dist/crypto/blake2b.js +0 -242
  28. package/dist/crypto/cnBase58.d.ts +0 -7
  29. package/dist/crypto/cnBase58.js +0 -209
  30. package/dist/crypto/segwit_addr.d.ts +0 -12
  31. package/dist/crypto/segwit_addr.js +0 -102
  32. package/dist/crypto/utils.d.ts +0 -26
  33. package/dist/crypto/utils.js +0 -123
  34. package/dist/helpers.d.ts +0 -2
  35. package/dist/helpers.js +0 -5
  36. package/dist/multichain-address-validator.d.ts +0 -3
  37. package/dist/multichain-address-validator.js +0 -8
  38. package/dist/types.d.ts +0 -15
  39. package/dist/types.js +0 -5
  40. package/dist/validators/algorand_validator.d.ts +0 -5
  41. package/dist/validators/algorand_validator.js +0 -23
  42. package/dist/validators/base58_validator.d.ts +0 -4
  43. package/dist/validators/base58_validator.js +0 -31
  44. package/dist/validators/bch_validator.d.ts +0 -13
  45. package/dist/validators/bch_validator.js +0 -50
  46. package/dist/validators/bip173_validator.d.ts +0 -7
  47. package/dist/validators/bip173_validator.js +0 -12
  48. package/dist/validators/bitcoin_validator.d.ts +0 -12
  49. package/dist/validators/bitcoin_validator.js +0 -68
  50. package/dist/validators/cardano_validator.d.ts +0 -5
  51. package/dist/validators/cardano_validator.js +0 -41
  52. package/dist/validators/eos_validator.d.ts +0 -5
  53. package/dist/validators/eos_validator.js +0 -10
  54. package/dist/validators/ethereum_validator.d.ts +0 -6
  55. package/dist/validators/ethereum_validator.js +0 -30
  56. package/dist/validators/hedera_validator.d.ts +0 -5
  57. package/dist/validators/hedera_validator.js +0 -8
  58. package/dist/validators/index.d.ts +0 -18
  59. package/dist/validators/index.js +0 -18
  60. package/dist/validators/monero_validator.d.ts +0 -5
  61. package/dist/validators/monero_validator.js +0 -58
  62. package/dist/validators/move_validator.d.ts +0 -5
  63. package/dist/validators/move_validator.js +0 -8
  64. package/dist/validators/nano_validator.d.ts +0 -6
  65. package/dist/validators/nano_validator.js +0 -23
  66. package/dist/validators/nem_validator.d.ts +0 -5
  67. package/dist/validators/nem_validator.js +0 -14
  68. package/dist/validators/polkadot_validator.d.ts +0 -5
  69. package/dist/validators/polkadot_validator.js +0 -49
  70. package/dist/validators/ripple_validator.d.ts +0 -10
  71. package/dist/validators/ripple_validator.js +0 -30
  72. package/dist/validators/sia_validator.d.ts +0 -5
  73. package/dist/validators/sia_validator.js +0 -27
  74. package/dist/validators/solana_validator.d.ts +0 -5
  75. package/dist/validators/solana_validator.js +0 -10
  76. package/dist/validators/tezos_validator.d.ts +0 -5
  77. package/dist/validators/tezos_validator.js +0 -30
  78. package/dist/validators/tron_validator.d.ts +0 -8
  79. package/dist/validators/tron_validator.js +0 -45
  80. package/dist/validators/xlm_validator.d.ts +0 -7
  81. package/dist/validators/xlm_validator.js +0 -38
@@ -1,102 +0,0 @@
1
- // Copyright (c) 2017, 2021 Pieter Wuille
2
- //
3
- // Permission is hereby granted, free of charge, to any person obtaining a copy
4
- // of this software and associated documentation files (the "Software"), to deal
5
- // in the Software without restriction, including without limitation the rights
6
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- // copies of the Software, and to permit persons to whom the Software is
8
- // furnished to do so, subject to the following conditions:
9
- //
10
- // The above copyright notice and this permission notice shall be included in
11
- // all copies or substantial portions of the Software.
12
- //
13
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- // THE SOFTWARE.
20
- import bech32 from './bech32';
21
- function convertbits(data, frombits, tobits, pad) {
22
- var acc = 0;
23
- var bits = 0;
24
- var ret = [];
25
- var maxv = (1 << tobits) - 1;
26
- for (var p = 0; p < data.length; ++p) {
27
- var value = data[p];
28
- if (value < 0 || (value >> frombits) !== 0) {
29
- return null;
30
- }
31
- acc = (acc << frombits) | value;
32
- bits += frombits;
33
- while (bits >= tobits) {
34
- bits -= tobits;
35
- ret.push((acc >> bits) & maxv);
36
- }
37
- }
38
- if (pad) {
39
- if (bits > 0) {
40
- ret.push((acc << (tobits - bits)) & maxv);
41
- }
42
- }
43
- else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
44
- return null;
45
- }
46
- return ret;
47
- }
48
- function decode(hrp, addr) {
49
- var bech32m = false;
50
- var dec = bech32.decode(addr, bech32.encodings.BECH32);
51
- if (dec === null) {
52
- dec = bech32.decode(addr, bech32.encodings.BECH32M);
53
- bech32m = true;
54
- }
55
- if (dec === null || dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16) {
56
- return null;
57
- }
58
- var res = convertbits(dec.data.slice(1), 5, 8, false);
59
- if (res === null || res.length < 2 || res.length > 40) {
60
- return null;
61
- }
62
- if (dec.data[0] === 0 && res.length !== 20 && res.length !== 32) {
63
- return null;
64
- }
65
- if (dec.data[0] === 0 && bech32m) {
66
- return null;
67
- }
68
- if (dec.data[0] !== 0 && !bech32m) {
69
- return null;
70
- }
71
- return { version: dec.data[0], program: res };
72
- }
73
- function encode(hrp, version, program) {
74
- var enc = bech32.encodings.BECH32;
75
- if (version > 0) {
76
- enc = bech32.encodings.BECH32M;
77
- }
78
- var ret = bech32.encode(hrp, [version].concat(convertbits(program, 8, 5, true)), enc);
79
- if (decode(hrp, ret, enc) === null) {
80
- return null;
81
- }
82
- return ret;
83
- }
84
- /////////////////////////////////////////////////////
85
- function isValidAddress(address, opts = {}) {
86
- if (!opts.bech32Hrp || opts.bech32Hrp.length === 0) {
87
- return false;
88
- }
89
- const correctBech32Hrps = opts.bech32Hrp;
90
- for (var chrp of correctBech32Hrps) {
91
- var ret = decode(chrp, address);
92
- if (ret) {
93
- return encode(chrp, ret.version, ret.program) === address.toLowerCase();
94
- }
95
- }
96
- return false;
97
- }
98
- export default {
99
- encode: encode,
100
- decode: decode,
101
- isValidAddress: isValidAddress,
102
- };
@@ -1,26 +0,0 @@
1
- declare function numberToHex(number: number, length: number): string;
2
- declare function byteArray2hexStr(byteArray: number[]): string;
3
- declare function hexStr2byteArray(str: string): Uint8Array;
4
- declare const _default: {
5
- numberToHex: typeof numberToHex;
6
- toHex: (arrayOfBytes: any) => string;
7
- sha256: (payload: any, format?: string) => string;
8
- sha256x2: (buffer: any, format?: string) => any;
9
- sha256Checksum: (payload: any) => any;
10
- sha512: (payload: any, format?: string) => string;
11
- sha512_256: (payload: any, format?: string) => string;
12
- blake256: (hexString: string) => any;
13
- blake256Checksum: (payload: any) => any;
14
- blake2b: (hexString: string, outlen: number) => any;
15
- keccak256: (hexString: string) => string;
16
- keccak256Checksum: (payload: any) => any;
17
- blake2b256: (hexString: string) => any;
18
- base58: (string: string) => number[];
19
- byteArray2hexStr: typeof byteArray2hexStr;
20
- hexStr2byteArray: typeof hexStr2byteArray;
21
- base32: {
22
- b32decode: (s: string) => Uint8Array;
23
- b32encode: (s: string) => string;
24
- };
25
- };
26
- export default _default;
@@ -1,123 +0,0 @@
1
- import { Buffer } from 'buffer';
2
- import { sha256 } from '@noble/hashes/sha256';
3
- import { sha512, sha512_256 } from '@noble/hashes/sha512';
4
- import { bytesToHex } from '@noble/hashes/utils';
5
- import { keccak_256 } from '@noble/hashes/sha3';
6
- import base32 from './base32.js';
7
- import base58 from './base58.js';
8
- import Blake256 from './blake256.js';
9
- import Blake2B from './blake2b.js';
10
- function numberToHex(number, length) {
11
- let hex = number.toString(16);
12
- if (hex.length % 2 === 1) {
13
- hex = '0' + hex;
14
- }
15
- return hex.padStart(length, '0');
16
- }
17
- function isHexChar(c) {
18
- if ((c >= 'A' && c <= 'F') ||
19
- (c >= 'a' && c <= 'f') ||
20
- (c >= '0' && c <= '9')) {
21
- return 1;
22
- }
23
- return 0;
24
- }
25
- /* Convert a hex char to value */
26
- function hexChar2byte(c) {
27
- let d = 0;
28
- if (c >= 'A' && c <= 'F') {
29
- d = c.charCodeAt(0) - 'A'.charCodeAt(0) + 10;
30
- }
31
- else if (c >= 'a' && c <= 'f') {
32
- d = c.charCodeAt(0) - 'a'.charCodeAt(0) + 10;
33
- }
34
- else if (c >= '0' && c <= '9') {
35
- d = c.charCodeAt(0) - '0'.charCodeAt(0);
36
- }
37
- return d;
38
- }
39
- /* Convert a byte to string */
40
- function byte2hexStr(byte) {
41
- const hexByteMap = "0123456789ABCDEF";
42
- let str = "";
43
- str += hexByteMap.charAt(byte >> 4);
44
- str += hexByteMap.charAt(byte & 0x0f);
45
- return str;
46
- }
47
- function byteArray2hexStr(byteArray) {
48
- let str = "";
49
- let i = 0;
50
- for (i = 0; i < (byteArray.length - 1); i++) {
51
- str += byte2hexStr(byteArray[i]);
52
- }
53
- str += byte2hexStr(byteArray[i]);
54
- return str;
55
- }
56
- function hexStr2byteArray(str) {
57
- const byteArray = new Uint8Array(str.length / 2);
58
- let d = 0;
59
- let i = 0;
60
- let j = 0;
61
- let k = 0;
62
- for (i = 0; i < str.length; i++) {
63
- const c = str.charAt(i);
64
- if (isHexChar(c)) {
65
- d <<= 4;
66
- d += hexChar2byte(c);
67
- j++;
68
- if (0 === (j % 2)) {
69
- byteArray[k++] = d;
70
- d = 0;
71
- }
72
- }
73
- }
74
- return byteArray;
75
- }
76
- export default {
77
- numberToHex: numberToHex,
78
- toHex: function (arrayOfBytes) {
79
- let hex = '';
80
- for (let i = 0; i < arrayOfBytes.length; i++) {
81
- // @ts-expect-error
82
- hex += numberToHex(arrayOfBytes[i]);
83
- }
84
- return hex;
85
- },
86
- sha256: function (payload, format = 'HEX') {
87
- return bytesToHex(sha256(hexStr2byteArray(payload)));
88
- },
89
- sha256x2: function (buffer, format = 'HEX') {
90
- return this.sha256(this.sha256(buffer, format), format);
91
- },
92
- sha256Checksum: function (payload) {
93
- return this.sha256(this.sha256(payload)).slice(0, 8);
94
- },
95
- sha512: function (payload, format = 'HEX') {
96
- return bytesToHex(sha512(payload));
97
- },
98
- sha512_256: function (payload, format = 'HEX') {
99
- return bytesToHex(sha512_256(hexStr2byteArray(payload)));
100
- },
101
- blake256: function (hexString) {
102
- return new Blake256().update(hexString, 'hex').digest('hex');
103
- },
104
- blake256Checksum: function (payload) {
105
- return this.blake256(this.blake256(payload)).substr(0, 8);
106
- },
107
- blake2b: function (hexString, outlen) {
108
- return new Blake2B(outlen).update(Buffer.from(hexString, 'hex')).digest('hex');
109
- },
110
- keccak256: function (hexString) {
111
- return bytesToHex(keccak_256(hexString));
112
- },
113
- keccak256Checksum: function (payload) {
114
- return this.keccak256(payload).toString().substr(0, 8);
115
- },
116
- blake2b256: function (hexString) {
117
- return new Blake2B(32).update(Buffer.from(hexString, 'hex')).digest('hex');
118
- },
119
- base58: base58.decode,
120
- byteArray2hexStr: byteArray2hexStr,
121
- hexStr2byteArray: hexStr2byteArray,
122
- base32: base32
123
- };
package/dist/helpers.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { Address } from './types.js';
2
- export declare function getAddress(address: Address): string;
package/dist/helpers.js DELETED
@@ -1,5 +0,0 @@
1
- export function getAddress(address) {
2
- return typeof address === 'string'
3
- ? address
4
- : address.address;
5
- }
@@ -1,3 +0,0 @@
1
- import { Address, Chain, NetworkType } from './types.js';
2
- export declare function validate(address: Address, chain: Chain): boolean;
3
- export type { Address, Chain, NetworkType };
@@ -1,8 +0,0 @@
1
- import { getValidatorForChain } from './chain-validators.js';
2
- export function validate(address, chain) {
3
- const validator = getValidatorForChain(chain);
4
- if (!validator) {
5
- throw new Error(`Missing validator for chain: ${chain}`);
6
- }
7
- return validator.isValidAddress(address);
8
- }
package/dist/types.d.ts DELETED
@@ -1,15 +0,0 @@
1
- export type Address = string | {
2
- address: string;
3
- memo?: string;
4
- };
5
- export type Chain = string | {
6
- chain: string;
7
- networkType?: NetworkType;
8
- };
9
- export interface Validator {
10
- isValidAddress(address: Address): boolean;
11
- }
12
- export declare enum NetworkType {
13
- MainNet = "mainnet",
14
- TestNet = "testnet"
15
- }
package/dist/types.js DELETED
@@ -1,5 +0,0 @@
1
- export var NetworkType;
2
- (function (NetworkType) {
3
- NetworkType["MainNet"] = "mainnet";
4
- NetworkType["TestNet"] = "testnet";
5
- })(NetworkType || (NetworkType = {}));
@@ -1,5 +0,0 @@
1
- import { Address } from '../types.js';
2
- declare const _default: {
3
- isValidAddress: (address: Address) => boolean;
4
- };
5
- export default _default;
@@ -1,23 +0,0 @@
1
- import cryptoUtils from '../crypto/utils.js';
2
- import { getAddress } from '../helpers.js';
3
- const ALGORAND_CHECKSUM_BYTE_LENGTH = 4;
4
- const ALGORAND_ADDRESS_LENGTH = 58;
5
- function verifyChecksum(address) {
6
- if (address.length !== ALGORAND_ADDRESS_LENGTH) {
7
- return false;
8
- }
9
- else {
10
- // Decode base32 Address
11
- const decoded = cryptoUtils.base32.b32decode(address);
12
- const addr = decoded.slice(0, decoded.length - ALGORAND_CHECKSUM_BYTE_LENGTH);
13
- const checksum = cryptoUtils.byteArray2hexStr(decoded.slice(-4));
14
- // Hash Address - Checksum
15
- const code = cryptoUtils.sha512_256(cryptoUtils.byteArray2hexStr(addr)).substr(-ALGORAND_CHECKSUM_BYTE_LENGTH * 2);
16
- return code.toUpperCase() === checksum;
17
- }
18
- }
19
- export default {
20
- isValidAddress: function (address) {
21
- return verifyChecksum(getAddress(address));
22
- }
23
- };
@@ -1,4 +0,0 @@
1
- declare const _default: {
2
- isValidAddress: (address: any, opts: any) => boolean;
3
- };
4
- export default _default;
@@ -1,31 +0,0 @@
1
- import base58 from '../crypto/base58.js';
2
- // simple base58 validator. Just checks if it can be decoded.
3
- export default {
4
- isValidAddress: function (address, opts) {
5
- try {
6
- if (!address || address.length == 0) {
7
- return false;
8
- }
9
- if (opts.minLength && (address.length < opts.minLength)) {
10
- return false;
11
- }
12
- if (opts.maxLength && (address.length > opts.maxLength)) {
13
- return false;
14
- }
15
- try {
16
- const decoded = base58.decode(address);
17
- if (!decoded || !decoded.length) {
18
- return false;
19
- }
20
- }
21
- catch (e) {
22
- // if decoding fails, assume invalid address
23
- return false;
24
- }
25
- return true;
26
- }
27
- catch (e) {
28
- return false;
29
- }
30
- }
31
- };
@@ -1,13 +0,0 @@
1
- import { Address, NetworkType } from '../types.js';
2
- interface BCHValidatorOpts {
3
- addressTypes: string[];
4
- expectedLength?: number;
5
- bech32Hrp?: [string];
6
- hashFunction?: 'blake256' | 'blake256keccak256' | 'keccak256' | 'sha256';
7
- regexp?: RegExp;
8
- networkType: NetworkType;
9
- }
10
- declare const _default: (opts: BCHValidatorOpts) => {
11
- isValidAddress: (address: Address) => boolean;
12
- };
13
- export default _default;
@@ -1,50 +0,0 @@
1
- import cryptoUtils from '../crypto/utils.js';
2
- import bech32 from '../crypto/bech32.js';
3
- import { NetworkType } from '../types.js';
4
- import BTCValidator from './bitcoin_validator.js';
5
- import { getAddress } from '../helpers.js';
6
- function validateAddress(address, opts) {
7
- const regexp = new RegExp(opts.regexp);
8
- let raw_address;
9
- const res = address.split(':');
10
- if (res.length === 1) {
11
- raw_address = address;
12
- }
13
- else {
14
- if (res[0] !== 'bitcoincash') {
15
- return false;
16
- }
17
- raw_address = res[1];
18
- }
19
- if (!regexp.test(raw_address)) {
20
- return false;
21
- }
22
- if (raw_address.toLowerCase() != raw_address && raw_address.toUpperCase() != raw_address) {
23
- return false;
24
- }
25
- const decoded = cryptoUtils.base32.b32decode(raw_address);
26
- const prefix = opts.networkType === NetworkType.MainNet
27
- ? 'bitcoincash'
28
- : 'bchtest';
29
- try {
30
- if (bech32.verifyChecksum(prefix, decoded, bech32.encodings.BECH32)) {
31
- return false;
32
- }
33
- }
34
- catch (e) {
35
- return false;
36
- }
37
- return true;
38
- }
39
- const DefaultBCHValidatorOpts = {
40
- // addressTypes: {mainnet: ['00', '05'], testnet: ['6f', 'c4', '3c', '26']},
41
- // bech32Hrp: {mainnet: ['bc'], testnet: ['tb']},
42
- regexp: /^[qQpP][0-9a-zA-Z]{41}$/,
43
- };
44
- export default (opts) => ({
45
- isValidAddress: function (address) {
46
- const addr = getAddress(address);
47
- const _opts = { ...DefaultBCHValidatorOpts, ...opts };
48
- return validateAddress(addr, _opts) || BTCValidator(opts).isValidAddress(address);
49
- }
50
- });
@@ -1,7 +0,0 @@
1
- interface Bip173Options {
2
- bech32Hrp: string[];
3
- }
4
- declare const _default: {
5
- isValidAddress: (address: string, opts: Bip173Options) => boolean;
6
- };
7
- export default _default;
@@ -1,12 +0,0 @@
1
- import bech32 from '../crypto/bech32.js';
2
- // bip 173 bech 32 addresses (https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki)
3
- export default {
4
- isValidAddress: function (address, opts) {
5
- const decoded = bech32.decode(address, bech32.encodings.BECH32);
6
- if (!decoded) {
7
- return false;
8
- }
9
- const bech32Hrp = decoded.hrp;
10
- return opts.bech32Hrp.indexOf(bech32Hrp) !== -1;
11
- }
12
- };
@@ -1,12 +0,0 @@
1
- import { Address } from '../types.js';
2
- interface BTCValidatorOpts {
3
- addressTypes: string[];
4
- expectedLength?: number;
5
- bech32Hrp?: [string];
6
- hashFunction?: 'blake256' | 'blake256keccak256' | 'keccak256' | 'sha256';
7
- regex?: RegExp;
8
- }
9
- declare const _default: (opts: BTCValidatorOpts) => {
10
- isValidAddress(address: Address): boolean;
11
- };
12
- export default _default;
@@ -1,68 +0,0 @@
1
- import base58 from '../crypto/base58.js';
2
- import segwit from '../crypto/segwit_addr.js';
3
- import cryptoUtils from '../crypto/utils.js';
4
- import { getAddress } from '../helpers.js';
5
- import { Buffer } from 'buffer';
6
- function getDecoded(address) {
7
- try {
8
- return base58.decode(address);
9
- }
10
- catch (e) {
11
- // if decoding fails, assume invalid address
12
- return null;
13
- }
14
- }
15
- function getChecksum(hashFunction, payload) {
16
- // Each currency may implement different hashing algorithm
17
- switch (hashFunction) {
18
- // blake then keccak hash chain
19
- case 'blake256keccak256':
20
- const blake = cryptoUtils.blake2b256(payload);
21
- return cryptoUtils.keccak256Checksum(Buffer.from(blake, 'hex'));
22
- case 'blake256':
23
- return cryptoUtils.blake256Checksum(payload);
24
- case 'keccak256':
25
- return cryptoUtils.keccak256Checksum(payload);
26
- case 'sha256':
27
- default:
28
- return cryptoUtils.sha256Checksum(payload);
29
- }
30
- }
31
- function getAddressType(address, opts) {
32
- // should be 25 bytes per btc address spec and 26 decred
33
- const expectedLength = opts.expectedLength || 25;
34
- const hashFunction = opts.hashFunction || 'sha256';
35
- const decoded = getDecoded(address);
36
- if (decoded) {
37
- const length = decoded.length;
38
- if (length !== expectedLength) {
39
- return null;
40
- }
41
- if (opts.regex) {
42
- if (!opts.regex.test(address)) {
43
- return false;
44
- }
45
- }
46
- const checksum = cryptoUtils.toHex(decoded.slice(length - 4, length)), body = cryptoUtils.toHex(decoded.slice(0, length - 4)), goodChecksum = getChecksum(hashFunction, body);
47
- return checksum === goodChecksum ? cryptoUtils.toHex(decoded.slice(0, expectedLength - 24)) : null;
48
- }
49
- return null;
50
- }
51
- function isValidP2PKHandP2SHAddress(address, opts) {
52
- const addressType = getAddressType(address, opts);
53
- if (addressType) {
54
- return opts.addressTypes.indexOf(addressType) >= 0;
55
- }
56
- return false;
57
- }
58
- // const DefaultBTCValidatorOpts: BTCValidatorOpts = {
59
- // addressTypes: {mainnet: ['00', '05'], testnet: ['6f', 'c4', '3c', '26']},
60
- // bech32Hrp: {mainnet: ['bc'], testnet: ['tb']},
61
- // }
62
- export default (opts) => ({
63
- isValidAddress(address) {
64
- const addr = getAddress(address);
65
- // const _opts = {...DefaultBTCValidatorOpts, ...opts}
66
- return isValidP2PKHandP2SHAddress(addr, opts) || segwit.isValidAddress(addr, opts);
67
- }
68
- });
@@ -1,5 +0,0 @@
1
- import { Address } from '../types.js';
2
- declare const _default: {
3
- isValidAddress(address: Address): boolean;
4
- };
5
- export default _default;
@@ -1,41 +0,0 @@
1
- import cbor from 'cbor-js';
2
- import CRC from 'crc';
3
- import base58 from '../crypto/base58.js';
4
- import BIP173Validator from './bip173_validator.js';
5
- import { getAddress } from '../helpers.js';
6
- function getDecoded(address) {
7
- try {
8
- const decoded = base58.decode(address);
9
- return cbor.decode(new Uint8Array(decoded).buffer);
10
- }
11
- catch (e) {
12
- // if decoding fails, assume invalid address
13
- return null;
14
- }
15
- }
16
- function isValidAddressV1(address) {
17
- const decoded = getDecoded(address);
18
- if (!decoded || (!Array.isArray(decoded) && decoded.length != 2)) {
19
- return false;
20
- }
21
- const tagged = decoded[0];
22
- const validCrc = decoded[1];
23
- if (typeof (validCrc) != 'number') {
24
- return false;
25
- }
26
- // get crc of the payload
27
- const crc = CRC.crc32(tagged);
28
- return crc == validCrc;
29
- }
30
- function isValidAddressShelley(address, opts) {
31
- // shelley address are just bip 173 - bech32 addresses (https://cips.cardano.org/cips/cip4/)
32
- return BIP173Validator.isValidAddress(address, opts);
33
- }
34
- export default {
35
- isValidAddress(address) {
36
- const addr = getAddress(address);
37
- return isValidAddressV1(getAddress(addr)) || isValidAddressShelley(addr, {
38
- bech32Hrp: ['addr']
39
- });
40
- }
41
- };
@@ -1,5 +0,0 @@
1
- import { Address } from '../types.js';
2
- declare const _default: {
3
- isValidAddress: (address: Address) => boolean;
4
- };
5
- export default _default;
@@ -1,10 +0,0 @@
1
- import { getAddress } from '../helpers.js';
2
- function isValidEOSAddress(address) {
3
- const regex = /^[a-z0-9.]+$/g; // Must be numbers, lowercase letters and decimal points only
4
- return address.search(regex) !== -1 && address.length === 12;
5
- }
6
- export default {
7
- isValidAddress: function (address) {
8
- return isValidEOSAddress(getAddress(address));
9
- }
10
- };
@@ -1,6 +0,0 @@
1
- import { Address } from '../types.js';
2
- declare const _default: {
3
- isValidAddress: (address: Address) => any;
4
- verifyChecksum: (address: string) => boolean;
5
- };
6
- export default _default;
@@ -1,30 +0,0 @@
1
- import cryptoUtils from '../crypto/utils.js';
2
- import { getAddress } from '../helpers.js';
3
- export default {
4
- isValidAddress: function (address) {
5
- const addr = getAddress(address);
6
- if (!/^0x[0-9a-fA-F]{40}$/.test(addr)) {
7
- // Check if it has the basic requirements of an address
8
- return false;
9
- }
10
- if (/^0x[0-9a-f]{40}$/.test(addr) || /^0x?[0-9A-F]{40}$/.test(addr)) {
11
- // If it's all small caps or all caps, return true
12
- return true;
13
- }
14
- // Otherwise check each case
15
- return this.verifyChecksum(addr);
16
- },
17
- verifyChecksum: function (address) {
18
- // Check each case
19
- address = address.replace('0x', '');
20
- const addressHash = cryptoUtils.keccak256(address.toLowerCase());
21
- for (let i = 0; i < 40; i++) {
22
- // The nth letter should be uppercase if the nth digit of casemap is 1
23
- if ((parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) ||
24
- (parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])) {
25
- return false;
26
- }
27
- }
28
- return true;
29
- }
30
- };
@@ -1,5 +0,0 @@
1
- import type { Address } from '../types.js';
2
- declare const _default: {
3
- isValidAddress(address: Address): boolean;
4
- };
5
- export default _default;
@@ -1,8 +0,0 @@
1
- import { getAddress } from '../helpers.js';
2
- export default {
3
- isValidAddress(address) {
4
- // Regex to validate the format "shard.realm.account"
5
- const regex = /^\d+\.\d+\.\d+$/;
6
- return regex.test(getAddress(address));
7
- }
8
- };