From bb015a71bd33bc7a698f50c5e99ac276263fd9d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pascal=20Eng=C3=A9libert?= <tuxmain@zettascript.org>
Date: Tue, 21 May 2019 19:55:31 +0200
Subject: [PATCH] Install doc in README & type doc in utils.py

---
 README.md | 36 +++++++++++++++++-------------------
 utils.py  | 34 +++++++++++++++++-----------------
 2 files changed, 34 insertions(+), 36 deletions(-)

diff --git a/README.md b/README.md
index 9d08254..a882c5a 100644
--- a/README.md
+++ b/README.md
@@ -3,32 +3,22 @@
 ÄžMixer is a Duniter currency mixer to anonymize transactions. This implementation can 
 run in an onion network, guaranteeing a strong anonymity.
 
-## Contribute
-
-There is a lot of things to do. You also can create a client, for exemple in JS.
-
-It would be great to have a logo (maybe with a mixer blade and an onion in a Äž), and schemas to explain the protocol.
-
 ## How to install it
 
-You can install the gmixer with the script install_gmixer.sh (works on Debian derivated system for the moment)
+### ➤ Debian (Ubuntu, Mint, etc.)
 
+    sh install_gmixer.sh
 
-## List of gmixer instances (real time updated)
+### ➤ Archlinux
 
-You will find a list of gmixer instances here :
+    sudo pacman -S python python-pip leveldb python-plyvel libsodium
+    sudo pip install --upgrade libnacl duniterpy silkaj py-ubjson plyvel PySocks
 
-[List 1 of gmixer instances](http://zettascript.org/tux/g1/gmixer-webui.html)
-
-[List 2 of gmixer instances](http://185.193.38.231/gmixer-instances.html)
-
-## How to use it
+### ➤ Other Linux
 
-Install dependances:
+Install dependencies:
 
-	sudo cp lib/libleveldb.a /usr/lib/libleveldb.a
-
-`libleveldb-dev=>1.20` is required for `plyvel`. You can copy it from this repository with the command above. You can also build it: If you can only get an older version with your package manager, [build it by hand](https://github.com/google/leveldb/releases). (currently it it the case for Debian Stretch, not for Archlinux)
+`libleveldb-dev=>1.20` is required for `plyvel`. If you can only get an older version with your package manager, [build it by hand](https://github.com/google/leveldb/releases). (currently it it the case for Debian Stretch, not for Archlinux)
 
 Install the following packages: `libleveldb-dev` `libsodium-dev` `python3` `python3-pip`
 
@@ -38,6 +28,8 @@ Install the following packages: `libleveldb-dev` `libsodium-dev` `python3` `pyth
     # If Python < 3.6:
     sudo pip3 install python2_secrets
 
+## How to use it
+
 Create config & data dir: (change `~/.gmixer-g1`, so you can run different 
 servers with different configs) (note that each server must have different dir)
 
@@ -46,13 +38,17 @@ servers with different configs) (note that each server must have different dir)
 Edit config and set your values. If you are not using any proxy, `bind_` and `public_` addresses should be the same.
 If `salt` or `password` are empty, they will be asked at runtime. For security reasons, it is not a good idea to write them in commandline.
 
+The server must know other nodes on the network. You can find here the list refreshed in real-time and the content to put into your `peers` file:
+ * [ÄžMixer web client](http://zettascript.org/tux/g1/gmixer-webclient/)
+ * [List of ÄžMixer instances](http://185.193.38.231/gmixer-instances.html)
+
 Now, start the server:
 
     python3 server.py -s
     # Use -d if using a different dir than default
     # Use -P for automatic fetch of public IP address
 
-### Proxy
+### Configure a proxy
 
 To use a SOCKS5 proxy for client connections, set the `client.proxy` value in `config.json` as following:
 
@@ -88,6 +84,8 @@ Example:
 
 ### HTTP API
 
+**Warning**: This doc is outdated!
+
 URL is used like commandline arguments. Arguments are separated by slashes. Different commands can be set in the same request.
 
 Command: **/list** list known peers (excluding itself)
diff --git a/utils.py b/utils.py
index d28ee76..341f073 100644
--- a/utils.py
+++ b/utils.py
@@ -28,18 +28,18 @@ sys.argv = _argv
 #-------- DATA
 
 # Decode binary int (Big Endian)
-def bin_to_int(b):
+def bin_to_int(b:bytes) -> int:
 	return sum([b[i]*256**i for i in range(len(b))])
 
 # Encode int into bin (Big Endian)
 # n: number ; b: bytes
-def int_to_bin(n, b=4):
+def int_to_bin(n:int, b:int=4) -> bytes:
 	return bytes([(n>>(i*8))&255 for i in range(b)])
 
-def gen_keys():
+def gen_keys() -> (str, str):
 	return secrets.token_urlsafe(), secrets.token_urlsafe()
 
-def gen_comment(seeds):
+def gen_comment(seeds:[bytes, bytes, bytes]) -> str:
 	return socks.b64encode(hashlib.sha512(b"".join(seeds)).digest()).decode()
 
 #-------- NETWORK
@@ -48,7 +48,7 @@ RECBUF = 1024
 
 p_clen = re.compile("\r?\ncontent-length: *(\d+)\r?\n?", re.IGNORECASE)
 
-def sdata(host, mode, url="/", data=b"", uagent="GMixer-py", proxy=None, proxy_onion_only=False):
+def sdata(host:(str, int), mode:str, url:str="/", data:bytes=b"", uagent:str="GMixer-py", proxy:(str, int)=None, proxy_onion_only:bool=False) -> (bytes, bytes):
 	tsocket = socket.socket
 	if proxy and (not proxy_onion_only or re.match("^.+\.onion$", host[0])):
 		socks.set_default_proxy(socks.PROXY_TYPE_SOCKS5, proxy[0], proxy[1])
@@ -109,11 +109,11 @@ LOG_ERROR = 8
 LOGMSG_TYPES = {LOG_INFO:"\033[96minfo\033[0m", LOG_TRACE:"\033[39mtrace\033[0m", LOG_WARN:"\033[93mwarn\033[0m", LOG_ERROR:"\033[91merror\033[0m"}
 VERBOSITY = LOG_INFO | LOG_WARN | LOG_ERROR
 
-def logPrint(msg, msgtype):
+def logPrint(msg:str, msgtype:int):
 	if msgtype & VERBOSITY:
 		print(time.strftime("%Y-%m-%d %H:%M:%S")+" ["+LOGMSG_TYPES[msgtype]+"] "+msg)
 
-def getargv(arg, default="", n=1, args=sys.argv):
+def getargv(arg:str, default:str="", n:int=1, args:list=sys.argv) -> str:
 	if arg in args and len(args) > args.index(arg)+n:
 		return args[args.index(arg)+n]
 	else:
@@ -123,7 +123,7 @@ def getargv(arg, default="", n=1, args=sys.argv):
 #-------- ÄžMixer
 
 class Peer:
-	def __init__(self, pubkey, host, port, up):
+	def __init__(self, pubkey:str, host:str, port:int, up:bool):
 		self.pubkey = pubkey
 		self.host = host
 		self.port = int(port)
@@ -131,20 +131,20 @@ class Peer:
 		
 		self.keys = PublicKey(pubkey)
 	
-	def __str__(self):
+	def __str__(self) -> str:
 		return self.pubkey[:8] + "@" + self.host + ":" + str(self.port)
 	
-	def export_str(self):
+	def export_str(self) -> str:
 		return self.pubkey + " " + self.host + " " + str(self.port)
 
 # Read peers list
-def readPeers(dir):
-	if not os.path.isfile(dir+"/peers"):
-		open(dir+"/peers", "w").close()
+def readPeers(cdir:str) -> (list, dict):
+	if not os.path.isfile(cdir+"/peers"):
+		open(cdir+"/peers", "w").close()
 	
 	peers = []
 	peers_index = {}
-	peersfile = open(dir+"/peers", "r")
+	peersfile = open(cdir+"/peers", "r")
 	while True:
 		line = peersfile.readline()
 		if len(line) <= 1:
@@ -158,13 +158,13 @@ def readPeers(dir):
 	return peers, peers_index
 
 # Save peers list
-def writePeers(dir, peers):
-	peersfile = open(dir+"/peers", "w")
+def writePeers(cdir:str, peers:list):
+	peersfile = open(cdir+"/peers", "w")
 	for peer in peers:
 		peersfile.write(peer.export_str()+"\n")
 	peersfile.close()
 
-def sendTransaction(sender_keys, receiver_pubkey, amount, comment):
+def sendTransaction(sender_keys:SigningKey, receiver_pubkey:str, amount:int, comment:str):
 	sender_amount = silkaj.money.get_amount_from_pubkey(sender_keys.pubkey)[0]
 	assert sender_amount >= amount, "not enough money"
 	
-- 
GitLab