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.
- package/README.md +1 -1
- package/package-lock.json +479 -70
- package/package.json +15 -3
- package/src/chain-validators.ts +3 -1
- package/src/crypto/cnBase58.js +1 -1
- package/src/crypto/segwit_addr.js +1 -1
- package/src/{multichain-address-validator.ts → index.ts} +6 -1
- package/test/addresses/ltc-testnet.json +3 -1
- package/test/addresses/ltc.json +0 -2
- package/test/multichain-address-validator.test.ts +2 -3
- package/tsconfig.cjs.json +7 -0
- package/tsconfig.esm.json +7 -0
- package/tsconfig.json +1 -0
- package/dist/chain-validators.d.ts +0 -2
- package/dist/chain-validators.js +0 -119
- package/dist/crypto/base32.d.ts +0 -5
- package/dist/crypto/base32.js +0 -64
- package/dist/crypto/base58.d.ts +0 -4
- package/dist/crypto/base58.js +0 -42
- package/dist/crypto/bech32.d.ts +0 -17
- package/dist/crypto/bech32.js +0 -124
- package/dist/crypto/biginteger.d.ts +0 -57
- package/dist/crypto/biginteger.js +0 -1311
- package/dist/crypto/blake256.d.ts +0 -22
- package/dist/crypto/blake256.js +0 -169
- package/dist/crypto/blake2b.d.ts +0 -13
- package/dist/crypto/blake2b.js +0 -242
- package/dist/crypto/cnBase58.d.ts +0 -7
- package/dist/crypto/cnBase58.js +0 -209
- package/dist/crypto/segwit_addr.d.ts +0 -12
- package/dist/crypto/segwit_addr.js +0 -102
- package/dist/crypto/utils.d.ts +0 -26
- package/dist/crypto/utils.js +0 -123
- package/dist/helpers.d.ts +0 -2
- package/dist/helpers.js +0 -5
- package/dist/multichain-address-validator.d.ts +0 -3
- package/dist/multichain-address-validator.js +0 -8
- package/dist/types.d.ts +0 -15
- package/dist/types.js +0 -5
- package/dist/validators/algorand_validator.d.ts +0 -5
- package/dist/validators/algorand_validator.js +0 -23
- package/dist/validators/base58_validator.d.ts +0 -4
- package/dist/validators/base58_validator.js +0 -31
- package/dist/validators/bch_validator.d.ts +0 -13
- package/dist/validators/bch_validator.js +0 -50
- package/dist/validators/bip173_validator.d.ts +0 -7
- package/dist/validators/bip173_validator.js +0 -12
- package/dist/validators/bitcoin_validator.d.ts +0 -12
- package/dist/validators/bitcoin_validator.js +0 -68
- package/dist/validators/cardano_validator.d.ts +0 -5
- package/dist/validators/cardano_validator.js +0 -41
- package/dist/validators/eos_validator.d.ts +0 -5
- package/dist/validators/eos_validator.js +0 -10
- package/dist/validators/ethereum_validator.d.ts +0 -6
- package/dist/validators/ethereum_validator.js +0 -30
- package/dist/validators/hedera_validator.d.ts +0 -5
- package/dist/validators/hedera_validator.js +0 -8
- package/dist/validators/index.d.ts +0 -18
- package/dist/validators/index.js +0 -18
- package/dist/validators/monero_validator.d.ts +0 -5
- package/dist/validators/monero_validator.js +0 -58
- package/dist/validators/move_validator.d.ts +0 -5
- package/dist/validators/move_validator.js +0 -8
- package/dist/validators/nano_validator.d.ts +0 -6
- package/dist/validators/nano_validator.js +0 -23
- package/dist/validators/nem_validator.d.ts +0 -5
- package/dist/validators/nem_validator.js +0 -14
- package/dist/validators/polkadot_validator.d.ts +0 -5
- package/dist/validators/polkadot_validator.js +0 -49
- package/dist/validators/ripple_validator.d.ts +0 -10
- package/dist/validators/ripple_validator.js +0 -30
- package/dist/validators/sia_validator.d.ts +0 -5
- package/dist/validators/sia_validator.js +0 -27
- package/dist/validators/solana_validator.d.ts +0 -5
- package/dist/validators/solana_validator.js +0 -10
- package/dist/validators/tezos_validator.d.ts +0 -5
- package/dist/validators/tezos_validator.js +0 -30
- package/dist/validators/tron_validator.d.ts +0 -8
- package/dist/validators/tron_validator.js +0 -45
- package/dist/validators/xlm_validator.d.ts +0 -7
- package/dist/validators/xlm_validator.js +0 -38
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export { default as AlgorandValidator } from "./algorand_validator.js";
|
|
2
|
-
export { default as BCHValidator } from "./bch_validator.js";
|
|
3
|
-
export { default as BTCValidator } from "./bitcoin_validator.js";
|
|
4
|
-
export { default as CardanoValidator } from "./cardano_validator.js";
|
|
5
|
-
export { default as EOSValidator } from "./eos_validator.js";
|
|
6
|
-
export { default as ETHValidator } from "./ethereum_validator.js";
|
|
7
|
-
export { default as HederaValidator } from "./hedera_validator.js";
|
|
8
|
-
export { default as MoneroValidator } from "./monero_validator.js";
|
|
9
|
-
export { default as MoveValidator } from "./move_validator.js";
|
|
10
|
-
export { default as NemValidator } from "./nem_validator.js";
|
|
11
|
-
export { default as NanoValidator } from "./nano_validator.js";
|
|
12
|
-
export { default as PolkadotValidator } from "./polkadot_validator.js";
|
|
13
|
-
export { default as RippleValidator } from "./ripple_validator.js";
|
|
14
|
-
export { default as SiaValidator } from "./sia_validator.js";
|
|
15
|
-
export { default as SolanaValidator } from "./solana_validator.js";
|
|
16
|
-
export { default as TezosValidator } from "./tezos_validator.js";
|
|
17
|
-
export { default as TronValidator } from "./tron_validator.js";
|
|
18
|
-
export { default as XLMValidator } from "./xlm_validator.js";
|
package/dist/validators/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export { default as AlgorandValidator } from "./algorand_validator.js";
|
|
2
|
-
export { default as BCHValidator } from "./bch_validator.js";
|
|
3
|
-
export { default as BTCValidator } from "./bitcoin_validator.js";
|
|
4
|
-
export { default as CardanoValidator } from "./cardano_validator.js";
|
|
5
|
-
export { default as EOSValidator } from "./eos_validator.js";
|
|
6
|
-
export { default as ETHValidator } from "./ethereum_validator.js";
|
|
7
|
-
export { default as HederaValidator } from "./hedera_validator.js";
|
|
8
|
-
export { default as MoneroValidator } from "./monero_validator.js";
|
|
9
|
-
export { default as MoveValidator } from "./move_validator.js";
|
|
10
|
-
export { default as NemValidator } from "./nem_validator.js";
|
|
11
|
-
export { default as NanoValidator } from "./nano_validator.js";
|
|
12
|
-
export { default as PolkadotValidator } from "./polkadot_validator.js";
|
|
13
|
-
export { default as RippleValidator } from "./ripple_validator.js";
|
|
14
|
-
export { default as SiaValidator } from "./sia_validator.js";
|
|
15
|
-
export { default as SolanaValidator } from "./solana_validator.js";
|
|
16
|
-
export { default as TezosValidator } from "./tezos_validator.js";
|
|
17
|
-
export { default as TronValidator } from "./tron_validator.js";
|
|
18
|
-
export { default as XLMValidator } from "./xlm_validator.js";
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import cryptoUtils from '../crypto/utils.js';
|
|
2
|
-
import { getAddress } from '../helpers.js';
|
|
3
|
-
import cnBase58 from '../crypto/cnBase58.js';
|
|
4
|
-
const addressRegTest = new RegExp('^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{95}$');
|
|
5
|
-
const integratedAddressRegTest = new RegExp('^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{106}$');
|
|
6
|
-
const types = {
|
|
7
|
-
addressTypes: { mainnet: ['18', '42'], testnet: ['53', '63'], stagenet: ['24'] },
|
|
8
|
-
iAddressTypes: { mainnet: ['19'], testnet: ['54'], stagenet: ['25'] },
|
|
9
|
-
};
|
|
10
|
-
function validateNetwork(decoded, networkType, addressType) {
|
|
11
|
-
const addressTypes = addressType === 'integrated'
|
|
12
|
-
? types.iAddressTypes[networkType]
|
|
13
|
-
: types.addressTypes[networkType];
|
|
14
|
-
const at = parseInt(decoded.substr(0, 2), 16).toString();
|
|
15
|
-
switch (networkType) {
|
|
16
|
-
case 'mainnet':
|
|
17
|
-
return addressTypes.indexOf(at) >= 0;
|
|
18
|
-
case 'testnet':
|
|
19
|
-
return addressTypes.indexOf(at) >= 0;
|
|
20
|
-
// case 'stagenet':
|
|
21
|
-
// return network.stagenet.indexOf(at) >= 0
|
|
22
|
-
// case 'both':
|
|
23
|
-
// return network.prod.indexOf(at) >= 0 || network.testnet.indexOf(at) >= 0 || network.stagenet.indexOf(at) >= 0
|
|
24
|
-
default:
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
function hextobin(hex) {
|
|
29
|
-
if (hex.length % 2 !== 0)
|
|
30
|
-
return null;
|
|
31
|
-
const res = new Uint8Array(hex.length / 2);
|
|
32
|
-
for (let i = 0; i < hex.length / 2; ++i) {
|
|
33
|
-
res[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
34
|
-
}
|
|
35
|
-
return res;
|
|
36
|
-
}
|
|
37
|
-
export default (networkType) => ({
|
|
38
|
-
isValidAddress(address) {
|
|
39
|
-
const addr = getAddress(address);
|
|
40
|
-
let addressType = 'standard';
|
|
41
|
-
if (!addressRegTest.test(addr)) {
|
|
42
|
-
if (integratedAddressRegTest.test(addr)) {
|
|
43
|
-
addressType = 'integrated';
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
const decodedAddrStr = cnBase58.decode(addr);
|
|
50
|
-
if (!decodedAddrStr)
|
|
51
|
-
return false;
|
|
52
|
-
if (!validateNetwork(decodedAddrStr, networkType, addressType))
|
|
53
|
-
return false;
|
|
54
|
-
const addrChecksum = decodedAddrStr.slice(-8);
|
|
55
|
-
const hashChecksum = cryptoUtils.keccak256Checksum(hextobin(decodedAddrStr.slice(0, -8)));
|
|
56
|
-
return addrChecksum === hashChecksum;
|
|
57
|
-
}
|
|
58
|
-
});
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import baseX from 'base-x';
|
|
2
|
-
import cryptoUtils from '../crypto/utils.js';
|
|
3
|
-
import { getAddress } from '../helpers.js';
|
|
4
|
-
const ALLOWED_CHARS = '13456789abcdefghijkmnopqrstuwxyz';
|
|
5
|
-
const codec = baseX(ALLOWED_CHARS);
|
|
6
|
-
// https://github.com/nanocurrency/raiblocks/wiki/Accounts,-Keys,-Seeds,-and-Wallet-Identifiers
|
|
7
|
-
const regexp = new RegExp('^(xrb|nano)_([' + ALLOWED_CHARS + ']{60})$');
|
|
8
|
-
export default {
|
|
9
|
-
isValidAddress(address) {
|
|
10
|
-
const addr = getAddress(address);
|
|
11
|
-
if (regexp.test(addr)) {
|
|
12
|
-
return this.verifyChecksum(addr);
|
|
13
|
-
}
|
|
14
|
-
return false;
|
|
15
|
-
},
|
|
16
|
-
verifyChecksum: function (address) {
|
|
17
|
-
const bytes = codec.decode(regexp.exec(address)[2]).slice(-37);
|
|
18
|
-
// https://github.com/nanocurrency/raiblocks/blob/master/rai/lib/numbers.cpp#L73
|
|
19
|
-
const computedChecksum = cryptoUtils.blake2b(cryptoUtils.toHex(bytes.slice(0, -5)), 5);
|
|
20
|
-
const checksum = cryptoUtils.toHex(bytes.slice(-5).reverse());
|
|
21
|
-
return computedChecksum === checksum;
|
|
22
|
-
}
|
|
23
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Buffer } from 'buffer';
|
|
2
|
-
import cryptoUtils from '../crypto/utils.js';
|
|
3
|
-
import { getAddress } from '../helpers.js';
|
|
4
|
-
export default {
|
|
5
|
-
isValidAddress(address) {
|
|
6
|
-
const addr = getAddress(address).toString().toUpperCase().replace(/-/g, '');
|
|
7
|
-
if (!address || addr.length !== 40) {
|
|
8
|
-
return false;
|
|
9
|
-
}
|
|
10
|
-
const decoded = cryptoUtils.toHex(cryptoUtils.base32.b32decode(addr));
|
|
11
|
-
const stepThreeChecksum = cryptoUtils.keccak256Checksum(Buffer.from(decoded.slice(0, 42), 'hex'));
|
|
12
|
-
return stepThreeChecksum === decoded.slice(42);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import cryptoUtils from '../crypto/utils.js';
|
|
2
|
-
import { getAddress } from '../helpers.js';
|
|
3
|
-
// from https://github.com/paritytech/substrate/wiki/External-Address-Format-(SS58)
|
|
4
|
-
const addressFormats = [
|
|
5
|
-
{ addressLength: 3, accountIndexLength: 1, checkSumLength: 1 },
|
|
6
|
-
{ addressLength: 4, accountIndexLength: 2, checkSumLength: 1 },
|
|
7
|
-
{ addressLength: 5, accountIndexLength: 2, checkSumLength: 2 },
|
|
8
|
-
{ addressLength: 6, accountIndexLength: 4, checkSumLength: 1 },
|
|
9
|
-
{ addressLength: 7, accountIndexLength: 4, checkSumLength: 2 },
|
|
10
|
-
{ addressLength: 8, accountIndexLength: 4, checkSumLength: 3 },
|
|
11
|
-
{ addressLength: 9, accountIndexLength: 4, checkSumLength: 4 },
|
|
12
|
-
{ addressLength: 10, accountIndexLength: 8, checkSumLength: 1 },
|
|
13
|
-
{ addressLength: 11, accountIndexLength: 8, checkSumLength: 2 },
|
|
14
|
-
{ addressLength: 12, accountIndexLength: 8, checkSumLength: 3 },
|
|
15
|
-
{ addressLength: 13, accountIndexLength: 8, checkSumLength: 4 },
|
|
16
|
-
{ addressLength: 14, accountIndexLength: 8, checkSumLength: 5 },
|
|
17
|
-
{ addressLength: 15, accountIndexLength: 8, checkSumLength: 6 },
|
|
18
|
-
{ addressLength: 16, accountIndexLength: 8, checkSumLength: 7 },
|
|
19
|
-
{ addressLength: 17, accountIndexLength: 8, checkSumLength: 8 },
|
|
20
|
-
{ addressLength: 34, accountIndexLength: 32, checkSumLength: 2 },
|
|
21
|
-
];
|
|
22
|
-
function verifyChecksum(address) {
|
|
23
|
-
try {
|
|
24
|
-
const preImage = '53533538505245';
|
|
25
|
-
const decoded = cryptoUtils.base58(address);
|
|
26
|
-
const addressType = cryptoUtils.byteArray2hexStr(decoded.slice(0, 1));
|
|
27
|
-
const addressAndChecksum = decoded.slice(1);
|
|
28
|
-
// get the address format
|
|
29
|
-
const addressFormat = addressFormats.find(af => af.addressLength === addressAndChecksum.length);
|
|
30
|
-
if (!addressFormat) {
|
|
31
|
-
throw new Error('Invalid address length');
|
|
32
|
-
}
|
|
33
|
-
const decodedAddress = cryptoUtils.byteArray2hexStr(addressAndChecksum.slice(0, addressFormat.accountIndexLength));
|
|
34
|
-
const checksum = cryptoUtils.byteArray2hexStr(addressAndChecksum.slice(-addressFormat.checkSumLength));
|
|
35
|
-
const calculatedHash = cryptoUtils
|
|
36
|
-
.blake2b(preImage + addressType + decodedAddress, 64)
|
|
37
|
-
.substr(0, addressFormat.checkSumLength * 2)
|
|
38
|
-
.toUpperCase();
|
|
39
|
-
return calculatedHash == checksum;
|
|
40
|
-
}
|
|
41
|
-
catch (err) {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
export default {
|
|
46
|
-
isValidAddress(address) {
|
|
47
|
-
return verifyChecksum(getAddress(address));
|
|
48
|
-
},
|
|
49
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Address } from '../types.js';
|
|
2
|
-
declare const _default: {
|
|
3
|
-
/**
|
|
4
|
-
* ripple address validation
|
|
5
|
-
*/
|
|
6
|
-
isValidAddress: (address: Address) => any;
|
|
7
|
-
verifyMemo(destinationTag: string | null): boolean;
|
|
8
|
-
verifyChecksum: (address: string) => boolean;
|
|
9
|
-
};
|
|
10
|
-
export default _default;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import baseX from 'base-x';
|
|
2
|
-
import cryptoUtils from '../crypto/utils.js';
|
|
3
|
-
import { getAddress } from '../helpers.js';
|
|
4
|
-
const ALLOWED_CHARS = 'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz';
|
|
5
|
-
const codec = baseX(ALLOWED_CHARS);
|
|
6
|
-
const regexp = new RegExp('^r[' + ALLOWED_CHARS + ']{27,35}$');
|
|
7
|
-
export default {
|
|
8
|
-
/**
|
|
9
|
-
* ripple address validation
|
|
10
|
-
*/
|
|
11
|
-
isValidAddress: function (address) {
|
|
12
|
-
const addr = getAddress(address);
|
|
13
|
-
const destinationTag = address.destinationTag;
|
|
14
|
-
const validAddress = regexp.test(addr) && this.verifyChecksum(addr);
|
|
15
|
-
return validAddress && this.verifyMemo(destinationTag);
|
|
16
|
-
},
|
|
17
|
-
verifyMemo(destinationTag) {
|
|
18
|
-
if (!destinationTag)
|
|
19
|
-
return true; // Optional
|
|
20
|
-
const tagNumber = Number(destinationTag);
|
|
21
|
-
// A destination tag is a 32-bit unsigned integer.
|
|
22
|
-
return /^[0-9]+$/.test(destinationTag) && tagNumber >= 0 && tagNumber <= 4294967295;
|
|
23
|
-
},
|
|
24
|
-
verifyChecksum: function (address) {
|
|
25
|
-
const bytes = codec.decode(address);
|
|
26
|
-
const computedChecksum = cryptoUtils.sha256Checksum(cryptoUtils.toHex(bytes.slice(0, -4)));
|
|
27
|
-
const checksum = cryptoUtils.toHex(bytes.slice(-4));
|
|
28
|
-
return computedChecksum === checksum;
|
|
29
|
-
}
|
|
30
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import isEqual from 'lodash.isequal';
|
|
2
|
-
import cryptoUtils from '../crypto/utils.js';
|
|
3
|
-
import { getAddress } from '../helpers.js';
|
|
4
|
-
function hexToBytes(hex) {
|
|
5
|
-
const bytes = [];
|
|
6
|
-
for (let c = 0; c < hex.length; c += 2) {
|
|
7
|
-
bytes.push(parseInt(hex.substr(c, 2), 16));
|
|
8
|
-
}
|
|
9
|
-
return bytes;
|
|
10
|
-
}
|
|
11
|
-
function verifyChecksum(address) {
|
|
12
|
-
const checksumBytes = address.slice(0, 32 * 2);
|
|
13
|
-
const check = address.slice(32 * 2, 38 * 2);
|
|
14
|
-
const blakeHash = cryptoUtils.blake2b(checksumBytes, 32).slice(0, 6 * 2);
|
|
15
|
-
return !!isEqual(blakeHash, check);
|
|
16
|
-
}
|
|
17
|
-
export default {
|
|
18
|
-
isValidAddress: function (address) {
|
|
19
|
-
const addr = getAddress(address);
|
|
20
|
-
if (addr.length !== 76) {
|
|
21
|
-
// Check if it has the basic requirements of an address
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
// Otherwise check each case
|
|
25
|
-
return verifyChecksum(addr);
|
|
26
|
-
},
|
|
27
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import base58Validator from './base58_validator.js';
|
|
2
|
-
import { getAddress } from '../helpers.js';
|
|
3
|
-
export default {
|
|
4
|
-
isValidAddress: function (address) {
|
|
5
|
-
return base58Validator.isValidAddress(getAddress(address), {
|
|
6
|
-
maxLength: 44,
|
|
7
|
-
minLength: 43,
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import base58 from '../crypto/base58.js';
|
|
2
|
-
import cryptoUtils from '../crypto/utils.js';
|
|
3
|
-
import { getAddress } from '../helpers.js';
|
|
4
|
-
const prefix = new Uint8Array([6, 161, 159]);
|
|
5
|
-
function decodeRaw(buffer) {
|
|
6
|
-
let payload = buffer.slice(0, -4);
|
|
7
|
-
let checksum = buffer.slice(-4);
|
|
8
|
-
let newChecksum = cryptoUtils.hexStr2byteArray(cryptoUtils.sha256x2(cryptoUtils.byteArray2hexStr(payload)));
|
|
9
|
-
if (checksum[0] ^ newChecksum[0] |
|
|
10
|
-
checksum[1] ^ newChecksum[1] |
|
|
11
|
-
checksum[2] ^ newChecksum[2] |
|
|
12
|
-
checksum[3] ^ newChecksum[3])
|
|
13
|
-
return;
|
|
14
|
-
return payload;
|
|
15
|
-
}
|
|
16
|
-
export default {
|
|
17
|
-
isValidAddress(address) {
|
|
18
|
-
try {
|
|
19
|
-
let buffer = base58.decode(getAddress(address));
|
|
20
|
-
let payload = decodeRaw(buffer);
|
|
21
|
-
if (!payload)
|
|
22
|
-
return false;
|
|
23
|
-
payload.slice(prefix.length);
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
catch (e) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
};
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import cryptoUtils from '../crypto/utils.js';
|
|
2
|
-
import { getAddress } from '../helpers.js';
|
|
3
|
-
function decodeBase58Address(base58String) {
|
|
4
|
-
if (typeof (base58String) !== 'string') {
|
|
5
|
-
return false;
|
|
6
|
-
}
|
|
7
|
-
if (base58String.length !== 34) {
|
|
8
|
-
return false;
|
|
9
|
-
}
|
|
10
|
-
let address;
|
|
11
|
-
try {
|
|
12
|
-
address = cryptoUtils.base58(base58String);
|
|
13
|
-
}
|
|
14
|
-
catch (e) {
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
const len = address.length;
|
|
18
|
-
const offset = len - 4;
|
|
19
|
-
const checkSum = address.slice(offset);
|
|
20
|
-
address = address.slice(0, offset);
|
|
21
|
-
const hash0 = cryptoUtils.sha256(cryptoUtils.byteArray2hexStr(address));
|
|
22
|
-
const hash1 = cryptoUtils.hexStr2byteArray(cryptoUtils.sha256(hash0));
|
|
23
|
-
const checkSum1 = hash1.slice(0, 4);
|
|
24
|
-
if (checkSum[0] === checkSum1[0] && checkSum[1] === checkSum1[1] && checkSum[2]
|
|
25
|
-
=== checkSum1[2] && checkSum[3] === checkSum1[3]) {
|
|
26
|
-
return address;
|
|
27
|
-
}
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
const DefaultTronValidatorOpts = {
|
|
31
|
-
addressTypes: ['65']
|
|
32
|
-
};
|
|
33
|
-
export default (opts) => ({
|
|
34
|
-
isValidAddress: function (address) {
|
|
35
|
-
const _opts = { ...DefaultTronValidatorOpts, ...opts };
|
|
36
|
-
const addr = decodeBase58Address(getAddress(address));
|
|
37
|
-
if (!addr) {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
if (addr.length !== 21) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
return _opts.addressTypes.includes(addr[0].toString());
|
|
44
|
-
}
|
|
45
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import baseX from 'base-x';
|
|
2
|
-
import crc from 'crc';
|
|
3
|
-
import cryptoUtils from '../crypto/utils.js';
|
|
4
|
-
import { getAddress } from '../helpers.js';
|
|
5
|
-
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
|
|
6
|
-
const base32 = baseX(ALPHABET);
|
|
7
|
-
const regexp = new RegExp('^[' + ALPHABET + ']{56}$');
|
|
8
|
-
const ed25519PublicKeyVersionByte = (6 << 3);
|
|
9
|
-
function swap16(number) {
|
|
10
|
-
const lower = number & 0xFF;
|
|
11
|
-
const upper = (number >> 8) & 0xFF;
|
|
12
|
-
return (lower << 8) | upper;
|
|
13
|
-
}
|
|
14
|
-
export default {
|
|
15
|
-
isValidAddress: function (address) {
|
|
16
|
-
const addr = getAddress(address);
|
|
17
|
-
const destinationTag = address.destinationTag;
|
|
18
|
-
const validAddress = regexp.test(addr) && this.verifyChecksum(addr);
|
|
19
|
-
return validAddress && this.verifyMemo(destinationTag);
|
|
20
|
-
},
|
|
21
|
-
verifyMemo(memo) {
|
|
22
|
-
if (!memo)
|
|
23
|
-
return true; // Optional
|
|
24
|
-
// Ensure it's a valid UTF-8 string and does not exceed 28 bytes
|
|
25
|
-
const encoder = new TextEncoder();
|
|
26
|
-
return encoder.encode(memo).length <= 28;
|
|
27
|
-
},
|
|
28
|
-
verifyChecksum: function (address) {
|
|
29
|
-
// based on https://github.com/stellar/js-stellar-base/blob/master/src/strkey.js#L126
|
|
30
|
-
var bytes = base32.decode(address);
|
|
31
|
-
if (bytes[0] !== ed25519PublicKeyVersionByte) {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
const computedChecksum = cryptoUtils.numberToHex(swap16(crc.crc16xmodem(bytes.slice(0, -2))), 4);
|
|
35
|
-
const checksum = cryptoUtils.toHex(bytes.slice(-2));
|
|
36
|
-
return computedChecksum === checksum;
|
|
37
|
-
}
|
|
38
|
-
};
|