Skip to content

Commit

Permalink
fix: browser connect - send transaction command
Browse files Browse the repository at this point in the history
Fixes #16063
  • Loading branch information
saledjenic committed Sep 20, 2024
1 parent a745f09 commit fa41043
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 17 deletions.
166 changes: 150 additions & 16 deletions ui/app/AppLayouts/Wallet/services/dapps/DappsConnectorSDK.qml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ WalletConnectSDKBase {
console.error("Error in event data lookup", JSON.stringify(event))
return null
}
const interpreted = d.prepareData(method, data)
let enoughFunds = !isTransactionMethod(method)
let obj = sessionRequestComponent.createObject(null, {
event,
Expand All @@ -88,12 +89,12 @@ WalletConnectSDKBase {
accountAddress,
chainId,
data,
preparedData: interpreted.preparedData,
maxFeesText: "?",
maxFeesEthText: "?",
enoughFunds: enoughFunds,
estimatedTimeText: "?",
preparedData: event.params.request.tx.data,
})

if (obj === null) {
console.error("Error creating SessionRequestResolved for event")
return null
Expand All @@ -113,9 +114,134 @@ WalletConnectSDKBase {
}
obj.resolveDappInfoFromSession(session)

if (d.isTransactionMethod(method)) {
let tx = obj.data.tx
if (tx === null) {
console.error("Error cannot resolve tx object")
return null
}
let BigOps = SQUtils.AmountsArithmetic
let gasLimit = hexToGwei(tx.gasLimit)

if (tx.gasPrice === null || tx.gasPrice === undefined) {
let maxFeePerGas = hexToGwei(tx.maxFeePerGas)
let maxPriorityFeePerGas = hexToGwei(tx.maxPriorityFeePerGas)
let totalMaxFees = BigOps.sum(maxFeePerGas, maxPriorityFeePerGas)
let maxFees = BigOps.times(gasLimit, totalMaxFees)
let maxFeesString = maxFees.toString()
obj.maxFeesText = maxFeesString
obj.maxFeesEthText = maxFeesString
obj.enoughFunds = true
} else {
let gasPrice = hexToGwei(tx.gasPrice)
let maxFees = BigOps.times(gasLimit, gasPrice)
let maxFeesString = maxFees.toString()
obj.maxFeesText = maxFeesString
obj.maxFeesEthText = maxFeesString
obj.enoughFunds = true
}
}

return obj
}

function getTxObject(method, data) {
let tx
if (method === SessionRequest.methods.signTransaction.name) {
tx = SessionRequest.methods.signTransaction.getTxObjFromData(data)
} else if (method === SessionRequest.methods.sendTransaction.name) {
tx = SessionRequest.methods.sendTransaction.getTxObjFromData(data)
} else {
console.error("Not a transaction method")
}
return tx
}

// returns {
// preparedData,
// value // null or ETH Big number
// }
function prepareData(method, data) {
let payload = null
switch(method) {
case SessionRequest.methods.personalSign.name: {
payload = SessionRequest.methods.personalSign.getMessageFromData(data)
break
}
case SessionRequest.methods.sign.name: {
payload = SessionRequest.methods.sign.getMessageFromData(data)
break
}
case SessionRequest.methods.signTypedData_v4.name: {
const stringPayload = SessionRequest.methods.signTypedData_v4.getMessageFromData(data)
payload = JSON.stringify(JSON.parse(stringPayload), null, 2)
break
}
case SessionRequest.methods.signTypedData.name: {
const stringPayload = SessionRequest.methods.signTypedData.getMessageFromData(data)
payload = JSON.stringify(JSON.parse(stringPayload), null, 2)
break
}
case SessionRequest.methods.signTransaction.name:
case SessionRequest.methods.sendTransaction.name:
// For transactions we process the data in a different way as follows
break
default:
console.error("Unhandled method", method)
break;
}

let value = SQUtils.AmountsArithmetic.fromNumber(0)
if (d.isTransactionMethod(method)) {
let txObj = d.getTxObject(method, data)
let tx = Object.assign({}, txObj)
if (tx.value) {
value = hexToEth(tx.value)
tx.value = value.toString()
}
if (tx.maxFeePerGas) {
tx.maxFeePerGas = hexToGwei(tx.maxFeePerGas).toString()
}
if (tx.maxPriorityFeePerGas) {
tx.maxPriorityFeePerGas = hexToGwei(tx.maxPriorityFeePerGas).toString()
}
if (tx.gasPrice) {
tx.gasPrice = hexToGwei(tx.gasPrice)
}
if (tx.gasLimit) {
tx.gasLimit = parseInt(root.store.hexToDec(tx.gasLimit))
}
if (tx.nonce) {
tx.nonce = parseInt(root.store.hexToDec(tx.nonce))
}

payload = JSON.stringify(tx, null, 2)
}
return {
preparedData: payload,
value: value
}
}

function hexToEth(value) {
return hexToEthDenomination(value, "eth")
}
function hexToGwei(value) {
return hexToEthDenomination(value, "gwei")
}
function hexToEthDenomination(value, ethUnit) {
let unitMapping = {
"gwei": 9,
"eth": 18
}
let BigOps = SQUtils.AmountsArithmetic
let decValue = root.store.hexToDec(value)
if (!!decValue) {
return BigOps.div(BigOps.fromNumber(decValue), BigOps.fromNumber(1, unitMapping[ethUnit]))
}
return BigOps.fromNumber(0)
}

function isTransactionMethod(method) {
return method === SessionRequest.methods.signTransaction.name
|| method === SessionRequest.methods.sendTransaction.name
Expand All @@ -128,7 +254,7 @@ WalletConnectSDKBase {
if (event.params.request.params.length < 2) {
return address
}
address = event.params.request.params[0]
address = event.params.request.params[1]
} else if (method === SessionRequest.methods.sign.name) {
if (event.params.request.params.length === 1) {
return address
Expand All @@ -146,7 +272,10 @@ WalletConnectSDKBase {
if (event.params.request.params.length == 0) {
return address
}
address = event.params.request.params[0]
address = event.params.request.params[0].from
} else {
console.error("Unsupported method to lookup account: ", method)
return null
}
const account = SQUtils.ModelUtils.getFirstModelEntryIf(root.wcService.validAccounts, (account) => {
return account.address.toLowerCase() === address.toLowerCase();
Expand Down Expand Up @@ -213,7 +342,7 @@ WalletConnectSDKBase {
if (event.params.request.params.length == 0) {
return null
}
let tx = event.params.request.params[0]
const tx = event.params.request.params[0]
return SessionRequest.methods.sendTransaction.buildDataObject(tx)
} else {
return null
Expand Down Expand Up @@ -267,12 +396,11 @@ WalletConnectSDKBase {
password,
pin)
} else if (request.method === SessionRequest.methods.sendTransaction.name) {
let txObj = SessionRequest.methods.sendTransaction.getTxObjFromData(request.data)
store.sendTransaction(request.topic,
request.id,
request.account.address,
request.chainId,
txObj,
request.network.chainId,
request.data.tx,
password,
pin)
}
Expand Down Expand Up @@ -478,7 +606,7 @@ WalletConnectSDKBase {
}
case SessionRequest.methods.sendTransaction.name: {
const jsonPayload = SessionRequest.methods.sendTransaction.getTxObjFromData(request.data)
return JSON.stringify(jsonPayload, null, 2)
return JSON.stringify(request.data, null, 2)
}
}
}
Expand Down Expand Up @@ -560,14 +688,20 @@ WalletConnectSDKBase {
"params": {
"chainId": `eip155:${dappInfo.chainId}`,
"request": {
"method": SessionRequest.methods.personalSign.name,
"tx": {
"data": txArgsParams.data,
},
"method": SessionRequest.methods.sendTransaction.name,
"params": [
txArgsParams.from,
txArgsParams.to,
],
{
"from": txArgsParams.from,
"to": txArgsParams.to,
"value": txArgsParams.value,
"gasLimit": txArgsParams.gas,
"gasPrice": txArgsParams.gasPrice,
"maxFeePerGas": txArgsParams.maxFeePerGas,
"maxPriorityFeePerGas": txArgsParams.maxPriorityFeePerGas,
"nonce": txArgsParams.nonce,
"data": txArgsParams.data
}
]
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/status-go
Submodule status-go updated 82 files
+14 −14 Makefile
+9 −5 contracts/mock/contracts.go
+0 −1 go.mod
+0 −1 go.sum
+0 −282 multiaccounts/settings/mocks/database_data_modification_settings.go
+0 −250 multiaccounts/settings/mocks/database_data_retriever_mock.go
+0 −236 multiaccounts/settings/mocks/database_info_retriever_mock.go
+729 −10 multiaccounts/settings/mocks/database_settings_manager_mock.go
+144 −53 rpc/chain/mock/client/client.go
+7 −0 rpc/client.go
+49 −2 rpc/mock/client/client.go
+8 −4 rpc/network/mock/network.go
+16 −5 services/connector/api.go
+5 −39 services/connector/api_test.go
+3 −7 services/connector/chainutils/utils.go
+2 −1 services/connector/commands/accounts.go
+10 −16 services/connector/commands/accounts_test.go
+4 −2 services/connector/commands/chain_id.go
+10 −36 services/connector/commands/chain_id_test.go
+2 −1 services/connector/commands/personal_sign.go
+23 −50 services/connector/commands/personal_sign_test.go
+2 −1 services/connector/commands/request_accounts.go
+16 −39 services/connector/commands/request_accounts_test.go
+2 −1 services/connector/commands/request_permissions.go
+6 −4 services/connector/commands/request_permissions_test.go
+2 −1 services/connector/commands/revoke_permissions.go
+11 −14 services/connector/commands/revoke_permissions_test.go
+14 −1 services/connector/commands/rpc_traits.go
+48 −1 services/connector/commands/send_transaction.go
+53 −50 services/connector/commands/send_transaction_test.go
+4 −2 services/connector/commands/switch_ethereum_chain.go
+13 −45 services/connector/commands/switch_ethereum_chain_test.go
+95 −22 services/connector/commands/test_helpers.go
+47 −66 services/connector/connector_flows_test.go
+5 −4 services/connector/service.go
+16 −42 services/connector/service_test.go
+98 −0 services/connector/test_helpers.go
+1 −1 services/ens/api_test.go
+1 −1 services/peer/api_test.go
+13 −8 services/peer/discoverer_mock.go
+1 −1 services/stickers/recent_test.go
+1 −1 services/wallet/activity/service_test.go
+1 −1 services/wallet/collectibles/manager_test.go
+11 −7 services/wallet/collectibles/mock/collectible_data_db.go
+11 −7 services/wallet/collectibles/mock/collection_data_db.go
+1 −1 services/wallet/history/service_test.go
+134 −47 services/wallet/market/market.go
+35 −0 services/wallet/market/market_cache.go
+116 −23 services/wallet/market/market_test.go
+1 −2 services/wallet/onramp/mock/types.go
+6 −6 services/wallet/reader.go
+1 −1 services/wallet/reader_test.go
+1 −1 services/wallet/router/fees/fees.go
+14 −10 services/wallet/router/pathprocessor/mock_pathprocessor/processor.go
+1 −1 services/wallet/router/pathprocessor/processor_swap_paraswap_test.go
+3 −2 services/wallet/router/sendtype/send_type.go
+1 −1 services/wallet/testutils/helpers.go
+20 −16 services/wallet/thirdparty/mock/collectible_types.go
+12 −8 services/wallet/thirdparty/mock/types.go
+12 −8 services/wallet/thirdparty/paraswap/mock/types.go
+1 −1 services/wallet/token/balancefetcher/balance_fetcher_test.go
+26 −22 services/wallet/token/mock/balance_persistence/balance_persistence.go
+15 −26 services/wallet/token/mock/token/tokenmanager.go
+1 −1 services/wallet/token/token_test.go
+1 −1 services/wallet/transfer/commands_sequential_test.go
+1 −1 services/wallet/transfer/downloader_test.go
+1 −1 services/wallet/transfer/transaction_manager_multitransaction_test.go
+1 −1 services/wallet/transfer/transaction_manager_test.go
+1 −1 services/web3provider/api_test.go
+30 −26 transactions/fake/mock.go
+1 −1 transactions/fake/txservice.go
+16 −12 transactions/mock_transactor/transactor.go
+1 −1 transactions/pendingtxtracker_test.go
+1 −1 transactions/transactor_test.go
+0 −12 vendor/github.com/golang/mock/AUTHORS
+0 −37 vendor/github.com/golang/mock/CONTRIBUTORS
+0 −202 vendor/github.com/golang/mock/LICENSE
+0 −445 vendor/github.com/golang/mock/gomock/call.go
+0 −113 vendor/github.com/golang/mock/gomock/callset.go
+0 −336 vendor/github.com/golang/mock/gomock/controller.go
+0 −341 vendor/github.com/golang/mock/gomock/matchers.go
+0 −3 vendor/modules.txt

0 comments on commit fa41043

Please sign in to comment.