Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
Fixes address queries (#501)
Browse files Browse the repository at this point in the history
* Bulletproofing account queries

* Extracting function

* Hardening address query

* Not reassigning function params enables better debug sessions
  • Loading branch information
evertonfraga authored Nov 16, 2018
1 parent 0263fff commit 179803e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
5 changes: 3 additions & 2 deletions app/client/lib/ethereum/observeTransactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ addTransactionAfterSend = function(
to,
gasPrice,
estimatedGas,
data,
rawData,
tokenId
) {
var jsonInterface = undefined,
contractName = undefined,
data = undefined,
txId = Helpers.makeId('tx', txHash);

if (_.isObject(data)) {
contractName = data.contract.name.replace(/([A-Z])/g, ' $1');
jsonInterface = data.contract.jsonInterface;
data = data.data;
data = rawData.data;
}

Transactions.upsert(txId, {
Expand Down
1 change: 0 additions & 1 deletion app/client/lib/ethereum/walletInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,6 @@ checkWalletOwners = function(address) {
function() {
returnValue.owners = owners;

// FIXME: helper should take address checksum into consideration
if ((account = Helpers.getAccountByAddress({ $in: owners }))) {
returnValue.info = TAPi18n.__(
'wallet.newWallet.accountType.import.youreOwner',
Expand Down
34 changes: 28 additions & 6 deletions app/client/lib/helpers/helperFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ Helpers.showNotification = function(i18nText, values, callback) {
if (typeof mist !== 'undefined') mist.sounds.bip();
};

var multipleCaseAddresses = function(address) {
return [address.toLowerCase(), web3.utils.toChecksumAddress(address)];
};

/**
Gets the docuement matching the given addess from the EthAccounts or Wallets collection.
Expand All @@ -199,13 +203,28 @@ Gets the docuement matching the given addess from the EthAccounts or Wallets col
@param {Boolean} reactive
*/
Helpers.getAccountByAddress = function(address, reactive) {
if (address == null) {
return null;
}
var options = reactive === false ? { reactive: false } : {};
// if(_.isString(address))
// address = address.toLowerCase();
var query;

if (_.isString(address)) {
query = { address: { $in: multipleCaseAddresses(address) } };
} else if ('$in' in address) {
// If provided query is a list of accounts, unwrap it and adds redundant addresses.
var addressArray = _.flatten(
address.$in.map(e => multipleCaseAddresses(e))
);
query = { address: { $in: addressArray } };
} else {
query = { address: address };
}

return (
EthAccounts.findOne({ address: address }, options) ||
Wallets.findOne({ address: address }, options) ||
CustomContracts.findOne({ address: address }, options)
EthAccounts.findOne(query, options) ||
Wallets.findOne(query, options) ||
CustomContracts.findOne(query, options)
);
};

Expand All @@ -218,7 +237,10 @@ Gets the docuement matching the given query from the EthAccounts or Wallets coll
*/
Helpers.getAccounts = function(query, reactive) {
var options = reactive === false ? { reactive: false } : {};
if (_.isString(query.address)) query.address = query.address.toLowerCase();
if (_.isString(query.address)) {
query.address = { $in: multipleCaseAddresses(query.address) };
}

return EthAccounts.find(query, options)
.fetch()
.concat(Wallets.find(query, options).fetch());
Expand Down
6 changes: 4 additions & 2 deletions app/client/templates/elements/transactionTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ Template['elements_transactions_row'].helpers({
transactionType: function() {
var to = Helpers.getAccountByAddress(this.to),
from = Helpers.getAccountByAddress(this.from),
initiator = Helpers.getAccountByAddress(this.initiator),
initiator = this.initiator
? Helpers.getAccountByAddress(this.initiator)
: null,
sendData = this.data;

if (from)
Expand Down Expand Up @@ -226,7 +228,7 @@ Template['elements_transactions_row'].helpers({
return blocksForConfirmation >= confirmations && confirmations >= 0
? {
confirmations: confirmations,
percent: confirmations / blocksForConfirmation * 100
percent: (confirmations / blocksForConfirmation) * 100
}
: false;
},
Expand Down
12 changes: 9 additions & 3 deletions app/client/templates/views/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ var translateExternalErrorMessage = function(message) {
}
};

var getSelectedFromAccount = function() {
return TemplateVar.getFrom(
'select[name="dapp-select-account"].send-from',
'value'
);
};

// Set basic variables
Template['views_send'].onCreated(function() {
var template = this;
Expand Down Expand Up @@ -604,9 +611,8 @@ Template['views_send'].events({
to = TemplateVar.getFrom('.dapp-address-input .to', 'value'),
gasPrice = TemplateVar.getFrom('.dapp-select-gas-price', 'gasPrice'),
estimatedGas = TemplateVar.get('estimatedGas'),
selectedAccount = Helpers.getAccountByAddress(
template.find('select[name="dapp-select-account"].send-from').value
),
selectedFrom = getSelectedFromAccount(),
selectedAccount = Helpers.getAccountByAddress(selectedFrom),
selectedAction = TemplateVar.get('selectedAction'),
data = getDataField(),
contract = TemplateVar.getFrom('.compile-contract', 'contract'),
Expand Down

0 comments on commit 179803e

Please sign in to comment.