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

[enh] Improve send_membership.py example

Remove the useless Identity document generation
Pass the key to get_membership_document()
Get the uid and the timestamp from the identity get from /wot/lookup
parent 4e7aec37
No related branches found
No related tags found
2 merge requests!110Merge dev into master for v0.58.0 release,!104Rework send membership and identity documents examples
...@@ -3,7 +3,7 @@ import getpass ...@@ -3,7 +3,7 @@ import getpass
from duniterpy.api import bma from duniterpy.api import bma
from duniterpy.api.client import Client from duniterpy.api.client import Client
from duniterpy.documents import BlockUID, Identity, Membership from duniterpy.documents import BlockUID, Membership
from duniterpy.key import SigningKey from duniterpy.key import SigningKey
# CONFIG ####################################### # CONFIG #######################################
...@@ -17,57 +17,19 @@ BMAS_ENDPOINT = "BMAS g1-test.duniter.org 443" ...@@ -17,57 +17,19 @@ BMAS_ENDPOINT = "BMAS g1-test.duniter.org 443"
################################################ ################################################
def get_identity_document(
current_block: dict, uid: str, salt: str, password: str
) -> Identity:
"""
Get an Identity document
:param current_block: Current block data
:param uid: Unique Identifier
:param salt: Passphrase of the account
:param password: Password of the account
:rtype: Identity
"""
# get current block BlockStamp
timestamp = BlockUID(current_block["number"], current_block["hash"])
# create keys from credentials
key = SigningKey.from_credentials(salt, password)
# create identity document
identity = Identity(
version=10,
currency=current_block["currency"],
pubkey=key.pubkey,
uid=uid,
ts=timestamp,
signature=None,
)
# sign document
identity.sign([key])
return identity
def get_membership_document( def get_membership_document(
membership_type: str, membership_type: str,
current_block: dict, current_block: dict,
identity: Identity, identity: dict,
salt: str, key: SigningKey,
password: str,
) -> Membership: ) -> Membership:
""" """
Get a Membership document Get a Membership document
:param membership_type: "IN" to ask for membership or "OUT" to cancel membership :param membership_type: "IN" to ask for membership or "OUT" to cancel membership
:param current_block: Current block data :param current_block: Current block data
:param identity: Identity document :param identity: identity card from /wot/lookup
:param salt: Passphrase of the account :param key: cryptographic key to sign documents
:param password: Password of the account
:rtype: Membership :rtype: Membership
""" """
...@@ -75,18 +37,19 @@ def get_membership_document( ...@@ -75,18 +37,19 @@ def get_membership_document(
# get current block BlockStamp # get current block BlockStamp
timestamp = BlockUID(current_block["number"], current_block["hash"]) timestamp = BlockUID(current_block["number"], current_block["hash"])
# create keys from credentials # get the uid and the timestamp of the corresponding identity
key = SigningKey.from_credentials(salt, password) uid = identity["uids"][0]["uid"]
identity_timestamp = identity["uids"][0]["meta"]["timestamp"]
# create identity document # create membership document
membership = Membership( membership = Membership(
version=10, version=10,
currency=current_block["currency"], currency=current_block["currency"],
issuer=key.pubkey, issuer=key.pubkey,
membership_ts=timestamp, membership_ts=timestamp,
membership_type=membership_type, membership_type=membership_type,
uid=identity.uid, uid=uid,
identity_ts=identity.timestamp, identity_ts=identity_timestamp,
) )
# sign document # sign document
...@@ -115,14 +78,16 @@ async def main(): ...@@ -115,14 +78,16 @@ async def main():
# prompt hidden user entry # prompt hidden user entry
password = getpass.getpass("Enter your password: ") password = getpass.getpass("Enter your password: ")
# prompt entry # create key from credentials
uid = input("Enter your UID: ") key = SigningKey.from_credentials(salt, password)
# create our signed identity document # Look for identities on the network, take the first result since the
identity = get_identity_document(current_block, uid, salt, password) # lookup was done with a pubkey, which should correspond to the first identity
identities = await client(bma.wot.lookup, key.pubkey)
identity = identities["results"][0]
# 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, key)
# send the membership signed raw document to the node # send the membership signed raw document to the node
response = await client(bma.blockchain.membership, membership.signed_raw()) response = await client(bma.blockchain.membership, membership.signed_raw())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment