merge master into sms-verification-modal

This commit is contained in:
Jannis R 2016-11-17 12:40:22 +01:00
commit a59526099d
No known key found for this signature in database
GPG Key ID: 0FE83946296A88A5
46 changed files with 825 additions and 295 deletions

2
Cargo.lock generated
View File

@ -1249,7 +1249,7 @@ dependencies = [
[[package]] [[package]]
name = "parity-ui-precompiled" name = "parity-ui-precompiled"
version = "1.4.0" version = "1.4.0"
source = "git+https://github.com/ethcore/js-precompiled.git#985a6d9cf9aa4621172fcb8e4bf6955f33d5e2a3" source = "git+https://github.com/ethcore/js-precompiled.git#957c5a66c33f3b06a7ae804ac5edc59c20e4535b"
dependencies = [ dependencies = [
"parity-dapps-glue 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "parity-dapps-glue 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View File

@ -20,6 +20,7 @@ use std::collections::HashMap;
use time; use time;
use ethkey::Address; use ethkey::Address;
use {json, SafeAccount, Error}; use {json, SafeAccount, Error};
use json::UUID;
use super::KeyDirectory; use super::KeyDirectory;
const IGNORED_FILES: &'static [&'static str] = &["thumbs.db", "address_book.json"]; const IGNORED_FILES: &'static [&'static str] = &["thumbs.db", "address_book.json"];
@ -112,7 +113,7 @@ impl KeyDirectory for DiskDirectory {
// build file path // build file path
let filename = account.filename.as_ref().cloned().unwrap_or_else(|| { let filename = account.filename.as_ref().cloned().unwrap_or_else(|| {
let timestamp = time::strftime("%Y-%m-%dT%H-%M-%S", &time::now_utc()).expect("Time-format string is valid."); let timestamp = time::strftime("%Y-%m-%dT%H-%M-%S", &time::now_utc()).expect("Time-format string is valid.");
format!("UTC--{}Z--{:?}", timestamp, account.address) format!("UTC--{}Z--{}", timestamp, UUID::from(account.id))
}); });
// update account filename // update account filename

View File

@ -1,6 +1,6 @@
{ {
"name": "parity.js", "name": "parity.js",
"version": "0.2.48", "version": "0.2.50",
"main": "release/index.js", "main": "release/index.js",
"jsnext:main": "src/index.js", "jsnext:main": "src/index.js",
"author": "Parity Team <admin@parity.io>", "author": "Parity Team <admin@parity.io>",
@ -122,6 +122,7 @@
"brace": "^0.9.0", "brace": "^0.9.0",
"bytes": "^2.4.0", "bytes": "^2.4.0",
"chart.js": "^2.3.0", "chart.js": "^2.3.0",
"es6-error": "^4.0.0",
"es6-promise": "^3.2.1", "es6-promise": "^3.2.1",
"ethereumjs-tx": "^1.1.2", "ethereumjs-tx": "^1.1.2",
"file-saver": "^1.3.3", "file-saver": "^1.3.3",

View File

@ -68,11 +68,13 @@ if [ "$BRANCH" == "master" ]; then
fi fi
echo "*** Updating cargo parity-ui-precompiled#$PRECOMPILED_HASH" echo "*** Updating cargo parity-ui-precompiled#$PRECOMPILED_HASH"
git submodule update
cargo update -p parity-ui-precompiled cargo update -p parity-ui-precompiled
# --precise "$PRECOMPILED_HASH" # --precise "$PRECOMPILED_HASH"
echo "*** Committing updated files" echo "*** Committing updated files"
git add . git add js
git add Cargo.lock
git commit -m "[ci skip] js-precompiled $UTCDATE" git commit -m "[ci skip] js-precompiled $UTCDATE"
git push origin HEAD:refs/heads/$BRANCH 2>$GITLOG git push origin HEAD:refs/heads/$BRANCH 2>$GITLOG

View File

@ -0,0 +1,53 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// 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 ExtendableError from 'es6-error';
export const ERROR_CODES = {
UNSUPPORTED_REQUEST: -32000,
NO_WORK: -32001,
NO_AUTHOR: -32002,
NO_NEW_WORK: -32003,
NOT_ENOUGH_DATA: -32006,
UNKNOWN_ERROR: -32009,
TRANSACTION_ERROR: -32010,
EXECUTION_ERROR: -32015,
ACCOUNT_LOCKED: -32020,
PASSWORD_INVALID: -32021,
ACCOUNT_ERROR: -32023,
SIGNER_DISABLED: -32030,
DAPPS_DISABLED: -32031,
NETWORK_DISABLED: -32035,
REQUEST_REJECTED: -32040,
REQUEST_REJECTED_LIMIT: -32041,
REQUEST_NOT_FOUND: -32042,
COMPILATION_ERROR: -32050,
ENCRYPTION_ERROR: -32055,
FETCH_ERROR: -32060
};
export default class TransportError extends ExtendableError {
constructor (method, code, message) {
const m = `${method}: ${code}: ${message}`;
super(m);
this.code = code;
this.type = Object.keys(ERROR_CODES).find((k) => ERROR_CODES[k] === code) || '';
this.method = method;
this.text = message;
}
}

View File

@ -16,6 +16,7 @@
import { Logging } from '../../subscriptions'; import { Logging } from '../../subscriptions';
import JsonRpcBase from '../jsonRpcBase'; import JsonRpcBase from '../jsonRpcBase';
import TransportError from '../error';
/* global fetch */ /* global fetch */
export default class Http extends JsonRpcBase { export default class Http extends JsonRpcBase {
@ -73,7 +74,8 @@ export default class Http extends JsonRpcBase {
this.error(JSON.stringify(response)); this.error(JSON.stringify(response));
console.error(`${method}(${JSON.stringify(params)}): ${response.error.code}: ${response.error.message}`); console.error(`${method}(${JSON.stringify(params)}): ${response.error.code}: ${response.error.message}`);
throw new Error(`${method}: ${response.error.code}: ${response.error.message}`); const error = new TransportError(method, response.error.code, response.error.message);
throw error;
} }
this.log(JSON.stringify(response)); this.log(JSON.stringify(response));

View File

@ -16,3 +16,4 @@
export Http from './http'; export Http from './http';
export Ws from './ws'; export Ws from './ws';
export TransportError from './error.js';

View File

@ -18,6 +18,7 @@ import { keccak_256 } from 'js-sha3'; // eslint-disable-line camelcase
import { Logging } from '../../subscriptions'; import { Logging } from '../../subscriptions';
import JsonRpcBase from '../jsonRpcBase'; import JsonRpcBase from '../jsonRpcBase';
import TransportError from '../error';
/* global WebSocket */ /* global WebSocket */
export default class Ws extends JsonRpcBase { export default class Ws extends JsonRpcBase {
@ -109,7 +110,9 @@ export default class Ws extends JsonRpcBase {
console.error(`${method}(${JSON.stringify(params)}): ${result.error.code}: ${result.error.message}`); console.error(`${method}(${JSON.stringify(params)}): ${result.error.code}: ${result.error.message}`);
reject(new Error(`${method}: ${result.error.code}: ${result.error.message}`)); const error = new TransportError(method, result.error.code, result.error.message);
reject(error);
delete this._messages[result.id]; delete this._messages[result.id];
return; return;
} }

View File

@ -17,3 +17,15 @@
export function bytesToHex (bytes) { export function bytesToHex (bytes) {
return '0x' + bytes.map((b) => ('0' + b.toString(16)).slice(-2)).join(''); return '0x' + bytes.map((b) => ('0' + b.toString(16)).slice(-2)).join('');
} }
export function hex2Ascii (_hex) {
const hex = /^(?:0x)?(.*)$/.exec(_hex.toString())[1];
let str = '';
for (let i = 0; i < hex.length; i += 2) {
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return str;
}

View File

@ -16,7 +16,7 @@
import { isAddress as isAddressValid, toChecksumAddress } from '../../abi/util/address'; import { isAddress as isAddressValid, toChecksumAddress } from '../../abi/util/address';
import { decodeCallData, decodeMethodInput, methodToAbi } from './decode'; import { decodeCallData, decodeMethodInput, methodToAbi } from './decode';
import { bytesToHex } from './format'; import { bytesToHex, hex2Ascii } from './format';
import { fromWei, toWei } from './wei'; import { fromWei, toWei } from './wei';
import { sha3 } from './sha3'; import { sha3 } from './sha3';
import { isArray, isFunction, isHex, isInstanceOf, isString } from './types'; import { isArray, isFunction, isHex, isInstanceOf, isString } from './types';
@ -30,6 +30,7 @@ export default {
isInstanceOf, isInstanceOf,
isString, isString,
bytesToHex, bytesToHex,
hex2Ascii,
createIdentityImg, createIdentityImg,
decodeCallData, decodeCallData,
decodeMethodInput, decodeMethodInput,

View File

@ -18,6 +18,7 @@ import DappReg from './dappreg';
import Registry from './registry'; import Registry from './registry';
import SignatureReg from './signaturereg'; import SignatureReg from './signaturereg';
import TokenReg from './tokenreg'; import TokenReg from './tokenreg';
import GithubHint from './githubhint';
let instance = null; let instance = null;
@ -30,6 +31,7 @@ export default class Contracts {
this._dappreg = new DappReg(api, this._registry); this._dappreg = new DappReg(api, this._registry);
this._signaturereg = new SignatureReg(api, this._registry); this._signaturereg = new SignatureReg(api, this._registry);
this._tokenreg = new TokenReg(api, this._registry); this._tokenreg = new TokenReg(api, this._registry);
this._githubhint = new GithubHint(api, this._registry);
} }
get registry () { get registry () {
@ -48,6 +50,10 @@ export default class Contracts {
return this._tokenreg; return this._tokenreg;
} }
get githubHint () {
return this._githubhint;
}
static create (api) { static create (api) {
return new Contracts(api); return new Contracts(api);
} }

View File

@ -0,0 +1,32 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// 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/>.
export default class GithubHint {
constructor (api, registry) {
this._api = api;
this._registry = registry;
this.getInstance();
}
getContract () {
return this._registry.getContract('githubhint');
}
getInstance () {
return this.getContract().instance;
}
}

View File

@ -42,7 +42,7 @@ export default class Registry {
}); });
} }
getContractInstance (_name) { getContract (_name) {
const name = _name.toLowerCase(); const name = _name.toLowerCase();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -54,13 +54,19 @@ export default class Registry {
this this
.lookupAddress(name) .lookupAddress(name)
.then((address) => { .then((address) => {
this._contracts[name] = this._api.newContract(abis[name], address).instance; this._contracts[name] = this._api.newContract(abis[name], address);
resolve(this._contracts[name]); resolve(this._contracts[name]);
}) })
.catch(reject); .catch(reject);
}); });
} }
getContractInstance (_name) {
return this
.getContract(_name)
.then((contract) => contract.instance);
}
lookupAddress (_name) { lookupAddress (_name) {
const name = _name.toLowerCase(); const name = _name.toLowerCase();
const sha3 = this._api.util.sha3(name); const sha3 = this._api.util.sha3(name);

View File

@ -22,8 +22,12 @@ export default class TokenReg {
this.getInstance(); this.getInstance();
} }
getContract () {
return this._registry.getContract('tokenreg');
}
getInstance () { getInstance () {
return this._registry.getContractInstance('tokenreg'); return this.getContract().instance;
} }
tokenCount () { tokenCount () {

View File

@ -46,8 +46,6 @@ export const loadAccounts = () => (dispatch) => {
address address
})); }));
console.log('accounts', accountsList);
dispatch(setAccounts(accountsList)); dispatch(setAccounts(accountsList));
dispatch(setAccountsInfo(accountsInfo)); dispatch(setAccountsInfo(accountsInfo));
dispatch(setSelectedAccount(accountsList[0].address)); dispatch(setSelectedAccount(accountsList[0].address));

View File

@ -42,12 +42,9 @@ export default class QueryAction extends Component {
onClose: PropTypes.func.isRequired, onClose: PropTypes.func.isRequired,
handleQueryToken: PropTypes.func.isRequired, handleQueryToken: PropTypes.func.isRequired,
handleQueryMetaLookup: PropTypes.func.isRequired,
data: PropTypes.object, data: PropTypes.object,
notFound: PropTypes.bool, notFound: PropTypes.bool
metaLoading: PropTypes.bool,
metaData: PropTypes.object
} }
state = initState; state = initState;
@ -131,11 +128,8 @@ export default class QueryAction extends Component {
return ( return (
<Token <Token
fullWidth fullWidth
handleMetaLookup={ this.props.handleQueryMetaLookup } tla={ data.tla }
isMetaLoading={ this.props.metaLoading } />
meta={ this.props.metaData }
{ ...data }
/>
); );
} }

View File

@ -16,8 +16,6 @@
import { getTokenTotalSupply } from '../utils'; import { getTokenTotalSupply } from '../utils';
const { sha3, bytesToHex } = window.parity.api.util;
export const SET_REGISTER_SENDING = 'SET_REGISTER_SENDING'; export const SET_REGISTER_SENDING = 'SET_REGISTER_SENDING';
export const setRegisterSending = (isSending) => ({ export const setRegisterSending = (isSending) => ({
type: SET_REGISTER_SENDING, type: SET_REGISTER_SENDING,
@ -41,8 +39,6 @@ export const registerCompleted = () => ({
}); });
export const registerToken = (tokenData) => (dispatch, getState) => { export const registerToken = (tokenData) => (dispatch, getState) => {
console.log('registering token', tokenData);
const state = getState(); const state = getState();
const contractInstance = state.status.contract.instance; const contractInstance = state.status.contract.instance;
const fee = state.status.contract.fee; const fee = state.status.contract.fee;
@ -83,8 +79,6 @@ export const registerToken = (tokenData) => (dispatch, getState) => {
}) })
.then((gasEstimate) => { .then((gasEstimate) => {
options.gas = gasEstimate.mul(1.2).toFixed(0); options.gas = gasEstimate.mul(1.2).toFixed(0);
console.log(`transfer: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`);
return contractInstance.register.postTransaction(options, values); return contractInstance.register.postTransaction(options, values);
}) })
.then((result) => { .then((result) => {
@ -183,34 +177,3 @@ export const queryToken = (key, query) => (dispatch, getState) => {
dispatch(setQueryLoading(false)); dispatch(setQueryLoading(false));
}); });
}; };
export const queryTokenMeta = (id, query) => (dispatch, getState) => {
console.log('loading token meta', query);
const state = getState();
const contractInstance = state.status.contract.instance;
const key = sha3(query);
const startDate = Date.now();
dispatch(setQueryMetaLoading(true));
contractInstance
.meta
.call({}, [ id, key ])
.then((value) => {
const meta = {
key, query,
value: value.find(v => v !== 0) ? bytesToHex(value) : null
};
dispatch(setQueryMeta(meta));
setTimeout(() => {
dispatch(setQueryMetaLoading(false));
}, 500 - (Date.now() - startDate));
})
.catch((e) => {
console.error('load meta query error', e);
});
};

View File

@ -37,7 +37,6 @@ export default class Actions extends Component {
handleQueryToken: PropTypes.func.isRequired, handleQueryToken: PropTypes.func.isRequired,
handleQueryClose: PropTypes.func.isRequired, handleQueryClose: PropTypes.func.isRequired,
handleQueryMetaLookup: PropTypes.func.isRequired,
query: PropTypes.object.isRequired query: PropTypes.object.isRequired
}; };
@ -82,7 +81,6 @@ export default class Actions extends Component {
show={ this.state.show[ QUERY_ACTION ] } show={ this.state.show[ QUERY_ACTION ] }
onClose={ this.onQueryClose } onClose={ this.onQueryClose }
handleQueryToken={ this.props.handleQueryToken } handleQueryToken={ this.props.handleQueryToken }
handleQueryMetaLookup={ this.props.handleQueryMetaLookup }
{ ...this.props.query } /> { ...this.props.query } />
</div> </div>
); );

View File

@ -19,7 +19,7 @@ import { connect } from 'react-redux';
import Actions from './component'; import Actions from './component';
import { registerToken, registerReset, queryToken, queryReset, queryTokenMeta } from './actions'; import { registerToken, registerReset, queryToken, queryReset } from './actions';
class TokensContainer extends Component { class TokensContainer extends Component {
@ -49,9 +49,6 @@ const mapDispatchToProps = (dispatch) => {
}, },
handleQueryClose: () => { handleQueryClose: () => {
dispatch(queryReset()); dispatch(queryReset());
},
handleQueryMetaLookup: (id, query) => {
dispatch(queryTokenMeta(id, query));
} }
}; };
}; };

View File

@ -19,6 +19,7 @@
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
padding-bottom: 10em;
} }
.warning { .warning {

View File

@ -14,11 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { import Contracts from '../../../contracts';
registry as registryAbi,
tokenreg as tokenregAbi,
githubhint as githubhintAbi
} from '../../../contracts/abi';
import { loadToken, setTokenPending, deleteToken, setTokenData } from '../Tokens/actions'; import { loadToken, setTokenPending, deleteToken, setTokenData } from '../Tokens/actions';
@ -34,43 +30,31 @@ export const FIND_CONTRACT = 'FIND_CONTRACT';
export const loadContract = () => (dispatch) => { export const loadContract = () => (dispatch) => {
dispatch(setLoading(true)); dispatch(setLoading(true));
api.parity const { tokenReg, githubHint } = new Contracts(api);
.registryAddress()
.then((registryAddress) => {
console.log(`registry found at ${registryAddress}`);
const registry = api.newContract(registryAbi, registryAddress).instance;
return Promise.all([
registry.getAddress.call({}, [api.util.sha3('tokenreg'), 'A']),
registry.getAddress.call({}, [api.util.sha3('githubhint'), 'A'])
]);
})
.then(([ tokenregAddress, githubhintAddress ]) => {
console.log(`tokenreg was found at ${tokenregAddress}`);
const tokenregContract = api
.newContract(tokenregAbi, tokenregAddress);
const githubhintContract = api
.newContract(githubhintAbi, githubhintAddress);
return Promise
.all([
tokenReg.getContract(),
githubHint.getContract()
])
.then(([ tokenRegContract, githubHintContract ]) => {
dispatch(setContractDetails({ dispatch(setContractDetails({
address: tokenregAddress, address: tokenRegContract.address,
instance: tokenregContract.instance, instance: tokenRegContract.instance,
raw: tokenregContract raw: tokenRegContract
})); }));
dispatch(setGithubhintDetails({ dispatch(setGithubhintDetails({
address: githubhintAddress, address: githubHintContract.address,
instance: githubhintContract.instance, instance: githubHintContract.instance,
raw: githubhintContract raw: githubHintContract
})); }));
dispatch(loadContractDetails()); dispatch(loadContractDetails());
dispatch(subscribeEvents()); dispatch(subscribeEvents());
}) })
.catch((error) => { .catch((error) => {
console.error('loadContract error', error); throw error;
}); });
}; };
@ -78,7 +62,7 @@ export const LOAD_CONTRACT_DETAILS = 'LOAD_CONTRACT_DETAILS';
export const loadContractDetails = () => (dispatch, getState) => { export const loadContractDetails = () => (dispatch, getState) => {
const state = getState(); const state = getState();
const instance = state.status.contract.instance; const { instance } = state.status.contract;
Promise Promise
.all([ .all([
@ -87,8 +71,6 @@ export const loadContractDetails = () => (dispatch, getState) => {
instance.fee.call() instance.fee.call()
]) ])
.then(([accounts, owner, fee]) => { .then(([accounts, owner, fee]) => {
console.log(`owner as ${owner}, fee set at ${fee.toFormat()}`);
const isOwner = accounts.filter(a => a === owner).length > 0; const isOwner = accounts.filter(a => a === owner).length > 0;
dispatch(setContractDetails({ dispatch(setContractDetails({
@ -119,14 +101,14 @@ export const setGithubhintDetails = (details) => ({
export const subscribeEvents = () => (dispatch, getState) => { export const subscribeEvents = () => (dispatch, getState) => {
const state = getState(); const state = getState();
const contract = state.status.contract.raw; const { raw } = state.status.contract;
const previousSubscriptionId = state.status.subscriptionId; const previousSubscriptionId = state.status.subscriptionId;
if (previousSubscriptionId) { if (previousSubscriptionId) {
contract.unsubscribe(previousSubscriptionId); raw.unsubscribe(previousSubscriptionId);
} }
contract raw
.subscribe(null, { .subscribe(null, {
fromBlock: 'latest', fromBlock: 'latest',
toBlock: 'pending', toBlock: 'pending',
@ -187,7 +169,7 @@ export const subscribeEvents = () => (dispatch, getState) => {
)); ));
} }
console.log('new log event', log); console.warn('unknown log event', log);
}); });
}) })
.then((subscriptionId) => { .then((subscriptionId) => {

View File

@ -27,15 +27,13 @@ const initialState = {
contract: { contract: {
address: null, address: null,
instance: null, instance: null,
raw: null,
owner: null, owner: null,
isOwner: false, isOwner: false,
fee: null fee: null
}, },
githubhint: { githubhint: {
address: null, address: null,
instance: null, instance: null
raw: null
} }
}; };

View File

@ -14,4 +14,4 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
export default from './token'; export default from './tokenContainer';

View File

@ -57,15 +57,28 @@ export default class Token extends Component {
isLoading: PropTypes.bool, isLoading: PropTypes.bool,
isPending: PropTypes.bool, isPending: PropTypes.bool,
isTokenOwner: PropTypes.bool.isRequired, isTokenOwner: PropTypes.bool.isRequired,
isContractOwner: PropTypes.bool.isRequired, isContractOwner: PropTypes.bool,
fullWidth: PropTypes.bool fullWidth: PropTypes.bool
}; };
state = { static defaultProps = {
metaKeyIndex: 0 isContractOwner: false
}; };
state = {
metaKeyIndex: 0,
showMeta: false
};
shouldComponentUpdate (nextProps) {
if (nextProps.isLoading && this.props.isLoading) {
return false;
}
return true;
}
render () { render () {
const { isLoading, fullWidth } = this.props; const { isLoading, fullWidth } = this.props;
@ -237,7 +250,12 @@ export default class Token extends Component {
} }
renderMeta (meta) { renderMeta (meta) {
const isMetaLoading = this.props.isMetaLoading; const { isMetaLoading } = this.props;
const { showMeta } = this.state;
if (!showMeta) {
return null;
}
if (isMetaLoading) { if (isMetaLoading) {
return (<div> return (<div>
@ -331,6 +349,7 @@ export default class Token extends Component {
const key = metaDataKeys[keyIndex].value; const key = metaDataKeys[keyIndex].value;
const index = this.props.index; const index = this.props.index;
this.setState({ showMeta: true });
this.props.handleMetaLookup(index, key); this.props.handleMetaLookup(index, key);
} }

View File

@ -0,0 +1,73 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// 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 React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import Token from './token';
import { queryTokenMeta, unregisterToken, addTokenMeta } from '../actions';
class TokenContainer extends Component {
static propTypes = {
handleMetaLookup: PropTypes.func.isRequired,
handleUnregister: PropTypes.func.isRequired,
handleAddMeta: PropTypes.func.isRequired,
tla: PropTypes.string.isRequired
};
render () {
return (
<Token
{ ...this.props }
/>
);
}
}
const mapStateToProps = (_, initProps) => {
const { tla } = initProps;
return (state) => {
const { isOwner } = state.status.contract;
const { tokens } = state.tokens;
const token = tokens.find((t) => t.tla === tla);
return { ...token, isContractOwner: isOwner };
};
};
const mapDispatchToProps = (dispatch) => {
return {
handleMetaLookup: (index, query) => {
dispatch(queryTokenMeta(index, query));
},
handleUnregister: (index) => {
dispatch(unregisterToken(index));
},
handleAddMeta: (index, key, value) => {
dispatch(addTokenMeta(index, key, value));
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(TokenContainer);

View File

@ -67,8 +67,6 @@ export const deleteToken = (index) => ({
}); });
export const loadTokens = () => (dispatch, getState) => { export const loadTokens = () => (dispatch, getState) => {
console.log('loading tokens...');
const state = getState(); const state = getState();
const contractInstance = state.status.contract.instance; const contractInstance = state.status.contract.instance;
@ -79,7 +77,6 @@ export const loadTokens = () => (dispatch, getState) => {
.call() .call()
.then((count) => { .then((count) => {
const tokenCount = parseInt(count); const tokenCount = parseInt(count);
console.log(`token count: ${tokenCount}`);
dispatch(setTokenCount(tokenCount)); dispatch(setTokenCount(tokenCount));
for (let i = 0; i < tokenCount; i++) { for (let i = 0; i < tokenCount; i++) {
@ -94,8 +91,6 @@ export const loadTokens = () => (dispatch, getState) => {
}; };
export const loadToken = (index) => (dispatch, getState) => { export const loadToken = (index) => (dispatch, getState) => {
console.log('loading token', index);
const state = getState(); const state = getState();
const contractInstance = state.status.contract.instance; const contractInstance = state.status.contract.instance;
@ -144,7 +139,7 @@ export const loadToken = (index) => (dispatch, getState) => {
} }
data.totalSupply = data.totalSupply.toNumber(); data.totalSupply = data.totalSupply.toNumber();
console.log(`token loaded: #${index}`, data);
dispatch(setTokenData(index, data)); dispatch(setTokenData(index, data));
dispatch(setTokenLoading(index, false)); dispatch(setTokenLoading(index, false));
}) })
@ -159,8 +154,6 @@ export const loadToken = (index) => (dispatch, getState) => {
}; };
export const queryTokenMeta = (index, query) => (dispatch, getState) => { export const queryTokenMeta = (index, query) => (dispatch, getState) => {
console.log('loading token meta', index, query);
const state = getState(); const state = getState();
const contractInstance = state.status.contract.instance; const contractInstance = state.status.contract.instance;
@ -176,7 +169,6 @@ export const queryTokenMeta = (index, query) => (dispatch, getState) => {
value: value.find(v => v !== 0) ? bytesToHex(value) : null value: value.find(v => v !== 0) ? bytesToHex(value) : null
}; };
console.log(`token meta loaded: #${index}`, value);
dispatch(setTokenMeta(index, meta)); dispatch(setTokenMeta(index, meta));
setTimeout(() => { setTimeout(() => {
@ -189,8 +181,6 @@ export const queryTokenMeta = (index, query) => (dispatch, getState) => {
}; };
export const addTokenMeta = (index, key, value) => (dispatch, getState) => { export const addTokenMeta = (index, key, value) => (dispatch, getState) => {
console.log('add token meta', index, key, value);
const state = getState(); const state = getState();
const contractInstance = state.status.contract.instance; const contractInstance = state.status.contract.instance;
const token = state.tokens.tokens.find(t => t.index === index); const token = state.tokens.tokens.find(t => t.index === index);
@ -203,8 +193,6 @@ export const addTokenMeta = (index, key, value) => (dispatch, getState) => {
.estimateGas(options, values) .estimateGas(options, values)
.then((gasEstimate) => { .then((gasEstimate) => {
options.gas = gasEstimate.mul(1.2).toFixed(0); options.gas = gasEstimate.mul(1.2).toFixed(0);
console.log(`addTokenMeta: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`);
return contractInstance.setMeta.postTransaction(options, values); return contractInstance.setMeta.postTransaction(options, values);
}) })
.catch((e) => { .catch((e) => {
@ -213,8 +201,6 @@ export const addTokenMeta = (index, key, value) => (dispatch, getState) => {
}; };
export const addGithubhintURL = (from, key, url) => (dispatch, getState) => { export const addGithubhintURL = (from, key, url) => (dispatch, getState) => {
console.log('add githubhint url', key, url);
const state = getState(); const state = getState();
const contractInstance = state.status.githubhint.instance; const contractInstance = state.status.githubhint.instance;
@ -227,8 +213,6 @@ export const addGithubhintURL = (from, key, url) => (dispatch, getState) => {
.estimateGas(options, values) .estimateGas(options, values)
.then((gasEstimate) => { .then((gasEstimate) => {
options.gas = gasEstimate.mul(1.2).toFixed(0); options.gas = gasEstimate.mul(1.2).toFixed(0);
console.log(`transfer: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`);
return contractInstance.hintURL.postTransaction(options, values); return contractInstance.hintURL.postTransaction(options, values);
}) })
.catch((e) => { .catch((e) => {
@ -237,8 +221,6 @@ export const addGithubhintURL = (from, key, url) => (dispatch, getState) => {
}; };
export const unregisterToken = (index) => (dispatch, getState) => { export const unregisterToken = (index) => (dispatch, getState) => {
console.log('unregistering token', index);
const { contract } = getState().status; const { contract } = getState().status;
const { instance, owner } = contract; const { instance, owner } = contract;
@ -252,8 +234,6 @@ export const unregisterToken = (index) => (dispatch, getState) => {
.estimateGas(options, values) .estimateGas(options, values)
.then((gasEstimate) => { .then((gasEstimate) => {
options.gas = gasEstimate.mul(1.2).toFixed(0); options.gas = gasEstimate.mul(1.2).toFixed(0);
console.log(`transfer: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`);
return instance.unregister.postTransaction(options, values); return instance.unregister.postTransaction(options, values);
}) })
.catch((e) => { .catch((e) => {

View File

@ -19,16 +19,13 @@ import { connect } from 'react-redux';
import Tokens from './tokens'; import Tokens from './tokens';
import { loadTokens, queryTokenMeta, unregisterToken, addTokenMeta } from './actions'; import { loadTokens } from './actions';
class TokensContainer extends Component { class TokensContainer extends Component {
static propTypes = { static propTypes = {
isOwner: PropTypes.bool,
isLoading: PropTypes.bool, isLoading: PropTypes.bool,
tokens: PropTypes.array, tokens: PropTypes.array,
tokenCount: PropTypes.number, onLoadTokens: PropTypes.func
onLoadTokens: PropTypes.func,
accounts: PropTypes.array
}; };
componentDidMount () { componentDidMount () {
@ -36,7 +33,6 @@ class TokensContainer extends Component {
} }
render () { render () {
console.log(this.props);
return ( return (
<Tokens <Tokens
{ ...this.props } { ...this.props }
@ -46,30 +42,19 @@ class TokensContainer extends Component {
} }
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
const { list } = state.accounts; const { isLoading, tokens } = state.tokens;
const { isLoading, tokens, tokenCount } = state.tokens;
const { isOwner } = state.status.contract; const filteredTokens = tokens
.filter((token) => token && token.tla)
.map((token) => ({ tla: token.tla, owner: token.owner }));
return { isLoading, tokens, tokenCount, isOwner, accounts: list }; return { isLoading, tokens: filteredTokens };
}; };
const mapDispatchToProps = (dispatch) => { const mapDispatchToProps = (dispatch) => {
return { return {
onLoadTokens: () => { onLoadTokens: () => {
dispatch(loadTokens()); dispatch(loadTokens());
},
handleMetaLookup: (index, query) => {
dispatch(queryTokenMeta(index, query));
},
handleUnregister: (index) => {
dispatch(unregisterToken(index));
},
handleAddMeta: (index, key, value) => {
dispatch(addTokenMeta(index, key, value));
} }
}; };
}; };

View File

@ -23,13 +23,8 @@ import styles from './tokens.css';
export default class Tokens extends Component { export default class Tokens extends Component {
static propTypes = { static propTypes = {
handleAddMeta: PropTypes.func.isRequired,
handleUnregister: PropTypes.func.isRequired,
handleMetaLookup: PropTypes.func.isRequired,
isOwner: PropTypes.bool.isRequired,
isLoading: PropTypes.bool.isRequired, isLoading: PropTypes.bool.isRequired,
tokens: PropTypes.array, tokens: PropTypes.array
accounts: PropTypes.array
}; };
render () { render () {
@ -45,24 +40,12 @@ export default class Tokens extends Component {
} }
renderTokens (tokens) { renderTokens (tokens) {
const { accounts, isOwner } = this.props; return tokens.map((token) => {
return tokens.map((token, index) => {
if (!token || !token.tla) {
return null;
}
const isTokenOwner = !!accounts.find((account) => account.address === token.owner);
return ( return (
<Token <Token
{ ...token } key={ token.tla }
handleUnregister={ this.props.handleUnregister } tla={ token.tla }
handleMetaLookup={ this.props.handleMetaLookup } />
handleAddMeta={ this.props.handleAddMeta }
key={ index }
isTokenOwner={ isTokenOwner }
isContractOwner={ isOwner } />
); );
}); });
} }

View File

@ -25,6 +25,7 @@ import ReactDOM from 'react-dom';
import injectTapEventPlugin from 'react-tap-event-plugin'; import injectTapEventPlugin from 'react-tap-event-plugin';
import { createHashHistory } from 'history'; import { createHashHistory } from 'history';
import { Redirect, Router, Route, useRouterHistory } from 'react-router'; import { Redirect, Router, Route, useRouterHistory } from 'react-router';
import qs from 'querystring';
import SecureApi from './secureApi'; import SecureApi from './secureApi';
import ContractInstances from './contracts'; import ContractInstances from './contracts';
@ -45,6 +46,7 @@ import './index.html';
injectTapEventPlugin(); injectTapEventPlugin();
const AUTH_HASH = '#/auth?';
const parityUrl = process.env.PARITY_URL || const parityUrl = process.env.PARITY_URL ||
( (
process.env.NODE_ENV === 'production' process.env.NODE_ENV === 'production'
@ -52,7 +54,12 @@ const parityUrl = process.env.PARITY_URL ||
: '127.0.0.1:8180' : '127.0.0.1:8180'
); );
const api = new SecureApi(`ws://${parityUrl}`); let token = null;
if (window.location.hash && window.location.hash.indexOf(AUTH_HASH) === 0) {
token = qs.parse(window.location.hash.substr(AUTH_HASH.length)).token;
}
const api = new SecureApi(`ws://${parityUrl}`, token);
ContractInstances.create(api); ContractInstances.create(api);
const store = initStore(api); const store = initStore(api);
@ -67,6 +74,7 @@ ReactDOM.render(
<ContextProvider api={ api } muiTheme={ muiTheme } store={ store }> <ContextProvider api={ api } muiTheme={ muiTheme } store={ store }>
<Router className={ styles.reset } history={ routerHistory }> <Router className={ styles.reset } history={ routerHistory }>
<Redirect from='/' to='/accounts' /> <Redirect from='/' to='/accounts' />
<Redirect from='/auth' to='/accounts' query={ {} } />
<Redirect from='/settings' to='/settings/views' /> <Redirect from='/settings' to='/settings/views' />
<Route path='/' component={ Application }> <Route path='/' component={ Application }>
<Route path='accounts' component={ Accounts } /> <Route path='accounts' component={ Accounts } />

View File

@ -26,6 +26,8 @@ import ErrorStep from './ErrorStep';
import styles from './deployContract.css'; import styles from './deployContract.css';
import { ERROR_CODES } from '../../api/transport/error';
const steps = ['contract details', 'deployment', 'completed']; const steps = ['contract details', 'deployment', 'completed'];
export default class DeployContract extends Component { export default class DeployContract extends Component {
@ -63,7 +65,8 @@ export default class DeployContract extends Component {
params: [], params: [],
paramsError: [], paramsError: [],
step: 0, step: 0,
deployError: null deployError: null,
rejected: false
} }
componentWillMount () { componentWillMount () {
@ -92,15 +95,20 @@ export default class DeployContract extends Component {
} }
render () { render () {
const { step, deployError } = this.state; const { step, deployError, rejected } = this.state;
const realSteps = deployError || rejected ? null : steps;
const title = realSteps
? null
: (deployError ? 'deployment failed' : 'rejected');
return ( return (
<Modal <Modal
actions={ this.renderDialogActions() } actions={ this.renderDialogActions() }
current={ step } current={ step }
steps={ deployError ? null : steps } steps={ realSteps }
title={ deployError ? 'deployment failed' : null } title={ title }
waiting={ [1] } waiting={ realSteps ? [1] : null }
visible visible
scroll> scroll>
{ this.renderStep() } { this.renderStep() }
@ -158,7 +166,7 @@ export default class DeployContract extends Component {
renderStep () { renderStep () {
const { accounts, readOnly } = this.props; const { accounts, readOnly } = this.props;
const { address, deployError, step, deployState, txhash } = this.state; const { address, deployError, step, deployState, txhash, rejected } = this.state;
if (deployError) { if (deployError) {
return ( return (
@ -166,6 +174,15 @@ export default class DeployContract extends Component {
); );
} }
if (rejected) {
return (
<BusyStep
title='The deployment has been rejected'
state='You can safely close this window, the contract deployment will not occur.'
/>
);
}
switch (step) { switch (step) {
case 0: case 0:
return ( return (
@ -273,6 +290,11 @@ export default class DeployContract extends Component {
}); });
}) })
.catch((error) => { .catch((error) => {
if (error.code === ERROR_CODES.REQUEST_REJECTED) {
this.setState({ rejected: true });
return false;
}
console.error('error deploying contract', error); console.error('error deploying contract', error);
this.setState({ deployError: error }); this.setState({ deployError: error });
store.dispatch({ type: 'newError', error }); store.dispatch({ type: 'newError', error });

View File

@ -23,6 +23,8 @@ import { validateAddress, validateUint } from '../../util/validation';
import DetailsStep from './DetailsStep'; import DetailsStep from './DetailsStep';
import { ERROR_CODES } from '../../api/transport/error';
export default class ExecuteContract extends Component { export default class ExecuteContract extends Component {
static contextTypes = { static contextTypes = {
api: PropTypes.object.isRequired, api: PropTypes.object.isRequired,
@ -49,7 +51,8 @@ export default class ExecuteContract extends Component {
step: 0, step: 0,
sending: false, sending: false,
busyState: null, busyState: null,
txhash: null txhash: null,
rejected: false
} }
componentDidMount () { componentDidMount () {
@ -80,6 +83,7 @@ export default class ExecuteContract extends Component {
const { onClose, fromAddress } = this.props; const { onClose, fromAddress } = this.props;
const { sending, step, fromAddressError, valuesError } = this.state; const { sending, step, fromAddressError, valuesError } = this.state;
const hasError = fromAddressError || valuesError.find((error) => error); const hasError = fromAddressError || valuesError.find((error) => error);
const cancelBtn = ( const cancelBtn = (
<Button <Button
key='cancel' key='cancel'
@ -115,7 +119,16 @@ export default class ExecuteContract extends Component {
renderStep () { renderStep () {
const { onFromAddressChange } = this.props; const { onFromAddressChange } = this.props;
const { step, busyState, txhash } = this.state; const { step, busyState, txhash, rejected } = this.state;
if (rejected) {
return (
<BusyStep
title='The execution has been rejected'
state='You can safely close this window, the function execution will not occur.'
/>
);
}
if (step === 0) { if (step === 0) {
return ( return (
@ -221,7 +234,17 @@ export default class ExecuteContract extends Component {
}) })
.then((requestId) => { .then((requestId) => {
this.setState({ busyState: 'Waiting for authorization in the Parity Signer' }); this.setState({ busyState: 'Waiting for authorization in the Parity Signer' });
return api.pollMethod('parity_checkRequest', requestId);
return api
.pollMethod('parity_checkRequest', requestId)
.catch((e) => {
if (e.code === ERROR_CODES.REQUEST_REJECTED) {
this.setState({ rejected: true });
return false;
}
throw e;
});
}) })
.then((txhash) => { .then((txhash) => {
this.setState({ sending: false, step: 2, txhash, busyState: 'Your transaction has been posted to the network' }); this.setState({ sending: false, step: 2, txhash, busyState: 'Your transaction has been posted to the network' });

View File

@ -28,13 +28,16 @@ import Extras from './Extras';
import ERRORS from './errors'; import ERRORS from './errors';
import styles from './transfer.css'; import styles from './transfer.css';
import { ERROR_CODES } from '../../api/transport/error';
const DEFAULT_GAS = '21000'; const DEFAULT_GAS = '21000';
const DEFAULT_GASPRICE = '20000000000'; const DEFAULT_GASPRICE = '20000000000';
const TITLES = { const TITLES = {
transfer: 'transfer details', transfer: 'transfer details',
sending: 'sending', sending: 'sending',
complete: 'complete', complete: 'complete',
extras: 'extra information' extras: 'extra information',
rejected: 'rejected'
}; };
const STAGES_BASIC = [TITLES.transfer, TITLES.sending, TITLES.complete]; const STAGES_BASIC = [TITLES.transfer, TITLES.sending, TITLES.complete];
const STAGES_EXTRA = [TITLES.transfer, TITLES.extras, TITLES.sending, TITLES.complete]; const STAGES_EXTRA = [TITLES.transfer, TITLES.extras, TITLES.sending, TITLES.complete];
@ -74,7 +77,8 @@ export default class Transfer extends Component {
valueAll: false, valueAll: false,
valueError: null, valueError: null,
isEth: true, isEth: true,
busyState: null busyState: null,
rejected: false
} }
componentDidMount () { componentDidMount () {
@ -82,13 +86,19 @@ export default class Transfer extends Component {
} }
render () { render () {
const { stage, extras } = this.state; const { stage, extras, rejected } = this.state;
const steps = [].concat(extras ? STAGES_EXTRA : STAGES_BASIC);
if (rejected) {
steps[steps.length - 1] = TITLES.rejected;
}
return ( return (
<Modal <Modal
actions={ this.renderDialogActions() } actions={ this.renderDialogActions() }
current={ stage } current={ stage }
steps={ extras ? STAGES_EXTRA : STAGES_BASIC } steps={ steps }
waiting={ extras ? [2] : [1] } waiting={ extras ? [2] : [1] }
visible visible
scroll scroll
@ -133,7 +143,16 @@ export default class Transfer extends Component {
} }
renderCompletePage () { renderCompletePage () {
const { sending, txhash, busyState } = this.state; const { sending, txhash, busyState, rejected } = this.state;
if (rejected) {
return (
<BusyStep
title='The transaction has been rejected'
state='You can safely close this window, the transfer will not occur.'
/>
);
}
if (sending) { if (sending) {
return ( return (
@ -455,7 +474,17 @@ export default class Transfer extends Component {
: this._sendToken() : this._sendToken()
).then((requestId) => { ).then((requestId) => {
this.setState({ busyState: 'Waiting for authorization in the Parity Signer' }); this.setState({ busyState: 'Waiting for authorization in the Parity Signer' });
return api.pollMethod('parity_checkRequest', requestId);
return api
.pollMethod('parity_checkRequest', requestId)
.catch((e) => {
if (e.code === ERROR_CODES.REQUEST_REJECTED) {
this.setState({ rejected: true });
return false;
}
throw e;
});
}) })
.then((txhash) => { .then((txhash) => {
this.onNext(); this.onNext();

View File

@ -19,12 +19,13 @@ import Api from './api';
const sysuiToken = window.localStorage.getItem('sysuiToken'); const sysuiToken = window.localStorage.getItem('sysuiToken');
export default class SecureApi extends Api { export default class SecureApi extends Api {
constructor (url) { constructor (url, nextToken) {
super(new Api.Transport.Ws(url, sysuiToken)); super(new Api.Transport.Ws(url, sysuiToken));
this._isConnecting = true; this._isConnecting = true;
this._connectState = sysuiToken === 'initial' ? 1 : 0; this._connectState = sysuiToken === 'initial' ? 1 : 0;
this._needsToken = false; this._needsToken = false;
this._nextToken = nextToken;
this._dappsPort = 8080; this._dappsPort = 8080;
this._dappsInterface = null; this._dappsInterface = null;
this._signerPort = 8180; this._signerPort = 8180;
@ -57,7 +58,11 @@ export default class SecureApi extends Api {
if (isConnected) { if (isConnected) {
return this.connectSuccess(); return this.connectSuccess();
} else if (lastError) { } else if (lastError) {
this.updateToken('initial', 1); const nextToken = this._nextToken || 'initial';
const nextState = this._nextToken ? 0 : 1;
this._nextToken = null;
this.updateToken(nextToken, nextState);
} }
break; break;

View File

@ -17,4 +17,10 @@
.container { .container {
z-index: 10101 !important; z-index: 10101 !important;
button {
color: white !important;
margin: 0 !important;
margin-right: -16px !important;
}
} }

View File

@ -23,9 +23,12 @@ import { closeErrors } from './actions';
import styles from './errors.css'; import styles from './errors.css';
const ERROR_REGEX = /-(\d+): (.+)$/;
class Errors extends Component { class Errors extends Component {
static propTypes = { static propTypes = {
message: PropTypes.string, message: PropTypes.string,
error: PropTypes.object,
visible: PropTypes.bool, visible: PropTypes.bool,
onCloseErrors: PropTypes.func onCloseErrors: PropTypes.func
}; };
@ -37,22 +40,60 @@ class Errors extends Component {
return null; return null;
} }
const text = this.getErrorMessage();
return ( return (
<Snackbar <Snackbar
open
className={ styles.container } className={ styles.container }
message={ message } open
autoHideDuration={ 5000 } action='close'
onRequestClose={ onCloseErrors } /> autoHideDuration={ 60000 }
message={ text }
onActionTouchTap={ onCloseErrors }
onRequestClose={ this.onRequestClose }
bodyStyle={ {
whiteSpace: 'pre-line',
height: 'auto'
} }
contentStyle={ {
display: 'flex',
flexDirection: 'row',
lineHeight: '1.5em',
padding: '0.75em 0',
alignItems: 'center'
} }
/>
); );
} }
getErrorMessage = () => {
const { message, error } = this.props;
if (!error.text && !ERROR_REGEX.test(message)) {
return message;
}
const matches = ERROR_REGEX.exec(message);
const code = error.code || parseInt(matches[1]) * -1;
const text = error.text || matches[2];
return `[${code}] ${text}`;
}
onRequestClose = (reason) => {
if (reason === 'timeout') {
this.props.onCloseErrors();
}
}
} }
function mapStateToProps (state) { function mapStateToProps (state) {
const { message, visible } = state.errors; const { message, error, visible } = state.errors;
return { return {
message, message,
error,
visible visible
}; };
} }

View File

@ -19,7 +19,8 @@ function newError (state, action) {
return Object.assign({}, state, { return Object.assign({}, state, {
visible: true, visible: true,
message: error.message message: error.message,
error
}); });
} }

View File

@ -144,7 +144,7 @@ export default class Input extends Component {
} }
renderCopyButton () { renderCopyButton () {
const { allowCopy, hideUnderline, label, hint, floatCopy } = this.props; const { allowCopy, label, hint, floatCopy } = this.props;
const { value } = this.state; const { value } = this.state;
if (!allowCopy) { if (!allowCopy) {
@ -159,7 +159,7 @@ export default class Input extends Component {
? allowCopy ? allowCopy
: value; : value;
if (hideUnderline && !label) { if (!label) {
style.marginBottom = 2; style.marginBottom = 2;
} else if (label && !hint) { } else if (label && !hint) {
style.marginBottom = 4; style.marginBottom = 4;
@ -182,6 +182,7 @@ export default class Input extends Component {
} }
onChange = (event, value) => { onChange = (event, value) => {
event.persist();
this.setValue(value, () => { this.setValue(value, () => {
this.props.onChange && this.props.onChange(event, value); this.props.onChange && this.props.onChange(event, value);
}); });

View File

@ -20,6 +20,7 @@
.input input { .input input {
padding-left: 48px !important; padding-left: 48px !important;
box-sizing: border-box;
} }
.inputEmpty input { .inputEmpty input {

View File

@ -76,6 +76,7 @@ class InputAddress extends Component {
} }
const classes = [disabled ? styles.iconDisabled : styles.icon]; const classes = [disabled ? styles.iconDisabled : styles.icon];
if (!label) { if (!label) {
classes.push(styles.noLabel); classes.push(styles.noLabel);
} }

View File

@ -18,6 +18,12 @@
.container { .container {
} }
.loading {
display: flex;
align-items: center;
justify-content: center;
}
.details, .details,
.gasDetails { .gasDetails {
color: #aaa; color: #aaa;
@ -46,7 +52,7 @@
.highlight { .highlight {
} }
.inputs { .inputs, .addressContainer {
padding-left: 2em; padding-left: 2em;
} }
@ -73,3 +79,7 @@
margin-bottom: -10px; margin-bottom: -10px;
margin-right: 0.5em; margin-right: 0.5em;
} }
.inputData {
word-break: break-all;
}

View File

@ -18,14 +18,14 @@ import BigNumber from 'bignumber.js';
import React, { Component, PropTypes } from 'react'; import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
import CircularProgress from 'material-ui/CircularProgress';
import Contracts from '../../contracts'; import Contracts from '../../contracts';
import IdentityIcon from '../IdentityIcon';
import IdentityName from '../IdentityName';
import { Input, InputAddress } from '../Form'; import { Input, InputAddress } from '../Form';
import styles from './methodDecoding.css'; import styles from './methodDecoding.css';
const ASCII_INPUT = /^[a-z0-9\s,?;.:/!()-_@'"#]+$/i;
const CONTRACT_CREATE = '0x60606040'; const CONTRACT_CREATE = '0x60606040';
const TOKEN_METHODS = { const TOKEN_METHODS = {
'0xa9059cbb': 'transfer(to,value)' '0xa9059cbb': 'transfer(to,value)'
@ -53,20 +53,36 @@ class MethodDecoding extends Component {
token: null, token: null,
isContract: false, isContract: false,
isDeploy: false, isDeploy: false,
isReceived: false isReceived: false,
isLoading: true
} }
componentWillMount () { componentWillMount () {
this.lookup(); const lookupResult = this.lookup();
if (typeof lookupResult === 'object' && typeof lookupResult.then === 'function') {
lookupResult.then(() => this.setState({ isLoading: false }));
} else {
this.setState({ isLoading: false });
}
} }
render () { render () {
const { transaction } = this.props; const { transaction } = this.props;
const { isLoading } = this.state;
if (!transaction) { if (!transaction) {
return null; return null;
} }
if (isLoading) {
return (
<div className={ styles.loading }>
<CircularProgress size={ 60 } thickness={ 2 } />
</div>
);
}
return ( return (
<div className={ styles.container }> <div className={ styles.container }>
{ this.renderAction() } { this.renderAction() }
@ -82,26 +98,33 @@ class MethodDecoding extends Component {
return ( return (
<div className={ styles.gasDetails }> <div className={ styles.gasDetails }>
{ historic ? 'Provided' : 'Provides' } <span className={ styles.highlight }>{ gas.toFormat(0) } gas ({ gasPrice.div(1000000).toFormat(0) }M/<small>ETH</small>)</span> for a total transaction value of <span className={ styles.highlight }>{ this.renderEtherValue(gasValue) }</span> <span>{ historic ? 'Provided' : 'Provides' } </span>
<span className={ styles.highlight }>
{ gas.toFormat(0) } gas ({ gasPrice.div(1000000).toFormat(0) }M/<small>ETH</small>)
</span>
<span> for a total transaction value of </span>
<span className={ styles.highlight }>{ this.renderEtherValue(gasValue) }</span>
</div> </div>
); );
} }
renderAction () { renderAction () {
const { methodName, methodInputs, methodSignature, token, isDeploy, isReceived } = this.state; const { methodName, methodInputs, methodSignature, token, isDeploy, isReceived, isContract } = this.state;
if (isDeploy) { if (isDeploy) {
return this.renderDeploy(); return this.renderDeploy();
} }
if (methodSignature) { if (isContract && methodSignature) {
if (token && TOKEN_METHODS[methodSignature] && methodInputs) { if (token && TOKEN_METHODS[methodSignature] && methodInputs) {
return this.renderTokenAction(); return this.renderTokenAction();
} }
return methodName if (methodName) {
? this.renderSignatureMethod() return this.renderSignatureMethod();
: this.renderUnknownMethod(); }
return this.renderUnknownMethod();
} }
return isReceived return isReceived
@ -109,6 +132,28 @@ class MethodDecoding extends Component {
: this.renderValueTransfer(); : this.renderValueTransfer();
} }
renderInputValue () {
const { api } = this.context;
const { transaction } = this.props;
if (!/^(0x)?([0]*[1-9a-f]+[0]*)+$/.test(transaction.input)) {
return null;
}
const ascii = api.util.hex2Ascii(transaction.input);
const text = ASCII_INPUT.test(ascii)
? ascii
: transaction.input;
return (
<div>
<span>with the input &nbsp;</span>
<code className={ styles.inputData }>{ text }</code>
</div>
);
}
renderTokenAction () { renderTokenAction () {
const { historic } = this.props; const { historic } = this.props;
const { methodSignature, methodInputs } = this.state; const { methodSignature, methodInputs } = this.state;
@ -120,7 +165,15 @@ class MethodDecoding extends Component {
default: default:
return ( return (
<div className={ styles.details }> <div className={ styles.details }>
{ historic ? 'Transferred' : 'Will transfer' } <span className={ styles.highlight }>{ this.renderTokenValue(value.value) }</span> to <span className={ styles.highlight }>{ this.renderAddressName(address) }</span>. <div>
<span>{ historic ? 'Transferred' : 'Will transfer' } </span>
<span className={ styles.highlight }>
{ this.renderTokenValue(value.value) }
</span>
<span> to </span>
</div>
{ this.renderAddressName(address) }
</div> </div>
); );
} }
@ -139,7 +192,11 @@ class MethodDecoding extends Component {
return ( return (
<div className={ styles.details }> <div className={ styles.details }>
Deployed a contract at address <span className={ styles.highlight }>{ this.renderAddressName(transaction.creates, false) }</span> <div>
<span>Deployed a contract at address </span>
</div>
{ this.renderAddressName(transaction.creates, false) }
</div> </div>
); );
} }
@ -150,7 +207,16 @@ class MethodDecoding extends Component {
return ( return (
<div className={ styles.details }> <div className={ styles.details }>
{ historic ? 'Received' : 'Will receive' } <span className={ styles.highlight }>{ this.renderEtherValue(transaction.value) }</span> from { isContract ? 'the contract' : '' } <span className={ styles.highlight }>{ this.renderAddressName(transaction.from) }</span> <div>
<span>{ historic ? 'Received' : 'Will receive' } </span>
<span className={ styles.highlight }>
{ this.renderEtherValue(transaction.value) }
</span>
<span> from { isContract ? 'the contract' : '' } </span>
</div>
{ this.renderAddressName(transaction.from) }
{ this.renderInputValue() }
</div> </div>
); );
} }
@ -161,19 +227,44 @@ class MethodDecoding extends Component {
return ( return (
<div className={ styles.details }> <div className={ styles.details }>
{ historic ? 'Transferred' : 'Will transfer' } <span className={ styles.highlight }>{ this.renderEtherValue(transaction.value) }</span> to { isContract ? 'the contract' : '' } <span className={ styles.highlight }>{ this.renderAddressName(transaction.to) }</span> <div>
<span>{ historic ? 'Transferred' : 'Will transfer' } </span>
<span className={ styles.highlight }>
{ this.renderEtherValue(transaction.value) }
</span>
<span> to { isContract ? 'the contract' : '' } </span>
</div>
{ this.renderAddressName(transaction.to) }
{ this.renderInputValue() }
</div> </div>
); );
} }
renderSignatureMethod () { renderSignatureMethod () {
const { historic, transaction } = this.props; const { historic, transaction } = this.props;
const { methodName } = this.state; const { methodName, methodInputs } = this.state;
return ( return (
<div className={ styles.details }> <div className={ styles.details }>
<div className={ styles.description }> <div className={ styles.description }>
{ historic ? 'Executed' : 'Will execute' } the <span className={ styles.name }>{ methodName }</span> function on the contract <span className={ styles.highlight }>{ this.renderAddressName(transaction.to) }</span>, transferring <span className={ styles.highlight }>{ this.renderEtherValue(transaction.value) }</span>, passing the following parameters: <div>
<span>{ historic ? 'Executed' : 'Will execute' } the </span>
<span className={ styles.name }>{ methodName }</span>
<span> function on the contract </span>
</div>
{ this.renderAddressName(transaction.to) }
<div>
<span>transferring </span>
<span className={ styles.highlight }>
{ this.renderEtherValue(transaction.value) }
</span>
<span>
{ methodInputs.length ? ', passing the following parameters:' : '.' }
</span>
</div>
</div> </div>
<div className={ styles.inputs }> <div className={ styles.inputs }>
{ this.renderInputs() } { this.renderInputs() }
@ -187,7 +278,21 @@ class MethodDecoding extends Component {
return ( return (
<div className={ styles.details }> <div className={ styles.details }>
{ historic ? 'Executed' : 'Will execute' } <span className={ styles.name }>an unknown/unregistered</span> method on the contract <span className={ styles.highlight }>{ this.renderAddressName(transaction.to) }</span>, transferring <span className={ styles.highlight }>{ this.renderEtherValue(transaction.value) }</span>. <div>
<span>{ historic ? 'Executed' : 'Will execute' } </span>
<span className={ styles.name }>an unknown/unregistered</span>
<span> method on the contract </span>
</div>
{ this.renderAddressName(transaction.to) }
<div>
<span>transferring </span>
<span className={ styles.highlight }>
{ this.renderEtherValue(transaction.value) }
</span>
<span>.</span>
</div>
</div> </div>
); );
} }
@ -239,7 +344,7 @@ class MethodDecoding extends Component {
return ( return (
<span className={ styles.tokenValue }> <span className={ styles.tokenValue }>
{ value.div(token.format).toFormat(5) }<small>{ token.tag }</small> { value.div(token.format).toFormat(5) }<small> { token.tag }</small>
</span> </span>
); );
} }
@ -250,17 +355,21 @@ class MethodDecoding extends Component {
return ( return (
<span className={ styles.etherValue }> <span className={ styles.etherValue }>
{ ether.toFormat(5) }<small>ETH</small> { ether.toFormat(5) }<small> ETH</small>
</span> </span>
); );
} }
renderAddressName (address, withName = true) { renderAddressName (address, withName = true) {
return ( return (
<span className={ styles.address }> <div className={ styles.addressContainer }>
<IdentityIcon center inline address={ address } className={ styles.identityicon } /> <InputAddress
{ withName ? <IdentityName address={ address } /> : address } disabled
</span> className={ styles.address }
value={ address }
text={ withName }
/>
</div>
); );
} }
@ -284,44 +393,57 @@ class MethodDecoding extends Component {
return; return;
} }
const { signature, paramdata } = api.util.decodeCallData(transaction.input); if (contractAddress === '0x') {
this.setState({ methodSignature: signature, methodParams: paramdata });
if (!signature || signature === CONTRACT_CREATE || transaction.creates) {
this.setState({ isDeploy: true });
return; return;
} }
Promise return api.eth
.all([ .getCode(contractAddress || transaction.creates)
api.eth.getCode(contractAddress), .then((bytecode) => {
Contracts.get().signatureReg.lookup(signature) const isContract = bytecode && /^(0x)?([0]*[1-9a-f]+[0]*)+$/.test(bytecode);
])
.then(([bytecode, method]) => {
let methodInputs = null;
let methodName = null;
if (method && method.length) { this.setState({ isContract });
const { methodParams } = this.state;
const abi = api.util.methodToAbi(method);
methodName = abi.name; if (!isContract) {
methodInputs = api.util return;
.decodeMethodInput(abi, methodParams)
.map((value, index) => {
const type = abi.inputs[index].type;
return { type, value };
});
} }
this.setState({ const { signature, paramdata } = api.util.decodeCallData(transaction.input);
method, this.setState({ methodSignature: signature, methodParams: paramdata });
methodName,
methodInputs, if (!signature || signature === CONTRACT_CREATE || transaction.creates) {
bytecode, this.setState({ isDeploy: true });
isContract: bytecode && bytecode !== '0x' return;
}); }
return Contracts.get()
.signatureReg
.lookup(signature)
.then((method) => {
let methodInputs = null;
let methodName = null;
if (method && method.length) {
const { methodParams } = this.state;
const abi = api.util.methodToAbi(method);
methodName = abi.name;
methodInputs = api.util
.decodeMethodInput(abi, methodParams)
.map((value, index) => {
const type = abi.inputs[index].type;
return { type, value };
});
}
this.setState({
method,
methodName,
methodInputs,
bytecode
});
});
}) })
.catch((error) => { .catch((error) => {
console.warn('lookup', error); console.warn('lookup', error);

View File

@ -35,7 +35,7 @@ use params::{ResealPolicy, AccountsConfig, GasPricerConfig, MinerExtras};
use ethcore_logger::Config as LogConfig; use ethcore_logger::Config as LogConfig;
use dir::Directories; use dir::Directories;
use dapps::Configuration as DappsConfiguration; use dapps::Configuration as DappsConfiguration;
use signer::{Configuration as SignerConfiguration, SignerCommand}; use signer::{Configuration as SignerConfiguration};
use run::RunCmd; use run::RunCmd;
use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, DataFormat}; use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, DataFormat};
use presale::ImportWallet; use presale::ImportWallet;
@ -49,7 +49,7 @@ pub enum Cmd {
Account(AccountCmd), Account(AccountCmd),
ImportPresaleWallet(ImportWallet), ImportPresaleWallet(ImportWallet),
Blockchain(BlockchainCmd), Blockchain(BlockchainCmd),
SignerToken(SignerCommand), SignerToken(SignerConfiguration),
Snapshot(SnapshotCommand), Snapshot(SnapshotCommand),
Hash(Option<String>), Hash(Option<String>),
} }
@ -103,9 +103,7 @@ impl Configuration {
let cmd = if self.args.flag_version { let cmd = if self.args.flag_version {
Cmd::Version Cmd::Version
} else if self.args.cmd_signer && self.args.cmd_new_token { } else if self.args.cmd_signer && self.args.cmd_new_token {
Cmd::SignerToken(SignerCommand { Cmd::SignerToken(signer_conf)
path: dirs.signer
})
} else if self.args.cmd_tools && self.args.cmd_hash { } else if self.args.cmd_tools && self.args.cmd_hash {
Cmd::Hash(self.args.arg_file) Cmd::Hash(self.args.arg_file)
} else if self.args.cmd_account { } else if self.args.cmd_account {
@ -690,7 +688,7 @@ mod tests {
use ethcore::miner::{MinerOptions, PrioritizationStrategy}; use ethcore::miner::{MinerOptions, PrioritizationStrategy};
use helpers::{replace_home, default_network_config}; use helpers::{replace_home, default_network_config};
use run::RunCmd; use run::RunCmd;
use signer::{Configuration as SignerConfiguration, SignerCommand}; use signer::{Configuration as SignerConfiguration};
use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, DataFormat}; use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, DataFormat};
use presale::ImportWallet; use presale::ImportWallet;
use account::{AccountCmd, NewAccount, ImportAccounts}; use account::{AccountCmd, NewAccount, ImportAccounts};
@ -827,8 +825,12 @@ mod tests {
let args = vec!["parity", "signer", "new-token"]; let args = vec!["parity", "signer", "new-token"];
let conf = parse(&args); let conf = parse(&args);
let expected = replace_home("$HOME/.parity/signer"); let expected = replace_home("$HOME/.parity/signer");
assert_eq!(conf.into_command().unwrap().cmd, Cmd::SignerToken(SignerCommand { assert_eq!(conf.into_command().unwrap().cmd, Cmd::SignerToken(SignerConfiguration {
path: expected, enabled: true,
signer_path: expected,
interface: "127.0.0.1".into(),
port: 8180,
skip_origin_validation: false,
})); }));
} }

View File

@ -93,13 +93,29 @@ pub struct RunCmd {
pub check_seal: bool, pub check_seal: bool,
} }
pub fn open_ui(dapps_conf: &dapps::Configuration, signer_conf: &signer::Configuration) -> Result<(), String> {
if !dapps_conf.enabled {
return Err("Cannot use UI command with Dapps turned off.".into())
}
if !signer_conf.enabled {
return Err("Cannot use UI command with UI turned off.".into())
}
let token = try!(signer::generate_token_and_url(signer_conf));
// Open a browser
url::open(&token.url);
// Print a message
println!("{}", token.message);
Ok(())
}
pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> { pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
if cmd.ui && cmd.dapps_conf.enabled { if cmd.ui && cmd.dapps_conf.enabled {
// Check if Parity is already running // Check if Parity is already running
let addr = format!("{}:{}", cmd.dapps_conf.interface, cmd.dapps_conf.port); let addr = format!("{}:{}", cmd.dapps_conf.interface, cmd.dapps_conf.port);
if !TcpListener::bind(&addr as &str).is_ok() { if !TcpListener::bind(&addr as &str).is_ok() {
url::open(&format!("http://{}:{}/", cmd.dapps_conf.interface, cmd.dapps_conf.port)); return open_ui(&cmd.dapps_conf, &cmd.signer_conf);
return Ok(());
} }
} }
@ -312,7 +328,7 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
}; };
// start signer server // start signer server
let signer_server = try!(signer::start(cmd.signer_conf, signer_deps)); let signer_server = try!(signer::start(cmd.signer_conf.clone(), signer_deps));
let informant = Arc::new(Informant::new( let informant = Arc::new(Informant::new(
service.client(), service.client(),
@ -366,10 +382,7 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
// start ui // start ui
if cmd.ui { if cmd.ui {
if !cmd.dapps_conf.enabled { try!(open_ui(&cmd.dapps_conf, &cmd.signer_conf));
return Err("Cannot use UI command with Dapps turned off.".into())
}
url::open(&format!("http://{}:{}/", cmd.dapps_conf.interface, cmd.dapps_conf.port));
} }
// Handle exit // Handle exit

View File

@ -27,7 +27,7 @@ pub use ethcore_signer::Server as SignerServer;
const CODES_FILENAME: &'static str = "authcodes"; const CODES_FILENAME: &'static str = "authcodes";
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub struct Configuration { pub struct Configuration {
pub enabled: bool, pub enabled: bool,
pub port: u16, pub port: u16,
@ -53,6 +53,12 @@ pub struct Dependencies {
pub apis: Arc<rpc_apis::Dependencies>, pub apis: Arc<rpc_apis::Dependencies>,
} }
pub struct NewToken {
pub token: String,
pub url: String,
pub message: String,
}
pub fn start(conf: Configuration, deps: Dependencies) -> Result<Option<SignerServer>, String> { pub fn start(conf: Configuration, deps: Dependencies) -> Result<Option<SignerServer>, String> {
if !conf.enabled { if !conf.enabled {
Ok(None) Ok(None)
@ -68,20 +74,33 @@ fn codes_path(path: String) -> PathBuf {
p p
} }
#[derive(Debug, PartialEq)] pub fn execute(cmd: Configuration) -> Result<String, String> {
pub struct SignerCommand { Ok(try!(generate_token_and_url(&cmd)).message)
pub path: String,
} }
pub fn execute(cmd: SignerCommand) -> Result<String, String> { pub fn generate_token_and_url(conf: &Configuration) -> Result<NewToken, String> {
generate_new_token(cmd.path) let code = try!(generate_new_token(conf.signer_path.clone()).map_err(|err| format!("Error generating token: {:?}", err)));
.map(|code| format!("This key code will authorise your System Signer UI: {}", Colour::White.bold().paint(code))) let auth_url = format!("http://{}:{}/#/auth?token={}", conf.interface, conf.port, code);
.map_err(|err| format!("Error generating token: {:?}", err)) // And print in to the console
Ok(NewToken {
token: code.clone(),
url: auth_url.clone(),
message: format!(
r#"
Open: {}
to authorize your browser.
Or use the generated token:
{}"#,
Colour::White.bold().paint(auth_url),
code
)
})
} }
pub fn generate_new_token(path: String) -> io::Result<String> { pub fn generate_new_token(path: String) -> io::Result<String> {
let path = codes_path(path); let path = codes_path(path);
let mut codes = try!(signer::AuthCodes::from_file(&path)); let mut codes = try!(signer::AuthCodes::from_file(&path));
codes.clear_garbage();
let code = try!(codes.generate_new()); let code = try!(codes.generate_new());
try!(codes.to_file(&path)); try!(codes.to_file(&path));
trace!("New key code created: {}", Colour::White.bold().paint(&code[..])); trace!("New key code created: {}", Colour::White.bold().paint(&code[..]));

View File

@ -16,12 +16,10 @@
use rand::Rng; use rand::Rng;
use rand::os::OsRng; use rand::os::OsRng;
use std::io; use std::io::{self, Read, Write};
use std::io::{Read, Write};
use std::fs;
use std::path::Path; use std::path::Path;
use std::time; use std::{fs, time, mem};
use util::{H256, Hashable}; use util::{H256, Hashable, Itertools};
/// Providing current time in seconds /// Providing current time in seconds
pub trait TimeProvider { pub trait TimeProvider {
@ -47,12 +45,35 @@ impl TimeProvider for DefaultTimeProvider {
/// No of seconds the hash is valid /// No of seconds the hash is valid
const TIME_THRESHOLD: u64 = 7; const TIME_THRESHOLD: u64 = 7;
/// minimal length of hash
const TOKEN_LENGTH: usize = 16; const TOKEN_LENGTH: usize = 16;
/// special "initial" token used for authorization when there are no tokens yet.
const INITIAL_TOKEN: &'static str = "initial"; const INITIAL_TOKEN: &'static str = "initial";
/// Separator between fields in serialized tokens file.
const SEPARATOR: &'static str = ";";
/// Number of seconds to keep unused tokens.
const UNUSED_TOKEN_TIMEOUT: u64 = 3600 * 24; // a day
struct Code {
code: String,
/// Duration since unix_epoch
created_at: time::Duration,
/// Duration since unix_epoch
last_used_at: Option<time::Duration>,
}
fn decode_time(val: &str) -> Option<time::Duration> {
let time = val.parse::<u64>().ok();
time.map(time::Duration::from_secs)
}
fn encode_time(time: time::Duration) -> String {
format!("{}", time.as_secs())
}
/// Manages authorization codes for `SignerUIs` /// Manages authorization codes for `SignerUIs`
pub struct AuthCodes<T: TimeProvider = DefaultTimeProvider> { pub struct AuthCodes<T: TimeProvider = DefaultTimeProvider> {
codes: Vec<String>, codes: Vec<Code>,
now: T, now: T,
} }
@ -69,13 +90,32 @@ impl AuthCodes<DefaultTimeProvider> {
"".into() "".into()
} }
}; };
let time_provider = DefaultTimeProvider::default();
let codes = content.lines() let codes = content.lines()
.filter(|f| f.len() >= TOKEN_LENGTH) .filter_map(|line| {
.map(String::from) let mut parts = line.split(SEPARATOR);
let token = parts.next();
let created = parts.next();
let used = parts.next();
match token {
None => None,
Some(token) if token.len() < TOKEN_LENGTH => None,
Some(token) => {
Some(Code {
code: token.into(),
last_used_at: used.and_then(decode_time),
created_at: created.and_then(decode_time)
.unwrap_or_else(|| time::Duration::from_secs(time_provider.now())),
})
}
}
})
.collect(); .collect();
Ok(AuthCodes { Ok(AuthCodes {
codes: codes, codes: codes,
now: DefaultTimeProvider::default(), now: time_provider,
}) })
} }
@ -86,19 +126,30 @@ impl<T: TimeProvider> AuthCodes<T> {
/// Writes all `AuthCodes` to a disk. /// Writes all `AuthCodes` to a disk.
pub fn to_file(&self, file: &Path) -> io::Result<()> { pub fn to_file(&self, file: &Path) -> io::Result<()> {
let mut file = try!(fs::File::create(file)); let mut file = try!(fs::File::create(file));
let content = self.codes.join("\n"); let content = self.codes.iter().map(|code| {
let mut data = vec![code.code.clone(), encode_time(code.created_at.clone())];
if let Some(used_at) = code.last_used_at.clone() {
data.push(encode_time(used_at));
}
data.join(SEPARATOR)
}).join("\n");
file.write_all(content.as_bytes()) file.write_all(content.as_bytes())
} }
/// Creates a new `AuthCodes` store with given `TimeProvider`. /// Creates a new `AuthCodes` store with given `TimeProvider`.
pub fn new(codes: Vec<String>, now: T) -> Self { pub fn new(codes: Vec<String>, now: T) -> Self {
AuthCodes { AuthCodes {
codes: codes, codes: codes.into_iter().map(|code| Code {
code: code,
created_at: time::Duration::from_secs(now.now()),
last_used_at: None,
}).collect(),
now: now, now: now,
} }
} }
/// Checks if given hash is correct identifier of `SignerUI` /// Checks if given hash is correct authcode of `SignerUI`
/// Updates this hash last used field in case it's valid.
#[cfg_attr(feature="dev", allow(wrong_self_convention))] #[cfg_attr(feature="dev", allow(wrong_self_convention))]
pub fn is_valid(&mut self, hash: &H256, time: u64) -> bool { pub fn is_valid(&mut self, hash: &H256, time: u64) -> bool {
let now = self.now.now(); let now = self.now.now();
@ -121,8 +172,14 @@ impl<T: TimeProvider> AuthCodes<T> {
} }
// look for code // look for code
self.codes.iter() for mut code in &mut self.codes {
.any(|code| &as_token(code) == hash) if &as_token(&code.code) == hash {
code.last_used_at = Some(time::Duration::from_secs(now));
return true;
}
}
false
} }
/// Generates and returns a new code that can be used by `SignerUIs` /// Generates and returns a new code that can be used by `SignerUIs`
@ -135,7 +192,11 @@ impl<T: TimeProvider> AuthCodes<T> {
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("-"); .join("-");
trace!(target: "signer", "New authentication token generated."); trace!(target: "signer", "New authentication token generated.");
self.codes.push(code); self.codes.push(Code {
code: code,
created_at: time::Duration::from_secs(self.now.now()),
last_used_at: None,
});
Ok(readable_code) Ok(readable_code)
} }
@ -143,12 +204,31 @@ impl<T: TimeProvider> AuthCodes<T> {
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.codes.is_empty() self.codes.is_empty()
} }
}
/// Removes old tokens that have not been used since creation.
pub fn clear_garbage(&mut self) {
let now = self.now.now();
let threshold = time::Duration::from_secs(now.saturating_sub(UNUSED_TOKEN_TIMEOUT));
let codes = mem::replace(&mut self.codes, Vec::new());
for code in codes {
// Skip codes that are old and were never used.
if code.last_used_at.is_none() && code.created_at <= threshold {
continue;
}
self.codes.push(code);
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use devtools;
use std::io::{Read, Write};
use std::{time, fs};
use std::cell::Cell;
use util::{H256, Hashable}; use util::{H256, Hashable};
use super::*; use super::*;
@ -217,6 +297,54 @@ mod tests {
assert_eq!(res2, false); assert_eq!(res2, false);
} }
#[test]
fn should_read_old_format_from_file() {
// given
let path = devtools::RandomTempPath::new();
let code = "23521352asdfasdfadf";
{
let mut file = fs::File::create(&path).unwrap();
file.write_all(b"a\n23521352asdfasdfadf\nb\n").unwrap();
}
// when
let mut authcodes = AuthCodes::from_file(&path).unwrap();
let time = time::UNIX_EPOCH.elapsed().unwrap().as_secs();
// then
assert!(authcodes.is_valid(&generate_hash(code, time), time), "Code should be read from file");
}
#[test]
fn should_remove_old_unused_tokens() {
// given
let path = devtools::RandomTempPath::new();
let code1 = "11111111asdfasdf111";
let code2 = "22222222asdfasdf222";
let code3 = "33333333asdfasdf333";
let time = Cell::new(100);
let mut codes = AuthCodes::new(vec![code1.into(), code2.into(), code3.into()], || time.get());
// `code2` should not be removed (we never remove tokens that were used)
codes.is_valid(&generate_hash(code2, time.get()), time.get());
// when
time.set(100 + 10_000_000);
// mark `code1` as used now
codes.is_valid(&generate_hash(code1, time.get()), time.get());
let new_code = codes.generate_new().unwrap().replace('-', "");
codes.clear_garbage();
codes.to_file(&path).unwrap();
// then
let mut content = String::new();
let mut file = fs::File::open(&path).unwrap();
file.read_to_string(&mut content).unwrap();
assert_eq!(content, format!("{};100;10000100\n{};100;100\n{};10000100", code1, code2, new_code));
}
} }

View File

@ -94,6 +94,9 @@ fn auth_is_valid(codes_path: &Path, protocols: ws::Result<Vec<&str>>) -> bool {
// Check if the code is valid // Check if the code is valid
AuthCodes::from_file(codes_path) AuthCodes::from_file(codes_path)
.map(|mut codes| { .map(|mut codes| {
// remove old tokens
codes.clear_garbage();
let res = codes.is_valid(&auth, time); let res = codes.is_valid(&auth, time);
// make sure to save back authcodes - it might have been modified // make sure to save back authcodes - it might have been modified
if let Err(_) = codes.to_file(codes_path) { if let Err(_) = codes.to_file(codes_path) {