From b13ea40d218925f3869dd1bcbdeaed935c6ac7c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Wed, 22 Jun 2022 02:41:41 +0100 Subject: [PATCH] fix: change operation order to ensure the safety of funds in unexpected conditions --- bin/lamassu-eth-sweep-to-new-wallet | 30 ++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/bin/lamassu-eth-sweep-to-new-wallet b/bin/lamassu-eth-sweep-to-new-wallet index 225075fd..8cb2ec6b 100644 --- a/bin/lamassu-eth-sweep-to-new-wallet +++ b/bin/lamassu-eth-sweep-to-new-wallet @@ -27,7 +27,13 @@ let lastUsedNonces = {} const hex = bigNum => '0x' + bigNum.integerValue(BN.ROUND_DOWN).toString(16) function writeNewMnemonic (mnemonic) { - return fs.writeFile(options.mnemonicPath, mnemonic) + return fs.writeFile(`${options.mnemonicPath}-new-temp`, mnemonic) + .then(() => `${options.mnemonicPath}-new-temp`) +} + +function renameNewMnemonic () { + return fs.rename(`${options.mnemonicPath}-new-temp`, `${options.mnemonicPath}`) + .then(() => options.mnemonicPath) } function backupMnemonic () { @@ -225,6 +231,19 @@ settingsLoader.loadLatest() process.exit(2) } + console.log(`Backing up old mnemonic...`) + return Promise.all([backupMnemonic(), infuraIsRunning, settings]) + }) + .then(([fileName, infuraIsRunning, settings]) => { + console.log(`Successfully backed up the old mnemonic, new location is ${fileName}`) + return Promise.all([writeNewMnemonic(mnemonic), infuraIsRunning, settings]) + }) + .then(([tempMnemonicFileName, infuraIsRunning, settings]) => { + console.log(`New mnemonic stored temporarily in ${tempMnemonicFileName}`) + console.log(`Starting funds transfer...`) + return Promise.all([infuraIsRunning, settings]) + }) + .then(([infuraIsRunning, settings]) => { if (infuraIsRunning) { const endpoint = _.startsWith('https://')(settings.accounts.infura.endpoint) ? settings.accounts.infura.endpoint @@ -244,7 +263,7 @@ settingsLoader.loadLatest() } const opts = { - chainId: 1, + chainId: 3, nonce: 0, includesFee: true } @@ -254,11 +273,8 @@ settingsLoader.loadLatest() .then(resTx => { console.log('Successfully moved funds from the old wallet to the new one.') console.log('Information about the transaction', resTx) - return backupMnemonic() - }) - .then(fileName => { - console.log(`Successfully backed up the old mnemonic, new location is ${fileName}`) - return writeNewMnemonic(mnemonic) + console.log('Moving the current mnemonic to the default file...') + return renameNewMnemonic() }) .then(() => { console.log('New mnemonic stored successfully! All your funds (minus the transaction fee) should be available in the next few minutes.')