update for beem: reworking wallet and key handling

in HiveDevs4 years ago

Repository

https://github.com/holgern/beem


beem-logo

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 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 or on PeakD

Sort:  

Very interesting what you do. Thank you for sharing and keeping us informed of all the updates. Python is wonderful!

I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!

La verdad muy explicito lo que has publicado, lo pondre en practica, gracias por compartir.

You do a great job. We want to thank you from our trenches and support you whenever we can You help to eliminate the plague. Receive our medieval thanks.

Great article, this would be a great one. Thank you for sharing some of this.

Another excellent tool. Cheers to you.

Hi @holger80, you have received a small bonus upvote from MAXUV.
This is to inform you that you now have new MPATH tokens in your Hive-Engine wallet.
Please read this post for more information.
Thanks for being a member of both MAXUV and MPATH!

The way the Hive community is growing the need of coding specialist will be increased. Nice way of giving lesson to young talent. Indeed learning about Beem and future will be great for young talent. Great effort.
Your post has been curated with @gitplait community account because this is the kind of publications we like to see in our community.

Join our Community on Hive and Chat with us on Discord.

[Gitplait

Thanks for sharing something like this it would be help to many person

Congratulations @holger80! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

Your post got the highest payout of the day

You can view your badges on your board And compare to others on the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Support the HiveBuzz project. Vote for our proposal!

Glad to see these updates to Beem. Wish the other libraries got similar love.