mirror of
https://github.com/status-im/status-desktop.git
synced 2025-02-10 21:56:37 +00:00
Add gas estimate for sticker pack purchase. Update transaction for sticker pack purchase. Add GasValidator component which validates gas is selected correctly and displays an error message if not. This component is not visible until it is not valid (at which point the valdiation error message is displayed). In a future PR, need to: 1. estimate gas for token txfer (sendTransaction) via a normalised method for estimating gas for EthSend 2. move sticker pack purchase to use an EthSend object so gas can be estimated and tx sent
115 lines
4.4 KiB
QML
115 lines
4.4 KiB
QML
pragma Singleton
|
|
|
|
import QtQuick 2.13
|
|
import "../shared/xss.js" as XSS
|
|
|
|
QtObject {
|
|
function isHex(value) {
|
|
return /^(-0x|0x)?[0-9a-fA-F]*$/i.test(value)
|
|
}
|
|
|
|
function startsWith0x(value) {
|
|
return value.startsWith('0x')
|
|
}
|
|
|
|
function isChatKey(value) {
|
|
return startsWith0x(value) && isHex(value) && value.length === 132
|
|
}
|
|
|
|
function isValidETHNamePrefix(value) {
|
|
return !(value.trim() === "" || value.endsWith(".") || value.indexOf("..") > -1)
|
|
}
|
|
|
|
function isAddress(value) {
|
|
return startsWith0x(value) && isHex(value) && value.length === 42
|
|
}
|
|
|
|
function isPrivateKey(value) {
|
|
return isHex(value) && ((startsWith0x(value) && value.length === 66) ||
|
|
(!startsWith0x(value) && value.length === 64))
|
|
}
|
|
|
|
function isMnemonic(value) {
|
|
if(!value.match(/^([a-z\s]+)$/)){
|
|
return false;
|
|
}
|
|
var len = value.split(/\s|,/).length;
|
|
return len >= 12 && len <= 24 && len % 3 == 0;
|
|
}
|
|
|
|
function compactAddress(addr, numberOfChars) {
|
|
if(addr.length <= 5 + (numberOfChars * 2)){ // 5 represents these chars 0x...
|
|
return addr;
|
|
}
|
|
return addr.substring(0, 2 + numberOfChars) + "..." + addr.substring(addr.length - numberOfChars);
|
|
}
|
|
|
|
function linkifyAndXSS(inputText) {
|
|
//URLs starting with http://, https://, or ftp://
|
|
var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
|
|
var replacedText = inputText.replace(replacePattern1, "<a href='$1'>$1</a>");
|
|
|
|
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
|
|
var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
|
|
replacedText = replacedText.replace(replacePattern2, "$1<a href='http://$2'>$2</a>");
|
|
|
|
replacedText = XSS.filterXSS(replacedText)
|
|
return replacedText;
|
|
}
|
|
|
|
function isOnlyEmoji(inputText) {
|
|
var emoji_regex = /^(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c[\ude32-\ude3a]|[\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff]|\s)+$/;
|
|
return emoji_regex.test(inputText);
|
|
}
|
|
|
|
function removeStatusEns(userName){
|
|
return userName.endsWith(".stateofus.eth") ? userName.substr(0, userName.length - 14) : userName
|
|
}
|
|
|
|
function isValidAddress(inputValue) {
|
|
return /^0x[a-fA-F0-9]{40}$/.test(inputValue)
|
|
}
|
|
|
|
/**
|
|
* Removes trailing zeros from a string-representation of a number. Throws
|
|
* if parameter is not a string
|
|
*/
|
|
function stripTrailingZeros(strNumber) {
|
|
if (!(typeof strNumber === "string")) {
|
|
try {
|
|
strNumber = strNumber.toString()
|
|
} catch(e) {
|
|
throw "[Utils.stripTrailingZeros] input parameter must be a string"
|
|
}
|
|
}
|
|
return strNumber.replace(/(\.[0-9]*[1-9])0+$|\.0*$/,'$1')
|
|
}
|
|
|
|
function setColorAlpha(color, alpha) {
|
|
return Qt.hsla(color.hslHue, color.hslSaturation, color.hslLightness, alpha)
|
|
}
|
|
|
|
function formatTime(timestamp) {
|
|
let messageDate = new Date(Math.floor(timestamp))
|
|
let minutes = messageDate.getMinutes();
|
|
let hours = messageDate.getHours();
|
|
return (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes)
|
|
}
|
|
|
|
function findAssetBySymbol(assets, symbolToFind) {
|
|
for(var i=0; i<assets.rowCount(); i++) {
|
|
const symbol = assets.rowData(i, "symbol")
|
|
if (symbol.toLowerCase() === symbolToFind.toLowerCase()) {
|
|
return {
|
|
name: assets.rowData(i, "name"),
|
|
symbol,
|
|
value: assets.rowData(i, "value"),
|
|
fiatBalanceDisplay: assets.rowData(i, "fiatBalanceDisplay"),
|
|
address: assets.rowData(i, "address"),
|
|
fiatBalance: assets.rowData(i, "fiatBalance")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|