XEC Airdrop Calculator
diff --git a/cashtab/src/components/Alias/Alias.js b/cashtab/src/components/Alias/Alias.js
--- a/cashtab/src/components/Alias/Alias.js
+++ b/cashtab/src/components/Alias/Alias.js
@@ -8,8 +8,12 @@
SidePaddingCtn,
WalletInfoCtn,
} from 'components/Common/Atoms';
-import { DestinationAddressSingle } from 'components/Common/EnhancedInputs';
-import { AntdFormWrapper } from 'components/Common/EnhancedInputs';
+import {
+ AntdFormWrapper,
+ AliasInput,
+ AliasAddressInput,
+ CashtabCheckbox,
+} from 'components/Common/EnhancedInputs';
import { Form, Modal } from 'antd';
import { SmartButton } from 'components/Common/PrimaryButton';
import BalanceHeader from 'components/Common/BalanceHeader';
@@ -32,6 +36,12 @@
} from 'components/Common/Notifications';
import { isAliasFormat, isValidAliasString } from 'utils/validation';
import { getPendingAliases } from 'utils/aliasUtils';
+import cashaddr from 'ecashaddrjs';
+
+export const CheckboxContainer = styled.div`
+ text-align: left;
+ margin-bottom: 12px;
+`;
export const NamespaceCtn = styled.div`
width: 100%;
@@ -73,8 +83,13 @@
aliasName: '',
aliasAddress: '',
});
+ const [useThisAddressChecked, setUseThisAddressChecked] = useState(false);
const [isValidAliasInput, setIsValidAliasInput] = useState(false); // tracks whether to activate the registration button
+ const [isValidAliasAddressInput, setIsValidAliasAddressInput] =
+ useState(false); // tracks whether to activate the registration button
const [aliasValidationError, setAliasValidationError] = useState(false);
+ const [aliasAddressValidationError, setAliasAddressValidationError] =
+ useState(false);
const [activeWalletAliases, setActiveWalletAliases] = useState([]); // stores the list of aliases registered to this active wallet
const [aliasLength, setAliasLength] = useState(false); // real time tracking of alias char length
const [aliasFee, setAliasFee] = useState(false); // real time tracking of alias registration fee
@@ -98,12 +113,10 @@
}
passLoadingStatus(true);
- // Set address of active wallet to default alias registration address
- // Use formdata approach as we will later add a form field for aliasAddress
- setFormData(formData => ({
- ...formData,
- aliasAddress: wallet.Path1899.cashAddress,
- }));
+ // Default to registering the user's active wallet
+ // Must be called in this useEffect to ensure that wallet is loaded
+ // Call with this function to ensure that checkbox state and checkbox are updated
+ handleDefaultAddressCheckboxChange({ target: { checked: true } });
// check whether the address is attached to an onchain alias on page load
const walletHasAlias = isAddressRegistered(
@@ -287,6 +300,82 @@
}));
};
+ const handleDefaultAddressCheckboxChange = e => {
+ /* handleDefaultAddressCheckboxChange
+ *
+ * Function to handle user action of checking or unchecking the
+ * checkbox on this page labeled 'Register active wallet address'
+ *
+ * May be called programmatically by mocking the usual js event
+ * of a user checking the box
+ *
+ * If the box is checked, set formData for aliasAddress to the active wallet's address
+ * If the box is unchecked, clear formData for aliasAddress
+ */
+ const checked = e.target.checked;
+ setUseThisAddressChecked(checked);
+ if (checked) {
+ // Set address of active wallet to default alias registration address
+ handleAliasAddressInput({
+ target: {
+ name: 'aliasAddress',
+ value: wallet.Path1899.cashAddress,
+ },
+ });
+ } else {
+ // Clear the form if the user unchecks
+ handleAliasAddressInput({
+ target: {
+ name: 'aliasAddress',
+ value: '',
+ },
+ });
+ }
+ };
+
+ const handleAliasAddressInput = e => {
+ /* handleAliasAddressInput
+ *
+ * Function called to handle any changes to the aliasAddress input form
+ *
+ * May be called programmatically by mocking the usual js event
+ * of a user updating the addressName input field
+ */
+ let { name, value } = e.target;
+
+ // remove any whitespaces
+ value = value.trim();
+
+ // Validate
+ let decoded;
+ let isValidAddress = false;
+ try {
+ decoded = cashaddr.decode(value, true);
+ const { hash } = decoded;
+ // We only support 20-byte payloads
+ isValidAddress = hash.length === 40;
+ } catch (err) {
+ // Invalid cashaddress
+ // Log to console for user support
+ console.log(`Invalid address`, err);
+ }
+
+ if (isValidAddress) {
+ setIsValidAliasAddressInput(true);
+ setAliasAddressValidationError(false);
+ } else {
+ setAliasAddressValidationError(
+ 'Invalid alias registration address.',
+ );
+ setIsValidAliasAddressInput(false);
+ }
+
+ setFormData(p => ({
+ ...p,
+ [name]: value,
+ }));
+ };
+
function handleAliasRegistrationError(errorObj) {
// Set loading to false here as well, as balance may not change depending on where error occured in try loop
passLoadingStatus(false);
@@ -363,7 +452,7 @@
}}
>
-
+
+
+ Register active wallet address
+
+
+ {!useThisAddressChecked && (
+
+ handleAliasAddressInput(
+ e,
+ ),
+ required: true,
+ }}
+ />
+ )}
{aliasLength &&
aliasFee &&
`Registration fee for this ${aliasLength} byte Alias is ${fromSatoshisToXec(
@@ -390,7 +516,10 @@
setIsModalVisible(true)
}
diff --git a/cashtab/src/components/Alias/__tests__/__snapshots__/Alias.test.js.snap b/cashtab/src/components/Alias/__tests__/__snapshots__/Alias.test.js.snap
--- a/cashtab/src/components/Alias/__tests__/__snapshots__/Alias.test.js.snap
+++ b/cashtab/src/components/Alias/__tests__/__snapshots__/Alias.test.js.snap
@@ -1,15 +1,15 @@
-// Jest Snapshot v1, https://goo.gl/fbAQLP
+// Jest Snapshot v1, https://goo.gl/fbAQLP @generated
exports[`Wallet with XEC balances 1`] = `
Array [
MigrationTestAlpha
@@ -51,7 +51,7 @@
You currently have 0
XEC
@@ -60,7 +60,7 @@
@@ -273,13 +290,13 @@
exports[`Wallet with XEC balances and tokens 1`] = `
Array [
MigrationTestAlpha
@@ -321,7 +338,7 @@
You currently have 0
XEC
@@ -330,7 +347,7 @@
@@ -543,13 +646,13 @@
exports[`Wallet with XEC balances and tokens and state field 1`] = `
Array [
MigrationTestAlpha
@@ -591,10 +694,10 @@
0.06
@@ -605,7 +708,7 @@
@@ -818,13 +938,13 @@
exports[`Wallet without XEC balance 1`] = `
Array [
MigrationTestAlpha
@@ -866,7 +986,7 @@
You currently have 0
XEC
@@ -875,7 +995,7 @@