def wallet(username, password): key = f'{username}:{password}'.encode('utf-8') box = SecretBox(bytes.fromhex(get_multihash(key))) encrypted_data = box.encrypt(json.dumps({'test': True, 'opened': True}).encode('utf-8')) MockedWallet = type('MockedWallet', (Wallet,), { 'get_data': Mock(return_value=encrypted_data), 'persist_as_blocks': Mock(return_value=get_multihash(b'foobar')) }) return MockedWallet({'test': True, 'created': True}, get_multihash(key))
def get_wallets_box(): password = environ.get('WALLETS_PASSWORD', None) if password is None: raise ValueError( 'You should set a WALLETS_PASSWORD environment variable') password_multihash = get_multihash(password.encode('utf-8')) return SecretBox(bytes.fromhex(password_multihash))
def create(name): password = getpass(f'Your password for "{name}" Wallet: ') key = get_multihash(f'{name}:{password}'.encode('utf-8')) wallet = Wallet({}, key) multihash = wallet.persist() print(f'Wallet address: {multihash}') cli.add_to_wallets_list(name, multihash) cli.add_to_wallets_keys(name, key)
def multihash(self): return utils.get_multihash(self.type + self.data)
def test_open_wallet(wallet, username, password): opened_wallet = wallet.open(username, password, get_multihash(b'foobar')) assert 'test' in opened_wallet.data assert opened_wallet.data['test'] is True
def test_create_wallet(wallet, username, password): assert wallet.data['test'] is True key = f'{username}:{password}'.encode('utf-8') assert wallet.key == get_multihash(key)
def test_persist_wallet(wallet, username, password): multihash = wallet.persist() assert wallet.persist_as_blocks.call_count == 1 assert multihash == get_multihash(b'foobar')
def test_block_multihash(block): assert block.multihash == get_multihash(block.type + block.data)
def test_get_multihash(): mh = utils.get_multihash('a'.encode('utf-8')) assert mh == 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb' mh = utils.get_multihash('it is a simple test'.encode('utf-8')) assert mh == '032f99226ca013dd46ff749d5fa6399d88ba730152c7c94ca0f3feef1b7cac15'
def a_multihash(): return get_multihash(b'a')
def open(cls, name, password, multihash): key = get_multihash(f'{name}:{password}'.encode('utf-8')) return cls.open_with_key(key, multihash)