How to sweep a multi-account Bitcoin Electrum wallet
How to sweep a multi-account Bitcoin Electrum wallet through Trezor if you can't export the mnemonic.
It’s easy to sweep one account in Electrum. Open the wallet with correct path following the HD wallet structure, which is m / purpose' / coin_type' / account' / change / address_index
For eg: m/49'/0'/99' if you want to access the 99th account under the current mnemonic.
Now if your funds are distributed across multiple addresses under multiple accounts (in the order of 10^5), you wouldn’t want to do that in the Electrum app. It’ll take days if not months.
Let’s get to hacking!
We will find all the unspent outputs (UTXOs), use them as inputs in a new transaction, sign the transaction and save the transaction hex in a file to broadcast manually.
We are going to work within the console provided by the Electrum app. So we have the liberty to use some of the functions to help make our solution simpler.
For the purpose of this example, let’s assume we have 10^5 accounts and 10 addresses under each account. Here goes. At the time of this writing, I was using trezor[hidapi]==0.12.2
A rough Python script, but you get the idea. Follow along.
And here’s the transformer function mentioned in the comments above.
And finally put these together to sign the transaction.
To run this from within the Electrum console, you’ll have to import the file and call the function.
Start electrum and load your Trezor wallet. Go to Electrum’s console and load the script file. Do not forget to change the file path to the full path on your system.
Now run the command to generate a signed transaction. (Don’t worry, it will not broadcast)
It’ll take a while if you have many UTXOs. Be patient.
Once done, load the signed transaction hex through Electrum’s import transaction functionality. Verify everything’s correct and broadcast.
Software Engineer, a product person, delves into graphic designs on lazy weekends or writes blog posts about software engineering and life experiences. Retired competitive programmer.