2018-07-31 07:03:20 +02:00
|
|
|
import ethUtil from 'ethereumjs-util'
|
|
|
|
import MethodRegistry from 'eth-method-registry'
|
2018-08-06 07:25:58 +02:00
|
|
|
import abi from 'human-standard-token-abi'
|
|
|
|
import abiDecoder from 'abi-decoder'
|
2018-12-09 21:48:06 +01:00
|
|
|
import {
|
|
|
|
TRANSACTION_TYPE_CANCEL,
|
|
|
|
TRANSACTION_STATUS_CONFIRMED,
|
2019-03-22 00:03:30 +01:00
|
|
|
} from '../../../../app/scripts/controllers/transactions/enums'
|
2020-06-04 21:22:45 +02:00
|
|
|
import { MESSAGE_TYPE } from '../../../../app/scripts/lib/enums'
|
2020-07-08 23:05:09 +02:00
|
|
|
import { getEtherscanNetworkPrefix } from '../../../lib/etherscan-prefix-for-network'
|
2019-06-18 14:17:14 +02:00
|
|
|
import fetchWithCache from './fetch-with-cache'
|
2018-08-03 05:20:15 +02:00
|
|
|
|
2018-07-31 07:03:20 +02:00
|
|
|
import {
|
|
|
|
TOKEN_METHOD_TRANSFER,
|
|
|
|
TOKEN_METHOD_APPROVE,
|
|
|
|
TOKEN_METHOD_TRANSFER_FROM,
|
|
|
|
SEND_ETHER_ACTION_KEY,
|
|
|
|
DEPLOY_CONTRACT_ACTION_KEY,
|
|
|
|
APPROVE_ACTION_KEY,
|
|
|
|
SEND_TOKEN_ACTION_KEY,
|
|
|
|
TRANSFER_FROM_ACTION_KEY,
|
2018-08-07 07:39:54 +02:00
|
|
|
SIGNATURE_REQUEST_KEY,
|
2020-02-19 19:24:16 +01:00
|
|
|
DECRYPT_REQUEST_KEY,
|
|
|
|
ENCRYPTION_PUBLIC_KEY_REQUEST_KEY,
|
2018-12-09 21:48:06 +01:00
|
|
|
CONTRACT_INTERACTION_KEY,
|
2018-09-17 19:34:29 +02:00
|
|
|
CANCEL_ATTEMPT_ACTION_KEY,
|
2019-08-16 20:54:10 +02:00
|
|
|
DEPOSIT_TRANSACTION_KEY,
|
2018-07-31 07:03:20 +02:00
|
|
|
} from '../constants/transactions'
|
|
|
|
|
2019-03-29 15:48:08 +01:00
|
|
|
import log from 'loglevel'
|
2019-03-22 00:03:30 +01:00
|
|
|
import { addCurrencies } from './conversion-util'
|
2018-08-31 21:36:07 +02:00
|
|
|
|
2018-08-06 07:25:58 +02:00
|
|
|
abiDecoder.addABI(abi)
|
|
|
|
|
2018-09-19 02:20:28 +02:00
|
|
|
export function getTokenData (data = '') {
|
2018-08-06 07:25:58 +02:00
|
|
|
return abiDecoder.decodeMethod(data)
|
|
|
|
}
|
|
|
|
|
2019-06-18 14:17:14 +02:00
|
|
|
async function getMethodFrom4Byte (fourBytePrefix) {
|
|
|
|
const fourByteResponse = (await fetchWithCache(`https://www.4byte.directory/api/v1/signatures/?hex_signature=${fourBytePrefix}`, {
|
|
|
|
referrerPolicy: 'no-referrer-when-downgrade',
|
|
|
|
body: null,
|
|
|
|
method: 'GET',
|
|
|
|
mode: 'cors',
|
|
|
|
}))
|
|
|
|
|
2019-09-04 21:10:23 +02:00
|
|
|
if (fourByteResponse.count === 1) {
|
|
|
|
return fourByteResponse.results[0].text_signature
|
2019-06-18 14:17:14 +02:00
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 15:14:27 +01:00
|
|
|
let registry
|
2018-08-06 07:25:58 +02:00
|
|
|
|
2018-10-24 17:27:16 +02:00
|
|
|
/**
|
2019-06-18 14:17:14 +02:00
|
|
|
* Attempts to return the method data from the MethodRegistry library, the message registry library and the token abi, in that order of preference
|
|
|
|
* @param {string} fourBytePrefix - The prefix from the method code associated with the data
|
2018-10-24 17:27:16 +02:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2019-06-18 14:17:14 +02:00
|
|
|
export async function getMethodDataAsync (fourBytePrefix) {
|
|
|
|
try {
|
|
|
|
const fourByteSig = getMethodFrom4Byte(fourBytePrefix).catch((e) => {
|
|
|
|
log.error(e)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
2020-03-17 15:14:27 +01:00
|
|
|
if (!registry) {
|
|
|
|
registry = new MethodRegistry({ provider: global.ethereumProvider })
|
|
|
|
}
|
|
|
|
|
2019-06-18 14:17:14 +02:00
|
|
|
let sig = await registry.lookup(fourBytePrefix)
|
|
|
|
|
|
|
|
if (!sig) {
|
|
|
|
sig = await fourByteSig
|
2019-03-29 15:48:08 +01:00
|
|
|
}
|
2018-10-24 17:27:16 +02:00
|
|
|
|
2019-06-18 14:17:14 +02:00
|
|
|
if (!sig) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsedResult = registry.parse(sig)
|
2018-08-06 07:25:58 +02:00
|
|
|
|
2019-06-18 14:17:14 +02:00
|
|
|
return {
|
|
|
|
name: parsedResult.name,
|
|
|
|
params: parsedResult.args,
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
log.error(error)
|
|
|
|
return {}
|
|
|
|
}
|
2018-08-06 07:25:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 07:03:20 +02:00
|
|
|
export function isConfirmDeployContract (txData = {}) {
|
|
|
|
const { txParams = {} } = txData
|
|
|
|
return !txParams.to
|
|
|
|
}
|
|
|
|
|
2019-01-22 19:22:56 +01:00
|
|
|
/**
|
|
|
|
* Returns four-byte method signature from data
|
|
|
|
*
|
|
|
|
* @param {string} data - The hex data (@code txParams.data) of a transaction
|
|
|
|
* @returns {string} - The four-byte method signature
|
|
|
|
*/
|
|
|
|
export function getFourBytePrefix (data = '') {
|
|
|
|
const prefixedData = ethUtil.addHexPrefix(data)
|
|
|
|
const fourBytePrefix = prefixedData.slice(0, 10)
|
|
|
|
return fourBytePrefix
|
|
|
|
}
|
|
|
|
|
2019-06-28 05:53:12 +02:00
|
|
|
/**
|
|
|
|
* Given an transaction category, returns a boolean which indicates whether the transaction is calling an erc20 token method
|
|
|
|
*
|
|
|
|
* @param {string} transactionCategory - The category of transaction being evaluated
|
|
|
|
* @returns {boolean} - whether the transaction is calling an erc20 token method
|
|
|
|
*/
|
|
|
|
export function isTokenMethodAction (transactionCategory) {
|
|
|
|
return [
|
|
|
|
TOKEN_METHOD_TRANSFER,
|
|
|
|
TOKEN_METHOD_APPROVE,
|
|
|
|
TOKEN_METHOD_TRANSFER_FROM,
|
|
|
|
].includes(transactionCategory)
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:02:55 +02:00
|
|
|
/**
|
|
|
|
* Returns the action of a transaction as a key to be passed into the translator.
|
|
|
|
* @param {Object} transaction - txData object
|
|
|
|
* @returns {string|undefined}
|
|
|
|
*/
|
2019-06-18 14:17:14 +02:00
|
|
|
export function getTransactionActionKey (transaction) {
|
|
|
|
const { msgParams, type, transactionCategory } = transaction
|
2018-09-17 19:34:29 +02:00
|
|
|
|
2019-08-16 20:54:10 +02:00
|
|
|
if (transactionCategory === 'incoming') {
|
|
|
|
return DEPOSIT_TRANSACTION_KEY
|
|
|
|
}
|
|
|
|
|
2018-09-17 19:34:29 +02:00
|
|
|
if (type === 'cancel') {
|
|
|
|
return CANCEL_ATTEMPT_ACTION_KEY
|
|
|
|
}
|
2018-08-07 07:39:54 +02:00
|
|
|
|
|
|
|
if (msgParams) {
|
2020-06-04 21:22:45 +02:00
|
|
|
if (type === MESSAGE_TYPE.ETH_DECRYPT) {
|
2020-02-19 19:24:16 +01:00
|
|
|
return DECRYPT_REQUEST_KEY
|
2020-06-04 21:22:45 +02:00
|
|
|
} else if (type === MESSAGE_TYPE.ETH_GET_ENCRYPTION_PUBLIC_KEY) {
|
2020-02-19 19:24:16 +01:00
|
|
|
return ENCRYPTION_PUBLIC_KEY_REQUEST_KEY
|
|
|
|
} else {
|
|
|
|
return SIGNATURE_REQUEST_KEY
|
|
|
|
}
|
2018-08-07 07:39:54 +02:00
|
|
|
}
|
2018-07-31 07:03:20 +02:00
|
|
|
|
|
|
|
if (isConfirmDeployContract(transaction)) {
|
|
|
|
return DEPLOY_CONTRACT_ACTION_KEY
|
|
|
|
}
|
|
|
|
|
2019-06-28 05:53:12 +02:00
|
|
|
const isTokenAction = isTokenMethodAction(transactionCategory)
|
2019-06-18 14:17:14 +02:00
|
|
|
const isNonTokenSmartContract = transactionCategory === CONTRACT_INTERACTION_KEY
|
2018-08-07 10:57:46 +02:00
|
|
|
|
2019-06-18 14:17:14 +02:00
|
|
|
if (isTokenAction || isNonTokenSmartContract) {
|
|
|
|
switch (transactionCategory) {
|
2018-07-31 07:03:20 +02:00
|
|
|
case TOKEN_METHOD_TRANSFER:
|
|
|
|
return SEND_TOKEN_ACTION_KEY
|
|
|
|
case TOKEN_METHOD_APPROVE:
|
|
|
|
return APPROVE_ACTION_KEY
|
|
|
|
case TOKEN_METHOD_TRANSFER_FROM:
|
|
|
|
return TRANSFER_FROM_ACTION_KEY
|
2019-06-18 14:17:14 +02:00
|
|
|
case CONTRACT_INTERACTION_KEY:
|
|
|
|
return CONTRACT_INTERACTION_KEY
|
2018-10-05 22:02:55 +02:00
|
|
|
default:
|
|
|
|
return undefined
|
2018-07-31 07:03:20 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return SEND_ETHER_ACTION_KEY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 06:12:30 +02:00
|
|
|
export function getLatestSubmittedTxWithNonce (transactions = [], nonce = '0x0') {
|
2018-08-03 05:20:15 +02:00
|
|
|
if (!transactions.length) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return transactions.reduce((acc, current) => {
|
2018-08-12 06:12:30 +02:00
|
|
|
const { submittedTime, txParams: { nonce: currentNonce } = {} } = current
|
2018-08-03 05:20:15 +02:00
|
|
|
|
2018-08-12 06:12:30 +02:00
|
|
|
if (currentNonce === nonce) {
|
|
|
|
return acc.submittedTime
|
|
|
|
? submittedTime > acc.submittedTime ? current : acc
|
|
|
|
: current
|
2018-08-03 05:20:15 +02:00
|
|
|
} else {
|
|
|
|
return acc
|
|
|
|
}
|
2018-08-12 06:12:30 +02:00
|
|
|
}, {})
|
2018-08-03 05:20:15 +02:00
|
|
|
}
|
2018-08-24 01:44:38 +02:00
|
|
|
|
|
|
|
export async function isSmartContractAddress (address) {
|
|
|
|
const code = await global.eth.getCode(address)
|
2018-10-21 06:52:41 +02:00
|
|
|
// Geth will return '0x', and ganache-core v2.2.1 will return '0x0'
|
|
|
|
const codeIsEmpty = !code || code === '0x' || code === '0x0'
|
|
|
|
return !codeIsEmpty
|
2018-08-24 01:44:38 +02:00
|
|
|
}
|
2018-08-31 21:36:07 +02:00
|
|
|
|
2018-09-07 22:59:05 +02:00
|
|
|
export function sumHexes (...args) {
|
2018-08-31 21:36:07 +02:00
|
|
|
const total = args.reduce((acc, base) => {
|
|
|
|
return addCurrencies(acc, base, {
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return ethUtil.addHexPrefix(total)
|
|
|
|
}
|
2018-10-16 00:00:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a status key for a transaction. Requires parsing the txMeta.txReceipt on top of
|
|
|
|
* txMeta.status because txMeta.status does not reflect on-chain errors.
|
|
|
|
* @param {Object} transaction - The txMeta object of a transaction.
|
|
|
|
* @param {Object} transaction.txReceipt - The transaction receipt.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export function getStatusKey (transaction) {
|
2018-12-09 21:48:06 +01:00
|
|
|
const { txReceipt: { status: receiptStatus } = {}, type, status } = transaction
|
2018-10-16 00:00:47 +02:00
|
|
|
|
|
|
|
// There was an on-chain failure
|
2018-12-09 21:48:06 +01:00
|
|
|
if (receiptStatus === '0x0') {
|
2018-10-16 00:00:47 +02:00
|
|
|
return 'failed'
|
|
|
|
}
|
|
|
|
|
2018-12-09 21:48:06 +01:00
|
|
|
if (status === TRANSACTION_STATUS_CONFIRMED && type === TRANSACTION_TYPE_CANCEL) {
|
|
|
|
return 'cancelled'
|
|
|
|
}
|
|
|
|
|
2018-10-16 00:00:47 +02:00
|
|
|
return transaction.status
|
|
|
|
}
|
2019-05-09 19:27:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an external block explorer URL at which a transaction can be viewed.
|
|
|
|
* @param {number} networkId
|
|
|
|
* @param {string} hash
|
|
|
|
* @param {Object} rpcPrefs
|
|
|
|
*/
|
|
|
|
export function getBlockExplorerUrlForTx (networkId, hash, rpcPrefs = {}) {
|
|
|
|
if (rpcPrefs.blockExplorerUrl) {
|
2020-05-14 04:09:40 +02:00
|
|
|
return `${rpcPrefs.blockExplorerUrl.replace(/\/+$/, '')}/tx/${hash}`
|
2019-05-09 19:27:14 +02:00
|
|
|
}
|
2020-07-08 23:05:09 +02:00
|
|
|
const prefix = getEtherscanNetworkPrefix(networkId)
|
2019-05-09 19:27:14 +02:00
|
|
|
return `https://${prefix}etherscan.io/tx/${hash}`
|
|
|
|
}
|