Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- function fatal_error {
- echo "$1! Press enter to quit..."
- read
- exit 1
- }
- function initialize {
- # is the bitcoin executable in the path?
- if [ -z `which bitcoin` ]; then
- fatal_error "Bitcoin not found"
- fi
- # is the gpg executable in the path?
- if [ -z `which gpg` ]; then
- fatal_error "GnuPG not found"
- fi
- if [ -n "`which srm`" ]; then
- delete_command="srm -rfsz"
- elif [ -n "`which shred`" ]; then
- delete_command="shred -u -z"
- else
- delete_command="rm"
- fi
- # if the datadir doesn't exist then create it
- if [ ! -d ~/.bitcoin ]; then
- mkdir ~/.bitcoin
- fi
- cd ~/.bitcoin
- }
- function decrypt_wallet {
- # quiet, decrypt $wallet to wallet.dat
- gpg -qd --yes -o wallet.dat $wallet
- if [ $? -ne 0 ]; then
- fatal_error "Unable to decrypt wallet"
- fi
- }
- function encrypt_wallet {
- prompt_keyid
- # quiet, encrypt wallet.dat to $wallet for $keyid recipient, trusting $keyid to ignore the prompt
- gpg -qe --yes -r $keyid --trusted-key $keyid -o $1 wallet.dat
- # remove the plaintext wallet
- $delete_command wallet.dat
- }
- function prompt_wallet {
- # as long as we don't have a valid wallet, keep trying
- while [ -z $wallet ]; do
- clear
- # display a menu of wallets, plus functions (create/delete)
- ls | awk '
- BEGIN { i=1; print "Choose a wallet to open, or a function:" }
- /\.asc$/ { split($1,file,"."); print i++ ") " file[1] }
- END { print "+) create"; print "-) delete" }'
- read idx
- if [ "$idx" == "+" ]; then
- clear
- echo "Choose a name for the new wallet:"
- read wallet
- wallet=$wallet.asc
- elif [ "$idx" == "-" ]; then
- clear
- # display a menu of wallets
- ls | awk '
- BEGIN { i=1; print "Choose a wallet to delete:" }
- /\.asc$/ { split($1,file,"."); print i++ ") " file[1] }'
- read idx
- # read the wallet file name at $idx into $deletewallet
- deletewallet=`ls | awk -v idx="$idx" '
- BEGIN { i=1 }
- /\.asc$/ { if(i++==idx) { print $1 }}'`
- if [ -n "$deletewallet" ]; then
- rm -i $deletewallet
- fi
- else
- # read the wallet file name at $idx into $wallet
- wallet=`ls | awk -v idx="$idx" '
- BEGIN { i=1 }
- /\.asc$/ { if(i++==idx) { print $1 }}'`
- fi
- done
- }
- function prompt_existing {
- existing_wallet=""
- while [ -z $existing_wallet ]; do
- clear
- echo $1
- read existing_wallet
- done
- }
- function check_existing {
- # if you have a plaintext wallet, prompt to import
- if [ -e "wallet.dat" ]; then
- while [ -z $existing_wallet ]; do
- clear
- echo "Found existing wallet.dat, choose an action:"
- echo "1) Encrypt and save"
- echo "2) Rename"
- echo "3) Delete"
- read existing_wallet
- case $existing_wallet in
- 1)
- prompt_existing "Enter wallet name (without extension)"
- encrypt_wallet $existing_wallet.asc
- ;;
- 2)
- prompt_existing "Enter new filename (with extension)"
- mv wallet.dat $existing_wallet
- ;;
- [3])
- $delete_command wallet.dat
- ;;
- *)
- existing_wallet=""
- esac
- done
- fi
- }
- function prompt_keyid {
- keyid=""
- # as long as we don't have a valid keyid, keep trying
- while [ -z $keyid ]; do
- clear
- # display a list of key names
- gpg -K | awk '
- BEGIN { i=1; print "Choose the private key to encrypt the wallet:" }
- /^uid/ {line=""; for(n=2;n<=NF;n++) {line=line " " $n}; print i++ ")" line}'
- read idx
- # read the long keyid at $idx into $keyid
- keyid=`gpg --keyid-format long -K | awk -v idx="$idx" '
- BEGIN { i=1 }
- /^sec/ { if(i++==idx) { print substr($2,index($2,"/")+1)}}'`
- done
- }
- initialize
- check_existing
- prompt_wallet
- # if we selected an existing wallet, decrypt it
- # otherwise, it will be created by bitcoin
- if [ -e $wallet ]; then
- # decrypt to wallet.dat
- decrypt_wallet
- fi
- # run bitcoin
- bitcoin
- encrypt_wallet $wallet
Advertisement
Add Comment
Please, Sign In to add comment