commit 065ee2dfde43b75933e2a148e5cb241005a2dfb0
parent df183c2de544d910df39799cb8323acea78412ed
Author: nolash <dev@holbrook.no>
Date: Fri, 16 Apr 2021 13:33:43 +0200
Add chain spec to salter
Diffstat:
5 files changed, 24 insertions(+), 11 deletions(-)
diff --git a/crypto_account_cache/account.py b/crypto_account_cache/account.py
@@ -8,7 +8,9 @@ from .crypto import Salter
class Account(Salter):
- def __init__(self, account, label=None, tags=[], create_digest=True):
+ def __init__(self, chain_spec, account, label=None, tags=[], create_digest=True):
+ super(Account, self).__init__(chain_spec)
+
if label == None:
label = str(account)
self.label = label
@@ -45,14 +47,14 @@ class Account(Salter):
@staticmethod
- def from_serialized(b, label=None):
+ def from_serialized(b, chain_spec, label=None):
l = len(b)
if l % 32 > 0:
raise ValueError('invalid data length; remainder {} of 32'.format(l % 32))
if l < 64:
raise ValueError('invalid data length; expected minimum 64, got {}'.format(l))
- a = Account(b[-32:], label=label, create_digest=False)
+ a = Account(chain_spec, b[-32:], label=label, create_digest=False)
a.tags.deserialize(b[:-32])
return a
diff --git a/crypto_account_cache/crypto.py b/crypto_account_cache/crypto.py
@@ -6,6 +6,12 @@ class Salter:
salt = os.urandom(32)
+ def __init__(self, chain_spec):
+ self.chain_spec = chain_spec
+ self.ionized_salt = self.salt
+ self.ionized_salt = self.sprinkle(str(chain_spec).encode('utf-8'))
+
+
def sprinkle(self, data):
h = hashlib.new('sha256')
if isinstance(data, list):
@@ -13,6 +19,6 @@ class Salter:
h.update(d)
else:
h.update(data)
- h.update(self.salt)
+ h.update(self.ionized_salt)
return h.digest()
diff --git a/tests/base.py b/tests/base.py
@@ -5,6 +5,9 @@ import logging
import tempfile
import shutil
+# external imports
+from chainlib.chain import ChainSpec
+
# local imports
from crypto_account_cache.account import Account
@@ -26,10 +29,12 @@ class TestBase(unittest.TestCase):
self.bits_size = BLOOM_BITS
self.bytes_size = (BLOOM_BITS - 1) / 8 + 1
- self.alice = Account(os.urandom(20), label='alice', tags=[b'inky'])
- self.bob = Account(os.urandom(20), label='bob', tags=[b'pinky'])
- self.eve = Account(os.urandom(20), label='eve', tags=[b'blinky'])
- self.mallory = Account(os.urandom(20), label='mallory', tags=[b'sue'])
+ self.chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
+
+ self.alice = Account(self.chain_spec, os.urandom(20), label='alice', tags=[b'inky'])
+ self.bob = Account(self.chain_spec, os.urandom(20), label='bob', tags=[b'pinky'])
+ self.eve = Account(self.chain_spec, os.urandom(20), label='eve', tags=[b'blinky'])
+ self.mallory = Account(self.chain_spec, os.urandom(20), label='mallory', tags=[b'sue'])
def tearDown(self):
diff --git a/tests/test_account.py b/tests/test_account.py
@@ -17,7 +17,7 @@ class TestAccount(TestBase):
alice_again = copy.copy(self.alice)
self.assertTrue(alice.is_same(alice_again))
- alice_alias = Account(self.alice.account_src)
+ alice_alias = Account(self.chain_spec, self.alice.account_src)
self.assertTrue(alice.is_same(alice_alias))
self.assertFalse(alice.is_same(self.bob))
@@ -36,7 +36,7 @@ class TestAccount(TestBase):
b = self.alice.serialize()
self.assertEqual(b[len(b)-32:], self.alice.account)
- new_alice = Account.from_serialized(b)
+ new_alice = Account.from_serialized(b, self.chain_spec)
self.assertTrue(new_alice.is_same(self.alice))
diff --git a/tests/test_cache.py b/tests/test_cache.py
@@ -42,7 +42,7 @@ class TestBasic(TestBase):
cache.add_subject(self.bob)
cache.add_object(self.eve)
cache.add_object(self.mallory)
- someaccount = Account(os.urandom(20))
+ someaccount = Account(self.chain_spec, os.urandom(20))
(subjects, objects) = cache.divide([self.alice.account, self.bob.account, self.eve.account, self.mallory.account, someaccount.account])