Skip to content

Commit

Permalink
Merge pull request #4 from Road2Crypto/ahmed/update
Browse files Browse the repository at this point in the history
Update Package Name, Import style and Doc
  • Loading branch information
afern247 authored May 27, 2024
2 parents aff1a15 + 518586e commit a7e674b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 61 deletions.
44 changes: 25 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# road2crypto-wallet-address-validator
# r2c-wallet-validator

A simple utility to validate a cryptocurrency wallet address.

Expand All @@ -7,15 +7,19 @@ A simple utility to validate a cryptocurrency wallet address.
To install the package, run:

```sh
npm install road2crypto-wallet-address-validator
npm install r2c-wallet-validator
```

## Usage

Import the `isWalletValid` function and use it to validate wallet addresses.
Import the necessary functions and types from `r2c-wallet-validator` and use them in your code.

```typescript
import isWalletValid from "road2crypto-wallet-address-validator";
import {
isWalletValid,
ValidationErrorMessage,
WalletType,
} from "r2c-wallet-validator";

const result = isWalletValid("your_wallet_address_here");

Expand All @@ -29,11 +33,7 @@ if (!result.valid) {
### Example

```typescript
import isWalletValid from "road2crypto-wallet-address-validator";
import {
ValidationErrorMessage,
WalletType,
} from "road2crypto-wallet-address-validator/dist/types";
import { isWalletValid, ValidationErrorMessage, WalletType } from "r2c-wallet-validator";

const addresses = [
"0x742d35Cc6634C0532925a3b844Bc454e4438f44e", // EVM
Expand All @@ -47,12 +47,8 @@ addresses.forEach((address) => {
if (!result.valid) {
if (result.error?.message === ValidationErrorMessage.EMPTY_ADDRESS) {
console.log(`Address: ${address} is invalid: Address is empty`);
} else if (
result.error?.message === ValidationErrorMessage.INVALID_ADDRESS
) {
console.log(
`Address: ${address} is invalid: Address format is incorrect`
);
} else if (result.error?.message === ValidationErrorMessage.INVALID_ADDRESS) {
console.log(`Address: ${address} is invalid: Address format is incorrect`);
}
} else {
console.log(`Address: ${address} is valid and of type ${result.type}`);
Expand All @@ -76,12 +72,22 @@ addresses.forEach((address) => {
- `statusCode` (number): The HTTP status code representing the error.
- `message` (ValidationErrorMessage): The error message.

## Error Messages
## Types

### `ValidationErrorMessage`

- An enum containing possible error messages when validating wallet addresses.
- Possible values:
- `EMPTY_ADDRESS`: The provided address is empty.
- `INVALID_ADDRESS`: The provided address does not match any supported wallet address patterns.

The possible error messages are:
### `WalletType`

- `ValidationErrorMessage.EMPTY_ADDRESS`: The provided address is empty.
- `ValidationErrorMessage.INVALID_ADDRESS`: The provided address does not match any supported wallet address patterns.
- An enum representing the type of cryptocurrency wallet addresses.
- Possible values:
- `EVM`: Ethereum Virtual Machine (EVM) wallet.
- `SOLANA`: Solana wallet.
- `BITCOIN`: Bitcoin wallet.

## Supported Chains

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 34 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
{
"name": "road2crypto-wallet-address-validator",
"version": "1.1.1",
"name": "r2c-wallet-validator",
"version": "0.0.1",
"description": "A simple utility to validate cryptocurrency wallet addresses for EVM, Solana, and Bitcoin chains.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.js"
},
"./dist": "./dist",
"./src": "./src"
},
"scripts": {
"build": "tsc",
"setup": "npm i && npm run build",
"test": "npm run setup && jest",
"prepublishOnly": "npm run build",
"publish": "npm test && npm publish --access public"
"build": "tsc",
"setup": "npm i && npm run build",
"test": "npm run setup && jest",
"prepublishOnly": "npm run build",
"publish": "npm test && npm publish --access public"
},
"repository": {
"type": "git",
"url": "https://github.com/Road2Crypto/wallet-address-validator.git"
"type": "git",
"url": "https://github.com/Road2Crypto/wallet-address-validator.git"
},
"keywords": [
"wallet",
"address",
"validator",
"cryptocurrency",
"EVM",
"Solana",
"Bitcoin"
"wallet",
"address",
"validator",
"cryptocurrency",
"EVM",
"Solana",
"Bitcoin"
],
"author": "Road2Crypto",
"license": "MIT",
"dependencies": {
"web3-validator": "2.0.5"
"web3-validator": "2.0.5"
},
"devDependencies": {
"@babel/core": "7.24.5",
"@babel/preset-env": "7.24.5",
"@babel/preset-typescript": "7.24.1",
"@types/jest": "29.5.12",
"babel-jest": "29.7.0",
"jest": "29.7.0",
"ts-jest": "29.1.3",
"typescript": "5.4.5"
"@babel/core": "7.24.5",
"@babel/preset-env": "7.24.5",
"@babel/preset-typescript": "7.24.1",
"@types/jest": "29.5.12",
"babel-jest": "29.7.0",
"jest": "29.7.0",
"ts-jest": "29.1.3",
"typescript": "5.4.5"
}
}
}
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { checkEmpty } from "./utils/checkEmpty";
import { getWalletType } from "./utils/getWalletType";
import { Response, ValidationErrorMessage } from "./types/response";
import { WalletValidationResponse, ValidationErrorMessage } from "./types/response";

// Function to check crypto address
export const isWalletValid = (address: string): Response => {
export const isWalletValid = (address: string): WalletValidationResponse => {
// Remove extra spaces
address = address.trim();

// Check if the address is empty
if (checkEmpty(address)) return { valid: false, error: { statusCode: 400, message: ValidationErrorMessage.EMPTY_ADDRESS } };
if (checkEmpty(address)) {
return { valid: false, error: { statusCode: 400, message: ValidationErrorMessage.EMPTY_ADDRESS } };
}

// Get address type and validate it
const walletAddressType = getWalletType(address);
if (walletAddressType === null) return { valid: false, error: { statusCode: 400, message: ValidationErrorMessage.INVALID_ADDRESS } };
if (walletAddressType === null) {
return { valid: false, error: { statusCode: 400, message: ValidationErrorMessage.INVALID_ADDRESS } };
}

// Return success response
return { valid: true, type: walletAddressType };
};

// Re-exporting types for easier import in other files
export { ValidationErrorMessage } from "./types/response";
export { WalletType } from "./types/wallet";
26 changes: 18 additions & 8 deletions src/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ export enum ValidationErrorMessage {
}

// Return type of `isWalletValid` function
export interface Response {
valid: boolean,
type?: WalletType,
error?: {
statusCode: number,
message: ValidationErrorMessage,
},
}
interface WalletValidationResponseError {
statusCode: number,
message: ValidationErrorMessage,
};

interface WalletValidationResponseWithType {
valid: true,
type: WalletType,
error?: undefined,
};

interface WalletValidationResponseWithError {
valid: false,
type?: undefined,
error: WalletValidationResponseError;
};

export type WalletValidationResponse = WalletValidationResponseWithType | WalletValidationResponseWithError;

0 comments on commit a7e674b

Please sign in to comment.