Skip to content

Commit

Permalink
Type checking and building on src and tests, unit tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed Dec 5, 2019
1 parent 658f9d5 commit 9aec5b7
Show file tree
Hide file tree
Showing 10 changed files with 2,563 additions and 3,918 deletions.
6,211 changes: 2,308 additions & 3,903 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions src/address.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* @flow */
import { c32checkEncode, c32checkDecode } from './checksum'
import base58check from 'base58check'
import * as base58check from 'base58check'

export const versions = {
mainnet: {
Expand All @@ -14,14 +13,14 @@ export const versions = {
}

// address conversion : bitcoin to stacks
const ADDR_BITCOIN_TO_STACKS = {}
const ADDR_BITCOIN_TO_STACKS: Record<number, number> = {}
ADDR_BITCOIN_TO_STACKS[0] = versions.mainnet.p2pkh
ADDR_BITCOIN_TO_STACKS[5] = versions.mainnet.p2sh
ADDR_BITCOIN_TO_STACKS[111] = versions.testnet.p2pkh
ADDR_BITCOIN_TO_STACKS[196] = versions.testnet.p2sh

// address conversion : stacks to bitcoin
const ADDR_STACKS_TO_BITCOIN = {}
const ADDR_STACKS_TO_BITCOIN: Record<number, number> = {}
ADDR_STACKS_TO_BITCOIN[versions.mainnet.p2pkh] = 0
ADDR_STACKS_TO_BITCOIN[versions.mainnet.p2sh] = 5
ADDR_STACKS_TO_BITCOIN[versions.testnet.p2pkh] = 111
Expand Down
5 changes: 5 additions & 0 deletions src/base58check.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module 'base58check' {
export function encode(data: string | Buffer, prefix?: string, encoding?: string): string;
export function decode(string: string): { prefix: Buffer; data: Buffer; };
export function decode(string: string, encoding: string): { prefix: string; data: string; };
}
4 changes: 1 addition & 3 deletions src/checksum.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* @flow */

import { c32encode, c32decode, c32normalize, c32 } from './encoding'
import crypto from 'crypto'
import * as crypto from 'crypto'

/**
* Get the c32check checksum of a hex-encoded string
Expand Down
2 changes: 0 additions & 2 deletions src/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* @flow */

export const c32 = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
const hex = '0123456789abcdef'

Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* @flow */

import {
c32encode,
c32decode,
Expand Down
11 changes: 11 additions & 0 deletions tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"module": "commonjs"
},
"include": [
"./unitTests/src/**/*",
"../src/**/*",
]
}
8 changes: 4 additions & 4 deletions tests/unitTests/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'tape-promise/tape'
import process from 'process'
import test = require('tape-promise/tape')
import * as process from 'process'
import {
c32encode,
c32decode,
Expand All @@ -9,7 +9,7 @@ import {
c32addressDecode,
c32ToB58,
b58ToC32
} from '../../../lib/index.js'
} from '../../../src/index'

export function c32encodingTests() {
const hexStrings = [
Expand Down Expand Up @@ -166,7 +166,7 @@ export function c32encodingTests() {
}

export function c32encodingRandomBytes() {
const testData = require('../data/random.json')
const testData: { hex: string; c32: string; }[] = require('../data/random.json')

test('c32encode', (t) => {
t.plan(testData.length)
Expand Down
218 changes: 218 additions & 0 deletions tests/unitTests/src/tape-promise.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
declare module 'tape-promise/tape' {

export = tape;

/**
* Create a new test with an optional name string and optional opts object.
* cb(t) fires with the new test object t once all preceeding tests have finished.
* Tests execute serially.
*/
function tape(name: string, cb: tape.TestCase): void;
function tape(name: string, opts: tape.TestOptions, cb: tape.TestCase): void;
function tape(cb: tape.TestCase): void;
function tape(opts: tape.TestOptions, cb: tape.TestCase): void;

interface TestCb { (t: tape.TestCase): Promise<any> }
function tape(name: string, cb: TestCb): void;


namespace tape {

interface TestCase {
(test: Test): void;
}

/**
* Available opts options for the tape function.
*/
interface TestOptions {
skip?: boolean; // See tape.skip.
timeout?: number; // Set a timeout for the test, after which it will fail. See tape.timeoutAfter.
}

/**
* Options for the createStream function.
*/
interface StreamOptions {
objectMode?: boolean;
}

/**
* Generate a new test that will be skipped over.
*/
export function skip(name: string, cb: tape.TestCase): void;
export function skip(name: string, opts: tape.TestOptions, cb: tape.TestCase): void;
export function skip(cb: tape.TestCase): void;
export function skip(opts: tape.TestOptions, cb: tape.TestCase): void;

/**
* The onFinish hook will get invoked when ALL tape tests have finished right before tape is about to print the test summary.
*/
export function onFinish(cb: () => void): void;

/**
* Like test(name?, opts?, cb) except if you use .only this is the only test case that will run for the entire process, all other test cases using tape will be ignored.
*/
export function only(name: string, cb: tape.TestCase): void;
export function only(name: string, opts: tape.TestOptions, cb: tape.TestCase): void;
export function only(cb: tape.TestCase): void;
export function only(opts: tape.TestOptions, cb: tape.TestCase): void;

/**
* Create a new test harness instance, which is a function like test(), but with a new pending stack and test state.
*/
export function createHarness(): typeof tape;
/**
* Create a stream of output, bypassing the default output stream that writes messages to console.log().
* By default stream will be a text stream of TAP output, but you can get an object stream instead by setting opts.objectMode to true.
*/
export function createStream(opts?: tape.StreamOptions): NodeJS.ReadableStream;

interface Test {
/**
* Create a subtest with a new test handle st from cb(st) inside the current test.
* cb(st) will only fire when t finishes.
* Additional tests queued up after t will not be run until all subtests finish.
*/
test(name: string, cb: tape.TestCase): void;
test(name: string, opts: TestOptions, cb: tape.TestCase): void;

/**
* Declare that n assertions should be run. end() will be called automatically after the nth assertion.
* If there are any more assertions after the nth, or after end() is called, they will generate errors.
*/
plan(n: number): void;

/**
* Declare the end of a test explicitly.
* If err is passed in t.end will assert that it is falsey.
*/
end(err?: any): void;

/**
* Generate a failing assertion with a message msg.
*/
fail(msg?: string): void;

/**
* Generate a passing assertion with a message msg.
*/
pass(msg?: string): void;

/**
* Automatically timeout the test after X ms.
*/
timeoutAfter(ms: number): void;

/**
* Generate an assertion that will be skipped over.
*/
skip(msg?: string): void;

/**
* Assert that value is truthy with an optional description message msg.
*/
ok(value: any, msg?: string): void;
true(value: any, msg?: string): void;
assert(value: any, msg?: string): void;

/**
* Assert that value is falsy with an optional description message msg.
*/
notOk(value: any, msg?: string): void;
false(value: any, msg?: string): void;
notok(value: any, msg?: string): void;

/**
* Assert that err is falsy.
* If err is non-falsy, use its err.message as the description message.
*/
error(err: any, msg?: string): void;
ifError(err: any, msg?: string): void;
ifErr(err: any, msg?: string): void;
iferror(err: any, msg?: string): void;

/**
* Assert that a === b with an optional description msg.
*/
equal(actual: any, expected: any, msg?: string): void;
equals(actual: any, expected: any, msg?: string): void;
isEqual(actual: any, expected: any, msg?: string): void;
is(actual: any, expected: any, msg?: string): void;
strictEqual(actual: any, expected: any, msg?: string): void;
strictEquals(actual: any, expected: any, msg?: string): void;

/**
* Assert that a !== b with an optional description msg.
*/
notEqual(actual: any, expected: any, msg?: string): void;
notEquals(actual: any, expected: any, msg?: string): void;
notStrictEqual(actual: any, expected: any, msg?: string): void;
notStrictEquals(actual: any, expected: any, msg?: string): void;
isNotEqual(actual: any, expected: any, msg?: string): void;
isNot(actual: any, expected: any, msg?: string): void;
not(actual: any, expected: any, msg?: string): void;
doesNotEqual(actual: any, expected: any, msg?: string): void;
isInequal(actual: any, expected: any, msg?: string): void;

/**
* Assert that a and b have the same structure and nested values using node's deepEqual() algorithm with strict comparisons (===) on leaf nodes and an optional description msg.
*/
deepEqual(actual: any, expected: any, msg?: string): void;
deepEquals(actual: any, expected: any, msg?: string): void;
isEquivalent(actual: any, expected: any, msg?: string): void;
same(actual: any, expected: any, msg?: string): void;

/**
* Assert that a and b do not have the same structure and nested values using node's deepEqual() algorithm with strict comparisons (===) on leaf nodes and an optional description msg.
*/
notDeepEqual(actual: any, expected: any, msg?: string): void;
notEquivalent(actual: any, expected: any, msg?: string): void;
notDeeply(actual: any, expected: any, msg?: string): void;
notSame(actual: any, expected: any, msg?: string): void;
isNotDeepEqual(actual: any, expected: any, msg?: string): void;
isNotDeeply(actual: any, expected: any, msg?: string): void;
isNotEquivalent(actual: any, expected: any, msg?: string): void;
isInequivalent(actual: any, expected: any, msg?: string): void;

/**
* Assert that a and b have the same structure and nested values using node's deepEqual() algorithm with loose comparisons (==) on leaf nodes and an optional description msg.
*/
deepLooseEqual(actual: any, expected: any, msg?: string): void;
looseEqual(actual: any, expected: any, msg?: string): void;
looseEquals(actual: any, expected: any, msg?: string): void;

/**
* Assert that a and b do not have the same structure and nested values using node's deepEqual() algorithm with loose comparisons (==) on leaf nodes and an optional description msg.
*/
notDeepLooseEqual(actual: any, expected: any, msg?: string): void;
notLooseEqual(actual: any, expected: any, msg?: string): void;
notLooseEquals(actual: any, expected: any, msg?: string): void;

/**
* Assert that the function call fn() throws an exception.
* expected, if present, must be a RegExp or Function, which is used to test the exception object.
*/
throws(fn: () => void, msg?: string): void;
throws(fn: () => void, exceptionExpected: RegExp | Function, msg?: string): void;

/**
* Assert that the function call fn() does not throw an exception.
*/
doesNotThrow(fn: () => void, msg?: string): void;
doesNotThrow(fn: () => void, exceptionExpected: RegExp | Function, msg?: string): void;

/**
* Print a message without breaking the tap output.
* (Useful when using e.g. tap-colorize where output is buffered & console.log will print in incorrect order vis-a-vis tap output.)
*/
comment(msg: string): void;

rejects(fn: Promise<any> | (() => Promise<any>), exceptionExpected: RegExp | Function, msg?: string): Promise<void>;

}
}


}

13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"declarationMap": true,
"outDir": "./lib",
"strict": true,
},
"include": [
"./src/**/*",
]
}

0 comments on commit 9aec5b7

Please sign in to comment.