Skip to content

Commit

Permalink
Replaces ecc with elliptic
Browse files Browse the repository at this point in the history
Changes JsonRpc usages to new eosjs version standard

Peer review changes and last ecc reference

Locking dependencies

bump

resolutions

Fix issues

lock dependencies

bump

Set RPC to null instead of hard-coding
  • Loading branch information
Bradley Hart authored and Jon La Marr committed Jan 10, 2020
1 parent e1c170b commit 25ce6e5
Show file tree
Hide file tree
Showing 9 changed files with 630 additions and 1,755 deletions.
4 changes: 0 additions & 4 deletions custom.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
///<reference types="chrome"/>

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>

type Extend<TType extends Object, TExtension extends Object> = { [K in keyof TType]: TType[K] } & TExtension

declare module "*.svg" {
Expand All @@ -14,8 +12,6 @@ declare module "*.gif" {
export default content
}

declare module "eosjs-ecc"

declare module "bip38"
declare module "wif"

Expand Down
38 changes: 16 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,33 @@
"private": true,
"sideEffects": false,
"dependencies": {
"bip38": "2.0.2",
"bip38": "2.0.3",
"bs58": "4.0.1",
"classnames": "2.2.6",
"eosjs": "20.0.0",
"eosjs-ecc": "4.0.2",
"eosjs-signature-provider-interface": "0.1.1",
"elliptic": "6.5.2",
"eosjs": "20.0.4-e2c667e.0",
"eosjs-signature-provider-interface": "0.1.2",
"hash.js": "1.1.5",
"lodash": "4.17.15",
"react": "16.8.3",
"react-dom": "16.8.3",
"react-redux": "5.1.1",
"react": "16.12.0",
"react-dom": "16.12.0",
"react-redux": "5.1.2",
"react-router-dom": "4.3.1",
"react-scripts-ts": "3.1.0",
"react-transition-group": "2.6.0",
"redux": "4.0.1",
"react-scripts-ts": "2.17.0",
"react-transition-group": "2.9.0",
"redux": "4.0.5",
"redux-thunk": "2.3.0",
"ricardian-template-toolkit": "0.2.0",
"ricardian-template-toolkit": "0.2.1",
"ripemd160": "2.0.2",
"uuid": "3.3.2",
"uuid": "3.3.3",
"wif": "2.0.6"
},
"devDependencies": {
"@blockone/tslint-config-blockone": "^4.0.1",
"@types/bs58": "^4.0.0",
"@types/chrome": "^0.0.74",
"@types/classnames": "^2.2.7",
"@types/elliptic": "^6.4.10",
"@types/enzyme": "^3.1.12",
"@types/handlebars": "^4.0.39",
"@types/jest": "^22.2.3",
Expand Down Expand Up @@ -72,11 +73,6 @@
"eslint-plugin-import": "^2.19.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.17.0",
"gulp": "^4.0.2",
"gulp-crx-pack": "^1.0.2",
"gulp-git": "^2.10.0",
"gulp-json-editor": "^2.5.4",
"gulp-zip": "^4.2.0",
"html-webpack-plugin": "^3.2.0",
"jest-coverage-ratchet": "^0.2.3",
"node-fetch": "^2.3.0",
Expand All @@ -91,14 +87,12 @@
"worker-loader": "^2.0.0"
},
"resolutions": {
"@types/sinon": "5.0.2",
"handlebars": "4.5.3",
"braces": "2.3.2",
"lodash": "4.17.15",
"handlebars": "4.5.3",
"js-yaml": "3.13.1",
"mem": "4.0.0",
"serialize-javascript": "2.1.1",
"webpack-dev-server": "3.1.11",
"mem": "4.0.0"
"webpack-dev-server": "3.1.11"
},
"scripts": {
"build": "npm-run-all -p build-css build-js build-scripts",
Expand Down
76 changes: 50 additions & 26 deletions src/components/auth/AddAuth/AddAuthContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as dappMessengerMocks from 'utils/__mocks__/DappMessenger.mock'

import * as React from 'react'
import { shallow, ShallowWrapper } from 'enzyme'
import * as ecc from 'eosjs-ecc'
import * as hashjs from 'hash.js'
import { PublicKey } from 'eosjs/dist/eosjs-jssig'

import { AddAuthContainer, mapDispatchToProps, ERROR_MESSAGES } from 'components/auth/AddAuth/AddAuthContainer'
import AddAuthView from 'components/auth/AddAuth/AddAuthView'
Expand All @@ -20,12 +20,20 @@ describe('AddAuthContainer', () => {
let auth: Auth
let history: any
let addAuthErrors: AddAuthFormInputs
let privateKeys: string[]

beforeEach(() => {

privateKeys = [
'5Juww5SS6aLWxopXBAWzwqrwadiZKz7XpKAiktXTKcfBGi1DWg8',
'5JnHjSFwe4r7xyqAUAaVs51G7HmzE86DWGa3VAA5VvQriGYnSUr',
'5K4XZH5XR2By7Q5KTcZnPAmUMU5yjUNBdoKzzXyrLfmiEZJqoKE',
]

auth = {
nickname: 'nickname1',
publicKey: 'publicKey1',
encryptedPrivateKey: 'privateKey1',
publicKey: 'PUB_K1_8VaY5CiTexYqgQZyPTJkc3qvWuZUi12QrZL9ssjqW2es7e7bRJ',
encryptedPrivateKey: privateKeys[0],
}

history = {
Expand Down Expand Up @@ -75,14 +83,12 @@ describe('AddAuthContainer', () => {
describe('when there are no errors', () => {
beforeEach(() => {
addAuthContainer.setState({
privateKey: 'privateKey2',
privateKey: privateKeys[2],
nickname: 'nickname2',
passphrase: 'passphrase2',
addAuthErrors: {},
})

jest.spyOn(ecc, 'isValidPrivate').mockReturnValue(true)
jest.spyOn(ecc, 'privateToPublic').mockReturnValue('pubKey2')
jest.spyOn(hashjs, 'sha256').mockReturnValue({
update: jest.fn().mockReturnValue({
digest: jest.fn().mockReturnValue('hashedPassphrase'),
Expand All @@ -92,7 +98,7 @@ describe('AddAuthContainer', () => {
})

it('calls the auth add callback', () => {
expect(onAuthAdd).toHaveBeenCalledWith('nickname2', 'privateKey2', 'passphrase2')
expect(onAuthAdd).toHaveBeenCalledWith('nickname2', privateKeys[2], 'passphrase2')
})

it('navigates to the auths page', () => {
Expand All @@ -104,13 +110,12 @@ describe('AddAuthContainer', () => {
describe('any error', () => {
beforeEach(() => {
addAuthContainer.setState({
privateKey: 'privateKey2',
privateKey: privateKeys[1],
nickname: 'nickname2',
passphrase: 'passphrase2',
addAuthErrors: {},
})

jest.spyOn(ecc, 'isValidPrivate').mockReturnValue(false)
addAuthContainer.find(AddAuthView).prop('onAuthAdd')()
})

Expand All @@ -127,13 +132,19 @@ describe('AddAuthContainer', () => {
describe('when there are duplicate nicknames', () => {
beforeEach(() => {
addAuthContainer.setState({
privateKey: 'privateKey2',
privateKey: privateKeys[2],
nickname: 'nickname1',
passphrase: 'passphrase2',
addAuthErrors: {},
})

jest.spyOn(ecc, 'isValidPrivate').mockReturnValue(true)
jest.spyOn(hashjs, 'sha256').mockReturnValue({
update: jest.fn().mockReturnValue({
digest: jest.fn().mockReturnValue('hashedPassphrase'),
}),
})
jest.spyOn(PublicKey.prototype, 'toString').mockReturnValueOnce('publicKey2')

addAuthContainer.find(AddAuthView).prop('onAuthAdd')()
})

Expand All @@ -146,24 +157,30 @@ describe('AddAuthContainer', () => {
}

expect(addAuthContainer.state()).toEqual({
privateKey: 'privateKey2',
privateKey: privateKeys[2],
nickname: 'nickname1',
passphrase: 'passphrase2',
addAuthErrors,
})
})
})

describe('when the nickame is empty', () => {
describe('when the nickname is empty', () => {
beforeEach(() => {
addAuthContainer.setState({
privateKey: 'privateKey2',
privateKey: privateKeys[1],
nickname: '',
passphrase: 'passphrase2',
addAuthErrors: {},
})

jest.spyOn(ecc, 'isValidPrivate').mockReturnValue(true)
jest.spyOn(hashjs, 'sha256').mockReturnValue({
update: jest.fn().mockReturnValue({
digest: jest.fn().mockReturnValue('hashedPassphrase'),
}),
})
jest.spyOn(PublicKey.prototype, 'toString').mockReturnValueOnce('publicKey1')

addAuthContainer.find(AddAuthView).prop('onAuthAdd')()
})

Expand All @@ -175,7 +192,7 @@ describe('AddAuthContainer', () => {
}

expect(addAuthContainer.state()).toEqual({
privateKey: 'privateKey2',
privateKey: privateKeys[1],
nickname: '',
passphrase: 'passphrase2',
addAuthErrors,
Expand All @@ -188,14 +205,18 @@ describe('AddAuthContainer', () => {
describe('when there are duplicate private keys', () => {
beforeEach(() => {
addAuthContainer.setState({
privateKey: 'privateKey2',
privateKey: privateKeys[1],
nickname: 'nickname2',
passphrase: 'passphrase2',
addAuthErrors: {},
})

jest.spyOn(ecc, 'isValidPrivate').mockReturnValue(true)
jest.spyOn(ecc, 'privateToPublic').mockReturnValue('publicKey1')
jest.spyOn(hashjs, 'sha256').mockReturnValue({
update: jest.fn().mockReturnValue({
digest: jest.fn().mockReturnValue('hashedPassphrase'),
}),
})

addAuthContainer.find(AddAuthView).prop('onAuthAdd')()
})

Expand All @@ -207,7 +228,7 @@ describe('AddAuthContainer', () => {
}

expect(addAuthContainer.state()).toEqual({
privateKey: 'privateKey2',
privateKey: privateKeys[1],
nickname: 'nickname2',
passphrase: 'passphrase2',
addAuthErrors,
Expand All @@ -224,7 +245,12 @@ describe('AddAuthContainer', () => {
addAuthErrors: {},
})

jest.spyOn(ecc, 'isValidPrivate').mockReturnValue(false)
jest.spyOn(hashjs, 'sha256').mockReturnValue({
update: jest.fn().mockReturnValue({
digest: jest.fn().mockReturnValue('hashedPassphrase'),
}),
})

addAuthContainer.find(AddAuthView).prop('onAuthAdd')()
})

Expand All @@ -248,20 +274,18 @@ describe('AddAuthContainer', () => {
describe('passphrase error', () => {
beforeEach(() => {
addAuthContainer.setState({
privateKey: 'privateKey2',
privateKey: privateKeys[1],
nickname: 'nickname2',
passphrase: 'passphrase2',
addAuthErrors: {},
})

jest.spyOn(ecc, 'isValidPrivate').mockReturnValue(true)
jest.spyOn(ecc, 'privateToPublic').mockReturnValue('publicKey2')

jest.spyOn(hashjs, 'sha256').mockReturnValue({
update: jest.fn().mockReturnValue({
digest: jest.fn().mockReturnValue('badHash'),
}),
})
jest.spyOn(PublicKey.prototype, 'toString').mockReturnValueOnce('publicKey1')

addAuthContainer.find(AddAuthView).prop('onAuthAdd')()
})
Expand All @@ -274,7 +298,7 @@ describe('AddAuthContainer', () => {
}

expect(addAuthContainer.state()).toEqual({
privateKey: 'privateKey2',
privateKey: privateKeys[1],
nickname: 'nickname2',
passphrase: 'passphrase2',
addAuthErrors,
Expand Down
18 changes: 12 additions & 6 deletions src/components/auth/AddAuth/AddAuthContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as React from 'react'
import { withRouter, RouteComponentProps } from 'react-router-dom'
import { connect } from 'react-redux'
import { sha256 } from 'hash.js'
import { isValidPrivate, privateToPublic } from 'eosjs-ecc'
import { ec as EC } from 'elliptic'
import { PrivateKey, PublicKey } from 'eosjs/dist/eosjs-jssig'

import AddAuthView from 'components/auth/AddAuth/AddAuthView'
import { AddAuthFormInputs } from 'components/auth/AddAuth/AddAuthView'
Expand All @@ -11,6 +12,8 @@ import { authAdd } from 'store/auths/authsActions'
import AppState from 'store/AppState'
import Auth from 'utils/Auth'

const ec = new EC('secp256k1')

export const ERROR_MESSAGES = {
INVALID_PRIVATE_KEY: 'Invalid private key',
INVALID_PASSPHRASE: 'Invalid passphrase',
Expand Down Expand Up @@ -80,14 +83,17 @@ export class AddAuthContainer extends React.Component<Props, State> {
let nicknameError = ''
let passphraseError = ''

if (!isValidPrivate(privateKey)) {
try {
const privateKeyElliptic = PrivateKey.fromString(privateKey).toElliptic()
const ellipticPublicKey = ec.keyFromPrivate(privateKeyElliptic)
const publicKey = PublicKey.fromElliptic(ellipticPublicKey).toString()
if (this.auths.find((auth) => auth.publicKey === publicKey)) {
privateKeyError = ERROR_MESSAGES.DUPLICATE_PRIVATE_KEY
}
} catch (e) {
privateKeyError = ERROR_MESSAGES.INVALID_PRIVATE_KEY
}

if (!privateKeyError && this.auths.find((auth) => auth.publicKey === privateToPublic(privateKey))) {
privateKeyError = ERROR_MESSAGES.DUPLICATE_PRIVATE_KEY
}

if (sha256().update(passphrase).digest('hex') !== this.props.passphraseHash) {
passphraseError = ERROR_MESSAGES.INVALID_PASSPHRASE
}
Expand Down
Loading

0 comments on commit 25ce6e5

Please sign in to comment.