Skip to content

Commit

Permalink
Nep5 migrate (#361)
Browse files Browse the repository at this point in the history
* Fix for: Password is displayed as visible text when creating wallet #342 (#347)

* Fix for: Give warning when API is down or wallet lacks connectivity #265 (#346)

* Fix lint errors and refactor

* Remove RPX from reducers

* Move loading indicator to the top

* hide scrollbars

* finetune the buttons

* Move the refresh button code to Dashboard.scss

* Remove unused styles

* Split token balances into its own component

* Add info icon

* add tokens balance to initiateGetBalance

* Remove unused methods

* POC tokenInfo

* Use PasswordField for WIF key in secret token sale screen (#351)

* Add token info modal and refactor the tokens state

* Update snapshots

* Remove uneeded code

* Better formatting (#350)

* Better formatting for bigger numbers

* Formating formatters file

* Removing mime requirement. Not needed.

* Remove exclamation that's not needed in UI (#353)

* Enter in textfields submits alt (#354)

* Enter button submits forms, simple solution

* Code style fixes

* Use tight version

* Remove unused function

* Remove comment

* Remove ToggleAsset

* Rename Gas -> GAS / Neo -> NEO

* Add isToken method

* WIP

* Use tight versioning

* WIP

* Rename rpx to sale, and clean it up

* Rename reducers to index

* Fix paths

* Add flow

* Add flow

* cleanups

* Rename initiateGetBalance to LoadWalletData

* Add TokenInfo type

* Fix tests

* Refactor prices (to support in the future)

* Add Type suffix for types

* Cleanup

* Cleanup

* Cleanup

* Fix typo

* Use neon-js constants

* Add support for test tokens

* Add percent buttons

* Fix token sale

* Sort imports

* Fix typo

* Revert back to not using NeonJS for Network

* Remove unsued functions

* Fix lint errors

* Refactor explorer

* Fix typo

* Revert back to local consts for ASSETS

* Load info when modal loads

* Rename to openExplorerTx

* Pass retrieveTokenInfo to the token info modal

* WIP

* Change token to symbol

* Add flow-typed for npm modules

* Move declarations upper level

* Correct GAS decimal length

* Fix token info modal

* Symbol > Asset

* Add a border to the dashboard

* working on migration changes

* finalize migration changes
  • Loading branch information
dvdschwrtz authored and evgenyboxer committed Nov 27, 2017
1 parent 7e640a3 commit b6f1758
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 58 deletions.
6 changes: 3 additions & 3 deletions app/core/wallet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import { ASSETS, TOKENS } from './constants'
import { getAccountFromWIFKey, verifyAddress } from 'neon-js'
import Neon, { wallet } from 'neon-js'

const MIN_PASSPHRASE_LEN = 4

Expand All @@ -12,7 +12,7 @@ export const verifyPrivateKey = (wif: string): boolean => {
if (!wif) {
return false
}
const account = getAccountFromWIFKey(wif)
const account = Neon.create.account(wif)
return account !== -1 && account.address
}

Expand Down Expand Up @@ -46,7 +46,7 @@ export const validateTransactionBeforeSending = (neoBalance: number, gasBalance:
}

try {
if (verifyAddress(sendAddress) !== true || sendAddress.charAt(0) !== 'A') {
if (wallet.isAddress(sendAddress) !== true || sendAddress.charAt(0) !== 'A') {
return {
error: 'The address you entered was not valid.',
valid: false
Expand Down
6 changes: 3 additions & 3 deletions app/ledger/ledgerNanoS.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import { serializeTransaction, createSignatureScript } from 'neon-js'
import { tx, wallet } from 'neon-js'

import commNode from './ledger-comm-node'

Expand All @@ -9,7 +9,7 @@ import asyncWrap from '../core/asyncHelper'
export const CURRENT_VERSION = 0

export const ledgerNanoSCreateSignatureAsync = async (unsignedTx: string, publicKeyEncoded: string) => {
const txData = serializeTransaction(unsignedTx)
const txData = tx.serializeTransaction(unsignedTx)
const signData = txData + BIP44_PATH
const validStatus = [0x9000]
const messages = []
Expand Down Expand Up @@ -101,7 +101,7 @@ export const ledgerNanoSCreateSignatureAsync = async (unsignedTx: string, public
}

const signature = r + s
const script = createSignatureScript(publicKeyEncoded)
const script = wallet.getVerificationScriptFromPublicKey(publicKeyEncoded)

// txData + '01' (sign num) + '41' (sign struct len) + '40' (sign data len) + signature + '23' (Contract data len) + script
return `${txData}014140${signature}23${script}`
Expand Down
10 changes: 5 additions & 5 deletions app/modules/account.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import { getAccountFromWIFKey, getPublicKeyEncoded, getAccountFromPublicKey, decryptWIF } from 'neon-js'
import Neon, { wallet } from 'neon-js'

import { showErrorNotification, showInfoNotification, hideNotification } from './notifications'

Expand Down Expand Up @@ -57,7 +57,7 @@ export const loginNep2 = (passphrase: string, wif: string, history: Object) => (

setTimeout(async () => {
try {
const [_err, responseWif] = await asyncWrap(decryptWIF(wif, passphrase)) // eslint-disable-line
const [_err, responseWif] = await asyncWrap(wallet.decryptWIF(wif, passphrase)) // eslint-disable-line
dispatch(hideNotification(infoNotificationId))
dispatch(login(responseWif))
return history.push(ROUTES.DASHBOARD)
Expand Down Expand Up @@ -174,10 +174,10 @@ export default (state: Object = initialState, action: ReduxAction) => {
let loadAccount: Object | number
try {
if (signingFunction) {
const publicKeyEncoded = getPublicKeyEncoded(state.publicKey)
loadAccount = getAccountFromPublicKey(publicKeyEncoded)
const publicKeyEncoded = wallet.getPublicKeyEncoded(state.publicKey)
loadAccount = Neon.create.account(publicKeyEncoded)
} else {
loadAccount = getAccountFromWIFKey(wif)
loadAccount = Neon.create.account(wif)
}
} catch (e) {
console.log(e.stack)
Expand Down
16 changes: 9 additions & 7 deletions app/modules/claim.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import { doClaimAllGas, doSendAsset, getClaimAmounts, hardwareDoSendAsset, hardwareDoClaimAllGas } from 'neon-js'
import { api } from 'neon-js'

import { showErrorNotification, showSuccessNotification, showInfoNotification } from './notifications'
import { getWif, getAddress, getSigningFunction, getPublicKey, LOGOUT } from './account'
Expand Down Expand Up @@ -42,8 +42,10 @@ export function disableClaim (disableClaimButton: boolean) {
}

export const syncAvailableClaim = (net: NetworkType, address: string) => async (dispatch: DispatchType) => {
const [_err, result] = await asyncWrap(getClaimAmounts(net, address)) // eslint-disable-line
return dispatch(setClaim(result.available, result.unavailable))
const [_err, result] = await asyncWrap(api.neonDB.getClaims(net, address)) // eslint-disable-line
const available = parseInt(result.total_claim)
const unavailable = parseInt(result.total_unspent_claim)
return dispatch(setClaim(available, unavailable))
}

export const doClaimNotify = () => async (dispatch: DispatchType, getState: GetStateType) => {
Expand All @@ -64,9 +66,9 @@ export const doClaimNotify = () => async (dispatch: DispatchType, getState: GetS
message: 'Sign transaction 2 of 2 to claim GAS on your hardware device (claiming GAS)',
autoDismiss: 0
}))
claimGasFn = () => hardwareDoClaimAllGas(net, publicKey, signingFunction)
claimGasFn = () => api.neonDB.doClaimAllGas(net, publicKey, signingFunction)
} else {
claimGasFn = () => doClaimAllGas(net, wif)
claimGasFn = () => api.neonDB.doClaimAllGas(net, wif, null)
}

const [err, response] = await asyncWrap(claimGasFn())
Expand Down Expand Up @@ -106,9 +108,9 @@ export const doGasClaim = () => async (dispatch: DispatchType, getState: GetStat
message: 'Sign transaction 1 of 2 to claim GAS on your hardware device (sending NEO to yourself)',
autoDismiss: 0
}))
sendAssetFn = () => hardwareDoSendAsset(net, address, publicKey, { [ASSETS.NEO]: neo }, signingFunction)
sendAssetFn = () => api.neonDB.doSendAsset(net, address, publicKey, { [ASSETS.NEO]: neo }, signingFunction)
} else {
sendAssetFn = () => doSendAsset(net, address, wif, { [ASSETS.NEO]: neo })
sendAssetFn = () => api.neonDB.doSendAsset(net, address, wif, { [ASSETS.NEO]: neo }, null)
}

const [err, response] = await asyncWrap(sendAssetFn())
Expand Down
14 changes: 7 additions & 7 deletions app/modules/generateWallet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import storage from 'electron-json-storage'
import { generateEncryptedWif, getAccountFromWIFKey, generatePrivateKey, getWIFFromPrivateKey, encryptWIF, encryptWifAccount } from 'neon-js'
import Neon, { wallet } from 'neon-js'

import { showErrorNotification, showInfoNotification, hideNotification, showSuccessNotification } from './notifications'

Expand Down Expand Up @@ -66,7 +66,7 @@ export const generateWalletFromWif = (passphrase: string, passphrase2: string, w
const infoNotificationId = dispatch(showInfoNotification({ message: 'Generating encoded key...', autoDismiss: 0 }))
setTimeout(async () => {
try {
const [_err, result] = await asyncWrap(encryptWifAccount(wif, passphrase)) // eslint-disable-line
const [_err, result] = await asyncWrap(wallet.encryptWifAccount(wif, passphrase)) // eslint-disable-line
dispatch(hideNotification(infoNotificationId))
return dispatch(newWallet(result))
} catch (e) {
Expand All @@ -87,7 +87,7 @@ export const generateNewWallet = (passphrase: string, passphrase2: string) => as
const infoNotificationId = dispatch(showInfoNotification({ message: 'Generating encoded key...', autoDismiss: 0 }))
setTimeout(async () => {
try {
const [_err, result] = await asyncWrap(generateEncryptedWif(passphrase)) //eslint-disable-line
const [_err, result] = await asyncWrap(wallet.generateEncryptedWif(passphrase)) //eslint-disable-line
dispatch(hideNotification(infoNotificationId))
return dispatch(newWallet(result))
} catch (e) {
Expand Down Expand Up @@ -118,10 +118,10 @@ export default (state: Object = initialState, action: ReduxAction) => {
switch (action.type) {
case NEW_WALLET_KEYS: {
const { passphrase } = action.payload
const newPrivateKey = generatePrivateKey()
const newWif = getWIFFromPrivateKey(newPrivateKey)
const encryptedWif = encryptWIF(newWif, passphrase)
const loadAccount = getAccountFromWIFKey(newWif)
const newPrivateKey = wallet.generatePrivateKey()
const newWif = wallet.getWIFFromPrivateKey(newPrivateKey)
const encryptedWif = wallet.encryptWIF(newWif, passphrase)
const loadAccount = Neon.create.account(newWif)
return {
...state,
wif: newWif,
Expand Down
6 changes: 3 additions & 3 deletions app/modules/metadata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import axios from 'axios'
import { getWalletDBHeight, getAPIEndpoint } from 'neon-js'
import { api } from 'neon-js'

import { showWarningNotification } from './notifications'

Expand Down Expand Up @@ -39,7 +39,7 @@ export function setBlockExplorer (blockExplorer: ExplorerType) {
export const checkVersion = () => async (dispatch: DispatchType, getState: GetStateType) => {
const state = getState().metadata
const { net } = state
const apiEndpoint = getAPIEndpoint(net)
const apiEndpoint = api.neonDB.getAPIEndpoint(net)

const [err, res] = await asyncWrap(axios.get(`${apiEndpoint}/v2/version`))
const shouldUpdate = res && res.data && res.data.version !== version
Expand All @@ -57,7 +57,7 @@ export const checkVersion = () => async (dispatch: DispatchType, getState: GetSt
}

export const syncBlockHeight = (net: NetworkType) => async (dispatch: DispatchType) => {
const [_err, blockHeight] = await asyncWrap(getWalletDBHeight(net)) // eslint-disable-line
const [_err, blockHeight] = await asyncWrap(api.neonDB.getWalletDBHeight(net)) // eslint-disable-line
return dispatch(setBlockHeight(blockHeight))
}

Expand Down
31 changes: 16 additions & 15 deletions app/modules/sale.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// @flow
import { getTokenBalance, getAccountFromWIFKey, doMintTokens } from 'neon-js'
import Neon, { api } from 'neon-js'

import { showErrorNotification, showInfoNotification, showSuccessNotification } from './notifications'
import { getWif, LOGOUT } from './account'
import { getNetwork } from './metadata'
import { getNEO } from './wallet'
import asyncWrap from '../core/asyncHelper'

export const participateInSale = (neoToSend: number, scriptHash: string) => (dispatch: DispatchType, getState: GetStateType) => {
export const participateInSale = (neoToSend: number, scriptHash: string) => async (dispatch: DispatchType, getState: GetStateType) => {
const state = getState()
const wif = getWif(state)
const neo = getNEO(state)
const net = getNetwork(state)

const account = getAccountFromWIFKey(wif)
const account = Neon.create.account(wif)
if (parseFloat(neoToSend) !== parseInt(neoToSend)) {
dispatch(showErrorNotification({ message: 'You cannot send fractional NEO to a token sale.' }))
return false
Expand All @@ -31,20 +32,20 @@ export const participateInSale = (neoToSend: number, scriptHash: string) => (dis

dispatch(showInfoNotification({ message: 'Sending transaction', autoDismiss: 0 }))

return getTokenBalance(net, _scriptHash, account.address).then((balance) => {
doMintTokens(net, _scriptHash, wif, toMint, 0).then((response) => {
if (response.result === true) {
dispatch(showSuccessNotification({ message: 'Sale participation was successful.' }))
return true
} else {
dispatch(showErrorNotification({ message: 'Sale participation failed.' }))
return false
}
})
}).catch((e) => {
const [error, rpcEndpoint] = await asyncWrap(api.neonDB.getRPCEndpoint(net)) // eslint-disable-line
const [err, balance] = await asyncWrap(api.nep5.getTokenBalance(rpcEndpoint, _scriptHash, account.address))
const [e, response] = await asyncWrap(api.neonDB.doMintTokens(net, _scriptHash, wif, toMint, 0))
if (error || err || e) {
dispatch(showErrorNotification({ message: 'This script hash cannot mint tokens.' }))
return false
})
}
if (response.result === true) {
dispatch(showSuccessNotification({ message: 'Sale participation was successful.' }))
return true
} else {
dispatch(showErrorNotification({ message: 'Sale participation failed.' }))
return false
}
}

const initialState = {
Expand Down
10 changes: 5 additions & 5 deletions app/modules/transactions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
/* eslint-disable camelcase */
import { capitalize } from 'lodash'
import { getTransactionHistory, doSendAsset, hardwareDoSendAsset, doTransferToken } from 'neon-js'
import { api } from 'neon-js'

import { setTransactionHistory, getNEO, getGAS, getTokens, getScriptHashForNetwork } from './wallet'
import { showErrorNotification, showInfoNotification, showSuccessNotification } from './notifications'
Expand All @@ -26,7 +26,7 @@ export const setIsLoadingTransaction = (isLoading: boolean) => ({

export const syncTransactionHistory = (net: NetworkType, address: string) => async (dispatch: DispatchType) => {
dispatch(setIsLoadingTransaction(true))
const [err, transactions] = await asyncWrap(getTransactionHistory(net, address))
const [err, transactions] = await asyncWrap(api.neonDB.getTransactionHistory(net, address))
if (!err && transactions) {
const txs = transactions.map(({ NEO, GAS, txid, block_index, neo_sent, neo_gas }: TransactionHistoryType) => ({
type: neo_sent ? ASSETS.NEO : ASSETS.GAS,
Expand Down Expand Up @@ -71,13 +71,13 @@ export const sendTransaction = (sendAddress: string, sendAmount: string, symbol:
let sendAssetFn
if (isHardwareSend) {
dispatch(showInfoNotification({ message: 'Please sign the transaction on your hardware device', autoDismiss: 0 }))
sendAssetFn = () => hardwareDoSendAsset(net, sendAddress, publicKey, sendAsset, signingFunction)
sendAssetFn = () => api.neonDB.doSendAsset(net, sendAddress, publicKey, sendAsset, signingFunction)
} else {
if (symbol === ASSETS.NEO || symbol === ASSETS.GAS) {
sendAssetFn = () => doSendAsset(net, sendAddress, wif, sendAsset)
sendAssetFn = () => api.neonDB.doSendAsset(net, sendAddress, wif, sendAsset, null)
} else {
const scriptHash = getScriptHashForNetwork(net, symbol)
sendAssetFn = () => doTransferToken(net, scriptHash, wif, sendAddress, parseFloat(sendAmount))
sendAssetFn = () => api.nep5.doTransferToken(net, scriptHash, wif, sendAddress, parseFloat(sendAmount))
}
}

Expand Down
10 changes: 6 additions & 4 deletions app/modules/wallet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import axios from 'axios'
import { getBalance, getTokenBalance, getTokenInfo } from 'neon-js'
import { api } from 'neon-js'
import { merge } from 'lodash'

import { syncTransactionHistory } from './transactions'
Expand Down Expand Up @@ -95,7 +95,7 @@ export const getGASMarketPriceUSD = () => async (dispatch: DispatchType) => {

export const retrieveBalance = (net: NetworkType, address: string) => async (dispatch: DispatchType) => {
// If API dies, still display balance - ignore _err
const [_err, resultBalance] = await asyncWrap(getBalance(net, address)) // eslint-disable-line
const [_err, resultBalance] = await asyncWrap(api.neonDB.getBalance(net, address)) // eslint-disable-line
return dispatch(setBalance(resultBalance.NEO.balance, resultBalance.GAS.balance))
}

Expand All @@ -118,7 +118,8 @@ export const retrieveTokensBalance = () => async (dispatch: DispatchType, getSta
for (let [symbol] of TOKEN_PAIRS) {
const scriptHash = getScriptHashForNetwork(net, symbol)
// override scripthash with test if on test net
let [_err, results] = await asyncWrap(getTokenBalance(net, scriptHash, address)) // eslint-disable-line
const [_error, rpcEndpoint] = await asyncWrap(api.neonDB.getRPCEndpoint(net)) // eslint-disable-line
const [_err, results] = await asyncWrap(api.nep5.getTokenBalance(rpcEndpoint, scriptHash, address)) // eslint-disable-line
if (results) {
tokens[symbol] = {
symbol,
Expand All @@ -135,7 +136,8 @@ export const retrieveTokenInfo = (symbol: TokenSymbolType) => async (dispatch: D
const state = getState()
const net = getNetwork(state)

let [_err, results] = await asyncWrap(getTokenInfo(net, getScriptHashForNetwork(net, symbol))) // eslint-disable-line
const [_error, rpcEndpoint] = await asyncWrap(api.neonDB.getRPCEndpoint(net)) // eslint-disable-line
const [_err, results] = await asyncWrap(api.nep5.getTokenInfo(rpcEndpoint, getScriptHashForNetwork(net, symbol))) // eslint-disable-line
dispatch(setTokenInfo(symbol, results))
return results
}
Expand Down
4 changes: 2 additions & 2 deletions app/util/Logs.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// @flow
import axios from 'axios'
import { getAPIEndpoint } from 'neon-js'
import { api } from 'neon-js'
import { version } from '../../package.json'

let sessionCount = 0

export const log = (net: NetworkType, type: string, address: string, data: Object) => {
const apiEndpoint = getAPIEndpoint(net)
const apiEndpoint = api.neonDB.getAPIEndpoint(net)
axios.post(apiEndpoint + '/v2/log', {
type: type,
time: Date.now(),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"isomorphic-fetch": "2.2.1",
"js-scrypt": "0.2.0",
"lodash": "4.17.4",
"neon-js": "git+https://github.com/dvdschwrtz/neon-js.git#6d4a065e277ccae524d45fd9a5f4662d05fbbfca",
"neon-js": "git+https://github.com/cityofzion/neon-js.git#4b2708c37a272919e482d7d4068d30d49f714e4c",
"node-hid": "0.5.7",
"numeral": "2.0.6",
"qrcode": "0.9.0",
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5965,9 +5965,9 @@ negotiator@0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9"

"neon-js@git+https://github.com/dvdschwrtz/neon-js.git#6d4a065e277ccae524d45fd9a5f4662d05fbbfca":
version "1.1.1"
resolved "git+https://github.com/dvdschwrtz/neon-js.git#6d4a065e277ccae524d45fd9a5f4662d05fbbfca"
"neon-js@git+https://github.com/cityofzion/neon-js.git#4b2708c37a272919e482d7d4068d30d49f714e4c":
version "2.0.0"
resolved "git+https://github.com/cityofzion/neon-js.git#4b2708c37a272919e482d7d4068d30d49f714e4c"
dependencies:
axios "^0.16.2"
base-x "^3.0.2"
Expand Down

0 comments on commit b6f1758

Please sign in to comment.