Skip to content

Commit

Permalink
Merge pull request #16 from VirgilSecurity/feature/storage-adapter
Browse files Browse the repository at this point in the history
Feature/storage adapter
  • Loading branch information
Alexey Smirnov authored Jan 10, 2019
2 parents 9e849ee + b572346 commit 49a5c32
Show file tree
Hide file tree
Showing 73 changed files with 25,227 additions and 90 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ const decryptedMsg = await eThree.decrypt(encryptedMsg, senderPublicKey);
```
You can find more examples in [examples folder](example) and on https://e3kit.readme.io.


### React Native usage

This package works with https://github.com/VirgilSecurity/virgil-key-storage-rn

```
import { EThree } from '@virgilsecurity/e3kit';
import createNativeKeyEntryStorage from '@virgilsecurity/key-storage-rn/native';
// or
import createExpoKeyEntryStorage from '@virgilsecurity/key-storage-rn/expo';
const keyEntryStorage = createNativeKeyEntryStorage();
// or
const keyEntryStorage = createExpoKeyEntryStorage();
EThree.initialize(getTokenCallback, { keyEntryStorage });
```

## Docs
Virgil Security has a powerful set of APIs, and the documentation below can get you started today.

Expand Down
35 changes: 35 additions & 0 deletions config/paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const path = require('path');
const noPythiaCryptoPath = require.resolve('virgil-crypto');
const pythiaCryptoPath = path.resolve(noPythiaCryptoPath, '../virgil-crypto-pythia.es.js');
const pythiaCryptoBrowserPath = path.resolve(noPythiaCryptoPath, '../virgil-crypto-pythia.browser.es.js');

class Paths {
constructor() {
this.PLATFORM = process.env.PLATFORM || 'node';
this.IS_BROWSER = this.PLATFORM === 'browser';
this.NAME = 'e3kit';

this.input = path.join(process.cwd(), 'src', 'index.ts');
this.outputDir = path.join(process.cwd(), 'dist');

this.formats = {
umd: 'umd',
es: 'es',
cjs: 'cjs'
}
this.pythiaCryptoPath = this.IS_BROWSER ? pythiaCryptoBrowserPath : pythiaCryptoPath;
}

getFileName(format) {
const parts = [this.NAME, this.PLATFORM, format];

if (format === this.formats.umd) {
parts.push('min');
}
const ext = (format === this.formats.es && this.PLATFORM === 'node') ? 'mjs' : 'js';
parts.push(ext);
return parts.join('.');
}
}

module.exports = new Paths();
40 changes: 40 additions & 0 deletions config/rollup-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const paths = require('./paths');

const commonjs = require('rollup-plugin-commonjs');
const inject = require('rollup-plugin-inject');
const nodeGlobals = require('rollup-plugin-node-globals');
const resolve = require('rollup-plugin-node-resolve');
const typescript = require('rollup-plugin-typescript2');
const { uglify } = require('rollup-plugin-uglify');
const sourcemap = require('rollup-plugin-sourcemaps');


function resolveVirgilCrypto () {
return {
name: 'resolve-virgil-crypto',
resolveId (importee, b) {
if (importee === 'virgil-crypto') {
return paths.pythiaCryptoPath;
}
return null;
}
}
}

class RollupPluginsResolver {

constructor() {
this.commonjs = commonjs;
this.nodeGlobals = nodeGlobals;
this.resolve = resolve;
this.sourcemap = sourcemap;
this.uglify = uglify;
this.resolveVirgilCrypto = resolveVirgilCrypto;
this.typescriptResolved = typescript({
exclude: ['**/*.test.ts', '**/*.spec.ts', '**/__mocks__/*.ts'],
useTsconfigDeclarationDir: true,
});
}
}

module.exports = new RollupPluginsResolver();
20 changes: 20 additions & 0 deletions config/rollup.config.es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const paths = require('./paths');
const plugins = require('./rollup-plugins');

const format = paths.formats.es;

module.exports = {
input: paths.input,
output: {
format: format,
file: paths.getFileName(format, false),
dir: paths.outputDir,
sourcemap: true,
},
plugins: [
plugins.resolveVirgilCrypto(),
plugins.resolve({ browser: true }),
plugins.commonjs(),
plugins.typescriptResolved,
],
};
23 changes: 23 additions & 0 deletions config/rollup.config.umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const paths = require('./paths');
const plugins = require('./rollup-plugins');

const UMD_NAME = 'E3kit';

module.exports = {
input: paths.input,
output: {
format: paths.formats.umd,
file: paths.getFileName(paths.formats.umd, true),
dir: paths.outputDir,
sourcemap: true,
name: UMD_NAME
},
plugins: [
plugins.sourcemap(),
plugins.resolveVirgilCrypto(),
plugins.resolve({ browser: true }),
plugins.commonjs(),
plugins.typescriptResolved,
plugins.uglify(),
],
};
3 changes: 3 additions & 0 deletions example/E3kitReactNative/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["module:metro-react-native-babel-preset"]
}
6 changes: 6 additions & 0 deletions example/E3kitReactNative/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

[android]
target = Google Inc.:Google APIs:23

[maven_repositories]
central = https://repo1.maven.org/maven2
70 changes: 70 additions & 0 deletions example/E3kitReactNative/.flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[ignore]
; We fork some components by platform
.*/*[.]android.js

; Ignore "BUCK" generated dirs
<PROJECT_ROOT>/\.buckd/

; Ignore unexpected extra "@providesModule"
.*/node_modules/.*/node_modules/fbjs/.*

; Ignore duplicate module providers
; For RN Apps installed via npm, "Libraries" folder is inside
; "node_modules/react-native" but in the source repo it is in the root
.*/Libraries/react-native/React.js

; Ignore polyfills
.*/Libraries/polyfills/.*

; Ignore metro
.*/node_modules/metro/.*

[include]

[libs]
node_modules/react-native/Libraries/react-native/react-native-interface.js
node_modules/react-native/flow/
node_modules/react-native/flow-github/

[options]
emoji=true

esproposal.optional_chaining=enable
esproposal.nullish_coalescing=enable

module.system=haste
module.system.haste.use_name_reducers=true
# get basename
module.system.haste.name_reducers='^.*/\([a-zA-Z0-9$_.-]+\.js\(\.flow\)?\)$' -> '\1'
# strip .js or .js.flow suffix
module.system.haste.name_reducers='^\(.*\)\.js\(\.flow\)?$' -> '\1'
# strip .ios suffix
module.system.haste.name_reducers='^\(.*\)\.ios$' -> '\1'
module.system.haste.name_reducers='^\(.*\)\.android$' -> '\1'
module.system.haste.name_reducers='^\(.*\)\.native$' -> '\1'
module.system.haste.paths.blacklist=.*/__tests__/.*
module.system.haste.paths.blacklist=.*/__mocks__/.*
module.system.haste.paths.blacklist=<PROJECT_ROOT>/node_modules/react-native/Libraries/Animated/src/polyfills/.*
module.system.haste.paths.whitelist=<PROJECT_ROOT>/node_modules/react-native/Libraries/.*

munge_underscores=true

module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub'

module.file_ext=.js
module.file_ext=.jsx
module.file_ext=.json
module.file_ext=.native.js

suppress_type=$FlowIssue
suppress_type=$FlowFixMe
suppress_type=$FlowFixMeProps
suppress_type=$FlowFixMeState

suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)?:? #[0-9]+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError

[version]
^0.78.0
1 change: 1 addition & 0 deletions example/E3kitReactNative/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pbxproj -text
56 changes: 56 additions & 0 deletions example/E3kitReactNative/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
*.keystore

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/

*/fastlane/report.xml
*/fastlane/Preview.html
*/fastlane/screenshots

# Bundle artifact
*.jsbundle
1 change: 1 addition & 0 deletions example/E3kitReactNative/.watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
75 changes: 75 additions & 0 deletions example/E3kitReactNative/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import { EThree } from '@virgilsecurity/e3kit';
import createNativeKeyEntryStorage from '@virgilsecurity/key-storage-rn/native';
const keyEntryStorage = createNativeKeyEntryStorage();
import 'whatwg-fetch';
import 'es6-symbol'

const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});

export default class App extends Component {

state = {
message: null
}

componentDidMount() {
const getToken = () => fetch("http://localhost:3000/get-virgil-jwt")
.then(res => res.json())
.then(data => data.token);

EThree.initialize(getToken, { keyEntryStorage: keyEntryStorage })
.then(client => sdk = client)
.then(() => sdk.register())
.then(() => sdk.encrypt('success!'))
.then((encryptedMessage) => sdk.decrypt(encryptedMessage))
.then((message) => this.setState({ message: message }))
.then(() => sdk.cleanup())
.catch((error) => {
this.setState({ message: error.toString() })
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<Text style={styles.instructions}>To get started, edit App.js</Text>
<Text style={styles.instructions}>{instructions}</Text>
{this.state.message && <Text style={styles.instructions}>{this.state.message}</Text>}
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
Loading

0 comments on commit 49a5c32

Please sign in to comment.