Skip to content

Commit

Permalink
Add preparations for sign modal and state variables
Browse files Browse the repository at this point in the history
  • Loading branch information
xdzurman committed Oct 15, 2020
1 parent 96d169a commit 3f644e8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 25 deletions.
77 changes: 65 additions & 12 deletions app/frontend/actions.ts
Expand Up @@ -1203,30 +1203,80 @@ export default ({setState, getState}: {setState: SetStateFn; getState: GetStateF
})
const fileJson = await JSON.parse(fileObj)
const deserializedTx = deserializeTransaction(fileJson)
// setCertFile(deserializedCert)
const poolTxValidationError = validatePoolRegUnsignedTx(deserializedTx)
if (poolTxValidationError) {
setErrorState('poolRegTxError', poolTxValidationError)
stopLoadingAction(state, {})
return
}
console.log(deserializedTx)
const poolTxPlan = unsignedPoolTxToTxPlan(deserializedTx)
const txAux = await wallet.prepareTxAux(poolTxPlan, deserializedTx.ttl)
console.log(poolTxPlan)
console.log(txAux)

setState({
poolCertTxVars: {
shouldShowPoolCertSignModal: false,
deserializedTx,
signature: null,
},
})
stopLoadingAction(state, {})
// setCertFileError(undefined)
} catch (err) {
debugLog(`Certificate file parsing failure: ${err}`)
stopLoadingAction(state, {})
setErrorState('poolRegTxError', err)
// setCertFileError(
// 'Provided file is incorrect. To continue, load a valid JSON certificate file.'
// )
}
// return true
}

const openPoolCertificateTxModal = (state) => {
setState({
poolCertTxVars: {
...state.poolCertTxVars,
shouldShowPoolCertSignModal: true,
},
})
}

const closePoolCertificateTxModal = (state) => {
setState({
poolCertTxVars: {
...state.poolCertTxVars,
shouldShowPoolCertSignModal: false,
},
})
}

const resetPoolCertificateTxVars = (state) => {
setState({
poolCertTxVars: {
shouldShowPoolCertSignModal: false,
deserializedTx: null,
signature: null,
},
})
}

const signPoolCertificateTx = async (state) => {
setState({waitingForHwWallet: true})
loadingAction(state, `Waiting for ${state.hwWalletName}...`)
try {
const deserializedTx = state.poolCertTxVars.deserializedTx
const poolTxPlan = unsignedPoolTxToTxPlan(deserializedTx)
const txAux = await wallet.prepareTxAux(poolTxPlan, deserializedTx.ttl)
const signature = await wallet.signTxAux(txAux)

setState({
poolCertTxVars: {
...state.poolCertTxVars,
shouldShowPoolCertSignModal: false,
signature,
},
})
} catch (e) {
} finally {
setState({
poolCertTxVars: {
...state.poolCertTxVars,
shouldShowPoolCertSignModal: false,
},
})
}
}

const downloadPoolSignature = (state) => {
Expand All @@ -1235,6 +1285,7 @@ export default ({setState, getState}: {setState: SetStateFn; getState: GetStateF
type: 'application/json;charset=utf-8',
})
saveAs(blob, 'PoolSignature.json')
resetPoolCertificateTxVars(state)
}

return {
Expand Down Expand Up @@ -1288,5 +1339,7 @@ export default ({setState, getState}: {setState: SetStateFn; getState: GetStateF
closeInfoModal,
loadPoolCertificateTx,
downloadPoolSignature,
openPoolCertificateTxModal,
closePoolCertificateTxModal,
}
}
30 changes: 17 additions & 13 deletions app/frontend/components/pages/advanced/poolOwner.tsx
Expand Up @@ -12,6 +12,10 @@ interface Props {
loadPoolCertificateTx: any
poolRegTxError: any
downloadPoolSignature: any
openPoolCertificateTxModal: any
shouldShowPoolCertSignModal: boolean
deserializedTx: any
signature: any
}

const PoolOwnerCard = ({
Expand All @@ -20,23 +24,20 @@ const PoolOwnerCard = ({
loadPoolCertificateTx,
poolRegTxError,
downloadPoolSignature,
openPoolCertificateTxModal,
shouldShowPoolCertSignModal,
deserializedTx,
signature,
}: Props) => {
const [fileName, setFileName] = useState<string>('')
const [certFile, setCertFile] = useState<any>(undefined)

const signCertificateFile = async () => {
loadingAction('Signing certificate file')
try {
// TODO: sign
console.log(certFile)
} catch (e) {
stopLoadingAction()
}
const signCertificateHandler = () => {
loadingAction('Loading modal...')
openPoolCertificateTxModal()
}

const readFile = async (targetFile) => {
setFileName(targetFile.name)
setCertFile(undefined)

const reader = new FileReader()
await reader.readAsText(targetFile)
Expand Down Expand Up @@ -75,8 +76,8 @@ const PoolOwnerCard = ({
)}
<div className="pool-owner-content-bottom">
<button
disabled={fileName === '' || !!error}
onClick={signCertificateFile}
disabled={fileName === '' || !!error || !deserializedTx}
onClick={signCertificateHandler}
className="button primary"
{...tooltip(
'Please insert a valid certificate\nJSON file before proceeding.',
Expand All @@ -90,7 +91,7 @@ const PoolOwnerCard = ({
</button>
<button
className="button secondary"
disabled //
disabled={!signature} //
{...tooltip(
'You have to sign the certificate\nto be able to download it.',
true //
Expand All @@ -107,6 +108,9 @@ const PoolOwnerCard = ({
export default connect(
(state) => ({
poolRegTxError: state.poolRegTxError,
shouldShowPoolCertSignModal: state.poolCertTxVars.shouldShowPoolCertSignModal,
deserializedTx: state.poolCertTxVars.deserializedTx,
signature: state.poolCertTxVars.signature,
}),
actions
)(PoolOwnerCard)
10 changes: 10 additions & 0 deletions app/frontend/state.ts
Expand Up @@ -73,6 +73,11 @@ export interface State {
shouldShowTransactionErrorModal?: boolean
shouldShowThanksForDonation?: boolean
shouldShowContactFormModal?: boolean
poolCertTxVars: {
shouldShowPoolCertSignModal: boolean
deserializedTx: any
signature: any
}

calculatingFee?: boolean
transactionFee?: any
Expand Down Expand Up @@ -234,6 +239,11 @@ const initialState: State = {
txConfirmType: '',
txSuccessTab: '',
keepConfirmationDialogOpen: false,
poolCertTxVars: {
shouldShowPoolCertSignModal: false,
deserializedTx: null,
signature: null,
},
}
export type SetStateFn = (newState: Partial<State>) => void
export type GetStateFn = () => State
Expand Down

0 comments on commit 3f644e8

Please sign in to comment.