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/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 diff --git a/package.json b/package.json index 753bb8e0ca..1e84fd3864 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", @@ -56,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/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..205df0b673 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 { SKIP_IDENTITY_REGISTRATION } from './env' 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,16 @@ export class BackendMiddleware { gateway: jolocomContractsGateway, }, }) + if (SKIP_IDENTITY_REGISTRATION) { + 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 { diff --git a/src/env.ts b/src/env.ts new file mode 100644 index 0000000000..f667401dca --- /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 || isTestE2E +export const SKIP_IDENTITY_REGISTRATION = isDev || isTestE2E 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 667da109ca..06c6f64cb4 100644 --- a/src/ui/registration/containers/entropy.tsx +++ b/src/ui/registration/containers/entropy.tsx @@ -1,3 +1,5 @@ +import { SKIP_ENTROPY_COLLECTION } from 'src/env' + import React from 'react' import { connect } from 'react-redux' import { registrationActions } from 'src/actions' @@ -10,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' @@ -58,7 +60,9 @@ export class EntropyContainer extends React.Component { } private updateEntropyProgress = async (): Promise => { - const { entropyProgress } = this.state + const entropyProgress = SKIP_ENTROPY_COLLECTION + ? 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() }) 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() 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"