[bchaddrjs] Add types for bchaddrjs (#37519)

This commit is contained in:
Emilio Almansi 2019-08-12 20:00:01 -03:00 committed by Pranav Senthilnathan
parent 37a3f3dd67
commit 855d02b94b
4 changed files with 193 additions and 0 deletions

View File

@ -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();
}
}

133
types/bchaddrjs/index.d.ts vendored Normal file
View File

@ -0,0 +1,133 @@
// Type definitions for bchaddrjs 0.4
// Project: https://github.com/bitcoincashjs/bchaddrjs#readme
// Definitions by: Emilio Almansi <https://github.com/ealmansi>
// 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(); }

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }