diff --git a/types/bchaddrjs/bchaddrjs-tests.ts b/types/bchaddrjs/bchaddrjs-tests.ts new file mode 100644 index 0000000000..ce242706c7 --- /dev/null +++ b/types/bchaddrjs/bchaddrjs-tests.ts @@ -0,0 +1,36 @@ +import * as bchaddr from 'bchaddrjs'; + +const formatLegacy: string = bchaddr.Format.Legacy; +const formatBitpay: string = bchaddr.Format.Bitpay; +const formatCashaddr: string = bchaddr.Format.Cashaddr; +const networkMainnet: string = bchaddr.Network.Mainnet; +const networkTestnet: string = bchaddr.Network.Testnet; +const typeP2PKH: string = bchaddr.Type.P2PKH; +const typeP2SH: string = bchaddr.Type.P2SH; +const someAddress = 'qph5kuz78czq00e3t85ugpgd7xmer5kr7c5f6jdpwk'; +const someFormat: string = bchaddr.detectAddressFormat(someAddress); +const someNetwork: string = bchaddr.detectAddressNetwork(someAddress); +const someType: string = bchaddr.detectAddressType(someAddress); +let someBoolean: boolean; +someBoolean = bchaddr.isValidAddress(someAddress); +someBoolean = bchaddr.isLegacyAddress(someAddress); +someBoolean = bchaddr.isBitpayAddress(someAddress); +someBoolean = bchaddr.isCashAddress(someAddress); +someBoolean = bchaddr.isMainnetAddress(someAddress); +someBoolean = bchaddr.isTestnetAddress(someAddress); +someBoolean = bchaddr.isP2PKHAddress(someAddress); +someBoolean = bchaddr.isP2SHAddress(someAddress); +let someOtherAddress: string; +someOtherAddress = bchaddr.toLegacyAddress(someAddress); +someOtherAddress = bchaddr.toBitpayAddress(someAddress); +someOtherAddress = bchaddr.toCashAddress(someAddress); +const someInvalidInput = 'abcdefghijklmn'; +try { + bchaddr.detectAddressFormat(someInvalidInput); +} catch (err) { + if (err instanceof bchaddr.InvalidAddressError) { + // As expected. + } else { + throw new bchaddr.InvalidAddressError(); + } +} diff --git a/types/bchaddrjs/index.d.ts b/types/bchaddrjs/index.d.ts new file mode 100644 index 0000000000..07a61e7f64 --- /dev/null +++ b/types/bchaddrjs/index.d.ts @@ -0,0 +1,133 @@ +// Type definitions for bchaddrjs 0.4 +// Project: https://github.com/bitcoincashjs/bchaddrjs#readme +// Definitions by: Emilio Almansi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export as namespace bchaddr; + +/** + * Supported Bitcoin Cash address formats. + */ +export namespace Format { + const Legacy: string; + const Bitpay: string; + const Cashaddr: string; +} + +/** + * Supported networks. + */ +export namespace Network { + const Mainnet: string; + const Testnet: string; +} + +/** + * Supported address types. + */ +export namespace Type { + const P2PKH: string; + const P2SH: string; +} + +/** + * Returns a boolean indicating whether the given input is a valid Bitcoin Cash address. + * @param input - Any input to check for validity. + */ +export function isValidAddress(input: any): boolean; + +/** + * Detects what is the given address' format. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function detectAddressFormat(address: string): string; + +/** + * Detects what is the given address' network. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function detectAddressNetwork(address: string): string; + +/** + * Detects what is the given address' type. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function detectAddressType(address: string): string; + +/** + * Translates the given address into legacy format. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function toLegacyAddress(address: string): string; + +/** + * Translates the given address into bitpay format. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function toBitpayAddress(address: string): string; + +/** + * Translates the given address into cashaddr format. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function toCashAddress(address: string): string; + +/** + * Returns a boolean indicating whether the address is in legacy format. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function isLegacyAddress(address: string): boolean; + +/** + * Returns a boolean indicating whether the address is in bitpay format. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function isBitpayAddress(address: string): boolean; + +/** + * Returns a boolean indicating whether the address is in cashaddr format. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function isCashAddress(address: string): boolean; + +/** + * Returns a boolean indicating whether the address is a mainnet address. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function isMainnetAddress(address: string): boolean; + +/** + * Returns a boolean indicating whether the address is a testnet address. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function isTestnetAddress(address: string): boolean; + +/** + * Returns a boolean indicating whether the address is a p2pkh address. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function isP2PKHAddress(address: string): boolean; + +/** + * Returns a boolean indicating whether the address is a p2sh address. + * @param address - A valid Bitcoin Cash address in any format. + * @throws {InvalidAddressError} + */ +export function isP2SHAddress(address: string): boolean; + +/** + * Error thrown when the address given as input is not a valid Bitcoin Cash address. + */ +export class InvalidAddressError extends Error { constructor(); } diff --git a/types/bchaddrjs/tsconfig.json b/types/bchaddrjs/tsconfig.json new file mode 100644 index 0000000000..acc91b1fc4 --- /dev/null +++ b/types/bchaddrjs/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es5" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "bchaddrjs-tests.ts" + ] +} diff --git a/types/bchaddrjs/tslint.json b/types/bchaddrjs/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/bchaddrjs/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }