taint

Crypto forensics for private use
git clone git://git.defalsify.org/taint.git
Log | Files | Refs | LICENSE

commit 335f62adfc1e00b38c73141ed89dfaa40580f36e
parent 5953f0609a707a31c9866134aaea99cbee656300
Author: nolash <dev@holbrook.no>
Date:   Fri, 16 Apr 2021 20:37:23 +0200

Add store get put, load accounts in interface

Diffstat:
Dtaint/store.py | 17-----------------
Ataint/store/base.py | 10++++++++++
Ataint/store/file.py | 46++++++++++++++++++++++++++++++++++++++++++++++
Mtaint/taint.py | 25+++++++++++++++++++------
Atests/test_interface.py | 36++++++++++++++++++++++++++++++++++++
5 files changed, 111 insertions(+), 23 deletions(-)

diff --git a/taint/store.py b/taint/store.py @@ -1,17 +0,0 @@ -class FileStore: - - def __init__(self, path): - self.path = path - self.initd = False - - - def save(self, data): - f = open(self.path, 'wb') - - l = len(data) - c = 0 - while c < l: - c += f.write(data[c:]) - f.close() - - self.initd = True diff --git a/taint/store/base.py b/taint/store/base.py @@ -0,0 +1,10 @@ +# external imports +from hexathon import even + + +def to_key(k): + if isinstance(k, bytes) or isinstance(k, bytearray): + k = k.hex() + else: + k = even(k) + return k diff --git a/taint/store/file.py b/taint/store/file.py @@ -0,0 +1,46 @@ +# standard imports +import os +import logging + +# local imports +from .base import to_key + +logg = logging.getLogger().getChild(__name__) + + +class FileStore: + + def __init__(self, base_dir): + os.makedirs(base_dir, exist_ok=True) + self.base_dir = base_dir + + + def put(self, k, v): + k = to_key(k) + filepath = os.path.join(self.base_dir, k) + + f = open(filepath, 'wb') + + l = len(v) + c = 0 + while c < l: + c += f.write(v[c:]) + f.close() + + + def get(self, k): + k = to_key(k) + filepath = os.path.join(self.base_dir, k) + + f = open(filepath, 'rb') + + b = b'' + c = -1 + while c != 0: + d = f.read(1024) + c = len(d) + b += d + + f.close() + + return b diff --git a/taint/taint.py b/taint/taint.py @@ -3,20 +3,21 @@ from hexathon import strip_0x # local imports from .cache import Cache +from .account import Account class Tainter(Cache): - def __init__(self, chain_spec, bits_size, result_handler=None, store_handler=None, cache_bloom=None): + def __init__(self, chain_spec, bits_size, result_handler=None, store=None, cache_bloom=None): super(Tainter, self).__init__(chain_spec, bits_size, cache_bloom=cache_bloom) - self.store_handler = store_handler + self.store = store self.result_handler = result_handler - def set_store(self, store): - self.store = store - if not store.initd and self.cache_bloom: - self.store.save(self.cache_bloom.serialize()) + #def set_store(self, store): + # self.store = store + # if not store.initd and self.cache_bloom: + # self.store.save(self.cache_bloom.serialize()) def add_account(self, account, label): @@ -42,3 +43,15 @@ class Tainter(Cache): self.result_handler.register(account) for account in objects: self.result_handler.register(account) + + + def save(self): + for account in self.subjects.values(): + self.store.put(account.account, account.serialize()) + + + def load_subject(self, k, label=None): + b = self.store.get(k) + a = Account.from_serialized(b, self.chain_spec, label) + self.subjects[k] = a + return True diff --git a/tests/test_interface.py b/tests/test_interface.py @@ -0,0 +1,36 @@ +# standard imports +import unittest +import logging +import os + +# test imports +from tests.base import TestBase +from taint.store.file import FileStore +from taint.taint import Tainter + + +class TestInterface(TestBase): + + def test_store(self): + self.store = FileStore(self.session_data_dir) + c = Tainter(self.chain_spec, self.bits_size, store=self.store) + c.add_subject(self.alice) + c.add_subject(self.bob) + c.save() + + for k in os.listdir(self.session_data_dir): + self.assertIn(bytes.fromhex(k), c.subjects) + + + c = Tainter(self.chain_spec, self.bits_size, store=self.store) + r = c.load_subject(self.alice.account) + self.assertTrue(r) + r = c.load_subject(self.bob.account) + self.assertTrue(r) + + self.assertTrue(self.alice.is_same(c.subjects[self.alice.account])) + self.assertTrue(self.bob.is_same(c.subjects[self.bob.account])) + + +if __name__ == '__main__': + unittest.main()