# -*- coding: utf-8 -*- # Copyright (c) 2019-2023 tecnovert # Distributed under the MIT software license, see the accompanying # file LICENSE or http://www.opensource.org/licenses/mit-license.php. import time import struct import sqlalchemy as sa from enum import IntEnum, auto from sqlalchemy.ext.declarative import declarative_base CURRENT_DB_VERSION = 19 CURRENT_DB_DATA_VERSION = 2 Base = declarative_base() class Concepts(IntEnum): OFFER = auto() BID = auto() NETWORK_MESSAGE = auto() AUTOMATION = auto() def strConcepts(state): if state == Concepts.OFFER: return 'Offer' if state == Concepts.BID: return 'Bid' if state == Concepts.NETWORK_MESSAGE: return 'Network Message' return 'Unknown' class DBKVInt(Base): __tablename__ = 'kv_int' key = sa.Column(sa.String, primary_key=True) value = sa.Column(sa.Integer) class DBKVString(Base): __tablename__ = 'kv_string' key = sa.Column(sa.String, primary_key=True) value = sa.Column(sa.String) class Offer(Base): __tablename__ = 'offers' offer_id = sa.Column(sa.LargeBinary, primary_key=True) active_ind = sa.Column(sa.Integer) protocol_version = sa.Column(sa.Integer) coin_from = sa.Column(sa.Integer) coin_to = sa.Column(sa.Integer) amount_from = sa.Column(sa.BigInteger) rate = sa.Column(sa.BigInteger) min_bid_amount = sa.Column(sa.BigInteger) time_valid = sa.Column(sa.BigInteger) lock_type = sa.Column(sa.Integer) lock_value = sa.Column(sa.Integer) swap_type = sa.Column(sa.Integer) proof_address = sa.Column(sa.String) proof_signature = sa.Column(sa.LargeBinary) pkhash_seller = sa.Column(sa.LargeBinary) secret_hash = sa.Column(sa.LargeBinary) addr_from = sa.Column(sa.String) addr_to = sa.Column(sa.String) created_at = sa.Column(sa.BigInteger) expire_at = sa.Column(sa.BigInteger) was_sent = sa.Column(sa.Boolean) # Sent by node from_feerate = sa.Column(sa.BigInteger) to_feerate = sa.Column(sa.BigInteger) amount_negotiable = sa.Column(sa.Boolean) rate_negotiable = sa.Column(sa.Boolean) # Local fields auto_accept_bids = sa.Column(sa.Boolean) withdraw_to_addr = sa.Column(sa.String) # Address to spend lock tx to - address from wallet if empty TODO security_token = sa.Column(sa.LargeBinary) state = sa.Column(sa.Integer) states = sa.Column(sa.LargeBinary) # Packed states and times def setState(self, new_state): now = int(time.time()) self.state = new_state if self.states is None: self.states = struct.pack(' L coin_a_lock_spend_tx_msg_id = sa.Column(sa.LargeBinary) # MSG4F L -> F coin_a_lock_release_msg_id = sa.Column(sa.LargeBinary) # MSG5F L -> F contract_count = sa.Column(sa.Integer) dest_af = sa.Column(sa.LargeBinary) # Destination for coin A amount to follower when swap completes successfully pkal = sa.Column(sa.LargeBinary) pkasl = sa.Column(sa.LargeBinary) pkaf = sa.Column(sa.LargeBinary) pkasf = sa.Column(sa.LargeBinary) vkbvl = sa.Column(sa.LargeBinary) vkbsl = sa.Column(sa.LargeBinary) pkbvl = sa.Column(sa.LargeBinary) pkbsl = sa.Column(sa.LargeBinary) vkbvf = sa.Column(sa.LargeBinary) vkbsf = sa.Column(sa.LargeBinary) pkbvf = sa.Column(sa.LargeBinary) pkbsf = sa.Column(sa.LargeBinary) kbsl_dleag = sa.Column(sa.LargeBinary) kbsf_dleag = sa.Column(sa.LargeBinary) vkbv = sa.Column(sa.LargeBinary) # chain b view private key pkbv = sa.Column(sa.LargeBinary) # chain b view public key pkbs = sa.Column(sa.LargeBinary) # chain b view spend key a_lock_tx = sa.Column(sa.LargeBinary) a_lock_tx_script = sa.Column(sa.LargeBinary) a_lock_tx_id = sa.Column(sa.LargeBinary) a_lock_tx_vout = sa.Column(sa.Integer) a_lock_refund_tx = sa.Column(sa.LargeBinary) a_lock_refund_tx_script = sa.Column(sa.LargeBinary) a_lock_refund_tx_id = sa.Column(sa.LargeBinary) a_swap_refund_value = sa.Column(sa.BigInteger) al_lock_refund_tx_sig = sa.Column(sa.LargeBinary) af_lock_refund_tx_sig = sa.Column(sa.LargeBinary) a_lock_refund_spend_tx = sa.Column(sa.LargeBinary) a_lock_refund_spend_tx_id = sa.Column(sa.LargeBinary) af_lock_refund_spend_tx_esig = sa.Column(sa.LargeBinary) af_lock_refund_spend_tx_sig = sa.Column(sa.LargeBinary) a_lock_spend_tx = sa.Column(sa.LargeBinary) a_lock_spend_tx_id = sa.Column(sa.LargeBinary) al_lock_spend_tx_esig = sa.Column(sa.LargeBinary) kal_sig = sa.Column(sa.LargeBinary) a_lock_refund_swipe_tx = sa.Column(sa.LargeBinary) # Follower spends script coin lock refund tx b_lock_tx_id = sa.Column(sa.LargeBinary) class XmrSplitData(Base): __tablename__ = 'xmr_split_data' record_id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) addr_from = sa.Column(sa.String) addr_to = sa.Column(sa.String) bid_id = sa.Column(sa.LargeBinary) msg_type = sa.Column(sa.Integer) msg_sequence = sa.Column(sa.Integer) dleag = sa.Column(sa.LargeBinary) created_at = sa.Column(sa.BigInteger) __table_args__ = (sa.UniqueConstraint('bid_id', 'msg_type', 'msg_sequence', name='uc_1'),) class RevokedMessage(Base): __tablename__ = 'revoked_messages' record_id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) active_ind = sa.Column(sa.Integer) msg_id = sa.Column(sa.LargeBinary) created_at = sa.Column(sa.BigInteger) expires_at = sa.Column(sa.BigInteger) class Wallets(Base): __tablename__ = 'wallets' record_id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) active_ind = sa.Column(sa.Integer) coin_id = sa.Column(sa.Integer) wallet_name = sa.Column(sa.String) wallet_data = sa.Column(sa.String) balance_type = sa.Column(sa.Integer) created_at = sa.Column(sa.BigInteger) class KnownIdentity(Base): __tablename__ = 'knownidentities' record_id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) active_ind = sa.Column(sa.Integer) address = sa.Column(sa.String) label = sa.Column(sa.String) publickey = sa.Column(sa.LargeBinary) num_sent_bids_successful = sa.Column(sa.Integer) num_recv_bids_successful = sa.Column(sa.Integer) num_sent_bids_rejected = sa.Column(sa.Integer) num_recv_bids_rejected = sa.Column(sa.Integer) num_sent_bids_failed = sa.Column(sa.Integer) num_recv_bids_failed = sa.Column(sa.Integer) automation_override = sa.Column(sa.Integer) # AutomationOverrideOptions visibility_override = sa.Column(sa.Integer) # VisibilityOverrideOptions data = sa.Column(sa.LargeBinary) note = sa.Column(sa.String) updated_at = sa.Column(sa.BigInteger) created_at = sa.Column(sa.BigInteger) class AutomationStrategy(Base): __tablename__ = 'automationstrategies' record_id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) active_ind = sa.Column(sa.Integer) label = sa.Column(sa.String) type_ind = sa.Column(sa.Integer) only_known_identities = sa.Column(sa.Integer) num_concurrent = sa.Column(sa.Integer) data = sa.Column(sa.LargeBinary) note = sa.Column(sa.String) created_at = sa.Column(sa.BigInteger) class AutomationLink(Base): __tablename__ = 'automationlinks' # Contains per order/bid options record_id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) active_ind = sa.Column(sa.Integer) linked_type = sa.Column(sa.Integer) linked_id = sa.Column(sa.LargeBinary) strategy_id = sa.Column(sa.Integer) data = sa.Column(sa.LargeBinary) repeat_limit = sa.Column(sa.Integer) repeat_count = sa.Column(sa.Integer) note = sa.Column(sa.String) created_at = sa.Column(sa.BigInteger) __table_args__ = (sa.Index('linked_index', 'linked_type', 'linked_id'), ) class History(Base): __tablename__ = 'history' record_id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) concept_type = sa.Column(sa.Integer) concept_id = sa.Column(sa.Integer) changed_data = sa.Column(sa.LargeBinary) created_at = sa.Column(sa.BigInteger) class BidState(Base): __tablename__ = 'bidstates' record_id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) active_ind = sa.Column(sa.Integer) state_id = sa.Column(sa.Integer) label = sa.Column(sa.String) in_progress = sa.Column(sa.Integer) note = sa.Column(sa.String) created_at = sa.Column(sa.BigInteger) class Notification(Base): __tablename__ = 'notifications' record_id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) active_ind = sa.Column(sa.Integer) created_at = sa.Column(sa.BigInteger) event_type = sa.Column(sa.Integer) event_data = sa.Column(sa.LargeBinary)