async def send_membership(self, password, community, mstype): """ Send a membership document to a target community. Signal "document_broadcasted" is emitted at the end. :param str password: The account SigningKey password :param community: The community target of the membership document :param str mstype: The type of membership demand. "IN" to join, "OUT" to leave """ logging.debug("Send membership") blockUID = community.network.current_blockUID self_identity = await self._identities_registry.future_find(self.pubkey, community) selfcert = await self_identity.selfcert(community) membership = Membership(PROTOCOL_VERSION, community.currency, selfcert.pubkey, blockUID, mstype, selfcert.uid, selfcert.timestamp, None) key = SigningKey(self.salt, password) membership.sign([key]) logging.debug("Membership : {0}".format(membership.signed_raw())) responses = await community.bma_access.broadcast(bma.blockchain.Membership, {}, {'membership': membership.signed_raw()}) result = (False, "") for r in responses: if r.status == 200: result = (True, (await r.json())) elif not result[0]: result = (False, (await r.text())) else: await r.release() return result
def get_membership_document(mtype, current_block, identity, salt, password): """ Get a Membership document :param str mtype: "IN" to ask for membership or "OUT" to cancel membership :param dict current_block: Current block data :param Identity identity: Identity document :param str salt: Passphrase of the account :param str password: Password of the account :rtype: Membership """ # get current block BlockStamp timestamp = BlockUID(current_block['number'], current_block['hash']) # create keys from credentials key = SigningKey(salt, password, ScryptParams(4096, 16, 1)) # create identity document membership = Membership(version=10, currency=current_block['currency'], issuer=key.pubkey, membership_ts=timestamp, membership_type=mtype, uid=identity.uid, identity_ts=identity.timestamp, signature=None) # sign document membership.sign([key]) return membership
def test_generate_membership_document(): generated_membership = membership.generate_membership_document( currency, pubkey, membership_timestamp, identity_uid, identity_timestamp, ) expected = Membership( version=10, currency=currency, issuer=pubkey, membership_ts=membership_timestamp, membership_type="IN", uid=identity_uid, identity_ts=identity_timestamp, ) # Direct equality check can be done without raw() once Membership.__eq__() is implemented assert expected.raw() == generated_membership.raw()
async def send_membership(self, connection, secret_key, password, mstype): """ Send a membership document to a target community. Signal "document_broadcasted" is emitted at the end. :param sakia.data.entities.Connection connection: the connection publishing ms doc :param str secret_key: The account SigningKey salt :param str password: The account SigningKey password :param str mstype: The type of membership demand. "IN" to join, "OUT" to leave """ self._logger.debug("Send membership") blockUID = self._blockchain_processor.current_buid(connection.currency) membership = Membership(10, connection.currency, connection.pubkey, blockUID, mstype, connection.uid, connection.blockstamp, None) key = SigningKey(secret_key, password, connection.scrypt_params) membership.sign([key]) self._logger.debug("Membership : {0}".format(membership.signed_raw())) responses = await self._bma_connector.broadcast( connection.currency, bma.blockchain.membership, req_args={'membership': membership.signed_raw()}) result = await parse_bma_responses(responses) return result
def get_membership_document( membership_type: str, current_block: dict, identity: dict, key: SigningKey, ) -> Membership: """ Get a Membership document :param membership_type: "IN" to ask for membership or "OUT" to cancel membership :param current_block: Current block data :param identity: identity card from /wot/lookup :param key: cryptographic key to sign documents :rtype: Membership """ # get current block BlockStamp timestamp = BlockUID(current_block["number"], current_block["hash"]) # get the uid and the timestamp of the corresponding identity uid = identity["uids"][0]["uid"] identity_timestamp = identity["uids"][0]["meta"]["timestamp"] # create membership document membership = Membership( version=10, currency=current_block["currency"], issuer=key.pubkey, membership_ts=timestamp, membership_type=membership_type, uid=uid, identity_ts=identity_timestamp, ) # sign document membership.sign([key]) return membership
def get_membership_document(membership_type: str, current_block: dict, identity: Identity, salt: str, password: str) -> Membership: """ Get a Membership document :param membership_type: "IN" to ask for membership or "OUT" to cancel membership :param current_block: Current block data :param identity: Identity document :param salt: Passphrase of the account :param password: Password of the account :rtype: Membership """ # 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 membership = Membership( version=10, currency=current_block['currency'], issuer=key.pubkey, membership_ts=timestamp, membership_type=membership_type, uid=identity.uid, identity_ts=identity.timestamp, signature=None ) # sign document membership.sign([key]) return membership
async def send_membership(self, connection, secret_key, password, mstype): """ Send a membership document to a target community. Signal "document_broadcasted" is emitted at the end. :param sakia.data.entities.Connection connection: the connection publishing ms doc :param str secret_key: The account SigningKey salt :param str password: The account SigningKey password :param str mstype: The type of membership demand. "IN" to join, "OUT" to leave """ self._logger.debug("Send membership") blockUID = self._blockchain_processor.current_buid(connection.currency) membership = Membership(10, connection.currency, connection.pubkey, blockUID, mstype, connection.uid, connection.blockstamp, None) key = SigningKey(secret_key, password, connection.scrypt_params) membership.sign([key]) self._logger.debug("Membership : {0}".format(membership.signed_raw())) responses = await self._bma_connector.broadcast(connection.currency, bma.blockchain.membership, req_args={'membership': membership.signed_raw()}) result = await parse_bma_responses(responses) return result
def generate_membership_document( currency, pubkey, membership_timestamp, identity_uid, identity_timestamp, ): return Membership( version=10, currency=currency, issuer=pubkey, membership_ts=membership_timestamp, membership_type="IN", uid=identity_uid, identity_ts=identity_timestamp, )
def test_copy_membership_to_clipboard(self, qmenu): ms_data = { "version": 2, "currency": "meta_brouzouf", "membership": "IN", "blockNumber": 49116, "blockHash": "7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67", "written": 49119 } ms_document = Membership( ms_data["version"], ms_data["currency"], self.identity.pubkey, BlockUID(ms_data["blockNumber"], ms_data["blockHash"]), ms_data["membership"], self.identity.uid, "49116-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67", "znWiWP7Sy9gg9pZq4YWKNpel8MM16VBM1lgBg2gWjSonnc+KVRCtQng5JB4JD0PgJJ0F8jdITuggFrRwqRfzAA==" ) self.identity.membership = CoroutineMock(return_value=ms_data) self.community.get_block = CoroutineMock( return_value={ "version": 2, "nonce": 127424, "number": 49119, "powMin": 5, "time": 1453921638, "medianTime": 1453912797, "membersCount": 18, "monetaryMass": 14028534972234185000, "currency": "meta_brouzouf", "issuer": "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk", "signature": "ZmjhoRubftJ/T2WYQ3gaDeTGGUJ3beUshtlWn1k/r5opk0vt48KG3w+9JU0T9YFR5uezllaek9efoNwAHRBLDw==", "hash": "49E2A1D1131F1496FAD6EDAE794A9ADBFA8844029675E3732D3B027ABB780243", "innerhash": "273DE1845F8A63677D69DD427E00DAD73D9AEDBA80356A2E0D2152939D9DAF0C", "parameters": "", "previousHash": "000005C27A1636FE07AB01766FBA060565142D79", "previousIssuer": "HBSSmqZjT4UQKsCntTSmZbu7iRP14HYtifLE6mW1PsBD", "dividend": None, "identities": [], "joiners": [ "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk:znWiWP7Sy9gg9pZq4YWKNpel8MM16VBM1lgBg2gWjSonnc+KVRCtQng5JB4JD0PgJJ0F8jdITuggFrRwqRfzAA==:49116-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67:49116-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67:A" ], "actives": [], "leavers": [], "excluded": [], "revoked": [], "certifications": [], "transactions": [], "raw": """Version: 2 Type: Block Currency: meta_brouzouf Number: 49119 PoWMin: 5 Time: 1453921638 MedianTime: 1453912797 Issuer: HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk PreviousHash: 000005C27A1636FE07AB01766FBA060565142D79 PreviousIssuer: HBSSmqZjT4UQKsCntTSmZbu7iRP14HYtifLE6mW1PsBD MembersCount: 18 Identities: Joiners: HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk:znWiWP7Sy9gg9pZq4YWKNpel8MM16VBM1lgBg2gWjSonnc+KVRCtQng5JB4JD0PgJJ0F8jdITuggFrRwqRfzAA==:49116-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67:49116-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67:A Actives: Leavers: Revoked: Excluded: Certifications: Transactions: InnerHash: 273DE1845F8A63677D69DD427E00DAD73D9AEDBA80356A2E0D2152939D9DAF0C Nonce: 127424 """ }) self.qapplication.clipboard().clear() async def exec_test(): context_menu = ContextMenu(qmenu, self.app, self.account, self.community, self.password_asker) context_menu.community = self.community context_menu.copy_membership_to_clipboard(self.identity) self.lp.run_until_complete(exec_test()) self.assertEqual(self.qapplication.clipboard().text(), ms_document.signed_raw())
def test_copy_membership_to_clipboard(self, qmenu): ms_data = { "version": 2, "currency": "meta_brouzouf", "membership": "IN", "blockNumber": 49116, "blockHash": "7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67", "written": 49119 } ms_document = Membership(ms_data["version"], ms_data["currency"], self.identity.pubkey, BlockUID(ms_data["blockNumber"], ms_data["blockHash"]), ms_data["membership"], self.identity.uid, "49116-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67", "znWiWP7Sy9gg9pZq4YWKNpel8MM16VBM1lgBg2gWjSonnc+KVRCtQng5JB4JD0PgJJ0F8jdITuggFrRwqRfzAA==") self.identity.membership = CoroutineMock(return_value=ms_data) self.community.get_block = CoroutineMock(return_value={ "version": 2, "nonce": 127424, "number": 49119, "powMin": 5, "time": 1453921638, "medianTime": 1453912797, "membersCount": 18, "monetaryMass": 14028534972234185000, "currency": "meta_brouzouf", "issuer": "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk", "signature": "ZmjhoRubftJ/T2WYQ3gaDeTGGUJ3beUshtlWn1k/r5opk0vt48KG3w+9JU0T9YFR5uezllaek9efoNwAHRBLDw==", "hash": "49E2A1D1131F1496FAD6EDAE794A9ADBFA8844029675E3732D3B027ABB780243", "innerhash": "273DE1845F8A63677D69DD427E00DAD73D9AEDBA80356A2E0D2152939D9DAF0C", "parameters": "", "previousHash": "000005C27A1636FE07AB01766FBA060565142D79", "previousIssuer": "HBSSmqZjT4UQKsCntTSmZbu7iRP14HYtifLE6mW1PsBD", "dividend": None, "identities": [], "joiners": [ "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk:znWiWP7Sy9gg9pZq4YWKNpel8MM16VBM1lgBg2gWjSonnc+KVRCtQng5JB4JD0PgJJ0F8jdITuggFrRwqRfzAA==:49116-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67:49116-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67:A" ], "actives": [], "leavers": [], "excluded": [], "revoked": [], "certifications": [], "transactions": [], "raw": """Version: 2 Type: Block Currency: meta_brouzouf Number: 49119 PoWMin: 5 Time: 1453921638 MedianTime: 1453912797 Issuer: HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk PreviousHash: 000005C27A1636FE07AB01766FBA060565142D79 PreviousIssuer: HBSSmqZjT4UQKsCntTSmZbu7iRP14HYtifLE6mW1PsBD MembersCount: 18 Identities: Joiners: HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk:znWiWP7Sy9gg9pZq4YWKNpel8MM16VBM1lgBg2gWjSonnc+KVRCtQng5JB4JD0PgJJ0F8jdITuggFrRwqRfzAA==:49116-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67:49116-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67:A Actives: Leavers: Revoked: Excluded: Certifications: Transactions: InnerHash: 273DE1845F8A63677D69DD427E00DAD73D9AEDBA80356A2E0D2152939D9DAF0C Nonce: 127424 """ }) self.qapplication.clipboard().clear() async def exec_test(): context_menu = ContextMenu(qmenu, self.app, self.account, self.community, self.password_asker) context_menu.community = self.community context_menu.copy_membership_to_clipboard(self.identity) self.lp.run_until_complete(exec_test()) self.assertEqual(self.qapplication.clipboard().text(), ms_document.signed_raw())
def leave(self, blockstamp): ms_doc = Membership(10, self.currency, self.key.pubkey, blockstamp, 'OUT', self.uid, self.blockstamp, []) ms_doc.sign([self.key]) return ms_doc