ADD tests for typings

pull/17/head
pubkey 6 years ago
parent b8b983a50e
commit ba09d5a1e4
  1. 3
      .travis.yml
  2. 4
      package.json
  3. 106
      test/typings.test.js
  4. 117
      typings/index.d.ts

@ -14,4 +14,5 @@ script:
- npm run build
- npm run test:node
- travis_retry npm run test:browser
- npm run test:size
- npm run test:typings
- npm run test:size

@ -25,6 +25,7 @@
"test:node": "npm run build && mocha ./test/index.test.js -b --timeout 6000 --exit",
"test:browser": "npm run build && karma start ./config/karma.conf.js --single-run",
"test:size": "npm run build && rimraf test_tmp/browserify.js && browserify --no-builtins dist/lib/browserify.index.js > test_tmp/browserify.js && uglifyjs --compress --mangle --output test_tmp/browserify.min.js -- test_tmp/browserify.js && echo \"Build-Size (minified+gzip):\" && gzip-size --raw test_tmp/browserify.min.js",
"test:typings": "npm run build && mocha ./test/typings.test.js -b --timeout 12000 --exit",
"lint": "eslint --ignore-path .eslintignore src test config && solhint \"contracts/**/*.sol\"",
"clear": "rimraf -rf ./dist && rimraf -rf ./gen && rimraf -rf ./test_tmp",
"build:sol": "solidity-cli -i './contracts/*.sol' -o ./gen",
@ -47,6 +48,7 @@
},
"homepage": "https://github.com/pubkey/eth-crypto#readme",
"devDependencies": {
"@types/bn.js": "4.11.3",
"assert": "1.4.1",
"async-test-util": "1.6.1",
"babel-cli": "6.26.0",
@ -96,6 +98,8 @@
"rimraf": "2.6.2",
"solhint": "1.4.0",
"solidity-cli": "1.0.2",
"ts-node": "7.0.1",
"typescript": "3.1.6",
"uglify-es": "3.3.9",
"web3": "1.0.0-beta.34"
},

@ -0,0 +1,106 @@
/**
* checks if the typings are correct
* run via 'npm run test:typings'
*/
const assert = require('assert');
const path = require('path');
const AsyncTestUtil = require('async-test-util');
describe('typings.test.ts', () => {
const mainPath = path.join(__dirname, '../');
const codeBase = `
import EthCrypto from '${mainPath}';
import * as EthCryptoAll from '${mainPath}';
`;
const transpileCode = async (code) => {
const spawn = require('child-process-promise').spawn;
const stdout = [];
const stderr = [];
const promise = spawn('ts-node', [
'--no-cache',
'--compilerOptions', '{"target":"es6", "strict": true, "strictNullChecks": true, "noImplicitAny": true}',
//'--type-check',
'-p', codeBase + '\n' + code
]);
const childProcess = promise.childProcess;
childProcess.stdout.on('data', data => stdout.push(data.toString()));
childProcess.stderr.on('data', data => stderr.push(data.toString()));
try {
await promise;
} catch (err) {
throw new Error(`could not run
# Error: ${err}
# Output: ${stdout}
# ErrOut: ${stderr}
`);
}
};
describe('basic', () => {
it('should sucess on basic test', async () => {
await transpileCode('console.log("Hello, world!")');
});
it('should fail on broken code', async () => {
const brokenCode = `
let x: string = 'foo';
x = 1337;
`;
let thrown = false;
try {
await transpileCode(brokenCode);
} catch (err) {
thrown = true;
}
assert.ok(thrown);
});
});
describe('statics', () => {
describe('.createIdentity()', () => {
it('usage', async () => {
const code = `
(async()=>{
const ident = EthCrypto.createIdentity();
const privKey: string = ident.privateKey;
const ident2 = EthCryptoAll.createIdentity();
const privKey2: string = ident2.privateKey;
})();
`;
await transpileCode(code);
});
it('EthCryptoAll.createIdentity() fail on wrong access', async () => {
const code = `
(async()=>{
const ident = EthCryptoAll.createIdentity();
const privKey: string = ident.privateKeyFoobar;
})();
`;
await AsyncTestUtil.assertThrows(
() => transpileCode(code)
);
});
it('EthCrypto.createIdentity() fail on wrong access', async () => {
const code = `
(async()=>{
const ident = EthCrypto.createIdentity();
const privKey: string = ident.privateKeyFoobar;
})();
`;
await AsyncTestUtil.assertThrows(
() => transpileCode(code)
);
});
});
describe('.publicKey.compress()', () => {
it('usage', async () => {
const code = `
(async()=>{
const ident = EthCrypto.createIdentity();
const pub1: string = EthCrypto.publicKey.compress(ident.publicKey);
const pub2: string = EthCryptoAll.publicKey.compress(ident.publicKey);
})();
`;
await transpileCode(code);
});
});
});
});

117
typings/index.d.ts vendored

@ -1,18 +1,21 @@
import { BigNumber } from 'bn.js';
export function createIdentity(): {
type createIdentityType = () => {
privateKey: string,
publicKey: string,
address: string
};
}
export const createIdentity: createIdentityType;
export type publicKey = {
type publicKeyType = {
compress(publicKey: string): string;
decompress(publicKey: string): string;
toAddress(publicKey: string): string;
};
}
export const publicKey: publicKeyType;
export function publicKeyByPrivateKey(privateKey: string): string;
type publicKeyByPrivateKeyType = (privateKey: string) => string;
export const publicKeyByPrivateKey: publicKeyByPrivateKeyType;
export type Signature = {
v: string,
@ -36,101 +39,93 @@ export type RawTx = {
code?: string
};
export function sign(privateKey: string, message: string): string;
export function recover(sig: string, message: string): string;
export function recoverPublicKey(sig: string, message: string): string;
type signType = (privateKey: string, message: string) => string;
export const sign: signType;
type recoverType = (sig: string, message: string) => string;
export const recover: recoverType;
export type vrs = {
type recoverPublicKeyType = (sig: string, message: string) => string;
export const recoverPublicKey: recoverPublicKeyType;
type vrsType = {
fromString(hexString): Signature;
toString(sig: Signature): string;
};
export const vrs: vrsType;
type encryptWithPublicKeyType = (publicKey: string, message: string) => Promise<Encrypted>;
export const encryptWithPublicKey: encryptWithPublicKeyType;
export function encryptWithPublicKey(publicKey: string, message: string): Promise<Encrypted>;
export function decryptWithPrivateKey(privateKey: string, encrypted: Encrypted): Promise<string>;
type decryptWithPrivateKeyType = (privateKey: string, encrypted: Encrypted) => Promise<string>;
export const decryptWithPrivateKey: decryptWithPrivateKeyType;
export type cipher = {
type cipherType = {
stringify(encrypted: Encrypted): string;
parse(encrypted: string): Encrypted;
};
export const cipher: cipherType;
export function signTransaction(
type signTransactionType = (
rawTx: RawTx,
privateKey: string
): string;
) => string;
export const signTransaction: signTransactionType;
export function txDataByCompiled(
type txDataByCompiledType = (
abi: any,
bytecode: string,
args?: Array<string | number | BigNumber>
): string;
) => string;
export const txDataByCompiled: txDataByCompiledType;
export function calculateContractAddress(
type calculateContractAddressType = (
creatorAddress: string,
nonce: number
): string;
) => string;
export const calculateContractAddress: calculateContractAddressType;
export type TypedValue = {
value: string | Number | BigNumber,
type: 'string' | 'uint256' | 'int256' | 'bool' | 'bytes' | 'bytes32' | 'address'
};
export type hash = {
type hashType = {
keccak256(params: TypedValue[]): string;
};
export const hash: hashType;
export type util = {
type utilType = {
removeTrailing0x(str: string): string;
addTrailing0x(str: string): string;
};
export const util: utilType;
export type hex = {
type hexType = {
compress(hex: string, base64?: boolean): string;
decompress(str: string, base64?: boolean): string;
};
export const hex: hexType;
export function publicKeyToAddress(publicKey: string): string;
declare const _default: {
createIdentity: () => {
privateKey: string,
publicKey: string,
address: string
},
publicKey: {
compress(publicKey: string): string;
decompress(publicKey: string): string;
toAddress(publicKey: string): string;
},
decryptWithPrivateKey: (privateKey: string, encrypted: Encrypted) => Promise<string>,
encryptWithPublicKey: (publicKey: string, message: string)=> Promise<Encrypted>,
cipher: {
stringify(encrypted: Encrypted): string;
parse(encrypted: string): Encrypted;
},
publicKeyByPrivateKey: (privateKey: string)=> string,
recover: (sig: string, message: string) => string,
recoverPublicKey: (sig: string, message: string) => string,
sign: (privateKey: string, message: string) => string,
signTransaction: (rawTx: RawTx, privateKey: string) => string,
txDataByCompiled: (
abi: any,
bytecode: string,
args?: Array<string | number | BigNumber>
) => string,
calculateContractAddress: (creatorAddress: string, nonce: number) => string,
hash: (params: TypedValue[]) => string,
hex: {
compress(hex: string, base64?: boolean): string;
decompress(str: string, base64?: boolean): string;
},
vrs: {
fromString(hexString): Signature;
toString(sig: Signature): string;
},
util: {
removeTrailing0x(str: string): string;
addTrailing0x(str: string): string;
}
createIdentity: createIdentityType,
publicKey: publicKeyType,
encryptWithPublicKey: encryptWithPublicKeyType,
decryptWithPrivateKey: decryptWithPrivateKeyType,
cipher: cipherType,
signTransaction: signTransactionType,
txDataByCompiled: txDataByCompiledType,
publicKeyByPrivateKey: publicKeyByPrivateKeyType,
recover: recoverType,
recoverPublicKey: recoverPublicKeyType,
sign: signType,
calculateContractAddress: calculateContractAddressType,
hash: hashType,
hex: hexType,
vrs: vrsType,
util: utilType
};
export default _default;

Loading…
Cancel
Save