Skip to content

Commit

Permalink
[GUI] Improve send tx flow (#274)
Browse files Browse the repository at this point in the history
* Close and clear tx inputs on send

* Remove non-used function

* bail out early if wallet if not synced yet
  • Loading branch information
panleone authored Dec 18, 2023
1 parent 3c60f53 commit 72a7f20
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 113 deletions.
19 changes: 11 additions & 8 deletions scripts/dashboard/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ async function lockWallet() {
* @param {number} amount - Amount of PIVs to send
*/
async function send(address, amount) {
// Ensure a wallet is loaded
// Ensure a wallet is unlocked
if (wallet.isViewOnly.value) {
return createAlert(
'warning',
Expand All @@ -280,16 +280,14 @@ async function send(address, amount) {
: 'import/create',
},
]),
3500
3000
);
}
// Ensure the wallet is unlocked
if (
wallet.isViewOnly.value &&
!(await restoreWallet(translation.walletUnlockTx))
)
return;
// Ensure wallet is synced
if (!getNetwork()?.fullSynced) {
return createAlert('warning', `${ALERTS.WALLET_NOT_SYNCED}`, 3000);
}
// Sanity check the receiver
address = address.trim();
Expand Down Expand Up @@ -352,6 +350,11 @@ async function send(address, amount) {
const nValue = Math.round(amount * COIN);
if (!validateAmount(nValue)) return;
// Close the send screen and clear inputs
showTransferMenu.value = false;
transferAddress.value = '';
transferAmount.value = '';
// Create and send the TX
await createAndSendTransaction({
address,
Expand Down
1 change: 0 additions & 1 deletion scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export {
changePassword,
} from './settings.js';
export {
createTxGUI,
undelegateGUI,
delegateGUI,
createMasternode,
Expand Down
104 changes: 0 additions & 104 deletions scripts/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,110 +64,6 @@ export function validateAmount(nAmountSats, nMinSats = 10000) {
return true;
}

/**
* Create a transaction using input from the user interface
*/
export async function createTxGUI() {
// Ensure a wallet is loaded
if (!(await wallet.hasWalletUnlocked(true))) return;

// Ensure the wallet is unlocked
if (
wallet.isViewOnly() &&
!(await restoreWallet(translation.walletUnlockTx))
)
return;

// Sanity check the receiver
const strRawReceiver = doms.domAddress1s.value.trim();

// Cache the "end" receiver, which will be an Address
let strReceiverAddress = strRawReceiver;

// Check for any contacts that match the input
const cDB = await Database.getInstance();
const cAccount = await cDB.getAccount();

// If we have an Account, then check our Contacts for anything matching too
const cContact = cAccount?.getContactBy({
name: strRawReceiver,
pubkey: strRawReceiver,
});
// If a Contact were found, we use it's Pubkey
if (cContact) strReceiverAddress = cContact.pubkey;

// If this is an XPub, we'll fetch their last used 'index', and derive a new public key for enhanced privacy
if (isXPub(strReceiverAddress)) {
const cNet = getNetwork();
if (!cNet.enabled)
return createAlert(
'warning',
ALERTS.WALLET_OFFLINE_AUTOMATIC,
3500
);

// Fetch the XPub info
const cXPub = await cNet.getXPubInfo(strReceiverAddress);

// Use the latest index plus one (or if the XPub is unused, then the second address)
const nIndex = (cXPub.usedTokens || 0) + 1;

// Create a receiver master-key
const cReceiverWallet = new HdMasterKey({ xpub: strReceiverAddress });
const strPath = cReceiverWallet.getDerivationPath(0, 0, nIndex);

// Set the 'receiver address' as the unused XPub-derived address
strReceiverAddress = cReceiverWallet.getAddress(strPath);
}

// If Staking address: redirect to staking page
if (isColdAddress(strReceiverAddress)) {
createAlert('warning', ALERTS.STAKE_NOT_SEND, 7500);
// Close the current Send Popup
toggleBottomMenu('transferMenu', 'transferAnimation');
// Open the Staking Dashboard
return doms.domStakeTab.click();
}

// Check if the Receiver Address is a valid P2PKH address
if (!isStandardAddress(strReceiverAddress))
return createAlert(
'warning',
tr(ALERTS.INVALID_ADDRESS, [{ address: strReceiverAddress }]),
2500
);

// Sanity check the amount
const nValue = Math.round(
Number(doms.domSendAmountCoins.value.trim()) * COIN
);
if (!validateAmount(nValue)) return;

// Create and send the TX
const cRes = await createAndSendTransaction({
address: strReceiverAddress,
amount: nValue,
isDelegation: false,
});

// If successful, wipe Tx input
if (cRes.ok) {
// Address
doms.domAddress1s.value = '';
// Amount
doms.domSendAmountCoins.value = '';
// Price
doms.domSendAmountValue.value = '';

// Wipe any Payment Request info
if (doms.domReqDesc.value) {
// Description
doms.domReqDesc.value = '';
doms.domReqDisplay.style.display = 'none';
}
}
}

/**
* Create a Cold Staking delegation transaction
*/
Expand Down

0 comments on commit 72a7f20

Please sign in to comment.