diff --git a/js/TxUtils.js b/js/TxUtils.js index 49b0c34e..28f25ef6 100644 --- a/js/TxUtils.js +++ b/js/TxUtils.js @@ -76,3 +76,34 @@ function isValidOpenAliasAddress(address) { // make sure it is not some kind of floating number return address.length > 2 && isNaN(parseFloat(address)) && address.indexOf('.') >= 0 } + +function handleOpenAliasResolution(address, descriptionText) { + const result = walletManager.resolveOpenAlias(address); + if (!result) { + return { message: qsTr("No address found") }; + } + + const [isDnssecValid, resolvedAddress] = result.split("|"); + const isAddressValid = walletManager.addressValid(resolvedAddress, appWindow.persistentSettings.nettype); + let updatedDescriptionText = descriptionText; + + if (isDnssecValid === "true") { + if (isAddressValid) { + updatedDescriptionText = descriptionText ? `${address} ${descriptionText}` : address; + return { address: resolvedAddress, description: updatedDescriptionText }; + } else { + return { message: qsTr("No valid address found at this OpenAlias address") }; + } + } else if (isDnssecValid === "false") { + if (isAddressValid) { + return { + address: resolvedAddress, + message: qsTr("Address found, but the DNSSEC signatures could not be verified, so this address may be spoofed"), + }; + } else { + return { message: qsTr("No valid address found at this OpenAlias address, but the DNSSEC signatures could not be verified, so this may be spoofed") }; + } + } else { + return { message: qsTr("Internal error") }; + } +} diff --git a/pages/AddressBook.qml b/pages/AddressBook.qml index 27821e4c..f0fd0f87 100644 --- a/pages/AddressBook.qml +++ b/pages/AddressBook.qml @@ -387,34 +387,17 @@ Rectangle { visible: TxUtils.isValidOpenAliasAddress(addressLine.text) enabled : visible onClicked: { - var result = walletManager.resolveOpenAlias(addressLine.text) - if (result) { - var parts = result.split("|") - if (parts.length === 2) { - var address_ok = walletManager.addressValid(parts[1], appWindow.persistentSettings.nettype) - if (parts[0] === "true") { - if (address_ok) { - // prepend openalias to description - descriptionLine.text = descriptionLine.text ? addressLine.text + " " + descriptionLine.text : addressLine.text - addressLine.text = parts[1] - } else { - root.oa_message(qsTr("No valid address found at this OpenAlias address")) - } - } else if (parts[0] === "false") { - if (address_ok) { - addressLine.text = parts[1] - root.oa_message(qsTr("Address found, but the DNSSEC signatures could not be verified, so this address may be spoofed")) - } else { - root.oa_message(qsTr("No valid address found at this OpenAlias address, but the DNSSEC signatures could not be verified, so this may be spoofed")) - } - } else { - root.oa_message(qsTr("Internal error")) - } - } else { - root.oa_message(qsTr("Internal error")) + const response = TxUtils.handleOpenAliasResolution(addressLine.text, descriptionLine.text); + if (response) { + if (response.message) { + oa_message(response.message); + } + if (response.address) { + addressLine.text = response.address; + } + if (response.description) { + descriptionLine.text = response.description; } - } else { - root.oa_message(qsTr("No address found")) } } } diff --git a/pages/Transfer.qml b/pages/Transfer.qml index 5719c744..8f2ce16c 100644 --- a/pages/Transfer.qml +++ b/pages/Transfer.qml @@ -415,41 +415,18 @@ Rectangle { text: qsTr("Resolve") + translationManager.emptyString visible: TxUtils.isValidOpenAliasAddress(address) onClicked: { - var result = walletManager.resolveOpenAlias(address) - if (result) { - var parts = result.split("|") - if (parts.length == 2) { - var address_ok = walletManager.addressValid(parts[1], appWindow.persistentSettings.nettype) - if (parts[0] === "true") { - if (address_ok) { - // prepend openalias to description - descriptionLine.text = descriptionLine.text ? address + " " + descriptionLine.text : address - descriptionCheckbox.checked = true - recipientRepeater.itemAt(index).children[1].children[0].text = parts[1]; - } - else - oa_message(qsTr("No valid address found at this OpenAlias address")) - } - else if (parts[0] === "false") { - if (address_ok) { - recipientRepeater.itemAt(index).children[1].children[0].text = parts[1]; - oa_message(qsTr("Address found, but the DNSSEC signatures could not be verified, so this address may be spoofed")) - } - else - { - oa_message(qsTr("No valid address found at this OpenAlias address, but the DNSSEC signatures could not be verified, so this may be spoofed")) - } - } - else { - oa_message(qsTr("Internal error")) - } + const response = TxUtils.handleOpenAliasResolution(address, descriptionLine.text); + if (response) { + if (response.message) { + oa_message(response.message); } - else { - oa_message(qsTr("Internal error")) + if (response.address) { + recipientRepeater.itemAt(index).children[1].children[0].text = response.address; + } + if (response.description) { + descriptionLine.text = response.description; + descriptionCheckbox.checked = true; } - } - else { - oa_message(qsTr("No address found")) } } }