update for beem: reworking wallet and key handling

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@holger80·
0.000 HBD
update for beem: reworking wallet and key handling
## Repository

https://github.com/holgern/beem<center>
![beem-logo](https://cdn.steemitimages.com/DQmcRrwLPSywSYMierfP6um6mejeMNGjN9Rxw7audJqTDgb/beem-logo)
</center>

[beem](https://github.com/holgern/beem) is a python library and command line tool for HIVE.  The current version is 0.24.0.

There is also a discord channel for beem: https://discord.gg/4HM592V

The newest beem version can be installed by:

```
pip install -U beem
```

Check that you are using hive nodes. The following command 

```
beempy updatenodes --hive
```

updates the nodelist and uses only hive nodes. After setting hive as default_chain, `beempy updatenodes` can be used.

The list of nodes can be checked with

```
beempy config
```

and

```
beempy currentnode
```

shows the currently connected node.

## Changelog for versions 0.24.0
* new beemstorage module
* Config is handled by SqliteConfigurationStore or InRamConfigurationStore
* Keys are handled by SqliteEncryptedKeyStore or InRamPlainKeyStore
* Move aes to beemgraphenebase
* Wallet.keys, Wallet.keyStorage, Wallet.token and Wallet.keyMap has been removed
* Wallet.store has now the Key Interface that handles key management
* Token handling has been removed from Wallet
* Token storage has been move from wallet to SteemConnect/HiveSigner
* handle virtual ops batch streaming fixed thanks to @crokkon 

## Accessing stored config
```
from beemstorage import SqliteConfigurationStore
config = SqliteConfigurationStore()
print(config.get("default_account"))
```
returns `holger80`. I can now change the value with:
```
config["default_account"] = "test"
```
This value is now available in all beem instances. A `beempy config` returns now:
```
+-----------------------+---------------------------------------------+
| Key                   | Value                                       |
+-----------------------+---------------------------------------------+
| default_account       | test                                        |
```
.

The new beemstorage structure makes it easy to access and to modify the config parameters stored inside the sqlite database.

## Accessing stored keys
```
from beemstorage import SqliteConfigurationStore, SqliteEncryptedKeyStore
config = SqliteConfigurationStore()
key_store = SqliteEncryptedKeyStore(config=config)
print(key_store.items())
```
returns the public key and the encrypted wif of all stored keys. Encryption of the first stored key is done by:

```
key_store.unlock("wallet_phrase")
print(key_store.decrypt(key_store.items()[0][1]))
```
where `wallet_phrase` is the set masterpassword.
Modifying items of the key_store object will change them for all beem instances on the same PC.


## Use Config and Key storage without writing to file system
It is now possible to use a Config and Key store which does not access the beem.sqlite file. 
```
from beem import Hive
from beem.storage import generate_config_store
from beemstorage import InRamConfigurationStore, InRamPlainKeyStore
config = generate_config_store(InRamConfigurationStore)()
key_store = InRamPlainKeyStore()
hive = Hive(config_store=config, key_store=key_store)
key_store.add("5K7iVnYgTpG9DVBswdEETBqieCS7MwqhqnyXVN16UQmPJHBReet", "STM7fyvenZ9sJ2SGz5ctNMGzCMii82DR5GSkgPtDhfBVkaYBy8Qfq")
print(hive.wallet.getPublicKeys())
```
return
```
['STM7fyvenZ9sJ2SGz5ctNMGzCMii82DR5GSkgPtDhfBVkaYBy8Qfq']
```

It is also possible to use the `keys` parameter in `Hive`:
```
from beem import Hive
from beem.storage import generate_config_store
from beemstorage import InRamConfigurationStore
config = generate_config_store(InRamConfigurationStore)()
key_store = InRamPlainKeyStore()
hive = Hive(config_store=config, keys=["5K7iVnYgTpG9DVBswdEETBqieCS7MwqhqnyXVN16UQmPJHBReet"])
print(hive.wallet.getPublicKeys())
```
returns
```
['STM7fyvenZ9sJ2SGz5ctNMGzCMii82DR5GSkgPtDhfBVkaYBy8Qfq']
```

The `generate_config_store` function fills the InRamConfigurationStore with default values.


## Creating your own config and key storage
It is now also possible to create new key and config storages. E.g. a config/key storage that uses a mongo or a postgres database.

The following methods needs to be implemented for creating a new storage class:

  * ``def setdefault(cls, key, value)``
  * ``def __init__(self, *args, **kwargs)``
  * ``def __setitem__(self, key, value)``
  * ``def __getitem__(self, key)``
  * ``def __iter__(self)``
  * ``def __len__(self)``
  * ``def __contains__(self, key)``

You can take a look at the [sqlite.py](https://github.com/holgern/beem/blob/master/beemstorage/sqlite.py#L173) file which is an implementation for sqlite.

## HiveSigner token storage

Token storage for access tokens from HiveSigner and SteemConnect has been moved from the Wallet class to HiveSigner/Steemconnect.

HiveSigner uses the same masterpassword than the incrypted key storage. You can create a wallet and set a master password with
```
beempy createwallet
```
The following code stores a valid access token into the token storage and uses the stored token in a `me` API request:
```
from beem.hivesigner import HiveSigner
hs = HiveSigner(blockchain_instance=None)
hs.unlock("abc123")
hs.setToken({"holger80": "ey..."})
ret = hs.me(username="holger80")
print(ret["scope"])
```
returns
```
['login']
```
As the token is now stored in the sqlite file, it is also possible to list it with
```
beempy listtoken
```
which returns
```
+----------+-----------+--------+
| name     | scope     | status |
+----------+-----------+--------+
| holger80 | ['login'] | ok     |
+----------+-----------+--------+
```

HiveSigner tokens which have a broader scope than login can be used to broadcast transactions.

___

*If you like what I do, consider casting a vote for me as witness on [Hivesigner](https://hivesigner.com/sign/account-witness-vote?witness=holger80&approve=1) or on [PeakD](https://peakd.com/witnesses)*
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,