chrisrico

Bitcoin encrypted wallet chooser

Sep 17th, 2011
1,940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.69 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function fatal_error {
  4.     echo "$1! Press enter to quit..."
  5.     read
  6.     exit 1
  7. }
  8.  
  9. function initialize {
  10.     # is the bitcoin executable in the path?
  11.     if [ -z `which bitcoin` ]; then
  12.         fatal_error "Bitcoin not found"
  13.     fi
  14.  
  15.     # is the gpg executable in the path?
  16.     if [ -z `which gpg` ]; then
  17.         fatal_error "GnuPG not found"
  18.     fi
  19.  
  20.     if [ -n "`which srm`" ]; then
  21.         delete_command="srm -rfsz"
  22.     elif [ -n "`which shred`" ]; then
  23.         delete_command="shred -u -z"
  24.     else
  25.         delete_command="rm"
  26.     fi
  27.  
  28.     # if the datadir doesn't exist then create it
  29.     if [ ! -d ~/.bitcoin ]; then
  30.         mkdir ~/.bitcoin
  31.     fi
  32.  
  33.     cd ~/.bitcoin
  34. }
  35.  
  36. function decrypt_wallet {
  37.     # quiet, decrypt $wallet to wallet.dat
  38.     gpg -qd --yes -o wallet.dat $wallet
  39.     if [ $? -ne 0 ]; then
  40.         fatal_error "Unable to decrypt wallet"
  41.     fi
  42. }
  43.  
  44. function encrypt_wallet {
  45.     prompt_keyid
  46.    
  47.     # quiet, encrypt wallet.dat to $wallet for $keyid recipient, trusting $keyid to ignore the prompt
  48.     gpg -qe --yes -r $keyid --trusted-key $keyid -o $1 wallet.dat
  49.     # remove the plaintext wallet
  50.     $delete_command wallet.dat
  51. }
  52.  
  53. function prompt_wallet {
  54.     # as long as we don't have a valid wallet, keep trying
  55.     while [ -z $wallet ]; do
  56.         clear
  57.  
  58.     # display a menu of wallets, plus functions (create/delete)
  59.         ls | awk '
  60.            BEGIN { i=1; print "Choose a wallet to open, or a function:" }
  61.            /\.asc$/ { split($1,file,"."); print i++ ") " file[1] }
  62.            END { print "+) create"; print "-) delete" }'
  63.  
  64.         read idx
  65.  
  66.         if [ "$idx" == "+" ]; then
  67.             clear
  68.             echo "Choose a name for the new wallet:"
  69.             read wallet
  70.             wallet=$wallet.asc
  71.         elif [ "$idx" == "-" ]; then
  72.             clear
  73.  
  74.             # display a menu of wallets
  75.             ls | awk '
  76.                BEGIN { i=1; print "Choose a wallet to delete:" }
  77.                /\.asc$/ { split($1,file,"."); print i++ ") " file[1] }'
  78.  
  79.             read idx
  80.  
  81.             # read the wallet file name at $idx into $deletewallet
  82.             deletewallet=`ls | awk -v idx="$idx" '
  83.                BEGIN { i=1 }
  84.                /\.asc$/ { if(i++==idx) { print $1 }}'`
  85.        
  86.             if [ -n "$deletewallet" ]; then
  87.                 rm -i $deletewallet
  88.             fi
  89.         else
  90.             # read the wallet file name at $idx into $wallet
  91.             wallet=`ls | awk -v idx="$idx" '
  92.                BEGIN { i=1 }
  93.                /\.asc$/ { if(i++==idx) { print $1 }}'`
  94.         fi
  95.  
  96.     done
  97. }
  98.  
  99. function prompt_existing {
  100.     existing_wallet=""
  101.     while [ -z $existing_wallet ]; do
  102.         clear
  103.        
  104.         echo $1
  105.        
  106.         read existing_wallet
  107.        
  108.     done
  109. }
  110.  
  111. function check_existing {
  112.     # if you have a plaintext wallet, prompt to import
  113.     if [ -e "wallet.dat" ]; then
  114.        
  115.         while [ -z $existing_wallet ]; do
  116.             clear
  117.            
  118.             echo "Found existing wallet.dat, choose an action:"
  119.             echo "1) Encrypt and save"
  120.             echo "2) Rename"
  121.             echo "3) Delete"
  122.            
  123.             read existing_wallet
  124.            
  125.             case $existing_wallet in
  126.                 1)
  127.                     prompt_existing "Enter wallet name (without extension)"
  128.                     encrypt_wallet $existing_wallet.asc
  129.                     ;;
  130.                 2)
  131.                     prompt_existing "Enter new filename (with extension)"
  132.                     mv wallet.dat $existing_wallet
  133.                     ;;
  134.                 [3])
  135.                     $delete_command wallet.dat
  136.                     ;;
  137.                 *)
  138.                     existing_wallet=""
  139.             esac
  140.            
  141.                
  142.         done
  143.  
  144.     fi
  145. }
  146.  
  147. function prompt_keyid {
  148.     keyid=""
  149.  
  150.     # as long as we don't have a valid keyid, keep trying
  151.     while [ -z $keyid ]; do
  152.  
  153.         clear
  154.  
  155.     # display a list of key names
  156.         gpg -K | awk '
  157.            BEGIN { i=1; print "Choose the private key to encrypt the wallet:" }
  158.            /^uid/ {line=""; for(n=2;n<=NF;n++) {line=line " " $n}; print i++ ")" line}'
  159.  
  160.         read idx
  161.  
  162.     # read the long keyid at $idx into $keyid
  163.         keyid=`gpg --keyid-format long -K | awk -v idx="$idx" '
  164.            BEGIN { i=1 }
  165.            /^sec/ { if(i++==idx) { print substr($2,index($2,"/")+1)}}'`
  166.  
  167.     done
  168. }
  169.  
  170. initialize
  171.  
  172. check_existing
  173.  
  174. prompt_wallet
  175.  
  176. # if we selected an existing wallet, decrypt it
  177. # otherwise, it will be created by bitcoin
  178. if [ -e $wallet ]; then
  179.     # decrypt to wallet.dat
  180.     decrypt_wallet    
  181. fi
  182.  
  183.  
  184. # run bitcoin
  185. bitcoin
  186.  
  187. encrypt_wallet $wallet
Advertisement
Add Comment
Please, Sign In to add comment