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 update.submitUpdateForm = (data, xpub) => {
37 let promise = Promise.resolve()
39 return function(dispatch) {
40 return promise.then(() => clientApi().updateAlias({
42 password: data.password,
45 if (resp.status === 'fail') {
48 dispatch(update.updated())
51 pathname: `/${type}s/${xpub}`,
60 const resetPassword = {
61 submitResetForm: (params) => {
62 let promise = Promise.resolve()
63 return (dispatch) => {
64 return promise.then(() => clientApi().resetPassword(params).then((resp) => {
65 if(resp.data.changed){
66 dispatch({ type: `RESET_PASSWORD_${type.toUpperCase()}`, resp })
68 pathname: `/${type}s/${params.xpub}`,
74 throw {code: 'F_BTM001'}
81 const checkPassword = (data) => (dispatch) => {
82 return clientApi().checkPassword(data)
84 if(resp.status === 'fail'){
86 }else if(!resp.data.checkResult){
87 throw {code: 'F_BTM000'}
89 dispatch({ type: 'KEY_PASSWORD_SUCCESS'})
93 const createExport = (arg, fileName) => (dispatch) => {
94 clientApi().export(arg).then(resp => {
95 if(resp.status == 'success'){
96 const privateKey = resp.data.privateKey
98 var element = document.createElement('a')
99 element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(privateKey))
100 element.setAttribute('download', fileName)
101 element.style.display = 'none'
102 document.body.appendChild(element)
105 document.body.removeChild(element)
106 }else if(resp.status == 'fail'){
107 dispatch({ type: 'ERROR', payload: {message: resp.msg} })
112 const createSuccess = ()=> (dispatch) =>{
113 dispatch(create.created())
114 dispatch(push('/keys'))