Add keystore test

This commit is contained in:
nolash 2021-10-10 11:33:30 +02:00
parent cd2d603164
commit 16308f736e
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
9 changed files with 117 additions and 13 deletions

View File

@ -0,0 +1,10 @@
{
"token": {
"reference": null,
"key_account": null
},
"token_index": {
"reference": null,
"key_account": null
}
}

View File

@ -9,10 +9,15 @@ logg = logging.getLogger(__name__)
class CICEth: class CICEth:
def __init__(self, reference, proofs, signer_hint=None): def __init__(self, resources, proofs, signer_hint=None):
self.reference = reference self.resources = resources
self.proofs = proofs self.proofs = proofs
self.signer_hint = signer_hint
def new(reference, proofs, signer_hint=None): def token_index(self):
return CICEth(reference, proofs, signer_hint=None) pass
def new(resources, proofs, signer_hint=None):
return CICEth(resources, proofs, signer_hint=None)

30
cic/keystore.py Normal file
View File

@ -0,0 +1,30 @@
# standard imports
import os
import json
import logging
# external imports
from funga.error import (
DecryptError,
KeyfileError,
)
from funga.keystore import Keystore
logg = logging.getLogger(__name__)
class KeystoreDirectory(Keystore):
def process_dir(self, path, password_retriever=None, default_password=''):
for v in os.listdir(path):
fp = os.path.join(path, v)
try:
self.import_keystore_file(fp, password=default_password)
except IsADirectoryError:
pass
except KeyfileError as e:
logg.warning('file {} could not be parsed as keyfile: {}'.format(fp, e))
except DecryptError as e:
if password_retriever == None:
raise e
password = password_retriever()
self.import_keystore_file(fp, password=password)

View File

@ -3,7 +3,10 @@ import os
import json import json
# local imports # local imports
from .base import * from .base import (
Data,
data_dir,
)
class Meta(Data): class Meta(Data):

View File

@ -3,7 +3,10 @@ import os
import json import json
# local imports # local imports
from .base import * from .base import (
Data,
data_dir,
)
class Network(Data): class Network(Data):
@ -31,15 +34,18 @@ class Network(Data):
def start(self): def start(self):
super(Network, self).load() super(Network, self).load()
network_template_file_path = os.path.join(data_dir, 'network_template_v{}.json'.format(self.version()))
f = open(network_template_file_path)
o_part = json.load(f)
f.close()
f = open(self.network_path, 'w') f = open(self.network_path, 'w')
o = {'references': {}} o = {'resources': {}}
for v in self.targets: for v in self.targets:
o['references'][v] = { o['resources'][v] = o_part
'token': None, json.dump(o, f)
'token_index': None,
}
json.dump(o, f)
f.close() f.close()

View File

@ -0,0 +1 @@
funga>=0.5.0a1,<0.6.0

47
tests/test_keyfile.py Normal file
View File

@ -0,0 +1,47 @@
# standard imports
import os
import unittest
import logging
# local imports
from cic.keystore import KeystoreDirectory
from funga.eth.keystore.dict import DictKeystore
from funga.error import DecryptError
from hexathon import uniform as hex_uniform
logging = logging.getLogger()
script_dir = os.path.dirname(os.path.realpath(__file__))
def pass_getter():
return 'test'
class EthKeystoreDirectory(DictKeystore, KeystoreDirectory):
pass
class TestKeyfile(unittest.TestCase):
def setUp(self):
self.path = os.path.join(script_dir, 'testdata', 'keystore')
self.keystore = EthKeystoreDirectory()
def test_keystore_bogus(self):
bogus_path = os.path.join(self.path, 'bogus')
self.keystore.process_dir(bogus_path)
def test_keystore_ok(self):
ok_path = os.path.join(self.path, 'ok')
with self.assertRaises(DecryptError):
self.keystore.process_dir(ok_path) # wrong password
self.keystore.process_dir(ok_path, default_password='test')
self.keystore.process_dir(ok_path, password_retriever=pass_getter)
self.assertTrue(hex_uniform('cc4f82F5DacDE395E1E0CFc4d62827C8B8B5688C') in self.keystore.list())
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1 @@
foo

1
tests/testdata/keystore/ok/key.json vendored Normal file
View File

@ -0,0 +1 @@
{"address": "cc4f82F5DacDE395E1E0CFc4d62827C8B8B5688C", "version": 3, "crypto": {"cipher": "aes-128-ctr", "ciphertext": "755c0911b7b4b36fab03f7c0238f29984db4a4d705b9a8aaeb5cd4a0066b586d", "cipherparams": {"iv": "61caf4396b9923f52c6a4a605a79fed5"}, "kdf": "scrypt", "kdfparams": {"dklen": 32, "n": 262144, "p": 1, "r": 8, "salt": "4a84bb019dfef7fb6d4fcfaa0be89f95bdd588d6df6827553da607366fee74b1"}, "mac": "fe551628027caaffaefefe20dd6f9150baca97e473bb238e7e3385c9cc6dd80f"}, "id": "95f197c8-29ab-11ec-aae8-02428a6d9287"}