1 import { baseListActions, baseCreateActions, baseUpdateActions } from 'features/shared/actions'
2 import { chainClient } from 'utility/environment'
3 import {push} from 'react-router-redux'
6 const clientApi = () => chainClient().mockHsm.keys
8 const list = baseListActions(type, {
12 const create = baseCreateActions(type, {
17 const update = baseUpdateActions(type, {
22 create.submitForm = (data) => function (dispatch) {
23 if (typeof data.alias == 'string') data.alias = data.alias.trim()
25 return clientApi().create(data)
27 if (resp.status === 'fail') {
31 dispatch({type: 'NEW_KEY', data: resp.data.mnemonic})
32 dispatch( push('/keys/mnemonic') )
36 const resetPassword = {
37 submitResetForm: (params) => {
38 let promise = Promise.resolve()
39 return (dispatch) => {
40 return promise.then(() => clientApi().resetPassword(params).then((resp) => {
41 if(resp.data.changed){
42 dispatch({ type: `RESET_PASSWORD_${type.toUpperCase()}`, resp })
44 pathname: `/${type}s/${params.xpub}`,
50 throw {code: 'F_BTM001'}
57 const checkPassword = (data) => (dispatch) => {
58 return clientApi().checkPassword(data)
60 if(resp.status === 'fail'){
62 }else if(!resp.data.checkResult){
63 throw {code: 'F_BTM000'}
65 dispatch({ type: 'KEY_PASSWORD_SUCCESS'})
69 const createExport = (arg, fileName) => (dispatch) => {
70 clientApi().export(arg).then(resp => {
71 if(resp.status == 'success'){
72 const privateKey = resp.data.privateKey
74 var element = document.createElement('a')
75 element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(privateKey))
76 element.setAttribute('download', fileName)
77 element.style.display = 'none'
78 document.body.appendChild(element)
81 document.body.removeChild(element)
82 }else if(resp.status == 'fail'){
83 dispatch({ type: 'ERROR', payload: {message: resp.msg} })
88 const createSuccess = ()=> (dispatch) =>{
89 dispatch(create.created())
90 dispatch(push('/keys'))