[cashaddrjs] Add types for cashaddrjs (#37530)

This commit is contained in:
Emilio Almansi 2019-08-12 19:42:25 -03:00 committed by Pranav Senthilnathan
parent 34dddd92f9
commit 37a3f3dd67
4 changed files with 74 additions and 0 deletions

View File

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

31
types/cashaddrjs/index.d.ts vendored Normal file
View File

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

View File

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

View File

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