create_and_publish_identity.py 2.63 KB
Newer Older
Vincent Texier's avatar
Vincent Texier committed
1
import asyncio
inso's avatar
inso committed
2
import getpass
Vincent Texier's avatar
Vincent Texier committed
3 4

import duniterpy.api.bma as bma
5
from duniterpy.api.client import Client
6
from duniterpy.documents import BlockUID, Identity
Vincent Texier's avatar
Vincent Texier committed
7 8 9 10 11 12
from duniterpy.key import SigningKey

# CONFIG #######################################

# You can either use a complete defined endpoint : [NAME_OF_THE_API] [DOMAIN] [IPv4] [IPv6] [PORT]
# or the simple definition : [NAME_OF_THE_API] [DOMAIN] [PORT]
13
# Here we use the secure BASIC_MERKLED_API (BMAS)
14
BMAS_ENDPOINT = "BMAS g1-test.duniter.org 443"
Vincent Texier's avatar
Vincent Texier committed
15 16 17 18 19


################################################


20
def get_identity_document(current_block: dict, uid: str, salt: str, password: str) -> Identity:
Vincent Texier's avatar
Vincent Texier committed
21 22 23
    """
    Get an Identity document

24 25 26 27
    :param current_block: Current block data
    :param uid: Unique IDentifier
    :param salt: Passphrase of the account
    :param password: Password of the account
Vincent Texier's avatar
Vincent Texier committed
28

29
    :rtype: Identity
Vincent Texier's avatar
Vincent Texier committed
30 31 32 33 34 35
    """

    # get current block BlockStamp
    timestamp = BlockUID(current_block['number'], current_block['hash'])

    # create keys from credentials
36
    key = SigningKey.from_credentials(salt, password)
Vincent Texier's avatar
Vincent Texier committed
37 38

    # create identity document
39
    identity = Identity(
40
        version=10,
Vincent Texier's avatar
Vincent Texier committed
41 42 43 44 45 46 47 48 49 50 51 52
        currency=current_block['currency'],
        pubkey=key.pubkey,
        uid=uid,
        ts=timestamp,
        signature=None
    )

    # sign document
    identity.sign([key])

    return identity

53

Vincent Texier's avatar
Vincent Texier committed
54 55 56 57
async def main():
    """
    Main code
    """
58 59 60 61 62 63
    # Create Client from endpoint string in Duniter format
    client = Client(BMAS_ENDPOINT)

    # Get the node summary infos to test the connection
    response = await client(bma.node.summary)
    print(response)
Vincent Texier's avatar
Vincent Texier committed
64

Vincent Texier's avatar
Vincent Texier committed
65
    # capture current block to get version and currency and blockstamp
66 67 68 69
    current_block = await client(bma.blockchain.current)

    # prompt entry
    uid = input("Enter your Unique IDentifier (pseudonym): ")
Vincent Texier's avatar
Vincent Texier committed
70

inso's avatar
inso committed
71 72
    # prompt hidden user entry
    salt = getpass.getpass("Enter your passphrase (salt): ")
Vincent Texier's avatar
Vincent Texier committed
73

inso's avatar
inso committed
74 75
    # prompt hidden user entry
    password = getpass.getpass("Enter your password: ")
Vincent Texier's avatar
Vincent Texier committed
76 77

    # create our signed identity document
78
    identity = get_identity_document(current_block, uid, salt, password)
Vincent Texier's avatar
Vincent Texier committed
79 80

    # send the identity document to the node
81
    response = await client(bma.wot.add, identity.signed_raw())
inso's avatar
inso committed
82 83 84
    if response.status == 200:
        print(await response.text())
    else:
inso's avatar
inso committed
85
        print("Error while publishing identity : {0}".format(await response.text()))
86 87 88 89

    # Close client aiohttp session
    await client.close()

Vincent Texier's avatar
Vincent Texier committed
90

inso's avatar
inso committed
91 92
# 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 )
93
asyncio.get_event_loop().run_until_complete(main())