Commit 17d0366e authored by Marek Kotewicz's avatar Marek Kotewicz
Browse files

redux thunk and common improvements

parent d18de4af
...@@ -64,8 +64,11 @@ data: ...@@ -64,8 +64,11 @@ data:
```json ```json
{ {
"rlp": "f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804", "action":"signTransaction",
"from": "006E27B6A72E1f34C626762F3C4761547Aff1421" "data":{
"rlp":"f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804",
"account":"006E27B6A72E1f34C626762F3C4761547Aff1421"
}
} }
``` ```
......
docs/tx_qr.png

1.38 KB | W: | H:

docs/tx_qr.png

1.61 KB | W: | H:

docs/tx_qr.png
docs/tx_qr.png
docs/tx_qr.png
docs/tx_qr.png
  • 2-up
  • Swipe
  • Onion skin
'use strict' 'use strict'
import { NEW_SCANNED_TX, SIGNED_TX } from '../constants/TransactionActions' import { Actions } from 'react-native-router-flux'
import { NEW_SCANNED_TX, SIGN_TX } from '../constants/TransactionActions'
import { brainWalletSign } from '../util/native'
export function scannedTx (rlpHash, transaction) { export function scannedTx (rlpHash, transaction) {
return { return {
...@@ -10,9 +12,18 @@ export function scannedTx (rlpHash, transaction) { ...@@ -10,9 +12,18 @@ export function scannedTx (rlpHash, transaction) {
} }
} }
export function signedTx (signature) { export function signTx (account) {
return { return function (dispatch, getState) {
type: SIGNED_TX, let hash = getState().transactions.pendingTransaction.rlpHash
signature: signature return brainWalletSign(account.seed, hash).then(
signature => {
dispatch({
type: SIGN_TX,
signature: signature
})
Actions.qrViewTx()
},
error => console.log(error)
)
} }
} }
'use strict' 'use strict'
export const NEW_SCANNED_TX = 'TRANSACTION_ACTION_NEW_SCANNED_TX' export const NEW_SCANNED_TX = 'TRANSACTION_ACTION_NEW_SCANNED_TX'
export const SIGNED_TX = 'TRANSACTION_ACTION_SIGNED_TX' export const SIGN_TX = 'TRANSACTION_ACTION_SIGNED_TX'
...@@ -5,30 +5,17 @@ import { connect } from 'react-redux' ...@@ -5,30 +5,17 @@ import { connect } from 'react-redux'
import { Actions } from 'react-native-router-flux' import { Actions } from 'react-native-router-flux'
import AccountPin from '../components/AccountPin' import AccountPin from '../components/AccountPin'
import { addAccount, setPin } from '../actions/accounts' import { addAccount, setPin } from '../actions/accounts'
import { signedTx } from '../actions/transactions' import { signTx } from '../actions/transactions'
import { brainWalletSign } from '../util/native'
import { saveAccount } from '../util/db' import { saveAccount } from '../util/db'
import store from '../util/store'
const mapStateToPropsEnterPin = (state, ownProps) => ({ const mapStateToPropsEnterPin = (state, ownProps) => ({
account: state.accounts.selected account: state.accounts.selected
}) })
async function signTransaction (dispatch, account) {
try {
let hash = store.getState().transactions.pendingTransaction.rlpHash
let signature = await brainWalletSign(account.seed, hash)
dispatch(signedTx(signature))
Actions.qrViewTx()
} catch (e) {
console.log(e)
}
}
const mapDispatchToPropsEnterPin = (dispatch, ownProps) => ({ const mapDispatchToPropsEnterPin = (dispatch, ownProps) => ({
onNextPressed: (pin, account) => { onNextPressed: (pin, account) => {
if (pin === account.pin) { if (pin === account.pin) {
signTransaction(dispatch, account) dispatch(signTx(account))
} else { } else {
Alert.alert('Invalid PIN') Alert.alert('Invalid PIN')
} }
......
...@@ -7,7 +7,7 @@ import QrScanner from '../components/QrScanner' ...@@ -7,7 +7,7 @@ import QrScanner from '../components/QrScanner'
import { selectAccount } from '../actions/accounts' import { selectAccount } from '../actions/accounts'
import { scannedTx } from '../actions/transactions' import { scannedTx } from '../actions/transactions'
import transaction from '../util/transaction' import transaction from '../util/transaction'
import keccak from '../util/native' import { keccak } from '../util/native'
import store from '../util/store' import store from '../util/store'
var scanning = false var scanning = false
...@@ -20,7 +20,7 @@ function displayAlert (text) { ...@@ -20,7 +20,7 @@ function displayAlert (text) {
function findAccountWithAddress (address) { function findAccountWithAddress (address) {
return store.getState().accounts.all.find(account => { return store.getState().accounts.all.find(account => {
return account.address.toLowerCase() === address return account.address.toLowerCase() === address.toLowerCase()
}) })
} }
...@@ -31,41 +31,35 @@ async function onScannedTransaction (data, dispatch) { ...@@ -31,41 +31,35 @@ async function onScannedTransaction (data, dispatch) {
} }
scanning = true scanning = true
let txRequest = JSON.parse(data) let txRequest = JSON.parse(data)
switch (txRequest.action) { if (txRequest.action === 'signTransaction') {
case 'signTransaction': { let account = findAccountWithAddress(txRequest.data.account)
let account = findAccountWithAddress(txRequest.data.account) if (!account) {
if (!account) { displayAlert('Invalid sender address ' + txRequest.data.account)
displayAlert('Invalid sender address ' + txRequest.data.account) return
return
}
let tx = await transaction(txRequest.data.rlp)
let hash = await keccak(txRequest.data.rlp)
dispatch(selectAccount(account))
dispatch(scannedTx(hash, tx))
break
}
case 'signTransactionHash': {
let account = findAccountWithAddress(txRequest.data.account)
if (!account) {
displayAlert('Invalid sender address ' + txRequest.data.account)
return
}
let details = txRequest.data.details
let hash = txRequest.data.hash
dispatch(selectAccount(account))
dispatch(scannedTx(hash, details))
break
} }
default: { let tx = await transaction(txRequest.data.rlp)
displayAlert('Invalid request') let hash = await keccak(txRequest.data.rlp)
dispatch(selectAccount(account))
dispatch(scannedTx(hash, tx))
} else if (txRequest.action === 'signTransactionHash') {
let account = findAccountWithAddress(txRequest.data.account)
if (!account) {
displayAlert('Invalid sender address ' + txRequest.data.account)
return return
} }
let details = txRequest.data.details
let hash = txRequest.data.hash
dispatch(selectAccount(account))
dispatch(scannedTx(hash, details))
} else {
displayAlert('Invalid request')
return
} }
Actions.txDetails() Actions.txDetails()
scanning = false scanning = false
} catch (e) { } catch (e) {
console.log(e) console.log(e)
displayAlert('Invalid transaction') displayAlert('Invalid transaction ' + e)
} }
} }
......
'use strict' 'use strict'
import { NEW_SCANNED_TX, SIGNED_TX } from '../constants/TransactionActions' import { NEW_SCANNED_TX, SIGN_TX } from '../constants/TransactionActions'
const initialState = { const initialState = {
pendingTransaction: { pendingTransaction: {
...@@ -21,7 +21,7 @@ export default function transactions (state = initialState, action) { ...@@ -21,7 +21,7 @@ export default function transactions (state = initialState, action) {
transaction: action.transaction transaction: action.transaction
} }
}) })
case SIGNED_TX: case SIGN_TX:
return Object.assign({}, state, { return Object.assign({}, state, {
signedTransaction: { signedTransaction: {
signature: action.signature signature: action.signature
......
'use strict' 'use strict'
import { createStore } from 'redux' import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import reducers from '../reducers' import reducers from '../reducers'
export default createStore(reducers) export default createStore(reducers, applyMiddleware(thunk))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment