Skip to content

Commit

Permalink
use checksum for from address (#1432)
Browse files Browse the repository at this point in the history
* use checksum for from address

* test cleanup
  • Loading branch information
mholtzman authored Feb 17, 2023
1 parent d908625 commit 19e1598
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
17 changes: 6 additions & 11 deletions main/provider/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const permission = (date: number, method: string) => ({ parentCapability: method
export function checkExistingNonceGas(tx: TransactionData) {
const { from, nonce } = tx

const reqs = store('main.accounts', from, 'requests')
const reqs = store('main.accounts', (from || '').toLowerCase(), 'requests')

const requests = Object.keys(reqs || {}).map((key) => reqs[key])
const existing = requests.filter(
(r) => r.mode === 'monitor' && r.status !== 'error' && r.data.nonce === nonce
Expand Down Expand Up @@ -75,11 +76,8 @@ function parseValue(value = '') {
return (!!parsedHex && addHexPrefix(unpadHexString(value))) || '0x0'
}

export function getRawTx(
newTx: RPC.SendTransaction.TxParams,
accountId: string | undefined
): TransactionData {
const { gas, gasLimit, data, value, type, to, ...rawTx } = newTx
export function getRawTx(newTx: RPC.SendTransaction.TxParams): TransactionData {
const { gas, gasLimit, data, value, type, from, to, ...rawTx } = newTx
const getNonce = () => {
// pass through hex string or undefined
if (rawTx.nonce === undefined || isHexString(rawTx.nonce)) {
Expand All @@ -96,7 +94,8 @@ export function getRawTx(

const tx: TransactionData = {
...rawTx,
from: rawTx.from || accountId,
...(from && { from: getAddress(from) }),
...(to && { to: getAddress(to) }),
type: '0x0',
value: parseValue(value),
data: addHexPrefix(padToEven(stripHexPrefix(data || '0x'))),
Expand All @@ -106,10 +105,6 @@ export function getRawTx(
gasFeesSource: GasFeesSource.Dapp
}

if (to) {
tx.to = getAddress(to)
}

return tx
}

Expand Down
20 changes: 9 additions & 11 deletions main/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,7 @@ export class Provider extends EventEmitter {

try {
const approvals: RequiredApproval[] = []
const accountId = (accounts.current() || {}).id
const rawTx = getRawTx(newTx, accountId)
const rawTx = getRawTx(newTx)
const gas = gasFees(rawTx)
const { chainConfig } = connection

Expand Down Expand Up @@ -520,19 +519,18 @@ export class Provider extends EventEmitter {

log.verbose(`sendTransaction(${JSON.stringify(tx)}`)

this.fillTransaction(tx, (err, transactionMetadata) => {
const from = tx.from || (currentAccount && currentAccount.id)

if (!currentAccount || !from || !hasAddress(currentAccount, from)) {
return resError('Transaction is not from currently selected account', payload, res)
}

this.fillTransaction({ ...tx, from }, (err, transactionMetadata) => {
if (err) {
resError(err, payload, res)
} else {
const txMetadata = transactionMetadata as TransactionMetadata
const from = txMetadata.tx.from

if (!currentAccount || !hasAddress(currentAccount, from)) {
return resError('Transaction is not from currently selected account', payload, res)
}

const handlerId = this.addRequestHandler(res)

const txMetadata = transactionMetadata as TransactionMetadata
const { feesUpdated, recipientType, ...data } = txMetadata.tx

const unclassifiedReq = {
Expand Down
4 changes: 1 addition & 3 deletions test/main/provider/helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import log from 'electron-log'
import { fromUtf8 } from '@ethereumjs/util'
import { getRawTx, getSignedAddress, processTxForGasFees } from '../../../main/provider/helpers'
import store from '../../../main/store'
import { GasFeesSource } from '../../../resources/domain/transaction'
import { getRawTx, getSignedAddress } from '../../../main/provider/helpers'

jest.mock('../../../main/store')

Expand Down

0 comments on commit 19e1598

Please sign in to comment.