From ca0d0bb68b6af12be69cfac40d366c4fa8d27513 Mon Sep 17 00:00:00 2001 From: Mina Nagy Zaki Date: Wed, 14 Aug 2019 17:24:54 +0200 Subject: [PATCH 1/7] use NODE_ENV to configure app add babel-plugin-transform-inline-environment-variables use process.env['NODE_ENV'] in src/config.ts --- babel.config.js | 2 ++ package.json | 1 + src/config.ts | 7 +++++++ yarn.lock | 5 +++++ 4 files changed, 15 insertions(+) diff --git a/babel.config.js b/babel.config.js index 915af28877..69bd3c88e7 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,6 +1,8 @@ module.exports = { presets: ['module:metro-react-native-babel-preset'], plugins: [ + ["transform-inline-environment-variables"], + ["babel-plugin-inline-import", { "extensions": [ ".xml", diff --git a/package.json b/package.json index 753bb8e0ca..19288519da 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@typescript-eslint/parser": "^1.5.0", "babel-jest": "^24.8.0", "babel-plugin-inline-import": "^3.0.0", + "babel-plugin-transform-inline-environment-variables": "^0.4.3", "babel-plugin-transform-typescript-metadata": "^0.2.2", "case-sensitive-paths-webpack-plugin": "^2.1.2", "class-transformer": "^0.1.9", diff --git a/src/config.ts b/src/config.ts index f78f3cde34..edc0856947 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,12 @@ import typeOrmConf from '../ormconfig' +const env = process.env['NODE_ENV'] || 'development' +const isDev = env === 'development' +const isTest = env === 'test' + +export const skipEntropyCollection = isDev || isTest +export const skipIdentityRegisteration = isDev || isTest + export default { fuelingEndpoint: 'https://faucet.jolocom.com/request', typeOrmConfig: typeOrmConf, diff --git a/yarn.lock b/yarn.lock index 41dcd1182a..763ad41449 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1851,6 +1851,11 @@ babel-plugin-syntax-trailing-function-commas@^7.0.0-beta.0: resolved "https://registry.yarnpkg.com/babel-plugin-syntax-trailing-function-commas/-/babel-plugin-syntax-trailing-function-commas-7.0.0-beta.0.tgz#aa213c1435e2bffeb6fca842287ef534ad05d5cf" integrity sha512-Xj9XuRuz3nTSbaTXWv3itLOcxyF4oPD8douBBmj7U9BBC6nEBYfyOJYQMf/8PJAFotC62UY5dFfIGEPr7WswzQ== +babel-plugin-transform-inline-environment-variables@^0.4.3: + version "0.4.3" + resolved "https://registry.yarnpkg.com/babel-plugin-transform-inline-environment-variables/-/babel-plugin-transform-inline-environment-variables-0.4.3.tgz#a3b09883353be8b5e2336e3ff1ef8a5d93f9c489" + integrity sha1-o7CYgzU76LXiM24/8e+KXZP5xIk= + babel-plugin-transform-typescript-metadata@^0.2.2: version "0.2.2" resolved "https://registry.yarnpkg.com/babel-plugin-transform-typescript-metadata/-/babel-plugin-transform-typescript-metadata-0.2.2.tgz#fc44611187409ed9d5cb372ca2f85939a359cada" From ef5172a5a7b1446d48084d2a366b56736bedc417 Mon Sep 17 00:00:00 2001 From: Mina Nagy Zaki Date: Wed, 14 Aug 2019 17:25:41 +0200 Subject: [PATCH 2/7] implement skipIdentityCreation feature flag --- src/actions/registration/index.ts | 4 ++-- src/backendMiddleware.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/actions/registration/index.ts b/src/actions/registration/index.ts index 6949c5721f..bc612d7f8b 100644 --- a/src/actions/registration/index.ts +++ b/src/actions/registration/index.ts @@ -66,7 +66,7 @@ export const createIdentity = (encodedEntropy: string): ThunkAction => async ( dispatch(setIsRegistering(true)) - const { encryptionLib, keyChainLib, storageLib, registry } = backendMiddleware + const { encryptionLib, keyChainLib, storageLib, registry, fuelKeyWithEther } = backendMiddleware const password = await keyChainLib.getPassword() const encEntropy = encryptionLib.encryptWithPass({ @@ -81,7 +81,7 @@ export const createIdentity = (encodedEntropy: string): ThunkAction => async ( dispatch(setLoadingMsg(loading.loadingStages[1])) - await JolocomLib.util.fuelKeyWithEther( + await fuelKeyWithEther( userVault.getPublicKey({ encryptionPass: password, derivationPath: JolocomLib.KeyTypes.ethereumKey, diff --git a/src/backendMiddleware.ts b/src/backendMiddleware.ts index 080289107e..e8eb490b75 100644 --- a/src/backendMiddleware.ts +++ b/src/backendMiddleware.ts @@ -11,6 +11,7 @@ import { IpfsCustomConnector } from './lib/ipfs' import { jolocomContractsAdapter } from 'jolocom-lib/js/contracts/contractsAdapter' import { jolocomEthereumResolver } from 'jolocom-lib/js/ethereum/ethereum' import { jolocomContractsGateway } from 'jolocom-lib/js/contracts/contractsGateway' +import { skipIdentityRegisteration } from './config'; export class BackendMiddleware { public identityWallet!: IdentityWallet @@ -18,6 +19,7 @@ export class BackendMiddleware { public encryptionLib: EncryptionLibInterface public keyChainLib: KeyChainInterface public registry: IRegistry + public fuelKeyWithEther: typeof JolocomLib.util.fuelKeyWithEther public constructor(config: { fuelingEndpoint: string @@ -26,6 +28,7 @@ export class BackendMiddleware { this.storageLib = new Storage(config.typeOrmConfig) this.encryptionLib = new EncryptionLib() this.keyChainLib = new KeyChain() + this.fuelKeyWithEther = JolocomLib.util.fuelKeyWithEther this.registry = createJolocomRegistry({ ipfsConnector: new IpfsCustomConnector({ host: 'ipfs.jolocom.com', @@ -38,6 +41,15 @@ export class BackendMiddleware { gateway: jolocomContractsGateway, }, }) + if (skipIdentityRegisteration) { + this.registry.commit = this.fuelKeyWithEther = async (arg: any) => undefined + this.setIdentityWallet = async function( + userVault: SoftwareKeyProvider, + pass: string, + ): Promise { + this.identityWallet = await this.registry.create(userVault, pass) + } + } } public async initStorage(): Promise { From dbbc118db03c819b7313af30ee47938428c736c5 Mon Sep 17 00:00:00 2001 From: Mina Nagy Zaki Date: Wed, 14 Aug 2019 17:30:57 +0200 Subject: [PATCH 3/7] implement skipEntropyCollection feature flag --- src/ui/registration/containers/entropy.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ui/registration/containers/entropy.tsx b/src/ui/registration/containers/entropy.tsx index 667da109ca..11473a6486 100644 --- a/src/ui/registration/containers/entropy.tsx +++ b/src/ui/registration/containers/entropy.tsx @@ -1,3 +1,4 @@ +import { skipEntropyCollection } from 'src/config' import React from 'react' import { connect } from 'react-redux' import { registrationActions } from 'src/actions' @@ -58,7 +59,10 @@ export class EntropyContainer extends React.Component { } private updateEntropyProgress = async (): Promise => { - const { entropyProgress } = this.state + let { entropyProgress } = this.state + if (skipEntropyCollection) { + entropyProgress = 1 + } if (entropyProgress >= 1) { this.setState({ sufficientEntropy: true, entropyProgress: 1 }) while (this.entropyGenerator.getProgress() < 1) { From 7d3cca5e4867fa47fabbc99921de8f67edacdc6a Mon Sep 17 00:00:00 2001 From: Mina Nagy Zaki Date: Thu, 15 Aug 2019 16:57:03 +0200 Subject: [PATCH 4/7] Update tests - run unit tests under proper NODE_ENV - fix fuelKeyWithEther call/test - minor typescript issues --- package.json | 4 ++-- src/config.ts | 6 ++++-- src/lib/storage/entities/verifiableCredentialEntity.ts | 1 - src/ui/registration/containers/entropy.tsx | 6 ++---- tests/actions/registration/index.test.ts | 7 ++----- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 19288519da..1e84fd3864 100644 --- a/package.json +++ b/package.json @@ -57,8 +57,8 @@ }, "scripts": { "start": "adb reverse tcp:8081 tcp:8081 & node node_modules/react-native/local-cli/cli.js start", - "test": "jest", - "test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand", + "test": "NODE_ENV=test jest", + "test:debug": "NODE_ENV=test node --inspect-brk node_modules/.bin/jest --runInBand", "run:ios": "react-native run-ios", "run:android": "react-native run-android --appIdSuffix debug", "run:android:staging": "react-native run-android --variant staging --appIdSuffix debugStaging", diff --git a/src/config.ts b/src/config.ts index edc0856947..840a003145 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,10 +2,12 @@ import typeOrmConf from '../ormconfig' const env = process.env['NODE_ENV'] || 'development' const isDev = env === 'development' +// @ts-ignore const isTest = env === 'test' +const isTestE2E = env === 'test-e2e' -export const skipEntropyCollection = isDev || isTest -export const skipIdentityRegisteration = isDev || isTest +export const skipEntropyCollection = isDev || isTest || isTestE2E +export const skipIdentityRegisteration = isDev || isTestE2E export default { fuelingEndpoint: 'https://faucet.jolocom.com/request', diff --git a/src/lib/storage/entities/verifiableCredentialEntity.ts b/src/lib/storage/entities/verifiableCredentialEntity.ts index abab11ef9b..8f2dd4fc59 100644 --- a/src/lib/storage/entities/verifiableCredentialEntity.ts +++ b/src/lib/storage/entities/verifiableCredentialEntity.ts @@ -5,7 +5,6 @@ import { OneToMany, ManyToOne, } from 'typeorm/browser' - import { Exclude, Expose, plainToClass, classToPlain } from 'class-transformer' import { SignedCredential } from 'jolocom-lib/js/credentials/signedCredential/signedCredential' import { ISignedCredentialAttrs } from 'jolocom-lib/js/credentials/signedCredential/types' diff --git a/src/ui/registration/containers/entropy.tsx b/src/ui/registration/containers/entropy.tsx index 11473a6486..2f3ca224b6 100644 --- a/src/ui/registration/containers/entropy.tsx +++ b/src/ui/registration/containers/entropy.tsx @@ -59,10 +59,8 @@ export class EntropyContainer extends React.Component { } private updateEntropyProgress = async (): Promise => { - let { entropyProgress } = this.state - if (skipEntropyCollection) { - entropyProgress = 1 - } + const entropyProgress = skipEntropyCollection ? 1 : this.state.entropyProgress + if (entropyProgress >= 1) { this.setState({ sufficientEntropy: true, entropyProgress: 1 }) while (this.entropyGenerator.getProgress() < 1) { diff --git a/tests/actions/registration/index.test.ts b/tests/actions/registration/index.test.ts index f990645a4c..80148efda2 100644 --- a/tests/actions/registration/index.test.ts +++ b/tests/actions/registration/index.test.ts @@ -1,6 +1,5 @@ import { registrationActions } from 'src/actions' import data from './data/mockRegistrationData' -import { JolocomLib } from 'jolocom-lib' import * as util from 'src/lib/util' import { withErrorScreen } from 'src/actions/modifiers' import { AppError, ErrorCode } from 'src/lib/errors' @@ -79,9 +78,6 @@ describe('Registration action creators', () => { it('should attempt to create an identity', async () => { MockDate.set(new Date(946681200000)) const { getPasswordResult, cipher, entropy, identityWallet } = data - const fuelSpy = jest - .spyOn(JolocomLib.util, 'fuelKeyWithEther') - .mockResolvedValueOnce(null) const mockMiddleware = { identityWallet, @@ -107,6 +103,7 @@ describe('Registration action creators', () => { create: () => identityWallet, }, setIdentityWallet: jest.fn(() => Promise.resolve()), + fuelKeyWithEther: jest.fn().mockResolvedValueOnce(null) } const mockState: Partial = { @@ -134,7 +131,7 @@ describe('Registration action creators', () => { expect( mockMiddleware.storageLib.store.derivedKey.mock.calls, ).toMatchSnapshot() - expect(fuelSpy.mock.calls).toMatchSnapshot() + expect(mockMiddleware.fuelKeyWithEther.mock.calls).toMatchSnapshot() MockDate.reset() }) From 3dd325834bcf67a3fc22b1a2528548c90dc37971 Mon Sep 17 00:00:00 2001 From: Mina Nagy Zaki Date: Thu, 15 Aug 2019 16:57:55 +0200 Subject: [PATCH 5/7] jest: fix setup for typeorm to be importable in tests It needs to be transpiled, but also the global window variable was breaking this code: https://github.com/typeorm/typeorm/blob/e4de0403e1f666639c0198e216409f997121c1e8/src/platform/BrowserPlatformTools.template#L20 Also add ConnectionOptions type back to the config object --- jest.config.js | 3 +-- ormconfig.ts | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/jest.config.js b/jest.config.js index a20da47a09..437d07f439 100644 --- a/jest.config.js +++ b/jest.config.js @@ -7,10 +7,9 @@ module.exports = { "enzyme-to-json/serializer" ], transformIgnorePatterns: [ - "node_modules/(?!react-native|native-base|@?react-navigation|react-native-fabric)" + "node_modules/(?!react-native|native-base|@?react-navigation|react-native-fabric|typeorm)" ], globals: { - window: true, "ts-jest": { babelConfig: true } diff --git a/ormconfig.ts b/ormconfig.ts index db250c15e4..ce99502024 100644 --- a/ormconfig.ts +++ b/ormconfig.ts @@ -1,7 +1,8 @@ import { entityList } from './src/lib/storage/entities' import { Initial1565886000404 } from './src/lib/storage/migration/1565886000404-initial' +import { ConnectionOptions } from 'typeorm/browser' -export default { +const typeOrmConfig: ConnectionOptions = { type: 'react-native', database: 'LocalSmartWalletData', location: 'default', @@ -14,3 +15,5 @@ export default { migrationsDir: 'src/lib/storage/migration', }, } + +export default typeOrmConfig From 9448d622033c96a3fae630e01fa11a54dc3bab76 Mon Sep 17 00:00:00 2001 From: Mina Nagy Zaki Date: Tue, 20 Aug 2019 16:39:46 +0200 Subject: [PATCH 6/7] move environment based constants to src/env.ts --- src/backendMiddleware.ts | 7 ++++--- src/config.ts | 9 --------- src/env.ts | 8 ++++++++ src/ui/registration/containers/entropy.tsx | 10 ++++++---- 4 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 src/env.ts diff --git a/src/backendMiddleware.ts b/src/backendMiddleware.ts index e8eb490b75..205df0b673 100644 --- a/src/backendMiddleware.ts +++ b/src/backendMiddleware.ts @@ -11,7 +11,7 @@ import { IpfsCustomConnector } from './lib/ipfs' import { jolocomContractsAdapter } from 'jolocom-lib/js/contracts/contractsAdapter' import { jolocomEthereumResolver } from 'jolocom-lib/js/ethereum/ethereum' import { jolocomContractsGateway } from 'jolocom-lib/js/contracts/contractsGateway' -import { skipIdentityRegisteration } from './config'; +import { SKIP_IDENTITY_REGISTRATION } from './env' export class BackendMiddleware { public identityWallet!: IdentityWallet @@ -41,8 +41,9 @@ export class BackendMiddleware { gateway: jolocomContractsGateway, }, }) - if (skipIdentityRegisteration) { - this.registry.commit = this.fuelKeyWithEther = async (arg: any) => undefined + if (SKIP_IDENTITY_REGISTRATION) { + this.registry.commit = this.fuelKeyWithEther = async (arg: any) => + undefined this.setIdentityWallet = async function( userVault: SoftwareKeyProvider, pass: string, diff --git a/src/config.ts b/src/config.ts index 840a003145..f78f3cde34 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,14 +1,5 @@ import typeOrmConf from '../ormconfig' -const env = process.env['NODE_ENV'] || 'development' -const isDev = env === 'development' -// @ts-ignore -const isTest = env === 'test' -const isTestE2E = env === 'test-e2e' - -export const skipEntropyCollection = isDev || isTest || isTestE2E -export const skipIdentityRegisteration = isDev || isTestE2E - export default { fuelingEndpoint: 'https://faucet.jolocom.com/request', typeOrmConfig: typeOrmConf, diff --git a/src/env.ts b/src/env.ts new file mode 100644 index 0000000000..4b0b40445a --- /dev/null +++ b/src/env.ts @@ -0,0 +1,8 @@ +const env = process.env['NODE_ENV'] || 'development' +const isDev = env === 'development' +// @ts-ignore unused so far +const isTest = env === 'test' +const isTestE2E = env === 'test-e2e' + +export const SKIP_ENTROPY_COLLECTION = isDev || isTest || isTestE2E +export const SKIP_IDENTITY_REGISTRATION = isDev || isTestE2E diff --git a/src/ui/registration/containers/entropy.tsx b/src/ui/registration/containers/entropy.tsx index 2f3ca224b6..06c6f64cb4 100644 --- a/src/ui/registration/containers/entropy.tsx +++ b/src/ui/registration/containers/entropy.tsx @@ -1,4 +1,5 @@ -import { skipEntropyCollection } from 'src/config' +import { SKIP_ENTROPY_COLLECTION } from 'src/env' + import React from 'react' import { connect } from 'react-redux' import { registrationActions } from 'src/actions' @@ -11,7 +12,7 @@ import { import { generateSecureRandomBytes } from 'src/lib/util' import { withErrorScreen } from 'src/actions/modifiers' import { ThunkDispatch } from 'src/store' -import { AppError, ErrorCode } from '../../../lib/errors' +import { AppError, ErrorCode } from 'src/lib/errors' import { routeList } from 'src/routeList' import { StatusBar } from 'react-native' @@ -59,8 +60,9 @@ export class EntropyContainer extends React.Component { } private updateEntropyProgress = async (): Promise => { - const entropyProgress = skipEntropyCollection ? 1 : this.state.entropyProgress - + const entropyProgress = SKIP_ENTROPY_COLLECTION + ? 1 + : this.state.entropyProgress if (entropyProgress >= 1) { this.setState({ sufficientEntropy: true, entropyProgress: 1 }) while (this.entropyGenerator.getProgress() < 1) { From 92ee5ca56fac746ea89e79cd294520374e783014 Mon Sep 17 00:00:00 2001 From: Mina Nagy Zaki Date: Tue, 20 Aug 2019 16:35:11 +0200 Subject: [PATCH 7/7] tests/containers/entropy: fix issue because of SKIP_ENTROPY_COLLECTION --- src/env.ts | 2 +- tests/ui/containers/entropy.test.tsx | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/env.ts b/src/env.ts index 4b0b40445a..f667401dca 100644 --- a/src/env.ts +++ b/src/env.ts @@ -4,5 +4,5 @@ const isDev = env === 'development' const isTest = env === 'test' const isTestE2E = env === 'test-e2e' -export const SKIP_ENTROPY_COLLECTION = isDev || isTest || isTestE2E +export const SKIP_ENTROPY_COLLECTION = isDev || isTestE2E export const SKIP_IDENTITY_REGISTRATION = isDev || isTestE2E diff --git a/tests/ui/containers/entropy.test.tsx b/tests/ui/containers/entropy.test.tsx index 5c480cf10c..c57e72c930 100644 --- a/tests/ui/containers/entropy.test.tsx +++ b/tests/ui/containers/entropy.test.tsx @@ -8,9 +8,9 @@ import { stub, reveal } from 'tests/utils' describe('Entropy container', () => { const mockInstanceGenerator = ( instance: EntropyContainer, - progressValues = [0], + progressValues: number[] | null = null, ) => { - const getProgress = jest.fn().mockReturnValue(progressValues[0]) + const getProgress = jest.fn() // @ts-ignore private const generatorMock = (instance.entropyGenerator = stub({ getProgress, @@ -18,7 +18,14 @@ describe('Entropy container', () => { generateRandomString: jest.fn().mockReturnValue('randomString'), })) - progressValues.forEach(v => getProgress.mockReturnValueOnce(v)) + if (progressValues) { + getProgress.mockReturnValue(progressValues[0]) + progressValues.forEach(v => getProgress.mockReturnValueOnce(v)) + } else { + getProgress.mockImplementation(() => { + throw new Error("shouldn't get here!") + }) + } return generatorMock } @@ -45,7 +52,7 @@ describe('Entropy container', () => { it('correctly handles added points with the entropy generator', () => { const rendered = shallow() const instance = rendered.instance() as EntropyContainer - const generatorMock = mockInstanceGenerator(instance) + const generatorMock = mockInstanceGenerator(instance, [0]) expect(rendered.state()).toMatchSnapshot()