2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-12-27 11:01:16 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import { api } from '../parity.js';
|
|
|
|
import postTx from '../util/post-tx';
|
2017-01-04 15:14:37 +01:00
|
|
|
import { getOwner } from '../util/registry';
|
|
|
|
|
|
|
|
export const clearError = () => ({
|
|
|
|
type: 'clearError'
|
|
|
|
});
|
2016-12-27 11:01:16 +01:00
|
|
|
|
|
|
|
export const start = (action, name, address) => ({ type: `reverse ${action} start`, name, address });
|
|
|
|
|
|
|
|
export const success = (action) => ({ type: `reverse ${action} success` });
|
|
|
|
|
2017-01-04 15:14:37 +01:00
|
|
|
export const fail = (action, error) => ({ type: `reverse ${action} fail`, error });
|
2016-12-27 11:01:16 +01:00
|
|
|
|
|
|
|
export const propose = (name, address) => (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2017-03-22 11:56:52 +01:00
|
|
|
const accountAddress = state.accounts.selected;
|
2016-12-27 11:01:16 +01:00
|
|
|
const contract = state.contract;
|
2017-01-04 15:14:37 +01:00
|
|
|
|
2017-03-22 11:56:52 +01:00
|
|
|
if (!contract || !accountAddress) {
|
2016-12-27 11:01:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = name.toLowerCase();
|
2017-01-04 15:14:37 +01:00
|
|
|
dispatch(start('propose', name, address));
|
2016-12-27 11:01:16 +01:00
|
|
|
|
2017-01-04 15:14:37 +01:00
|
|
|
return getOwner(contract, name)
|
|
|
|
.then((owner) => {
|
2017-03-22 11:56:52 +01:00
|
|
|
if (owner.toLowerCase() !== accountAddress.toLowerCase()) {
|
2017-01-04 15:14:37 +01:00
|
|
|
throw new Error(`you are not the owner of "${name}"`);
|
|
|
|
}
|
2016-12-27 11:01:16 +01:00
|
|
|
|
2017-01-04 15:14:37 +01:00
|
|
|
const { proposeReverse } = contract.instance;
|
2016-12-27 11:01:16 +01:00
|
|
|
|
2017-01-04 15:14:37 +01:00
|
|
|
const options = {
|
2017-03-22 11:56:52 +01:00
|
|
|
from: accountAddress
|
2017-01-04 15:14:37 +01:00
|
|
|
};
|
2016-12-27 11:01:16 +01:00
|
|
|
|
2017-01-04 15:14:37 +01:00
|
|
|
const values = [
|
|
|
|
name,
|
|
|
|
address
|
|
|
|
];
|
|
|
|
|
|
|
|
return postTx(api, proposeReverse, options, values);
|
|
|
|
})
|
2016-12-27 11:01:16 +01:00
|
|
|
.then((txHash) => {
|
|
|
|
dispatch(success('propose'));
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2017-01-04 15:14:37 +01:00
|
|
|
if (err.type !== 'REQUEST_REJECTED') {
|
|
|
|
console.error(`error proposing ${name}`, err);
|
|
|
|
return dispatch(fail('propose', err));
|
2016-12-27 11:01:16 +01:00
|
|
|
}
|
2017-01-04 15:14:37 +01:00
|
|
|
|
2016-12-27 11:01:16 +01:00
|
|
|
dispatch(fail('propose'));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const confirm = (name) => (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2017-03-22 11:56:52 +01:00
|
|
|
const accountAddress = state.accounts.selected;
|
2016-12-27 11:01:16 +01:00
|
|
|
const contract = state.contract;
|
2017-01-04 15:14:37 +01:00
|
|
|
|
2017-03-22 11:56:52 +01:00
|
|
|
if (!contract || !accountAddress) {
|
2016-12-27 11:01:16 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-01-04 15:14:37 +01:00
|
|
|
|
2016-12-27 11:01:16 +01:00
|
|
|
name = name.toLowerCase();
|
2017-01-04 15:14:37 +01:00
|
|
|
dispatch(start('confirm', name));
|
2016-12-27 11:01:16 +01:00
|
|
|
|
2017-01-04 15:14:37 +01:00
|
|
|
return getOwner(contract, name)
|
|
|
|
.then((owner) => {
|
2017-03-22 11:56:52 +01:00
|
|
|
if (owner.toLowerCase() !== accountAddress.toLowerCase()) {
|
2017-01-04 15:14:37 +01:00
|
|
|
throw new Error(`you are not the owner of "${name}"`);
|
|
|
|
}
|
2016-12-27 11:01:16 +01:00
|
|
|
|
2017-01-04 15:14:37 +01:00
|
|
|
const { confirmReverse } = contract.instance;
|
2016-12-27 11:01:16 +01:00
|
|
|
|
2017-01-04 15:14:37 +01:00
|
|
|
const options = {
|
2017-03-22 11:56:52 +01:00
|
|
|
from: accountAddress
|
2017-01-04 15:14:37 +01:00
|
|
|
};
|
2016-12-27 11:01:16 +01:00
|
|
|
|
2017-01-04 15:14:37 +01:00
|
|
|
const values = [
|
|
|
|
name
|
|
|
|
];
|
|
|
|
|
|
|
|
return postTx(api, confirmReverse, options, values);
|
|
|
|
})
|
2016-12-27 11:01:16 +01:00
|
|
|
.then((txHash) => {
|
|
|
|
dispatch(success('confirm'));
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2017-01-04 15:14:37 +01:00
|
|
|
if (err.type !== 'REQUEST_REJECTED') {
|
|
|
|
console.error(`error confirming ${name}`, err);
|
|
|
|
return dispatch(fail('confirm', err));
|
2016-12-27 11:01:16 +01:00
|
|
|
}
|
2017-01-04 15:14:37 +01:00
|
|
|
|
2016-12-27 11:01:16 +01:00
|
|
|
dispatch(fail('confirm'));
|
|
|
|
});
|
|
|
|
};
|
2017-01-04 15:14:37 +01:00
|
|
|
|