From ed1809d52137de9c609e607c7999968559353d93 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Tue, 24 Mar 2015 00:03:10 -0700 Subject: [PATCH] change API to support upcoming --verify flag --- src/wormhole/blocking/transcribe.py | 16 +++++++--------- src/wormhole/scripts/cmd_receive_file.py | 16 ++++++++-------- src/wormhole/scripts/cmd_receive_text.py | 6 +++--- src/wormhole/scripts/cmd_send_file.py | 13 +++++++------ src/wormhole/scripts/cmd_send_text.py | 4 ++-- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/wormhole/blocking/transcribe.py b/src/wormhole/blocking/transcribe.py index e0fa524..f3edfb7 100644 --- a/src/wormhole/blocking/transcribe.py +++ b/src/wormhole/blocking/transcribe.py @@ -138,7 +138,7 @@ class Common: assert len(key) == SecretBox.KEY_SIZE box = SecretBox(key) nonce = utils.random(SecretBox.NONCE_SIZE) - return box.encrypt(self.data, nonce) + return box.encrypt(data, nonce) def _post_data(self, data): post_data = json.dumps({"message": hexlify(data).decode("ascii")}) @@ -167,9 +167,8 @@ class Common: return HKDF(self.key, length, CTXinfo=purpose) class Initiator(Common): - def __init__(self, appid, data, relay): + def __init__(self, appid, relay): self.appid = appid - self.data = data self.relay = relay assert self.relay.endswith("/") self.started = time.time() @@ -186,12 +185,12 @@ class Initiator(Common): self._post_pake() return self.code - def get_data(self): + def get_data(self, outbound_data): key = self._get_pake([]) self.key = key try: outbound_key = self.derive_key(b"sender") - outbound_encrypted = self._encrypt_data(outbound_key, self.data) + outbound_encrypted = self._encrypt_data(outbound_key, outbound_data) other_msgs = self._post_data(outbound_encrypted) inbound_encrypted = self._get_data(other_msgs) @@ -207,9 +206,8 @@ class Initiator(Common): class Receiver(Common): - def __init__(self, appid, data, relay): + def __init__(self, appid, relay): self.appid = appid - self.data = data self.relay = relay assert self.relay.endswith("/") self.started = time.time() @@ -239,7 +237,7 @@ class Receiver(Common): idA=self.appid+":Initiator", idB=self.appid+":Receiver") - def get_data(self): + def get_data(self, outbound_data): assert self.code is not None assert self.channel_id is not None other_msgs = self._post_pake() @@ -248,7 +246,7 @@ class Receiver(Common): try: outbound_key = self.derive_key(b"receiver") - outbound_encrypted = self._encrypt_data(outbound_key, self.data) + outbound_encrypted = self._encrypt_data(outbound_key, outbound_data) other_msgs = self._post_data(outbound_encrypted) inbound_encrypted = self._get_data(other_msgs) diff --git a/src/wormhole/scripts/cmd_receive_file.py b/src/wormhole/scripts/cmd_receive_file.py index 23271e2..bf1f74d 100644 --- a/src/wormhole/scripts/cmd_receive_file.py +++ b/src/wormhole/scripts/cmd_receive_file.py @@ -10,21 +10,21 @@ def receive_file(args): # we're receiving transit_receiver = TransitReceiver(transit_relay=args.transit_helper) - mydata = json.dumps({ - "transit": { - "direct_connection_hints": transit_receiver.get_direct_hints(), - "relay_connection_hints": transit_receiver.get_relay_hints(), - }, - }).encode("utf-8") - r = Receiver(APPID, mydata, args.relay_url) + r = Receiver(APPID, args.relay_url) code = args.code if not code: code = r.input_code("Enter receive-file wormhole code: ", args.code_length) r.set_code(code) + mydata = json.dumps({ + "transit": { + "direct_connection_hints": transit_receiver.get_direct_hints(), + "relay_connection_hints": transit_receiver.get_relay_hints(), + }, + }).encode("utf-8") try: - data = json.loads(r.get_data().decode("utf-8")) + data = json.loads(r.get_data(mydata).decode("utf-8")) except WrongPasswordError as e: print("ERROR: " + e.explain(), file=sys.stderr) return 1 diff --git a/src/wormhole/scripts/cmd_receive_text.py b/src/wormhole/scripts/cmd_receive_text.py index f9ad196..d5b0e29 100644 --- a/src/wormhole/scripts/cmd_receive_text.py +++ b/src/wormhole/scripts/cmd_receive_text.py @@ -6,15 +6,15 @@ APPID = "lothar.com/wormhole/text-xfer" def receive_text(args): # we're receiving - data = json.dumps({"message": "ok"}).encode("utf-8") - r = Receiver(APPID, data, args.relay_url) + r = Receiver(APPID, args.relay_url) code = args.code if not code: code = r.input_code("Enter receive-text wormhole code: ", args.code_length) r.set_code(code) + data = json.dumps({"message": "ok"}).encode("utf-8") try: - them_bytes = r.get_data() + them_bytes = r.get_data(data) except WrongPasswordError as e: print("ERROR: " + e.explain(), file=sys.stderr) return 1 diff --git a/src/wormhole/scripts/cmd_send_file.py b/src/wormhole/scripts/cmd_send_file.py index 8bab63f..bc79527 100644 --- a/src/wormhole/scripts/cmd_send_file.py +++ b/src/wormhole/scripts/cmd_send_file.py @@ -12,6 +12,12 @@ def send_file(args): assert os.path.isfile(filename) transit_sender = TransitSender(transit_relay=args.transit_helper) + i = Initiator(APPID, args.relay_url) + code = i.get_code(args.code_length) + print("On the other computer, please run: wormhole receive-file") + print("Wormhole code is '%s'" % code) + print() + filesize = os.stat(filename).st_size data = json.dumps({ "file": { @@ -24,13 +30,8 @@ def send_file(args): }, }).encode("utf-8") - i = Initiator(APPID, data, args.relay_url) - code = i.get_code(args.code_length) - print("On the other computer, please run: wormhole receive-file") - print("Wormhole code is '%s'" % code) - print() try: - them_bytes = i.get_data() + them_bytes = i.get_data(data) except WrongPasswordError as e: print("ERROR: " + e.explain(), file=sys.stderr) return 1 diff --git a/src/wormhole/scripts/cmd_send_text.py b/src/wormhole/scripts/cmd_send_text.py index 0d106b5..9cc8795 100644 --- a/src/wormhole/scripts/cmd_send_text.py +++ b/src/wormhole/scripts/cmd_send_text.py @@ -9,13 +9,13 @@ def send_text(args): message = args.text data = json.dumps({"message": message, }).encode("utf-8") - i = Initiator(APPID, data, args.relay_url) + i = Initiator(APPID, args.relay_url) code = i.get_code(args.code_length) print("On the other computer, please run: wormhole receive-text") print("Wormhole code is: %s" % code) print("") try: - them_bytes = i.get_data() + them_bytes = i.get_data(data) except WrongPasswordError as e: print("ERROR: " + e.explain(), file=sys.stderr) return 1