Skip to content

Commit

Permalink
Fix address rotation (#470)
Browse files Browse the repository at this point in the history
* feat: Update addressIndices after sync

* feat: Introduce getNewChangeAddress and correctly select the address to undelegate

* feat: Avoid address re-use

* refactor: Make private every wallet method that can be private

* more private methods

* test: Add integration test to check address rotation
  • Loading branch information
panleone authored Nov 21, 2024
1 parent 60399c6 commit 68e82a7
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 68 deletions.
6 changes: 2 additions & 4 deletions scripts/composables/use_wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export const useWallet = defineStore('wallet', () => {
const getKeyToBackup = async () => await wallet.getKeyToBackup();
const getKeyToExport = () => wallet.getKeyToExport();
const isEncrypted = ref(true);
const loadFromDisk = () => wallet.loadFromDisk();
const hasShield = ref(wallet.hasShield());

const setMasterKey = async ({ mk, extsk }) => {
Expand All @@ -71,7 +70,7 @@ export const useWallet = defineStore('wallet', () => {
wallet.setShield(shield);
hasShield.value = wallet.hasShield();
};
const getAddress = () => wallet.getAddress();
const getNewChangeAddress = () => wallet.getNewChangeAddress();
const isHardwareWallet = ref(wallet.isHardwareWallet());
const isHD = ref(wallet.isHD());
const checkDecryptPassword = async (passwd) =>
Expand Down Expand Up @@ -157,7 +156,7 @@ export const useWallet = defineStore('wallet', () => {
isHardwareWallet,
checkDecryptPassword,
encrypt,
getAddress,
getNewChangeAddress,
wipePrivateData: () => {
wallet.wipePrivateData();
isViewOnly.value = wallet.isViewOnly();
Expand All @@ -173,7 +172,6 @@ export const useWallet = defineStore('wallet', () => {
price,
sync,
createAndSendTransaction,
loadFromDisk,
coldBalance,
};
});
11 changes: 5 additions & 6 deletions scripts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,11 @@ export function optimiseCurrencyLocale(nAmount) {
*/
export async function openExplorer(strAddress = '') {
const network = useNetwork();
const toExport = wallet.getKeyToExport();
if (wallet.isLoaded() && wallet.isHD() && !strAddress) {
const xpub = wallet.getXPub();
window.open(network.explorerUrl + '/xpub/' + xpub, '_blank');
window.open(network.explorerUrl + '/xpub/' + toExport, '_blank');
} else {
const address = strAddress || wallet.getAddress();
window.open(network.explorerUrl + '/address/' + address, '_blank');
window.open(network.explorerUrl + '/address/' + toExport, '_blank');
}
}

Expand Down Expand Up @@ -745,7 +744,7 @@ export async function sweepAddress(arrUTXOs, sweepingMasterKey, nFixedFee) {
const txBuilder = TransactionBuilder.create().addUTXOs(arrUTXOs);

const outputValue = txBuilder.valueIn - (nFixedFee || txBuilder.getFee());
const [address] = wallet.getNewAddress(1);
const address = wallet.getNewChangeAddress();
const tx = txBuilder
.addOutput({
address,
Expand Down Expand Up @@ -1677,7 +1676,7 @@ export async function createProposal() {
// If Advanced Mode is enabled and an address is given, use the provided address, otherwise, generate a new one
const strAddress =
document.getElementById('proposalAddress').value.trim() ||
wallet.getNewAddress(1)[0];
wallet.getNewChangeAddress();
const nextSuperblock = await getNetwork().getNextSuperblock();
const proposal = {
name: strTitle,
Expand Down
2 changes: 1 addition & 1 deletion scripts/stake/Stake.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async function unstake(value) {
}
const res = await wallet.createAndSendTransaction(
getNetwork(),
wallet.getAddress(1),
wallet.getNewChangeAddress(),
value,
{
useDelegatedInputs: true,
Expand Down
Loading

0 comments on commit 68e82a7

Please sign in to comment.