from lib import keybase
import gnupg
from os import system
from pprint import pprint
import triplesec
import msgpack
from base64 import b64decode, b64encode
from binascii import hexlify, unhexlify

# Test code for use with user lookup
# lookup = 'temp'
# users = []
# fields = 'basics'
# while lookup != '':
#     lookup = raw_input("User to look up: ")
#     if lookup != '':
#         users.append(lookup)
#
# status = keybase.user_lookup('domain', users, fields)
# print status['status']

# Test code for obtaining a user's public key
# pub_key = keybase.user_pub_key('thorodinson')
# print pub_key

# Log in and import private key of logged in user
system('rm -rf /home/testgpguser/gpghome')  # clean GPG test directory before proceeding with test
gpg = gnupg.GPG(gnupghome='/home/testgpguser/gpghome')
user = raw_input("Username: ")
salt = keybase.get_salt(user)

pw = raw_input("Password: ")
ts = triplesec.TripleSec(key=pw)
login_reply = keybase.login(user, pw, salt["salt"], salt["session"])
me = login_reply['me']
session = login_reply['session']

# user_priv_key = me['private_keys']['primary']['bundle']
# json_priv_key = msgpack.unpackb(b64decode(user_priv_key))
# pprint(json_priv_key)
# print '\n'
#
# enc = json_priv_key['body']['priv']['data']
# print ts.decrypt(enc)

keys = keybase.key_fetch(me['private_keys']['primary']['kid'], ['sign'], session)
enc = msgpack.unpackb(b64decode(keys[0]['bundle']))
enc = enc['body']['priv']['data']
priv_key = ts.decrypt(enc)
import_result = gpg.import_keys(priv_key)
pprint(import_result.results)



@@ -191,16 +191,170 @@
 exlify%0A%0A
+# clean GPG test directory before proceeding with test%0Asystem('rm -rf /home/testgpguser/gpghome')%0Agpg = gnupg.GPG(gnupghome='/home/testgpguser/gpghome')%0A%0A
 # Test c
@@ -382,16 +382,16 @@
  lookup%0A
-
 # lookup
@@ -804,162 +804,8 @@
 ser%0A
-system('rm -rf /home/testgpguser/gpghome')  # clean GPG test directory before proceeding with test%0Agpg = gnupg.GPG(gnupghome='/home/testgpguser/gpghome')%0A
 user
@@ -1016,16 +1016,16 @@
 y%5B'me'%5D%0A
+
 session 
@@ -1054,236 +1054,8 @@
 '%5D%0A%0A
-# user_priv_key = me%5B'private_keys'%5D%5B'primary'%5D%5B'bundle'%5D%0A# json_priv_key = msgpack.unpackb(b64decode(user_priv_key))%0A# pprint(json_priv_key)%0A# print '%5Cn'%0A#%0A# enc = json_priv_key%5B'body'%5D%5B'priv'%5D%5B'data'%5D%0A# print ts.decrypt(enc)%0A%0A
 keys

remove unneeded commented-out and duplicate test code