def create(): """ Create a new user. Returns the `user_id` of the newly created user. """ user_id = int(random() * (2**32 - 1)) MySQL.run('INSERT INTO user (user_id) VALUES ({})'.format(user_id)) return user_id
def add(user_id, address, xpub): """ Add the address info to the lookup table. """ query = ''' INSERT INTO address_lookup (user_id, address, xpub) VALUES ({}, '{}', '{}', '{}') '''.format(user_id, address, xpub) MySQL.run(query)
def create(user_id, platform, system_version, app_version, push_token): """ Create a new device for the user `user_id`. Returns the `device_id` of the newly created user device. """ device_id = int(random() * (2 ** 32 - 1)) query = ''' INSERT INTO user_device device_id, user_id, platform, system_version, app_version, push_token, last_seen VALUES ({}, {}, '{}', '{}', '{}', '{}', CURRENT_TIMESTAMP) '''.format(device_id, user_id, platform, system_version, app_version, push_token) MySQL.run(query) return device_id
def multi_add(user_id, addresses, offsets, xpub): """ Add multiple addresses info to the lookup table. """ query = ''' INSERT INTO address_lookup (user_id, address, xpub, offset) VALUES ''' values = [] for i in xrange(0, len(addresses)): address = addresses[i] offset = offsets[i] values.append("({}, '{}', '{}', {})".format(user_id, address, xpub, offset)) query += ", ".join(values) MySQL.run(query)
def get_all(user_id): """ Returns all the devices linked to the user `user_id`. """ res = MySQL.run('SELECT device_id, user_id, platform, system_version, app_version, push_token,' 'creation_time, last_seen FROM user_device WHERE user_id = {}'.format(user_id)) return [UserDevice(*device_data) for device_data in res]
def get(user_id): """ Returns the user `user_id`. """ res = MySQL.run('SELECT user_id, creation_time FROM user WHERE user_id = {}'.format(user_id)) if len(res) == 0: raise MyBitsException('User {} not found'.format(user_id)) return User(*res[0])
def multi_get(user_id, addresses): """ Returns the info of all the `addresses` that we have in the lookup table (restricted to a specific `user_id`) """ addresses = "', '".join(addresses) res = MySQL.run(''' SELECT address, user_id, xpub, offset, creation_time FROM address_lookup WHERE user_id = {} AND address IN ('{}')'''.format(user_id, addresses)) return [AddressLookup(*address_info) for address_info in res]
def get(device_id): """ Returns the device `device_id`. """ res = MySQL.run('SELECT device_id, user_id, platform, system_version, app_version, push_token,' 'creation_time, last_seen FROM user_device WHERE device_id = {}'.format(device_id)) if len(res) == 0: raise MyBitsException('Device {} not found'.format(device_id)) return UserDevice(*res[0])
def get_oldest(user_id, xpub): """ Returns the info of the oldest address for a specific `user_id` and `xpub` """ res = MySQL.run(''' SELECT address, user_id, xpub, offset, creation_time FROM address_lookup WHERE user_id = {} AND xpub = '{}' ORDER BY offset DESC LIMIT 1'''.format(user_id, xpub)) if len(res) == 0: return None return AddressLookup(*res[0])
def get(user_id, address): """ Returns all the info of where the address is coming from (restricted to a specific `user_id`) """ res = MySQL.run(''' SELECT address, user_id, xpub, offset, creation_time FROM address_lookup WHERE user_id = {} AND address = {}'''.format(user_id, address)) if len(res) == 0: return None return AddressLookup(*res[0])