def encode_fields(claim_dictionary): """Encode bytes to hex and b58 for return by ClaimDict""" claim_dictionary = deepcopy(claim_dictionary) claim_type = CLAIM_TYPES[claim_dictionary[CLAIM_TYPE]] claim_value = claim_dictionary[claim_type] if claim_type == CLAIM_TYPES[STREAM_TYPE]: claim_value['source']['source'] = binascii.hexlify( claim_value['source']['source']).decode() if 'fee' in claim_value['metadata']: try: address = encode_address( claim_value['metadata']['fee']['address']) except InvalidAddress as err: raise DecodeError("Invalid fee address: %s" % err) claim_value['metadata']['fee']['address'] = address elif claim_type == CLAIM_TYPES[CERTIFICATE_TYPE]: public_key = claim_value["publicKey"] claim_value["publicKey"] = binascii.hexlify(public_key).decode() if SIGNATURE in claim_dictionary: encoded_sig = binascii.hexlify( claim_dictionary[SIGNATURE]['signature']).decode() encoded_cert_id = binascii.hexlify( claim_dictionary[SIGNATURE]['certificateId']).decode() claim_dictionary[SIGNATURE]['signature'] = encoded_sig claim_dictionary[SIGNATURE]['certificateId'] = encoded_cert_id claim_dictionary[claim_type] = claim_value return claim_dictionary
def load_protobuf(cls, pb): return cls({ "version": pb.version, "currency": CURRENCY_NAMES[pb.currency], "address": encode_address(pb.address), "amount": pb.amount })
def test_regtest_address_encode_error(self): invalid_prefix = "6dcdac187757dbf05500f613ada6fdd953d59b9acbf3c9343f" invalid_checksum = "6fcdac187757dbf05500f613ada6fdd953d59b9acbf3c9343d" invalid_length = "6fcdac187757dbf05500f613ada6fdd953d59b9acbf3c934" with self.assertRaises(InvalidAddress): encode_address(invalid_prefix.decode('hex')) encode_address(invalid_checksum.decode('hex')) encode_address(invalid_length.decode('hex'))
def test_mainnet_address_encode_error(self): invalid_prefix = "54be482f953ed0feda4fc5c4d012681b6119274993dc96bf10" invalid_checksum = "55be482f953ed0feda4fc5c4d012681b6119274993dc96bf11" invalid_length = "55482f953ed0feda4fc5c4d012681b6119274993dc96bf10" with self.assertRaises(InvalidAddress): encode_address(invalid_prefix.decode('hex')) encode_address(invalid_checksum.decode('hex')) encode_address(invalid_length.decode('hex'))
def test_regtest_address_encode_decode(self): valid_addr_hex = "6fcdac187757dbf05500f613ada6fdd953d59b9acbf3c9343f" self.assertEqual(encode_address(valid_addr_hex.decode('hex')), "mzGSynizDwSgURdnFjosZwakSVuZrdE8V4") self.assertEqual(decode_address("mzGSynizDwSgURdnFjosZwakSVuZrdE8V4"), valid_addr_hex.decode('hex'))
def test_mainnet_address_encode_decode(self): valid_addr_hex = "55be482f953ed0feda4fc5c4d012681b6119274993dc96bf10" self.assertEqual(encode_address(valid_addr_hex.decode('hex')), "bW5PZEvEBNPQRVhwpYXSjabFgbSw1oaHyR") self.assertEqual(decode_address("bW5PZEvEBNPQRVhwpYXSjabFgbSw1oaHyR"), valid_addr_hex.decode('hex'))