mirror of
https://github.com/status-im/status-desktop.git
synced 2025-02-21 11:08:55 +00:00
Compute max fees for transaction related requests and display the results to user. Also: - Add helper to convert from hex (backend) to decimal (frontend) values. - Add helper to convert from float gwei to hex wei - Update tests to accommodate for the new dependencies. Sourcing of account balances is not included therefore the transaction is allowed to go through even if the account balance is insufficient. An error will be generated by the backend in this case. Updates: #15192
103 lines
3.3 KiB
JavaScript
103 lines
3.3 KiB
JavaScript
.import StatusQ.Core.Utils 0.1 as SQUtils
|
|
|
|
function chainIdFromEip155(chain) {
|
|
return parseInt(chain.split(':').pop().trim(), 10)
|
|
}
|
|
|
|
function isHex(str) {
|
|
return str.startsWith('0x') && str.length % 2 === 0 && /^[0-9a-fA-F]*$/.test(str.slice(2))
|
|
}
|
|
|
|
function hexToString(hex) {
|
|
if (hex.startsWith("0x")) {
|
|
hex = hex.substring(2);
|
|
}
|
|
|
|
var str = '';
|
|
for (var i = 0; i < hex.length; i += 2) {
|
|
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
|
|
}
|
|
return str;
|
|
}
|
|
|
|
function strToHex(str) {
|
|
var hex = '';
|
|
for (var i = 0; i < str.length; i++) {
|
|
var byte = str.charCodeAt(i).toString(16);
|
|
hex += (byte.length < 2 ? '0' : '') + byte;
|
|
}
|
|
return '0x' + hex;
|
|
}
|
|
|
|
function extractChainsAndAccountsFromApprovedNamespaces(approvedNamespaces) {
|
|
const eip155Data = approvedNamespaces.eip155;
|
|
const chains = eip155Data.chains.map(chainIdFromEip155);
|
|
const accountSet = new Set(
|
|
eip155Data.accounts.map(account => account.split(':').pop().trim())
|
|
);
|
|
const uniqueAccounts = Array.from(accountSet);
|
|
return { chains, accounts: uniqueAccounts };
|
|
}
|
|
|
|
function buildSupportedNamespacesFromModels(chainsModel, accountsModel, methods) {
|
|
var chainIds = []
|
|
var addresses = []
|
|
for (let i = 0; i < chainsModel.count; i++) {
|
|
let entry = SQUtils.ModelUtils.get(chainsModel, i)
|
|
chainIds.push(parseInt(entry.chainId))
|
|
}
|
|
for (let i = 0; i < accountsModel.count; i++) {
|
|
let entry = SQUtils.ModelUtils.get(accountsModel, i)
|
|
addresses.push(entry.address)
|
|
}
|
|
return buildSupportedNamespaces(chainIds, addresses, methods)
|
|
}
|
|
|
|
function buildSupportedNamespaces(chainIds, addresses, methods) {
|
|
var eipChainIds = []
|
|
var eipAddresses = []
|
|
for (let i = 0; i < chainIds.length; i++) {
|
|
let chainId = chainIds[i]
|
|
eipChainIds.push(`"eip155:${chainId}"`)
|
|
for (let i = 0; i < addresses.length; i++) {
|
|
eipAddresses.push(`"eip155:${chainId}:${addresses[i]}"`)
|
|
}
|
|
}
|
|
let methodsStr = methods.map(method => `"${method}"`).join(',')
|
|
return `{
|
|
"eip155":{"chains": [${eipChainIds.join(',')}],"methods": [${methodsStr}],"events": ["accountsChanged", "chainChanged"],"accounts": [${eipAddresses.join(',')}]}}`
|
|
}
|
|
|
|
function validURI(uri) {
|
|
var regex = /^wc:[0-9a-fA-F-]*@([1-9][0-9]*)(\?([a-zA-Z-]+=[^&]+)(&[a-zA-Z-]+=[^&]+)*)?$/
|
|
return regex.test(uri)
|
|
}
|
|
|
|
function containsOnlyEmoji(uri) {
|
|
var emojiRegex = new RegExp("[\\u203C-\\u3299\\u1F000-\\u1F644]");
|
|
return !emojiRegex.test(uri);
|
|
}
|
|
|
|
function extractInfoFromPairUri(uri) {
|
|
var topic = ""
|
|
var expiry = NaN
|
|
// Extract topic and expiry from wc:99fdcac5cc081ac8c1181b4c38c5dc49fb5eb212706d5c94c445be549765e7f0@2?expiryTimestamp=1720090818&relay-protocol=irn&symKey=c6b67d94174bd42d16ff288220ce9b8966e5b56a2d3570a30d5b0a760f1953f0
|
|
const regex = /wc:([0-9a-fA-F]*)/
|
|
const match = uri.match(regex)
|
|
if (match) {
|
|
topic = match[1]
|
|
}
|
|
|
|
var parts = uri.split('?')
|
|
if (parts.length > 1) {
|
|
var params = parts[1].split('&')
|
|
for (let i = 0; i < params.length; i++) {
|
|
var keyVal = params[i].split('=')
|
|
if (keyVal[0] === 'expiryTimestamp') {
|
|
expiry = parseInt(keyVal[1])
|
|
}
|
|
}
|
|
}
|
|
return { topic, expiry }
|
|
}
|