validator0x 0.2.0 → 0.3.0
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/AGENTS.md +65 -0
- package/CHANGELOG.md +34 -0
- package/README.md +193 -101
- package/dist/bitcoin.cjs +1 -0
- package/dist/bitcoin.d.cts +1 -0
- package/dist/bitcoin.d.mts +1 -0
- package/dist/bitcoin.mjs +1 -0
- package/dist/cardano.cjs +1 -0
- package/dist/cardano.d.cts +1 -0
- package/dist/cardano.d.mts +1 -0
- package/dist/cardano.mjs +1 -0
- package/dist/cosmos.cjs +1 -0
- package/dist/cosmos.d.cts +1 -0
- package/dist/cosmos.d.mts +1 -0
- package/dist/cosmos.mjs +1 -0
- package/dist/dogecoin.cjs +1 -0
- package/dist/dogecoin.d.cts +1 -0
- package/dist/dogecoin.d.mts +1 -0
- package/dist/dogecoin.mjs +1 -0
- package/dist/ethereum.cjs +1 -0
- package/dist/ethereum.d.cts +1 -0
- package/dist/ethereum.d.mts +1 -0
- package/dist/ethereum.mjs +1 -0
- package/dist/evm.cjs +1 -0
- package/dist/evm.d.cts +1 -0
- package/dist/evm.d.mts +1 -0
- package/dist/evm.mjs +1 -0
- package/dist/formatters.cjs +1 -0
- package/dist/formatters.d.cts +1 -0
- package/dist/formatters.d.mts +1 -0
- package/dist/formatters.mjs +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +164 -0
- package/dist/index.d.mts +164 -0
- package/dist/index.mjs +1 -0
- package/dist/index.umd.js +1 -2
- package/dist/litecoin.cjs +1 -0
- package/dist/litecoin.d.cts +1 -0
- package/dist/litecoin.d.mts +1 -0
- package/dist/litecoin.mjs +1 -0
- package/dist/move.cjs +1 -0
- package/dist/move.d.cts +1 -0
- package/dist/move.d.mts +1 -0
- package/dist/move.mjs +1 -0
- package/dist/near.cjs +1 -0
- package/dist/near.d.cts +1 -0
- package/dist/near.d.mts +1 -0
- package/dist/near.mjs +1 -0
- package/dist/ripple.cjs +1 -0
- package/dist/ripple.d.cts +1 -0
- package/dist/ripple.d.mts +1 -0
- package/dist/ripple.mjs +1 -0
- package/dist/solana.cjs +1 -0
- package/dist/solana.d.cts +1 -0
- package/dist/solana.d.mts +1 -0
- package/dist/solana.mjs +1 -0
- package/dist/tron.cjs +1 -0
- package/dist/tron.d.cts +1 -0
- package/dist/tron.d.mts +1 -0
- package/dist/tron.mjs +1 -0
- package/package.json +244 -29
- package/dist/detectors/chain-detector.d.ts +0 -8
- package/dist/detectors/chain-detector.d.ts.map +0 -1
- package/dist/formatters/index.d.ts +0 -5
- package/dist/formatters/index.d.ts.map +0 -1
- package/dist/index.cjs.js +0 -2
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.d.ts +0 -20
- package/dist/index.d.ts.map +0 -1
- package/dist/index.esm.js +0 -2
- package/dist/index.esm.js.map +0 -1
- package/dist/index.umd.js.map +0 -1
- package/dist/security/sanitizer.d.ts +0 -4
- package/dist/security/sanitizer.d.ts.map +0 -1
- package/dist/types.d.ts +0 -36
- package/dist/types.d.ts.map +0 -1
- package/dist/utils/base58.d.ts +0 -3
- package/dist/utils/base58.d.ts.map +0 -1
- package/dist/utils/bech32.d.ts +0 -12
- package/dist/utils/bech32.d.ts.map +0 -1
- package/dist/utils/hex.d.ts +0 -4
- package/dist/utils/hex.d.ts.map +0 -1
- package/dist/utils/keccak256.d.ts +0 -2
- package/dist/utils/keccak256.d.ts.map +0 -1
- package/dist/utils/sha256.d.ts +0 -2
- package/dist/utils/sha256.d.ts.map +0 -1
- package/dist/validators/base.d.ts +0 -12
- package/dist/validators/base.d.ts.map +0 -1
- package/dist/validators/bitcoin.d.ts +0 -9
- package/dist/validators/bitcoin.d.ts.map +0 -1
- package/dist/validators/cardano.d.ts +0 -9
- package/dist/validators/cardano.d.ts.map +0 -1
- package/dist/validators/dogecoin.d.ts +0 -7
- package/dist/validators/dogecoin.d.ts.map +0 -1
- package/dist/validators/ethereum.d.ts +0 -10
- package/dist/validators/ethereum.d.ts.map +0 -1
- package/dist/validators/evm.d.ts +0 -18
- package/dist/validators/evm.d.ts.map +0 -1
- package/dist/validators/litecoin.d.ts +0 -9
- package/dist/validators/litecoin.d.ts.map +0 -1
- package/dist/validators/polygon.d.ts +0 -6
- package/dist/validators/polygon.d.ts.map +0 -1
- package/dist/validators/ripple.d.ts +0 -7
- package/dist/validators/ripple.d.ts.map +0 -1
- package/dist/validators/solana.d.ts +0 -7
- package/dist/validators/solana.d.ts.map +0 -1
package/AGENTS.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# validator0x: instructions for AI coding agents
|
|
2
|
+
|
|
3
|
+
This file serves two audiences: agents integrating the installed npm package, and agents changing this repository. Apply only the relevant section. Read `README.md` and the installed package version before using an API. The consuming project's instructions and the user's explicit requirements take precedence.
|
|
4
|
+
|
|
5
|
+
## Integrating the npm package
|
|
6
|
+
|
|
7
|
+
1. Use the public package exports. Import from `validator0x` for multiple chains, or from a documented family entrypoint such as `validator0x/bitcoin` or `validator0x/solana` for smaller bundles. Never import private `src/` or `dist/` paths.
|
|
8
|
+
2. The package is synchronous and offline. It needs no API key, RPC connection, wallet connection, Node crypto polyfill, or runtime dependency. Node.js 22+ and modern ES2020 browsers/workers are supported; browser runtimes need BigInt and TextEncoder.
|
|
9
|
+
3. Take the intended chain from application state or the user. For dynamic chain values, narrow with `isSupportedChain` before calling `validateAddress`, `validateBatch`, or `formatAddress`; unsupported chains throw.
|
|
10
|
+
4. Pass untrusted addresses directly to `validateAddress`. It accepts `unknown` and returns `ValidationResult`. Check `result.valid` before using an address. For rejected input, use `result.error` and `result.details?.errorCode`, not string comparisons with implementation-specific messages.
|
|
11
|
+
5. Validation trims outer ASCII spaces but does not return a normalized address. After successful validation of a string, use `input.trim()` for subsequent operations. Do not lowercase Base58 addresses or silently repair checksums, Unicode, missing characters, or invalid input.
|
|
12
|
+
6. `strict: true` requires exact EIP-55 capitalization for EVM addresses, and canonical lowercase AIP-40 representation for Aptos. Mixed-case EVM checksums and mandatory Base58Check/Bech32 checksums are always checked. Choose strict mode deliberately; do not enable it blindly for existing lowercase EVM input flows.
|
|
13
|
+
7. `network: 'mainnet' | 'testnet' | 'auto'` and `allowTestnet` only filter networks encoded in an address. EVM, Solana, TRON, Cosmos, Sui, Aptos, and NEAR cannot establish mainnet/testnet from address syntax. An absent `details.network` means unknown, not mainnet.
|
|
14
|
+
8. `detectBlockchain` returns a chain, an array of format candidates, or `null`. Never pick the first candidate automatically for a payment. EVM chains, Sui/Aptos, and some UTXO/NEAR formats overlap. Plain NEAR names are deliberately excluded from detection.
|
|
15
|
+
9. Validation does not prove ownership, account existence, spendability, token compatibility, or recipient safety. Handle application-specific destination tags, memos, RPC checks, and asset/network selection separately. `allowBurnAddress` only controls the EVM all-zero address.
|
|
16
|
+
10. Formatting is display logic. `shortenAddress` output must never become a transaction destination. `formatAddress` does not generally validate input; EVM checksum formatting and Aptos formatting can throw on malformed input. Validate first.
|
|
17
|
+
11. Keep client bundle size small: reuse a family validator instance when only one chain is needed. Do not add an entire chain SDK just to wrap these checks.
|
|
18
|
+
12. Add consumer tests for valid input, invalid checksum/encoding, unsupported chain input, network policy, and ambiguous detection as relevant to the integration. Use the published protocol fixtures or verified addresses rather than invented strings.
|
|
19
|
+
|
|
20
|
+
### Safe dynamic-input example
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { isSupportedChain, validateAddress } from 'validator0x';
|
|
24
|
+
|
|
25
|
+
export function checkRecipient(input: unknown, chain: unknown) {
|
|
26
|
+
if (!isSupportedChain(chain)) {
|
|
27
|
+
return { ok: false as const, error: 'Choose a supported chain' };
|
|
28
|
+
}
|
|
29
|
+
const result = validateAddress(input, chain, { network: 'mainnet' });
|
|
30
|
+
if (!result.valid || typeof input !== 'string') {
|
|
31
|
+
return { ok: false as const, error: result.error ?? 'Invalid address' };
|
|
32
|
+
}
|
|
33
|
+
// The application must separately verify the selected asset and actual network.
|
|
34
|
+
return { ok: true as const, address: input.trim(), chain };
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Small single-family example
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { BitcoinValidator } from 'validator0x/bitcoin';
|
|
42
|
+
|
|
43
|
+
const bitcoin = new BitcoinValidator();
|
|
44
|
+
const result = bitcoin.validate('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', {
|
|
45
|
+
network: 'mainnet',
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Discovering this file after installation
|
|
50
|
+
|
|
51
|
+
The npm archive includes this file. Resolve it using `require.resolve('validator0x/AGENTS.md')` or read `node_modules/validator0x/AGENTS.md`. Merely installing a package does not guarantee that an agent automatically reads its instructions; explicitly point the agent to this file.
|
|
52
|
+
|
|
53
|
+
## Contributing to this repository
|
|
54
|
+
|
|
55
|
+
- Use Node.js 22.13+ or a supported newer LTS for development. Install the locked development dependencies with `npm ci`.
|
|
56
|
+
- Preserve zero runtime dependencies, synchronous offline behavior, and existing public exports. Never add telemetry or address logging. Cryptographic/encoding code lives in `src/utils/`; family validators in `src/validators/`; the shared registry in `src/registry.ts`; public types in `src/types.ts`.
|
|
57
|
+
- Route validation through `AddressValidator.validate` so sanitization, options, and network policy are consistent. Preserve the 256-character bound before decoding. Check decoded version bytes, payload sizes, checksum variants, and padding rather than relying on a prefix or regex alone.
|
|
58
|
+
- When adding chain support, verify current primary specifications. Update types, the registry, detection behavior, README scope, and positive/negative tests. Do not claim support for a chain's distinct address formats just because one format matches EVM.
|
|
59
|
+
- When adding public entrypoints, update `package.json` exports/typesVersions, `rollup.config.mjs`, and `scripts/declarations.mjs` together. Declaration re-exports must preserve type identity across root and family imports. Keep the package-content allowlist and consumer checks in `scripts/test-package.mjs` aligned.
|
|
60
|
+
- Keep `@noble/hashes` and `@scure/base` development-only. They are independent test oracles, not runtime imports. Test changes to hash/encoding code against independent implementations and official vectors.
|
|
61
|
+
- Required release checks: `npm run check`, `npm run test:coverage`, and `npm audit`. `npm run check` includes lint, source/test type checking, unit tests, build, differential tests, and installation of the actual tarball into fresh consumers. Coverage includes crypto and public APIs; do not weaken coverage or size thresholds just to make a change pass.
|
|
62
|
+
- Tests generate `.coverage/`; builds generate `dist/`; release archives end in `.tgz`. Do not commit generated output, credentials, npm tokens, or unrelated local changes. Legacy tracked `coverage/` files are not current test evidence.
|
|
63
|
+
- Inspect `git status` before staging. Stage the intended source, tests, configuration, and documentation explicitly; preserve unrelated user edits.
|
|
64
|
+
- For release steps, consult `RELEASING.md` in the repository. Prepare and verify the archive before asking for approval. Publishing, moving npm dist-tags, or unpublishing requires the maintainer's explicit authorization. A request to prepare a release is not permission to publish. The maintainer completes npm login and any MFA flow directly; never ask them to paste passwords, tokens, or one-time codes into the chat.
|
|
65
|
+
- Report what was tested, package sizes, limitations, and whether changes were committed, pushed, or published. Never claim the package is bug-free or formally audited.
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.3.0 - Unreleased
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- TRON, Cosmos Hub, Osmosis, Celestia, Injective, Sei (Bech32), Sui, Aptos, and NEAR validators.
|
|
8
|
+
- Generic EVM support plus Fantom, Sonic, Gnosis, Celo, Cronos, Moonbeam, Moonriver, zkSync, Linea, Scroll, Mantle, Blast, opBNB, Zora, and World Chain identifiers.
|
|
9
|
+
- Mainnet/testnet checks for encoded UTXO and Cardano networks; readonly batch input; safe `unknown` address input; `SUPPORTED_CHAINS` and `isSupportedChain`.
|
|
10
|
+
- Native ESM/CommonJS exports with declarations for each module system; small family-specific entrypoints; browser UMD distribution; package size budgets.
|
|
11
|
+
- Protocol vector, independent codec/crypto, packed consumer, and cross-platform CI checks.
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
|
|
15
|
+
- Validation options now reach every validator. EVM strict mode requires the exact checksum.
|
|
16
|
+
- Bitcoin/Litecoin witness programs are converted from five-bit words before checking length; padding, encoding/version combinations, and future witness lengths are validated.
|
|
17
|
+
- Base58 address versions are verified, SHA-256 works at block-padding boundaries, and all-zero Base58 encoding round-trips correctly.
|
|
18
|
+
- Cardano requires correct headers, payload sizes, prefixes, network tags, and canonical padding; Byron requires a complete CBOR envelope and CRC32 rather than a plausible Base58 length.
|
|
19
|
+
- EVM/Solana and other shared-format addresses no longer imply mainnet. Detector results preserve format ambiguity and accept untrusted input safely.
|
|
20
|
+
- Unsupported chain names cannot access Object.prototype members. Input limits apply before trimming; control and invisible characters are rejected.
|
|
21
|
+
- Normalization and checksum helpers cover every EVM chain. Shortening handles non-EVM prefixes and rejects invalid lengths. The error enum is available at runtime.
|
|
22
|
+
|
|
23
|
+
### Migration from 0.2.x
|
|
24
|
+
|
|
25
|
+
- Malformed addresses previously accepted by incomplete checks now fail. Replace fabricated test fixtures with published valid vectors.
|
|
26
|
+
- `strict: true` now rejects noncanonical uniform-case EVM input. Use checksum formatting on validated hex if canonical presentation is needed.
|
|
27
|
+
- Default network selection is `auto`; testnet UTXO addresses are now recognized. Set `network: 'mainnet'` or `allowTestnet: false` to reject identifiable testnet addresses.
|
|
28
|
+
- `details.network` is absent when the address format cannot encode it. Detection may return more candidates; do not depend on a fixed EVM array length or infer chain ownership.
|
|
29
|
+
- EVM zero-address rejection remains the default; `allowBurnAddress: true` opts in to syntactic validation.
|
|
30
|
+
- Public package imports are described by `exports`. Replace imports into private `dist/` paths with the root or documented family entrypoints.
|
|
31
|
+
- Supported Node.js baseline is 22; browsers/workers need ES2020, BigInt, and TextEncoder. Development tools require Node 22.13+ (or 24+).
|
|
32
|
+
- Tests now run with Vitest. Coverage is generated in `.coverage/` and includes crypto and the public API. Generated legacy coverage is not used as evidence of current coverage.
|
|
33
|
+
|
|
34
|
+
This version is prepared locally; publication is a separate release action.
|
package/README.md
CHANGED
|
@@ -1,124 +1,216 @@
|
|
|
1
1
|
# validator0x
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Small, synchronous wallet address validation for **38 chain identifiers**, with **zero runtime dependencies**. Works offline in Node.js 22+, modern browsers, and workers supporting ES2020, BigInt, and TextEncoder. Includes native ESM, CommonJS, browser UMD, and TypeScript declarations.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Validation checks address encoding, payload structure, and checksums where the format has one. It does **not** establish ownership, account existence, balance, network connectivity, or whether an address can safely receive a particular asset.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
- **Batch Validation**: Validate multiple addresses in a single call for better performance.
|
|
9
|
-
- **Zero Dependencies**: Lightweight and secure. All cryptographic primitives (Keccak-256, Base58, Bech32) are implemented internally.
|
|
10
|
-
- **Security Focused**:
|
|
11
|
-
- Input sanitization against control characters and length attacks.
|
|
12
|
-
- Burn address detection (e.g., `0x00...00` or System Programs).
|
|
13
|
-
- **Comprehensive Formatting**: Checksumming, lowercasing, and safe shortening.
|
|
14
|
-
- **TypeScript First**: Full type definitions included.
|
|
7
|
+
## Install and use
|
|
15
8
|
|
|
16
|
-
|
|
9
|
+
This branch documents the upcoming **0.3.0** release. Until it is published, the registry's `latest` version may differ. For a local preview, install the prepared `validator0x-0.3.0.tgz` archive. See [migration notes](./CHANGELOG.md) before upgrading from 0.2.x.
|
|
17
10
|
|
|
18
|
-
```
|
|
11
|
+
```sh
|
|
19
12
|
npm install validator0x
|
|
20
13
|
```
|
|
21
14
|
|
|
22
|
-
|
|
15
|
+
```ts
|
|
16
|
+
import { validateAddress, validateBatch, ValidationError } from 'validator0x';
|
|
23
17
|
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
const result = validateAddress(
|
|
19
|
+
'0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
|
|
20
|
+
'base',
|
|
21
|
+
{ strict: true },
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
if (!result.valid) {
|
|
25
|
+
console.log(result.error, result.details?.errorCode);
|
|
26
|
+
// e.g. ValidationError.INVALID_CHECKSUM
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const results = validateBatch(
|
|
30
|
+
['TNPeeaaFB7K9cmo4uQpcU32zGK8G1NYqeL', 'invalid'],
|
|
31
|
+
'tron',
|
|
32
|
+
);
|
|
33
|
+
console.log(results.map(result => result.valid)); // [true, false]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
CommonJS works too:
|
|
26
37
|
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
38
|
+
```js
|
|
39
|
+
const { validateAddress } = require('validator0x');
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Integrate into an application
|
|
43
|
+
|
|
44
|
+
Keep chain selection explicit. For an API request or form whose values are not already typed, validate the chain before validating the address:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { isSupportedChain, validateAddress } from 'validator0x';
|
|
48
|
+
|
|
49
|
+
export function checkRecipient(address: unknown, chain: unknown) {
|
|
50
|
+
if (!isSupportedChain(chain)) {
|
|
51
|
+
return { ok: false as const, error: 'Choose a supported chain' };
|
|
52
|
+
}
|
|
53
|
+
const result = validateAddress(address, chain, { network: 'mainnet' });
|
|
54
|
+
if (!result.valid || typeof address !== 'string') {
|
|
55
|
+
return { ok: false as const, error: result.error ?? 'Invalid address' };
|
|
56
|
+
}
|
|
57
|
+
return { ok: true as const, address: address.trim(), chain };
|
|
33
58
|
}
|
|
59
|
+
```
|
|
34
60
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
61
|
+
The validator trims outer ASCII spaces internally; use the trimmed string after successful validation. Keep the original capitalization for case-sensitive addresses. Apply asset/network selection and any on-chain recipient checks separately. The package can be used directly in browser forms, server handlers, and workers without initialization or a wallet SDK.
|
|
62
|
+
|
|
63
|
+
| Public API | Result and behavior |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| `validateAddress(input, chain, options?)` | `ValidationResult`; invalid addresses return `valid: false` |
|
|
66
|
+
| `validateBatch(inputs, chain, options?)` | Results in input order; accepts a readonly array |
|
|
67
|
+
| `isSupportedChain(value)` | Boolean and TypeScript type guard for unknown chain input |
|
|
68
|
+
| `SUPPORTED_CHAINS` | Frozen list of supported identifiers; useful for a chain selector |
|
|
69
|
+
| `detectBlockchain(input, options?)` | One chain, candidate array, or `null`; never proof of chain ownership |
|
|
70
|
+
| `formatAddress(address, chain, options?)` | Display string; validation is a separate step |
|
|
71
|
+
| `ValidationError` | Runtime enum for `result.details?.errorCode` |
|
|
72
|
+
|
|
73
|
+
### AI-assisted integration
|
|
74
|
+
|
|
75
|
+
Point your coding agent to [AGENTS.md](./AGENTS.md). It contains integration examples, API rules, chain ambiguity and network limitations, bundle guidance, and repository contribution instructions. It ships inside the npm package and is discoverable after installation:
|
|
76
|
+
|
|
77
|
+
```sh
|
|
78
|
+
node -p "require.resolve('validator0x/AGENTS.md')"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Ask the agent to read that file before implementing an integration. Instructions inside installed packages are not automatically loaded by every AI tool.
|
|
82
|
+
|
|
83
|
+
## Keep your bundle small
|
|
84
|
+
|
|
85
|
+
Import a validator directly when your project needs only one family:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { BitcoinValidator } from 'validator0x/bitcoin';
|
|
89
|
+
|
|
90
|
+
const bitcoin = new BitcoinValidator();
|
|
91
|
+
bitcoin.validate('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', {
|
|
92
|
+
network: 'mainnet',
|
|
39
93
|
});
|
|
40
|
-
|
|
94
|
+
```
|
|
41
95
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
96
|
+
All direct imports support ESM, CommonJS, and TypeScript:
|
|
97
|
+
|
|
98
|
+
| Import | Exports |
|
|
99
|
+
| --- | --- |
|
|
100
|
+
| `validator0x/ethereum` | `EthereumValidator` |
|
|
101
|
+
| `validator0x/evm` | `EvmValidator`, existing BSC/Avalanche/Arbitrum/Optimism/Base classes |
|
|
102
|
+
| `validator0x/bitcoin` | `BitcoinValidator` |
|
|
103
|
+
| `validator0x/litecoin` | `LitecoinValidator` |
|
|
104
|
+
| `validator0x/dogecoin` | `DogecoinValidator` |
|
|
105
|
+
| `validator0x/solana` | `SolanaValidator` |
|
|
106
|
+
| `validator0x/ripple` | `RippleValidator` |
|
|
107
|
+
| `validator0x/cardano` | `CardanoValidator` |
|
|
108
|
+
| `validator0x/tron` | `TronValidator` |
|
|
109
|
+
| `validator0x/cosmos` | `CosmosValidator` |
|
|
110
|
+
| `validator0x/move` | `SuiValidator`, `AptosValidator` |
|
|
111
|
+
| `validator0x/near` | `NearValidator` |
|
|
112
|
+
| `validator0x/formatters` | `shortenAddress`, `normalizeAddress`, `addChecksum` |
|
|
113
|
+
|
|
114
|
+
For example, `new CosmosValidator('osmosis')` or `new EvmValidator('sonic')`. Existing validator classes, including `PolygonValidator`, remain available from the root import.
|
|
115
|
+
|
|
116
|
+
The full minified ESM bundle is approximately **6.8 kB gzip**, and the Solana-only entry approximately **1.2 kB gzip**. `npm run test:package` prints exact sizes and enforces budgets of 12 kB and 3 kB respectively. Runtime dependencies, RPC requests, polyfills, and chain SDKs: **none**. Root imports include the chain registry; direct imports exclude unrelated validators.
|
|
117
|
+
|
|
118
|
+
## Supported address formats
|
|
119
|
+
|
|
120
|
+
| Chain identifier | Scope |
|
|
121
|
+
| --- | --- |
|
|
122
|
+
| `ethereum`, `polygon`, `bsc`, `avalanche`, `arbitrum`, `optimism`, `base` | Ethereum 20-byte hexadecimal addresses; EIP-55 checksum |
|
|
123
|
+
| `evm`, `fantom`, `sonic`, `gnosis`, `celo`, `cronos`, `moonbeam`, `moonriver`, `zksync`, `linea`, `scroll`, `mantle`, `blast`, `opbnb`, `zora`, `worldchain` | Same Ethereum/EIP-55 address rules |
|
|
124
|
+
| `bitcoin` | Base58Check P2PKH/P2SH; SegWit v0; Bech32m v1-16, including Taproot; mainnet and testnet |
|
|
125
|
+
| `litecoin` | Base58Check P2PKH/P2SH (including legacy shared P2SH prefixes); standard witness v0-16; mainnet and testnet |
|
|
126
|
+
| `dogecoin` | Base58Check P2PKH/P2SH; mainnet and testnet |
|
|
127
|
+
| `solana` | Base58 decoding to exactly 32 bytes, including program addresses and PDAs |
|
|
128
|
+
| `cardano` | Shelley base, enterprise, pointer, and stake addresses; header/network/length/padding checks; Byron CBOR envelope and CRC32 |
|
|
129
|
+
| `ripple` | XRP classic account addresses with the XRP Base58 alphabet and checksum |
|
|
130
|
+
| `tron` | 34-character Base58Check or 42-character hex beginning with `41` |
|
|
131
|
+
| `cosmos`, `osmosis`, `sei` | Bech32 account prefixes `cosmos`, `osmo`, `sei`; 20- or 32-byte payloads |
|
|
132
|
+
| `celestia`, `injective` | Bech32 account prefixes `celestia`, `inj`; 20-byte payloads |
|
|
133
|
+
| `sui` | `0x` followed by exactly 64 hex digits |
|
|
134
|
+
| `aptos` | Full 32-byte hexadecimal addresses or special addresses `0x0` through `0xf`; strict canonical AIP-40 mode |
|
|
135
|
+
| `near` | 2-64 character account IDs with lowercase letters, numbers, and valid separators; named, implicit, Ethereum-like, deterministic, and universal forms |
|
|
136
|
+
|
|
137
|
+
`avalanche` covers C-Chain addresses. For additional chains using standard Ethereum addresses, use `evm`; chain-specific checksum schemes such as EIP-1191 are outside its scope. For Sei's EVM format, use `evm`; `sei` validates its Bech32 format.
|
|
138
|
+
|
|
139
|
+
Not supported: Bitcoin/Litecoin regtest HRPs, Litecoin MWEB, XRP X-addresses/destination tags, Cardano addresses longer than 256 characters, Cosmos validator/consensus prefixes, ENS or other name-service resolution. A syntactically valid future witness address is not necessarily spendable under today's consensus rules. Cardano pointer addresses are recognized for compatibility; this does not assert they can be newly created on mainnet. Solana validation does not test curve membership.
|
|
140
|
+
|
|
141
|
+
## Options and errors
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
validateAddress(address, chain, {
|
|
145
|
+
strict: true,
|
|
146
|
+
network: 'mainnet', // 'mainnet' | 'testnet' | 'auto'
|
|
147
|
+
allowTestnet: false,
|
|
148
|
+
allowBurnAddress: false,
|
|
149
|
+
});
|
|
150
|
+
```
|
|
45
151
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
152
|
+
- `strict` defaults to `false`. On EVM, `true` requires the exact EIP-55 capitalization, including the rare valid all-lowercase/all-uppercase checksums. Invalid mixed-case EVM checksums are always rejected. On Aptos, strict mode requires lowercase AIP-40 canonical representation. Other formats always verify their mandatory checksums.
|
|
153
|
+
- `network` defaults to `auto`. It filters networks **only when encoded in the address**, currently Bitcoin, Litecoin, Dogecoin, and Cardano. `details.network` is omitted for formats that cannot identify mainnet/testnet. Selecting a network never proves an address belongs to it.
|
|
154
|
+
- `allowTestnet: false` rejects identifiable testnet addresses, including when `network: 'testnet'` is also supplied. It is not an on-chain network check.
|
|
155
|
+
- `allowBurnAddress` defaults to `false` and controls only the EVM all-zero address. It is not a comprehensive burn-address database. Other reserved/program addresses may be syntactically valid.
|
|
156
|
+
|
|
157
|
+
`validateAddress` accepts `unknown` input and returns `{ valid: false, error, details: { errorCode } }` for malformed addresses. Outer ASCII spaces are trimmed. Inputs longer than 256 characters, control characters, Unicode, and invisible characters are rejected before decoding. `validateBatch` accepts a readonly array and preserves order. Unsupported chain identifiers throw a descriptive error; use `isSupportedChain(value)` for dynamic user input. `SUPPORTED_CHAINS` is a frozen list.
|
|
158
|
+
|
|
159
|
+
## Detection
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
import { detectBlockchain } from 'validator0x';
|
|
163
|
+
|
|
164
|
+
// 'bitcoin'
|
|
165
|
+
detectBlockchain('bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq');
|
|
166
|
+
|
|
167
|
+
// An array of EVM candidates, since the format is shared.
|
|
168
|
+
detectBlockchain('0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045');
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Returns one chain, an array of candidates, or `null`. The optional second argument accepts validation options. Ambiguity also exists between Sui/Aptos, shared UTXO prefixes, and lowercase EVM/NEAR addresses. Detection never chooses the intended network for a transfer. Plain NEAR names such as `root` are omitted from auto-detection to avoid matching arbitrary text; explicit NEAR validation accepts them.
|
|
172
|
+
|
|
173
|
+
## Formatting
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
import { formatAddress, shortenAddress, normalizeAddress } from 'validator0x';
|
|
177
|
+
|
|
178
|
+
formatAddress('0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 'ethereum', {
|
|
179
|
+
checksum: true,
|
|
180
|
+
shorten: true,
|
|
181
|
+
}); // '0xd8dA...6045'
|
|
182
|
+
|
|
183
|
+
shortenAddress('HN7cABqLq46Es1jh92dQQy4aWDtG751BHhP1K2HWT7k1'); // 'HN7c...T7k1'
|
|
184
|
+
normalizeAddress('0xABCD', 'base'); // '0xabcd'
|
|
52
185
|
```
|
|
53
186
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
- **Ripple (XRP)**: Starts with `r`.
|
|
66
|
-
|
|
67
|
-
## Development
|
|
68
|
-
|
|
69
|
-
```bash
|
|
70
|
-
npm install
|
|
71
|
-
npm test
|
|
72
|
-
npm run build
|
|
187
|
+
Formatting is for display, not validation. Never send to a shortened address. `shortenLength` must be a positive integer; short strings are left intact. The `0x` prefix does not count against the leading character limit. EVM checksum formatting throws on malformed hex instead of returning misleading output. Aptos formatting returns canonical AIP-40 representation. `normalizeAddress` lowercases EVM/Sui/Aptos and preserves case-sensitive encodings.
|
|
188
|
+
|
|
189
|
+
## Development and verification
|
|
190
|
+
|
|
191
|
+
```sh
|
|
192
|
+
npm ci
|
|
193
|
+
npm run check # lint, type checks, tests, build, differential and packed-consumer checks
|
|
194
|
+
npm run test:coverage # includes crypto and public API; writes .coverage/
|
|
195
|
+
npm run benchmark # measures the existing production build
|
|
196
|
+
npm audit
|
|
197
|
+
npm pack # rebuilds and produces a local release archive
|
|
73
198
|
```
|
|
74
199
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
We welcome contributions! Please follow these steps to set up your development environment:
|
|
89
|
-
|
|
90
|
-
1. **Clone the repository**:
|
|
91
|
-
```bash
|
|
92
|
-
git clone https://github.com/Samarth208P/validator0x.git
|
|
93
|
-
cd validator0x
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
2. **Install dependencies**:
|
|
97
|
-
```bash
|
|
98
|
-
npm install
|
|
99
|
-
# Note: We use dev-dependencies for testing and building.
|
|
100
|
-
# The core package is zero-dependency.
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
3. **Run Tests**:
|
|
104
|
-
```bash
|
|
105
|
-
npm test
|
|
106
|
-
# Run specific test file
|
|
107
|
-
npx jest tests/ethereum.test.ts
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
4. **Lint & Format**:
|
|
111
|
-
```bash
|
|
112
|
-
npm run lint
|
|
113
|
-
npm run format
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
5. **Build**:
|
|
117
|
-
```bash
|
|
118
|
-
npm run build
|
|
119
|
-
# Output will be in /dist
|
|
120
|
-
```
|
|
200
|
+
For maintainers, `npm run prepare:release` runs the release checks and creates the verified archive. See [RELEASING.md on GitHub](https://github.com/Samarth208P/validator0x/blob/main/RELEASING.md) for the dry run, maintainer login, approval, and publication steps. Pushing commits does not publish to npm.
|
|
201
|
+
|
|
202
|
+
Tests include the complete native witness vectors from [BIP-350](https://bips.dev/350/), Shelley/stake vectors from [CIP-19](https://cips.cardano.org/cip/CIP-19), and EIP-55 checksum cases. Differential checks compare SHA-256 to Node crypto, Keccak-256 to `@noble/hashes`, and Base58/Bech32 to `@scure/base` across block boundaries, address versions, payload lengths, and malformed cases. Reference libraries are development-only dependencies.
|
|
203
|
+
|
|
204
|
+
Package checks install the actual tarball into a temporary consumer and verify all ESM/CommonJS entrypoints, TypeScript NodeNext/Bundler/legacy resolution, and UMD execution without Node globals. CI runs on Linux and Windows with Node 22, 24, and 26. Local checks do not substitute for executing that CI matrix.
|
|
205
|
+
|
|
206
|
+
The development-only `minimatch` override selects the patched 11.x release for the lint dependency tree; remove it once upstream ranges include the fix. Runtime consumers do not install it.
|
|
207
|
+
|
|
208
|
+
See [CHANGELOG.md](./CHANGELOG.md) for migration notes. These checks improve reliability but are not a formal audit or a guarantee of error-free behavior.
|
|
209
|
+
|
|
210
|
+
## Specification references
|
|
211
|
+
|
|
212
|
+
Address rules reviewed on **2026-11-13** against [EIP-55](https://eips.ethereum.org/EIPS/eip-55), [TRON accounts](https://developers.tron.network/docs/account), [Sui address requirements](https://www.docs.sui.io/develop/testing-debugging/common-errors), [Aptos AIP-40 SDK documentation](https://aptos-labs.github.io/aptos-rust-sdk/aptos_sdk/types/struct.AccountAddress.html), [NEAR account IDs](https://docs.near.org/protocol/accounts-contracts/account-id), [Cosmos ADR-028](https://github.com/cosmos/cosmos-sdk/blob/main/docs/architecture/adr-028-public-key-addresses.md), and the [Cosmos chain registry](https://github.com/cosmos/chain-registry).
|
|
121
213
|
|
|
122
214
|
## License
|
|
123
215
|
|
|
124
|
-
MIT
|
|
216
|
+
MIT. The CIP-19 fixture source is credited under CC-BY-4.0; see the [CIP-19 source and license](https://cips.cardano.org/cip/CIP-19).
|
package/dist/bitcoin.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t;!function(t){t.INVALID_FORMAT="INVALID_FORMAT",t.INVALID_CHECKSUM="INVALID_CHECKSUM",t.INVALID_LENGTH="INVALID_LENGTH",t.INVALID_CHARACTERS="INVALID_CHARACTERS",t.UNSUPPORTED_CHAIN="UNSUPPORTED_CHAIN",t.BURN_ADDRESS="BURN_ADDRESS",t.UNSAFE_INPUT="UNSAFE_INPUT",t.NETWORK_MISMATCH="NETWORK_MISMATCH"}(t||(t={}));class n{static sanitize(n){if(null==n)throw new Error(t.UNSAFE_INPUT);if("string"!=typeof n)throw new Error(t.UNSAFE_INPUT);if(n.length>256)throw new Error(t.UNSAFE_INPUT);if(/[^\x20-\x7E]/.test(n))throw new Error(t.UNSAFE_INPUT);return n.trim()}}class e{validate(e,r){try{const i=n.sanitize(e),o=this.validateImplementation(i,r),s=o.details?.network;return o.valid&&s&&(r?.network&&"auto"!==r.network&&r.network!==s||!1===r?.allowTestnet&&"testnet"===s)?{valid:!1,error:"Address network does not match the requested network",details:{...o.details,errorCode:t.NETWORK_MISMATCH}}:o}catch(n){const e=n instanceof Error?n.message:String(n);return e===t.UNSAFE_INPUT?{valid:!1,error:"Unsafe input detected",details:{errorCode:t.UNSAFE_INPUT}}:{valid:!1,error:e,details:{errorCode:t.INVALID_FORMAT}}}}format(t,e){return function(t,e,r=4){const i=n.sanitize(t);if(!e?.shorten)return i;const o=e.shortenLength??r;if(!Number.isSafeInteger(o)||o<1)throw new RangeError("shortenLength must be a positive integer");const s=i.startsWith("0x")?2:0;return i.length<=2*o+s+3?i:`${i.slice(0,o+s)}...${i.slice(-o)}`}(t,e)}}const r="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",i=r.charAt(0),o={};for(let t=0;t<58;t++)o[r.charAt(t)]=t;function s(t){const n=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],e=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],r=8*t.length,i=t.length,o=new Uint8Array(64-(i+8)%64+8);o[0]=128;const s=r,a=Math.floor(s/4294967296),c=s%4294967296,h=o.length;o[h-4]=c>>>24&255,o[h-3]=c>>>16&255,o[h-2]=c>>>8&255,o[h-1]=255&c,o[h-8]=a>>>24&255,o[h-7]=a>>>16&255,o[h-6]=a>>>8&255,o[h-5]=255&a;const u=new Uint8Array(i+o.length);u.set(t),u.set(o,i);const f=new Uint32Array(64);for(let t=0;t<u.length;t+=64){for(let n=0;n<16;n++)f[n]=u[t+4*n]<<24|u[t+4*n+1]<<16|u[t+4*n+2]<<8|u[t+4*n+3];for(let t=16;t<64;t++){const n=l(f[t-2],17)^l(f[t-2],19)^f[t-2]>>>10,e=l(f[t-15],7)^l(f[t-15],18)^f[t-15]>>>3;f[t]=n+f[t-7]+e+f[t-16]|0}let r=e[0],i=e[1],o=e[2],s=e[3],a=e[4],c=e[5],h=e[6],d=e[7];for(let t=0;t<64;t++){const e=d+(l(a,6)^l(a,11)^l(a,25))+(a&c^~a&h)+n[t]+f[t]|0,u=r&i^r&o^i&o;d=h,h=c,c=a,a=s+e|0,s=o,o=i,i=r,r=e+((l(r,2)^l(r,13)^l(r,22))+u|0)|0}e[0]=e[0]+r|0,e[1]=e[1]+i|0,e[2]=e[2]+o|0,e[3]=e[3]+s|0,e[4]=e[4]+a|0,e[5]=e[5]+c|0,e[6]=e[6]+h|0,e[7]=e[7]+d|0}const d=new Uint8Array(32);for(let t=0;t<8;t++)d[4*t]=e[t]>>>24&255,d[4*t+1]=e[t]>>>16&255,d[4*t+2]=e[t]>>>8&255,d[4*t+3]=255&e[t];return d}function l(t,n){return t>>>n|t<<32-n}function a(t){try{const n=function(t){if(0===t.length)return new Uint8Array(0);let n=0;for(;n<t.length&&t[n]===i;)n++;const e=n,r=[0];for(;n<t.length;n++){const e=t[n],i=o[e];if(void 0===i)throw new Error(`Invalid Base58 character: ${e}`);for(let t=0;t<r.length;t++)r[t]*=58;r[0]+=i;let s=0;for(let t=0;t<r.length;++t)r[t]+=s,s=r[t]>>8,r[t]&=255;for(;s;)r.push(255&s),s>>=8}const s=t.length===e?[]:r.reverse();return Uint8Array.from([...new Array(e).fill(0),...s])}(t);if(n.length<5)return null;const e=n.slice(0,-4),r=s(s(e));return n.slice(-4).every((t,n)=>t===r[n])?e:null}catch{return null}}var c;!function(t){t.BECH32="bech32",t.BECH32M="bech32m"}(c||(c={}));const h=[996825010,642813549,513874426,1027748829,705979059];function u(t,n,e){const r=e===c.BECH32M?734539939:1;return function(t){let n=1;for(const e of t){const t=n>>25;n=(33554431&n)<<5^e;for(let e=0;e<5;e++)t>>e&1&&(n^=h[e])}return n}(function(t){const n=[];for(let e=0;e<t.length;e++)n.push(t.charCodeAt(e)>>5);n.push(0);for(let e=0;e<t.length;e++)n.push(31&t.charCodeAt(e));return n}(t).concat(n))===r}class f extends e{constructor(t){super(),this.config=t}validateImplementation(n){const e=n.toLowerCase().split("1")[0];if(this.config.hrps&&Object.prototype.hasOwnProperty.call(this.config.hrps,e))return this.validateWitness(n,this.config.hrps[e]);if(n.length<26||n.length>35)return this.invalid("Invalid address length",t.INVALID_LENGTH);const r=a(n);if(!r)return this.invalid("Invalid Base58Check address",t.INVALID_CHECKSUM);if(21!==r.length)return this.invalid("Invalid payload length",t.INVALID_LENGTH);const i=this.config.versions[r[0]];return i?{valid:!0,blockchain:this.config.chain,details:{format:"legacy",network:i,checksum:!0}}:this.invalid("Invalid address version")}validateWitness(n,e){const r=function(t,n=90){if(t.length<8||t.length>n)return null;const e=t.toLowerCase();if(t!==e&&t!==t.toUpperCase())return null;const r=e.lastIndexOf("1");if(r<1||r+7>e.length)return null;const i=e.substring(0,r);if(/[^\x21-\x7E]/.test(i))return null;const o=[];for(let t=r+1;t<e.length;t++){const n="qpzry9x8gf2tvdw0s3jn54khce6mua7l".indexOf(e[t]);if(-1===n)return null;o.push(n)}return u(i,o,c.BECH32)?{hrp:i,data:o.slice(0,o.length-6),encoding:c.BECH32}:u(i,o,c.BECH32M)?{hrp:i,data:o.slice(0,o.length-6),encoding:c.BECH32M}:null}(n);if(!r||0===r.data.length)return this.invalid("Invalid Bech32 encoding");const i=r.data[0],o=function(t){let n=0,e=0;const r=[];for(const i of t){if(!Number.isInteger(i)||i<0||i>31)return null;n=65535&(n<<5|i),e+=5,e>=8&&(e-=8,r.push(n>>>e&255))}return e>=5||n<<8-e&255?null:Uint8Array.from(r)}(r.data.slice(1));if(i>16)return this.invalid("Invalid witness version");if(!o||o.length<2||o.length>40||0===i&&20!==o.length&&32!==o.length)return this.invalid("Invalid witness program or padding",t.INVALID_LENGTH);const s=0===i?c.BECH32:c.BECH32M;if(r.encoding!==s)return this.invalid("Wrong checksum encoding for witness version",t.INVALID_CHECKSUM);const l=0===i?"segwit":1===i&&32===o.length?"taproot":"future";return{valid:!0,blockchain:this.config.chain,details:{format:l,network:e,checksum:!0}}}invalid(n,e=t.INVALID_FORMAT){return{valid:!1,error:n,details:{errorCode:e}}}}exports.BitcoinValidator=class extends f{constructor(){super({chain:"bitcoin",versions:{0:"mainnet",5:"mainnet",111:"testnet",196:"testnet"},hrps:{bc:"mainnet",tb:"testnet"}})}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BitcoinValidator } from './index.cjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BitcoinValidator } from './index.mjs';
|
package/dist/bitcoin.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var t;!function(t){t.INVALID_FORMAT="INVALID_FORMAT",t.INVALID_CHECKSUM="INVALID_CHECKSUM",t.INVALID_LENGTH="INVALID_LENGTH",t.INVALID_CHARACTERS="INVALID_CHARACTERS",t.UNSUPPORTED_CHAIN="UNSUPPORTED_CHAIN",t.BURN_ADDRESS="BURN_ADDRESS",t.UNSAFE_INPUT="UNSAFE_INPUT",t.NETWORK_MISMATCH="NETWORK_MISMATCH"}(t||(t={}));class n{static sanitize(n){if(null==n)throw new Error(t.UNSAFE_INPUT);if("string"!=typeof n)throw new Error(t.UNSAFE_INPUT);if(n.length>256)throw new Error(t.UNSAFE_INPUT);if(/[^\x20-\x7E]/.test(n))throw new Error(t.UNSAFE_INPUT);return n.trim()}}class e{validate(e,r){try{const i=n.sanitize(e),o=this.validateImplementation(i,r),s=o.details?.network;return o.valid&&s&&(r?.network&&"auto"!==r.network&&r.network!==s||!1===r?.allowTestnet&&"testnet"===s)?{valid:!1,error:"Address network does not match the requested network",details:{...o.details,errorCode:t.NETWORK_MISMATCH}}:o}catch(n){const e=n instanceof Error?n.message:String(n);return e===t.UNSAFE_INPUT?{valid:!1,error:"Unsafe input detected",details:{errorCode:t.UNSAFE_INPUT}}:{valid:!1,error:e,details:{errorCode:t.INVALID_FORMAT}}}}format(t,e){return function(t,e,r=4){const i=n.sanitize(t);if(!e?.shorten)return i;const o=e.shortenLength??r;if(!Number.isSafeInteger(o)||o<1)throw new RangeError("shortenLength must be a positive integer");const s=i.startsWith("0x")?2:0;return i.length<=2*o+s+3?i:`${i.slice(0,o+s)}...${i.slice(-o)}`}(t,e)}}const r="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",i=r.charAt(0),o={};for(let t=0;t<58;t++)o[r.charAt(t)]=t;function s(t){const n=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],e=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],r=8*t.length,i=t.length,o=new Uint8Array(64-(i+8)%64+8);o[0]=128;const s=r,a=Math.floor(s/4294967296),c=s%4294967296,h=o.length;o[h-4]=c>>>24&255,o[h-3]=c>>>16&255,o[h-2]=c>>>8&255,o[h-1]=255&c,o[h-8]=a>>>24&255,o[h-7]=a>>>16&255,o[h-6]=a>>>8&255,o[h-5]=255&a;const u=new Uint8Array(i+o.length);u.set(t),u.set(o,i);const f=new Uint32Array(64);for(let t=0;t<u.length;t+=64){for(let n=0;n<16;n++)f[n]=u[t+4*n]<<24|u[t+4*n+1]<<16|u[t+4*n+2]<<8|u[t+4*n+3];for(let t=16;t<64;t++){const n=l(f[t-2],17)^l(f[t-2],19)^f[t-2]>>>10,e=l(f[t-15],7)^l(f[t-15],18)^f[t-15]>>>3;f[t]=n+f[t-7]+e+f[t-16]|0}let r=e[0],i=e[1],o=e[2],s=e[3],a=e[4],c=e[5],h=e[6],d=e[7];for(let t=0;t<64;t++){const e=d+(l(a,6)^l(a,11)^l(a,25))+(a&c^~a&h)+n[t]+f[t]|0,u=r&i^r&o^i&o;d=h,h=c,c=a,a=s+e|0,s=o,o=i,i=r,r=e+((l(r,2)^l(r,13)^l(r,22))+u|0)|0}e[0]=e[0]+r|0,e[1]=e[1]+i|0,e[2]=e[2]+o|0,e[3]=e[3]+s|0,e[4]=e[4]+a|0,e[5]=e[5]+c|0,e[6]=e[6]+h|0,e[7]=e[7]+d|0}const d=new Uint8Array(32);for(let t=0;t<8;t++)d[4*t]=e[t]>>>24&255,d[4*t+1]=e[t]>>>16&255,d[4*t+2]=e[t]>>>8&255,d[4*t+3]=255&e[t];return d}function l(t,n){return t>>>n|t<<32-n}function a(t){try{const n=function(t){if(0===t.length)return new Uint8Array(0);let n=0;for(;n<t.length&&t[n]===i;)n++;const e=n,r=[0];for(;n<t.length;n++){const e=t[n],i=o[e];if(void 0===i)throw new Error(`Invalid Base58 character: ${e}`);for(let t=0;t<r.length;t++)r[t]*=58;r[0]+=i;let s=0;for(let t=0;t<r.length;++t)r[t]+=s,s=r[t]>>8,r[t]&=255;for(;s;)r.push(255&s),s>>=8}const s=t.length===e?[]:r.reverse();return Uint8Array.from([...new Array(e).fill(0),...s])}(t);if(n.length<5)return null;const e=n.slice(0,-4),r=s(s(e));return n.slice(-4).every((t,n)=>t===r[n])?e:null}catch{return null}}var c;!function(t){t.BECH32="bech32",t.BECH32M="bech32m"}(c||(c={}));const h=[996825010,642813549,513874426,1027748829,705979059];function u(t,n,e){const r=e===c.BECH32M?734539939:1;return function(t){let n=1;for(const e of t){const t=n>>25;n=(33554431&n)<<5^e;for(let e=0;e<5;e++)t>>e&1&&(n^=h[e])}return n}(function(t){const n=[];for(let e=0;e<t.length;e++)n.push(t.charCodeAt(e)>>5);n.push(0);for(let e=0;e<t.length;e++)n.push(31&t.charCodeAt(e));return n}(t).concat(n))===r}class f extends e{constructor(t){super(),this.config=t}validateImplementation(n){const e=n.toLowerCase().split("1")[0];if(this.config.hrps&&Object.prototype.hasOwnProperty.call(this.config.hrps,e))return this.validateWitness(n,this.config.hrps[e]);if(n.length<26||n.length>35)return this.invalid("Invalid address length",t.INVALID_LENGTH);const r=a(n);if(!r)return this.invalid("Invalid Base58Check address",t.INVALID_CHECKSUM);if(21!==r.length)return this.invalid("Invalid payload length",t.INVALID_LENGTH);const i=this.config.versions[r[0]];return i?{valid:!0,blockchain:this.config.chain,details:{format:"legacy",network:i,checksum:!0}}:this.invalid("Invalid address version")}validateWitness(n,e){const r=function(t,n=90){if(t.length<8||t.length>n)return null;const e=t.toLowerCase();if(t!==e&&t!==t.toUpperCase())return null;const r=e.lastIndexOf("1");if(r<1||r+7>e.length)return null;const i=e.substring(0,r);if(/[^\x21-\x7E]/.test(i))return null;const o=[];for(let t=r+1;t<e.length;t++){const n="qpzry9x8gf2tvdw0s3jn54khce6mua7l".indexOf(e[t]);if(-1===n)return null;o.push(n)}return u(i,o,c.BECH32)?{hrp:i,data:o.slice(0,o.length-6),encoding:c.BECH32}:u(i,o,c.BECH32M)?{hrp:i,data:o.slice(0,o.length-6),encoding:c.BECH32M}:null}(n);if(!r||0===r.data.length)return this.invalid("Invalid Bech32 encoding");const i=r.data[0],o=function(t){let n=0,e=0;const r=[];for(const i of t){if(!Number.isInteger(i)||i<0||i>31)return null;n=65535&(n<<5|i),e+=5,e>=8&&(e-=8,r.push(n>>>e&255))}return e>=5||n<<8-e&255?null:Uint8Array.from(r)}(r.data.slice(1));if(i>16)return this.invalid("Invalid witness version");if(!o||o.length<2||o.length>40||0===i&&20!==o.length&&32!==o.length)return this.invalid("Invalid witness program or padding",t.INVALID_LENGTH);const s=0===i?c.BECH32:c.BECH32M;if(r.encoding!==s)return this.invalid("Wrong checksum encoding for witness version",t.INVALID_CHECKSUM);const l=0===i?"segwit":1===i&&32===o.length?"taproot":"future";return{valid:!0,blockchain:this.config.chain,details:{format:l,network:e,checksum:!0}}}invalid(n,e=t.INVALID_FORMAT){return{valid:!1,error:n,details:{errorCode:e}}}}class d extends f{constructor(){super({chain:"bitcoin",versions:{0:"mainnet",5:"mainnet",111:"testnet",196:"testnet"},hrps:{bc:"mainnet",tb:"testnet"}})}}export{d as BitcoinValidator};
|
package/dist/cardano.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t;!function(t){t.INVALID_FORMAT="INVALID_FORMAT",t.INVALID_CHECKSUM="INVALID_CHECKSUM",t.INVALID_LENGTH="INVALID_LENGTH",t.INVALID_CHARACTERS="INVALID_CHARACTERS",t.UNSUPPORTED_CHAIN="UNSUPPORTED_CHAIN",t.BURN_ADDRESS="BURN_ADDRESS",t.UNSAFE_INPUT="UNSAFE_INPUT",t.NETWORK_MISMATCH="NETWORK_MISMATCH"}(t||(t={}));class e{static sanitize(e){if(null==e)throw new Error(t.UNSAFE_INPUT);if("string"!=typeof e)throw new Error(t.UNSAFE_INPUT);if(e.length>256)throw new Error(t.UNSAFE_INPUT);if(/[^\x20-\x7E]/.test(e))throw new Error(t.UNSAFE_INPUT);return e.trim()}}class n{validate(n,r){try{const o=e.sanitize(n),s=this.validateImplementation(o,r),i=s.details?.network;return s.valid&&i&&(r?.network&&"auto"!==r.network&&r.network!==i||!1===r?.allowTestnet&&"testnet"===i)?{valid:!1,error:"Address network does not match the requested network",details:{...s.details,errorCode:t.NETWORK_MISMATCH}}:s}catch(e){const n=e instanceof Error?e.message:String(e);return n===t.UNSAFE_INPUT?{valid:!1,error:"Unsafe input detected",details:{errorCode:t.UNSAFE_INPUT}}:{valid:!1,error:n,details:{errorCode:t.INVALID_FORMAT}}}}format(t,e){return r(t,e)}}function r(t,n,r=4){const o=e.sanitize(t);if(!n?.shorten)return o;const s=n.shortenLength??r;if(!Number.isSafeInteger(s)||s<1)throw new RangeError("shortenLength must be a positive integer");const i=o.startsWith("0x")?2:0;return o.length<=2*s+i+3?o:`${o.slice(0,s+i)}...${o.slice(-s)}`}const o="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",s=o.charAt(0),i={};for(let t=0;t<58;t++)i[o.charAt(t)]=t;var l;!function(t){t.BECH32="bech32",t.BECH32M="bech32m"}(l||(l={}));const a=[996825010,642813549,513874426,1027748829,705979059];function f(t,e,n){const r=n===l.BECH32M?734539939:1;return function(t){let e=1;for(const n of t){const t=e>>25;e=(33554431&e)<<5^n;for(let n=0;n<5;n++)t>>n&1&&(e^=a[n])}return e}(function(t){const e=[];for(let n=0;n<t.length;n++)e.push(t.charCodeAt(n)>>5);e.push(0);for(let n=0;n<t.length;n++)e.push(31&t.charCodeAt(n));return e}(t).concat(e))===r}class h{constructor(t){this.data=t,this.offset=0}head(t){if(this.offset>=this.data.length)throw new Error("Truncated CBOR");const e=this.data[this.offset++];if(e>>>5!==t)throw new Error("Invalid CBOR type");const n=31&e;if(n<24)return n;if(n>26)throw new Error("Unsupported CBOR length");const r=1<<n-24;if(this.offset+r>this.data.length)throw new Error("Truncated CBOR");let o=0;for(let t=0;t<r;t++)o=256*o+this.data[this.offset++];return o}bytes(){const t=this.head(2);if(this.offset+t>this.data.length)throw new Error("Truncated bytes");const e=this.data.slice(this.offset,this.offset+t);return this.offset+=t,e}end(){if(this.offset!==this.data.length)throw new Error("Trailing CBOR data")}}exports.CardanoValidator=class extends n{validateImplementation(t){if(/^(addr|stake)(_test)?1/i.test(t))return this.validateShelley(t);try{const e=function(t){try{const e=new h(t);if(2!==e.head(4)||24!==e.head(6))return null;const n=e.bytes();if(e.head(0)!==function(t){let e=4294967295;for(const n of t){e^=n;for(let t=0;t<8;t++)e=e>>>1^(1&e?3988292384:0)}return(4294967295^e)>>>0}(n))return null;e.end();const r=new h(n);if(3!==r.head(4)||28!==r.bytes().length)return null;const o=r.head(5);if(o>n.length)return null;const s=new Set;let i="mainnet";for(let t=0;t<o;t++){const t=r.head(0);if(s.has(t)||t>255)return null;s.add(t);const e=r.bytes();if(1===t){const t=new h(e);if(28!==t.bytes().length)return null;t.end()}if(2===t){const t=new h(e);t.head(0),t.end(),i="testnet"}}const l=r.head(0);return 0!==l&&2!==l?null:(r.end(),i)}catch{return null}}(function(t){if(0===t.length)return new Uint8Array(0);let e=0;for(;e<t.length&&t[e]===s;)e++;const n=e,r=[0];for(;e<t.length;e++){const n=t[e],o=i[n];if(void 0===o)throw new Error(`Invalid Base58 character: ${n}`);for(let t=0;t<r.length;t++)r[t]*=58;r[0]+=o;let s=0;for(let t=0;t<r.length;++t)r[t]+=s,s=r[t]>>8,r[t]&=255;for(;s;)r.push(255&s),s>>=8}const o=t.length===n?[]:r.reverse();return Uint8Array.from([...new Array(n).fill(0),...o])}(t));if(e)return{valid:!0,blockchain:"cardano",details:{format:"byron",network:e,checksum:!0}}}catch{}return this.invalid()}validateShelley(t){const e=function(t,e=90){if(t.length<8||t.length>e)return null;const n=t.toLowerCase();if(t!==n&&t!==t.toUpperCase())return null;const r=n.lastIndexOf("1");if(r<1||r+7>n.length)return null;const o=n.substring(0,r);if(/[^\x21-\x7E]/.test(o))return null;const s=[];for(let t=r+1;t<n.length;t++){const e="qpzry9x8gf2tvdw0s3jn54khce6mua7l".indexOf(n[t]);if(-1===e)return null;s.push(e)}return f(o,s,l.BECH32)?{hrp:o,data:s.slice(0,s.length-6),encoding:l.BECH32}:f(o,s,l.BECH32M)?{hrp:o,data:s.slice(0,s.length-6),encoding:l.BECH32M}:null}(t,256),n=e&&function(t){let e=0,n=0;const r=[];for(const o of t){if(!Number.isInteger(o)||o<0||o>31)return null;e=65535&(e<<5|o),n+=5,n>=8&&(n-=8,r.push(e>>>n&255))}return n>=5||e<<8-n&255?null:Uint8Array.from(r)}(e.data);if(!e||e.encoding!==l.BECH32||!n?.length)return this.invalid();const r=n[0]>>>4,o=15&n[0];if(0!==o&&1!==o)return this.invalid();const s=14===r||15===r,i=(s?"stake":"addr")+(0===o?"_test":"");if(e.hrp!==i)return this.invalid();let a=!1;return r<=3?a=57===n.length:6===r||7===r||s?a=29===n.length:4!==r&&5!==r||(a=this.validPointer(n)),a?{valid:!0,blockchain:"cardano",details:{format:s?"stake":"shelley",network:1===o?"mainnet":"testnet",checksum:!0}}:this.invalid()}validPointer(t){let e=29;for(let n=0;n<3;n++){let n=0n;const r=e;do{if(e>=t.length||e-r>=10)return!1;n=n<<7n|BigInt(127&t[e])}while(128&t[e++]);if(e-r>1&&128===t[r]||n>0xffffffffffffffffn)return!1}return e===t.length}invalid(){return{valid:!1,error:"Invalid Cardano address structure, network, or checksum",details:{errorCode:t.INVALID_FORMAT}}}format(t,e){return r(t,e,8)}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CardanoValidator } from './index.cjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CardanoValidator } from './index.mjs';
|
package/dist/cardano.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var t;!function(t){t.INVALID_FORMAT="INVALID_FORMAT",t.INVALID_CHECKSUM="INVALID_CHECKSUM",t.INVALID_LENGTH="INVALID_LENGTH",t.INVALID_CHARACTERS="INVALID_CHARACTERS",t.UNSUPPORTED_CHAIN="UNSUPPORTED_CHAIN",t.BURN_ADDRESS="BURN_ADDRESS",t.UNSAFE_INPUT="UNSAFE_INPUT",t.NETWORK_MISMATCH="NETWORK_MISMATCH"}(t||(t={}));class e{static sanitize(e){if(null==e)throw new Error(t.UNSAFE_INPUT);if("string"!=typeof e)throw new Error(t.UNSAFE_INPUT);if(e.length>256)throw new Error(t.UNSAFE_INPUT);if(/[^\x20-\x7E]/.test(e))throw new Error(t.UNSAFE_INPUT);return e.trim()}}class n{validate(n,r){try{const o=e.sanitize(n),s=this.validateImplementation(o,r),i=s.details?.network;return s.valid&&i&&(r?.network&&"auto"!==r.network&&r.network!==i||!1===r?.allowTestnet&&"testnet"===i)?{valid:!1,error:"Address network does not match the requested network",details:{...s.details,errorCode:t.NETWORK_MISMATCH}}:s}catch(e){const n=e instanceof Error?e.message:String(e);return n===t.UNSAFE_INPUT?{valid:!1,error:"Unsafe input detected",details:{errorCode:t.UNSAFE_INPUT}}:{valid:!1,error:n,details:{errorCode:t.INVALID_FORMAT}}}}format(t,e){return r(t,e)}}function r(t,n,r=4){const o=e.sanitize(t);if(!n?.shorten)return o;const s=n.shortenLength??r;if(!Number.isSafeInteger(s)||s<1)throw new RangeError("shortenLength must be a positive integer");const i=o.startsWith("0x")?2:0;return o.length<=2*s+i+3?o:`${o.slice(0,s+i)}...${o.slice(-s)}`}const o="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",s=o.charAt(0),i={};for(let t=0;t<58;t++)i[o.charAt(t)]=t;var l;!function(t){t.BECH32="bech32",t.BECH32M="bech32m"}(l||(l={}));const a=[996825010,642813549,513874426,1027748829,705979059];function f(t,e,n){const r=n===l.BECH32M?734539939:1;return function(t){let e=1;for(const n of t){const t=e>>25;e=(33554431&e)<<5^n;for(let n=0;n<5;n++)t>>n&1&&(e^=a[n])}return e}(function(t){const e=[];for(let n=0;n<t.length;n++)e.push(t.charCodeAt(n)>>5);e.push(0);for(let n=0;n<t.length;n++)e.push(31&t.charCodeAt(n));return e}(t).concat(e))===r}class h{constructor(t){this.data=t,this.offset=0}head(t){if(this.offset>=this.data.length)throw new Error("Truncated CBOR");const e=this.data[this.offset++];if(e>>>5!==t)throw new Error("Invalid CBOR type");const n=31&e;if(n<24)return n;if(n>26)throw new Error("Unsupported CBOR length");const r=1<<n-24;if(this.offset+r>this.data.length)throw new Error("Truncated CBOR");let o=0;for(let t=0;t<r;t++)o=256*o+this.data[this.offset++];return o}bytes(){const t=this.head(2);if(this.offset+t>this.data.length)throw new Error("Truncated bytes");const e=this.data.slice(this.offset,this.offset+t);return this.offset+=t,e}end(){if(this.offset!==this.data.length)throw new Error("Trailing CBOR data")}}class c extends n{validateImplementation(t){if(/^(addr|stake)(_test)?1/i.test(t))return this.validateShelley(t);try{const e=function(t){try{const e=new h(t);if(2!==e.head(4)||24!==e.head(6))return null;const n=e.bytes();if(e.head(0)!==function(t){let e=4294967295;for(const n of t){e^=n;for(let t=0;t<8;t++)e=e>>>1^(1&e?3988292384:0)}return(4294967295^e)>>>0}(n))return null;e.end();const r=new h(n);if(3!==r.head(4)||28!==r.bytes().length)return null;const o=r.head(5);if(o>n.length)return null;const s=new Set;let i="mainnet";for(let t=0;t<o;t++){const t=r.head(0);if(s.has(t)||t>255)return null;s.add(t);const e=r.bytes();if(1===t){const t=new h(e);if(28!==t.bytes().length)return null;t.end()}if(2===t){const t=new h(e);t.head(0),t.end(),i="testnet"}}const l=r.head(0);return 0!==l&&2!==l?null:(r.end(),i)}catch{return null}}(function(t){if(0===t.length)return new Uint8Array(0);let e=0;for(;e<t.length&&t[e]===s;)e++;const n=e,r=[0];for(;e<t.length;e++){const n=t[e],o=i[n];if(void 0===o)throw new Error(`Invalid Base58 character: ${n}`);for(let t=0;t<r.length;t++)r[t]*=58;r[0]+=o;let s=0;for(let t=0;t<r.length;++t)r[t]+=s,s=r[t]>>8,r[t]&=255;for(;s;)r.push(255&s),s>>=8}const o=t.length===n?[]:r.reverse();return Uint8Array.from([...new Array(n).fill(0),...o])}(t));if(e)return{valid:!0,blockchain:"cardano",details:{format:"byron",network:e,checksum:!0}}}catch{}return this.invalid()}validateShelley(t){const e=function(t,e=90){if(t.length<8||t.length>e)return null;const n=t.toLowerCase();if(t!==n&&t!==t.toUpperCase())return null;const r=n.lastIndexOf("1");if(r<1||r+7>n.length)return null;const o=n.substring(0,r);if(/[^\x21-\x7E]/.test(o))return null;const s=[];for(let t=r+1;t<n.length;t++){const e="qpzry9x8gf2tvdw0s3jn54khce6mua7l".indexOf(n[t]);if(-1===e)return null;s.push(e)}return f(o,s,l.BECH32)?{hrp:o,data:s.slice(0,s.length-6),encoding:l.BECH32}:f(o,s,l.BECH32M)?{hrp:o,data:s.slice(0,s.length-6),encoding:l.BECH32M}:null}(t,256),n=e&&function(t){let e=0,n=0;const r=[];for(const o of t){if(!Number.isInteger(o)||o<0||o>31)return null;e=65535&(e<<5|o),n+=5,n>=8&&(n-=8,r.push(e>>>n&255))}return n>=5||e<<8-n&255?null:Uint8Array.from(r)}(e.data);if(!e||e.encoding!==l.BECH32||!n?.length)return this.invalid();const r=n[0]>>>4,o=15&n[0];if(0!==o&&1!==o)return this.invalid();const s=14===r||15===r,i=(s?"stake":"addr")+(0===o?"_test":"");if(e.hrp!==i)return this.invalid();let a=!1;return r<=3?a=57===n.length:6===r||7===r||s?a=29===n.length:4!==r&&5!==r||(a=this.validPointer(n)),a?{valid:!0,blockchain:"cardano",details:{format:s?"stake":"shelley",network:1===o?"mainnet":"testnet",checksum:!0}}:this.invalid()}validPointer(t){let e=29;for(let n=0;n<3;n++){let n=0n;const r=e;do{if(e>=t.length||e-r>=10)return!1;n=n<<7n|BigInt(127&t[e])}while(128&t[e++]);if(e-r>1&&128===t[r]||n>0xffffffffffffffffn)return!1}return e===t.length}invalid(){return{valid:!1,error:"Invalid Cardano address structure, network, or checksum",details:{errorCode:t.INVALID_FORMAT}}}format(t,e){return r(t,e,8)}}export{c as CardanoValidator};
|
package/dist/cosmos.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t;!function(t){t.INVALID_FORMAT="INVALID_FORMAT",t.INVALID_CHECKSUM="INVALID_CHECKSUM",t.INVALID_LENGTH="INVALID_LENGTH",t.INVALID_CHARACTERS="INVALID_CHARACTERS",t.UNSUPPORTED_CHAIN="UNSUPPORTED_CHAIN",t.BURN_ADDRESS="BURN_ADDRESS",t.UNSAFE_INPUT="UNSAFE_INPUT",t.NETWORK_MISMATCH="NETWORK_MISMATCH"}(t||(t={}));class e{static sanitize(e){if(null==e)throw new Error(t.UNSAFE_INPUT);if("string"!=typeof e)throw new Error(t.UNSAFE_INPUT);if(e.length>256)throw new Error(t.UNSAFE_INPUT);if(/[^\x20-\x7E]/.test(e))throw new Error(t.UNSAFE_INPUT);return e.trim()}}class n{validate(n,r){try{const o=e.sanitize(n),s=this.validateImplementation(o,r),i=s.details?.network;return s.valid&&i&&(r?.network&&"auto"!==r.network&&r.network!==i||!1===r?.allowTestnet&&"testnet"===i)?{valid:!1,error:"Address network does not match the requested network",details:{...s.details,errorCode:t.NETWORK_MISMATCH}}:s}catch(e){const n=e instanceof Error?e.message:String(e);return n===t.UNSAFE_INPUT?{valid:!1,error:"Unsafe input detected",details:{errorCode:t.UNSAFE_INPUT}}:{valid:!1,error:n,details:{errorCode:t.INVALID_FORMAT}}}}format(t,n){return function(t,n,r=4){const o=e.sanitize(t);if(!n?.shorten)return o;const s=n.shortenLength??r;if(!Number.isSafeInteger(s)||s<1)throw new RangeError("shortenLength must be a positive integer");const i=o.startsWith("0x")?2:0;return o.length<=2*s+i+3?o:`${o.slice(0,s+i)}...${o.slice(-s)}`}(t,n)}}var r;!function(t){t.BECH32="bech32",t.BECH32M="bech32m"}(r||(r={}));const o=[996825010,642813549,513874426,1027748829,705979059];function s(t,e,n){const s=n===r.BECH32M?734539939:1;return function(t){let e=1;for(const n of t){const t=e>>25;e=(33554431&e)<<5^n;for(let n=0;n<5;n++)t>>n&1&&(e^=o[n])}return e}(function(t){const e=[];for(let n=0;n<t.length;n++)e.push(t.charCodeAt(n)>>5);e.push(0);for(let n=0;n<t.length;n++)e.push(31&t.charCodeAt(n));return e}(t).concat(e))===s}const i={cosmos:"cosmos",osmosis:"osmo",celestia:"celestia",injective:"inj",sei:"sei"};exports.CosmosValidator=class extends n{constructor(t="cosmos"){super(),this.chain=t}validateImplementation(e){const n=function(t,e=90){if(t.length<8||t.length>e)return null;const n=t.toLowerCase();if(t!==n&&t!==t.toUpperCase())return null;const o=n.lastIndexOf("1");if(o<1||o+7>n.length)return null;const i=n.substring(0,o);if(/[^\x21-\x7E]/.test(i))return null;const c=[];for(let t=o+1;t<n.length;t++){const e="qpzry9x8gf2tvdw0s3jn54khce6mua7l".indexOf(n[t]);if(-1===e)return null;c.push(e)}return s(i,c,r.BECH32)?{hrp:i,data:c.slice(0,c.length-6),encoding:r.BECH32}:s(i,c,r.BECH32M)?{hrp:i,data:c.slice(0,c.length-6),encoding:r.BECH32M}:null}(e),o=n&&function(t){let e=0,n=0;const r=[];for(const o of t){if(!Number.isInteger(o)||o<0||o>31)return null;e=65535&(e<<5|o),n+=5,n>=8&&(n-=8,r.push(e>>>n&255))}return n>=5||e<<8-n&255?null:Uint8Array.from(r)}(n.data),c="injective"===this.chain||"celestia"===this.chain?[20]:[20,32];return n&&n.hrp===i[this.chain]&&n.encoding===r.BECH32&&o&&c.includes(o.length)?{valid:!0,blockchain:this.chain,details:{format:"bech32",checksum:!0}}:{valid:!1,error:"Invalid account prefix, checksum, length, or padding",details:{errorCode:t.INVALID_FORMAT}}}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CosmosValidator } from './index.cjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CosmosValidator } from './index.mjs';
|
package/dist/cosmos.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var t;!function(t){t.INVALID_FORMAT="INVALID_FORMAT",t.INVALID_CHECKSUM="INVALID_CHECKSUM",t.INVALID_LENGTH="INVALID_LENGTH",t.INVALID_CHARACTERS="INVALID_CHARACTERS",t.UNSUPPORTED_CHAIN="UNSUPPORTED_CHAIN",t.BURN_ADDRESS="BURN_ADDRESS",t.UNSAFE_INPUT="UNSAFE_INPUT",t.NETWORK_MISMATCH="NETWORK_MISMATCH"}(t||(t={}));class e{static sanitize(e){if(null==e)throw new Error(t.UNSAFE_INPUT);if("string"!=typeof e)throw new Error(t.UNSAFE_INPUT);if(e.length>256)throw new Error(t.UNSAFE_INPUT);if(/[^\x20-\x7E]/.test(e))throw new Error(t.UNSAFE_INPUT);return e.trim()}}class n{validate(n,r){try{const o=e.sanitize(n),s=this.validateImplementation(o,r),i=s.details?.network;return s.valid&&i&&(r?.network&&"auto"!==r.network&&r.network!==i||!1===r?.allowTestnet&&"testnet"===i)?{valid:!1,error:"Address network does not match the requested network",details:{...s.details,errorCode:t.NETWORK_MISMATCH}}:s}catch(e){const n=e instanceof Error?e.message:String(e);return n===t.UNSAFE_INPUT?{valid:!1,error:"Unsafe input detected",details:{errorCode:t.UNSAFE_INPUT}}:{valid:!1,error:n,details:{errorCode:t.INVALID_FORMAT}}}}format(t,n){return function(t,n,r=4){const o=e.sanitize(t);if(!n?.shorten)return o;const s=n.shortenLength??r;if(!Number.isSafeInteger(s)||s<1)throw new RangeError("shortenLength must be a positive integer");const i=o.startsWith("0x")?2:0;return o.length<=2*s+i+3?o:`${o.slice(0,s+i)}...${o.slice(-s)}`}(t,n)}}var r;!function(t){t.BECH32="bech32",t.BECH32M="bech32m"}(r||(r={}));const o=[996825010,642813549,513874426,1027748829,705979059];function s(t,e,n){const s=n===r.BECH32M?734539939:1;return function(t){let e=1;for(const n of t){const t=e>>25;e=(33554431&e)<<5^n;for(let n=0;n<5;n++)t>>n&1&&(e^=o[n])}return e}(function(t){const e=[];for(let n=0;n<t.length;n++)e.push(t.charCodeAt(n)>>5);e.push(0);for(let n=0;n<t.length;n++)e.push(31&t.charCodeAt(n));return e}(t).concat(e))===s}const i={cosmos:"cosmos",osmosis:"osmo",celestia:"celestia",injective:"inj",sei:"sei"};class c extends n{constructor(t="cosmos"){super(),this.chain=t}validateImplementation(e){const n=function(t,e=90){if(t.length<8||t.length>e)return null;const n=t.toLowerCase();if(t!==n&&t!==t.toUpperCase())return null;const o=n.lastIndexOf("1");if(o<1||o+7>n.length)return null;const i=n.substring(0,o);if(/[^\x21-\x7E]/.test(i))return null;const c=[];for(let t=o+1;t<n.length;t++){const e="qpzry9x8gf2tvdw0s3jn54khce6mua7l".indexOf(n[t]);if(-1===e)return null;c.push(e)}return s(i,c,r.BECH32)?{hrp:i,data:c.slice(0,c.length-6),encoding:r.BECH32}:s(i,c,r.BECH32M)?{hrp:i,data:c.slice(0,c.length-6),encoding:r.BECH32M}:null}(e),o=n&&function(t){let e=0,n=0;const r=[];for(const o of t){if(!Number.isInteger(o)||o<0||o>31)return null;e=65535&(e<<5|o),n+=5,n>=8&&(n-=8,r.push(e>>>n&255))}return n>=5||e<<8-n&255?null:Uint8Array.from(r)}(n.data),c="injective"===this.chain||"celestia"===this.chain?[20]:[20,32];return n&&n.hrp===i[this.chain]&&n.encoding===r.BECH32&&o&&c.includes(o.length)?{valid:!0,blockchain:this.chain,details:{format:"bech32",checksum:!0}}:{valid:!1,error:"Invalid account prefix, checksum, length, or padding",details:{errorCode:t.INVALID_FORMAT}}}}export{c as CosmosValidator};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t;!function(t){t.INVALID_FORMAT="INVALID_FORMAT",t.INVALID_CHECKSUM="INVALID_CHECKSUM",t.INVALID_LENGTH="INVALID_LENGTH",t.INVALID_CHARACTERS="INVALID_CHARACTERS",t.UNSUPPORTED_CHAIN="UNSUPPORTED_CHAIN",t.BURN_ADDRESS="BURN_ADDRESS",t.UNSAFE_INPUT="UNSAFE_INPUT",t.NETWORK_MISMATCH="NETWORK_MISMATCH"}(t||(t={}));class n{static sanitize(n){if(null==n)throw new Error(t.UNSAFE_INPUT);if("string"!=typeof n)throw new Error(t.UNSAFE_INPUT);if(n.length>256)throw new Error(t.UNSAFE_INPUT);if(/[^\x20-\x7E]/.test(n))throw new Error(t.UNSAFE_INPUT);return n.trim()}}class e{validate(e,r){try{const i=n.sanitize(e),o=this.validateImplementation(i,r),s=o.details?.network;return o.valid&&s&&(r?.network&&"auto"!==r.network&&r.network!==s||!1===r?.allowTestnet&&"testnet"===s)?{valid:!1,error:"Address network does not match the requested network",details:{...o.details,errorCode:t.NETWORK_MISMATCH}}:o}catch(n){const e=n instanceof Error?n.message:String(n);return e===t.UNSAFE_INPUT?{valid:!1,error:"Unsafe input detected",details:{errorCode:t.UNSAFE_INPUT}}:{valid:!1,error:e,details:{errorCode:t.INVALID_FORMAT}}}}format(t,e){return function(t,e,r=4){const i=n.sanitize(t);if(!e?.shorten)return i;const o=e.shortenLength??r;if(!Number.isSafeInteger(o)||o<1)throw new RangeError("shortenLength must be a positive integer");const s=i.startsWith("0x")?2:0;return i.length<=2*o+s+3?i:`${i.slice(0,o+s)}...${i.slice(-o)}`}(t,e)}}const r="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",i=r.charAt(0),o={};for(let t=0;t<58;t++)o[r.charAt(t)]=t;function s(t){const n=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],e=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],r=8*t.length,i=t.length,o=new Uint8Array(64-(i+8)%64+8);o[0]=128;const s=r,a=Math.floor(s/4294967296),c=s%4294967296,h=o.length;o[h-4]=c>>>24&255,o[h-3]=c>>>16&255,o[h-2]=c>>>8&255,o[h-1]=255&c,o[h-8]=a>>>24&255,o[h-7]=a>>>16&255,o[h-6]=a>>>8&255,o[h-5]=255&a;const u=new Uint8Array(i+o.length);u.set(t),u.set(o,i);const f=new Uint32Array(64);for(let t=0;t<u.length;t+=64){for(let n=0;n<16;n++)f[n]=u[t+4*n]<<24|u[t+4*n+1]<<16|u[t+4*n+2]<<8|u[t+4*n+3];for(let t=16;t<64;t++){const n=l(f[t-2],17)^l(f[t-2],19)^f[t-2]>>>10,e=l(f[t-15],7)^l(f[t-15],18)^f[t-15]>>>3;f[t]=n+f[t-7]+e+f[t-16]|0}let r=e[0],i=e[1],o=e[2],s=e[3],a=e[4],c=e[5],h=e[6],d=e[7];for(let t=0;t<64;t++){const e=d+(l(a,6)^l(a,11)^l(a,25))+(a&c^~a&h)+n[t]+f[t]|0,u=r&i^r&o^i&o;d=h,h=c,c=a,a=s+e|0,s=o,o=i,i=r,r=e+((l(r,2)^l(r,13)^l(r,22))+u|0)|0}e[0]=e[0]+r|0,e[1]=e[1]+i|0,e[2]=e[2]+o|0,e[3]=e[3]+s|0,e[4]=e[4]+a|0,e[5]=e[5]+c|0,e[6]=e[6]+h|0,e[7]=e[7]+d|0}const d=new Uint8Array(32);for(let t=0;t<8;t++)d[4*t]=e[t]>>>24&255,d[4*t+1]=e[t]>>>16&255,d[4*t+2]=e[t]>>>8&255,d[4*t+3]=255&e[t];return d}function l(t,n){return t>>>n|t<<32-n}function a(t){try{const n=function(t){if(0===t.length)return new Uint8Array(0);let n=0;for(;n<t.length&&t[n]===i;)n++;const e=n,r=[0];for(;n<t.length;n++){const e=t[n],i=o[e];if(void 0===i)throw new Error(`Invalid Base58 character: ${e}`);for(let t=0;t<r.length;t++)r[t]*=58;r[0]+=i;let s=0;for(let t=0;t<r.length;++t)r[t]+=s,s=r[t]>>8,r[t]&=255;for(;s;)r.push(255&s),s>>=8}const s=t.length===e?[]:r.reverse();return Uint8Array.from([...new Array(e).fill(0),...s])}(t);if(n.length<5)return null;const e=n.slice(0,-4),r=s(s(e));return n.slice(-4).every((t,n)=>t===r[n])?e:null}catch{return null}}var c;!function(t){t.BECH32="bech32",t.BECH32M="bech32m"}(c||(c={}));const h=[996825010,642813549,513874426,1027748829,705979059];function u(t,n,e){const r=e===c.BECH32M?734539939:1;return function(t){let n=1;for(const e of t){const t=n>>25;n=(33554431&n)<<5^e;for(let e=0;e<5;e++)t>>e&1&&(n^=h[e])}return n}(function(t){const n=[];for(let e=0;e<t.length;e++)n.push(t.charCodeAt(e)>>5);n.push(0);for(let e=0;e<t.length;e++)n.push(31&t.charCodeAt(e));return n}(t).concat(n))===r}class f extends e{constructor(t){super(),this.config=t}validateImplementation(n){const e=n.toLowerCase().split("1")[0];if(this.config.hrps&&Object.prototype.hasOwnProperty.call(this.config.hrps,e))return this.validateWitness(n,this.config.hrps[e]);if(n.length<26||n.length>35)return this.invalid("Invalid address length",t.INVALID_LENGTH);const r=a(n);if(!r)return this.invalid("Invalid Base58Check address",t.INVALID_CHECKSUM);if(21!==r.length)return this.invalid("Invalid payload length",t.INVALID_LENGTH);const i=this.config.versions[r[0]];return i?{valid:!0,blockchain:this.config.chain,details:{format:"legacy",network:i,checksum:!0}}:this.invalid("Invalid address version")}validateWitness(n,e){const r=function(t,n=90){if(t.length<8||t.length>n)return null;const e=t.toLowerCase();if(t!==e&&t!==t.toUpperCase())return null;const r=e.lastIndexOf("1");if(r<1||r+7>e.length)return null;const i=e.substring(0,r);if(/[^\x21-\x7E]/.test(i))return null;const o=[];for(let t=r+1;t<e.length;t++){const n="qpzry9x8gf2tvdw0s3jn54khce6mua7l".indexOf(e[t]);if(-1===n)return null;o.push(n)}return u(i,o,c.BECH32)?{hrp:i,data:o.slice(0,o.length-6),encoding:c.BECH32}:u(i,o,c.BECH32M)?{hrp:i,data:o.slice(0,o.length-6),encoding:c.BECH32M}:null}(n);if(!r||0===r.data.length)return this.invalid("Invalid Bech32 encoding");const i=r.data[0],o=function(t){let n=0,e=0;const r=[];for(const i of t){if(!Number.isInteger(i)||i<0||i>31)return null;n=65535&(n<<5|i),e+=5,e>=8&&(e-=8,r.push(n>>>e&255))}return e>=5||n<<8-e&255?null:Uint8Array.from(r)}(r.data.slice(1));if(i>16)return this.invalid("Invalid witness version");if(!o||o.length<2||o.length>40||0===i&&20!==o.length&&32!==o.length)return this.invalid("Invalid witness program or padding",t.INVALID_LENGTH);const s=0===i?c.BECH32:c.BECH32M;if(r.encoding!==s)return this.invalid("Wrong checksum encoding for witness version",t.INVALID_CHECKSUM);const l=0===i?"segwit":1===i&&32===o.length?"taproot":"future";return{valid:!0,blockchain:this.config.chain,details:{format:l,network:e,checksum:!0}}}invalid(n,e=t.INVALID_FORMAT){return{valid:!1,error:n,details:{errorCode:e}}}}exports.DogecoinValidator=class extends f{constructor(){super({chain:"dogecoin",versions:{30:"mainnet",22:"mainnet",113:"testnet",196:"testnet"}})}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DogecoinValidator } from './index.cjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DogecoinValidator } from './index.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var t;!function(t){t.INVALID_FORMAT="INVALID_FORMAT",t.INVALID_CHECKSUM="INVALID_CHECKSUM",t.INVALID_LENGTH="INVALID_LENGTH",t.INVALID_CHARACTERS="INVALID_CHARACTERS",t.UNSUPPORTED_CHAIN="UNSUPPORTED_CHAIN",t.BURN_ADDRESS="BURN_ADDRESS",t.UNSAFE_INPUT="UNSAFE_INPUT",t.NETWORK_MISMATCH="NETWORK_MISMATCH"}(t||(t={}));class n{static sanitize(n){if(null==n)throw new Error(t.UNSAFE_INPUT);if("string"!=typeof n)throw new Error(t.UNSAFE_INPUT);if(n.length>256)throw new Error(t.UNSAFE_INPUT);if(/[^\x20-\x7E]/.test(n))throw new Error(t.UNSAFE_INPUT);return n.trim()}}class e{validate(e,r){try{const i=n.sanitize(e),o=this.validateImplementation(i,r),s=o.details?.network;return o.valid&&s&&(r?.network&&"auto"!==r.network&&r.network!==s||!1===r?.allowTestnet&&"testnet"===s)?{valid:!1,error:"Address network does not match the requested network",details:{...o.details,errorCode:t.NETWORK_MISMATCH}}:o}catch(n){const e=n instanceof Error?n.message:String(n);return e===t.UNSAFE_INPUT?{valid:!1,error:"Unsafe input detected",details:{errorCode:t.UNSAFE_INPUT}}:{valid:!1,error:e,details:{errorCode:t.INVALID_FORMAT}}}}format(t,e){return function(t,e,r=4){const i=n.sanitize(t);if(!e?.shorten)return i;const o=e.shortenLength??r;if(!Number.isSafeInteger(o)||o<1)throw new RangeError("shortenLength must be a positive integer");const s=i.startsWith("0x")?2:0;return i.length<=2*o+s+3?i:`${i.slice(0,o+s)}...${i.slice(-o)}`}(t,e)}}const r="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",i=r.charAt(0),o={};for(let t=0;t<58;t++)o[r.charAt(t)]=t;function s(t){const n=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],e=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],r=8*t.length,i=t.length,o=new Uint8Array(64-(i+8)%64+8);o[0]=128;const s=r,a=Math.floor(s/4294967296),c=s%4294967296,h=o.length;o[h-4]=c>>>24&255,o[h-3]=c>>>16&255,o[h-2]=c>>>8&255,o[h-1]=255&c,o[h-8]=a>>>24&255,o[h-7]=a>>>16&255,o[h-6]=a>>>8&255,o[h-5]=255&a;const u=new Uint8Array(i+o.length);u.set(t),u.set(o,i);const f=new Uint32Array(64);for(let t=0;t<u.length;t+=64){for(let n=0;n<16;n++)f[n]=u[t+4*n]<<24|u[t+4*n+1]<<16|u[t+4*n+2]<<8|u[t+4*n+3];for(let t=16;t<64;t++){const n=l(f[t-2],17)^l(f[t-2],19)^f[t-2]>>>10,e=l(f[t-15],7)^l(f[t-15],18)^f[t-15]>>>3;f[t]=n+f[t-7]+e+f[t-16]|0}let r=e[0],i=e[1],o=e[2],s=e[3],a=e[4],c=e[5],h=e[6],d=e[7];for(let t=0;t<64;t++){const e=d+(l(a,6)^l(a,11)^l(a,25))+(a&c^~a&h)+n[t]+f[t]|0,u=r&i^r&o^i&o;d=h,h=c,c=a,a=s+e|0,s=o,o=i,i=r,r=e+((l(r,2)^l(r,13)^l(r,22))+u|0)|0}e[0]=e[0]+r|0,e[1]=e[1]+i|0,e[2]=e[2]+o|0,e[3]=e[3]+s|0,e[4]=e[4]+a|0,e[5]=e[5]+c|0,e[6]=e[6]+h|0,e[7]=e[7]+d|0}const d=new Uint8Array(32);for(let t=0;t<8;t++)d[4*t]=e[t]>>>24&255,d[4*t+1]=e[t]>>>16&255,d[4*t+2]=e[t]>>>8&255,d[4*t+3]=255&e[t];return d}function l(t,n){return t>>>n|t<<32-n}function a(t){try{const n=function(t){if(0===t.length)return new Uint8Array(0);let n=0;for(;n<t.length&&t[n]===i;)n++;const e=n,r=[0];for(;n<t.length;n++){const e=t[n],i=o[e];if(void 0===i)throw new Error(`Invalid Base58 character: ${e}`);for(let t=0;t<r.length;t++)r[t]*=58;r[0]+=i;let s=0;for(let t=0;t<r.length;++t)r[t]+=s,s=r[t]>>8,r[t]&=255;for(;s;)r.push(255&s),s>>=8}const s=t.length===e?[]:r.reverse();return Uint8Array.from([...new Array(e).fill(0),...s])}(t);if(n.length<5)return null;const e=n.slice(0,-4),r=s(s(e));return n.slice(-4).every((t,n)=>t===r[n])?e:null}catch{return null}}var c;!function(t){t.BECH32="bech32",t.BECH32M="bech32m"}(c||(c={}));const h=[996825010,642813549,513874426,1027748829,705979059];function u(t,n,e){const r=e===c.BECH32M?734539939:1;return function(t){let n=1;for(const e of t){const t=n>>25;n=(33554431&n)<<5^e;for(let e=0;e<5;e++)t>>e&1&&(n^=h[e])}return n}(function(t){const n=[];for(let e=0;e<t.length;e++)n.push(t.charCodeAt(e)>>5);n.push(0);for(let e=0;e<t.length;e++)n.push(31&t.charCodeAt(e));return n}(t).concat(n))===r}class f extends e{constructor(t){super(),this.config=t}validateImplementation(n){const e=n.toLowerCase().split("1")[0];if(this.config.hrps&&Object.prototype.hasOwnProperty.call(this.config.hrps,e))return this.validateWitness(n,this.config.hrps[e]);if(n.length<26||n.length>35)return this.invalid("Invalid address length",t.INVALID_LENGTH);const r=a(n);if(!r)return this.invalid("Invalid Base58Check address",t.INVALID_CHECKSUM);if(21!==r.length)return this.invalid("Invalid payload length",t.INVALID_LENGTH);const i=this.config.versions[r[0]];return i?{valid:!0,blockchain:this.config.chain,details:{format:"legacy",network:i,checksum:!0}}:this.invalid("Invalid address version")}validateWitness(n,e){const r=function(t,n=90){if(t.length<8||t.length>n)return null;const e=t.toLowerCase();if(t!==e&&t!==t.toUpperCase())return null;const r=e.lastIndexOf("1");if(r<1||r+7>e.length)return null;const i=e.substring(0,r);if(/[^\x21-\x7E]/.test(i))return null;const o=[];for(let t=r+1;t<e.length;t++){const n="qpzry9x8gf2tvdw0s3jn54khce6mua7l".indexOf(e[t]);if(-1===n)return null;o.push(n)}return u(i,o,c.BECH32)?{hrp:i,data:o.slice(0,o.length-6),encoding:c.BECH32}:u(i,o,c.BECH32M)?{hrp:i,data:o.slice(0,o.length-6),encoding:c.BECH32M}:null}(n);if(!r||0===r.data.length)return this.invalid("Invalid Bech32 encoding");const i=r.data[0],o=function(t){let n=0,e=0;const r=[];for(const i of t){if(!Number.isInteger(i)||i<0||i>31)return null;n=65535&(n<<5|i),e+=5,e>=8&&(e-=8,r.push(n>>>e&255))}return e>=5||n<<8-e&255?null:Uint8Array.from(r)}(r.data.slice(1));if(i>16)return this.invalid("Invalid witness version");if(!o||o.length<2||o.length>40||0===i&&20!==o.length&&32!==o.length)return this.invalid("Invalid witness program or padding",t.INVALID_LENGTH);const s=0===i?c.BECH32:c.BECH32M;if(r.encoding!==s)return this.invalid("Wrong checksum encoding for witness version",t.INVALID_CHECKSUM);const l=0===i?"segwit":1===i&&32===o.length?"taproot":"future";return{valid:!0,blockchain:this.config.chain,details:{format:l,network:e,checksum:!0}}}invalid(n,e=t.INVALID_FORMAT){return{valid:!1,error:n,details:{errorCode:e}}}}class d extends f{constructor(){super({chain:"dogecoin",versions:{30:"mainnet",22:"mainnet",113:"testnet",196:"testnet"}})}}export{d as DogecoinValidator};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t;!function(t){t.INVALID_FORMAT="INVALID_FORMAT",t.INVALID_CHECKSUM="INVALID_CHECKSUM",t.INVALID_LENGTH="INVALID_LENGTH",t.INVALID_CHARACTERS="INVALID_CHARACTERS",t.UNSUPPORTED_CHAIN="UNSUPPORTED_CHAIN",t.BURN_ADDRESS="BURN_ADDRESS",t.UNSAFE_INPUT="UNSAFE_INPUT",t.NETWORK_MISMATCH="NETWORK_MISMATCH"}(t||(t={}));class e{static sanitize(e){if(null==e)throw new Error(t.UNSAFE_INPUT);if("string"!=typeof e)throw new Error(t.UNSAFE_INPUT);if(e.length>256)throw new Error(t.UNSAFE_INPUT);if(/[^\x20-\x7E]/.test(e))throw new Error(t.UNSAFE_INPUT);return e.trim()}}class r{validate(r,n){try{const o=e.sanitize(r),s=this.validateImplementation(o,n),i=s.details?.network;return s.valid&&i&&(n?.network&&"auto"!==n.network&&n.network!==i||!1===n?.allowTestnet&&"testnet"===i)?{valid:!1,error:"Address network does not match the requested network",details:{...s.details,errorCode:t.NETWORK_MISMATCH}}:s}catch(e){const r=e instanceof Error?e.message:String(e);return r===t.UNSAFE_INPUT?{valid:!1,error:"Unsafe input detected",details:{errorCode:t.UNSAFE_INPUT}}:{valid:!1,error:r,details:{errorCode:t.INVALID_FORMAT}}}}format(t,e){return n(t,e)}}function n(t,r,n=4){const o=e.sanitize(t);if(!r?.shorten)return o;const s=r.shortenLength??n;if(!Number.isSafeInteger(s)||s<1)throw new RangeError("shortenLength must be a positive integer");const i=o.startsWith("0x")?2:0;return o.length<=2*s+i+3?o:`${o.slice(0,s+i)}...${o.slice(-s)}`}function o(t){return t.startsWith("0x")?t.slice(2):t}function s(t){return t.startsWith("0x")?t:`0x${t}`}const i=(1n<<64n)-1n,a=[0,1,62,28,27,36,44,6,55,20,3,10,43,25,39,41,45,15,21,8,18,2,61,56,14],l=[0x0000000000000001n,0x0000000000008082n,0x800000000000808an,0x8000000080008000n,0x000000000000808bn,0x0000000080000001n,0x8000000080008081n,0x8000000000008009n,0x000000000000008an,0x0000000000000088n,0x0000000080008009n,0x000000008000000an,0x000000008000808bn,0x800000000000008bn,0x8000000000008089n,0x8000000000008003n,0x8000000000008002n,0x8000000000000080n,0x000000000000800an,0x800000008000000an,0x8000000080008081n,0x8000000000008080n,0x0000000080000001n,0x8000000080008008n];function d(t,e){return(t<<BigInt(e)|t>>BigInt(64-e))&i}function c(t){const e=new Array(5),r=new Array(25);for(const n of l){for(let r=0;r<5;r++)e[r]=t[r]^t[r+5]^t[r+10]^t[r+15]^t[r+20];for(let r=0;r<5;r++){const n=e[(r+4)%5]^d(e[(r+1)%5],1);for(let e=0;e<5;e++)t[r+5*e]^=n}for(let e=0;e<5;e++)for(let n=0;n<5;n++)r[n+(2*e+3*n)%5*5]=d(t[e+5*n],a[e+5*n]);for(let e=0;e<5;e++)for(let n=0;n<5;n++)t[e+5*n]=r[e+5*n]^~r[(e+1)%5+5*n]&r[(e+2)%5+5*n];t[0]^=n}}exports.EthereumValidator=class extends r{validateImplementation(e,r){const n=o(e);if(40!==n.length)return{valid:!1,error:"Invalid length",details:{errorCode:t.INVALID_LENGTH}};if(!function(t){if("string"!=typeof t)return!1;const e=t.startsWith("0x")?t.slice(2):t;return/^[0-9a-fA-F]*$/.test(e)}(n))return{valid:!1,error:"Invalid hex characters",details:{errorCode:t.INVALID_CHARACTERS}};if(!r?.allowBurnAddress&&/^0+$/.test(n))return{valid:!1,error:"Burn address detected",details:{errorCode:t.BURN_ADDRESS}};const s=this.isChecksummed(n);return(r?.strict||n!==n.toLowerCase()&&n!==n.toUpperCase())&&!s?{valid:!1,error:"Invalid checksum",details:{errorCode:t.INVALID_CHECKSUM}}:{valid:!0,blockchain:"ethereum",details:{checksum:s}}}format(t,r){let o=s(e.sanitize(t));return r?.checksum&&(o=this.toChecksumAddress(o)),n(o,r)}isChecksummed(t){const e=o(t);return s(e)===this.toChecksumAddress(e)}toChecksumAddress(t){const e=o(t).toLowerCase();if(!/^[0-9a-f]{40}$/.test(e))throw new Error("Expected a 20-byte hexadecimal address");const r=function(t){const e=(new TextEncoder).encode(t),r=new Uint8Array(136*(Math.floor(e.length/136)+1));r.set(e),r[e.length]=1,r[r.length-1]|=128;const n=new Array(25).fill(0n);for(let t=0;t<r.length;t+=136){for(let e=0;e<136;e++)n[e>>3]^=BigInt(r[t+e])<<BigInt(8*(7&e));c(n)}let o="";for(let t=0;t<32;t++)o+=Number(n[t>>3]>>BigInt(8*(7&t))&255n).toString(16).padStart(2,"0");return o}(e);return"0x"+[...e].map((t,e)=>parseInt(r[e],16)>=8?t.toUpperCase():t).join("")}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { EthereumValidator } from './index.cjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { EthereumValidator } from './index.mjs';
|