diff --git a/duniterpy/api/bma/api.py b/duniterpy/api/bma/api.py index 8d213cd60364a93643237241de3db9ac57af721d..47b577fd9e9d840c07d2bb9dc4713961f7a4e9f1 100644 --- a/duniterpy/api/bma/api.py +++ b/duniterpy/api/bma/api.py @@ -101,11 +101,12 @@ async def parse_response(response, schema): """ try: data = await response.json() + response.close() if schema is not None: jsonschema.validate(data, schema) return data - except (TypeError, json.decoder.JSONDecodeError): - raise jsonschema.ValidationError("Could not parse json") + except (TypeError, json.decoder.JSONDecodeError) as e: + raise jsonschema.ValidationError("Could not parse json : {0}".format(str(e))) class API(object): diff --git a/duniterpy/api/bma/blockchain.py b/duniterpy/api/bma/blockchain.py index d5bfb3357836b78b0a8af9978c15775057085dff..e4cb26a0f447a2946f870d3b58ee89b259ac909f 100644 --- a/duniterpy/api/bma/blockchain.py +++ b/duniterpy/api/bma/blockchain.py @@ -326,7 +326,7 @@ async def membership(connection, membership): :param duniterpy.api.bma.ConnectionHandler connection: Connection handler instance :param str membership: Membership signed raw document - :rtype: dict + :rtype: aiohttp.ClientResponse """ client = API(connection, URL_PATH) @@ -350,7 +350,8 @@ async def block(connection, number=0, block=None, signature=None): # GET block r = await client.requests_get('/block/%d' % number) - return await parse_response(r, BLOCK_SCHEMA) + data = await parse_response(r, BLOCK_SCHEMA) + return data async def current(connection): """ diff --git a/duniterpy/api/bma/network.py b/duniterpy/api/bma/network.py index 66887671e567e00c004c65cfa06cf142b1b293fd..b8a63998decce8ded87302f445880cc7d2536212 100644 --- a/duniterpy/api/bma/network.py +++ b/duniterpy/api/bma/network.py @@ -16,7 +16,7 @@ # Caner Candan <caner@candan.fr>, http://caner.candan.fr # -from duniterpy.api.bma import API, logging +from duniterpy.api.bma import API, logging, parse_response logger = logging.getLogger("duniter/network") @@ -110,7 +110,7 @@ async def peering(connection): client = API(connection, URL_PATH) r = await client.requests_get('/peering') - return await client.parse_response(r, PEERING_SCHEMA) + return await parse_response(r, PEERING_SCHEMA) async def peers(connection, entry=None, signature=None): """ @@ -130,7 +130,7 @@ async def peers(connection, entry=None, signature=None): # GET Peers r = await client.requests_get('/peering/peers') - return await client.parse_response(r, PEERS_SCHEMA) + return await parse_response(r, PEERS_SCHEMA) # async def status(connection): # """ diff --git a/duniterpy/api/bma/node.py b/duniterpy/api/bma/node.py index cd48e1b1e5ecc18374bc30510651c82fa9b17c62..c6b26411af8b4a047ae0d2fa8e57bad2df77f0bf 100644 --- a/duniterpy/api/bma/node.py +++ b/duniterpy/api/bma/node.py @@ -16,7 +16,7 @@ # Caner Candan <caner@candan.fr>, http://caner.candan.fr # -from duniterpy.api.bma import API, logging +from duniterpy.api.bma import API, logging, parse_response logger = logging.getLogger("duniter/node") @@ -53,4 +53,4 @@ async def summary(connection): client = API(connection, URL_PATH) r = await client.requests_get('/summary') - return await client.parse_response(r, schema) + return await parse_response(r, schema) diff --git a/examples/create_and_publish_identity.py b/examples/create_and_publish_identity.py index c783766744625dc5f39eb9c539c128e4aefe72ef..164fe5dce56a5a42b5ae93dbe1fe1ea94560b5ee 100644 --- a/examples/create_and_publish_identity.py +++ b/examples/create_and_publish_identity.py @@ -1,5 +1,6 @@ import asyncio import aiohttp +import getpass import duniterpy.api.bma as bma from duniterpy.documents import BMAEndpoint, BlockUID, Identity @@ -13,16 +14,10 @@ from duniterpy.key import SigningKey # Here we use the BASIC_MERKLED_API BMA_ENDPOINT = "BASIC_MERKLED_API cgeek.fr 9330" -# Credentials should be prompted or kept in a separate secure file -# create a file with the salt on the first line and the password on the second line -# the script will load them from the file -FROM_CREDENTIALS_FILE = "/home/username/.credentials.txt" - # Your unique identifier in the Web of Trust UID = "MyIdentity" ################################################ - # Latest duniter-python-api is asynchronous and you have to create an aiohttp session to send request # ( http://pythonhosted.org/aiohttp ) AIOHTTP_SESSION = aiohttp.ClientSession() @@ -65,30 +60,31 @@ async def main(): """ Main code """ - # connection handler from BMA endpoint - connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler() + # connection handler from BMA endpoint + connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION) # capture current block to get version and currency and blockstamp current_block = await bma.blockchain.current(connection) - # load credentials from a text file - salt, password = open(FROM_CREDENTIALS_FILE).readlines() + # prompt hidden user entry + salt = getpass.getpass("Enter your passphrase (salt): ") - # cleanup newlines - salt, password = salt.strip(), password.strip() + # prompt hidden user entry + password = getpass.getpass("Enter your password: ") # create our signed identity document identity = get_identity_document(current_block, UID, salt, password) # send the identity document to the node response = await bma.wot.add(connection, identity.signed_raw()) - - print(response) - + if response.status == 200: + print(await response.text()) + else: + print("Error while publishing identity : {0}".format(response.text())) response.close() -with AIOHTTP_SESSION: +# 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 ) - # 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 ) +with AIOHTTP_SESSION: asyncio.get_event_loop().run_until_complete(main()) diff --git a/examples/create_public_key.py b/examples/create_public_key.py index ea7ab135da0af81d2d6e7bde5a010075f897f844..8ef741e1b1eb0511877dc39d482d7f9548f464ed 100644 --- a/examples/create_public_key.py +++ b/examples/create_public_key.py @@ -1,25 +1,15 @@ +import getpass from duniterpy.key import SigningKey -# CONFIG ####################################### - -# CREDENTIALS in Duniter are a couple of strings: -# - A secret pass-phrase -# - A password -# They create a seed which create keys (some are private and one is public) - -# Credentials should be prompted or kept in a separate secure file -# create a file with the salt on the first line and the password on the second line -# the script will load them from the file -CREDENTIALS_FILE_PATH = "/home/username/.credentials.txt" - ################################################ if __name__ == '__main__': - # Load your credentials from a text file - salt, password = open(CREDENTIALS_FILE_PATH).readlines() - # Cleanup newlines - salt, password = salt.strip(), password.strip() + # prompt hidden user entry + salt = getpass.getpass("Enter your passphrase (salt): ") + + # prompt hidden user entry + password = getpass.getpass("Enter your password: ") # Create key object key = SigningKey(salt, password) diff --git a/examples/save_revoke_document.py b/examples/save_revoke_document.py index ce1a4d325814e95043d6e45d5be3a328a31add6c..589a8188dc3dc230e3329095de03227e39baf9b7 100644 --- a/examples/save_revoke_document.py +++ b/examples/save_revoke_document.py @@ -29,9 +29,6 @@ BMA_ENDPOINT = "BASIC_MERKLED_API cgeek.fr 9330" REVOKE_DOCUMENT_FILE_PATH = os.path.join(home_path, "duniter_account_revoke_document.txt") ################################################ - -# Latest duniter-python-api is asynchronous and you have to create an aiohttp session to send request -# ( http://pythonhosted.org/aiohttp ) AIOHTTP_SESSION = aiohttp.ClientSession() # Current protocole version @@ -76,7 +73,7 @@ async def get_identity_document(connection, currency, pubkey): ) -async def get_revoke_document(identity, salt, password): +def get_revoke_document(identity, salt, password): """ Generate account revocation document for given identity @@ -115,8 +112,7 @@ async def main(): exit(0) # connection handler from BMA endpoint - connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler() - + connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION) # capture current block to get currency name current_block = await bma.blockchain.current(connection) @@ -124,7 +120,7 @@ async def main(): identity_document = await get_identity_document(connection, current_block['currency'], pubkey) # get the revoke document - revoke_document = await get_revoke_document(identity_document, salt, password) + revoke_document = get_revoke_document(identity_document, salt, password) # save revoke document in a file fp = open(REVOKE_DOCUMENT_FILE_PATH, 'w') @@ -134,8 +130,8 @@ async def main(): # document saved print("Revoke document saved in %s" % REVOKE_DOCUMENT_FILE_PATH) -with AIOHTTP_SESSION: +with AIOHTTP_SESSION: # 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()) diff --git a/examples/send_certification.py b/examples/send_certification.py index 42d890c21f0c86df0bdf2de95ca5cc9c14f6f7b2..bb9b57f8a879114b4c3496559560751a970dbee8 100644 --- a/examples/send_certification.py +++ b/examples/send_certification.py @@ -1,5 +1,6 @@ import asyncio import aiohttp +import getpass import duniterpy.api.bma as bma from duniterpy.documents import BMAEndpoint, BlockUID, Identity, Certification from duniterpy.key import SigningKey @@ -100,7 +101,7 @@ async def main(): Main code """ # connection handler from BMA endpoint - connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler() + connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION) # capture current block to get version and currency and blockstamp current_block = await bma.blockchain.current(connection) @@ -108,11 +109,11 @@ async def main(): # create our SelfCertification document to sign the Certification document identity = await get_identity_document(connection, current_block, TO_PUBKEY) - # load credentials from a text file - salt, password = open(FROM_CREDENTIALS_FILE).readlines() + # prompt hidden user entry + salt = getpass.getpass("Enter your passphrase (salt): ") - # cleanup newlines - salt, password = salt.strip(), password.strip() + # prompt hidden user entry + password = getpass.getpass("Enter your password: ") # send the Certification document to the node certification = get_certification_document(current_block, identity, FROM_PUBKEY, salt, password) @@ -120,8 +121,10 @@ async def main(): # Here we request for the path wot/certify response = await bma.wot.certify(connection, certification.signed_raw(identity)) - print(response) - + if response.status_code == 200: + print(await response.text()) + else: + print("Error while publishing certification : {0}".format(response.text())) response.close() with AIOHTTP_SESSION: diff --git a/examples/send_membership.py b/examples/send_membership.py index 171a35e92239390656369930dc234eaeb2926612..f3ca5be10da283def158c46e987fdaa19a1d4f1f 100644 --- a/examples/send_membership.py +++ b/examples/send_membership.py @@ -1,5 +1,6 @@ import asyncio import aiohttp +import getpass import duniterpy.api.bma as bma from duniterpy.documents import BMAEndpoint, BlockUID, Identity, Membership @@ -103,16 +104,16 @@ async def main(): Main code """ # connection handler from BMA endpoint - connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler() + connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION) # capture current block to get version and currency and blockstamp current_block = await bma.blockchain.current(connection) - # load credentials from a text file - salt, password = open(FROM_CREDENTIALS_FILE).readlines() + # prompt hidden user entry + salt = getpass.getpass("Enter your passphrase (salt): ") - # cleanup newlines - salt, password = salt.strip(), password.strip() + # prompt hidden user entry + password = getpass.getpass("Enter your password: ") # create our signed identity document identity = get_identity_document(current_block, UID, salt, password) @@ -123,7 +124,11 @@ async def main(): # send the membership document to the node response = await bma.blockchain.membership(connection, membership.signed_raw()) - print(response) + if response.status == 200: + print(await response.text()) + else: + print("Error while publishing membership : {0}".format(response.text())) + response.close() diff --git a/examples/send_transaction.py b/examples/send_transaction.py index 2bb237e8e7dd5b783e337a0756a351f23d2153c6..4facb78615d6ce688dcc7fdf6cc5ac603bb85020 100644 --- a/examples/send_transaction.py +++ b/examples/send_transaction.py @@ -1,8 +1,8 @@ import asyncio - +import getpass import aiohttp -import duniterpy.api.bma as bma +from duniterpy.api import bma from duniterpy.documents import BMAEndpoint, BlockUID, Transaction from duniterpy.documents.transaction import InputSource, OutputSource, Unlock, SIGParameter from duniterpy.grammars.output import Condition, SIG @@ -105,7 +105,7 @@ async def main(): Main code """ # connection handler from BMA endpoint - connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler() + connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION) # capture current block to get version and currency and blockstamp current_block = await bma.blockchain.current(connection) @@ -138,10 +138,13 @@ async def main(): # send the Transaction document to the node response = await bma.tx.process(connection, transaction.signed_raw()) - print(response) - + if response.status == 200: + print(await response.text()) + else: + print("Error while publishing transaction : {0}".format(response.text())) response.close() + with AIOHTTP_SESSION: # Latest duniter-python-api is asynchronous and you have to use asyncio, an asyncio loop and a "as" on the data.