Skip to content

Instantly share code, notes, and snippets.

@dcod3d
Last active July 4, 2021 19:53
Show Gist options
  • Save dcod3d/90fe6209a0b3ae815a6eaa2aef53524c to your computer and use it in GitHub Desktop.
Save dcod3d/90fe6209a0b3ae815a6eaa2aef53524c to your computer and use it in GitHub Desktop.

Purpose

Removing dust UTXOs from your wallet using electrum to prevent accidental spending. This example creates an OP_RETURN transaction with the entire dust amount being used as the transaction fee. The transaction fee will be ~3 to 5 sat/vB and may take some time to be confirmed. To prevent linking between multiple dust transactions, wait at least until the transaction is confirmed before submitting another.

Electrum

Coins Tab

Select one dust UTXO and right click Spend

Send Tab

Pay to: OP_RETURN ########,0
######## can be up to 64 hex characters

Create random 64 hex xxd -u -l "32" -p /dev/urandom | tr -d " \n"
Or convert ASCII text to hex echo "547 satoshi dust" | xxd -u -p | tr -d " \n" > 353437207361746F73686920647573740A

Click Pay..

Create Transaction Window

Adjust Fee to dust amount (Ex: 0.00000547 BTC)
Veryfy Input: 1 UTXO with the dust amount
Verify Output: 1 output showing SCRIPT ######## with 0. amount

Click Finalize

Transaction Window

Click Sign or Export > ... to get a psbt to use with a hardware wallet.

Once signed, Transaction ID field should now be populated.

Click Broadcast or increase privacy by
Export > Copy to Clipboard to copy the raw transaction.
Go to http://explorerzydxu5ecjrkwceayqybizmpjjznk5izmitf2modhcusuqlid.onion/tx/push and paste the raw transaction to broadcast.

Bitcoin Core

Bash script using bitcoin-cli. The script will either need the private key or can provide a psbt.

#!/bin/bash

Red=$'\e[1;31m'
Green=$'\e[1;32m'
Blue=$'\e[1;34m'
White=$'\e[1;37m'

read -p "$Blue""Enter dust transaction id: $White"  txid
read -p "$Blue""Enter vout: $White" vout

address=$(bitcoin-cli gettxout $txid $vout | jq -r '.scriptPubKey.addresses[0]')
value=$(bitcoin-cli gettxout $txid $vout | jq '.value')

opreturn=$(xxd -u -l "32" -p /dev/urandom | tr -d " \n")

if [ -z $address ]; then
  echo "$Red""***Output already spent***"
  exit
fi

echo "**************************************"
echo "$Green""Bitcoin Address:" 
echo "$Red"$address
echo "$Green""Dust Amount:$Red"
printf "%'.8f\n" $value
rawtx=$(bitcoin-cli createrawtransaction "[{\"txid\":\"$txid\",\"vout\":$vout}]" "{\"data\":\"$opreturn\"}")
psbtx=$(bitcoin-cli createpsbt "[{\"txid\":\"$txid\",\"vout\":$vout}]" "{\"data\":\"$opreturn\"}")
dectx=$(bitcoin-cli decoderawtransaction $rawtx)
echo "$White""**************************************"
echo "$Green""Raw Transaction:"
echo "$Red"$rawtx
echo "$Green""Raw PSBT:"
echo "$Red"$psbtx
echo "$Green""Decoded Transaction:$White"
echo $dectx | jq


echo "**************************************"
read -p "$Blue""Enter private key for $address: $White"  privkey

signed=$(bitcoin-cli signrawtransactionwithkey $rawtx "[\"$privkey\"]")
echo "$Green""Signed Transaction:$White"
echo $signed | jq

read -p "$Blue""Enter SEND to broadcast transaction: $White" send
if [ $send == "SEND" ]; then
  echo "$Green""Broadcasting transaction...$White""
  broadcast=$(bitcoin-cli sendrawtransaction $signed)
  echo $broadcast | jq 
fi
@Shymaa-Arafat
Copy link

Shymaa-Arafat commented Jul 4, 2021

Has anyone done some effort to store dust UTXOS separately (or at least their hashes if we're talking about stateless nodes)
-I mean a simple If- Statement or like code if val ≤ Threshold do so & so
Like this
https://bitcointalk.org/index.php?topic=5343748.0

-Such that they don't burden the system status until their owner do as u suggest here?
-This way the system can also be cautious about them causing attacks from the beginning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment