2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-12-06 09:37:59 +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/>.
|
|
|
|
|
2017-03-28 14:34:31 +02:00
|
|
|
import { noop } from 'lodash';
|
2016-12-06 09:37:59 +01:00
|
|
|
import { observable, computed, action, transaction } from 'mobx';
|
2017-01-26 16:11:04 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
2016-12-06 09:37:59 +01:00
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
import Contract from '~/api/contract';
|
2017-01-26 16:11:04 +01:00
|
|
|
import Contracts from '~/contracts';
|
2016-12-07 12:47:44 +01:00
|
|
|
import { wallet as walletAbi } from '~/contracts/abi';
|
2017-03-07 20:19:55 +01:00
|
|
|
import { wallet as walletCode, walletLibrary as walletLibraryCode, walletLibraryRegKey, fullWalletCode } from '~/contracts/code/wallet';
|
2016-12-07 12:47:44 +01:00
|
|
|
|
2016-12-09 13:44:10 +01:00
|
|
|
import { validateUint, validateAddress, validateName } from '~/util/validation';
|
2016-12-10 01:26:28 +01:00
|
|
|
import { toWei } from '~/api/util/wei';
|
2017-05-16 16:38:16 +02:00
|
|
|
import { deploy, getSender, loadSender, setSender } from '~/util/tx';
|
2016-12-07 12:47:44 +01:00
|
|
|
import WalletsUtils from '~/util/wallets';
|
2016-12-06 09:37:59 +01:00
|
|
|
|
|
|
|
const STEPS = {
|
2017-01-26 16:11:04 +01:00
|
|
|
TYPE: {
|
|
|
|
title: (
|
|
|
|
<FormattedMessage
|
|
|
|
id='createWallet.steps.type'
|
|
|
|
defaultMessage='wallet type'
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
DETAILS: {
|
|
|
|
title: (
|
|
|
|
<FormattedMessage
|
|
|
|
id='createWallet.steps.details'
|
|
|
|
defaultMessage='wallet details'
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
INFO: {
|
|
|
|
title: (
|
|
|
|
<FormattedMessage
|
|
|
|
id='createWallet.steps.info'
|
|
|
|
defaultMessage='wallet informaton'
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2016-12-06 09:37:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class CreateWalletStore {
|
|
|
|
@observable step = null;
|
|
|
|
@observable txhash = null;
|
2017-03-28 14:34:31 +02:00
|
|
|
@observable walletType = 'MULTISIG';
|
2016-12-06 09:37:59 +01:00
|
|
|
|
|
|
|
@observable wallet = {
|
|
|
|
account: '',
|
|
|
|
address: '',
|
|
|
|
owners: [],
|
|
|
|
required: 1,
|
2016-12-10 01:26:28 +01:00
|
|
|
daylimit: toWei(1),
|
2016-12-06 09:37:59 +01:00
|
|
|
|
|
|
|
name: '',
|
|
|
|
description: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
@observable errors = {
|
|
|
|
account: null,
|
2016-12-07 12:47:44 +01:00
|
|
|
address: null,
|
2016-12-06 09:37:59 +01:00
|
|
|
owners: null,
|
|
|
|
required: null,
|
|
|
|
daylimit: null,
|
2016-12-07 12:47:44 +01:00
|
|
|
name: null
|
2016-12-06 09:37:59 +01:00
|
|
|
};
|
|
|
|
|
2017-03-28 14:34:31 +02:00
|
|
|
onClose = noop;
|
|
|
|
onSetRequest = noop;
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
@computed get stage () {
|
2016-12-07 12:47:44 +01:00
|
|
|
return this.stepsKeys.findIndex((k) => k === this.step);
|
2016-12-06 09:37:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed get hasErrors () {
|
2016-12-07 12:47:44 +01:00
|
|
|
return !!Object.keys(this.errors)
|
|
|
|
.filter((errorKey) => {
|
|
|
|
if (this.walletType === 'WATCH') {
|
|
|
|
return ['address', 'name'].includes(errorKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return errorKey !== 'address';
|
|
|
|
})
|
|
|
|
.find((key) => !!this.errors[key]);
|
2016-12-06 09:37:59 +01:00
|
|
|
}
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
@computed get stepsKeys () {
|
|
|
|
return this.steps.map((s) => s.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed get steps () {
|
|
|
|
return Object
|
|
|
|
.keys(STEPS)
|
|
|
|
.map((key) => {
|
|
|
|
return {
|
|
|
|
...STEPS[key],
|
|
|
|
key
|
|
|
|
};
|
|
|
|
})
|
2017-03-28 14:34:31 +02:00
|
|
|
.filter((step) => this.walletType === 'WATCH' || step.key !== 'INFO');
|
2016-12-07 12:47:44 +01:00
|
|
|
}
|
|
|
|
|
2017-03-28 14:34:31 +02:00
|
|
|
constructor (api, { accounts, onClose, onSetRequest }) {
|
2016-12-06 09:37:59 +01:00
|
|
|
this.api = api;
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
this.step = this.stepsKeys[0];
|
2017-05-16 16:38:16 +02:00
|
|
|
this.wallet.account = getSender() || Object.values(accounts)[0].address;
|
2016-12-07 12:47:44 +01:00
|
|
|
this.validateWallet(this.wallet);
|
2017-03-28 14:34:31 +02:00
|
|
|
this.onClose = onClose;
|
|
|
|
this.onSetRequest = onSetRequest;
|
2017-05-16 16:38:16 +02:00
|
|
|
|
|
|
|
loadSender(this.api)
|
|
|
|
.then((defaultAccount) => {
|
|
|
|
if (defaultAccount !== this.wallet.account) {
|
|
|
|
this.onChange({ account: defaultAccount });
|
|
|
|
}
|
|
|
|
});
|
2016-12-07 12:47:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@action onTypeChange = (type) => {
|
|
|
|
this.walletType = type;
|
|
|
|
this.validateWallet(this.wallet);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action onNext = () => {
|
|
|
|
const stepIndex = this.stepsKeys.findIndex((k) => k === this.step) + 1;
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
this.step = this.stepsKeys[stepIndex];
|
2016-12-06 09:37:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@action onChange = (_wallet) => {
|
|
|
|
const newWallet = Object.assign({}, this.wallet, _wallet);
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
this.validateWallet(newWallet);
|
|
|
|
}
|
2016-12-06 09:37:59 +01:00
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
@action onAdd = () => {
|
|
|
|
if (this.hasErrors) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const walletContract = new Contract(this.api, walletAbi).at(this.wallet.address);
|
|
|
|
|
|
|
|
return Promise
|
|
|
|
.all([
|
|
|
|
WalletsUtils.fetchRequire(walletContract),
|
|
|
|
WalletsUtils.fetchOwners(walletContract),
|
|
|
|
WalletsUtils.fetchDailylimit(walletContract)
|
|
|
|
])
|
|
|
|
.then(([ require, owners, dailylimit ]) => {
|
|
|
|
transaction(() => {
|
|
|
|
this.wallet.owners = owners;
|
|
|
|
this.wallet.required = require.toNumber();
|
|
|
|
this.wallet.dailylimit = dailylimit.limit;
|
2017-03-07 20:19:55 +01:00
|
|
|
|
|
|
|
this.wallet = this.getWalletWithMeta(this.wallet);
|
2016-12-07 12:47:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return this.addWallet(this.wallet);
|
|
|
|
});
|
2016-12-06 09:37:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@action onCreate = () => {
|
|
|
|
if (this.hasErrors) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
const { account, owners, required, daylimit } = this.wallet;
|
2016-12-06 09:37:59 +01:00
|
|
|
|
2016-12-09 16:24:11 +01:00
|
|
|
Contracts
|
|
|
|
.get()
|
|
|
|
.registry
|
|
|
|
.lookupAddress(walletLibraryRegKey)
|
2017-02-07 19:42:21 +01:00
|
|
|
.catch(() => {
|
|
|
|
return null; // exception when registry is not available
|
|
|
|
})
|
2016-12-09 16:24:11 +01:00
|
|
|
.then((address) => {
|
2017-03-07 20:19:55 +01:00
|
|
|
if (!address || /^(0x)?0*$/.test(address)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that it's actually the expected code
|
|
|
|
return this.api.eth
|
|
|
|
.getCode(address)
|
|
|
|
.then((code) => {
|
|
|
|
const strippedCode = code.replace(/^0x/, '');
|
|
|
|
|
|
|
|
// The actual deployed code is included in the wallet
|
|
|
|
// library code (which might have some more data)
|
|
|
|
if (walletLibraryCode.indexOf(strippedCode) >= 0) {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then((address) => {
|
|
|
|
let code = fullWalletCode;
|
|
|
|
|
|
|
|
if (address) {
|
|
|
|
const walletLibraryAddress = address.replace(/^0x/, '').toLowerCase();
|
|
|
|
|
|
|
|
code = walletCode.replace(/(_)+WalletLibrary(_)+/g, walletLibraryAddress);
|
|
|
|
} else {
|
|
|
|
console.warn('wallet library has not been found in the registry');
|
|
|
|
}
|
2016-12-09 16:24:11 +01:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
data: code,
|
|
|
|
from: account
|
|
|
|
};
|
2016-12-06 09:37:59 +01:00
|
|
|
|
2017-03-07 20:19:55 +01:00
|
|
|
const contract = this.api.newContract(walletAbi);
|
|
|
|
|
2017-05-16 16:38:16 +02:00
|
|
|
setSender(account);
|
2017-03-07 20:19:55 +01:00
|
|
|
this.wallet = this.getWalletWithMeta(this.wallet);
|
2017-03-28 14:34:31 +02:00
|
|
|
this.onClose();
|
|
|
|
return deploy(contract, options, [ owners, required, daylimit ])
|
|
|
|
.then((requestId) => {
|
|
|
|
const metadata = { ...this.wallet.metadata, deployment: true };
|
2016-12-06 09:37:59 +01:00
|
|
|
|
2017-03-28 14:34:31 +02:00
|
|
|
this.onSetRequest(requestId, { metadata }, false);
|
|
|
|
});
|
2016-12-06 09:37:59 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
@action addWallet = (wallet) => {
|
2017-03-07 20:19:55 +01:00
|
|
|
const { address, name, metadata } = wallet;
|
2016-12-07 12:47:44 +01:00
|
|
|
|
|
|
|
return Promise
|
|
|
|
.all([
|
|
|
|
this.api.parity.setAccountName(address, name),
|
2017-03-07 20:19:55 +01:00
|
|
|
this.api.parity.setAccountMeta(address, metadata)
|
2016-12-07 12:47:44 +01:00
|
|
|
])
|
|
|
|
.then(() => {
|
|
|
|
this.step = 'INFO';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-07 20:19:55 +01:00
|
|
|
getWalletWithMeta = (wallet) => {
|
|
|
|
const { name, description } = wallet;
|
|
|
|
|
|
|
|
const metadata = {
|
|
|
|
abi: walletAbi,
|
|
|
|
wallet: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
deleted: false,
|
|
|
|
tags: [ 'wallet' ],
|
|
|
|
description,
|
|
|
|
name
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
...wallet,
|
|
|
|
metadata
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
@action validateWallet = (_wallet) => {
|
|
|
|
const addressValidation = validateAddress(_wallet.address);
|
2016-12-06 09:37:59 +01:00
|
|
|
const accountValidation = validateAddress(_wallet.account);
|
|
|
|
const requiredValidation = validateUint(_wallet.required);
|
|
|
|
const daylimitValidation = validateUint(_wallet.daylimit);
|
|
|
|
const nameValidation = validateName(_wallet.name);
|
|
|
|
|
|
|
|
const errors = {
|
2016-12-07 12:47:44 +01:00
|
|
|
address: addressValidation.addressError,
|
2016-12-06 09:37:59 +01:00
|
|
|
account: accountValidation.addressError,
|
|
|
|
required: requiredValidation.valueError,
|
|
|
|
daylimit: daylimitValidation.valueError,
|
|
|
|
name: nameValidation.nameError
|
|
|
|
};
|
|
|
|
|
|
|
|
const wallet = {
|
|
|
|
..._wallet,
|
2016-12-07 12:47:44 +01:00
|
|
|
address: addressValidation.address,
|
2016-12-06 09:37:59 +01:00
|
|
|
account: accountValidation.address,
|
|
|
|
required: requiredValidation.value,
|
|
|
|
daylimit: daylimitValidation.value,
|
|
|
|
name: nameValidation.name
|
|
|
|
};
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
transaction(() => {
|
|
|
|
this.wallet = wallet;
|
|
|
|
this.errors = errors;
|
|
|
|
});
|
2016-12-06 09:37:59 +01:00
|
|
|
}
|
|
|
|
}
|