class AlertMessageSerializer(Serializer): ALERT = Structure([VarstrSerializer("payload"), VarstrSerializer("signature")]) PAYLOAD = AlertPayloadSerializer() def serialize(self, alert_msg): payload_data = AlertPayloadSerializer().serialize(alert_msg.payload) return (self.ALERT.serialize([payload_data, alert_msg.signature])) def deserialize(self, data, cursor=0): (payload_data, signature), cursor = self.ALERT.deserialize(data, cursor) payload, _ = AlertPayloadSerializer().deserialize(payload_data) return (AlertMessage(payload, signature), cursor)
def __init__(self, bsddb_env, filename): self.bsddb_env = bsddb_env self.filename = filename self.varstr_serializer = VarstrSerializer() self.uint256_serializer = Uint256Serializer("") self.wallet_tx_serializer = WalletTxSerializer() self.master_key_serializer = MasterKeySerializer() self.reset_wallet() self.db = bsddb.db.DB(self.bsddb_env.dbenv) self.dbflags = bsddb.db.DB_THREAD
class MasterKeySerializer(): MASTER_KEY = Structure([ VarstrSerializer(), VarstrSerializer(), Field("<I"), Field("<I"), VarstrSerializer() ]) def serialize(self, master_key): return (self.MASTER_KEY.serialize([ master_key.crypted_key, master_key.salt, master_key.derivation_method, master_key.derive_iterations, master_key.other_derivation_parameters ])) def deserialize(self, data, cursor): master_key_fields, cursor = self.MASTER_KEY.deserialize(data, cursor) (crypted_key, salt, derivation_method, derive_iterations, other_derivation_parameters) = master_key_fields return (MasterKey(crypted_key, salt, derivation_method, derive_iterations, other_derivation_parameters), cursor)
class AlertPayloadSerializer(Serializer): PAYLOAD = Structure([Field("<I", "version"), Field("<Q", "relay_until"), Field("<Q", "expiration"), Field("<I", "id"), Field("<I", "cancel"), VarsizelistSerializer(VarintSerializer("count"), VarintSerializer("set_cancel")), Field("<I", "min_ver"), Field("<I", "max_ver"), VarsizelistSerializer(VarintSerializer("count"), VarstrSerializer("set_sub_ver")), Field("<I", "priority"), VarstrSerializer("comment"), VarstrSerializer("statusbar"), VarstrSerializer("reserved")]) def serialize(self, alert_payload): return (self.PAYLOAD.serialize([alert_payload.version, alert_payload.relay_until, alert_payload.expiration, alert_payload.id, alert_payload.cancel, alert_payload.set_cancel, alert_payload.min_ver, alert_payload.max_ver, alert_payload.set_sub_ver, alert_payload.priority, alert_payload.comment, alert_payload.statusbar, alert_payload.reserved])) def deserialize(self, data, cursor=0): payload_fields, cursor = self.PAYLOAD.deserialize(data, cursor) (version, relay_until, expiration, id, cancel, set_cancel, min_ver, max_ver, \ set_sub_ver, priority, comment, statusbar, reserved) = payload_fields return (AlertPayload(version, relay_until, expiration, id, cancel, set(set_cancel), min_ver, max_ver, set(set_sub_ver), priority, comment, statusbar, reserved), cursor)
class VersionMessageSerializer(Serializer): VERSION_STRUCT = Structure([ Field("<I", "version"), Field("<Q", "services"), Field("<Q", "timestamp"), NetAddrSerializer("addr_me"), NetAddrSerializer("addr_you"), Field("<Q", "nonce"), VarstrSerializer("sub_version_num"), Field("<I", "start_height") ], "version_message") def serialize(self, version_msg): return (self.VERSION_STRUCT.serialize([ version_msg.version, version_msg.services, version_msg.timestamp, version_msg.addr_me, version_msg.addr_you, version_msg.nonce, version_msg.sub_version_num, version_msg.start_height ])) def deserialize(self, data, cursor=0): result, cursor = self.VERSION_STRUCT.deserialize(data, cursor) return (VersionMessage(*result), cursor)
def __init__(self): self.serializer = ScriptSerializer() self.strencoder = VarstrSerializer()
def test_varstr_deserialize(self): str, _ = VarstrSerializer().deserialize(decodehexstr("0568656c6c6f")) self.assertEquals(str, "hello")
def test_varstr_serialize(self): self.assertEquals(hexstr(VarstrSerializer().serialize("hello")), "0568656c6c6f")