2018-11-04 07:24:41 +01:00
|
|
|
from torba.client.basedatabase import BaseDatabase
|
2018-06-12 17:53:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
class WalletDatabase(BaseDatabase):
|
|
|
|
|
2018-07-09 15:55:07 +02:00
|
|
|
CREATE_TXO_TABLE = """
|
|
|
|
create table if not exists txo (
|
2018-07-15 05:02:19 +02:00
|
|
|
txid text references tx,
|
|
|
|
txoid text primary key,
|
|
|
|
address text references pubkey_address,
|
2018-07-09 15:55:07 +02:00
|
|
|
position integer not null,
|
|
|
|
amount integer not null,
|
|
|
|
script blob not null,
|
|
|
|
is_reserved boolean not null default 0,
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2018-07-15 05:02:19 +02:00
|
|
|
claim_id text,
|
2018-07-09 15:55:07 +02:00
|
|
|
claim_name text,
|
|
|
|
is_claim boolean not null default 0,
|
|
|
|
is_update boolean not null default 0,
|
2018-08-14 22:16:29 +02:00
|
|
|
is_support boolean not null default 0,
|
2018-08-23 05:47:37 +02:00
|
|
|
is_buy boolean not null default 0,
|
|
|
|
is_sell boolean not null default 0
|
2018-07-09 15:55:07 +02:00
|
|
|
);
|
2018-10-10 05:58:32 +02:00
|
|
|
create index if not exists txo_claim_id_idx on txo (claim_id);
|
2018-07-09 15:55:07 +02:00
|
|
|
"""
|
|
|
|
|
2018-06-12 17:53:29 +02:00
|
|
|
CREATE_TABLES_QUERY = (
|
|
|
|
BaseDatabase.CREATE_TX_TABLE +
|
|
|
|
BaseDatabase.CREATE_PUBKEY_ADDRESS_TABLE +
|
2018-10-10 05:40:02 +02:00
|
|
|
BaseDatabase.CREATE_PUBKEY_ADDRESS_INDEX +
|
2018-07-09 15:55:07 +02:00
|
|
|
CREATE_TXO_TABLE +
|
2018-10-10 05:40:02 +02:00
|
|
|
BaseDatabase.CREATE_TXO_INDEX +
|
|
|
|
BaseDatabase.CREATE_TXI_TABLE +
|
|
|
|
BaseDatabase.CREATE_TXI_INDEX
|
2018-06-12 17:53:29 +02:00
|
|
|
)
|
2018-07-09 15:55:07 +02:00
|
|
|
|
|
|
|
def txo_to_row(self, tx, address, txo):
|
2018-07-22 00:34:59 +02:00
|
|
|
row = super().txo_to_row(tx, address, txo)
|
2018-07-09 15:55:07 +02:00
|
|
|
row.update({
|
|
|
|
'is_claim': txo.script.is_claim_name,
|
|
|
|
'is_update': txo.script.is_update_claim,
|
|
|
|
'is_support': txo.script.is_support_claim,
|
2018-08-23 05:47:37 +02:00
|
|
|
'is_buy': txo.script.is_buy_claim,
|
|
|
|
'is_sell': txo.script.is_sell_claim,
|
2018-07-09 15:55:07 +02:00
|
|
|
})
|
|
|
|
if txo.script.is_claim_involved:
|
2018-08-04 18:10:41 +02:00
|
|
|
row['claim_id'] = txo.claim_id
|
|
|
|
row['claim_name'] = txo.claim_name
|
2018-07-09 15:55:07 +02:00
|
|
|
return row
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def get_txos(self, **constraints):
|
2018-10-08 16:41:07 +02:00
|
|
|
my_account = constraints.get('my_account', constraints.get('account', None))
|
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
txos = await super().get_txos(**constraints)
|
2018-10-05 15:02:02 +02:00
|
|
|
|
2018-10-08 16:41:07 +02:00
|
|
|
channel_ids = set()
|
2018-10-05 15:02:02 +02:00
|
|
|
for txo in txos:
|
|
|
|
if txo.script.is_claim_name or txo.script.is_update_claim:
|
2019-03-20 06:46:23 +01:00
|
|
|
if txo.claim.is_signed:
|
2019-03-22 07:18:34 +01:00
|
|
|
channel_ids.add(txo.claim.signing_channel_id)
|
2018-10-08 16:41:07 +02:00
|
|
|
if txo.claim_name.startswith('@') and my_account is not None:
|
2018-10-12 15:49:13 +02:00
|
|
|
txo.private_key = my_account.get_certificate_private_key(txo.ref)
|
2018-10-05 15:02:02 +02:00
|
|
|
|
2018-10-08 16:41:07 +02:00
|
|
|
if channel_ids:
|
2018-10-05 15:02:02 +02:00
|
|
|
channels = {
|
|
|
|
txo.claim_id: txo for txo in
|
2018-10-30 21:20:43 +01:00
|
|
|
(await self.get_claims(
|
2018-10-05 15:02:02 +02:00
|
|
|
my_account=my_account,
|
2018-10-08 16:41:07 +02:00
|
|
|
claim_id__in=channel_ids
|
2018-10-05 15:02:02 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
for txo in txos:
|
|
|
|
if txo.script.is_claim_name or txo.script.is_update_claim:
|
2019-03-22 07:18:34 +01:00
|
|
|
txo.channel = channels.get(txo.claim.signing_channel_id, None)
|
2018-10-05 15:02:02 +02:00
|
|
|
|
|
|
|
return txos
|
|
|
|
|
2018-10-08 16:41:07 +02:00
|
|
|
@staticmethod
|
|
|
|
def constrain_claims(constraints):
|
2018-11-07 20:49:38 +01:00
|
|
|
constraints['claim_type__any'] = {'is_claim': 1, 'is_update': 1, 'is_support': 1}
|
2018-10-08 16:41:07 +02:00
|
|
|
|
|
|
|
def get_claims(self, **constraints):
|
|
|
|
self.constrain_claims(constraints)
|
2018-10-03 18:00:21 +02:00
|
|
|
return self.get_utxos(**constraints)
|
2018-09-19 04:23:41 +02:00
|
|
|
|
2018-10-09 20:46:44 +02:00
|
|
|
def get_claim_count(self, **constraints):
|
2018-10-08 16:41:07 +02:00
|
|
|
self.constrain_claims(constraints)
|
|
|
|
return self.get_utxo_count(**constraints)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def constrain_channels(constraints):
|
2018-10-03 18:00:21 +02:00
|
|
|
if 'claim_name' not in constraints or 'claim_id' not in constraints:
|
|
|
|
constraints['claim_name__like'] = '@%'
|
2018-10-08 16:41:07 +02:00
|
|
|
|
|
|
|
def get_channels(self, **constraints):
|
|
|
|
self.constrain_channels(constraints)
|
2018-10-03 18:00:21 +02:00
|
|
|
return self.get_claims(**constraints)
|
2018-07-12 05:18:59 +02:00
|
|
|
|
2018-10-09 20:46:44 +02:00
|
|
|
def get_channel_count(self, **constraints):
|
2018-10-08 16:41:07 +02:00
|
|
|
self.constrain_channels(constraints)
|
2018-10-09 20:46:44 +02:00
|
|
|
return self.get_claim_count(**constraints)
|
2018-10-08 16:41:07 +02:00
|
|
|
|
2018-10-15 23:16:43 +02:00
|
|
|
async def get_certificates(self, private_key_accounts, exclude_without_key=False, **constraints):
|
|
|
|
channels = await self.get_channels(**constraints)
|
2018-07-16 02:54:55 +02:00
|
|
|
certificates = []
|
2018-07-12 05:18:59 +02:00
|
|
|
if private_key_accounts is not None:
|
2018-10-03 18:00:21 +02:00
|
|
|
for channel in channels:
|
2018-10-12 15:49:13 +02:00
|
|
|
if not channel.has_private_key:
|
2018-10-08 16:41:07 +02:00
|
|
|
private_key = None
|
|
|
|
for account in private_key_accounts:
|
|
|
|
private_key = account.get_certificate_private_key(channel.ref)
|
|
|
|
if private_key is not None:
|
|
|
|
break
|
|
|
|
if private_key is None and exclude_without_key:
|
|
|
|
continue
|
2018-10-12 15:49:13 +02:00
|
|
|
channel.private_key = private_key
|
2018-10-08 16:41:07 +02:00
|
|
|
certificates.append(channel)
|
2018-09-19 04:23:41 +02:00
|
|
|
return certificates
|
2019-01-04 08:49:29 +01:00
|
|
|
|
|
|
|
async def release_all_outputs(self, account):
|
|
|
|
await self.db.execute(
|
|
|
|
"UPDATE txo SET is_reserved = 0 WHERE"
|
|
|
|
" is_reserved = 1 AND txo.address IN ("
|
|
|
|
" SELECT address from pubkey_address WHERE account = ?"
|
|
|
|
" )", [account.public_key.address]
|
|
|
|
)
|