Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaces eosjs-ecc with elliptic #37

Merged
merged 3 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ declare module "*.gif" {
export default content
}

declare module "eosjs-ecc"

declare module "bip38"
declare module "wif"

Expand Down
36 changes: 15 additions & 21 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": "21.0.0-131a987.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": "2.17.0",
"react-transition-group": "2.6.0",
"redux": "4.0.3",
"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
15 changes: 8 additions & 7 deletions src/components/auth/AddAuth/AddAuthContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ 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 AddAuthView from 'components/auth/AddAuth/AddAuthView'
import { AddAuthFormInputs } from 'components/auth/AddAuth/AddAuthView'
import { Dispatch } from 'store/storeHelpers'
import { authAdd } from 'store/auths/authsActions'
import { authAdd, recoverPublicKey } from 'store/auths/authsActions'
import AppState from 'store/AppState'
import Auth from 'utils/Auth'

Expand Down Expand Up @@ -80,12 +79,14 @@ export class AddAuthContainer extends React.Component<Props, State> {
let nicknameError = ''
let passphraseError = ''

if (!isValidPrivate(privateKey)) {
privateKeyError = ERROR_MESSAGES.INVALID_PRIVATE_KEY
}
try {
const publicKey = recoverPublicKey(privateKey)

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

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