Skip to content
Snippets Groups Projects
Commit cf522e26 authored by Moul's avatar Moul
Browse files

[mod] #160: Define functions in examples

The function names are based on the files' names
Pylint: Use unused variables
Allow to pass files path in f() prompt as arg
- Reduce usage message
- Add missing exits
parent d0d74e92
No related branches found
No related tags found
No related merge requests found
Showing
with 196 additions and 167 deletions
......@@ -20,7 +20,7 @@ from duniterpy.key import SigningKey
################################################
if __name__ == "__main__":
def create_public_key():
# prompt hidden user entry
salt = getpass.getpass("Enter your passphrase (salt): ")
......@@ -32,3 +32,7 @@ if __name__ == "__main__":
# Display your public key
print("Public key for your credentials: %s" % key.pubkey)
if __name__ == "__main__":
create_public_key()
......@@ -37,7 +37,7 @@ CURRENCY = "g1-test"
################################################
def main():
def listen_ws2p():
"""
Main code
"""
......@@ -101,4 +101,4 @@ def main():
if __name__ == "__main__":
main()
listen_ws2p()
......@@ -20,15 +20,12 @@ import sys
from duniterpy.key import SigningKey
if __name__ == "__main__":
def load_binary_encrypted_message(signed_message_path=None):
if not signed_message_path:
if len(sys.argv) < 2:
print(
"""
Usage:
python decrypt_message.py ENCRYPTED_MESSAGE_FILEPATH
"""
)
print("Usage: python decrypt_message.py ENCRYPTED_MESSAGE_FILEPATH")
sys.exit(1)
# capture encrypted message filepath argument
signed_message_path = sys.argv[1]
......@@ -54,3 +51,6 @@ if __name__ == "__main__":
message = str(error)
print(message)
if __name__ == "__main__":
load_binary_encrypted_message()
......@@ -19,15 +19,12 @@ import sys
from duniterpy.key import VerifyingKey
if __name__ == "__main__":
def load_binary_signed_message(signed_message_path=None):
if not signed_message_path:
if len(sys.argv) < 2:
print(
"""
Usage:
python verify_signed_message.py SIGNED_MESSAGE_FILEPATH
"""
)
print("Usage: python verify_signed_message.py SIGNED_MESSAGE_FILEPATH")
sys.exit(1)
# capture signed message filepath argument
signed_message_path = sys.argv[1]
......@@ -48,3 +45,7 @@ if __name__ == "__main__":
message = str(error)
print(message)
if __name__ == "__main__":
load_binary_signed_message()
......@@ -23,7 +23,7 @@ CLEARTEXT_AA_MESSAGE_PATH = "/tmp/duniter_cleartext_aa_message.txt"
################################################
if __name__ == "__main__":
def load_cleartext_ascii_armor_message():
# Ask public key of the issuer
pubkeyBase58 = input("Enter public key of the message issuer: ")
......@@ -44,3 +44,7 @@ if __name__ == "__main__":
+ "----------------------------------"
)
print(result)
if __name__ == "__main__":
load_cleartext_ascii_armor_message()
......@@ -19,7 +19,7 @@ import sys
from duniterpy.key import SigningKey
if __name__ == "__main__":
def load_credentials_file():
if len(sys.argv) < 2:
print("Usage: python load_credentials_file.py FILEPATH")
sys.exit(1)
......@@ -34,3 +34,7 @@ if __name__ == "__main__":
# print pubkey
print("Public key from credentials file: {}".format(signing_key_instance.pubkey))
if __name__ == "__main__":
load_credentials_file()
......@@ -25,7 +25,7 @@ ENCRYPTED_AA_MESSAGE_PATH = "/tmp/duniter_aa_encrypted_message.txt"
################################################
if __name__ == "__main__":
def load_encrypted_ascii_armor_message():
# Ask public key of the recipient
pubkeyBase58 = input("Enter public key of the message issuer: ")
......@@ -49,3 +49,7 @@ if __name__ == "__main__":
)
print(AsciiArmor.parse(ascii_armor_block, signing_key, [pubkeyBase58]))
if __name__ == "__main__":
load_encrypted_ascii_armor_message()
......@@ -21,6 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
from duniterpy.helpers.blockchain import load
def load_local_blockchain():
bc = load() # gets blockchain iterator
b = next(bc) # gets block
print(f"first block number is: {b.number}") # should return 0
......@@ -28,3 +29,6 @@ print(f"first block number is: {b.number}") # should return 0
print(f"second block number is: {next(bc).number}") # should return 1
print(f"third block number is: {next(bc).number}") # should return 2
# (and so on)
if __name__ == "__main__":
load_local_blockchain()
......@@ -19,14 +19,11 @@ import sys
from duniterpy.key import SigningKey
if __name__ == "__main__":
def load_scuttlebutt_file():
if len(sys.argv) < 2:
print("Usage: python load_scuttlebutt_file.py FILEPATH")
sys.exit(1)
# capture filepath argument
scuttlebutt_filepath = sys.argv[1]
# create SigningKey instance from file
signing_key_instance = SigningKey.from_ssb_file(
scuttlebutt_filepath
......@@ -34,3 +31,7 @@ if __name__ == "__main__":
# print pubkey
print("Public key from scuttlebutt file: {}".format(signing_key_instance.pubkey))
if __name__ == "__main__":
load_scuttlebutt_file()
......@@ -29,7 +29,7 @@ BMAS_ENDPOINT = "BMAS g1-test.duniter.org 443"
################################################
def main():
def request_available_nodes():
"""
Main code
"""
......@@ -49,4 +49,4 @@ def main():
if __name__ == "__main__":
main()
request_available_nodes()
......@@ -29,7 +29,7 @@ BMAS_ENDPOINT = "BMAS g1-test.duniter.org 443"
################################################
def main():
def request_data():
"""
Main code (synchronous requests)
"""
......@@ -80,4 +80,4 @@ def main():
if __name__ == "__main__":
main()
request_data()
......@@ -39,7 +39,7 @@ async def coroutine(function, *args, **kwargs):
return function(*args, **kwargs)
async def main():
async def request_data_async():
"""
Main code (asynchronous requests)
......@@ -72,4 +72,5 @@ async def main():
# Latest duniter-python-api is asynchronous and you have to use asyncio, an asyncio loop and a "as" on the data.
# ( https://docs.python.org/3/library/asyncio.html )
asyncio.get_event_loop().run_until_complete(main())
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(request_data_async())
......@@ -32,7 +32,7 @@ ES_USER_ENDPOINT = "ES_USER_API g1-test.data.duniter.fr 443"
################################################
def main():
def request_data_elasticsearch():
"""
Main code (synchronous requests)
"""
......@@ -64,4 +64,4 @@ def main():
if __name__ == "__main__":
main()
request_data_elasticsearch()
......@@ -16,7 +16,7 @@ GVA_ENDPOINT = "GVA S g1.librelois.fr 443 gva"
################################################
def main():
def request_data_graphql():
client = Client(GVA_ENDPOINT)
# get query to get schema from api
......@@ -56,4 +56,4 @@ def main():
if __name__ == "__main__":
main()
request_data_graphql()
......@@ -33,7 +33,7 @@ BMAS_ENDPOINT = "BMAS g1-test.duniter.org 443"
################################################
def main():
def request_web_socket_block():
"""
Main code
"""
......@@ -61,4 +61,4 @@ def main():
if __name__ == "__main__":
main()
request_web_socket_block()
......@@ -79,7 +79,7 @@ def send(ws: WSConnection, request: str, request_id: str, schema: dict) -> Any:
return response
def main():
def request_ws2p():
"""
Main code
"""
......@@ -169,4 +169,4 @@ def main():
if __name__ == "__main__":
main()
request_ws2p()
......@@ -38,6 +38,7 @@ PRIVATE_KEYS_FILE_PATH = os.path.join(home_path, ".duniter_account_private_keys.
################################################
def save_and_load_private_key_file():
# prompt hidden user entry
salt = getpass.getpass("Enter your passphrase (salt): ")
......@@ -65,6 +66,7 @@ print("Private keys for public key %s saved in %s" % (pubkey, PRIVATE_KEYS_FILE_
loaded_signer = SigningKey.from_private_key(PRIVATE_KEYS_FILE_PATH) # type: SigningKey
# check public key from file
print("Public key %s loaded from file %s" % (pubkey, PRIVATE_KEYS_FILE_PATH))
print("Public key %s loaded from file %s" % (loaded_signer.pubkey, PRIVATE_KEYS_FILE_PATH))
sys.exit(0)
if __name__ == "__main__":
save_and_load_private_key_file()
......@@ -38,6 +38,7 @@ PRIVATE_KEY_FILE_PATH = os.path.join(home_path, ".duniter_account_ewif_v1.dunite
################################################
def save_and_load_private_key_file_ewif():
# prompt hidden user entry
salt = getpass.getpass("Enter your passphrase (salt): ")
......@@ -82,5 +83,5 @@ except IOError as error:
print(error)
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
save_and_load_private_key_file_ewif()
......@@ -38,6 +38,8 @@ PRIVATE_KEY_FILE_PATH = os.path.join(home_path, ".duniter_account_pubsec_v1.duni
################################################
def save_and_load_private_key_file_pubsec():
# prompt hidden user entry
salt = getpass.getpass("Enter your passphrase (salt): ")
......@@ -77,5 +79,5 @@ except IOError as error:
print(error)
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
save_and_load_private_key_file_pubsec()
......@@ -38,6 +38,7 @@ PRIVATE_KEY_FILE_PATH = os.path.join(home_path, ".duniter_account_wif_v1.duniter
################################################
def save_and_load_private_key_file_wif():
# prompt hidden user entry
salt = getpass.getpass("Enter your passphrase (salt): ")
......@@ -77,5 +78,5 @@ except IOError as error:
print(error)
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
save_and_load_private_key_file_wif()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment