def parse_type_from_did(did, rd=None): """ Parse the correct DID type used in provided did address Args: did(string): did address Returns: :obj:`AbtDid` Examples: >>> did_type = AbtDid.parse_type_from_did('did:abt:z1jqq6DaT76Q9aTRZ4ndNjh9AthotPBvyEP') >>> did_type.hash_type 'sha3' >>> did_type.role_type 'account' >>> did_type.key_type 'ed25519' """ try: did = did.lstrip(AbtDid.PREFIX) decoded = utils.multibase_b58decode(did) type_bytes = decoded[0:2] return AbtDid._bytes_to_type(type_bytes, rd=rd) except Exception as e: logger.error('Fail to parse type from given did {}'.format(did)) logger.error(e, exc_info=True)
def get_handler(**args): address = args.get('user_did').lstrip(forge_did.PREFIX) event_address = args.get('request').args.get('event_address') pk = forge_utils.multibase_b58decode(args.get('user_pk')) acquire_asset_tx, _ = forge.rpc.build_acquire_asset_tx( to=event_address, spec_datas=[{ "id": str(uuid.uuid4()) }], type_url='ec:s:general_ticket', proto_lib=protos) tx = forge_utils.build_unsigned_tx(itx=forge_utils.encode_to_any( 'fg:t:acquire_asset', acquire_asset_tx), chain_id=forge.config.chain_id, address=address, pk=pk) return { 'request_type': 'signature', 'workflow': 'buy-ticket', 'tx': tx, 'description': 'Confirm Purchase of Ticket', }
def is_valid(did, rd=None): """ Check is the provided DID address valid Args: did(string): DID address Returns: bool Examples: >>> AbtDid.is_valid('did:abt:z1XEw92uJKkTqyTuMnFFQ1BrgkGinfz72dF') True >>> AbtDid.is_valid('did:abt:z1XEw92uJKkTqyTuMnFFQ1Brgk72dF') False """ did = did.lstrip('did:abt:') try: decoded = utils.multibase_b58decode(did) type_bytes = decoded[0:2] pk_hash = decoded[2:22] actual_check_sum = decoded[22:26] did_type = AbtDid._bytes_to_type(type_bytes, rd=rd) expectued_check_sum = did_type.hasher.hash(type_bytes + pk_hash)[0:4] if actual_check_sum == expectued_check_sum: return True return False except Exception as e: logger.error('Fail to verify did {}'.format(did)) logger.error(e, exc_info=True) return False
def is_match_pk(did, pk, rd=None): """ check if the provided did is calculated from provided public key Args: did(string): did address pk(bytes): public key Returns: bool Examples: >>> import base64 >>> pk = base64.b16decode('A5AB55816BB81D2526D5CAE3CE3082F4F2FAF9D658D8938EC085E8BADAFF5B9F') >>> did_address ='did:abt:z1XEw92uJKkTqyTuMnFFQ1BrgkGinfz72dF' >>> AbtDid.is_match_pk(did_address, pk) True """ if did.startswith(AbtDid.PREFIX): did = did.lstrip(AbtDid.PREFIX) try: decoded = utils.multibase_b58decode(did) type_bytes = decoded[0:2] did_type = AbtDid._bytes_to_type(type_bytes, rd=rd) if did == did_type.pk_to_did(pk).lstrip(AbtDid.PREFIX): return True return False except Exception as e: logger.error('Fail to match pk {}'.format(pk)) logger.error(e, exc_info=True) return False
def post_handler(**args): wallet_res = args.get('wallet_res') asset_address = wallet_res.get_asset_address() ticket = controllers.get_ticket_state(asset_address) new_tx = controllers.update_tx_multisig(tx=forge_utils.parse_to_proto( forge_utils.multibase_b58decode(ticket.consume_tx), forge_protos.Transaction), signer=wallet_res.get_address(), pk=wallet_res.get_user_pk(), data=forge_utils.encode_to_any( 'fg:x:address', asset_address, )) token = args.get('token') utils.mark_token_status(token, 'succeed') params = { 'tx': new_tx, 'url': utils.server_url( f'/api/did/consume_ticket/consume?_t_={token}&ticket_address={ticket.address}' ), 'description': 'Confirm to use the ticket.', 'workflow': 'use-ticket', 'user_did': wallet_res.get_address() } res = utils.send_did_request(request_type='signature', **params, **args.get('app_params')) logger.debug(f"POST Response: {res}") return json.loads(res)
def parse_origin_tx(parsed, protobuf=None): origin = parsed.get('requestedClaims')[0].get('origin') tx = forge_utils.parse_to_proto(forge_utils.multibase_b58decode(origin), forge_protos.Transaction) if protobuf: itx = forge_utils.parse_to_proto(tx.itx.value, protobuf) return tx, itx else: return tx, None
def buy_ticket_post(middle_user_info): origin = get_origin(middle_user_info) tx = forge_utils.parse_to_proto(forge_utils.multibase_b58decode(origin), forge_protos.Transaction) signature = forge_rpc.sign_tx(user, tx) data = send_back_signature(middle_user_info, user.address, signature) r = requests.post(url=buy_url, data=json.dumps(data)) return r.json()
def __init__(self, pk, address=None, sk=None, token=None, did=None): self.address = address self.pk = pk if isinstance(pk, bytes) else forge_utils.multibase_b58decode(pk) self.sk = sk self.token = token self.did = did if self.did: self.address = self.did.split(":")[-1]
def get_handler(**args): tx = forge_utils.build_poke_tx( chain_id=forge.config.chain_id, address=args.get('user_did').lstrip(forge_did.PREFIX), pk=forge_utils.multibase_b58decode(args.get('user_pk'))) logging.debug(tx) return { 'request_type': 'signature', 'workflow': 'poke', 'tx': tx, 'description': 'Get 25 TBA', }
def consume_asset(middle_user_info,ticket_address): consume_url = f'http://10.1.10.177:5000/api/mobile/consume/{ticket_address}' origin = get_origin(middle_user_info) tx = forge_utils.parse_to_proto(forge_utils.multibase_b58decode(origin), forge_protos.Transaction) signature = forge_rpc.sign_tx(user, tx) data = send_back_signature(middle_user_info, user.address, signature) r = requests.post(url=consume_url, data=json.dumps(data)) return r.json()
def get_handler(**args): address = args.get('user_did').lstrip(forge_did.PREFIX) pk = forge_utils.multibase_b58decode(args.get('user_pk')) tx = forge_utils.build_transfer_tx(chain_id=forge.config.chain_id, to=env.APP_ADDR, value=2, address=address, pk=pk) return { 'request_type': 'signature', 'workflow': 'payment', 'tx': tx, 'description': 'Pay 2 TBA', }
def get_user_pk(self): pk = self.response.get('userPk') logger.debug("Got userpk from wallet {}".format(pk)) return forge_utils.multibase_b58decode(pk)