Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 63 additions & 72 deletions app/util/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,48 @@ export const TOKEN_METHOD_TRANSFER_FROM = 'transferfrom';
export const CONTRACT_METHOD_DEPLOY = 'deploy';

export const SEND_ETHER_ACTION_KEY = 'sentEther';
export const DEPLOY_CONTRACT_ACTION_KEY = 'contractDeployment';
export const DEPLOY_CONTRACT_ACTION_KEY = 'deploy';
export const APPROVE_ACTION_KEY = 'approve';
export const SEND_TOKEN_ACTION_KEY = 'sentTokens';
export const TRANSFER_FROM_ACTION_KEY = 'transferFrom';
export const SEND_TOKEN_ACTION_KEY = 'transfer';
export const TRANSFER_FROM_ACTION_KEY = 'transferfrom';
export const UNKNOWN_FUNCTION_KEY = 'unknownFunction';
export const SMART_CONTRACT_INTERACTION_ACTION_KEY = 'smartContractInteraction';

export const TRANSFER_FUNCTION_SIGNATURE = '0xa9059cbb';
export const TRANSFER_FROM_FUNCTION_SIGNATURE = '0x23b872dd';
export const APPROVE_FUNCTION_SIGNATURE = '0x095ea7b3';
export const CONTRACT_CREATION_SIGNATURE = '0x60a060405260046060527f48302e31';

/**
* Utility class with the single responsibility
* of caching ActionKeys
* of caching CollectibleAddresses
*/
class ActionKeys {
class CollectibleAddresses {
static cache = {};
}

/**
* Utility class with the single responsibility
* of caching CollectibleAddresses
* Object containing all known action keys, to be used in transaction review
*/
class CollectibleAddresses {
static cache = {};
}
const reviewActionKeys = {
[SEND_TOKEN_ACTION_KEY]: strings('transactions.tx_review_transfer'),
[SEND_ETHER_ACTION_KEY]: strings('transactions.tx_review_confirm'),
[DEPLOY_CONTRACT_ACTION_KEY]: strings('transactions.tx_review_contract_deployment'),
[TRANSFER_FROM_ACTION_KEY]: strings('transactions.tx_review_transfer_from'),
[SMART_CONTRACT_INTERACTION_ACTION_KEY]: strings('transactions.tx_review_unknown'),
[APPROVE_ACTION_KEY]: strings('transactions.tx_review_approve')
};

/**
* Object containing all known action keys, to be used in transactions list
*/
const actionKeys = {
[SEND_TOKEN_ACTION_KEY]: strings('transactions.sent_tokens'),
[TRANSFER_FROM_ACTION_KEY]: strings('transactions.sent_collectible'),
[DEPLOY_CONTRACT_ACTION_KEY]: strings('transactions.contract_deploy'),
[SMART_CONTRACT_INTERACTION_ACTION_KEY]: strings('transactions.smart_contract_interaction'),
[APPROVE_ACTION_KEY]: strings('transactions.approve')
};

/**
* Generates transfer data for specified method
Expand Down Expand Up @@ -117,15 +133,28 @@ export function decodeTransferData(type, data) {
* @param {string} data - Transaction data
* @returns {object} - Method data object containing the name if is valid
*/
export function getMethodData(data) {
// TODO use eth-method-registry from GABA
if (data.substr(0, 10) === TRANSFER_FUNCTION_SIGNATURE) {
export async function getMethodData(data) {
if (data.length < 10) return {};
const fourByteSignature = data.substr(0, 10);
if (fourByteSignature === TRANSFER_FUNCTION_SIGNATURE) {
return { name: TOKEN_METHOD_TRANSFER };
} else if (data.substr(0, 10) === TRANSFER_FROM_FUNCTION_SIGNATURE) {
} else if (fourByteSignature === TRANSFER_FROM_FUNCTION_SIGNATURE) {
return { name: TOKEN_METHOD_TRANSFER_FROM };
} else if (fourByteSignature === APPROVE_FUNCTION_SIGNATURE) {
return { name: TOKEN_METHOD_APPROVE };
} else if (data.substr(0, 32) === CONTRACT_CREATION_SIGNATURE) {
return { name: CONTRACT_METHOD_DEPLOY };
}
const { TransactionController } = Engine.context;
// If it's a new method, use on-chain method registry
try {
const registryObject = await TransactionController.handleMethodData(fourByteSignature);
if (registryObject) {
return registryObject.parsedRegistryMethod;
}
} catch (e) {
// Ignore and return empty object

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this look in the UI when it fails? empty? Should we default to smart contract interaction?

@estebanmino estebanmino Apr 2, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we default to smart contract interaction if the transaction is made to a smart contract https://github.com/MetaMask/MetaMask/pull/571/files/8c3c9b6b63a12279623051ca679273bc3d6768e1#diff-fc361a43ab6582de245f077607ee2be9R222
Information that comes from GABA using transaction.toSmartContract
Anyways very good point I believe I made a mistake here getTransactionReviewActionKey if it fails will show CONFIRM in tx review instead of UNKNOWN METHOD

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brunobar79 solved now, defaulting to 'UNKNOWN METHOD' if method is unknown instead of 'Smart Contract Interaction' in Transaction Review. In transactions list will show 'Smart Contract Interaction' as we did until now.

}
return {};
}

Expand Down Expand Up @@ -175,35 +204,13 @@ export async function isCollectibleAddress(address, tokenId) {
* @returns {string} - Corresponding transaction action key
*/
export async function getTransactionActionKey(transaction) {
const { transactionHash, transaction: { data, to } = {} } = transaction;
const cache = ActionKeys.cache[transactionHash];
if (transactionHash && cache) {
return Promise.resolve(cache);
}
const { transaction: { data, to } = {} } = transaction;
let ret;
// if data in transaction try to get method data
if (data && data !== '0x') {
const methodData = getMethodData(data);
const methodData = await getMethodData(data);
const { name } = methodData;
const methodName = name && name.toLowerCase();
switch (methodName) {
case TOKEN_METHOD_TRANSFER:
ret = SEND_TOKEN_ACTION_KEY;
break;
case TOKEN_METHOD_APPROVE:
ret = APPROVE_ACTION_KEY;
break;
case TOKEN_METHOD_TRANSFER_FROM:
ret = TRANSFER_FROM_ACTION_KEY;
break;
case CONTRACT_METHOD_DEPLOY:
ret = DEPLOY_CONTRACT_ACTION_KEY;
break;
}
if (ret) {
ActionKeys.cache[transactionHash] = ret;
return ret;
}
if (name) return name;
}
const toSmartContract =
transaction.toSmartContract !== undefined ? transaction.toSmartContract : await isSmartContractAddress(to);
Expand All @@ -214,7 +221,6 @@ export async function getTransactionActionKey(transaction) {
// If there is no data and no smart contract interaction
ret = SEND_ETHER_ACTION_KEY;
}
ActionKeys.cache[transactionHash] = ret;
return ret;
}

Expand All @@ -227,26 +233,20 @@ export async function getTransactionActionKey(transaction) {
*/
export async function getActionKey(tx, selectedAddress) {
const actionKey = await getTransactionActionKey(tx);
const incoming = toChecksumAddress(tx.transaction.to) === toChecksumAddress(selectedAddress);
const selfSent = incoming && toChecksumAddress(tx.transaction.from) === toChecksumAddress(selectedAddress);
switch (actionKey) {
case SEND_TOKEN_ACTION_KEY:
return strings('transactions.sent_tokens');
case TRANSFER_FROM_ACTION_KEY:
return strings('transactions.sent_collectible');
case SEND_ETHER_ACTION_KEY:
return incoming
? selfSent
? strings('transactions.self_sent_ether')
: strings('transactions.received_ether')
: strings('transactions.sent_ether');
case DEPLOY_CONTRACT_ACTION_KEY:
return strings('transactions.contract_deploy');
case SMART_CONTRACT_INTERACTION_ACTION_KEY:
return strings('transactions.smart_contract_interaction');
default:
return strings('transactions.smart_contract_interaction');
if (actionKey === SEND_ETHER_ACTION_KEY) {
const incoming = toChecksumAddress(tx.transaction.to) === toChecksumAddress(selectedAddress);
const selfSent = incoming && toChecksumAddress(tx.transaction.from) === toChecksumAddress(selectedAddress);
return incoming
? selfSent
? strings('transactions.self_sent_ether')
: strings('transactions.received_ether')
: strings('transactions.sent_ether');
}
const transactionActionKey = actionKeys[actionKey];
if (transactionActionKey) {
return transactionActionKey;
}
return actionKey;
}

/**
Expand All @@ -257,18 +257,9 @@ export async function getActionKey(tx, selectedAddress) {
*/
export async function getTransactionReviewActionKey(transaction) {
const actionKey = await getTransactionActionKey({ transaction });
switch (actionKey) {
case SEND_TOKEN_ACTION_KEY:
return strings('transactions.tx_review_transfer');
case SEND_ETHER_ACTION_KEY:
return strings('transactions.tx_review_confirm');
case DEPLOY_CONTRACT_ACTION_KEY:
return strings('transactions.tx_review_contract_deployment');
case TRANSFER_FROM_ACTION_KEY:
return strings('transactions.tx_review_transfer_from');
case SMART_CONTRACT_INTERACTION_ACTION_KEY:
return strings('transactions.tx_review_confirm');
default:
return strings('transactions.tx_review_unknown');
const transactionReviewActionKey = reviewActionKeys[actionKey];
if (transactionReviewActionKey) {
return transactionReviewActionKey;
}
return actionKey;
}
16 changes: 10 additions & 6 deletions app/util/transactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,21 @@ describe('Transactions utils :: decodeTransferData', () => {
});

describe('Transactions utils :: getMethodData', () => {
it('getMethodData', () => {
it('getMethodData', async () => {
const transferData =
'0xa9059cbb00000000000000000000000056ced0d816c668d7c0bcc3fbf0ab2c6896f589a00000000000000000000000000000000000000000000000000000000000000001';
const contractData =
'0x60a060405260046060527f48302e31000000000000000000000000000000000000000000000000000000006080526006805460008290527f48302e310000000000000000000000000000000000000000000000000000000882556100b5907ff652222313e28459528d920b65115c16c04f3efc82aaedc97be59f3f377c0d3f602060026001841615610100026000190190931692909204601f01919091048101905b8082111561017957600081556001016100a1565b505060405161094b38038061094b833981';
const randomData = '0x987654321';
const randomData = '0x987654321000000000';
const transferFromData = '0x23b872dd0000000000000000000000000000';
expect(getMethodData(transferData).name).toEqual(TOKEN_METHOD_TRANSFER);
expect(getMethodData(contractData).name).toEqual(CONTRACT_METHOD_DEPLOY);
expect(getMethodData(transferFromData).name).toEqual(TOKEN_METHOD_TRANSFER_FROM);
expect(getMethodData(randomData)).toEqual({});
const firstMethodData = await getMethodData(transferData);
const secondtMethodData = await getMethodData(contractData);
const thirdMethodData = await getMethodData(transferFromData);
const fourthMethodData = await getMethodData(randomData);
expect(firstMethodData.name).toEqual(TOKEN_METHOD_TRANSFER);
expect(secondtMethodData.name).toEqual(CONTRACT_METHOD_DEPLOY);
expect(thirdMethodData.name).toEqual(TOKEN_METHOD_TRANSFER_FROM);
expect(fourthMethodData).toEqual({});
});
});

Expand Down
2 changes: 2 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@
"tx_review_contract_deployment": "CONTRACT DEPLOYMENT",
"tx_review_transfer_from": "TRANSFER FROM",
"tx_review_unknown": "UNKNOWN METHOD",
"tx_review_approve": "APPROVE",
"sent_ether": "Sent Ether",
"self_sent_ether": "Sent Yourself Ether",
"received_ether": "Received Ether",
Expand All @@ -394,6 +395,7 @@
"contract_deploy": "Contract Deployment",
"to_contract": "New Contract",
"smart_contract_interaction": "Smart Contract Interaction",
"approve": "Approve",
"hash": "Hash",
"from": "From",
"to": "To",
Expand Down
2 changes: 2 additions & 0 deletions locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@
"tx_review_contract_deployment": "CONTRATO DESPLEGADO",
"tx_review_transfer_from": "TRANSFERIR DESDE",
"tx_review_unknown": "MÉTODO DESCONOCIDO",
"tx_review_approve": "APROBAR",
"sent_ether": "Enviaste Ether",
"self_sent_ether": "Enviaste Ether a ti mismo",
"received_ether": "Recibiste Ether",
Expand All @@ -392,6 +393,7 @@
"contract_deploy": "Despliegue de contrato",
"to_contract": "Nuevo Contrato",
"smart_contract_interaction": "Interación con contrato inteligente ",
"approve": "Aprobar",
"hash": "Hash",
"from": "Desde",
"to": "Para",
Expand Down
Loading