modified password input routines to allow for input through stdin
authorJulian Tosh <Julian@Tosh.us>
Sat, 7 Jul 2012 04:45:57 +0000 (21:45 -0700)
committerJulian Tosh <Julian@Tosh.us>
Sat, 7 Jul 2012 04:45:57 +0000 (21:45 -0700)
electrum
lib/__init__.py
lib/wallet.py

index a351f73..f12f159 100755 (executable)
--- a/electrum
+++ b/electrum
@@ -16,7 +16,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
 
-import re, sys, getpass
+import re, sys
 
 try:
     import ecdsa  
@@ -31,9 +31,9 @@ except:
     sys.exit(1)
 
 try:
-    from lib import Wallet, WalletSynchronizer, format_satoshis, mnemonic
+    from lib import Wallet, WalletSynchronizer, format_satoshis, mnemonic, prompt_password
 except ImportError:
-    from electrum import Wallet, WalletSynchronizer, format_satoshis, mnemonic
+    from electrum import Wallet, WalletSynchronizer, format_satoshis, mnemonic, prompt_password
     
 from optparse import OptionParser
 from decimal import Decimal
@@ -178,14 +178,7 @@ if __name__ == '__main__':
         if wallet.file_exists:
             print "remove the existing wallet first!"
             sys.exit(0)
-        password = getpass.getpass("Password (hit return if you do not wish to encrypt your wallet):")
-        if password:
-            password2 = getpass.getpass("Confirm password:")
-            if password != password2:
-                print "error"
-                sys.exit(1)
-        else:
-            password = None
+        password = prompt_password("Password (hit return if you do not wish to encrypt your wallet):")
 
         w_host, w_port, w_protocol = wallet.server.split(':')
         host = raw_input("server (default:%s):"%w_host)
@@ -269,12 +262,12 @@ if __name__ == '__main__':
                 
     # commands needing password
     if cmd in protected_commands or ( cmd=='addresses' and options.show_keys):
-        password = getpass.getpass('Password:') if wallet.use_encryption and not is_temporary else None
+        password = prompt_password('Password:') if wallet.use_encryption and not is_temporary else None
         # check password
         try:
             wallet.pw_decode( wallet.seed, password)
         except:
-            print "invalid password"
+            print "Error: This password does not decode this wallet."
             exit(1)
 
     if cmd == 'import':
@@ -435,7 +428,7 @@ if __name__ == '__main__':
     elif cmd in ['payto', 'mktx']:
         if from_addr and is_temporary:
             if from_addr.find(":") == -1:
-                keypair = from_addr + ":" + getpass.getpass('Private key:')
+                keypair = from_addr + ":" + prompt_password('Private key:', False)
             else:
                 keypair = from_addr
                 from_addr = keypair.split(':')[0]
@@ -484,13 +477,11 @@ if __name__ == '__main__':
         try:
             seed = wallet.pw_decode( wallet.seed, password)
         except:
-            print "sorry"
+            print "Error: Password does not decrypt this wallet."
             sys.exit(1)
-        new_password = getpass.getpass('New password:')
-        if new_password == getpass.getpass('Confirm new password:'):
-            wallet.update_password(seed, password, new_password)
-        else:
-            print "error: mismatch"
+
+        new_password = prompt_password('New password:')
+        wallet.update_password(seed, password, new_password)
 
     elif cmd == 'signmessage':
         address = args[1]
index df10c3b..a2e1aca 100644 (file)
@@ -1,3 +1,3 @@
-from wallet import Wallet, format_satoshis
+from wallet import Wallet, format_satoshis, prompt_password
 from interface import WalletSynchronizer
 from interface import TcpStratumInterface
index bc584dd..6148635 100644 (file)
@@ -17,7 +17,7 @@
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
 
 
-import sys, base64, os, re, hashlib, copy, operator, ast, threading, random
+import sys, base64, os, re, hashlib, copy, operator, ast, threading, random, getpass
 import aes, ecdsa
 from ecdsa.util import string_to_number, number_to_string
 
@@ -147,6 +147,26 @@ def ASecretToSecret(key):
 
 ########### end pywallet functions #######################
 
+# get password routine
+def prompt_password(prompt, confirm=True):
+    if sys.stdin.isatty():
+        password = getpass.getpass(prompt)
+
+        if password and confirm:
+            password2 = getpass.getpass("Confirm: ")
+
+            if password != password2:
+                print "Error: Passwords do not match."
+                sys.exit(1)
+
+    else:
+        password = raw_input(prompt)
+
+    if not password:
+        password = None
+
+    return password
+
 # URL decode
 _ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
 urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)