Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cold Staking customisation UI #146

Merged
merged 3 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,12 @@ <h3 class="noselect balance-title">
<span class="reload noselect" onclick="MPW.refreshChainData()"><i id="balanceReloadStaking" class="fa-solid fa-rotate-right cur-pointer"></i></span>
</h3>
</div>

<div class="col-6 d-flex dcWallet-topRightMenu" style="justify-content: flex-end;">
<div class="btn-group dropleft">
<i class="fa-solid fa-gear" style="width: 20px;" onclick="MPW.guiSetColdStakingAddress()"></i>
</div>
</div>
</div>

Staking<br>
Expand Down
59 changes: 43 additions & 16 deletions scripts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
decryptWallet,
getNewAddress,
getDerivationPath,
LegacyMasterKey,

Check warning on line 13 in scripts/global.js

View workflow job for this annotation

GitHub Actions / ESLint

scripts/global.js#L13

'LegacyMasterKey' is defined but never used (@typescript-eslint/no-unused-vars)
} from './wallet.js';
import { getNetwork, HistoricalTxType } from './network.js';
import {
Expand All @@ -19,6 +19,8 @@
debug,
cMarket,
strCurrency,
setColdStakingAddress,
strColdStakingAddress,
} from './settings.js';
import { createAndSendTransaction, signTransaction } from './transactions.js';
import {
Expand Down Expand Up @@ -434,9 +436,6 @@
export const mempool = new Mempool();
let exportHidden = false;

// PIVX Labs' Cold Pool
export let cachedColdStakeAddr = 'SdgQDpS8jDRJDX8yK8m9KnTMarsE84zdsy';

/**
* Open a UI 'tab' menu, and close all other tabs, intended for frontend use
* @param {Event} evt - The click event target
Expand Down Expand Up @@ -1638,19 +1637,6 @@
domID.style.display = domID.style.display === 'block' ? 'none' : 'block';
}

export function askForCSAddr(force = false) {
if (force) cachedColdStakeAddr = null;
if (cachedColdStakeAddr === '' || cachedColdStakeAddr === null) {
cachedColdStakeAddr = prompt(
'Please provide a Cold Staking address (either from your own node, or a 3rd-party!)'
).trim();
if (cachedColdStakeAddr) return true;
} else {
return true;
}
return false;
}

export function isMasternodeUTXO(cUTXO, cMasternode) {
if (cMasternode?.collateralTxId) {
const { collateralTxId, outidx } = cMasternode;
Expand All @@ -1660,6 +1646,47 @@
}
}

/**
* Creates a GUI popup for the user to check or customise their Cold Address
*/
export async function guiSetColdStakingAddress() {
if (
await confirmPopup({
title: 'Set your Cold Staking address',
html: `<p>Current address:<br><span class="mono">${strColdStakingAddress}</span><br><br><span style="opacity: 0.65; margin: 10px;">A Cold Address stakes coins on your behalf, it cannot spend coins, so it's even safe to use a stranger's Cold Address!</span></p><br><input type="text" id="newColdAddress" placeholder="Example: ${strColdStakingAddress.substring(
0,
6
)}..." style="text-align: center;">`,
})
) {
// Fetch address from the popup input
const strColdAddress = document.getElementById('newColdAddress').value;

// If it's empty, just return false
if (!strColdAddress) return false;

// Sanity-check, and set!
if (
strColdAddress[0] === cChainParams.current.STAKING_PREFIX &&
strColdAddress.length === 34
) {
await setColdStakingAddress(strColdAddress);
createAlert(
'info',
'<b>Cold Address set!</b><br>Future stakes will use this address.',
[],
5000
);
return true;
} else {
createAlert('warning', 'Invalid Cold Staking address!', [], 2500);
return false;
}
} else {
return false;
}
}

export async function wipePrivateData() {
const isEncrypted = await hasEncryptedWallet();
const title = isEncrypted
Expand Down
1 change: 1 addition & 0 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export {
openTab,
accessOrImportWallet,
guiImportWallet,
guiSetColdStakingAddress,
onPrivateKeyChanged,
toClipboard,
toggleExportUI,
Expand Down
28 changes: 26 additions & 2 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export let cExplorer = cChainParams.current.Explorers[0];
export let cNode = cChainParams.current.Nodes[0];
/** A mode which allows MPW to automatically select it's data sources */
export let fAutoSwitch = true;
/** The active Cold Staking address: default is the PIVX Labs address */
export let strColdStakingAddress = 'SdgQDpS8jDRJDX8yK8m9KnTMarsE84zdsy';

let transparencyReport;

Expand All @@ -57,6 +59,10 @@ export class Settings {
* @type {Boolean} The Auto-Switch mode state
*/
autoswitch;
/**
* @type {String} The user's active Cold Staking address
*/
coldAddress;
/**
* @type {String} translation to use
*/
Expand All @@ -70,13 +76,15 @@ export class Settings {
explorer,
node,
autoswitch = true,
coldAddress = strColdStakingAddress,
translation = 'en',
displayCurrency = 'usd',
} = {}) {
this.analytics = analytics;
this.explorer = explorer;
this.node = node;
this.autoswitch = autoswitch;
this.coldAddress = coldAddress;
this.translation = translation;
this.displayCurrency = displayCurrency;
}
Expand Down Expand Up @@ -165,8 +173,14 @@ export async function start() {
const database = await Database.getInstance();

// Fetch settings from Database
const { analytics: strSettingAnalytics, autoswitch } =
await database.getSettings();
const {
analytics: strSettingAnalytics,
autoswitch,
coldAddress,
} = await database.getSettings();

// Set the Cold Staking address
strColdStakingAddress = coldAddress;

// Set any Toggles to their default or DB state
fAutoSwitch = autoswitch;
Expand Down Expand Up @@ -277,6 +291,16 @@ async function setCurrency(currency) {
getBalance(true);
}

/**
* Sets and saves the active Cold Staking address
* @param {string} strColdAddress - The Cold Staking address
*/
export async function setColdStakingAddress(strColdAddress) {
strColdStakingAddress = strColdAddress;
const database = await Database.getInstance();
database.setSettings({ coldAddress: strColdAddress });
}

/**
* Fills the translation dropbox on the settings page
*/
Expand Down
28 changes: 11 additions & 17 deletions scripts/transactions.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import bitjs from './bitTrx.js';
import { debug } from './settings.js';
import { debug, strColdStakingAddress } from './settings.js';
import { ALERTS } from './i18n.js';
import {
doms,
getBalance,
mempool,
isMasternodeUTXO,
askForCSAddr,
cachedColdStakeAddr,
restoreWallet,
toggleBottomMenu,
guiSetColdStakingAddress,
} from './global.js';
import {
hasWalletUnlocked,
masterKey,
getNewAddress,
isYourAddress,
cHardwareWallet,
strHardwareName,
} from './wallet.js';
Expand Down Expand Up @@ -149,21 +147,17 @@ export async function delegateGUI() {
if (!validateAmount(nAmount, COIN)) return;

// Ensure the user has an address set - if not, request one!
if (!askForCSAddr()) return;

// Sanity
if (
cachedColdStakeAddr.length !== 34 ||
!cachedColdStakeAddr.startsWith(cChainParams.current.STAKING_PREFIX)
) {
askForCSAddr(true);
return createAlert('success', ALERTS.SUCCESS_STAKING_ADDR_SET, []);
}
(!strColdStakingAddress ||
strColdStakingAddress[0] !== cChainParams.current.STAKING_PREFIX) &&
(await guiSetColdStakingAddress()) === false
)
return;

// Perform the TX
const cTxRes = await createAndSendTransaction({
amount: nAmount,
address: cachedColdStakeAddr,
address: strColdStakingAddress,
isDelegation: true,
useDelegatedInputs: false,
});
Expand Down Expand Up @@ -211,11 +205,11 @@ export async function undelegateGUI() {
isDelegation: false,
useDelegatedInputs: true,
delegateChange: true,
changeDelegationAddress: cachedColdStakeAddr,
changeDelegationAddress: strColdStakingAddress,
});

if (!cTxRes.ok && cTxRes.err === 'No change addr') {
askForCSAddr(true);
await guiSetColdStakingAddress();
await undelegateGUI();
} else {
// If successful, reset the inputs
Expand Down Expand Up @@ -290,7 +284,7 @@ export async function createAndSendTransaction({
if (nChange > 0) {
if (delegateChange && nChange > 1.01 * COIN) {
if (!changeDelegationAddress)
return { ok: false, error: 'No change addr' };
return { ok: false, err: 'No change addr' };
cTx.addcoldstakingoutput(
changeAddress,
changeDelegationAddress,
Expand Down
Loading