commit b615fe0c8c91a23d3c723f9d454b96810df8ab7a
parent b8c1c0c31704ad3731b88f3acf1dc5573c825194
Author: Julian Tosh <Julian@Tosh.us>
Date: Fri, 6 Jul 2012 21:45:57 -0700
modified password input routines to allow for input through stdin
Diffstat:
3 files changed, 33 insertions(+), 22 deletions(-)
diff --git 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]
diff --git a/lib/__init__.py b/lib/__init__.py
@@ -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
diff --git a/lib/wallet.py b/lib/wallet.py
@@ -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)