Skip to content
Snippets Groups Projects
Commit 9af35190 authored by Vincent Texier's avatar Vincent Texier
Browse files

Simplify examples

parent ac4902e9
No related branches found
No related tags found
1 merge request!30Examples scripts
...@@ -27,15 +27,16 @@ UID = "MyIdentity" ...@@ -27,15 +27,16 @@ UID = "MyIdentity"
# ( http://pythonhosted.org/aiohttp ) # ( http://pythonhosted.org/aiohttp )
AIOHTTP_SESSION = aiohttp.ClientSession() AIOHTTP_SESSION = aiohttp.ClientSession()
async def get_current_block(): async def get_current_block(connection):
""" """
Get the current block data Get the current block data
:param bma.api.ConnectionHandler connection: Connection handler
:rtype: dict :rtype: dict
""" """
# Here we request for the path blockchain/block/N # Here we request for the path blockchain/block/N
return await bma.blockchain.Current(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ return await bma.blockchain.Current(connection).get(AIOHTTP_SESSION)
.get(AIOHTTP_SESSION)
def get_identity_document(current_block, uid, salt, password): def get_identity_document(current_block, uid, salt, password):
...@@ -75,8 +76,11 @@ async def main(): ...@@ -75,8 +76,11 @@ async def main():
""" """
Main code 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 # 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 # load credentials from a text file
salt, password = open(FROM_CREDENTIALS_FILE).readlines() salt, password = open(FROM_CREDENTIALS_FILE).readlines()
...@@ -89,7 +93,7 @@ async def main(): ...@@ -89,7 +93,7 @@ async def main():
# send the identity document to the node # send the identity document to the node
data = {identity: identity.signed_raw()} 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) print(response)
......
import asyncio import asyncio
import aiohttp import aiohttp
from aiohttp.client_reqrep import ClientResponse
import duniterpy.api.bma as bma import duniterpy.api.bma as bma
from duniterpy.documents import BMAEndpoint from duniterpy.documents import BMAEndpoint
...@@ -17,46 +16,24 @@ BMA_ENDPOINT = "BASIC_MERKLED_API cgeek.fr 9330" ...@@ -17,46 +16,24 @@ BMA_ENDPOINT = "BASIC_MERKLED_API cgeek.fr 9330"
# ( http://pythonhosted.org/aiohttp ) # ( http://pythonhosted.org/aiohttp )
AIOHTTP_SESSION = aiohttp.ClientSession() 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(): async def main():
""" """
Main code 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: with AIOHTTP_SESSION:
......
...@@ -34,28 +34,29 @@ AIOHTTP_SESSION = aiohttp.ClientSession() ...@@ -34,28 +34,29 @@ AIOHTTP_SESSION = aiohttp.ClientSession()
# Current protocole version # Current protocole version
PROTOCOL_VERSION = 2 PROTOCOL_VERSION = 2
async def get_current_block(): async def get_current_block(connection):
""" """
Get the current block data Get the current block data
:param bma.api.ConnectionHandler connection: Connection handler
:rtype: dict :rtype: dict
""" """
# Here we request for the path blockchain/current # Here we request for the path blockchain/current
return await bma.blockchain.Current(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ return await bma.blockchain.Current(connection).get(AIOHTTP_SESSION)
.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 Get the SelfCertification document of the pubkey
:param bma.api.ConnectionHandler connection: Connection handler
:param str currency: Currency name :param str currency: Currency name
:param str pubkey: Public key :param str pubkey: Public key
:rtype: SelfCertification :rtype: SelfCertification
""" """
# Here we request for the path wot/lookup/pubkey # Here we request for the path wot/lookup/pubkey
lookup_data = await bma.wot.Lookup(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(), pubkey)\ lookup_data = await bma.wot.Lookup(connection, pubkey).get(AIOHTTP_SESSION)
.get(AIOHTTP_SESSION)
# init vars # init vars
uid = None uid = None
...@@ -83,38 +84,44 @@ async def get_self_certification_document(currency, pubkey): ...@@ -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 salt: Salt
:param str password: Password :param str password: Password
:return: the revokation document :return: the revokation document
:rtype: duniterpy.documents.certification.Revocation :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) key = SigningKey(salt, password)
document.sign(self_certification, [key]) document.sign(identity, [key])
return document.signed_raw(self_certification) return document.signed_raw(identity)
async def main(): async def main():
""" """
Main code Main code
""" """
# connection handler from BMA endpoint
connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()
# capture current block to get currency name # 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 # 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 # load credentials from a text file
salt, password = open(CREDENTIALS_FILE_PATH).readlines() salt, password = open(CREDENTIALS_FILE_PATH).readlines()
# cleanup newlines
salt, password = salt.strip(), password.strip()
# get the revoke document # 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 # save revoke document in a file
fp = open(REVOKE_DOCUMENT_FILE_PATH, 'w') fp = open(REVOKE_DOCUMENT_FILE_PATH, 'w')
......
...@@ -30,28 +30,28 @@ TO_PUBKEY = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" ...@@ -30,28 +30,28 @@ TO_PUBKEY = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
# ( http://pythonhosted.org/aiohttp ) # ( http://pythonhosted.org/aiohttp )
AIOHTTP_SESSION = aiohttp.ClientSession() AIOHTTP_SESSION = aiohttp.ClientSession()
async def get_current_block(): async def get_current_block(connection):
""" """
Get the current block data Get the current block data
:param bma.api.ConnectionHandler connection: Connection handler
:rtype: dict :rtype: dict
""" """
# Here we request for the path blockchain/block/N # Here we request for the path blockchain/block/N
return await bma.blockchain.Current(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ return await bma.blockchain.Current(connection).get(AIOHTTP_SESSION)
.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 Get the identity document of the pubkey
:param bma.api.ConnectionHandler connection: Connection handler
:param dict current_block: Current block data :param dict current_block: Current block data
:param str pubkey: Public key :param str pubkey: Public key
:rtype: SelfCertification :rtype: SelfCertification
""" """
# Here we request for the path wot/lookup/pubkey # Here we request for the path wot/lookup/pubkey
lookup_data = await bma.wot.Lookup(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(), pubkey)\ lookup_data = await bma.wot.Lookup(connection, pubkey).get(AIOHTTP_SESSION)
.get(AIOHTTP_SESSION)
# init vars # init vars
uid = None uid = None
...@@ -110,11 +110,14 @@ async def main(): ...@@ -110,11 +110,14 @@ async def main():
""" """
Main code 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 # 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 # 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 # load credentials from a text file
salt, password = open(FROM_CREDENTIALS_FILE).readlines() salt, password = open(FROM_CREDENTIALS_FILE).readlines()
...@@ -127,8 +130,7 @@ async def main(): ...@@ -127,8 +130,7 @@ async def main():
# Here we request for the path wot/certify # Here we request for the path wot/certify
data = {'cert': certification.signed_raw(identity)} data = {'cert': certification.signed_raw(identity)}
response = await bma.wot.Certify(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ response = await bma.wot.Certify(connection).post(AIOHTTP_SESSION, **data)
.post(AIOHTTP_SESSION, **data)
print(response) print(response)
......
...@@ -27,15 +27,15 @@ UID = "MyIdentity" ...@@ -27,15 +27,15 @@ UID = "MyIdentity"
# ( http://pythonhosted.org/aiohttp ) # ( http://pythonhosted.org/aiohttp )
AIOHTTP_SESSION = aiohttp.ClientSession() AIOHTTP_SESSION = aiohttp.ClientSession()
async def get_current_block(): async def get_current_block(connection):
""" """
Get the current block data Get the current block data
:param bma.api.ConnectionHandler connection: Connection handler
:rtype: dict :rtype: dict
""" """
# Here we request for the path blockchain/block/N # Here we request for the path blockchain/block/N
return await bma.blockchain.Current(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ return await bma.blockchain.Current(connection).get(AIOHTTP_SESSION)
.get(AIOHTTP_SESSION)
def get_identity_document(current_block, uid, salt, password): def get_identity_document(current_block, uid, salt, password):
...@@ -112,8 +112,11 @@ async def main(): ...@@ -112,8 +112,11 @@ async def main():
""" """
Main code 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 # 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 # load credentials from a text file
salt, password = open(FROM_CREDENTIALS_FILE).readlines() salt, password = open(FROM_CREDENTIALS_FILE).readlines()
...@@ -127,18 +130,9 @@ async def main(): ...@@ -127,18 +130,9 @@ async def main():
# create a membership demand document # create a membership demand document
membership = get_membership_document("IN", current_block, identity, salt, password) 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 # send the membership document to the node
data = {membership: membership.signed_raw()} data = {membership: membership.signed_raw()}
response = await bma.blockchain.Membership(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler())\ response = await bma.blockchain.Membership(connection).post(AIOHTTP_SESSION, **data)
.post(AIOHTTP_SESSION, **data)
print(response) print(response)
response.close() response.close()
......
...@@ -37,27 +37,27 @@ AIOHTTP_SESSION = aiohttp.ClientSession() ...@@ -37,27 +37,27 @@ AIOHTTP_SESSION = aiohttp.ClientSession()
# Version of the transaction document # Version of the transaction document
TRANSACTION_VERSION = 3 TRANSACTION_VERSION = 3
async def get_current_block(): async def get_current_block(connection):
""" """
Get the current block data Get the current block data
:param bma.api.ConnectionHandler connection: Connection handler
:rtype: dict :rtype: dict
""" """
# Here we request for the path blockchain/block/N # Here we request for the path blockchain/block/N
return await bma.blockchain.Current(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ return await bma.blockchain.Current(connection).get(AIOHTTP_SESSION)
.get(AIOHTTP_SESSION)
async def get_sources(pubkey): async def get_sources(connection, pubkey):
""" """
Get the current block data Get the current block data
:param bma.api.ConnectionHandler connection: Connection handler
:param str pubkey: Public key of the sources account :param str pubkey: Public key of the sources account
:rtype: dict :rtype: dict
""" """
# Here we request for the path blockchain/block/N # Here we request for the path blockchain/block/N
return await bma.tx.Sources(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(), pubkey) \ return await bma.tx.Sources(connection, pubkey).get(AIOHTTP_SESSION)
.get(AIOHTTP_SESSION)
def get_transaction_document(current_block, source, from_pubkey, to_pubkey): def get_transaction_document(current_block, source, from_pubkey, to_pubkey):
...@@ -126,10 +126,13 @@ async def main(): ...@@ -126,10 +126,13 @@ async def main():
""" """
Main code 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 # 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: if len(response['sources']) == 0:
print("no sources found for account %s" % FROM_PUBKEY) print("no sources found for account %s" % FROM_PUBKEY)
...@@ -155,8 +158,7 @@ async def main(): ...@@ -155,8 +158,7 @@ async def main():
# send the Transaction document to the node # send the Transaction document to the node
data = {'transaction': transaction.signed_raw()} data = {'transaction': transaction.signed_raw()}
response = await bma.tx.Process(BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()) \ response = await bma.tx.Process(connection).post(AIOHTTP_SESSION, **data)
.post(AIOHTTP_SESSION, **data)
print(response) print(response)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment