diff --git a/types/cashaddrjs/cashaddrjs-tests.ts b/types/cashaddrjs/cashaddrjs-tests.ts new file mode 100644 index 0000000000..6059b2355a --- /dev/null +++ b/types/cashaddrjs/cashaddrjs-tests.ts @@ -0,0 +1,19 @@ +import * as cashaddr from 'cashaddrjs'; + +let someAddress = 'bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a'; +let prefix: string; +let type: string; +let hash: Uint8Array; +({ prefix, type, hash } = cashaddr.decode(someAddress)); +someAddress = cashaddr.encode(prefix, type, hash); +const someInvalidInput = 'abcdefghijklmn'; +try { + cashaddr.decode(someInvalidInput); +} catch (err) { + if (err instanceof cashaddr.ValidationError) { + // As expected. + } else { + const someMessage = 'some error message'; + throw new cashaddr.ValidationError(someMessage); + } +} diff --git a/types/cashaddrjs/index.d.ts b/types/cashaddrjs/index.d.ts new file mode 100644 index 0000000000..2862dcda03 --- /dev/null +++ b/types/cashaddrjs/index.d.ts @@ -0,0 +1,31 @@ +// Type definitions for cashaddrjs 0.3 +// Project: https://github.com/bitcoincashjs/cashaddrjs#readme +// Definitions by: Emilio Almansi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export as namespace cashaddr; + +/** + * Encodes a hash from a given type into a Bitcoin Cash address with the given prefix. + * + * @param prefix Network prefix. E.g.: 'bitcoincash'. + * @param type Type of address to generate. Either 'P2PKH' or 'P2SH'. + * @param hash Hash to encode represented as an array of 8-bit integers. + * @throws {ValidationError} + */ +export function encode(prefix: string, type: string, hash: Uint8Array): string; + +/** + * Decodes the given address into its constituting prefix, type and hash. + * + * @param address Address to decode. E.g.: 'bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a'. + * @throws {ValidationError} + */ +export function decode(address: string): { prefix: string, type: string, hash: Uint8Array }; + +/** + * Error thrown when encoding or decoding fail due to invalid input. + * + * @param message Error description. + */ +export class ValidationError extends Error { constructor(message: string); } diff --git a/types/cashaddrjs/tsconfig.json b/types/cashaddrjs/tsconfig.json new file mode 100644 index 0000000000..2f2acbb28e --- /dev/null +++ b/types/cashaddrjs/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "cashaddrjs-tests.ts" + ] +} diff --git a/types/cashaddrjs/tslint.json b/types/cashaddrjs/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/cashaddrjs/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }