diff --git a/examples/create_and_publish_identity.py b/examples/create_and_publish_identity.py index 0a7bcf234438571f826f1934bb318c0d6b1d5c79..2bd1fae18aead9d9033a36cf695a7f54589272aa 100644 --- a/examples/create_and_publish_identity.py +++ b/examples/create_and_publish_identity.py @@ -27,15 +27,16 @@ UID = "MyIdentity" # ( http://pythonhosted.org/aiohttp ) AIOHTTP_SESSION = aiohttp.ClientSession() -async def get_current_block(): +async def get_current_block(connection): """ Get the current block data + :param bma.api.ConnectionHandler connection: Connection handler + :rtype: dict """ # Here we request for the path blockchain/block/N - return await bma.blockchain.Current(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ - .get(AIOHTTP_SESSION) + return await bma.blockchain.Current(connection).get(AIOHTTP_SESSION) def get_identity_document(current_block, uid, salt, password): @@ -75,8 +76,11 @@ async def main(): """ Main code """ + # connection handler from BMA endpoint + connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler() + # capture current block to get version and currency and blockstamp - current_block = await get_current_block() + current_block = await get_current_block(connection) # load credentials from a text file salt, password = open(FROM_CREDENTIALS_FILE).readlines() @@ -89,7 +93,7 @@ async def main(): # send the identity document to the node data = {identity: identity.signed_raw()} - response = await bma.wot.Add(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()).post(AIOHTTP_SESSION, **data) + response = await bma.wot.Add(connection).post(AIOHTTP_SESSION, **data) print(response) diff --git a/examples/request_data.py b/examples/request_data.py index cbbc5a71fabd1b5c5daa920152d47f138e686ba4..b658eafc6d8de7bac30c316aefe9e15b0dcc3b7b 100644 --- a/examples/request_data.py +++ b/examples/request_data.py @@ -1,6 +1,5 @@ import asyncio import aiohttp -from aiohttp.client_reqrep import ClientResponse import duniterpy.api.bma as bma from duniterpy.documents import BMAEndpoint @@ -17,46 +16,24 @@ BMA_ENDPOINT = "BASIC_MERKLED_API cgeek.fr 9330" # ( http://pythonhosted.org/aiohttp ) AIOHTTP_SESSION = aiohttp.ClientSession() -async def get_summary_info(): - """ - Get the node info - - :rtype: ClientResponse - """ - # Here we request for the path /node/summary - return await bma.node.Summary(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()).get(AIOHTTP_SESSION) - -async def get_current_block(): - """ - Get the current block data - - :rtype: ClientResponse - """ - # Here we request for the path blockchain/current - return await bma.blockchain.Current(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ - .get(AIOHTTP_SESSION) - -async def get_block(block_number): - """ - Get the a block data - :param: int block_number Number of the block - - :rtype: ClientResponse - """ - # Here we request for the path blockchain/block/N - return await bma.blockchain.Block(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(), block_number)\ - .get(AIOHTTP_SESSION) - - async def main(): """ Main code """ - print(await get_summary_info()) + # connection handler from BMA endpoint + connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler() + + # Get the node summary infos + response = await bma.node.Summary(connection).get(AIOHTTP_SESSION) + print(response) - print(await get_current_block()) + # Get the current block data + response = await bma.blockchain.Current(connection).get(AIOHTTP_SESSION) + print(response) - print(await get_block(0)) + # Get the block number 0 + response = await bma.blockchain.Block(connection, 0).get(AIOHTTP_SESSION) + print(response) with AIOHTTP_SESSION: diff --git a/examples/save_revoke_document.py b/examples/save_revoke_document.py index 0731217b85d6800ae91b498d75826973ba7122ea..e47d94c90fb00b88f0bc1886c7d9b240a381b641 100644 --- a/examples/save_revoke_document.py +++ b/examples/save_revoke_document.py @@ -34,28 +34,29 @@ AIOHTTP_SESSION = aiohttp.ClientSession() # Current protocole version PROTOCOL_VERSION = 2 -async def get_current_block(): +async def get_current_block(connection): """ Get the current block data + :param bma.api.ConnectionHandler connection: Connection handler + :rtype: dict """ # Here we request for the path blockchain/current - return await bma.blockchain.Current(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ - .get(AIOHTTP_SESSION) + return await bma.blockchain.Current(connection).get(AIOHTTP_SESSION) -async def get_self_certification_document(currency, pubkey): +async def get_identity_document(connection, currency, pubkey): """ Get the SelfCertification document of the pubkey + :param bma.api.ConnectionHandler connection: Connection handler :param str currency: Currency name :param str pubkey: Public key :rtype: SelfCertification """ # Here we request for the path wot/lookup/pubkey - lookup_data = await bma.wot.Lookup(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(), pubkey)\ - .get(AIOHTTP_SESSION) + lookup_data = await bma.wot.Lookup(connection, pubkey).get(AIOHTTP_SESSION) # init vars uid = None @@ -83,38 +84,44 @@ async def get_self_certification_document(currency, pubkey): ) -async def get_revoke_document(self_certification, salt, password): +async def get_revoke_document(identity, salt, password): """ - Generate account revokation document for given identity + Generate account revocation document for given identity - :param SelfCertification self_certification: Self Certification of the identity + :param SelfCertification identity: Self Certification of the identity :param str salt: Salt :param str password: Password :return: the revokation document :rtype: duniterpy.documents.certification.Revocation """ - document = Revocation(PROTOCOL_VERSION, self_certification.currency, self_certification.pubkey, "") + document = Revocation(PROTOCOL_VERSION, identity.currency, identity.pubkey, "") key = SigningKey(salt, password) - document.sign(self_certification, [key]) - return document.signed_raw(self_certification) + document.sign(identity, [key]) + return document.signed_raw(identity) async def main(): """ Main code """ + # connection handler from BMA endpoint + connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler() + # capture current block to get currency name - current_block = await get_current_block() + current_block = await get_current_block(connection) # create our SelfCertification document to sign the revoke document - self_cert_document = await get_self_certification_document(current_block['currency'], PUBKEY) + identity_document = await get_identity_document(connection, current_block['currency'], PUBKEY) # load credentials from a text file salt, password = open(CREDENTIALS_FILE_PATH).readlines() + # cleanup newlines + salt, password = salt.strip(), password.strip() + # get the revoke document - revoke_document = await get_revoke_document(self_cert_document, salt, password) + revoke_document = await get_revoke_document(identity_document, salt, password) # save revoke document in a file fp = open(REVOKE_DOCUMENT_FILE_PATH, 'w') diff --git a/examples/send_certification.py b/examples/send_certification.py index 4f9812a922ab2609f31c712e2719594a60881f2b..9897138cd5c3ab4315a62216b13bf8ef6b485f00 100644 --- a/examples/send_certification.py +++ b/examples/send_certification.py @@ -30,28 +30,28 @@ TO_PUBKEY = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" # ( http://pythonhosted.org/aiohttp ) AIOHTTP_SESSION = aiohttp.ClientSession() -async def get_current_block(): +async def get_current_block(connection): """ Get the current block data + :param bma.api.ConnectionHandler connection: Connection handler :rtype: dict """ # Here we request for the path blockchain/block/N - return await bma.blockchain.Current(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ - .get(AIOHTTP_SESSION) + return await bma.blockchain.Current(connection).get(AIOHTTP_SESSION) -async def get_identity_document(current_block, pubkey): +async def get_identity_document(connection, current_block, pubkey): """ Get the identity document of the pubkey + :param bma.api.ConnectionHandler connection: Connection handler :param dict current_block: Current block data :param str pubkey: Public key :rtype: SelfCertification """ # Here we request for the path wot/lookup/pubkey - lookup_data = await bma.wot.Lookup(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(), pubkey)\ - .get(AIOHTTP_SESSION) + lookup_data = await bma.wot.Lookup(connection, pubkey).get(AIOHTTP_SESSION) # init vars uid = None @@ -110,11 +110,14 @@ async def main(): """ Main code """ + # connection handler from BMA endpoint + connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler() + # capture current block to get version and currency and blockstamp - current_block = await get_current_block() + current_block = await get_current_block(connection) # create our SelfCertification document to sign the Certification document - identity = await get_identity_document(current_block, TO_PUBKEY) + identity = await get_identity_document(connection, current_block, TO_PUBKEY) # load credentials from a text file salt, password = open(FROM_CREDENTIALS_FILE).readlines() @@ -127,8 +130,7 @@ async def main(): # Here we request for the path wot/certify data = {'cert': certification.signed_raw(identity)} - response = await bma.wot.Certify(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ - .post(AIOHTTP_SESSION, **data) + response = await bma.wot.Certify(connection).post(AIOHTTP_SESSION, **data) print(response) diff --git a/examples/send_membership.py b/examples/send_membership.py index b0d679e0ae7b23476a6f580df53621e992ea227a..cc31ec7c5337a2616a0287d1838a85c06df4e1c6 100644 --- a/examples/send_membership.py +++ b/examples/send_membership.py @@ -27,15 +27,15 @@ UID = "MyIdentity" # ( http://pythonhosted.org/aiohttp ) AIOHTTP_SESSION = aiohttp.ClientSession() -async def get_current_block(): +async def get_current_block(connection): """ Get the current block data + :param bma.api.ConnectionHandler connection: Connection handler :rtype: dict """ # Here we request for the path blockchain/block/N - return await bma.blockchain.Current(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ - .get(AIOHTTP_SESSION) + return await bma.blockchain.Current(connection).get(AIOHTTP_SESSION) def get_identity_document(current_block, uid, salt, password): @@ -112,8 +112,11 @@ async def main(): """ Main code """ + # connection handler from BMA endpoint + connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler() + # capture current block to get version and currency and blockstamp - current_block = await get_current_block() + current_block = await get_current_block(connection) # load credentials from a text file salt, password = open(FROM_CREDENTIALS_FILE).readlines() @@ -127,18 +130,9 @@ async def main(): # create a membership demand document membership = get_membership_document("IN", current_block, identity, salt, password) - # send the identity document to the node - data = {identity: identity.signed_raw()} - response = await bma.wot.Add(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler())\ - .post(AIOHTTP_SESSION, **data) - - print(response) - response.close() - # send the membership document to the node data = {membership: membership.signed_raw()} - response = await bma.blockchain.Membership(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler())\ - .post(AIOHTTP_SESSION, **data) + response = await bma.blockchain.Membership(connection).post(AIOHTTP_SESSION, **data) print(response) response.close() diff --git a/examples/send_transaction.py b/examples/send_transaction.py index c39d06e9779aea0463972146c709b5e69d6bfa77..496516c5ba132d0311d8b9cfa99744c16839f2af 100644 --- a/examples/send_transaction.py +++ b/examples/send_transaction.py @@ -37,27 +37,27 @@ AIOHTTP_SESSION = aiohttp.ClientSession() # Version of the transaction document TRANSACTION_VERSION = 3 -async def get_current_block(): +async def get_current_block(connection): """ Get the current block data + :param bma.api.ConnectionHandler connection: Connection handler :rtype: dict """ # Here we request for the path blockchain/block/N - return await bma.blockchain.Current(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ - .get(AIOHTTP_SESSION) + return await bma.blockchain.Current(connection).get(AIOHTTP_SESSION) -async def get_sources(pubkey): +async def get_sources(connection, pubkey): """ Get the current block data + :param bma.api.ConnectionHandler connection: Connection handler :param str pubkey: Public key of the sources account :rtype: dict """ # Here we request for the path blockchain/block/N - return await bma.tx.Sources(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(), pubkey) \ - .get(AIOHTTP_SESSION) + return await bma.tx.Sources(connection, pubkey).get(AIOHTTP_SESSION) def get_transaction_document(current_block, source, from_pubkey, to_pubkey): @@ -126,10 +126,13 @@ async def main(): """ Main code """ + # connection handler from BMA endpoint + connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler() + # capture current block to get version and currency and blockstamp - current_block = await get_current_block() + current_block = await get_current_block(connection) - response = await get_sources(FROM_PUBKEY) + response = await get_sources(connection, FROM_PUBKEY) if len(response['sources']) == 0: print("no sources found for account %s" % FROM_PUBKEY) @@ -155,8 +158,7 @@ async def main(): # send the Transaction document to the node data = {'transaction': transaction.signed_raw()} - response = await bma.tx.Process(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ - .post(AIOHTTP_SESSION, **data) + response = await bma.tx.Process(connection).post(AIOHTTP_SESSION, **data) print(response)