Skip to content

Commit

Permalink
refactor(file name): implement project layout defined in witnet#126
Browse files Browse the repository at this point in the history
BREAKING CHANGE: everyone need to update what they are working on in order to follow the new project
structure

fix witnet#144
  • Loading branch information
Anler Hernández Peral committed Jun 14, 2018
1 parent cc20f79 commit 9119c24
Show file tree
Hide file tree
Showing 73 changed files with 183 additions and 224 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ main.js.map

.idea
.vscode
/config/tsconfig.tslint.json
61 changes: 0 additions & 61 deletions app/lib/crypto/pbkdf2.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions app/lib/crypto/keyPath.ts → app/main/crypto/keyPath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {HARDENED_KEY_INDEX} from "./constants"
import {Errors} from "./errors"
import { HARDENED_KEY_INDEX } from "./constants"
import { Errors } from "./errors"

export type KeyPath = Array<number>

Expand Down
File renamed without changes.
61 changes: 61 additions & 0 deletions app/main/crypto/pbkdf2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Errors } from "./errors"

const assert = require("assert")
const crypto = require("crypto")

/**
* PBKDF2
* https://github.com/stayradiated/pbkdf2-sha512
* @param {string | Buffer} key
* @param {string | Buffer} salt
* @param {number} iterations
* @param {number} dkLen
* @returns {Buffer}
*/
export const pbkdf2 =
(key: string | Buffer, salt: string | Buffer, iterations: number, dkLen: number) => {
const hLen = 64 // SHA512 Mac length
const validKeyLength = dkLen <= (Math.pow(2, 32) - 1) * hLen
assert(validKeyLength, Errors.INVALID_KEY_LENGTH)

const saltBuffer = (typeof salt === "string") ? Buffer.from(salt) : salt
const keyBuffer = (typeof key === "string") ? Buffer.from(key) : key

const DK = Buffer.alloc(dkLen)
const T = Buffer.alloc(hLen)

const block1 = Buffer.alloc(saltBuffer.length + 4)

const l = Math.ceil(dkLen / hLen)
const r = dkLen - (l - 1) * hLen

saltBuffer.copy(block1, 0, 0, saltBuffer.length)
for (let i = 1; i <= l; i++) {
block1[saltBuffer.length + 0] = (i >> 24 & 0xFF)
block1[saltBuffer.length + 1] = (i >> 16 & 0xFF)
block1[saltBuffer.length + 2] = (i >> 8 & 0xFF)
block1[saltBuffer.length + 3] = (i >> 0 & 0xFF)

let U = crypto.createHmac("sha512", keyBuffer)
.update(block1)
.digest()

U.copy(T, 0, 0, hLen)

for (let j = 1; j < iterations; j++) {
U = crypto.createHmac("sha512", keyBuffer)
.update(U)
.digest()

for (let k = 0; k < hLen; k++) {
T[k] ^= U[k]
}
}

const destPos = (i - 1) * hLen
const len = (i === l ? r : hLen)
T.copy(DK, destPos, 0, len)
}

return DK
}
2 changes: 1 addition & 1 deletion app/lib/crypto/seed.ts → app/main/crypto/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Mnemonic from "./mnemonic"
import * as Hash from "./hash"
import * as assert from "assert"
import * as bip39 from "bip39"
import {Errors} from "./errors"
import { Errors } from "./errors"

export type Seed = {
masterSecret: Buffer
Expand Down
File renamed without changes.
16 changes: 8 additions & 8 deletions app/lib/encoding/slip32.ts → app/main/encoding/slip32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as KeyPath from "../crypto/keyPath"
import * as Key from "../crypto/key/key"
import * as PrivateKey from "../crypto/key/privateKey"
import * as PublicKey from "../crypto/key/publicKey"
import {integerAsBuffer} from "../utils/conversions"
import {privateKeyVerify, publicKeyVerify} from "secp256k1"
import { integerAsBuffer } from "../utils/conversions"
import { privateKeyVerify, publicKeyVerify } from "secp256k1"
import * as Bech32 from "bech32"

/*
Expand Down Expand Up @@ -35,9 +35,9 @@ type PrefixedBuffer = {
* @returns {PrefixedBuffer}
*/
export const decode = (encodedBech32: string): PrefixedBuffer => {
const {prefix: hrp, words} = Bech32.decode(encodedBech32, BECH32_LIMIT)
const { prefix: hrp, words } = Bech32.decode(encodedBech32, BECH32_LIMIT)

return {hrp, words}
return { hrp, words }
}

/**
Expand All @@ -46,7 +46,7 @@ export const decode = (encodedBech32: string): PrefixedBuffer => {
* @param {Buffer} words
* @returns {string}
*/
export const encode = ({hrp, words}: PrefixedBuffer): string => {
export const encode = ({ hrp, words }: PrefixedBuffer): string => {
return Bech32.encode(hrp, Buffer.from(words), BECH32_LIMIT)
}

Expand All @@ -72,9 +72,9 @@ export const exportKeyToSlip32 = (keyPath: KeyPath.KeyPath, extKey:
// Encode hrp + words (5-bit)
const words = Bech32.toWords(Buffer.concat(data))
if (extKey.key.type === "private") {
return encode({hrp: "xprv", words})
return encode({ hrp: "xprv", words })
} else {
return encode({hrp: "xpub", words})
return encode({ hrp: "xpub", words })
}
}

Expand All @@ -88,7 +88,7 @@ export const importKeyFromSlip32 = (slip32: string): {
extendedKey: Key.ExtendedKey<PrivateKey.PrivateKey> | Key.ExtendedKey<PublicKey.PublicKey>
} => {
// Decode slip32
const {hrp, words}: { hrp: string, words: Buffer } = decode(slip32)
const { hrp, words }: { hrp: string, words: Buffer } = decode(slip32)
// Check hrp
if ((hrp !== "xprv") && (hrp !== "xpub")) {
throw Error("Malformed slip32 serialized key: invalid hrp")
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const defaultSettings: Settings = {
const serialize = (buffers: Array<Buffer>): Buffer => {
const parts: Array<Buffer> = []

buffers.forEach( (part: Buffer) => {
buffers.forEach((part: Buffer) => {
const dataLength = Buffer.alloc(4)
dataLength.writeUInt32BE(part.length, 0)
parts.push(dataLength)
Expand Down Expand Up @@ -148,4 +148,4 @@ export const decrypt = (data: Buffer, settings: Settings): Buffer => {
const plaintext = decipher.update(ciphertext)

return Buffer.concat([plaintext, decipher.final()])
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {LevelUp} from "levelup"
import { LevelUp } from "levelup"
import IStorageBackend from "./storageBackend"

/**
Expand All @@ -10,7 +10,7 @@ export default class LevelBackend implements IStorageBackend {
* The LevelBackend constructor takes a reference to a LevelUp backend.
* @param {levelup.LevelUp} connection
*/
constructor(private connection: LevelUp) {}
constructor(private connection: LevelUp) { }

/**
* Database closing method implementation for LevelDB.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EncryptedStorage from "./encryptedStorage"
import * as Encryption from "./encryption"
import * as path from "path"
import {homedir} from "os"
import { homedir } from "os"
import LevelBackend from "./levelBackend"
import * as level from "level"

Expand All @@ -24,4 +24,4 @@ const levelEncryptedStorageFactory = async (
return new EncryptedStorage(database, encryptionSettings)
}

export {levelEncryptedStorageFactory}
export { levelEncryptedStorageFactory }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/lib/utils/utils.ts → app/main/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export function kvSwap(obj: stringToNumber | numberToString | stringToString): a
return Object
.entries(obj)
.reduce((acc, [key, value]) => {
return {...acc, [value]: key}
return { ...acc, [value]: key }
}, {})
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/index.tsx → app/renderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from "react"
import { render } from "react-dom"
import { AppContainer } from "react-hot-loader"
import Root from "./containers/Root"
import "./app.global.scss"
import "./ui/app.global.scss"

const { configureStore, history } = require("./store/configureStore")
const store = configureStore()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 2 additions & 4 deletions app/app.html → app/renderer/ui/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
if (!process.env.HOT) {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = './style.css';
// HACK: Writing the script path should be done with webpack
link.href = '<%= htmlWebpackPlugin.options.styleFilename %>';
document.getElementsByTagName('head')[0].appendChild(link);
}
}());
Expand All @@ -23,8 +22,7 @@
const port = process.env.PORT || 3000;
script.src = (process.env.HOT)
? 'http://localhost:' + port + '/dist/bundle.js'
: './bundle.js';
// HACK: Writing the script path should be done with webpack
: '<%= htmlWebpackPlugin.options.bundleFilename %>';
document.body.appendChild(script);
}
</script>
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions config/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ module.exports = {
"roots": ["<rootDir>/.."],
"globals": {
"ts-jest": {
"tsConfigFile": "../config/tsconfig.json"
"tsConfigFile": "../config/tsconfig.test.json"
}
},
"moduleNameMapper": {
"appCommon\/(.*)$": "<rootDir>/../app/common/$1",
"appMain\/(.*)$": "<rootDir>/../app/main/$1",
"appRenderer\/(.*)$": "<rootDir>/../app/renderer/$1",
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/../test/__mocks__/fileMock.js",
"\\.(css|less|sass|scss)$": "identity-obj-proxy"
},
Expand All @@ -20,7 +23,7 @@ module.exports = {
],
"moduleDirectories": [
"<rootDir>/../node_modules",
"<rootDir>/../app/node_modules"
"<rootDir>/../app/lib"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
Expand Down
4 changes: 1 addition & 3 deletions config/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
"outDir": "dist"
},
"files": [
"../app/index.tsx"
"../app/renderer/index.tsx"
],
"include": [
"../app/**/*.ts",
"../app/**/*.tsx",
"../test/**/*.ts",
"../test/**/*.tsx",
"../node_modules/@types/**/*.d.ts",
"../typings/**/*.d.ts"
],
Expand Down
7 changes: 7 additions & 0 deletions config/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig",
"include": [
"../test/**/*.ts",
"../test/**/*.tsx"
]
}
25 changes: 10 additions & 15 deletions config/webpack.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const path = require("path");
const webpack = require("webpack");
const webpackMergeConfigs = require("webpack-merge");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpackRenderer = require("./webpack.renderer");

const port = process.env.PORT || 3000;
const forProduction = process.env.NODE_ENV === "production";
Expand All @@ -14,7 +14,7 @@ const baseConfig = {
devtool: "source-map",
target: "electron-main",

entry: [path.resolve(__dirname, "../app/main.electron")],
entry: [path.resolve(__dirname, "../app/main/index.js")],

resolve: {
extensions: [".js", ".ts", ".json"]
Expand All @@ -34,20 +34,8 @@ const baseConfig = {
},

plugins: [
// Add source map support for stack traces in node
// https://github.com/evanw/node-source-map-support
new webpack.BannerPlugin(
{ banner: "require(\"source-map-support\").install();", raw: true, entryOnly: false }
),

new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(forProduction ? "production" : "development")
}),

new HtmlWebpackPlugin({
filename: path.resolve(__dirname, "../dist/app.html"),
template: path.resolve(__dirname, "../app/app.html"),
inject: false
})
],

Expand All @@ -65,7 +53,14 @@ const productionConfig = {
};

const developmentConfig = {
mode: "development"
mode: "development",
plugins: [
// Add source map support for stack traces in node
// https://github.com/evanw/node-source-map-support
new webpack.BannerPlugin(
{ banner: "require(\"source-map-support\").install();", raw: true, entryOnly: false }
)
]
};

if (forProduction) {
Expand Down
Loading

0 comments on commit 9119c24

Please sign in to comment.