def pairwise_info2tags(pairwise: PairwiseInfo) -> dict: """ Given pairwise info with metadata mapping tags to values, return corresponding indy-sdk non_secrets record tags dict to store same in wallet (via non_secrets) unencrypted (for WQL search options). Canonicalize metadata values to strings via raw() for WQL fitness. Raise BadRecord if metadata does not coerce into non_secrets API tags spec of {str:str}. :param pairwise: pairwise info with metadata dict mapping tags to values :return: corresponding non_secrets tags dict marked for unencrypted storage """ rv = { canon_pairwise_tag(tag): raw(pairwise.metadata[tag]) for tag in pairwise.metadata or {} } rv['~their_did'] = pairwise.their_did rv['~their_verkey'] = pairwise.their_verkey rv['~my_did'] = pairwise.my_did rv['~my_verkey'] = pairwise.my_verkey if not StorageRecord.ok_tags(rv): raise BadRecord( 'Pairwise metadata {} must map strings to strings'.format(rv)) return rv
def encode(orig: Any) -> str: """ Encode credential attribute value, purely stringifying any int32 and leaving numeric int32 strings alone, but mapping any other input to a stringified 256-bit (but not 32-bit) integer. Predicates in indy-sdk operate on int32 values properly only when their encoded values match their raw values. :param orig: original value to encode :return: encoded value """ if isinstance(orig, int) and -I32_BOUND <= orig < I32_BOUND: return str(int(orig)) # python bools are ints try: i32orig = int(str(orig)) # don't encode floats as ints if -I32_BOUND <= i32orig < I32_BOUND: return str(i32orig) except (ValueError, TypeError): pass rv = int.from_bytes(sha256(raw(orig).encode()).digest(), 'big') while -I32_BOUND <= rv < I32_BOUND: rv = int.from_bytes( sha256(rv.encode()).digest(), 'big') # sha256 maps no 32-bit int to another: terminates return str(rv)
def cred_attr_value(orig: Any) -> dict: """ Given a value, return corresponding credential attribute value dict for indy-sdk processing. :param orig: original attribute value of any stringifiable type :return: dict on 'raw' and 'encoded' keys for indy-sdk processing """ return {'raw': raw(orig), 'encoded': encode(orig)}
def pairwise_info2tags(pairwise: PairwiseInfo) -> dict: """ Given pairwise info with metadata mapping tags to values, return corresponding indy-sdk non_secrets record tags dict to store same in wallet (via non_secrets) unencrypted (for WQL search options). Canonicalize metadata values to strings via raw() for WQL fitness. :param pairwise: pairwise info with metadata dict mapping tags to values :return: corresponding non_secrets tags dict marked for unencrypted storage """ rv = { canon_pairwise_tag(tag): raw(pairwise.metadata[tag]) for tag in pairwise.metadata or {} } rv['~their_did'] = pairwise.their_did rv['~their_verkey'] = pairwise.their_verkey rv['~my_did'] = pairwise.my_did rv['~my_verkey'] = pairwise.my_verkey return rv