Skip to content

Commit

Permalink
feat: build update for common, encryption and storage packages
Browse files Browse the repository at this point in the history
  • Loading branch information
yknl committed Oct 8, 2020
1 parent 16ea055 commit fd71e71
Show file tree
Hide file tree
Showing 26 changed files with 322 additions and 261 deletions.
34 changes: 21 additions & 13 deletions packages/common/package.json
Expand Up @@ -5,9 +5,6 @@
"author": "yknl <yukanliao@gmail.com>",
"homepage": "https://blockstack.org",
"license": "GPL-3.0-or-later",
"main": "./dist/index.js",
"umd:main": "./dist/common.umd.production.js",
"module": "./dist/common.esm.js",
"directories": {
"lib": "lib",
"dist": "dist",
Expand All @@ -24,12 +21,17 @@
"url": "git+https://github.com/blockstack/blockstack.js.git"
},
"scripts": {
"build": "cross-env NODE_ENV=production tsdx build --format=cjs,esm,umd",
"build-all": "run-p build:*",
"build:cjs": "tsc --outDir ./lib -m commonjs -t es2017",
"build:esm": "tsc --outDir ./lib-esm -m es6 -t es2017",
"build:cjs:watch": "tsc --outDir ./lib -m commonjs -t es2017 --watch",
"build:esm:watch": "tsc --outDir ./lib-esm -m es6 -t es2017 --watch",
"dev": "yarn start",
"lint": "yarn lint:eslint && yarn lint:prettier",
"lint:eslint": "eslint \"src/**/*.{ts,tsx}\" -f unix",
"lint:fix": "eslint \"src/**/*.{ts,tsx}\" -f unix --fix",
"lint:prettier": "prettier --check \"src/**/*.{ts,tsx}\" *.js",
"lint:prettier:fix": "prettier --write \"src/**/*.{ts,tsx}\" *.js",
"start": "tsdx watch --verbose --noClean --onSuccess yalc publish --push",
"build": "tsdx build --format cjs,esm,umd",
"typecheck": "tsc --noEmit",
"typecheck:watch": "npm run typecheck -- --watch",
"prepublishOnly": "yarn build",
"test": "jest",
"test:watch": "jest --watch --coverage=false",
"codecovUpload": "codecov"
Expand All @@ -40,12 +42,18 @@
"dependencies": {
"bitcoinjs-lib": "^5.1.10",
"codecov": "^3.7.2",
"cross-fetch": "^3.0.5",
"tsdx": "^0.13.3"
"cross-fetch": "^3.0.5"
},
"devDependencies": {
"@types/jest": "24.9.0",
"jest": "^24.9.0",
"jest-fetch-mock": "^3.0.3"
}
"jest-fetch-mock": "^3.0.3",
"tsdx": "^0.14.0"
},
"sideEffects": false,
"main": "dist/index.js",
"module": "dist/index.esm.js",
"typings": "dist/auth/src/index.d.ts",
"umd:main": "dist/auth.umd.production.js",
"unpkg": "dist/auth.cjs.production.min.js"
}
6 changes: 3 additions & 3 deletions packages/common/src/errors.ts
Expand Up @@ -75,7 +75,7 @@ export class BlockstackError extends Error {
*/
export class InvalidParameterError extends BlockstackError {
constructor(parameter: string, message: string = '') {
super({ code: ERROR_CODES.MISSING_PARAMETER, message, parameter: '' });
super({ code: ERROR_CODES.MISSING_PARAMETER, message, parameter });
this.name = 'MissingParametersError';
}
}
Expand Down Expand Up @@ -301,8 +301,8 @@ export class PayloadTooLargeError extends GaiaHubError {

maxUploadByteSize: number;

constructor(message: string, response: GaiaHubErrorResponse, maxUploadByteSize: number) {
super({ message, code: ERROR_CODES.PAYLOAD_TOO_LARGE_ERROR }, response);
constructor(message: string, response: GaiaHubErrorResponse | null, maxUploadByteSize: number) {
super({ message, code: ERROR_CODES.PAYLOAD_TOO_LARGE_ERROR }, response!);
this.name = 'PayloadTooLargeError';
this.maxUploadByteSize = maxUploadByteSize;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/utils.ts
Expand Up @@ -91,11 +91,11 @@ export function updateQueryStringParameter(uri: string, key: string, value: stri
*/

export function isLaterVersion(v1: string, v2: string) {
if (v1 === undefined) {
if (v1 === undefined || v1 === '') {
v1 = '0.0.0';
}

if (v2 === undefined) {
if (v2 === undefined || v1 === '') {
v2 = '0.0.0';
}

Expand Down
37 changes: 1 addition & 36 deletions packages/common/tests/utils.test.ts
@@ -1,42 +1,7 @@
// import { SECP256K1Client } from 'jsontokens'
// import {
// getEntropy, makeECPrivateKey, publicKeyToAddress, isSameOriginAbsoluteUrl,
// ecPairToHexString, hexStringToECPair, ecPairToAddress, isLaterVersion
// } from '../src'

import { isSameOriginAbsoluteUrl, isLaterVersion } from '../src'

// test('makeECPrivateKey', () => {
// t.plan(5)

// const entropy = getEntropy(32)
// t.ok(entropy, 'Entropy should have been created')

// const privateKey = makeECPrivateKey()
// t.ok(privateKey, 'Private key should have been created')
// t.equal(typeof privateKey, 'string', 'Private key should be a string')

// const publicKey = SECP256K1Client.derivePublicKey(privateKey)

// const address = publicKeyToAddress(publicKey)
// t.ok(address, 'Address should have been created')
// t.equal(typeof address, 'string', 'Address should be a string')
// })

// test('ecPairToHexString', (t) => {
// t.plan(2)

// const privateKey = '00cdce6b5f87d38f2a830cae0da82162e1b487f07c5affa8130f01fe1a2a25fb01'
// const expectedAddress = '1WykMawQRnLh7SWmmoRL4qTDNCgAsVRF1'

// const computedECPair = hexStringToECPair(privateKey)
// t.equal(privateKey, ecPairToHexString(computedECPair), 'Should return same hex string')

// t.equal(expectedAddress, ecPairToAddress(computedECPair), 'Should parse to correct address')
// })

test('isLaterVersion', () => {
expect(isLaterVersion(undefined, '1.1.0')).toEqual(false)
expect(isLaterVersion('', '1.1.0')).toEqual(false)
expect(isLaterVersion('1.2.0', '1.1.0')).toEqual(true)
expect(isLaterVersion('1.1.0', '1.1.0')).toEqual(true)
expect(isLaterVersion('1.1.0', '1.2.0')).toEqual(false)
Expand Down
52 changes: 46 additions & 6 deletions packages/common/tsconfig.json
@@ -1,7 +1,47 @@
{
"extends": "../../tsconfig.json",
"include": [
"./src/**/*",
"./tests/**/*"
]
}
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["dom", "esnext"],
"jsx": "react",
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"strict": true,
"pretty": true,
"removeComments": true,
"noImplicitAny": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"alwaysStrict": true,
"stripInternal": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"emitDecoratorMetadata": false,
"noEmit": false,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"suppressImplicitAnyIndexErrors": false,
"outDir": "./dist",
"baseUrl": "./src",
"skipLibCheck": true,
"esModuleInterop": true,
"strictNullChecks": true,
"paths": {
"@stacks/auth": ["../../auth/src"],
"@stacks/cli": ["../../cli/src"],
"@stacks/common": ["../../common/src"],
"@stacks/encryption": ["../../encryption/src"],
"@stacks/keychain": ["../../keychain/src"],
"@stacks/network": ["../../network/src"],
"@stacks/profile": ["../../profile/src"],
"@stacks/storage": ["../../storage/src"],
"@stacks/transactions": ["../../transactions/src"]
}
},
"include": ["src/**/*", "tests/**/*"]
}
15 changes: 15 additions & 0 deletions packages/common/tsdx.config.js
@@ -0,0 +1,15 @@
module.exports = {
rollup(config, options) {
if (options.format === 'esm') {
config.output = {
...config.output,
dir: 'dist/',
entryFileNames: '[name].esm.js',
preserveModules: true,
preserveModulesRoot: 'src',
};
delete config.output.file;
}
return config;
},
};
34 changes: 21 additions & 13 deletions packages/encryption/package.json
Expand Up @@ -5,9 +5,6 @@
"author": "yknl <yukanliao@gmail.com>",
"homepage": "https://blockstack.org",
"license": "GPL-3.0-or-later",
"main": "./dist/index.js",
"umd:main": "./dist/common.umd.production.js",
"module": "./dist/common.esm.js",
"directories": {
"lib": "lib",
"dist": "dist",
Expand All @@ -21,12 +18,17 @@
"url": "git+https://github.com/blockstack/blockstack.js.git"
},
"scripts": {
"build": "cross-env NODE_ENV=production tsdx build --format=cjs,esm,umd",
"build-all": "run-p build:*",
"build:cjs": "tsc --outDir ./lib -m commonjs -t es2017",
"build:esm": "tsc --outDir ./lib-esm -m es6 -t es2017",
"build:cjs:watch": "tsc --outDir ./lib -m commonjs -t es2017 --watch",
"build:esm:watch": "tsc --outDir ./lib-esm -m es6 -t es2017 --watch",
"dev": "yarn start",
"lint": "yarn lint:eslint && yarn lint:prettier",
"lint:eslint": "eslint \"src/**/*.{ts,tsx}\" -f unix",
"lint:fix": "eslint \"src/**/*.{ts,tsx}\" -f unix --fix",
"lint:prettier": "prettier --check \"src/**/*.{ts,tsx}\" *.js",
"lint:prettier:fix": "prettier --write \"src/**/*.{ts,tsx}\" *.js",
"start": "tsdx watch --verbose --noClean --onSuccess yalc publish --push",
"build": "tsdx build --format cjs,esm,umd",
"typecheck": "tsc --noEmit",
"typecheck:watch": "npm run typecheck -- --watch",
"prepublishOnly": "yarn build",
"test": "jest",
"test:watch": "jest --watch --coverage=false",
"codecovUpload": "codecov"
Expand All @@ -43,12 +45,18 @@
"elliptic": "^6.5.2",
"randombytes": "^2.1.0",
"ripemd160-min": "^0.0.6",
"sha.js": "^2.4.11",
"tsdx": "^0.13.3"
"sha.js": "^2.4.11"
},
"devDependencies": {
"@types/jest": "24.9.0",
"jest": "^24.9.0",
"jest-fetch-mock": "^3.0.3"
}
"jest-fetch-mock": "^3.0.3",
"tsdx": "^0.14.0"
},
"sideEffects": false,
"main": "dist/index.js",
"module": "dist/index.esm.js",
"typings": "dist/auth/src/index.d.ts",
"umd:main": "dist/auth.umd.production.js",
"unpkg": "dist/auth.cjs.production.min.js"
}
4 changes: 1 addition & 3 deletions packages/encryption/src/cryptoRandom.ts
@@ -1,6 +1,4 @@
import * as randombytes_ from 'randombytes';

let randombytes = randombytes_;
import randombytes from 'randombytes';

export { randombytes as randomBytes };

Expand Down
4 changes: 2 additions & 2 deletions packages/encryption/src/encryption.ts
Expand Up @@ -60,7 +60,7 @@ export async function encryptContent(
options?: EncryptContentOptions
): Promise<string> {
const opts = Object.assign({}, options);
let privateKey: string;
let privateKey: string | undefined;
if (!opts.publicKey) {
if (!opts.privateKey) {
throw new Error('Either public key or private key must be supplied for encryption.');
Expand All @@ -87,7 +87,7 @@ export async function encryptContent(
} else if (!privateKey) {
privateKey = opts.privateKey;
}
const signatureObject = signECDSA(privateKey, cipherPayload);
const signatureObject = signECDSA(privateKey!, cipherPayload);
const signedCipherObject: SignedCipherObject = {
signature: signatureObject.signature,
publicKey: signatureObject.publicKey,
Expand Down
4 changes: 2 additions & 2 deletions packages/encryption/src/keys.ts
Expand Up @@ -21,7 +21,7 @@ export function getEntropy(arg: number): Buffer {
*/
export function makeECPrivateKey() {
const keyPair = ECPair.makeRandom({ rng: getEntropy });
return keyPair.privateKey.toString('hex');
return keyPair.privateKey!.toString('hex');
}

/**
Expand Down Expand Up @@ -77,7 +77,7 @@ export function hexStringToECPair(skHex: string, network?: Network): ECPair.ECPa
* @ignore
*/
export function ecPairToHexString(secretKey: ECPair.ECPairInterface) {
const ecPointHex = secretKey.privateKey.toString('hex');
const ecPointHex = secretKey.privateKey!.toString('hex');
if (secretKey.compressed) {
return `${ecPointHex}01`;
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/encryption/src/wallet.ts
Expand Up @@ -120,20 +120,20 @@ async function decryptMnemonicBuffer(dataBuffer: Buffer, password: string): Prom
function decryptLegacy(
dataBuffer: Buffer,
password: string,
triplesecDecrypt: TriplesecDecryptSignature
triplesecDecrypt?: TriplesecDecryptSignature
): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
if (!triplesecDecrypt) {
reject(new Error('The `triplesec.decrypt` function must be provided'));
}
triplesecDecrypt(
triplesecDecrypt!(
{
key: Buffer.from(password),
data: dataBuffer,
},
(err, plaintextBuffer) => {
if (!err) {
resolve(plaintextBuffer);
resolve(plaintextBuffer!);
} else {
reject(err);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/encryption/tests/encryption.test.ts
Expand Up @@ -11,7 +11,7 @@ import * as aesCipher from '../src/aesCipher'
import * as sha2Hash from '../src/sha2Hash'
import * as hmacSha256 from '../src/hmacSha256'
import * as ripemd160 from '../src/hashRipemd160'
import * as BN from 'bn.js'
import BN from 'bn.js'
import { getBufferFromBN } from '../src/ec'

const privateKey = 'a5c61c6ca7b3e7e55edee68566aeab22e4da26baa285c7bd10e8d2218aa3b229'
Expand Down
50 changes: 45 additions & 5 deletions packages/encryption/tsconfig.json
@@ -1,7 +1,47 @@
{
"extends": "../../tsconfig.json",
"include": [
"./src/**/*",
"./tests/**/*"
]
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["dom", "esnext"],
"jsx": "react",
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"strict": true,
"pretty": true,
"removeComments": true,
"noImplicitAny": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"alwaysStrict": true,
"stripInternal": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"emitDecoratorMetadata": false,
"noEmit": false,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"suppressImplicitAnyIndexErrors": false,
"outDir": "./dist",
"baseUrl": "./src",
"skipLibCheck": true,
"esModuleInterop": true,
"strictNullChecks": true,
"paths": {
"@stacks/auth": ["../../auth/src"],
"@stacks/cli": ["../../cli/src"],
"@stacks/common": ["../../common/src"],
"@stacks/encryption": ["../../encryption/src"],
"@stacks/keychain": ["../../keychain/src"],
"@stacks/network": ["../../network/src"],
"@stacks/profile": ["../../profile/src"],
"@stacks/storage": ["../../storage/src"],
"@stacks/transactions": ["../../transactions/src"]
}
},
"include": ["src/**/*", "tests/**/*"]
}

0 comments on commit fd71e71

Please sign in to comment.