bitcoin private key import script


SUBMITTED BY: Guest

DATE: Aug. 23, 2012, 2:05 p.m.

FORMAT: Bash

SIZE: 2.1 kB

HITS: 2789

  1. #!/bin/bash
  2. #
  3. # Bitcoin private key import helper (c)2012 ElasticNinja
  4. # bitcoin:1NinjaRFABUh48pwL5CdAYgnt8bTPnsuu4
  5. #
  6. # Requires bitcoin client running in server mode. If you don't already have this
  7. # then edit ~/.bitcoin/bitcoin.conf and add following three lines:
  8. # rpcuser=<some random username>
  9. # rpcpassword=<some random password>
  10. # server=1
  11. #
  12. # After modifying restart your bitcoin client. When you are finished adding keys
  13. # you can remove those lines from configuration.
  14. #
  15. #
  16. function unlock_wallet()
  17. {
  18. bitcoind walletpassphrase "$1" "$2"
  19. }
  20. function import_key()
  21. {
  22. bitcoind importprivkey "$1" "$2"
  23. }
  24. function fatal()
  25. {
  26. echo $1
  27. exit 1
  28. }
  29. function usage()
  30. {
  31. echo "Usage: bitkeyimport [-P|-p passphrase] [-l label] -k privkey"
  32. echo ""
  33. echo "-h this help message"
  34. echo "-P ask for wallet passphrase"
  35. echo "-p use provided passphrase (less secure, but useful in scripting)"
  36. echo "-l addressbook label for new address"
  37. echo "-k the private key for the new address (required)"
  38. echo ""
  39. echo "(c)2012 ElasticNinja bitcoin:1NinjaRFABUh48pwL5CdAYgnt8bTPnsuu4"
  40. exit 0;
  41. }
  42. while getopts "hk:l:p:P" option; do
  43. case $option in
  44. P) askpass=1 ;;
  45. p) pass=$OPTARG ;;
  46. l) label=$OPTARG ;;
  47. k) key=$OPTARG ;;
  48. *) usage ;;
  49. esac
  50. done
  51. if ((askpass==1)); then
  52. echo -n "Enter wallet passphrase: "
  53. stty -echo echonl
  54. read pass
  55. stty echo
  56. fi
  57. if [ -z "$key" ]; then
  58. echo "No private key specified!"
  59. exit 1
  60. fi
  61. if [ ! -z "$pass" ]; then
  62. unlock_wallet "$pass" 120 || fatal "Could not unlock wallet, passphrase incorrect?"
  63. fi
  64. echo "Importing key.. be patient, this may take a while."
  65. import_key "$key" "$label" || fatal "Could not add key, wallet still locked?"
  66. echo "All done!"

comments powered by Disqus