Skip to content

Commit

Permalink
Merge pull request #11 from bchainhub/update/crypto-types-01
Browse files Browse the repository at this point in the history
Crypto types
  • Loading branch information
rastislavcore authored Dec 7, 2024
2 parents 3153492 + be337c5 commit a493edf
Show file tree
Hide file tree
Showing 12 changed files with 398 additions and 915 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
test/**/*.js
# Ensure dist is not ignored by standard
!dist/
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '18.x'
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- name: Install & Build 🔧
run: npm i && npm run build
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 18
node-version: '20.x'
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm run unit
Expand All @@ -25,7 +25,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 18
node-version: '20.x'
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm run standard
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ node_modules
yarn.lock
package-lock.json

/dist

.DS_Store
.AppleDouble
.LSOverride
Icon
/Icon
._*
.DocumentRevisions-V100
.fseventsd
Expand Down
124 changes: 89 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

# ican.js

ICAN and BCAN validation, formatting, and conversion in JavaScript.

ICAN.js adheres to the [ISO 13616 IBAN Registry technical specification](https://www.swift.com/standards/data-standards/iban) and includes support for Crypto addresses.
ICAN.js adheres to the [ISO 13616 IBAN Registry technical specification](https://www.swift.com/standards/data-standards/iban) and includes support for Crypto addresses, including distinctions for mainnets, testnets, and enterprise networks.

## Usage

Expand All @@ -12,71 +13,124 @@ ICAN.js is compatible with both CommonJS and AMD module definitions.

You can install [@blockchainhub/ican from NPM](https://www.npmjs.com/package/@blockchainhub/ican) using Yarn, NPM, or another tool.

> Yarn
> NPM
```sh
yarn add @blockchainhub/ican
npm install @blockchainhub/ican
```

> NPM
> Bun
```sh
npm i @blockchainhub/ican
bun add @blockchainhub/ican
```

### In node.js
### In Node.js

```js
var ICAN = require('@blockchainhub/ican');
ICAN.isValid('hello world'); // false
ICAN.isValid('BE68539007547034'); // true
const ICAN = require('@blockchainhub/ican');
console.log(ICAN.isValid('hello world')); // false
console.log(ICAN.isValid('BE68539007547034')); // true
```

### In the browser
### In the Browser

You can use a module loader (AMD or CommonJS) or access it directly through the global ```ICAN``` object:
You can use a module loader (AMD or CommonJS) or access it directly through the global `ICAN` object:

```html
<script src="ican.js"></script>
<script>
// the API is now accessible from the window.ICAN global object
ICAN.isValid('hello world'); // false
ICAN.isValid('BE68539007547034'); // true
// The API is now accessible from the window.ICAN global object
console.log(ICAN.isValid('hello world')); // false
console.log(ICAN.isValid('BE68539007547034')); // true
</script>
```

### With React
### With ESM

Using ICAN.js with React is easy. For example:
Using ICAN.js with React is straightforward:

```js
import Ican from '@blockchainhub/ican';
Ican.isValid('hello world');
Ican.isValid('BE68539007547034');
```ts
import ICAN from '@blockchainhub/ican';
console.log(ICAN.isValid('hello world')); // false
console.log(ICAN.isValid('BE68539007547034')); // true
```

## API

- isValid(ican, onlyCrypto)
- toBCAN(ican, separator, onlyCrypto)
- fromBCAN(countryCode, bcan)
- isValidBCAN(countryCode, bcan, onlyCrypto)
- printFormat(ican, separator)
- electronicFormat(ican)
- shortFormat(ican, separator, frontCount, backCount)
### Methods

## Contributions
#### `isValid(ican, onlyCrypto)`

We welcome contributions in any form. Here's how you can help:
- Validates an ICAN.
- **Parameters**:
- `ican`: The ICAN to validate.
- `onlyCrypto`: (Optional) Restrict validation to crypto definitions. Possible values:
- `true`: Include all crypto networks.
- `false`: Exclude crypto networks.
- `'main'`: Mainnets.
- `'test'`: Testnets.
- `'enter'`: Enterprise networks.

#### `toBCAN(ican, separator)`

- Converts an ICAN to its country-specific BCAN representation.
- **Parameters**:
- `ican`: The ICAN to convert.
- `separator`: (Optional) The separator to use between BCAN blocks (default is `' '`).

#### `fromBCAN(countryCode, bcan)`

- Converts a BCAN to an ICAN for a specific country.
- **Parameters**:
- `countryCode`: The country code of the BCAN.
- `bcan`: The BCAN to convert.

#### `isValidBCAN(countryCode, bcan, onlyCrypto)`

- Fork [this repository](/fork)
- Open a [pull request](/pulls)
- Support us with some Øres / ₡ores: [cb7147879011ea207df5b35a24ca6f0859dcfb145999](https://blockindex.net/address/cb7147879011ea207df5b35a24ca6f0859dcfb145999)
- Star this repository
- Validates a BCAN.
- **Parameters**:
- `countryCode`: The country code of the BCAN.
- `bcan`: The BCAN to validate.
- `onlyCrypto`: (Optional) Restrict validation to crypto definitions (same values as `isValid`).

## Epigram
#### `printFormat(ican, separator)`

- Formats an ICAN for display with separators.
- **Parameters**:
- `ican`: The ICAN to format.
- `separator`: (Optional) The separator to use (default is `' '`).

#### `electronicFormat(ican)`

- Converts an ICAN to its electronic format (removing non-alphanumeric characters and uppercasing).

#### `shortFormat(ican, separator, frontCount, backCount)`

- Produces a shortened version of the ICAN with a custom separator.
- **Parameters**:
- `ican`: The ICAN to shorten.
- `separator`: (Optional) The separator to use (default is `'…'`).
- `frontCount`: (Optional) Number of characters to display at the start (default is `4`).
- `backCount`: (Optional) Number of characters to display at the end (default is `4`).

### Variables

- `ican`: An International Crypto Account Number.
- `bcan`: A country-specific Base Crypto Account Number.
- `countryCode`: The country code for the BCAN.
- `separator`: A separator to format ICAN or BCAN strings.
- `onlyCrypto`: A filter for crypto validation (values: `true`, `false`, `'main'`, `'test'`, `'enter'`).
- `frontCount` and `backCount`: Define how many characters to include in the `shortFormat` method.

## Contributions

We welcome contributions in any form. Here's how you can help:

> 「Cryptoni Confidimus」
1. Fork [this repository](/fork).
2. Open a [pull request](/pulls).
3. Support us with some Øres / ₡ores: [cb7147879011ea207df5b35a24ca6f0859dcfb145999](https://blockindex.net/address/cb7147879011ea207df5b35a24ca6f0859dcfb145999).
4. Star this repository.

## License

Expand Down
142 changes: 142 additions & 0 deletions ican.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
interface ICANStatic {
/**
* An object containing all the known ICAN specifications.
*/
countries: Record<ICAN.Specification['countryCode'], ICAN.Specification>;

/**
* Returns the ICAN in an electronic format.
* @param ican The ICAN to convert.
* @returns The ICAN in electronic format.
*/
electronicFormat(ican: string): string;

/**
* Convert the passed BCAN to an ICAN for this country specification.
* @param countryCode The country of the BCAN.
* @param bcan The BCAN to convert to ICAN.
* @returns The ICAN.
*/
fromBCAN(countryCode: string, bcan: string): string;

/**
* Check if the passed ICAN is valid according to this specification.
* @param ican The ICAN to validate.
* @param onlyCrypto Check only crypto definitions. Possible values:
* - `true` = Include all crypto networks.
* - `false` = Exclude crypto networks.
* - `'main' | 'mainnet'` = Mainnets.
* - `'test' | 'testnet'` = Testnets.
* - `'enter' | 'enterprise'` = Enterprise networks.
* @returns True if valid, false otherwise.
*/
isValid(ican: string, onlyCrypto?: boolean | 'main' | 'test' | 'enter' | 'mainnet' | 'testnet' | 'enterprise'): boolean;

/**
* Check if the passed BCAN is valid.
* @param countryCode The country of the BCAN.
* @param bcan The BCAN to validate.
* @param onlyCrypto Check only crypto definitions. Possible values:
* - `true` = Include all crypto networks.
* - `false` = Exclude crypto networks.
* - `'main' | 'mainnet'` = Mainnets.
* - `'test' | 'testnet'` = Testnets.
* - `'enter' | 'enterprise'` = Enterprise networks.
* @returns True if valid, false otherwise.
*/
isValidBCAN(countryCode: string, bcan: string, onlyCrypto?: boolean | 'main' | 'test' | 'enter' | 'mainnet' | 'testnet' | 'enterprise'): boolean;

/**
* Returns the ICAN in a print format.
* @param ican The ICAN to convert.
* @param [separator] The separator to use between ICAN blocks, defaults to ' '.
* @returns The formatted ICAN.
*/
printFormat(ican: string, separator?: string): string;

/**
* Convert the passed ICAN to a country-specific BCAN.
* @param ican The ICAN to convert.
* @param [separator] The separator to use between BCAN blocks, defaults to ' '.
* @returns The BCAN
*/
toBCAN(ican: string, separator?: string): string;

/**
* Returns the ICAN in a short format.
* @param ican The ICAN to convert.
* @param [separator] The separator to use between ICAN openings/endings, defaults to '…'.
* @param [frontCount] The number of characters to show at the beginning, defaults to 4.
* @param [backCount] The number of characters to show at the end, defaults to 4.
* @returns The shortened ICAN.
*/
shortFormat(ican: string, separator?: string, frontCount?: number, backCount?: number): string;
}

declare var ICAN: ICANStatic;

declare namespace ICAN {
interface Specification {
/** The code of the country. */
readonly countryCode: string;
/** The length of the ICAN. */
readonly length: number;
/** The structure of the underlying BCAN (for validation and formatting). */
readonly structure: string;
/**
* The crypto property. Possible values:
* - `true` = All crypto networks.
* - `false` = Non-crypto.
* - `'main' | 'mainnet'` = Mainnets.
* - `'test' | 'testnet'` = Testnets.
* - `'enter' | 'enterprise'` = Enterprise networks.
*/
readonly crypto: boolean | 'main' | 'test' | 'enter' | 'mainnet' | 'testnet' | 'enterprise';
/** An example valid ICAN. */
readonly example: string;

/**
* Check if the passed ICAN is valid according to this specification.
* @param ican The ICAN to validate.
* @param onlyCrypto Check only crypto definitions. Possible values:
* - `true` = Include all crypto networks.
* - `false` = Exclude crypto networks.
* - `'main' | 'mainnet'` = Mainnets.
* - `'test' | 'testnet'` = Testnets.
* - `'enter' | 'enterprise'` = Enterprise networks.
* @returns True if valid, false otherwise.
*/
isValid(ican: string, onlyCrypto?: boolean | 'main' | 'test' | 'enter' | 'mainnet' | 'testnet' | 'enterprise'): boolean;

/**
* Convert the passed ICAN to a country-specific BCAN.
* @param ican The ICAN to convert.
* @param separator The separator to use between BCAN blocks.
* @returns The BCAN.
*/
toBCAN(ican: string, separator: string): string;

/**
* Convert the passed BCAN to an ICAN for this country specification.
* @param bcan The BCAN to convert to ICAN.
* @returns The ICAN.
*/
fromBCAN(bcan: string): string;

/**
* Check if the passed BCAN is valid.
* @param bcan The BCAN to validate.
* @param onlyCrypto Check only crypto definitions. Possible values:
* - `true` = Include all crypto networks.
* - `false` = Exclude crypto networks.
* - `'main' | 'mainnet'` = Mainnets.
* - `'test' | 'testnet'` = Testnets.
* - `'enter' | 'enterprise'` = Enterprise networks.
* @returns True if valid, false otherwise.
*/
isValidBCAN(bcan: string, onlyCrypto?: boolean | 'main' | 'test' | 'enter' | 'mainnet' | 'testnet' | 'enterprise'): boolean;
}
}

export = ICAN;
export as namespace ICAN;
Loading

0 comments on commit a493edf

Please sign in to comment.